blob: c7fdc655d7378d4fbbb5e4f8faa034536804750c [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
grayskyddff8d02015-10-19 08:24:14 -040084$defaultConfig
85 = ("## Supported server options to pass to vncserver upon invocation can be listed\n".
86 "## in this file. See the following manpages for more: vncserver(1) Xvnc(1).\n".
87 "## Several common ones are shown below. Uncomment and modify to your liking.\n".
88 "##\n".
89 "# securitytypes=vncauth,tlsvnc\n".
90 "# desktop=sandbox\n".
91 "# geometry=2000x1200\n".
92 "# localhost\n".
93 "# alwaysshared\n");
94
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000095chop($host = `uname -n`);
96
DRC989dbd12009-04-22 13:23:17 +000097if (-d "/etc/X11/fontpath.d") {
98 $fontPath = "catalogue:/etc/X11/fontpath.d";
99}
DRC36546c12009-04-15 06:47:23 +0000100
DRCd6821bf2009-03-26 18:17:49 +0000101@fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/');
102if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');}
103if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');}
104if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');}
105push(@fontpaths, '/usr/share/fonts/default');
106
107@fonttypes = ('misc',
108 '75dpi',
109 '100dpi',
110 'Speedo',
111 'Type1');
112
113foreach $_fpath (@fontpaths) {
114 foreach $_ftype (@fonttypes) {
115 if (-f "$_fpath/$_ftype/fonts.dir") {
116 if (! -l "$_fpath/$_ftype") {
DRC36546c12009-04-15 06:47:23 +0000117 $defFontPath .= "$_fpath/$_ftype,";
DRCd6821bf2009-03-26 18:17:49 +0000118 }
119 }
120 }
121}
DRCd28792b2010-01-11 20:53:00 +0000122
DRC36546c12009-04-15 06:47:23 +0000123if ($defFontPath) {
124 if (substr($defFontPath, -1, 1) == ',') {
125 chop $defFontPath;
DRCd6821bf2009-03-26 18:17:49 +0000126 }
127}
128
DRCd28792b2010-01-11 20:53:00 +0000129if ($fontPath eq "") {
130 $fontPath = $defFontPath;
131}
132
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000133# Check command line options
134
135&ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000136 "-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",0);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000137
138&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
139
140&Kill() if ($opt{'-kill'});
141
DRCb9d8e762011-02-09 08:24:58 +0000142&List() if ($opt{'-list'});
143
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000144# Uncomment this line if you want default geometry, depth and pixelformat
145# to match the current X display:
146# &GetXDisplayDefaults();
147
148if ($opt{'-geometry'}) {
149 $geometry = $opt{'-geometry'};
150}
151if ($opt{'-depth'}) {
152 $depth = $opt{'-depth'};
153 $pixelformat = "";
154}
155if ($opt{'-pixelformat'}) {
156 $pixelformat = $opt{'-pixelformat'};
157}
DRCeed5d1f2009-03-26 19:16:19 +0000158if ($opt{'-fp'}) {
159 $fontPath = $opt{'-fp'};
DRC36546c12009-04-15 06:47:23 +0000160 $fpArgSpecified = 1;
DRCeed5d1f2009-03-26 19:16:19 +0000161}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000162
163&CheckGeometryAndDepth();
164
165
166# Create the user's vnc directory if necessary.
167
168if (!(-e $vncUserDir)) {
169 if (!mkdir($vncUserDir,0755)) {
170 die "$prog: Could not create $vncUserDir.\n";
171 }
172}
173
Adam Tkacf586b842011-04-27 11:20:18 +0000174# Check whether VNC authentication is enabled, and if so, prompt the user to
175# create a VNC password if they don't already have one.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000176
Adam Tkacf586b842011-04-27 11:20:18 +0000177$securityTypeArgSpecified = 0;
178$vncAuthEnabled = 0;
179$passwordArgSpecified = 0;
180
181for ($i = 0; $i < @ARGV; ++$i) {
182 # -SecurityTypes can be followed by a space or "="
183 my @splitargs = split('=', $ARGV[$i]);
184 if (@splitargs <= 1 && $i < @ARGV - 1) {
185 push(@splitargs, $ARGV[$i + 1]);
186 }
187 if (lc(@splitargs[0]) eq "-securitytypes") {
188 if (@splitargs > 1) {
189 $securityTypeArgSpecified = 1;
190 }
191 foreach $arg2 (split(',', @splitargs[1])) {
192 if (lc($arg2) eq "vncauth" || lc($arg2) eq "tlsvnc"
193 || lc($arg2) eq "x509vnc") {
194 $vncAuthEnabled = 1;
195 }
196 }
197 }
198 if ((lc(@splitargs[0]) eq "-password")
199 || (lc(@splitargs[0]) eq "-passwordfile"
200 || (lc(@splitargs[0]) eq "-rfbauth"))) {
201 $passwordArgSpecified = 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000202 }
203}
204
Adam Tkacf586b842011-04-27 11:20:18 +0000205if ((!$securityTypeArgSpecified || $vncAuthEnabled) && !$passwordArgSpecified) {
206 ($z,$z,$mode) = stat("$vncUserDir/passwd");
207 if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
208 warn "\nYou will require a password to access your desktops.\n\n";
209 system($exedir."vncpasswd -q $vncUserDir/passwd");
210 if (($? >> 8) != 0) {
211 exit 1;
212 }
213 }
214}
215
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000216# Find display number.
217
218if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
219 $displayNumber = $1;
220 shift(@ARGV);
221 if (!&CheckDisplayNumber($displayNumber)) {
222 die "A VNC server is already running as :$displayNumber\n";
223 }
Adam Tkac39c9d992010-07-21 14:08:38 +0000224} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000225 &Usage();
226} else {
227 $displayNumber = &GetDisplayNumber();
228}
229
230$vncPort = 5900 + $displayNumber;
231
232$desktopLog = "$vncUserDir/$host:$displayNumber.log";
233unlink($desktopLog);
234
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000235# Make an X server cookie - use /dev/urandom on systems that have it,
236# otherwise use perl's random number generator, seeded with the sum
237# of the current time, our PID and part of the encrypted form of the password.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000238
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000239my $cookie = "";
240if (open(URANDOM, '<', '/dev/urandom')) {
241 my $randata;
242 if (sysread(URANDOM, $randata, 16) == 16) {
243 $cookie = unpack 'h*', $randata;
244 }
245 close(URANDOM);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000246}
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000247if ($cookie eq "") {
248 srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
249 for (1..16) {
250 $cookie .= sprintf("%02x", int(rand(256)) % 256);
251 }
252}
253
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000254system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
255system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
256
257if ($opt{'-name'}) {
258 $desktopName = $opt{'-name'};
259} else {
260 $desktopName = "$host:$displayNumber ($ENV{USER})";
261}
262
263# Now start the X VNC Server
264
DRC190854c2009-03-26 18:13:00 +0000265$cmd = $exedir."Xvnc :$displayNumber";
grayskyddff8d02015-10-19 08:24:14 -0400266
267my %default_opts;
268my %config;
269
270$default_opts{desktop} = &quotedString($desktopName);
271$default_opts{httpd} = $vncJavaFiles if ($vncJavaFiles);
272$default_opts{auth} = $xauthorityFile;
273$default_opts{geometry} = $geometry if ($geometry);
274$default_opts{depth} = $depth if ($depth);
275$default_opts{pixelformat} = $pixelformat if ($pixelformat);
276$default_opts{rfbwait} = 30000;
277$default_opts{rfbauth} = "$vncUserDir/passwd";
278$default_opts{rfbport} = $vncPort;
279$default_opts{fp} = $fontPath if ($fontPath);
280$default_opts{pn} = "";
281
282# if a user configuration file already exists
283if(stat("$vncUserDir/config")) {
284
285 # loads and parses configuration file
286 if(open(IN, "$vncUserDir/config")) {
287 while(<IN>) {
288 next if /^#/;
289 if(my ($k, $v) = /^\s*(\w+)\s*=\s*(.+)$/) {
290 $config{$k} = $v;
291 } elsif ($_ =~ m/^\s*(\S+)/) {
292 $config{$1} = $k;
293 }
294 }
295 close(IN);
296 }
297}
298
299foreach my $k (sort keys %config) {
300 $cmd .= " -$k $config{$k}";
301 # user's option takes precedence
302 delete $default_opts{$k};
303}
304
305foreach my $k (sort keys %default_opts) {
306 $cmd .= " -$k $default_opts{$k}";
307}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000308
DRCd6821bf2009-03-26 18:17:49 +0000309# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000310#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000311# $cmd .= " -co /usr/lib/X11/rgb";
312#
313
314foreach $arg (@ARGV) {
315 $cmd .= " " . &quotedString($arg);
316}
317$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
318
319# Run $cmd and record the process ID.
320
321$pidFile = "$vncUserDir/$host:$displayNumber.pid";
322system("$cmd & echo \$! >$pidFile");
323
324# Give Xvnc a chance to start up
325
326sleep(3);
DRCd28792b2010-01-11 20:53:00 +0000327if ($fontPath ne $defFontPath) {
328 unless (kill 0, `cat $pidFile`) {
329 if ($fpArgSpecified) {
330 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
331 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
332 warn "determine an appropriate font path for this system and restart Xvnc using\n";
333 warn "that font path ...\n";
334 } else {
335 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
336 warn "catalog is not properly configured. Attempting to determine an appropriate\n";
337 warn "font path for this system and restart Xvnc using that font path ...\n";
338 }
339 $cmd =~ s@-fp [^ ]+@@;
340 $cmd .= " -fp $defFontPath" if ($defFontPath);
341 system("$cmd & echo \$! >$pidFile");
342 sleep(3);
DRC36546c12009-04-15 06:47:23 +0000343 }
DRCd6821bf2009-03-26 18:17:49 +0000344}
345unless (kill 0, `cat $pidFile`) {
346 warn "Could not start Xvnc.\n\n";
Michal Srbe6e11f92015-10-02 02:28:26 +0300347 unlink $pidFile;
DRCd6821bf2009-03-26 18:17:49 +0000348 open(LOG, "<$desktopLog");
349 while (<LOG>) { print; }
350 close(LOG);
351 die "\n";
352}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000353
354warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
355
356# Create the user's xstartup script if necessary.
357
358if (!(-e "$vncUserDir/xstartup")) {
359 warn "Creating default startup script $vncUserDir/xstartup\n";
360 open(XSTARTUP, ">$vncUserDir/xstartup");
361 print XSTARTUP $defaultXStartup;
362 close(XSTARTUP);
363 chmod 0755, "$vncUserDir/xstartup";
364}
365
grayskyddff8d02015-10-19 08:24:14 -0400366# Create the user's config file if necessary.
367
368if (!(-e "$vncUserDir/config")) {
369 warn "Creating default config $vncUserDir/config\n";
370 open(XSTARTUP, ">$vncUserDir/config");
371 print XSTARTUP $defaultConfig;
372 close(XSTARTUP);
373 chmod 0644, "$vncUserDir/config";
374}
375
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000376# Run the X startup script.
377
378warn "Starting applications specified in $vncUserDir/xstartup\n";
379warn "Log file is $desktopLog\n\n";
380
381# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
382# TCP (DISPLAY=host:n)
383
384if (-e "/tmp/.X11-unix/X$displayNumber" ||
385 -e "/usr/spool/sockets/X11/$displayNumber")
386{
387 $ENV{DISPLAY}= ":$displayNumber";
388} else {
389 $ENV{DISPLAY}= "$host:$displayNumber";
390}
391$ENV{VNCDESKTOP}= $desktopName;
392
Pierre Ossmana5b37c02015-07-30 11:04:02 +0200393system($exedir."vncconfig -nowin >> " . &quotedString($desktopLog) . " 2>&1 &");
DRCf6b58402011-10-05 21:28:03 +0000394
DRC8fb11912011-03-03 10:42:14 +0000395if ($opt{'-fg'}) {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000396 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1");
397 if (kill 0, `cat $pidFile`) {
398 $opt{'-kill'} = ':'.$displayNumber;
399 &Kill();
400 }
DRC8fb11912011-03-03 10:42:14 +0000401} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000402 if ($opt{'-autokill'}) {
403 system("($vncUserDir/xstartup; $0 -kill :$displayNumber) >> "
404 . &quotedString($desktopLog) . " 2>&1 &");
405 } else {
406 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog)
407 . " 2>&1 &");
408 }
DRC8fb11912011-03-03 10:42:14 +0000409}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000410
411exit;
412
413
414###############################################################################
415#
416# CheckGeometryAndDepth simply makes sure that the geometry and depth values
417# are sensible.
418#
419
420sub CheckGeometryAndDepth
421{
422 if ($geometry =~ /^(\d+)x(\d+)$/) {
423 $width = $1; $height = $2;
424
425 if (($width<1) || ($height<1)) {
426 die "$prog: geometry $geometry is invalid\n";
427 }
428
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000429 $geometry = "${width}x$height";
430 } else {
431 die "$prog: geometry $geometry is invalid\n";
432 }
433
DRCe5b4f752009-03-26 18:23:29 +0000434 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000435 die "Depth must be between 8 and 32\n";
436 }
437}
438
439
440#
441# GetDisplayNumber gets the lowest available display number. A display number
442# n is taken if something is listening on the VNC server port (5900+n) or the
443# X server port (6000+n).
444#
445
446sub GetDisplayNumber
447{
448 foreach $n (1..99) {
449 if (&CheckDisplayNumber($n)) {
450 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
451 }
452 }
453
454 die "$prog: no free display number on $host.\n";
455}
456
457
458#
459# CheckDisplayNumber checks if the given display number is available. A
460# display number n is taken if something is listening on the VNC server port
461# (5900+n) or the X server port (6000+n).
462#
463
464sub CheckDisplayNumber
465{
466 local ($n) = @_;
467
468 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
469 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
470 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
471 close(S);
472 return 0;
473 }
474 close(S);
475
476 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
477 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
478 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
479 close(S);
480 return 0;
481 }
482 close(S);
483
484 if (-e "/tmp/.X$n-lock") {
485 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
486 warn "Remove this file if there is no X server $host:$n\n";
487 return 0;
488 }
489
490 if (-e "/tmp/.X11-unix/X$n") {
491 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
492 warn "Remove this file if there is no X server $host:$n\n";
493 return 0;
494 }
495
496 if (-e "/usr/spool/sockets/X11/$n") {
497 warn("\nWarning: $host:$n is taken because of ".
498 "/usr/spool/sockets/X11/$n\n");
499 warn "Remove this file if there is no X server $host:$n\n";
500 return 0;
501 }
502
503 return 1;
504}
505
506
507#
508# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
509# format of the current X display being used. If successful, it sets the
510# options as appropriate so that the X VNC server will use the same settings
511# (minus an allowance for window manager decorations on the geometry). Using
512# the same depth and pixel format means that the VNC server won't have to
513# translate pixels when the desktop is being viewed on this X display (for
514# TrueColor displays anyway).
515#
516
517sub GetXDisplayDefaults
518{
519 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
520 $red, $green, $blue);
521
522 $wmDecorationWidth = 4; # a guess at typical size for window manager
523 $wmDecorationHeight = 24; # decoration size
524
525 return if (!defined($ENV{DISPLAY}));
526
527 @lines = `xdpyinfo 2>/dev/null`;
528
529 return if ($? != 0);
530
531 @matchlines = grep(/dimensions/, @lines);
532 if (@matchlines) {
533 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
534
535 $width -= $wmDecorationWidth;
536 $height -= $wmDecorationHeight;
537
538 $geometry = "${width}x$height";
539 }
540
541 @matchlines = grep(/default visual id/, @lines);
542 if (@matchlines) {
543 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
544
545 for ($i = 0; $i < @lines; $i++) {
546 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
547 if (($lines[$i+1] !~ /TrueColor/) ||
548 ($lines[$i+2] !~ /depth/) ||
549 ($lines[$i+4] !~ /red, green, blue masks/))
550 {
551 return;
552 }
553 last;
554 }
555 }
556
557 return if ($i >= @lines);
558
559 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
560 ($red,$green,$blue)
561 = ($lines[$i+4]
562 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
563
564 $red = hex($red);
565 $green = hex($green);
566 $blue = hex($blue);
567
568 if ($red > $blue) {
569 $red = int(log($red) / log(2)) - int(log($green) / log(2));
570 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
571 $blue = int(log($blue) / log(2)) + 1;
572 $pixelformat = "rgb$red$green$blue";
573 } else {
574 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
575 $green = int(log($green) / log(2)) - int(log($red) / log(2));
576 $red = int(log($red) / log(2)) + 1;
577 $pixelformat = "bgr$blue$green$red";
578 }
579 }
580}
581
582
583#
584# quotedString returns a string which yields the original string when parsed
585# by a shell.
586#
587
588sub quotedString
589{
590 local ($in) = @_;
591
592 $in =~ s/\'/\'\"\'\"\'/g;
593
594 return "'$in'";
595}
596
597
598#
599# removeSlashes turns slashes into underscores for use as a file name.
600#
601
602sub removeSlashes
603{
604 local ($in) = @_;
605
606 $in =~ s|/|_|g;
607
608 return "$in";
609}
610
611
612#
613# Usage
614#
615
616sub Usage
617{
618 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
619 " [-geometry <width>x<height>]\n".
620 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000621 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000622 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000623 " [-autokill]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000624 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000625 " $prog -kill <X-display>\n\n".
626 " $prog -list\n\n");
627}
628
629
630#
631# List
632#
633
634sub List
635{
636 opendir(dir, $vncUserDir);
637 my @filelist = readdir(dir);
638 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000639 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000640 print "X DISPLAY #\tPROCESS ID\n";
641 foreach my $file (@filelist) {
642 if ($file =~ /$host:(\d+)$\.pid/) {
Michal Srbe6e11f92015-10-02 02:28:26 +0300643 chop($tmp_pid = `cat $vncUserDir/$file`);
644 if (kill 0, $tmp_pid) {
645 print ":".$1."\t\t".`cat $vncUserDir/$file`;
646 } else {
647 unlink ($vncUserDir . "/" . $file);
648 }
DRCb9d8e762011-02-09 08:24:58 +0000649 }
650 }
651 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000652}
653
654
655#
656# Kill
657#
658
659sub Kill
660{
661 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
662
663 if ($opt{'-kill'} =~ /^:\d+$/) {
664 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
665 } else {
666 if ($opt{'-kill'} !~ /^$host:/) {
667 die "\nCan't tell if $opt{'-kill'} is on $host\n".
668 "Use -kill :<number> instead\n\n";
669 }
670 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
671 }
672
673 if (! -r $pidFile) {
674 die "\nCan't find file $pidFile\n".
675 "You'll have to kill the Xvnc process manually\n\n";
676 }
677
678 $SIG{'HUP'} = 'IGNORE';
679 chop($pid = `cat $pidFile`);
680 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000681
682 if (kill 0, $pid) {
683 system("kill $pid");
684 sleep(1);
685 if (kill 0, $pid) {
686 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
687 print " ".$0." -kill ".$opt{'-kill'}."\n";
688 print "to clean up the socket files.\n";
689 exit
690 }
691
692 } else {
693 warn "Xvnc process ID $pid already killed\n";
694 $opt{'-kill'} =~ s/://;
695
696 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
697 print "Xvnc did not appear to shut down cleanly.";
698 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
699 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
700 }
701 if (-e "/tmp/.X$opt{'-kill'}-lock") {
702 print "Xvnc did not appear to shut down cleanly.";
703 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
704 unlink "/tmp/.X$opt{'-kill'}-lock";
705 }
706 }
707
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000708 unlink $pidFile;
709 exit;
710}
711
712
713#
714# ParseOptions takes a list of possible options and a boolean indicating
715# whether the option has a value following, and sets up an associative array
716# %opt of the values of the options given on the command line. It removes all
717# the arguments it uses from @ARGV and returns them in @optArgs.
718#
719
720sub ParseOptions
721{
722 local (@optval) = @_;
723 local ($opt, @opts, %valFollows, @newargs);
724
725 while (@optval) {
726 $opt = shift(@optval);
727 push(@opts,$opt);
728 $valFollows{$opt} = shift(@optval);
729 }
730
731 @optArgs = ();
732 %opt = ();
733
734 arg: while (defined($arg = shift(@ARGV))) {
735 foreach $opt (@opts) {
736 if ($arg eq $opt) {
737 push(@optArgs, $arg);
738 if ($valFollows{$opt}) {
739 if (@ARGV == 0) {
740 &Usage();
741 }
742 $opt{$opt} = shift(@ARGV);
743 push(@optArgs, $opt{$opt});
744 } else {
745 $opt{$opt} = 1;
746 }
747 next arg;
748 }
749 }
750 push(@newargs,$arg);
751 }
752
753 @ARGV = @newargs;
754}
755
756
757#
758# Routine to make sure we're operating in a sane environment.
759#
760
761sub SanityCheck
762{
763 local ($cmd);
764
765 #
766 # Get the program name
767 #
768
769 ($prog) = ($0 =~ m|([^/]+)$|);
770
771 #
772 # Check we have all the commands we'll need on the path.
773 #
774
775 cmd:
DRC190854c2009-03-26 18:13:00 +0000776 foreach $cmd ("uname") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000777 for (split(/:/,$ENV{PATH})) {
778 if (-x "$_/$cmd") {
779 next cmd;
780 }
781 }
782 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
783 }
DRC190854c2009-03-26 18:13:00 +0000784 if (-x "/usr/X11R6/bin/xauth") {
785 $xauth = "/usr/X11R6/bin/xauth";
786 }
787 else {
788 cmd1:
789 foreach $cmd ("xauth") {
790 for (split(/:/,$ENV{PATH})) {
791 if (-x "$_/$cmd") {
792 next cmd1;
793 }
794 }
795 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
796 }
797 }
798
799 if($exedir eq "") {
800 cmd2:
801 foreach $cmd ("Xvnc","vncpasswd") {
802 for (split(/:/,$ENV{PATH})) {
803 if (-x "$_/$cmd") {
804 $vncClasses = "$_/../vnc/classes";
805 next cmd2;
806 }
807 }
808 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
809 }
810 }
811 else {
812 cmd3:
813 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
814 for (split(/:/,$ENV{PATH})) {
815 if (-x "$cmd") {
816 $vncClasses = $exedir."../vnc/classes";
817 next cmd3;
818 }
819 }
820 die "$prog: couldn't find \"$cmd\".\n";
821 }
822 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000823
824 #
825 # Check the HOME environment variable is set
826 #
827
828 if (!defined($ENV{HOME})) {
829 die "$prog: The HOME environment variable is not set.\n";
830 }
DRC190854c2009-03-26 18:13:00 +0000831# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000832
833 #
834 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
835 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
836 # we just guess at the values. If you find perl moaning here, just
837 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
838 # for your platform by looking in /usr/include/sys/socket.h and related
839 # files.
840 #
841
842 chop($os = `uname`);
843 chop($osrev = `uname -r`);
844
845 eval 'use Socket';
846 if ($@) {
847 eval 'require "sys/socket.ph"';
848 if ($@) {
849 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
850 $AF_INET = 2;
851 $SOCK_STREAM = 2;
852 } else {
853 $AF_INET = 2;
854 $SOCK_STREAM = 1;
855 }
856 } else {
857 $AF_INET = &AF_INET;
858 $SOCK_STREAM = &SOCK_STREAM;
859 }
860 } else {
861 $AF_INET = &AF_INET;
862 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000863 }
864}