blob: 0fbed190c745669c813f9deabed1630e9c8f5980 [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001#!/usr/bin/env perl
2#
DRCb9d8e762011-02-09 08:24:58 +00003# Copyright (C) 2009-2010 D. R. Commander. All Rights Reserved.
DRC190854c2009-03-26 18:13:00 +00004# Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
5# Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00006# 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#
26# vncserver - wrapper script to start an X VNC server.
27#
28
29#
30# First make sure we're operating in a sane environment.
31#
32
DRC190854c2009-03-26 18:13:00 +000033$exedir = "";
34$slashndx = rindex($0, "/");
35if($slashndx>=0) {
36 $exedir = substr($0, 0, $slashndx+1);
37}
38
39$vncClasses = "";
40
41$xauth = "xauth";
42
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000043&SanityCheck();
44
45#
46# Global variables. You may want to configure some of these for your site.
47#
48
49$geometry = "1024x768";
DRC9d1c1572009-03-26 18:18:51 +000050#$depth = 16;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000051$vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
DRC190854c2009-03-26 18:13:00 +000052 ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes") ||
53 ((-d "$vncClasses") && "$vncClasses"));
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000054$vncUserDir = "$ENV{HOME}/.vnc";
55$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
56
57$defaultXStartup
58 = ("#!/bin/sh\n\n".
Adam Tkac804d86f2009-04-03 14:33:51 +000059 "vncconfig -iconic &\n".
DRC93248982009-03-26 18:14:38 +000060 "unset SESSION_MANAGER\n".
Adam Tkacc071e492009-05-20 09:01:24 +000061 "unset DBUS_SESSION_BUS_ADDRESS\n".
DRC93248982009-03-26 18:14:38 +000062 "OS=`uname -s`\n".
63 "if [ \$OS = 'Linux' ]; then\n".
64 " case \"\$WINDOWMANAGER\" in\n".
65 " \*gnome\*)\n".
66 " if [ -e /etc/SuSE-release ]; then\n".
67 " PATH=\$PATH:/opt/gnome/bin\n".
68 " export PATH\n".
69 " fi\n".
70 " ;;\n".
71 " esac\n".
72 "fi\n".
73 "if [ -x /etc/X11/xinit/xinitrc ]; then\n".
74 " exec /etc/X11/xinit/xinitrc\n".
75 "fi\n".
76 "if [ -f /etc/X11/xinit/xinitrc ]; then\n".
77 " exec sh /etc/X11/xinit/xinitrc\n".
78 "fi\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000079 "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
80 "xsetroot -solid grey\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000081 "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
82 "twm &\n");
83
84chop($host = `uname -n`);
85
DRC989dbd12009-04-22 13:23:17 +000086if (-d "/etc/X11/fontpath.d") {
87 $fontPath = "catalogue:/etc/X11/fontpath.d";
88}
DRC36546c12009-04-15 06:47:23 +000089
DRCd6821bf2009-03-26 18:17:49 +000090@fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/');
91if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');}
92if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');}
93if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');}
94push(@fontpaths, '/usr/share/fonts/default');
95
96@fonttypes = ('misc',
97 '75dpi',
98 '100dpi',
99 'Speedo',
100 'Type1');
101
102foreach $_fpath (@fontpaths) {
103 foreach $_ftype (@fonttypes) {
104 if (-f "$_fpath/$_ftype/fonts.dir") {
105 if (! -l "$_fpath/$_ftype") {
DRC36546c12009-04-15 06:47:23 +0000106 $defFontPath .= "$_fpath/$_ftype,";
DRCd6821bf2009-03-26 18:17:49 +0000107 }
108 }
109 }
110}
DRCd28792b2010-01-11 20:53:00 +0000111
DRC36546c12009-04-15 06:47:23 +0000112if ($defFontPath) {
113 if (substr($defFontPath, -1, 1) == ',') {
114 chop $defFontPath;
DRCd6821bf2009-03-26 18:17:49 +0000115 }
116}
117
DRCd28792b2010-01-11 20:53:00 +0000118if ($fontPath eq "") {
119 $fontPath = $defFontPath;
120}
121
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000122# Check command line options
123
124&ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
DRC8fb11912011-03-03 10:42:14 +0000125 "-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000126
127&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
128
129&Kill() if ($opt{'-kill'});
130
DRCb9d8e762011-02-09 08:24:58 +0000131&List() if ($opt{'-list'});
132
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000133# Uncomment this line if you want default geometry, depth and pixelformat
134# to match the current X display:
135# &GetXDisplayDefaults();
136
137if ($opt{'-geometry'}) {
138 $geometry = $opt{'-geometry'};
139}
140if ($opt{'-depth'}) {
141 $depth = $opt{'-depth'};
142 $pixelformat = "";
143}
144if ($opt{'-pixelformat'}) {
145 $pixelformat = $opt{'-pixelformat'};
146}
DRCeed5d1f2009-03-26 19:16:19 +0000147if ($opt{'-fp'}) {
148 $fontPath = $opt{'-fp'};
DRC36546c12009-04-15 06:47:23 +0000149 $fpArgSpecified = 1;
DRCeed5d1f2009-03-26 19:16:19 +0000150}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000151
152&CheckGeometryAndDepth();
153
154
155# Create the user's vnc directory if necessary.
156
157if (!(-e $vncUserDir)) {
158 if (!mkdir($vncUserDir,0755)) {
159 die "$prog: Could not create $vncUserDir.\n";
160 }
161}
162
163# Make sure the user has a password.
164
165($z,$z,$mode) = stat("$vncUserDir/passwd");
166if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
167 warn "\nYou will require a password to access your desktops.\n\n";
DRC190854c2009-03-26 18:13:00 +0000168 system($exedir."vncpasswd -q $vncUserDir/passwd");
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000169 if (($? >> 8) != 0) {
170 exit 1;
171 }
172}
173
174# Find display number.
175
176if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
177 $displayNumber = $1;
178 shift(@ARGV);
179 if (!&CheckDisplayNumber($displayNumber)) {
180 die "A VNC server is already running as :$displayNumber\n";
181 }
Adam Tkac39c9d992010-07-21 14:08:38 +0000182} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000183 &Usage();
184} else {
185 $displayNumber = &GetDisplayNumber();
186}
187
188$vncPort = 5900 + $displayNumber;
189
190$desktopLog = "$vncUserDir/$host:$displayNumber.log";
191unlink($desktopLog);
192
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000193# Make an X server cookie - use /dev/urandom on systems that have it,
194# otherwise use perl's random number generator, seeded with the sum
195# of the current time, our PID and part of the encrypted form of the password.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000196
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000197my $cookie = "";
198if (open(URANDOM, '<', '/dev/urandom')) {
199 my $randata;
200 if (sysread(URANDOM, $randata, 16) == 16) {
201 $cookie = unpack 'h*', $randata;
202 }
203 close(URANDOM);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000204}
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000205if ($cookie eq "") {
206 srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
207 for (1..16) {
208 $cookie .= sprintf("%02x", int(rand(256)) % 256);
209 }
210}
211
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000212system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
213system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
214
215if ($opt{'-name'}) {
216 $desktopName = $opt{'-name'};
217} else {
218 $desktopName = "$host:$displayNumber ($ENV{USER})";
219}
220
221# Now start the X VNC Server
222
DRC190854c2009-03-26 18:13:00 +0000223$cmd = $exedir."Xvnc :$displayNumber";
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000224$cmd .= " -desktop " . &quotedString($desktopName);
225$cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles);
226$cmd .= " -auth $xauthorityFile";
227$cmd .= " -geometry $geometry" if ($geometry);
228$cmd .= " -depth $depth" if ($depth);
229$cmd .= " -pixelformat $pixelformat" if ($pixelformat);
230$cmd .= " -rfbwait 30000";
231$cmd .= " -rfbauth $vncUserDir/passwd";
232$cmd .= " -rfbport $vncPort";
DRCd6821bf2009-03-26 18:17:49 +0000233$cmd .= " -fp $fontPath" if ($fontPath);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000234$cmd .= " -pn";
235
DRCd6821bf2009-03-26 18:17:49 +0000236# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000237#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000238# $cmd .= " -co /usr/lib/X11/rgb";
239#
240
241foreach $arg (@ARGV) {
242 $cmd .= " " . &quotedString($arg);
243}
244$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
245
246# Run $cmd and record the process ID.
247
248$pidFile = "$vncUserDir/$host:$displayNumber.pid";
249system("$cmd & echo \$! >$pidFile");
250
251# Give Xvnc a chance to start up
252
253sleep(3);
DRCd28792b2010-01-11 20:53:00 +0000254if ($fontPath ne $defFontPath) {
255 unless (kill 0, `cat $pidFile`) {
256 if ($fpArgSpecified) {
257 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
258 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
259 warn "determine an appropriate font path for this system and restart Xvnc using\n";
260 warn "that font path ...\n";
261 } else {
262 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
263 warn "catalog is not properly configured. Attempting to determine an appropriate\n";
264 warn "font path for this system and restart Xvnc using that font path ...\n";
265 }
266 $cmd =~ s@-fp [^ ]+@@;
267 $cmd .= " -fp $defFontPath" if ($defFontPath);
268 system("$cmd & echo \$! >$pidFile");
269 sleep(3);
DRC36546c12009-04-15 06:47:23 +0000270 }
DRCd6821bf2009-03-26 18:17:49 +0000271}
272unless (kill 0, `cat $pidFile`) {
273 warn "Could not start Xvnc.\n\n";
274 open(LOG, "<$desktopLog");
275 while (<LOG>) { print; }
276 close(LOG);
277 die "\n";
278}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000279
280warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
281
282# Create the user's xstartup script if necessary.
283
284if (!(-e "$vncUserDir/xstartup")) {
285 warn "Creating default startup script $vncUserDir/xstartup\n";
286 open(XSTARTUP, ">$vncUserDir/xstartup");
287 print XSTARTUP $defaultXStartup;
288 close(XSTARTUP);
289 chmod 0755, "$vncUserDir/xstartup";
290}
291
292# Run the X startup script.
293
294warn "Starting applications specified in $vncUserDir/xstartup\n";
295warn "Log file is $desktopLog\n\n";
296
297# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
298# TCP (DISPLAY=host:n)
299
300if (-e "/tmp/.X11-unix/X$displayNumber" ||
301 -e "/usr/spool/sockets/X11/$displayNumber")
302{
303 $ENV{DISPLAY}= ":$displayNumber";
304} else {
305 $ENV{DISPLAY}= "$host:$displayNumber";
306}
307$ENV{VNCDESKTOP}= $desktopName;
308
DRC8fb11912011-03-03 10:42:14 +0000309if ($opt{'-fg'}) {
310 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1");
311 if (kill 0, `cat $pidFile`) {
312 $opt{'-kill'} = ':'.$displayNumber;
313 &Kill();
314 }
315} else {
316 system("($vncUserDir/xstartup; $0 -kill :$displayNumber) >> " . &quotedString($desktopLog) . " 2>&1 &");
317}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000318
319exit;
320
321
322###############################################################################
323#
324# CheckGeometryAndDepth simply makes sure that the geometry and depth values
325# are sensible.
326#
327
328sub CheckGeometryAndDepth
329{
330 if ($geometry =~ /^(\d+)x(\d+)$/) {
331 $width = $1; $height = $2;
332
333 if (($width<1) || ($height<1)) {
334 die "$prog: geometry $geometry is invalid\n";
335 }
336
337 while (($width % 4)!=0) {
338 $width = $width + 1;
339 }
340
341 while (($height % 2)!=0) {
342 $height = $height + 1;
343 }
344
345 $geometry = "${width}x$height";
346 } else {
347 die "$prog: geometry $geometry is invalid\n";
348 }
349
DRCe5b4f752009-03-26 18:23:29 +0000350 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000351 die "Depth must be between 8 and 32\n";
352 }
353}
354
355
356#
357# GetDisplayNumber gets the lowest available display number. A display number
358# n is taken if something is listening on the VNC server port (5900+n) or the
359# X server port (6000+n).
360#
361
362sub GetDisplayNumber
363{
364 foreach $n (1..99) {
365 if (&CheckDisplayNumber($n)) {
366 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
367 }
368 }
369
370 die "$prog: no free display number on $host.\n";
371}
372
373
374#
375# CheckDisplayNumber checks if the given display number is available. A
376# display number n is taken if something is listening on the VNC server port
377# (5900+n) or the X server port (6000+n).
378#
379
380sub CheckDisplayNumber
381{
382 local ($n) = @_;
383
384 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
385 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
386 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
387 close(S);
388 return 0;
389 }
390 close(S);
391
392 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
393 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
394 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
395 close(S);
396 return 0;
397 }
398 close(S);
399
400 if (-e "/tmp/.X$n-lock") {
401 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
402 warn "Remove this file if there is no X server $host:$n\n";
403 return 0;
404 }
405
406 if (-e "/tmp/.X11-unix/X$n") {
407 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
408 warn "Remove this file if there is no X server $host:$n\n";
409 return 0;
410 }
411
412 if (-e "/usr/spool/sockets/X11/$n") {
413 warn("\nWarning: $host:$n is taken because of ".
414 "/usr/spool/sockets/X11/$n\n");
415 warn "Remove this file if there is no X server $host:$n\n";
416 return 0;
417 }
418
419 return 1;
420}
421
422
423#
424# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
425# format of the current X display being used. If successful, it sets the
426# options as appropriate so that the X VNC server will use the same settings
427# (minus an allowance for window manager decorations on the geometry). Using
428# the same depth and pixel format means that the VNC server won't have to
429# translate pixels when the desktop is being viewed on this X display (for
430# TrueColor displays anyway).
431#
432
433sub GetXDisplayDefaults
434{
435 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
436 $red, $green, $blue);
437
438 $wmDecorationWidth = 4; # a guess at typical size for window manager
439 $wmDecorationHeight = 24; # decoration size
440
441 return if (!defined($ENV{DISPLAY}));
442
443 @lines = `xdpyinfo 2>/dev/null`;
444
445 return if ($? != 0);
446
447 @matchlines = grep(/dimensions/, @lines);
448 if (@matchlines) {
449 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
450
451 $width -= $wmDecorationWidth;
452 $height -= $wmDecorationHeight;
453
454 $geometry = "${width}x$height";
455 }
456
457 @matchlines = grep(/default visual id/, @lines);
458 if (@matchlines) {
459 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
460
461 for ($i = 0; $i < @lines; $i++) {
462 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
463 if (($lines[$i+1] !~ /TrueColor/) ||
464 ($lines[$i+2] !~ /depth/) ||
465 ($lines[$i+4] !~ /red, green, blue masks/))
466 {
467 return;
468 }
469 last;
470 }
471 }
472
473 return if ($i >= @lines);
474
475 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
476 ($red,$green,$blue)
477 = ($lines[$i+4]
478 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
479
480 $red = hex($red);
481 $green = hex($green);
482 $blue = hex($blue);
483
484 if ($red > $blue) {
485 $red = int(log($red) / log(2)) - int(log($green) / log(2));
486 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
487 $blue = int(log($blue) / log(2)) + 1;
488 $pixelformat = "rgb$red$green$blue";
489 } else {
490 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
491 $green = int(log($green) / log(2)) - int(log($red) / log(2));
492 $red = int(log($red) / log(2)) + 1;
493 $pixelformat = "bgr$blue$green$red";
494 }
495 }
496}
497
498
499#
500# quotedString returns a string which yields the original string when parsed
501# by a shell.
502#
503
504sub quotedString
505{
506 local ($in) = @_;
507
508 $in =~ s/\'/\'\"\'\"\'/g;
509
510 return "'$in'";
511}
512
513
514#
515# removeSlashes turns slashes into underscores for use as a file name.
516#
517
518sub removeSlashes
519{
520 local ($in) = @_;
521
522 $in =~ s|/|_|g;
523
524 return "$in";
525}
526
527
528#
529# Usage
530#
531
532sub Usage
533{
534 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
535 " [-geometry <width>x<height>]\n".
536 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000537 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000538 " [-fg]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000539 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000540 " $prog -kill <X-display>\n\n".
541 " $prog -list\n\n");
542}
543
544
545#
546# List
547#
548
549sub List
550{
551 opendir(dir, $vncUserDir);
552 my @filelist = readdir(dir);
553 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000554 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000555 print "X DISPLAY #\tPROCESS ID\n";
556 foreach my $file (@filelist) {
557 if ($file =~ /$host:(\d+)$\.pid/) {
558 print ":".$1."\t\t".`cat $vncUserDir/$file`;
559 }
560 }
561 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000562}
563
564
565#
566# Kill
567#
568
569sub Kill
570{
571 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
572
573 if ($opt{'-kill'} =~ /^:\d+$/) {
574 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
575 } else {
576 if ($opt{'-kill'} !~ /^$host:/) {
577 die "\nCan't tell if $opt{'-kill'} is on $host\n".
578 "Use -kill :<number> instead\n\n";
579 }
580 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
581 }
582
583 if (! -r $pidFile) {
584 die "\nCan't find file $pidFile\n".
585 "You'll have to kill the Xvnc process manually\n\n";
586 }
587
588 $SIG{'HUP'} = 'IGNORE';
589 chop($pid = `cat $pidFile`);
590 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000591
592 if (kill 0, $pid) {
593 system("kill $pid");
594 sleep(1);
595 if (kill 0, $pid) {
596 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
597 print " ".$0." -kill ".$opt{'-kill'}."\n";
598 print "to clean up the socket files.\n";
599 exit
600 }
601
602 } else {
603 warn "Xvnc process ID $pid already killed\n";
604 $opt{'-kill'} =~ s/://;
605
606 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
607 print "Xvnc did not appear to shut down cleanly.";
608 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
609 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
610 }
611 if (-e "/tmp/.X$opt{'-kill'}-lock") {
612 print "Xvnc did not appear to shut down cleanly.";
613 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
614 unlink "/tmp/.X$opt{'-kill'}-lock";
615 }
616 }
617
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000618 unlink $pidFile;
619 exit;
620}
621
622
623#
624# ParseOptions takes a list of possible options and a boolean indicating
625# whether the option has a value following, and sets up an associative array
626# %opt of the values of the options given on the command line. It removes all
627# the arguments it uses from @ARGV and returns them in @optArgs.
628#
629
630sub ParseOptions
631{
632 local (@optval) = @_;
633 local ($opt, @opts, %valFollows, @newargs);
634
635 while (@optval) {
636 $opt = shift(@optval);
637 push(@opts,$opt);
638 $valFollows{$opt} = shift(@optval);
639 }
640
641 @optArgs = ();
642 %opt = ();
643
644 arg: while (defined($arg = shift(@ARGV))) {
645 foreach $opt (@opts) {
646 if ($arg eq $opt) {
647 push(@optArgs, $arg);
648 if ($valFollows{$opt}) {
649 if (@ARGV == 0) {
650 &Usage();
651 }
652 $opt{$opt} = shift(@ARGV);
653 push(@optArgs, $opt{$opt});
654 } else {
655 $opt{$opt} = 1;
656 }
657 next arg;
658 }
659 }
660 push(@newargs,$arg);
661 }
662
663 @ARGV = @newargs;
664}
665
666
667#
668# Routine to make sure we're operating in a sane environment.
669#
670
671sub SanityCheck
672{
673 local ($cmd);
674
675 #
676 # Get the program name
677 #
678
679 ($prog) = ($0 =~ m|([^/]+)$|);
680
681 #
682 # Check we have all the commands we'll need on the path.
683 #
684
685 cmd:
DRC190854c2009-03-26 18:13:00 +0000686 foreach $cmd ("uname") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000687 for (split(/:/,$ENV{PATH})) {
688 if (-x "$_/$cmd") {
689 next cmd;
690 }
691 }
692 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
693 }
DRC190854c2009-03-26 18:13:00 +0000694 if (-x "/usr/X11R6/bin/xauth") {
695 $xauth = "/usr/X11R6/bin/xauth";
696 }
697 else {
698 cmd1:
699 foreach $cmd ("xauth") {
700 for (split(/:/,$ENV{PATH})) {
701 if (-x "$_/$cmd") {
702 next cmd1;
703 }
704 }
705 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
706 }
707 }
708
709 if($exedir eq "") {
710 cmd2:
711 foreach $cmd ("Xvnc","vncpasswd") {
712 for (split(/:/,$ENV{PATH})) {
713 if (-x "$_/$cmd") {
714 $vncClasses = "$_/../vnc/classes";
715 next cmd2;
716 }
717 }
718 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
719 }
720 }
721 else {
722 cmd3:
723 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
724 for (split(/:/,$ENV{PATH})) {
725 if (-x "$cmd") {
726 $vncClasses = $exedir."../vnc/classes";
727 next cmd3;
728 }
729 }
730 die "$prog: couldn't find \"$cmd\".\n";
731 }
732 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000733
734 #
735 # Check the HOME environment variable is set
736 #
737
738 if (!defined($ENV{HOME})) {
739 die "$prog: The HOME environment variable is not set.\n";
740 }
DRC190854c2009-03-26 18:13:00 +0000741# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000742
743 #
744 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
745 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
746 # we just guess at the values. If you find perl moaning here, just
747 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
748 # for your platform by looking in /usr/include/sys/socket.h and related
749 # files.
750 #
751
752 chop($os = `uname`);
753 chop($osrev = `uname -r`);
754
755 eval 'use Socket';
756 if ($@) {
757 eval 'require "sys/socket.ph"';
758 if ($@) {
759 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
760 $AF_INET = 2;
761 $SOCK_STREAM = 2;
762 } else {
763 $AF_INET = 2;
764 $SOCK_STREAM = 1;
765 }
766 } else {
767 $AF_INET = &AF_INET;
768 $SOCK_STREAM = &SOCK_STREAM;
769 }
770 } else {
771 $AF_INET = &AF_INET;
772 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000773 }
774}