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