blob: 28764cbe363deb531a62582efa528ce4768fa6cd [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001#!/usr/bin/env perl
2#
DRC190854c2009-03-26 18:13:00 +00003# Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
4# Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00005# Copyright (C) 2002-2005 RealVNC Ltd.
6# Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
7#
8# This is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This software is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this software; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21# USA.
22#
23
24#
25# vncserver - wrapper script to start an X VNC server.
26#
27
28#
29# First make sure we're operating in a sane environment.
30#
31
DRC190854c2009-03-26 18:13:00 +000032$exedir = "";
33$slashndx = rindex($0, "/");
34if($slashndx>=0) {
35 $exedir = substr($0, 0, $slashndx+1);
36}
37
38$vncClasses = "";
39
40$xauth = "xauth";
41
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000042&SanityCheck();
43
44#
45# Global variables. You may want to configure some of these for your site.
46#
47
48$geometry = "1024x768";
DRC9d1c1572009-03-26 18:18:51 +000049#$depth = 16;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000050$vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
DRC190854c2009-03-26 18:13:00 +000051 ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes") ||
52 ((-d "$vncClasses") && "$vncClasses"));
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000053$vncUserDir = "$ENV{HOME}/.vnc";
54$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
55
56$defaultXStartup
57 = ("#!/bin/sh\n\n".
Adam Tkac804d86f2009-04-03 14:33:51 +000058 "vncconfig -iconic &\n".
DRC93248982009-03-26 18:14:38 +000059 "unset SESSION_MANAGER\n".
Adam Tkacc071e492009-05-20 09:01:24 +000060 "unset DBUS_SESSION_BUS_ADDRESS\n".
DRC93248982009-03-26 18:14:38 +000061 "OS=`uname -s`\n".
62 "if [ \$OS = 'Linux' ]; then\n".
63 " case \"\$WINDOWMANAGER\" in\n".
64 " \*gnome\*)\n".
65 " if [ -e /etc/SuSE-release ]; then\n".
66 " PATH=\$PATH:/opt/gnome/bin\n".
67 " export PATH\n".
68 " fi\n".
69 " ;;\n".
70 " esac\n".
71 "fi\n".
72 "if [ -x /etc/X11/xinit/xinitrc ]; then\n".
73 " exec /etc/X11/xinit/xinitrc\n".
74 "fi\n".
75 "if [ -f /etc/X11/xinit/xinitrc ]; then\n".
76 " exec sh /etc/X11/xinit/xinitrc\n".
77 "fi\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000078 "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
79 "xsetroot -solid grey\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000080 "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
81 "twm &\n");
82
83chop($host = `uname -n`);
84
DRC36546c12009-04-15 06:47:23 +000085if (CheckXFS(1)) {
86 $fontPath = "unix/:7100";
87} else {
88 if (CheckXFS(0)) {
89 $fontPath = "inet/:7100"; # Some Solaris systems require this
90 }
91}
DRC989dbd12009-04-22 13:23:17 +000092if (-d "/etc/X11/fontpath.d") {
93 $fontPath = "catalogue:/etc/X11/fontpath.d";
94}
DRC36546c12009-04-15 06:47:23 +000095
DRCd6821bf2009-03-26 18:17:49 +000096@fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/');
97if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');}
98if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');}
99if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');}
100push(@fontpaths, '/usr/share/fonts/default');
101
102@fonttypes = ('misc',
103 '75dpi',
104 '100dpi',
105 'Speedo',
106 'Type1');
107
108foreach $_fpath (@fontpaths) {
109 foreach $_ftype (@fonttypes) {
110 if (-f "$_fpath/$_ftype/fonts.dir") {
111 if (! -l "$_fpath/$_ftype") {
DRC36546c12009-04-15 06:47:23 +0000112 $defFontPath .= "$_fpath/$_ftype,";
DRCd6821bf2009-03-26 18:17:49 +0000113 }
114 }
115 }
116}
DRC36546c12009-04-15 06:47:23 +0000117if ($defFontPath) {
118 if (substr($defFontPath, -1, 1) == ',') {
119 chop $defFontPath;
DRCd6821bf2009-03-26 18:17:49 +0000120 }
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,
DRCeed5d1f2009-03-26 19:16:19 +0000126 "-help",0,"-h",0,"--help",0,"-fp",1);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000127
128&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
129
130&Kill() if ($opt{'-kill'});
131
132# Uncomment this line if you want default geometry, depth and pixelformat
133# to match the current X display:
134# &GetXDisplayDefaults();
135
136if ($opt{'-geometry'}) {
137 $geometry = $opt{'-geometry'};
138}
139if ($opt{'-depth'}) {
140 $depth = $opt{'-depth'};
141 $pixelformat = "";
142}
143if ($opt{'-pixelformat'}) {
144 $pixelformat = $opt{'-pixelformat'};
145}
DRCeed5d1f2009-03-26 19:16:19 +0000146if ($opt{'-fp'}) {
147 $fontPath = $opt{'-fp'};
DRC36546c12009-04-15 06:47:23 +0000148 $fpArgSpecified = 1;
DRCeed5d1f2009-03-26 19:16:19 +0000149}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000150
151&CheckGeometryAndDepth();
152
153
154# Create the user's vnc directory if necessary.
155
156if (!(-e $vncUserDir)) {
157 if (!mkdir($vncUserDir,0755)) {
158 die "$prog: Could not create $vncUserDir.\n";
159 }
160}
161
162# Make sure the user has a password.
163
164($z,$z,$mode) = stat("$vncUserDir/passwd");
165if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
166 warn "\nYou will require a password to access your desktops.\n\n";
DRC190854c2009-03-26 18:13:00 +0000167 system($exedir."vncpasswd -q $vncUserDir/passwd");
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000168 if (($? >> 8) != 0) {
169 exit 1;
170 }
171}
172
173# Find display number.
174
175if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
176 $displayNumber = $1;
177 shift(@ARGV);
178 if (!&CheckDisplayNumber($displayNumber)) {
179 die "A VNC server is already running as :$displayNumber\n";
180 }
181} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/)) {
182 &Usage();
183} else {
184 $displayNumber = &GetDisplayNumber();
185}
186
187$vncPort = 5900 + $displayNumber;
188
189$desktopLog = "$vncUserDir/$host:$displayNumber.log";
190unlink($desktopLog);
191
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000192# Make an X server cookie - use /dev/urandom on systems that have it,
193# otherwise use perl's random number generator, seeded with the sum
194# of the current time, our PID and part of the encrypted form of the password.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000195
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000196my $cookie = "";
197if (open(URANDOM, '<', '/dev/urandom')) {
198 my $randata;
199 if (sysread(URANDOM, $randata, 16) == 16) {
200 $cookie = unpack 'h*', $randata;
201 }
202 close(URANDOM);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000203}
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000204if ($cookie eq "") {
205 srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
206 for (1..16) {
207 $cookie .= sprintf("%02x", int(rand(256)) % 256);
208 }
209}
210
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000211system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
212system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
213
214if ($opt{'-name'}) {
215 $desktopName = $opt{'-name'};
216} else {
217 $desktopName = "$host:$displayNumber ($ENV{USER})";
218}
219
220# Now start the X VNC Server
221
DRC190854c2009-03-26 18:13:00 +0000222$cmd = $exedir."Xvnc :$displayNumber";
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000223$cmd .= " -desktop " . &quotedString($desktopName);
224$cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles);
225$cmd .= " -auth $xauthorityFile";
226$cmd .= " -geometry $geometry" if ($geometry);
227$cmd .= " -depth $depth" if ($depth);
228$cmd .= " -pixelformat $pixelformat" if ($pixelformat);
229$cmd .= " -rfbwait 30000";
230$cmd .= " -rfbauth $vncUserDir/passwd";
231$cmd .= " -rfbport $vncPort";
DRCd6821bf2009-03-26 18:17:49 +0000232$cmd .= " -fp $fontPath" if ($fontPath);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000233$cmd .= " -pn";
234
DRCd6821bf2009-03-26 18:17:49 +0000235# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000236#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000237# $cmd .= " -co /usr/lib/X11/rgb";
238#
239
240foreach $arg (@ARGV) {
241 $cmd .= " " . &quotedString($arg);
242}
243$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
244
245# Run $cmd and record the process ID.
246
247$pidFile = "$vncUserDir/$host:$displayNumber.pid";
248system("$cmd & echo \$! >$pidFile");
249
250# Give Xvnc a chance to start up
251
252sleep(3);
DRCd6821bf2009-03-26 18:17:49 +0000253unless (kill 0, `cat $pidFile`) {
DRC36546c12009-04-15 06:47:23 +0000254 if ($fpArgSpecified) {
255 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
256 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
257 warn "determine an appropriate font path for this system and restart Xvnc using\n";
258 warn "that font path ...\n";
259 } else {
260 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the X Font\n";
261 warn "Server is not running and this system does not use a font catalog. Attempting\n";
262 warn "to determine an appropriate font path for this system and restart Xvnc using\n";
263 warn "that font path ...\n";
264 }
DRCd6821bf2009-03-26 18:17:49 +0000265 $cmd =~ s@-fp [^ ]+@@;
266 $cmd .= " -fp $defFontPath" if ($defFontPath);
267 system("$cmd & echo \$! >$pidFile");
268 sleep(3);
269}
270unless (kill 0, `cat $pidFile`) {
271 warn "Could not start Xvnc.\n\n";
272 open(LOG, "<$desktopLog");
273 while (<LOG>) { print; }
274 close(LOG);
275 die "\n";
276}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000277
278warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
279
280# Create the user's xstartup script if necessary.
281
282if (!(-e "$vncUserDir/xstartup")) {
283 warn "Creating default startup script $vncUserDir/xstartup\n";
284 open(XSTARTUP, ">$vncUserDir/xstartup");
285 print XSTARTUP $defaultXStartup;
286 close(XSTARTUP);
287 chmod 0755, "$vncUserDir/xstartup";
288}
289
290# Run the X startup script.
291
292warn "Starting applications specified in $vncUserDir/xstartup\n";
293warn "Log file is $desktopLog\n\n";
294
295# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
296# TCP (DISPLAY=host:n)
297
298if (-e "/tmp/.X11-unix/X$displayNumber" ||
299 -e "/usr/spool/sockets/X11/$displayNumber")
300{
301 $ENV{DISPLAY}= ":$displayNumber";
302} else {
303 $ENV{DISPLAY}= "$host:$displayNumber";
304}
305$ENV{VNCDESKTOP}= $desktopName;
306
307system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1 &");
308
309exit;
310
311
312###############################################################################
313#
314# CheckGeometryAndDepth simply makes sure that the geometry and depth values
315# are sensible.
316#
317
318sub CheckGeometryAndDepth
319{
320 if ($geometry =~ /^(\d+)x(\d+)$/) {
321 $width = $1; $height = $2;
322
323 if (($width<1) || ($height<1)) {
324 die "$prog: geometry $geometry is invalid\n";
325 }
326
327 while (($width % 4)!=0) {
328 $width = $width + 1;
329 }
330
331 while (($height % 2)!=0) {
332 $height = $height + 1;
333 }
334
335 $geometry = "${width}x$height";
336 } else {
337 die "$prog: geometry $geometry is invalid\n";
338 }
339
DRCe5b4f752009-03-26 18:23:29 +0000340 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000341 die "Depth must be between 8 and 32\n";
342 }
343}
344
345
346#
347# GetDisplayNumber gets the lowest available display number. A display number
348# n is taken if something is listening on the VNC server port (5900+n) or the
349# X server port (6000+n).
350#
351
352sub GetDisplayNumber
353{
354 foreach $n (1..99) {
355 if (&CheckDisplayNumber($n)) {
356 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
357 }
358 }
359
360 die "$prog: no free display number on $host.\n";
361}
362
363
364#
365# CheckDisplayNumber checks if the given display number is available. A
366# display number n is taken if something is listening on the VNC server port
367# (5900+n) or the X server port (6000+n).
368#
369
370sub CheckDisplayNumber
371{
372 local ($n) = @_;
373
374 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
375 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
376 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
377 close(S);
378 return 0;
379 }
380 close(S);
381
382 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
383 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
384 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
385 close(S);
386 return 0;
387 }
388 close(S);
389
390 if (-e "/tmp/.X$n-lock") {
391 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
392 warn "Remove this file if there is no X server $host:$n\n";
393 return 0;
394 }
395
396 if (-e "/tmp/.X11-unix/X$n") {
397 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
398 warn "Remove this file if there is no X server $host:$n\n";
399 return 0;
400 }
401
402 if (-e "/usr/spool/sockets/X11/$n") {
403 warn("\nWarning: $host:$n is taken because of ".
404 "/usr/spool/sockets/X11/$n\n");
405 warn "Remove this file if there is no X server $host:$n\n";
406 return 0;
407 }
408
409 return 1;
410}
411
412
413#
DRC36546c12009-04-15 06:47:23 +0000414# CheckXFS checks if the X Font Server is running on the default port (7100)
415#
416
417sub CheckXFS
418{
DRC5e6c57f2009-04-16 20:56:25 +0000419 if ($_[0] == 1) {
420 if (-e "/tmp/.font-unix/fs7100") {
421 return 1;
DRC36546c12009-04-15 06:47:23 +0000422 }
423 } else {
424 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
425 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
DRC5e6c57f2009-04-16 20:56:25 +0000426 if (!bind(S, pack('S n x12', $AF_INET, 7100))) {
DRC36546c12009-04-15 06:47:23 +0000427 close(S);
DRC5e6c57f2009-04-16 20:56:25 +0000428 return 1;
DRC36546c12009-04-15 06:47:23 +0000429 }
430 }
431 close(S);
DRC5e6c57f2009-04-16 20:56:25 +0000432 return 0;
DRC36546c12009-04-15 06:47:23 +0000433}
434
435
436#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000437# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
438# format of the current X display being used. If successful, it sets the
439# options as appropriate so that the X VNC server will use the same settings
440# (minus an allowance for window manager decorations on the geometry). Using
441# the same depth and pixel format means that the VNC server won't have to
442# translate pixels when the desktop is being viewed on this X display (for
443# TrueColor displays anyway).
444#
445
446sub GetXDisplayDefaults
447{
448 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
449 $red, $green, $blue);
450
451 $wmDecorationWidth = 4; # a guess at typical size for window manager
452 $wmDecorationHeight = 24; # decoration size
453
454 return if (!defined($ENV{DISPLAY}));
455
456 @lines = `xdpyinfo 2>/dev/null`;
457
458 return if ($? != 0);
459
460 @matchlines = grep(/dimensions/, @lines);
461 if (@matchlines) {
462 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
463
464 $width -= $wmDecorationWidth;
465 $height -= $wmDecorationHeight;
466
467 $geometry = "${width}x$height";
468 }
469
470 @matchlines = grep(/default visual id/, @lines);
471 if (@matchlines) {
472 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
473
474 for ($i = 0; $i < @lines; $i++) {
475 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
476 if (($lines[$i+1] !~ /TrueColor/) ||
477 ($lines[$i+2] !~ /depth/) ||
478 ($lines[$i+4] !~ /red, green, blue masks/))
479 {
480 return;
481 }
482 last;
483 }
484 }
485
486 return if ($i >= @lines);
487
488 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
489 ($red,$green,$blue)
490 = ($lines[$i+4]
491 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
492
493 $red = hex($red);
494 $green = hex($green);
495 $blue = hex($blue);
496
497 if ($red > $blue) {
498 $red = int(log($red) / log(2)) - int(log($green) / log(2));
499 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
500 $blue = int(log($blue) / log(2)) + 1;
501 $pixelformat = "rgb$red$green$blue";
502 } else {
503 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
504 $green = int(log($green) / log(2)) - int(log($red) / log(2));
505 $red = int(log($red) / log(2)) + 1;
506 $pixelformat = "bgr$blue$green$red";
507 }
508 }
509}
510
511
512#
513# quotedString returns a string which yields the original string when parsed
514# by a shell.
515#
516
517sub quotedString
518{
519 local ($in) = @_;
520
521 $in =~ s/\'/\'\"\'\"\'/g;
522
523 return "'$in'";
524}
525
526
527#
528# removeSlashes turns slashes into underscores for use as a file name.
529#
530
531sub removeSlashes
532{
533 local ($in) = @_;
534
535 $in =~ s|/|_|g;
536
537 return "$in";
538}
539
540
541#
542# Usage
543#
544
545sub Usage
546{
547 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
548 " [-geometry <width>x<height>]\n".
549 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000550 " [-fp <font-path>]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000551 " <Xvnc-options>...\n\n".
552 " $prog -kill <X-display>\n\n");
553}
554
555
556#
557# Kill
558#
559
560sub Kill
561{
562 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
563
564 if ($opt{'-kill'} =~ /^:\d+$/) {
565 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
566 } else {
567 if ($opt{'-kill'} !~ /^$host:/) {
568 die "\nCan't tell if $opt{'-kill'} is on $host\n".
569 "Use -kill :<number> instead\n\n";
570 }
571 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
572 }
573
574 if (! -r $pidFile) {
575 die "\nCan't find file $pidFile\n".
576 "You'll have to kill the Xvnc process manually\n\n";
577 }
578
579 $SIG{'HUP'} = 'IGNORE';
580 chop($pid = `cat $pidFile`);
581 warn "Killing Xvnc process ID $pid\n";
582 system("kill $pid");
583 unlink $pidFile;
584 exit;
585}
586
587
588#
589# ParseOptions takes a list of possible options and a boolean indicating
590# whether the option has a value following, and sets up an associative array
591# %opt of the values of the options given on the command line. It removes all
592# the arguments it uses from @ARGV and returns them in @optArgs.
593#
594
595sub ParseOptions
596{
597 local (@optval) = @_;
598 local ($opt, @opts, %valFollows, @newargs);
599
600 while (@optval) {
601 $opt = shift(@optval);
602 push(@opts,$opt);
603 $valFollows{$opt} = shift(@optval);
604 }
605
606 @optArgs = ();
607 %opt = ();
608
609 arg: while (defined($arg = shift(@ARGV))) {
610 foreach $opt (@opts) {
611 if ($arg eq $opt) {
612 push(@optArgs, $arg);
613 if ($valFollows{$opt}) {
614 if (@ARGV == 0) {
615 &Usage();
616 }
617 $opt{$opt} = shift(@ARGV);
618 push(@optArgs, $opt{$opt});
619 } else {
620 $opt{$opt} = 1;
621 }
622 next arg;
623 }
624 }
625 push(@newargs,$arg);
626 }
627
628 @ARGV = @newargs;
629}
630
631
632#
633# Routine to make sure we're operating in a sane environment.
634#
635
636sub SanityCheck
637{
638 local ($cmd);
639
640 #
641 # Get the program name
642 #
643
644 ($prog) = ($0 =~ m|([^/]+)$|);
645
646 #
647 # Check we have all the commands we'll need on the path.
648 #
649
650 cmd:
DRC190854c2009-03-26 18:13:00 +0000651 foreach $cmd ("uname") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000652 for (split(/:/,$ENV{PATH})) {
653 if (-x "$_/$cmd") {
654 next cmd;
655 }
656 }
657 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
658 }
DRC190854c2009-03-26 18:13:00 +0000659 if (-x "/usr/X11R6/bin/xauth") {
660 $xauth = "/usr/X11R6/bin/xauth";
661 }
662 else {
663 cmd1:
664 foreach $cmd ("xauth") {
665 for (split(/:/,$ENV{PATH})) {
666 if (-x "$_/$cmd") {
667 next cmd1;
668 }
669 }
670 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
671 }
672 }
673
674 if($exedir eq "") {
675 cmd2:
676 foreach $cmd ("Xvnc","vncpasswd") {
677 for (split(/:/,$ENV{PATH})) {
678 if (-x "$_/$cmd") {
679 $vncClasses = "$_/../vnc/classes";
680 next cmd2;
681 }
682 }
683 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
684 }
685 }
686 else {
687 cmd3:
688 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
689 for (split(/:/,$ENV{PATH})) {
690 if (-x "$cmd") {
691 $vncClasses = $exedir."../vnc/classes";
692 next cmd3;
693 }
694 }
695 die "$prog: couldn't find \"$cmd\".\n";
696 }
697 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000698
699 #
700 # Check the HOME environment variable is set
701 #
702
703 if (!defined($ENV{HOME})) {
704 die "$prog: The HOME environment variable is not set.\n";
705 }
DRC190854c2009-03-26 18:13:00 +0000706# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000707
708 #
709 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
710 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
711 # we just guess at the values. If you find perl moaning here, just
712 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
713 # for your platform by looking in /usr/include/sys/socket.h and related
714 # files.
715 #
716
717 chop($os = `uname`);
718 chop($osrev = `uname -r`);
719
720 eval 'use Socket';
721 if ($@) {
722 eval 'require "sys/socket.ph"';
723 if ($@) {
724 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
725 $AF_INET = 2;
726 $SOCK_STREAM = 2;
727 } else {
728 $AF_INET = 2;
729 $SOCK_STREAM = 1;
730 }
731 } else {
732 $AF_INET = &AF_INET;
733 $SOCK_STREAM = &SOCK_STREAM;
734 }
735 } else {
736 $AF_INET = &AF_INET;
737 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000738 }
739}