vncserver refactor: use standard way of calling functions

For somebody coming from Python/Ruby/JavaScript, foo() is easier to
understand than &foo. Also, less noise.
This commit is contained in:
Dmitry Maksyoma 2021-10-14 21:41:40 +13:00
parent ee3b7f9aa5
commit b34fe08786

View File

@ -36,29 +36,29 @@ use constant {
OPTIONAL_ARG_VALUE => 2 OPTIONAL_ARG_VALUE => 2
}; };
&CheckWeCanRunInThisEnvironment(); CheckWeCanRunInThisEnvironment();
&DefineFilePathsAndStuff(); DefineFilePathsAndStuff();
&ParseAndProcessCliOptions(); ParseAndProcessCliOptions();
&PrepareLoggingAndXvncKillingFramework(); PrepareLoggingAndXvncKillingFramework();
&DisableLegacyVncAuth(); DisableLegacyVncAuth();
&AllowXProgramsToConnectToXvnc(); AllowXProgramsToConnectToXvnc();
&EnsureAtLeastOneKasmUserExists(); EnsureAtLeastOneKasmUserExists();
&AskUserToChooseDeOrManualXstartup(); AskUserToChooseDeOrManualXstartup();
&PrepareDefaultsForPassingToXvnc(); PrepareDefaultsForPassingToXvnc();
&StartXvncOrExit(); StartXvncOrExit();
&PrintLogFilenameAndConfiguredUsersAndStuff(); PrintLogFilenameAndConfiguredUsersAndStuff();
&CreateUserConfigIfNeeded(); CreateUserConfigIfNeeded();
if (! $skipxstartup) { if (! $skipxstartup) {
&CreateXstartupIfNeeded(); CreateXstartupIfNeeded();
&RunXstartup(); RunXstartup();
} }
PrintBrowserUrl(); PrintBrowserUrl();
@ -144,7 +144,7 @@ sub CheckGeometryAndDepthAreSensible
sub GetLowestAvailableDisplayNumber sub GetLowestAvailableDisplayNumber
{ {
foreach $n (1..99) { foreach $n (1..99) {
if (&CheckVncIsntRunningOnDisplay($n)) { if (CheckVncIsntRunningOnDisplay($n)) {
return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
} }
} }
@ -334,7 +334,7 @@ sub List
foreach my $file (@filelist) { foreach my $file (@filelist) {
if ($file =~ /$host:(\d+)$\.pid/) { if ($file =~ /$host:(\d+)$\.pid/) {
chop($tmp_pid = `cat $vncUserDir/$file`); chop($tmp_pid = `cat $vncUserDir/$file`);
if (&IsProcessRunning($tmp_pid)) { if (IsProcessRunning($tmp_pid)) {
print ":".$1."\t\t".`cat $vncUserDir/$file`; print ":".$1."\t\t".`cat $vncUserDir/$file`;
} else { } else {
unlink ($vncUserDir . "/" . $file); unlink ($vncUserDir . "/" . $file);
@ -372,12 +372,12 @@ sub Kill
chop($pid = `cat $pidFile`); chop($pid = `cat $pidFile`);
warn "Killing Xvnc process ID $pid\n"; warn "Killing Xvnc process ID $pid\n";
if (&IsProcessRunning($pid)) { if (IsProcessRunning($pid)) {
system("kill $pid"); system("kill $pid");
&WaitForTimeLimitOrSubReturningTrue(1, sub { WaitForTimeLimitOrSubReturningTrue(1, sub {
!IsProcessRunning($pid) !IsProcessRunning($pid)
}); });
if (&IsProcessRunning($pid)) { if (IsProcessRunning($pid)) {
print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n"; print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
print " ".$0." -kill ".$opt{'-kill'}."\n"; print " ".$0." -kill ".$opt{'-kill'}."\n";
print "to clean up the socket files.\n"; print "to clean up the socket files.\n";
@ -437,7 +437,7 @@ sub ParseOptionsAndRemoveMatchesFromARGV
} }
case REQUIRED_ARG_VALUE { case REQUIRED_ARG_VALUE {
if (@ARGV == 0) { if (@ARGV == 0) {
&Usage(); Usage();
} }
$opt{$opt} = shift(@ARGV); $opt{$opt} = shift(@ARGV);
@ -547,7 +547,7 @@ sub CheckRequiredDependenciesArePresent
$SOCK_STREAM = &SOCK_STREAM; $SOCK_STREAM = &SOCK_STREAM;
} }
&CheckUserHasAccessToDefaultCertOnDebian(); CheckUserHasAccessToDefaultCertOnDebian();
} }
sub IsDebian { sub IsDebian {
@ -555,7 +555,7 @@ sub IsDebian {
} }
sub CheckUserHasAccessToDefaultCertOnDebian { sub CheckUserHasAccessToDefaultCertOnDebian {
if (!&IsDebian()) { if (!IsDebian()) {
return; return;
} }
@ -623,26 +623,26 @@ sub DetectAndExportDisplay {
sub RunXstartup { sub RunXstartup {
warn "Starting applications specified in $xstartupFile\n"; warn "Starting applications specified in $xstartupFile\n";
&DetectAndExportDisplay(); DetectAndExportDisplay();
$ENV{VNCDESKTOP}= $desktopName; $ENV{VNCDESKTOP}= $desktopName;
if ($opt{'-fg'}) { if ($opt{'-fg'}) {
if (! $skipxstartup) { if (! $skipxstartup) {
system("$xstartupFile >> " . &quotedString($desktopLog) . " 2>&1"); system("$xstartupFile >> " . quotedString($desktopLog) . " 2>&1");
} }
if (&IsXvncRunning()) { if (IsXvncRunning()) {
$opt{'-kill'} = ':'.$displayNumber; $opt{'-kill'} = ':'.$displayNumber;
&Kill(); Kill();
} }
} else { } else {
if ($opt{'-autokill'}) { if ($opt{'-autokill'}) {
if (! $skipxstartup) { if (! $skipxstartup) {
system("($xstartupFile; $0 -kill :$displayNumber) >> " system("($xstartupFile; $0 -kill :$displayNumber) >> "
. &quotedString($desktopLog) . " 2>&1 &"); . quotedString($desktopLog) . " 2>&1 &");
} }
} else { } else {
if (! $skipxstartup) { if (! $skipxstartup) {
system("$xstartupFile >> " . &quotedString($desktopLog) system("$xstartupFile >> " . quotedString($desktopLog)
. " 2>&1 &"); . " 2>&1 &");
} }
} }
@ -702,15 +702,15 @@ sub DetectFontPath {
} }
sub ProcessCliOptions { sub ProcessCliOptions {
&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'}); Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
&Kill() if ($opt{'-kill'}); Kill() if ($opt{'-kill'});
&List() if ($opt{'-list'}); List() if ($opt{'-list'});
# Uncomment this line if you want default geometry, depth and pixelformat # Uncomment this line if you want default geometry, depth and pixelformat
# to match the current X display: # to match the current X display:
# &GetXDisplayDefaults(); # GetXDisplayDefaults();
if ($opt{'-geometry'}) { if ($opt{'-geometry'}) {
$geometry = $opt{'-geometry'}; $geometry = $opt{'-geometry'};
@ -751,13 +751,13 @@ sub DeWasSelectedEarlier {
} }
sub AskUserToChooseDeOrManualXstartup { sub AskUserToChooseDeOrManualXstartup {
if (&DeWasSelectedEarlier() && !$opt{'-select-de'}) { if (DeWasSelectedEarlier() && !$opt{'-select-de'}) {
return; return;
} }
&ForgetSelectedDe(); ForgetSelectedDe();
$selectDeCmd = &ConstructSelectDeCmd(); $selectDeCmd = ConstructSelectDeCmd();
system($selectDeCmd) == 0 || die("Failed to execute $selectDeCmd\n"); system($selectDeCmd) == 0 || die("Failed to execute $selectDeCmd\n");
} }
@ -788,7 +788,7 @@ sub DetectDisplayNumberFromCliArgs {
if ($ARGV[0] =~ /^:(\d+)$/) { if ($ARGV[0] =~ /^:(\d+)$/) {
$displayNumber = $1; $displayNumber = $1;
shift(@ARGV); shift(@ARGV);
if (!&CheckVncIsntRunningOnDisplay($displayNumber)) { if (!CheckVncIsntRunningOnDisplay($displayNumber)) {
die "A VNC server is already running as :$displayNumber\n"; die "A VNC server is already running as :$displayNumber\n";
} }
} }
@ -801,8 +801,8 @@ sub CheckCliOptionsForBeingValid {
return; return;
} }
if (! &IsCliOption($ARGV[0])) { if (! IsCliOption($ARGV[0])) {
&Usage(); Usage();
} }
} }
@ -813,8 +813,8 @@ sub IsCliOption {
} }
sub SetReasonabeDefaults { sub SetReasonabeDefaults {
$default_opts{desktop} = &quotedString($desktopName); $default_opts{desktop} = quotedString($desktopName);
$default_opts{auth} = &quotedString($xauthorityFile); $default_opts{auth} = quotedString($xauthorityFile);
$default_opts{geometry} = $geometry if ($geometry); $default_opts{geometry} = $geometry if ($geometry);
$default_opts{depth} = $depth if ($depth); $default_opts{depth} = $depth if ($depth);
$default_opts{pixelformat} = $pixelformat if ($pixelformat); $default_opts{pixelformat} = $pixelformat if ($pixelformat);
@ -849,7 +849,7 @@ sub DisableLegacyVncAuth() {
} }
sub TellUserToSetupUserAndPassword { sub TellUserToSetupUserAndPassword {
if (&AtLeastOneUserConfigured()) { if (AtLeastOneUserConfigured()) {
return; return;
} }
@ -869,7 +869,7 @@ Let's create a user.
NEEDTOCREATEUSER NEEDTOCREATEUSER
my $username = &Prompt("Enter username (default: $defaultUser): "); my $username = Prompt("Enter username (default: $defaultUser): ");
$username ||= $defaultUser; $username ||= $defaultUser;
system($exedir."kasmvncpasswd -u \"$username\" -w $kasmPasswdFile"); system($exedir."kasmvncpasswd -u \"$username\" -w $kasmPasswdFile");
@ -938,7 +938,7 @@ sub MakeXCookie {
} }
sub SetupXauthorityFile { sub SetupXauthorityFile {
my $cookie = &MakeXCookie(); my $cookie = MakeXCookie();
open(XAUTH, "|xauth -f $xauthorityFile source -"); open(XAUTH, "|xauth -f $xauthorityFile source -");
print XAUTH "add $host:$displayNumber . $cookie\n"; print XAUTH "add $host:$displayNumber . $cookie\n";
@ -962,12 +962,12 @@ sub ConstructXvncCmd {
# $cmd .= " -co /usr/lib/X11/rgb"; # $cmd .= " -co /usr/lib/X11/rgb";
foreach $arg (@ARGV) { foreach $arg (@ARGV) {
$cmd .= " " . &quotedString($arg); $cmd .= " " . quotedString($arg);
} }
$cmd .= SwallowedArgs(); $cmd .= SwallowedArgs();
$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1"; $cmd .= " >> " . quotedString($desktopLog) . " 2>&1";
return $cmd; return $cmd;
} }
@ -976,7 +976,7 @@ sub SwallowedArgs {
my $cmd; my $cmd;
if ($opt{"-interface"}) { if ($opt{"-interface"}) {
$cmd .= " " . "-interface " . &quotedString($opt{"-interface"}); $cmd .= " " . "-interface " . quotedString($opt{"-interface"});
} }
$cmd; $cmd;
@ -1003,11 +1003,11 @@ sub StartXvncWithSafeFontPath {
} }
$cmd =~ s@-fp [^ ]+@@; $cmd =~ s@-fp [^ ]+@@;
$cmd .= " -fp $defFontPath" if ($defFontPath); $cmd .= " -fp $defFontPath" if ($defFontPath);
&StartXvncAndRecordPID(); StartXvncAndRecordPID();
} }
sub IsXvncRunning { sub IsXvncRunning {
&IsProcessRunning(`cat $pidFile`); IsProcessRunning(`cat $pidFile`);
} }
sub WarnUserXvncNotStartedAndExit { sub WarnUserXvncNotStartedAndExit {
@ -1024,7 +1024,7 @@ sub WaitForXvncToRespond {
my $sleptFor = 0; my $sleptFor = 0;
my $sleepLimit = 3; my $sleepLimit = 3;
until (&IsXvncResponding() || $sleptFor >= $sleepLimit) { until (IsXvncResponding() || $sleptFor >= $sleepLimit) {
sleep($sleepSlice); sleep($sleepSlice);
$sleptFor += $sleepSlice; $sleptFor += $sleepSlice;
} }
@ -1060,8 +1060,8 @@ sub PrintKasmUsers {
} }
sub CheckWeCanRunInThisEnvironment { sub CheckWeCanRunInThisEnvironment {
$exedir = &DetectBinariesDir(); $exedir = DetectBinariesDir();
&CheckRequiredDependenciesArePresent(); CheckRequiredDependenciesArePresent();
} }
sub DefineFilePathsAndStuff { sub DefineFilePathsAndStuff {
@ -1076,7 +1076,7 @@ sub DefineFilePathsAndStuff {
$vncUserConfig = "$vncUserDir/config"; $vncUserConfig = "$vncUserDir/config";
$kasmPasswdFile = "$ENV{HOME}/.kasmpasswd"; $kasmPasswdFile = "$ENV{HOME}/.kasmpasswd";
$selectDeBin = &DetectSelectDeBin(); $selectDeBin = DetectSelectDeBin();
$de_was_selected_file="$ENV{HOME}/.vnc/.de-was-selected"; $de_was_selected_file="$ENV{HOME}/.vnc/.de-was-selected";
$vncSystemConfigDir = "/etc/kasmvnc"; $vncSystemConfigDir = "/etc/kasmvnc";
@ -1102,17 +1102,17 @@ sub DefineFilePathsAndStuff {
chop($host = `uname -n`); chop($host = `uname -n`);
chop($hostIP = `hostname -i`); chop($hostIP = `hostname -i`);
&DetectFontPath(); DetectFontPath();
} }
sub ParseAndProcessCliOptions { sub ParseAndProcessCliOptions {
my @supportedOptions = ("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,"-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",0,"-noxstartup",0,"-xstartup",1,"-select-de",OPTIONAL_ARG_VALUE, "-interface", REQUIRED_ARG_VALUE, '-debug', NO_ARG_VALUE); my @supportedOptions = ("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,"-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",0,"-noxstartup",0,"-xstartup",1,"-select-de",OPTIONAL_ARG_VALUE, "-interface", REQUIRED_ARG_VALUE, '-debug', NO_ARG_VALUE);
&ParseOptionsAndRemoveMatchesFromARGV(@supportedOptions); ParseOptionsAndRemoveMatchesFromARGV(@supportedOptions);
&ProcessCliOptions(); ProcessCliOptions();
&CheckGeometryAndDepthAreSensible(); CheckGeometryAndDepthAreSensible();
$displayNumber = &DetectDisplayNumberFromCliArgs() || $displayNumber = DetectDisplayNumberFromCliArgs() ||
&GetLowestAvailableDisplayNumber(); GetLowestAvailableDisplayNumber();
&CheckCliOptionsForBeingValid(); CheckCliOptionsForBeingValid();
} }
sub CheckBrowserHostDefined { sub CheckBrowserHostDefined {
@ -1129,33 +1129,33 @@ sub PrepareDefaultsForPassingToXvnc {
$websocketPort = $defaultWebsocketPort + $displayNumber; $websocketPort = $defaultWebsocketPort + $displayNumber;
$desktopName = $opt{'-name'} || "$host:$displayNumber ($ENV{USER})"; $desktopName = $opt{'-name'} || "$host:$displayNumber ($ENV{USER})";
&SetReasonabeDefaults(); SetReasonabeDefaults();
&LoadSystemThenUserThenMandatoryConfigs(); LoadSystemThenUserThenMandatoryConfigs();
CheckBrowserHostDefined(); CheckBrowserHostDefined();
} }
sub EnsureAtLeastOneKasmUserExists { sub EnsureAtLeastOneKasmUserExists {
@kasmPasswdUsers = &LoadKasmPasswdUsers(); @kasmPasswdUsers = LoadKasmPasswdUsers();
if (!&AtLeastOneUserConfigured()) { if (!AtLeastOneUserConfigured()) {
&GuideUserToSetupKasmPasswdUser(); GuideUserToSetupKasmPasswdUser();
@kasmPasswdUsers = &LoadKasmPasswdUsers(); @kasmPasswdUsers = LoadKasmPasswdUsers();
} }
} }
sub StartXvncOrExit { sub StartXvncOrExit {
$cmd = &ConstructXvncCmd(); $cmd = ConstructXvncCmd();
&DeleteLogLeftFromPreviousXvncRun(); DeleteLogLeftFromPreviousXvncRun();
&StartXvncAndRecordPID(); StartXvncAndRecordPID();
&WaitForXvncToRespond(); WaitForXvncToRespond();
if (!&IsXvncRunning() && !&UsingSafeFontPath()) { if (!IsXvncRunning() && !UsingSafeFontPath()) {
&StartXvncWithSafeFontPath(); StartXvncWithSafeFontPath();
&WaitForXvncToRespond(); WaitForXvncToRespond();
} }
unless (&IsXvncRunning()) { unless (IsXvncRunning()) {
&WarnUserXvncNotStartedAndExit(); WarnUserXvncNotStartedAndExit();
} }
} }
@ -1183,17 +1183,17 @@ sub DefineLogAndPidFilesForDisplayNumber {
} }
sub PrepareLoggingAndXvncKillingFramework { sub PrepareLoggingAndXvncKillingFramework {
&CreateDotVncDir(); CreateDotVncDir();
&DefineLogAndPidFilesForDisplayNumber(); DefineLogAndPidFilesForDisplayNumber();
} }
sub AllowXProgramsToConnectToXvnc { sub AllowXProgramsToConnectToXvnc {
&SetupXauthorityFile(); SetupXauthorityFile();
} }
sub PrintLogFilenameAndConfiguredUsersAndStuff { sub PrintLogFilenameAndConfiguredUsersAndStuff {
warn "\nNew '$desktopName' desktop is $host:$displayNumber\n"; warn "\nNew '$desktopName' desktop is $host:$displayNumber\n";
&PrintKasmUsers(); PrintKasmUsers();
warn "Log file is $desktopLog\n\n"; warn "Log file is $desktopLog\n\n";
} }
@ -1233,10 +1233,10 @@ sub IsThisSystemBinary {
} }
sub DetectSelectDeBin { sub DetectSelectDeBin {
if (&IsThisSystemBinary()) { if (IsThisSystemBinary()) {
"/usr/lib/kasmvncserver/select-de.sh"; "/usr/lib/kasmvncserver/select-de.sh";
} else { } else {
&LocalSelectDePath(); LocalSelectDePath();
} }
} }