blob: 85140350dbe4cec7a4dffe43a373349d041af84d [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;
DRCe85eba52011-10-04 06:57:19 +000051$vncJavaFiles = (((-d "$vncClasses") && "$vncClasses") ||
52 ((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
53 ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes"));
54
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000055$vncUserDir = "$ENV{HOME}/.vnc";
56$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
57
58$defaultXStartup
59 = ("#!/bin/sh\n\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
DRCf6b58402011-10-05 21:28:03 +0000340system($exedir."vncconfig -iconic >> " . &quotedString($desktopLog) . " 2>&1 &");
341
DRC8fb11912011-03-03 10:42:14 +0000342if ($opt{'-fg'}) {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000343 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1");
344 if (kill 0, `cat $pidFile`) {
345 $opt{'-kill'} = ':'.$displayNumber;
346 &Kill();
347 }
DRC8fb11912011-03-03 10:42:14 +0000348} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000349 if ($opt{'-autokill'}) {
350 system("($vncUserDir/xstartup; $0 -kill :$displayNumber) >> "
351 . &quotedString($desktopLog) . " 2>&1 &");
352 } else {
353 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog)
354 . " 2>&1 &");
355 }
DRC8fb11912011-03-03 10:42:14 +0000356}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000357
358exit;
359
360
361###############################################################################
362#
363# CheckGeometryAndDepth simply makes sure that the geometry and depth values
364# are sensible.
365#
366
367sub CheckGeometryAndDepth
368{
369 if ($geometry =~ /^(\d+)x(\d+)$/) {
370 $width = $1; $height = $2;
371
372 if (($width<1) || ($height<1)) {
373 die "$prog: geometry $geometry is invalid\n";
374 }
375
376 while (($width % 4)!=0) {
377 $width = $width + 1;
378 }
379
380 while (($height % 2)!=0) {
381 $height = $height + 1;
382 }
383
384 $geometry = "${width}x$height";
385 } else {
386 die "$prog: geometry $geometry is invalid\n";
387 }
388
DRCe5b4f752009-03-26 18:23:29 +0000389 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000390 die "Depth must be between 8 and 32\n";
391 }
392}
393
394
395#
396# GetDisplayNumber gets the lowest available display number. A display number
397# n is taken if something is listening on the VNC server port (5900+n) or the
398# X server port (6000+n).
399#
400
401sub GetDisplayNumber
402{
403 foreach $n (1..99) {
404 if (&CheckDisplayNumber($n)) {
405 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
406 }
407 }
408
409 die "$prog: no free display number on $host.\n";
410}
411
412
413#
414# CheckDisplayNumber checks if the given display number is available. A
415# display number n is taken if something is listening on the VNC server port
416# (5900+n) or the X server port (6000+n).
417#
418
419sub CheckDisplayNumber
420{
421 local ($n) = @_;
422
423 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
424 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
425 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
426 close(S);
427 return 0;
428 }
429 close(S);
430
431 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
432 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
433 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
434 close(S);
435 return 0;
436 }
437 close(S);
438
439 if (-e "/tmp/.X$n-lock") {
440 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
441 warn "Remove this file if there is no X server $host:$n\n";
442 return 0;
443 }
444
445 if (-e "/tmp/.X11-unix/X$n") {
446 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
447 warn "Remove this file if there is no X server $host:$n\n";
448 return 0;
449 }
450
451 if (-e "/usr/spool/sockets/X11/$n") {
452 warn("\nWarning: $host:$n is taken because of ".
453 "/usr/spool/sockets/X11/$n\n");
454 warn "Remove this file if there is no X server $host:$n\n";
455 return 0;
456 }
457
458 return 1;
459}
460
461
462#
463# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
464# format of the current X display being used. If successful, it sets the
465# options as appropriate so that the X VNC server will use the same settings
466# (minus an allowance for window manager decorations on the geometry). Using
467# the same depth and pixel format means that the VNC server won't have to
468# translate pixels when the desktop is being viewed on this X display (for
469# TrueColor displays anyway).
470#
471
472sub GetXDisplayDefaults
473{
474 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
475 $red, $green, $blue);
476
477 $wmDecorationWidth = 4; # a guess at typical size for window manager
478 $wmDecorationHeight = 24; # decoration size
479
480 return if (!defined($ENV{DISPLAY}));
481
482 @lines = `xdpyinfo 2>/dev/null`;
483
484 return if ($? != 0);
485
486 @matchlines = grep(/dimensions/, @lines);
487 if (@matchlines) {
488 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
489
490 $width -= $wmDecorationWidth;
491 $height -= $wmDecorationHeight;
492
493 $geometry = "${width}x$height";
494 }
495
496 @matchlines = grep(/default visual id/, @lines);
497 if (@matchlines) {
498 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
499
500 for ($i = 0; $i < @lines; $i++) {
501 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
502 if (($lines[$i+1] !~ /TrueColor/) ||
503 ($lines[$i+2] !~ /depth/) ||
504 ($lines[$i+4] !~ /red, green, blue masks/))
505 {
506 return;
507 }
508 last;
509 }
510 }
511
512 return if ($i >= @lines);
513
514 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
515 ($red,$green,$blue)
516 = ($lines[$i+4]
517 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
518
519 $red = hex($red);
520 $green = hex($green);
521 $blue = hex($blue);
522
523 if ($red > $blue) {
524 $red = int(log($red) / log(2)) - int(log($green) / log(2));
525 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
526 $blue = int(log($blue) / log(2)) + 1;
527 $pixelformat = "rgb$red$green$blue";
528 } else {
529 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
530 $green = int(log($green) / log(2)) - int(log($red) / log(2));
531 $red = int(log($red) / log(2)) + 1;
532 $pixelformat = "bgr$blue$green$red";
533 }
534 }
535}
536
537
538#
539# quotedString returns a string which yields the original string when parsed
540# by a shell.
541#
542
543sub quotedString
544{
545 local ($in) = @_;
546
547 $in =~ s/\'/\'\"\'\"\'/g;
548
549 return "'$in'";
550}
551
552
553#
554# removeSlashes turns slashes into underscores for use as a file name.
555#
556
557sub removeSlashes
558{
559 local ($in) = @_;
560
561 $in =~ s|/|_|g;
562
563 return "$in";
564}
565
566
567#
568# Usage
569#
570
571sub Usage
572{
573 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
574 " [-geometry <width>x<height>]\n".
575 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000576 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000577 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000578 " [-autokill]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000579 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000580 " $prog -kill <X-display>\n\n".
581 " $prog -list\n\n");
582}
583
584
585#
586# List
587#
588
589sub List
590{
591 opendir(dir, $vncUserDir);
592 my @filelist = readdir(dir);
593 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000594 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000595 print "X DISPLAY #\tPROCESS ID\n";
596 foreach my $file (@filelist) {
597 if ($file =~ /$host:(\d+)$\.pid/) {
598 print ":".$1."\t\t".`cat $vncUserDir/$file`;
599 }
600 }
601 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000602}
603
604
605#
606# Kill
607#
608
609sub Kill
610{
611 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
612
613 if ($opt{'-kill'} =~ /^:\d+$/) {
614 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
615 } else {
616 if ($opt{'-kill'} !~ /^$host:/) {
617 die "\nCan't tell if $opt{'-kill'} is on $host\n".
618 "Use -kill :<number> instead\n\n";
619 }
620 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
621 }
622
623 if (! -r $pidFile) {
624 die "\nCan't find file $pidFile\n".
625 "You'll have to kill the Xvnc process manually\n\n";
626 }
627
628 $SIG{'HUP'} = 'IGNORE';
629 chop($pid = `cat $pidFile`);
630 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000631
632 if (kill 0, $pid) {
633 system("kill $pid");
634 sleep(1);
635 if (kill 0, $pid) {
636 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
637 print " ".$0." -kill ".$opt{'-kill'}."\n";
638 print "to clean up the socket files.\n";
639 exit
640 }
641
642 } else {
643 warn "Xvnc process ID $pid already killed\n";
644 $opt{'-kill'} =~ s/://;
645
646 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
647 print "Xvnc did not appear to shut down cleanly.";
648 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
649 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
650 }
651 if (-e "/tmp/.X$opt{'-kill'}-lock") {
652 print "Xvnc did not appear to shut down cleanly.";
653 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
654 unlink "/tmp/.X$opt{'-kill'}-lock";
655 }
656 }
657
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000658 unlink $pidFile;
659 exit;
660}
661
662
663#
664# ParseOptions takes a list of possible options and a boolean indicating
665# whether the option has a value following, and sets up an associative array
666# %opt of the values of the options given on the command line. It removes all
667# the arguments it uses from @ARGV and returns them in @optArgs.
668#
669
670sub ParseOptions
671{
672 local (@optval) = @_;
673 local ($opt, @opts, %valFollows, @newargs);
674
675 while (@optval) {
676 $opt = shift(@optval);
677 push(@opts,$opt);
678 $valFollows{$opt} = shift(@optval);
679 }
680
681 @optArgs = ();
682 %opt = ();
683
684 arg: while (defined($arg = shift(@ARGV))) {
685 foreach $opt (@opts) {
686 if ($arg eq $opt) {
687 push(@optArgs, $arg);
688 if ($valFollows{$opt}) {
689 if (@ARGV == 0) {
690 &Usage();
691 }
692 $opt{$opt} = shift(@ARGV);
693 push(@optArgs, $opt{$opt});
694 } else {
695 $opt{$opt} = 1;
696 }
697 next arg;
698 }
699 }
700 push(@newargs,$arg);
701 }
702
703 @ARGV = @newargs;
704}
705
706
707#
708# Routine to make sure we're operating in a sane environment.
709#
710
711sub SanityCheck
712{
713 local ($cmd);
714
715 #
716 # Get the program name
717 #
718
719 ($prog) = ($0 =~ m|([^/]+)$|);
720
721 #
722 # Check we have all the commands we'll need on the path.
723 #
724
725 cmd:
DRC190854c2009-03-26 18:13:00 +0000726 foreach $cmd ("uname") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000727 for (split(/:/,$ENV{PATH})) {
728 if (-x "$_/$cmd") {
729 next cmd;
730 }
731 }
732 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
733 }
DRC190854c2009-03-26 18:13:00 +0000734 if (-x "/usr/X11R6/bin/xauth") {
735 $xauth = "/usr/X11R6/bin/xauth";
736 }
737 else {
738 cmd1:
739 foreach $cmd ("xauth") {
740 for (split(/:/,$ENV{PATH})) {
741 if (-x "$_/$cmd") {
742 next cmd1;
743 }
744 }
745 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
746 }
747 }
748
749 if($exedir eq "") {
750 cmd2:
751 foreach $cmd ("Xvnc","vncpasswd") {
752 for (split(/:/,$ENV{PATH})) {
753 if (-x "$_/$cmd") {
754 $vncClasses = "$_/../vnc/classes";
755 next cmd2;
756 }
757 }
758 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
759 }
760 }
761 else {
762 cmd3:
763 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
764 for (split(/:/,$ENV{PATH})) {
765 if (-x "$cmd") {
766 $vncClasses = $exedir."../vnc/classes";
767 next cmd3;
768 }
769 }
770 die "$prog: couldn't find \"$cmd\".\n";
771 }
772 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000773
774 #
775 # Check the HOME environment variable is set
776 #
777
778 if (!defined($ENV{HOME})) {
779 die "$prog: The HOME environment variable is not set.\n";
780 }
DRC190854c2009-03-26 18:13:00 +0000781# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000782
783 #
784 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
785 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
786 # we just guess at the values. If you find perl moaning here, just
787 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
788 # for your platform by looking in /usr/include/sys/socket.h and related
789 # files.
790 #
791
792 chop($os = `uname`);
793 chop($osrev = `uname -r`);
794
795 eval 'use Socket';
796 if ($@) {
797 eval 'require "sys/socket.ph"';
798 if ($@) {
799 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
800 $AF_INET = 2;
801 $SOCK_STREAM = 2;
802 } else {
803 $AF_INET = 2;
804 $SOCK_STREAM = 1;
805 }
806 } else {
807 $AF_INET = &AF_INET;
808 $SOCK_STREAM = &SOCK_STREAM;
809 }
810 } else {
811 $AF_INET = &AF_INET;
812 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000813 }
814}