blob: 689641f01e34ba342d9db949d30d9997a9cf2a77 [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,
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000125 "-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",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
Adam Tkacf586b842011-04-27 11:20:18 +0000163# Check whether VNC authentication is enabled, and if so, prompt the user to
164# create a VNC password if they don't already have one.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000165
Adam Tkacf586b842011-04-27 11:20:18 +0000166$securityTypeArgSpecified = 0;
167$vncAuthEnabled = 0;
168$passwordArgSpecified = 0;
169
170for ($i = 0; $i < @ARGV; ++$i) {
171 # -SecurityTypes can be followed by a space or "="
172 my @splitargs = split('=', $ARGV[$i]);
173 if (@splitargs <= 1 && $i < @ARGV - 1) {
174 push(@splitargs, $ARGV[$i + 1]);
175 }
176 if (lc(@splitargs[0]) eq "-securitytypes") {
177 if (@splitargs > 1) {
178 $securityTypeArgSpecified = 1;
179 }
180 foreach $arg2 (split(',', @splitargs[1])) {
181 if (lc($arg2) eq "vncauth" || lc($arg2) eq "tlsvnc"
182 || lc($arg2) eq "x509vnc") {
183 $vncAuthEnabled = 1;
184 }
185 }
186 }
187 if ((lc(@splitargs[0]) eq "-password")
188 || (lc(@splitargs[0]) eq "-passwordfile"
189 || (lc(@splitargs[0]) eq "-rfbauth"))) {
190 $passwordArgSpecified = 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000191 }
192}
193
Adam Tkacf586b842011-04-27 11:20:18 +0000194if ((!$securityTypeArgSpecified || $vncAuthEnabled) && !$passwordArgSpecified) {
195 ($z,$z,$mode) = stat("$vncUserDir/passwd");
196 if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
197 warn "\nYou will require a password to access your desktops.\n\n";
198 system($exedir."vncpasswd -q $vncUserDir/passwd");
199 if (($? >> 8) != 0) {
200 exit 1;
201 }
202 }
203}
204
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000205# Find display number.
206
207if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
208 $displayNumber = $1;
209 shift(@ARGV);
210 if (!&CheckDisplayNumber($displayNumber)) {
211 die "A VNC server is already running as :$displayNumber\n";
212 }
Adam Tkac39c9d992010-07-21 14:08:38 +0000213} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000214 &Usage();
215} else {
216 $displayNumber = &GetDisplayNumber();
217}
218
219$vncPort = 5900 + $displayNumber;
220
221$desktopLog = "$vncUserDir/$host:$displayNumber.log";
222unlink($desktopLog);
223
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000224# Make an X server cookie - use /dev/urandom on systems that have it,
225# otherwise use perl's random number generator, seeded with the sum
226# of the current time, our PID and part of the encrypted form of the password.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000227
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000228my $cookie = "";
229if (open(URANDOM, '<', '/dev/urandom')) {
230 my $randata;
231 if (sysread(URANDOM, $randata, 16) == 16) {
232 $cookie = unpack 'h*', $randata;
233 }
234 close(URANDOM);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000235}
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000236if ($cookie eq "") {
237 srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
238 for (1..16) {
239 $cookie .= sprintf("%02x", int(rand(256)) % 256);
240 }
241}
242
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000243system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
244system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
245
246if ($opt{'-name'}) {
247 $desktopName = $opt{'-name'};
248} else {
249 $desktopName = "$host:$displayNumber ($ENV{USER})";
250}
251
252# Now start the X VNC Server
253
DRC190854c2009-03-26 18:13:00 +0000254$cmd = $exedir."Xvnc :$displayNumber";
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000255$cmd .= " -desktop " . &quotedString($desktopName);
256$cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles);
257$cmd .= " -auth $xauthorityFile";
258$cmd .= " -geometry $geometry" if ($geometry);
259$cmd .= " -depth $depth" if ($depth);
260$cmd .= " -pixelformat $pixelformat" if ($pixelformat);
261$cmd .= " -rfbwait 30000";
262$cmd .= " -rfbauth $vncUserDir/passwd";
263$cmd .= " -rfbport $vncPort";
DRCd6821bf2009-03-26 18:17:49 +0000264$cmd .= " -fp $fontPath" if ($fontPath);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000265$cmd .= " -pn";
266
DRCd6821bf2009-03-26 18:17:49 +0000267# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000268#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000269# $cmd .= " -co /usr/lib/X11/rgb";
270#
271
272foreach $arg (@ARGV) {
273 $cmd .= " " . &quotedString($arg);
274}
275$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
276
277# Run $cmd and record the process ID.
278
279$pidFile = "$vncUserDir/$host:$displayNumber.pid";
280system("$cmd & echo \$! >$pidFile");
281
282# Give Xvnc a chance to start up
283
284sleep(3);
DRCd28792b2010-01-11 20:53:00 +0000285if ($fontPath ne $defFontPath) {
286 unless (kill 0, `cat $pidFile`) {
287 if ($fpArgSpecified) {
288 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
289 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
290 warn "determine an appropriate font path for this system and restart Xvnc using\n";
291 warn "that font path ...\n";
292 } else {
293 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
294 warn "catalog is not properly configured. Attempting to determine an appropriate\n";
295 warn "font path for this system and restart Xvnc using that font path ...\n";
296 }
297 $cmd =~ s@-fp [^ ]+@@;
298 $cmd .= " -fp $defFontPath" if ($defFontPath);
299 system("$cmd & echo \$! >$pidFile");
300 sleep(3);
DRC36546c12009-04-15 06:47:23 +0000301 }
DRCd6821bf2009-03-26 18:17:49 +0000302}
303unless (kill 0, `cat $pidFile`) {
304 warn "Could not start Xvnc.\n\n";
305 open(LOG, "<$desktopLog");
306 while (<LOG>) { print; }
307 close(LOG);
308 die "\n";
309}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000310
311warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
312
313# Create the user's xstartup script if necessary.
314
315if (!(-e "$vncUserDir/xstartup")) {
316 warn "Creating default startup script $vncUserDir/xstartup\n";
317 open(XSTARTUP, ">$vncUserDir/xstartup");
318 print XSTARTUP $defaultXStartup;
319 close(XSTARTUP);
320 chmod 0755, "$vncUserDir/xstartup";
321}
322
323# Run the X startup script.
324
325warn "Starting applications specified in $vncUserDir/xstartup\n";
326warn "Log file is $desktopLog\n\n";
327
328# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
329# TCP (DISPLAY=host:n)
330
331if (-e "/tmp/.X11-unix/X$displayNumber" ||
332 -e "/usr/spool/sockets/X11/$displayNumber")
333{
334 $ENV{DISPLAY}= ":$displayNumber";
335} else {
336 $ENV{DISPLAY}= "$host:$displayNumber";
337}
338$ENV{VNCDESKTOP}= $desktopName;
339
DRC8fb11912011-03-03 10:42:14 +0000340if ($opt{'-fg'}) {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000341 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1");
342 if (kill 0, `cat $pidFile`) {
343 $opt{'-kill'} = ':'.$displayNumber;
344 &Kill();
345 }
DRC8fb11912011-03-03 10:42:14 +0000346} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000347 if ($opt{'-autokill'}) {
348 system("($vncUserDir/xstartup; $0 -kill :$displayNumber) >> "
349 . &quotedString($desktopLog) . " 2>&1 &");
350 } else {
351 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog)
352 . " 2>&1 &");
353 }
DRC8fb11912011-03-03 10:42:14 +0000354}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000355
356exit;
357
358
359###############################################################################
360#
361# CheckGeometryAndDepth simply makes sure that the geometry and depth values
362# are sensible.
363#
364
365sub CheckGeometryAndDepth
366{
367 if ($geometry =~ /^(\d+)x(\d+)$/) {
368 $width = $1; $height = $2;
369
370 if (($width<1) || ($height<1)) {
371 die "$prog: geometry $geometry is invalid\n";
372 }
373
374 while (($width % 4)!=0) {
375 $width = $width + 1;
376 }
377
378 while (($height % 2)!=0) {
379 $height = $height + 1;
380 }
381
382 $geometry = "${width}x$height";
383 } else {
384 die "$prog: geometry $geometry is invalid\n";
385 }
386
DRCe5b4f752009-03-26 18:23:29 +0000387 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000388 die "Depth must be between 8 and 32\n";
389 }
390}
391
392
393#
394# GetDisplayNumber gets the lowest available display number. A display number
395# n is taken if something is listening on the VNC server port (5900+n) or the
396# X server port (6000+n).
397#
398
399sub GetDisplayNumber
400{
401 foreach $n (1..99) {
402 if (&CheckDisplayNumber($n)) {
403 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
404 }
405 }
406
407 die "$prog: no free display number on $host.\n";
408}
409
410
411#
412# CheckDisplayNumber checks if the given display number is available. A
413# display number n is taken if something is listening on the VNC server port
414# (5900+n) or the X server port (6000+n).
415#
416
417sub CheckDisplayNumber
418{
419 local ($n) = @_;
420
421 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
422 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
423 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
424 close(S);
425 return 0;
426 }
427 close(S);
428
429 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
430 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
431 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
432 close(S);
433 return 0;
434 }
435 close(S);
436
437 if (-e "/tmp/.X$n-lock") {
438 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
439 warn "Remove this file if there is no X server $host:$n\n";
440 return 0;
441 }
442
443 if (-e "/tmp/.X11-unix/X$n") {
444 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
445 warn "Remove this file if there is no X server $host:$n\n";
446 return 0;
447 }
448
449 if (-e "/usr/spool/sockets/X11/$n") {
450 warn("\nWarning: $host:$n is taken because of ".
451 "/usr/spool/sockets/X11/$n\n");
452 warn "Remove this file if there is no X server $host:$n\n";
453 return 0;
454 }
455
456 return 1;
457}
458
459
460#
461# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
462# format of the current X display being used. If successful, it sets the
463# options as appropriate so that the X VNC server will use the same settings
464# (minus an allowance for window manager decorations on the geometry). Using
465# the same depth and pixel format means that the VNC server won't have to
466# translate pixels when the desktop is being viewed on this X display (for
467# TrueColor displays anyway).
468#
469
470sub GetXDisplayDefaults
471{
472 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
473 $red, $green, $blue);
474
475 $wmDecorationWidth = 4; # a guess at typical size for window manager
476 $wmDecorationHeight = 24; # decoration size
477
478 return if (!defined($ENV{DISPLAY}));
479
480 @lines = `xdpyinfo 2>/dev/null`;
481
482 return if ($? != 0);
483
484 @matchlines = grep(/dimensions/, @lines);
485 if (@matchlines) {
486 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
487
488 $width -= $wmDecorationWidth;
489 $height -= $wmDecorationHeight;
490
491 $geometry = "${width}x$height";
492 }
493
494 @matchlines = grep(/default visual id/, @lines);
495 if (@matchlines) {
496 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
497
498 for ($i = 0; $i < @lines; $i++) {
499 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
500 if (($lines[$i+1] !~ /TrueColor/) ||
501 ($lines[$i+2] !~ /depth/) ||
502 ($lines[$i+4] !~ /red, green, blue masks/))
503 {
504 return;
505 }
506 last;
507 }
508 }
509
510 return if ($i >= @lines);
511
512 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
513 ($red,$green,$blue)
514 = ($lines[$i+4]
515 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
516
517 $red = hex($red);
518 $green = hex($green);
519 $blue = hex($blue);
520
521 if ($red > $blue) {
522 $red = int(log($red) / log(2)) - int(log($green) / log(2));
523 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
524 $blue = int(log($blue) / log(2)) + 1;
525 $pixelformat = "rgb$red$green$blue";
526 } else {
527 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
528 $green = int(log($green) / log(2)) - int(log($red) / log(2));
529 $red = int(log($red) / log(2)) + 1;
530 $pixelformat = "bgr$blue$green$red";
531 }
532 }
533}
534
535
536#
537# quotedString returns a string which yields the original string when parsed
538# by a shell.
539#
540
541sub quotedString
542{
543 local ($in) = @_;
544
545 $in =~ s/\'/\'\"\'\"\'/g;
546
547 return "'$in'";
548}
549
550
551#
552# removeSlashes turns slashes into underscores for use as a file name.
553#
554
555sub removeSlashes
556{
557 local ($in) = @_;
558
559 $in =~ s|/|_|g;
560
561 return "$in";
562}
563
564
565#
566# Usage
567#
568
569sub Usage
570{
571 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
572 " [-geometry <width>x<height>]\n".
573 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000574 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000575 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000576 " [-autokill]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000577 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000578 " $prog -kill <X-display>\n\n".
579 " $prog -list\n\n");
580}
581
582
583#
584# List
585#
586
587sub List
588{
589 opendir(dir, $vncUserDir);
590 my @filelist = readdir(dir);
591 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000592 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000593 print "X DISPLAY #\tPROCESS ID\n";
594 foreach my $file (@filelist) {
595 if ($file =~ /$host:(\d+)$\.pid/) {
596 print ":".$1."\t\t".`cat $vncUserDir/$file`;
597 }
598 }
599 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000600}
601
602
603#
604# Kill
605#
606
607sub Kill
608{
609 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
610
611 if ($opt{'-kill'} =~ /^:\d+$/) {
612 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
613 } else {
614 if ($opt{'-kill'} !~ /^$host:/) {
615 die "\nCan't tell if $opt{'-kill'} is on $host\n".
616 "Use -kill :<number> instead\n\n";
617 }
618 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
619 }
620
621 if (! -r $pidFile) {
622 die "\nCan't find file $pidFile\n".
623 "You'll have to kill the Xvnc process manually\n\n";
624 }
625
626 $SIG{'HUP'} = 'IGNORE';
627 chop($pid = `cat $pidFile`);
628 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000629
630 if (kill 0, $pid) {
631 system("kill $pid");
632 sleep(1);
633 if (kill 0, $pid) {
634 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
635 print " ".$0." -kill ".$opt{'-kill'}."\n";
636 print "to clean up the socket files.\n";
637 exit
638 }
639
640 } else {
641 warn "Xvnc process ID $pid already killed\n";
642 $opt{'-kill'} =~ s/://;
643
644 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
645 print "Xvnc did not appear to shut down cleanly.";
646 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
647 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
648 }
649 if (-e "/tmp/.X$opt{'-kill'}-lock") {
650 print "Xvnc did not appear to shut down cleanly.";
651 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
652 unlink "/tmp/.X$opt{'-kill'}-lock";
653 }
654 }
655
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000656 unlink $pidFile;
657 exit;
658}
659
660
661#
662# ParseOptions takes a list of possible options and a boolean indicating
663# whether the option has a value following, and sets up an associative array
664# %opt of the values of the options given on the command line. It removes all
665# the arguments it uses from @ARGV and returns them in @optArgs.
666#
667
668sub ParseOptions
669{
670 local (@optval) = @_;
671 local ($opt, @opts, %valFollows, @newargs);
672
673 while (@optval) {
674 $opt = shift(@optval);
675 push(@opts,$opt);
676 $valFollows{$opt} = shift(@optval);
677 }
678
679 @optArgs = ();
680 %opt = ();
681
682 arg: while (defined($arg = shift(@ARGV))) {
683 foreach $opt (@opts) {
684 if ($arg eq $opt) {
685 push(@optArgs, $arg);
686 if ($valFollows{$opt}) {
687 if (@ARGV == 0) {
688 &Usage();
689 }
690 $opt{$opt} = shift(@ARGV);
691 push(@optArgs, $opt{$opt});
692 } else {
693 $opt{$opt} = 1;
694 }
695 next arg;
696 }
697 }
698 push(@newargs,$arg);
699 }
700
701 @ARGV = @newargs;
702}
703
704
705#
706# Routine to make sure we're operating in a sane environment.
707#
708
709sub SanityCheck
710{
711 local ($cmd);
712
713 #
714 # Get the program name
715 #
716
717 ($prog) = ($0 =~ m|([^/]+)$|);
718
719 #
720 # Check we have all the commands we'll need on the path.
721 #
722
723 cmd:
DRC190854c2009-03-26 18:13:00 +0000724 foreach $cmd ("uname") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000725 for (split(/:/,$ENV{PATH})) {
726 if (-x "$_/$cmd") {
727 next cmd;
728 }
729 }
730 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
731 }
DRC190854c2009-03-26 18:13:00 +0000732 if (-x "/usr/X11R6/bin/xauth") {
733 $xauth = "/usr/X11R6/bin/xauth";
734 }
735 else {
736 cmd1:
737 foreach $cmd ("xauth") {
738 for (split(/:/,$ENV{PATH})) {
739 if (-x "$_/$cmd") {
740 next cmd1;
741 }
742 }
743 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
744 }
745 }
746
747 if($exedir eq "") {
748 cmd2:
749 foreach $cmd ("Xvnc","vncpasswd") {
750 for (split(/:/,$ENV{PATH})) {
751 if (-x "$_/$cmd") {
752 $vncClasses = "$_/../vnc/classes";
753 next cmd2;
754 }
755 }
756 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
757 }
758 }
759 else {
760 cmd3:
761 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
762 for (split(/:/,$ENV{PATH})) {
763 if (-x "$cmd") {
764 $vncClasses = $exedir."../vnc/classes";
765 next cmd3;
766 }
767 }
768 die "$prog: couldn't find \"$cmd\".\n";
769 }
770 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000771
772 #
773 # Check the HOME environment variable is set
774 #
775
776 if (!defined($ENV{HOME})) {
777 die "$prog: The HOME environment variable is not set.\n";
778 }
DRC190854c2009-03-26 18:13:00 +0000779# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000780
781 #
782 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
783 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
784 # we just guess at the values. If you find perl moaning here, just
785 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
786 # for your platform by looking in /usr/include/sys/socket.h and related
787 # files.
788 #
789
790 chop($os = `uname`);
791 chop($osrev = `uname -r`);
792
793 eval 'use Socket';
794 if ($@) {
795 eval 'require "sys/socket.ph"';
796 if ($@) {
797 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
798 $AF_INET = 2;
799 $SOCK_STREAM = 2;
800 } else {
801 $AF_INET = 2;
802 $SOCK_STREAM = 1;
803 }
804 } else {
805 $AF_INET = &AF_INET;
806 $SOCK_STREAM = &SOCK_STREAM;
807 }
808 } else {
809 $AF_INET = &AF_INET;
810 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000811 }
812}