blob: 64d10c5bd2e819e3419a46c21cba01a1916b7aeb [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";
Michal Srbe6e11f92015-10-02 02:28:26 +0300305 unlink $pidFile;
DRCd6821bf2009-03-26 18:17:49 +0000306 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
Pierre Ossmana5b37c02015-07-30 11:04:02 +0200341system($exedir."vncconfig -nowin >> " . &quotedString($desktopLog) . " 2>&1 &");
DRCf6b58402011-10-05 21:28:03 +0000342
DRC8fb11912011-03-03 10:42:14 +0000343if ($opt{'-fg'}) {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000344 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1");
345 if (kill 0, `cat $pidFile`) {
346 $opt{'-kill'} = ':'.$displayNumber;
347 &Kill();
348 }
DRC8fb11912011-03-03 10:42:14 +0000349} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000350 if ($opt{'-autokill'}) {
351 system("($vncUserDir/xstartup; $0 -kill :$displayNumber) >> "
352 . &quotedString($desktopLog) . " 2>&1 &");
353 } else {
354 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog)
355 . " 2>&1 &");
356 }
DRC8fb11912011-03-03 10:42:14 +0000357}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000358
359exit;
360
361
362###############################################################################
363#
364# CheckGeometryAndDepth simply makes sure that the geometry and depth values
365# are sensible.
366#
367
368sub CheckGeometryAndDepth
369{
370 if ($geometry =~ /^(\d+)x(\d+)$/) {
371 $width = $1; $height = $2;
372
373 if (($width<1) || ($height<1)) {
374 die "$prog: geometry $geometry is invalid\n";
375 }
376
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000377 $geometry = "${width}x$height";
378 } else {
379 die "$prog: geometry $geometry is invalid\n";
380 }
381
DRCe5b4f752009-03-26 18:23:29 +0000382 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000383 die "Depth must be between 8 and 32\n";
384 }
385}
386
387
388#
389# GetDisplayNumber gets the lowest available display number. A display number
390# n is taken if something is listening on the VNC server port (5900+n) or the
391# X server port (6000+n).
392#
393
394sub GetDisplayNumber
395{
396 foreach $n (1..99) {
397 if (&CheckDisplayNumber($n)) {
398 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
399 }
400 }
401
402 die "$prog: no free display number on $host.\n";
403}
404
405
406#
407# CheckDisplayNumber checks if the given display number is available. A
408# display number n is taken if something is listening on the VNC server port
409# (5900+n) or the X server port (6000+n).
410#
411
412sub CheckDisplayNumber
413{
414 local ($n) = @_;
415
416 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
417 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
418 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
419 close(S);
420 return 0;
421 }
422 close(S);
423
424 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
425 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
426 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
427 close(S);
428 return 0;
429 }
430 close(S);
431
432 if (-e "/tmp/.X$n-lock") {
433 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
434 warn "Remove this file if there is no X server $host:$n\n";
435 return 0;
436 }
437
438 if (-e "/tmp/.X11-unix/X$n") {
439 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
440 warn "Remove this file if there is no X server $host:$n\n";
441 return 0;
442 }
443
444 if (-e "/usr/spool/sockets/X11/$n") {
445 warn("\nWarning: $host:$n is taken because of ".
446 "/usr/spool/sockets/X11/$n\n");
447 warn "Remove this file if there is no X server $host:$n\n";
448 return 0;
449 }
450
451 return 1;
452}
453
454
455#
456# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
457# format of the current X display being used. If successful, it sets the
458# options as appropriate so that the X VNC server will use the same settings
459# (minus an allowance for window manager decorations on the geometry). Using
460# the same depth and pixel format means that the VNC server won't have to
461# translate pixels when the desktop is being viewed on this X display (for
462# TrueColor displays anyway).
463#
464
465sub GetXDisplayDefaults
466{
467 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
468 $red, $green, $blue);
469
470 $wmDecorationWidth = 4; # a guess at typical size for window manager
471 $wmDecorationHeight = 24; # decoration size
472
473 return if (!defined($ENV{DISPLAY}));
474
475 @lines = `xdpyinfo 2>/dev/null`;
476
477 return if ($? != 0);
478
479 @matchlines = grep(/dimensions/, @lines);
480 if (@matchlines) {
481 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
482
483 $width -= $wmDecorationWidth;
484 $height -= $wmDecorationHeight;
485
486 $geometry = "${width}x$height";
487 }
488
489 @matchlines = grep(/default visual id/, @lines);
490 if (@matchlines) {
491 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
492
493 for ($i = 0; $i < @lines; $i++) {
494 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
495 if (($lines[$i+1] !~ /TrueColor/) ||
496 ($lines[$i+2] !~ /depth/) ||
497 ($lines[$i+4] !~ /red, green, blue masks/))
498 {
499 return;
500 }
501 last;
502 }
503 }
504
505 return if ($i >= @lines);
506
507 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
508 ($red,$green,$blue)
509 = ($lines[$i+4]
510 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
511
512 $red = hex($red);
513 $green = hex($green);
514 $blue = hex($blue);
515
516 if ($red > $blue) {
517 $red = int(log($red) / log(2)) - int(log($green) / log(2));
518 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
519 $blue = int(log($blue) / log(2)) + 1;
520 $pixelformat = "rgb$red$green$blue";
521 } else {
522 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
523 $green = int(log($green) / log(2)) - int(log($red) / log(2));
524 $red = int(log($red) / log(2)) + 1;
525 $pixelformat = "bgr$blue$green$red";
526 }
527 }
528}
529
530
531#
532# quotedString returns a string which yields the original string when parsed
533# by a shell.
534#
535
536sub quotedString
537{
538 local ($in) = @_;
539
540 $in =~ s/\'/\'\"\'\"\'/g;
541
542 return "'$in'";
543}
544
545
546#
547# removeSlashes turns slashes into underscores for use as a file name.
548#
549
550sub removeSlashes
551{
552 local ($in) = @_;
553
554 $in =~ s|/|_|g;
555
556 return "$in";
557}
558
559
560#
561# Usage
562#
563
564sub Usage
565{
566 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
567 " [-geometry <width>x<height>]\n".
568 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000569 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000570 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000571 " [-autokill]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000572 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000573 " $prog -kill <X-display>\n\n".
574 " $prog -list\n\n");
575}
576
577
578#
579# List
580#
581
582sub List
583{
584 opendir(dir, $vncUserDir);
585 my @filelist = readdir(dir);
586 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000587 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000588 print "X DISPLAY #\tPROCESS ID\n";
589 foreach my $file (@filelist) {
590 if ($file =~ /$host:(\d+)$\.pid/) {
Michal Srbe6e11f92015-10-02 02:28:26 +0300591 chop($tmp_pid = `cat $vncUserDir/$file`);
592 if (kill 0, $tmp_pid) {
593 print ":".$1."\t\t".`cat $vncUserDir/$file`;
594 } else {
595 unlink ($vncUserDir . "/" . $file);
596 }
DRCb9d8e762011-02-09 08:24:58 +0000597 }
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}