You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncserver 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. #!/usr/bin/env perl
  2. #
  3. # Copyright (C) 2009-2010 D. R. Commander. All Rights Reserved.
  4. # Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
  5. # Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved.
  6. # Copyright (C) 2002-2005 RealVNC Ltd.
  7. # Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
  8. #
  9. # This is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This software is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this software; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  22. # USA.
  23. #
  24. #
  25. # vncserver - wrapper script to start an X VNC server.
  26. #
  27. #
  28. # First make sure we're operating in a sane environment.
  29. #
  30. $exedir = "";
  31. $slashndx = rindex($0, "/");
  32. if($slashndx>=0) {
  33. $exedir = substr($0, 0, $slashndx+1);
  34. }
  35. $vncClasses = "";
  36. $xauth = "xauth";
  37. &SanityCheck();
  38. #
  39. # Global variables. You may want to configure some of these for your site.
  40. #
  41. $geometry = "1024x768";
  42. #$depth = 16;
  43. $vncJavaFiles = (((-d "$vncClasses") && "$vncClasses") ||
  44. ((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
  45. ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes"));
  46. $vncUserDir = "$ENV{HOME}/.vnc";
  47. $xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
  48. $defaultXStartup
  49. = ("#!/bin/sh\n\n".
  50. "unset SESSION_MANAGER\n".
  51. "unset DBUS_SESSION_BUS_ADDRESS\n".
  52. "OS=`uname -s`\n".
  53. "if [ \$OS = 'Linux' ]; then\n".
  54. " case \"\$WINDOWMANAGER\" in\n".
  55. " \*gnome\*)\n".
  56. " if [ -e /etc/SuSE-release ]; then\n".
  57. " PATH=\$PATH:/opt/gnome/bin\n".
  58. " export PATH\n".
  59. " fi\n".
  60. " ;;\n".
  61. " esac\n".
  62. "fi\n".
  63. "if [ -x /etc/X11/xinit/xinitrc ]; then\n".
  64. " exec /etc/X11/xinit/xinitrc\n".
  65. "fi\n".
  66. "if [ -f /etc/X11/xinit/xinitrc ]; then\n".
  67. " exec sh /etc/X11/xinit/xinitrc\n".
  68. "fi\n".
  69. "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
  70. "xsetroot -solid grey\n".
  71. "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
  72. "twm &\n");
  73. chop($host = `uname -n`);
  74. if (-d "/etc/X11/fontpath.d") {
  75. $fontPath = "catalogue:/etc/X11/fontpath.d";
  76. }
  77. @fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/');
  78. if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');}
  79. if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');}
  80. if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');}
  81. push(@fontpaths, '/usr/share/fonts/default');
  82. @fonttypes = ('misc',
  83. '75dpi',
  84. '100dpi',
  85. 'Speedo',
  86. 'Type1');
  87. foreach $_fpath (@fontpaths) {
  88. foreach $_ftype (@fonttypes) {
  89. if (-f "$_fpath/$_ftype/fonts.dir") {
  90. if (! -l "$_fpath/$_ftype") {
  91. $defFontPath .= "$_fpath/$_ftype,";
  92. }
  93. }
  94. }
  95. }
  96. if ($defFontPath) {
  97. if (substr($defFontPath, -1, 1) == ',') {
  98. chop $defFontPath;
  99. }
  100. }
  101. if ($fontPath eq "") {
  102. $fontPath = $defFontPath;
  103. }
  104. # Check command line options
  105. &ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
  106. "-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",0);
  107. &Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
  108. &Kill() if ($opt{'-kill'});
  109. &List() if ($opt{'-list'});
  110. # Uncomment this line if you want default geometry, depth and pixelformat
  111. # to match the current X display:
  112. # &GetXDisplayDefaults();
  113. if ($opt{'-geometry'}) {
  114. $geometry = $opt{'-geometry'};
  115. }
  116. if ($opt{'-depth'}) {
  117. $depth = $opt{'-depth'};
  118. $pixelformat = "";
  119. }
  120. if ($opt{'-pixelformat'}) {
  121. $pixelformat = $opt{'-pixelformat'};
  122. }
  123. if ($opt{'-fp'}) {
  124. $fontPath = $opt{'-fp'};
  125. $fpArgSpecified = 1;
  126. }
  127. &CheckGeometryAndDepth();
  128. # Create the user's vnc directory if necessary.
  129. if (!(-e $vncUserDir)) {
  130. if (!mkdir($vncUserDir,0755)) {
  131. die "$prog: Could not create $vncUserDir.\n";
  132. }
  133. }
  134. # Check whether VNC authentication is enabled, and if so, prompt the user to
  135. # create a VNC password if they don't already have one.
  136. $securityTypeArgSpecified = 0;
  137. $vncAuthEnabled = 0;
  138. $passwordArgSpecified = 0;
  139. for ($i = 0; $i < @ARGV; ++$i) {
  140. # -SecurityTypes can be followed by a space or "="
  141. my @splitargs = split('=', $ARGV[$i]);
  142. if (@splitargs <= 1 && $i < @ARGV - 1) {
  143. push(@splitargs, $ARGV[$i + 1]);
  144. }
  145. if (lc(@splitargs[0]) eq "-securitytypes") {
  146. if (@splitargs > 1) {
  147. $securityTypeArgSpecified = 1;
  148. }
  149. foreach $arg2 (split(',', @splitargs[1])) {
  150. if (lc($arg2) eq "vncauth" || lc($arg2) eq "tlsvnc"
  151. || lc($arg2) eq "x509vnc") {
  152. $vncAuthEnabled = 1;
  153. }
  154. }
  155. }
  156. if ((lc(@splitargs[0]) eq "-password")
  157. || (lc(@splitargs[0]) eq "-passwordfile"
  158. || (lc(@splitargs[0]) eq "-rfbauth"))) {
  159. $passwordArgSpecified = 1;
  160. }
  161. }
  162. if ((!$securityTypeArgSpecified || $vncAuthEnabled) && !$passwordArgSpecified) {
  163. ($z,$z,$mode) = stat("$vncUserDir/passwd");
  164. if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
  165. warn "\nYou will require a password to access your desktops.\n\n";
  166. system($exedir."vncpasswd -q $vncUserDir/passwd");
  167. if (($? >> 8) != 0) {
  168. exit 1;
  169. }
  170. }
  171. }
  172. # Find display number.
  173. if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
  174. $displayNumber = $1;
  175. shift(@ARGV);
  176. if (!&CheckDisplayNumber($displayNumber)) {
  177. die "A VNC server is already running as :$displayNumber\n";
  178. }
  179. } elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) {
  180. &Usage();
  181. } else {
  182. $displayNumber = &GetDisplayNumber();
  183. }
  184. $vncPort = 5900 + $displayNumber;
  185. $desktopLog = "$vncUserDir/$host:$displayNumber.log";
  186. unlink($desktopLog);
  187. # Make an X server cookie - use /dev/urandom on systems that have it,
  188. # otherwise use perl's random number generator, seeded with the sum
  189. # of the current time, our PID and part of the encrypted form of the password.
  190. my $cookie = "";
  191. if (open(URANDOM, '<', '/dev/urandom')) {
  192. my $randata;
  193. if (sysread(URANDOM, $randata, 16) == 16) {
  194. $cookie = unpack 'h*', $randata;
  195. }
  196. close(URANDOM);
  197. }
  198. if ($cookie eq "") {
  199. srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
  200. for (1..16) {
  201. $cookie .= sprintf("%02x", int(rand(256)) % 256);
  202. }
  203. }
  204. system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
  205. system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
  206. if ($opt{'-name'}) {
  207. $desktopName = $opt{'-name'};
  208. } else {
  209. $desktopName = "$host:$displayNumber ($ENV{USER})";
  210. }
  211. # Now start the X VNC Server
  212. $cmd = $exedir."Xvnc :$displayNumber";
  213. $cmd .= " -desktop " . &quotedString($desktopName);
  214. $cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles);
  215. $cmd .= " -auth $xauthorityFile";
  216. $cmd .= " -geometry $geometry" if ($geometry);
  217. $cmd .= " -depth $depth" if ($depth);
  218. $cmd .= " -pixelformat $pixelformat" if ($pixelformat);
  219. $cmd .= " -rfbwait 30000";
  220. $cmd .= " -rfbauth $vncUserDir/passwd";
  221. $cmd .= " -rfbport $vncPort";
  222. $cmd .= " -fp $fontPath" if ($fontPath);
  223. $cmd .= " -pn";
  224. # Add color database stuff here, e.g.:
  225. #
  226. # $cmd .= " -co /usr/lib/X11/rgb";
  227. #
  228. foreach $arg (@ARGV) {
  229. $cmd .= " " . &quotedString($arg);
  230. }
  231. $cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
  232. # Run $cmd and record the process ID.
  233. $pidFile = "$vncUserDir/$host:$displayNumber.pid";
  234. system("$cmd & echo \$! >$pidFile");
  235. # Give Xvnc a chance to start up
  236. sleep(3);
  237. if ($fontPath ne $defFontPath) {
  238. unless (kill 0, `cat $pidFile`) {
  239. if ($fpArgSpecified) {
  240. warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
  241. warn "path you specified using the -fp argument is incorrect. Attempting to\n";
  242. warn "determine an appropriate font path for this system and restart Xvnc using\n";
  243. warn "that font path ...\n";
  244. } else {
  245. warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
  246. warn "catalog is not properly configured. Attempting to determine an appropriate\n";
  247. warn "font path for this system and restart Xvnc using that font path ...\n";
  248. }
  249. $cmd =~ s@-fp [^ ]+@@;
  250. $cmd .= " -fp $defFontPath" if ($defFontPath);
  251. system("$cmd & echo \$! >$pidFile");
  252. sleep(3);
  253. }
  254. }
  255. unless (kill 0, `cat $pidFile`) {
  256. warn "Could not start Xvnc.\n\n";
  257. open(LOG, "<$desktopLog");
  258. while (<LOG>) { print; }
  259. close(LOG);
  260. die "\n";
  261. }
  262. warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
  263. # Create the user's xstartup script if necessary.
  264. if (!(-e "$vncUserDir/xstartup")) {
  265. warn "Creating default startup script $vncUserDir/xstartup\n";
  266. open(XSTARTUP, ">$vncUserDir/xstartup");
  267. print XSTARTUP $defaultXStartup;
  268. close(XSTARTUP);
  269. chmod 0755, "$vncUserDir/xstartup";
  270. }
  271. # Run the X startup script.
  272. warn "Starting applications specified in $vncUserDir/xstartup\n";
  273. warn "Log file is $desktopLog\n\n";
  274. # If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
  275. # TCP (DISPLAY=host:n)
  276. if (-e "/tmp/.X11-unix/X$displayNumber" ||
  277. -e "/usr/spool/sockets/X11/$displayNumber")
  278. {
  279. $ENV{DISPLAY}= ":$displayNumber";
  280. } else {
  281. $ENV{DISPLAY}= "$host:$displayNumber";
  282. }
  283. $ENV{VNCDESKTOP}= $desktopName;
  284. system($exedir."vncconfig -iconic >> " . &quotedString($desktopLog) . " 2>&1 &");
  285. if ($opt{'-fg'}) {
  286. system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1");
  287. if (kill 0, `cat $pidFile`) {
  288. $opt{'-kill'} = ':'.$displayNumber;
  289. &Kill();
  290. }
  291. } else {
  292. if ($opt{'-autokill'}) {
  293. system("($vncUserDir/xstartup; $0 -kill :$displayNumber) >> "
  294. . &quotedString($desktopLog) . " 2>&1 &");
  295. } else {
  296. system("$vncUserDir/xstartup >> " . &quotedString($desktopLog)
  297. . " 2>&1 &");
  298. }
  299. }
  300. exit;
  301. ###############################################################################
  302. #
  303. # CheckGeometryAndDepth simply makes sure that the geometry and depth values
  304. # are sensible.
  305. #
  306. sub CheckGeometryAndDepth
  307. {
  308. if ($geometry =~ /^(\d+)x(\d+)$/) {
  309. $width = $1; $height = $2;
  310. if (($width<1) || ($height<1)) {
  311. die "$prog: geometry $geometry is invalid\n";
  312. }
  313. while (($width % 4)!=0) {
  314. $width = $width + 1;
  315. }
  316. while (($height % 2)!=0) {
  317. $height = $height + 1;
  318. }
  319. $geometry = "${width}x$height";
  320. } else {
  321. die "$prog: geometry $geometry is invalid\n";
  322. }
  323. if ($depth && (($depth < 8) || ($depth > 32))) {
  324. die "Depth must be between 8 and 32\n";
  325. }
  326. }
  327. #
  328. # GetDisplayNumber gets the lowest available display number. A display number
  329. # n is taken if something is listening on the VNC server port (5900+n) or the
  330. # X server port (6000+n).
  331. #
  332. sub GetDisplayNumber
  333. {
  334. foreach $n (1..99) {
  335. if (&CheckDisplayNumber($n)) {
  336. return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
  337. }
  338. }
  339. die "$prog: no free display number on $host.\n";
  340. }
  341. #
  342. # CheckDisplayNumber checks if the given display number is available. A
  343. # display number n is taken if something is listening on the VNC server port
  344. # (5900+n) or the X server port (6000+n).
  345. #
  346. sub CheckDisplayNumber
  347. {
  348. local ($n) = @_;
  349. socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
  350. eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
  351. if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
  352. close(S);
  353. return 0;
  354. }
  355. close(S);
  356. socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
  357. eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
  358. if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
  359. close(S);
  360. return 0;
  361. }
  362. close(S);
  363. if (-e "/tmp/.X$n-lock") {
  364. warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
  365. warn "Remove this file if there is no X server $host:$n\n";
  366. return 0;
  367. }
  368. if (-e "/tmp/.X11-unix/X$n") {
  369. warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
  370. warn "Remove this file if there is no X server $host:$n\n";
  371. return 0;
  372. }
  373. if (-e "/usr/spool/sockets/X11/$n") {
  374. warn("\nWarning: $host:$n is taken because of ".
  375. "/usr/spool/sockets/X11/$n\n");
  376. warn "Remove this file if there is no X server $host:$n\n";
  377. return 0;
  378. }
  379. return 1;
  380. }
  381. #
  382. # GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
  383. # format of the current X display being used. If successful, it sets the
  384. # options as appropriate so that the X VNC server will use the same settings
  385. # (minus an allowance for window manager decorations on the geometry). Using
  386. # the same depth and pixel format means that the VNC server won't have to
  387. # translate pixels when the desktop is being viewed on this X display (for
  388. # TrueColor displays anyway).
  389. #
  390. sub GetXDisplayDefaults
  391. {
  392. local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
  393. $red, $green, $blue);
  394. $wmDecorationWidth = 4; # a guess at typical size for window manager
  395. $wmDecorationHeight = 24; # decoration size
  396. return if (!defined($ENV{DISPLAY}));
  397. @lines = `xdpyinfo 2>/dev/null`;
  398. return if ($? != 0);
  399. @matchlines = grep(/dimensions/, @lines);
  400. if (@matchlines) {
  401. ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
  402. $width -= $wmDecorationWidth;
  403. $height -= $wmDecorationHeight;
  404. $geometry = "${width}x$height";
  405. }
  406. @matchlines = grep(/default visual id/, @lines);
  407. if (@matchlines) {
  408. ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
  409. for ($i = 0; $i < @lines; $i++) {
  410. if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
  411. if (($lines[$i+1] !~ /TrueColor/) ||
  412. ($lines[$i+2] !~ /depth/) ||
  413. ($lines[$i+4] !~ /red, green, blue masks/))
  414. {
  415. return;
  416. }
  417. last;
  418. }
  419. }
  420. return if ($i >= @lines);
  421. ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
  422. ($red,$green,$blue)
  423. = ($lines[$i+4]
  424. =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
  425. $red = hex($red);
  426. $green = hex($green);
  427. $blue = hex($blue);
  428. if ($red > $blue) {
  429. $red = int(log($red) / log(2)) - int(log($green) / log(2));
  430. $green = int(log($green) / log(2)) - int(log($blue) / log(2));
  431. $blue = int(log($blue) / log(2)) + 1;
  432. $pixelformat = "rgb$red$green$blue";
  433. } else {
  434. $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
  435. $green = int(log($green) / log(2)) - int(log($red) / log(2));
  436. $red = int(log($red) / log(2)) + 1;
  437. $pixelformat = "bgr$blue$green$red";
  438. }
  439. }
  440. }
  441. #
  442. # quotedString returns a string which yields the original string when parsed
  443. # by a shell.
  444. #
  445. sub quotedString
  446. {
  447. local ($in) = @_;
  448. $in =~ s/\'/\'\"\'\"\'/g;
  449. return "'$in'";
  450. }
  451. #
  452. # removeSlashes turns slashes into underscores for use as a file name.
  453. #
  454. sub removeSlashes
  455. {
  456. local ($in) = @_;
  457. $in =~ s|/|_|g;
  458. return "$in";
  459. }
  460. #
  461. # Usage
  462. #
  463. sub Usage
  464. {
  465. die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
  466. " [-geometry <width>x<height>]\n".
  467. " [-pixelformat rgbNNN|bgrNNN]\n".
  468. " [-fp <font-path>]\n".
  469. " [-fg]\n".
  470. " [-autokill]\n".
  471. " <Xvnc-options>...\n\n".
  472. " $prog -kill <X-display>\n\n".
  473. " $prog -list\n\n");
  474. }
  475. #
  476. # List
  477. #
  478. sub List
  479. {
  480. opendir(dir, $vncUserDir);
  481. my @filelist = readdir(dir);
  482. closedir(dir);
  483. print "\nTigerVNC server sessions:\n\n";
  484. print "X DISPLAY #\tPROCESS ID\n";
  485. foreach my $file (@filelist) {
  486. if ($file =~ /$host:(\d+)$\.pid/) {
  487. print ":".$1."\t\t".`cat $vncUserDir/$file`;
  488. }
  489. }
  490. exit 1;
  491. }
  492. #
  493. # Kill
  494. #
  495. sub Kill
  496. {
  497. $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
  498. if ($opt{'-kill'} =~ /^:\d+$/) {
  499. $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
  500. } else {
  501. if ($opt{'-kill'} !~ /^$host:/) {
  502. die "\nCan't tell if $opt{'-kill'} is on $host\n".
  503. "Use -kill :<number> instead\n\n";
  504. }
  505. $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
  506. }
  507. if (! -r $pidFile) {
  508. die "\nCan't find file $pidFile\n".
  509. "You'll have to kill the Xvnc process manually\n\n";
  510. }
  511. $SIG{'HUP'} = 'IGNORE';
  512. chop($pid = `cat $pidFile`);
  513. warn "Killing Xvnc process ID $pid\n";
  514. if (kill 0, $pid) {
  515. system("kill $pid");
  516. sleep(1);
  517. if (kill 0, $pid) {
  518. print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
  519. print " ".$0." -kill ".$opt{'-kill'}."\n";
  520. print "to clean up the socket files.\n";
  521. exit
  522. }
  523. } else {
  524. warn "Xvnc process ID $pid already killed\n";
  525. $opt{'-kill'} =~ s/://;
  526. if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
  527. print "Xvnc did not appear to shut down cleanly.";
  528. print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
  529. unlink "/tmp/.X11-unix/X$opt{'-kill'}";
  530. }
  531. if (-e "/tmp/.X$opt{'-kill'}-lock") {
  532. print "Xvnc did not appear to shut down cleanly.";
  533. print " Removing /tmp/.X$opt{'-kill'}-lock\n";
  534. unlink "/tmp/.X$opt{'-kill'}-lock";
  535. }
  536. }
  537. unlink $pidFile;
  538. exit;
  539. }
  540. #
  541. # ParseOptions takes a list of possible options and a boolean indicating
  542. # whether the option has a value following, and sets up an associative array
  543. # %opt of the values of the options given on the command line. It removes all
  544. # the arguments it uses from @ARGV and returns them in @optArgs.
  545. #
  546. sub ParseOptions
  547. {
  548. local (@optval) = @_;
  549. local ($opt, @opts, %valFollows, @newargs);
  550. while (@optval) {
  551. $opt = shift(@optval);
  552. push(@opts,$opt);
  553. $valFollows{$opt} = shift(@optval);
  554. }
  555. @optArgs = ();
  556. %opt = ();
  557. arg: while (defined($arg = shift(@ARGV))) {
  558. foreach $opt (@opts) {
  559. if ($arg eq $opt) {
  560. push(@optArgs, $arg);
  561. if ($valFollows{$opt}) {
  562. if (@ARGV == 0) {
  563. &Usage();
  564. }
  565. $opt{$opt} = shift(@ARGV);
  566. push(@optArgs, $opt{$opt});
  567. } else {
  568. $opt{$opt} = 1;
  569. }
  570. next arg;
  571. }
  572. }
  573. push(@newargs,$arg);
  574. }
  575. @ARGV = @newargs;
  576. }
  577. #
  578. # Routine to make sure we're operating in a sane environment.
  579. #
  580. sub SanityCheck
  581. {
  582. local ($cmd);
  583. #
  584. # Get the program name
  585. #
  586. ($prog) = ($0 =~ m|([^/]+)$|);
  587. #
  588. # Check we have all the commands we'll need on the path.
  589. #
  590. cmd:
  591. foreach $cmd ("uname") {
  592. for (split(/:/,$ENV{PATH})) {
  593. if (-x "$_/$cmd") {
  594. next cmd;
  595. }
  596. }
  597. die "$prog: couldn't find \"$cmd\" on your PATH.\n";
  598. }
  599. if (-x "/usr/X11R6/bin/xauth") {
  600. $xauth = "/usr/X11R6/bin/xauth";
  601. }
  602. else {
  603. cmd1:
  604. foreach $cmd ("xauth") {
  605. for (split(/:/,$ENV{PATH})) {
  606. if (-x "$_/$cmd") {
  607. next cmd1;
  608. }
  609. }
  610. die "$prog: couldn't find \"$cmd\" on your PATH.\n";
  611. }
  612. }
  613. if($exedir eq "") {
  614. cmd2:
  615. foreach $cmd ("Xvnc","vncpasswd") {
  616. for (split(/:/,$ENV{PATH})) {
  617. if (-x "$_/$cmd") {
  618. $vncClasses = "$_/../vnc/classes";
  619. next cmd2;
  620. }
  621. }
  622. die "$prog: couldn't find \"$cmd\" on your PATH.\n";
  623. }
  624. }
  625. else {
  626. cmd3:
  627. foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
  628. for (split(/:/,$ENV{PATH})) {
  629. if (-x "$cmd") {
  630. $vncClasses = $exedir."../vnc/classes";
  631. next cmd3;
  632. }
  633. }
  634. die "$prog: couldn't find \"$cmd\".\n";
  635. }
  636. }
  637. #
  638. # Check the HOME environment variable is set
  639. #
  640. if (!defined($ENV{HOME})) {
  641. die "$prog: The HOME environment variable is not set.\n";
  642. }
  643. # chdir($ENV{HOME});
  644. #
  645. # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
  646. # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
  647. # we just guess at the values. If you find perl moaning here, just
  648. # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
  649. # for your platform by looking in /usr/include/sys/socket.h and related
  650. # files.
  651. #
  652. chop($os = `uname`);
  653. chop($osrev = `uname -r`);
  654. eval 'use Socket';
  655. if ($@) {
  656. eval 'require "sys/socket.ph"';
  657. if ($@) {
  658. if (($os eq "SunOS") && ($osrev !~ /^4/)) {
  659. $AF_INET = 2;
  660. $SOCK_STREAM = 2;
  661. } else {
  662. $AF_INET = 2;
  663. $SOCK_STREAM = 1;
  664. }
  665. } else {
  666. $AF_INET = &AF_INET;
  667. $SOCK_STREAM = &SOCK_STREAM;
  668. }
  669. } else {
  670. $AF_INET = &AF_INET;
  671. $SOCK_STREAM = &SOCK_STREAM;
  672. }
  673. }