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