Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # |
DRC | b9d8e76 | 2011-02-09 08:24:58 +0000 | [diff] [blame] | 3 | # Copyright (C) 2009-2010 D. R. Commander. All Rights Reserved. |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 4 | # Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 5 | # Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 6 | # 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 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 33 | $exedir = ""; |
| 34 | $slashndx = rindex($0, "/"); |
| 35 | if($slashndx>=0) { |
| 36 | $exedir = substr($0, 0, $slashndx+1); |
| 37 | } |
| 38 | |
| 39 | $vncClasses = ""; |
| 40 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 41 | &SanityCheck(); |
| 42 | |
| 43 | # |
| 44 | # Global variables. You may want to configure some of these for your site. |
| 45 | # |
| 46 | |
| 47 | $geometry = "1024x768"; |
DRC | 9d1c157 | 2009-03-26 18:18:51 +0000 | [diff] [blame] | 48 | #$depth = 16; |
DRC | e85eba5 | 2011-10-04 06:57:19 +0000 | [diff] [blame] | 49 | $vncJavaFiles = (((-d "$vncClasses") && "$vncClasses") || |
| 50 | ((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") || |
| 51 | ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes")); |
| 52 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 53 | $vncUserDir = "$ENV{HOME}/.vnc"; |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 54 | $skipxstartup = 0; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 55 | $xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority"; |
| 56 | |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 57 | $xstartup = $vncUserDir . "/xstartup"; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 58 | $defaultXStartup |
| 59 | = ("#!/bin/sh\n\n". |
DRC | 9324898 | 2009-03-26 18:14:38 +0000 | [diff] [blame] | 60 | "unset SESSION_MANAGER\n". |
Adam Tkac | c071e49 | 2009-05-20 09:01:24 +0000 | [diff] [blame] | 61 | "unset DBUS_SESSION_BUS_ADDRESS\n". |
DRC | 9324898 | 2009-03-26 18:14:38 +0000 | [diff] [blame] | 62 | "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 Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 79 | "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n". |
| 80 | "xsetroot -solid grey\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 81 | "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n". |
| 82 | "twm &\n"); |
| 83 | |
graysky | ddff8d0 | 2015-10-19 08:24:14 -0400 | [diff] [blame] | 84 | $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 Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 95 | chop($host = `uname -n`); |
| 96 | |
DRC | 989dbd1 | 2009-04-22 13:23:17 +0000 | [diff] [blame] | 97 | if (-d "/etc/X11/fontpath.d") { |
| 98 | $fontPath = "catalogue:/etc/X11/fontpath.d"; |
| 99 | } |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 100 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 101 | @fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/'); |
| 102 | if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');} |
| 103 | if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');} |
| 104 | if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');} |
| 105 | push(@fontpaths, '/usr/share/fonts/default'); |
| 106 | |
| 107 | @fonttypes = ('misc', |
| 108 | '75dpi', |
| 109 | '100dpi', |
| 110 | 'Speedo', |
| 111 | 'Type1'); |
| 112 | |
| 113 | foreach $_fpath (@fontpaths) { |
| 114 | foreach $_ftype (@fonttypes) { |
| 115 | if (-f "$_fpath/$_ftype/fonts.dir") { |
| 116 | if (! -l "$_fpath/$_ftype") { |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 117 | $defFontPath .= "$_fpath/$_ftype,"; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
DRC | d28792b | 2010-01-11 20:53:00 +0000 | [diff] [blame] | 122 | |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 123 | if ($defFontPath) { |
| 124 | if (substr($defFontPath, -1, 1) == ',') { |
| 125 | chop $defFontPath; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
DRC | d28792b | 2010-01-11 20:53:00 +0000 | [diff] [blame] | 129 | if ($fontPath eq "") { |
| 130 | $fontPath = $defFontPath; |
| 131 | } |
| 132 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 133 | # Check command line options |
| 134 | |
| 135 | &ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1, |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 136 | "-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",0,"-noxstartup",0,"-xstartup",1); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 137 | |
| 138 | &Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'}); |
| 139 | |
| 140 | &Kill() if ($opt{'-kill'}); |
| 141 | |
DRC | b9d8e76 | 2011-02-09 08:24:58 +0000 | [diff] [blame] | 142 | &List() if ($opt{'-list'}); |
| 143 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 144 | # Uncomment this line if you want default geometry, depth and pixelformat |
| 145 | # to match the current X display: |
| 146 | # &GetXDisplayDefaults(); |
| 147 | |
| 148 | if ($opt{'-geometry'}) { |
| 149 | $geometry = $opt{'-geometry'}; |
| 150 | } |
| 151 | if ($opt{'-depth'}) { |
| 152 | $depth = $opt{'-depth'}; |
| 153 | $pixelformat = ""; |
| 154 | } |
| 155 | if ($opt{'-pixelformat'}) { |
| 156 | $pixelformat = $opt{'-pixelformat'}; |
| 157 | } |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 158 | if ($opt{'-noxstartup'}) { |
| 159 | $skipxstartup = 1; |
| 160 | } |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 161 | if ($opt{'-xstartup'}) { |
| 162 | $xstartup = $opt{'-xstartup'}; |
| 163 | } |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 164 | if ($opt{'-fp'}) { |
| 165 | $fontPath = $opt{'-fp'}; |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 166 | $fpArgSpecified = 1; |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 167 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 168 | |
| 169 | &CheckGeometryAndDepth(); |
| 170 | |
| 171 | |
| 172 | # Create the user's vnc directory if necessary. |
| 173 | |
| 174 | if (!(-e $vncUserDir)) { |
| 175 | if (!mkdir($vncUserDir,0755)) { |
| 176 | die "$prog: Could not create $vncUserDir.\n"; |
| 177 | } |
| 178 | } |
| 179 | |
Adam Tkac | f586b84 | 2011-04-27 11:20:18 +0000 | [diff] [blame] | 180 | # Check whether VNC authentication is enabled, and if so, prompt the user to |
| 181 | # create a VNC password if they don't already have one. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 182 | |
Adam Tkac | f586b84 | 2011-04-27 11:20:18 +0000 | [diff] [blame] | 183 | $securityTypeArgSpecified = 0; |
| 184 | $vncAuthEnabled = 0; |
| 185 | $passwordArgSpecified = 0; |
| 186 | |
| 187 | for ($i = 0; $i < @ARGV; ++$i) { |
| 188 | # -SecurityTypes can be followed by a space or "=" |
| 189 | my @splitargs = split('=', $ARGV[$i]); |
| 190 | if (@splitargs <= 1 && $i < @ARGV - 1) { |
| 191 | push(@splitargs, $ARGV[$i + 1]); |
| 192 | } |
| 193 | if (lc(@splitargs[0]) eq "-securitytypes") { |
| 194 | if (@splitargs > 1) { |
| 195 | $securityTypeArgSpecified = 1; |
| 196 | } |
| 197 | foreach $arg2 (split(',', @splitargs[1])) { |
| 198 | if (lc($arg2) eq "vncauth" || lc($arg2) eq "tlsvnc" |
| 199 | || lc($arg2) eq "x509vnc") { |
| 200 | $vncAuthEnabled = 1; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | if ((lc(@splitargs[0]) eq "-password") |
| 205 | || (lc(@splitargs[0]) eq "-passwordfile" |
| 206 | || (lc(@splitargs[0]) eq "-rfbauth"))) { |
| 207 | $passwordArgSpecified = 1; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Adam Tkac | f586b84 | 2011-04-27 11:20:18 +0000 | [diff] [blame] | 211 | if ((!$securityTypeArgSpecified || $vncAuthEnabled) && !$passwordArgSpecified) { |
| 212 | ($z,$z,$mode) = stat("$vncUserDir/passwd"); |
| 213 | if (!(-e "$vncUserDir/passwd") || ($mode & 077)) { |
| 214 | warn "\nYou will require a password to access your desktops.\n\n"; |
| 215 | system($exedir."vncpasswd -q $vncUserDir/passwd"); |
| 216 | if (($? >> 8) != 0) { |
| 217 | exit 1; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 222 | # Find display number. |
| 223 | |
| 224 | if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) { |
| 225 | $displayNumber = $1; |
| 226 | shift(@ARGV); |
| 227 | if (!&CheckDisplayNumber($displayNumber)) { |
| 228 | die "A VNC server is already running as :$displayNumber\n"; |
| 229 | } |
Adam Tkac | 39c9d99 | 2010-07-21 14:08:38 +0000 | [diff] [blame] | 230 | } elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 231 | &Usage(); |
| 232 | } else { |
| 233 | $displayNumber = &GetDisplayNumber(); |
| 234 | } |
| 235 | |
| 236 | $vncPort = 5900 + $displayNumber; |
| 237 | |
| 238 | $desktopLog = "$vncUserDir/$host:$displayNumber.log"; |
| 239 | unlink($desktopLog); |
| 240 | |
Adam Tkac | 6cbd9d1 | 2009-11-12 10:39:54 +0000 | [diff] [blame] | 241 | # Make an X server cookie - use /dev/urandom on systems that have it, |
| 242 | # otherwise use perl's random number generator, seeded with the sum |
| 243 | # of the current time, our PID and part of the encrypted form of the password. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 244 | |
Adam Tkac | 6cbd9d1 | 2009-11-12 10:39:54 +0000 | [diff] [blame] | 245 | my $cookie = ""; |
| 246 | if (open(URANDOM, '<', '/dev/urandom')) { |
| 247 | my $randata; |
| 248 | if (sysread(URANDOM, $randata, 16) == 16) { |
| 249 | $cookie = unpack 'h*', $randata; |
| 250 | } |
| 251 | close(URANDOM); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 252 | } |
Adam Tkac | 6cbd9d1 | 2009-11-12 10:39:54 +0000 | [diff] [blame] | 253 | if ($cookie eq "") { |
| 254 | srand(time+$$+unpack("L",`cat $vncUserDir/passwd`)); |
| 255 | for (1..16) { |
| 256 | $cookie .= sprintf("%02x", int(rand(256)) % 256); |
| 257 | } |
| 258 | } |
| 259 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 260 | system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie"); |
| 261 | system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie"); |
| 262 | |
| 263 | if ($opt{'-name'}) { |
| 264 | $desktopName = $opt{'-name'}; |
| 265 | } else { |
| 266 | $desktopName = "$host:$displayNumber ($ENV{USER})"; |
| 267 | } |
| 268 | |
| 269 | # Now start the X VNC Server |
| 270 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 271 | $cmd = $exedir."Xvnc :$displayNumber"; |
graysky | ddff8d0 | 2015-10-19 08:24:14 -0400 | [diff] [blame] | 272 | |
| 273 | my %default_opts; |
| 274 | my %config; |
| 275 | |
| 276 | $default_opts{desktop} = "edString($desktopName); |
| 277 | $default_opts{httpd} = $vncJavaFiles if ($vncJavaFiles); |
| 278 | $default_opts{auth} = $xauthorityFile; |
| 279 | $default_opts{geometry} = $geometry if ($geometry); |
| 280 | $default_opts{depth} = $depth if ($depth); |
| 281 | $default_opts{pixelformat} = $pixelformat if ($pixelformat); |
| 282 | $default_opts{rfbwait} = 30000; |
| 283 | $default_opts{rfbauth} = "$vncUserDir/passwd"; |
| 284 | $default_opts{rfbport} = $vncPort; |
| 285 | $default_opts{fp} = $fontPath if ($fontPath); |
| 286 | $default_opts{pn} = ""; |
| 287 | |
| 288 | # if a user configuration file already exists |
| 289 | if(stat("$vncUserDir/config")) { |
| 290 | |
| 291 | # loads and parses configuration file |
| 292 | if(open(IN, "$vncUserDir/config")) { |
| 293 | while(<IN>) { |
| 294 | next if /^#/; |
| 295 | if(my ($k, $v) = /^\s*(\w+)\s*=\s*(.+)$/) { |
| 296 | $config{$k} = $v; |
| 297 | } elsif ($_ =~ m/^\s*(\S+)/) { |
| 298 | $config{$1} = $k; |
| 299 | } |
| 300 | } |
| 301 | close(IN); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | foreach my $k (sort keys %config) { |
| 306 | $cmd .= " -$k $config{$k}"; |
| 307 | # user's option takes precedence |
| 308 | delete $default_opts{$k}; |
| 309 | } |
| 310 | |
| 311 | foreach my $k (sort keys %default_opts) { |
| 312 | $cmd .= " -$k $default_opts{$k}"; |
| 313 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 314 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 315 | # Add color database stuff here, e.g.: |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 316 | # |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 317 | # $cmd .= " -co /usr/lib/X11/rgb"; |
| 318 | # |
| 319 | |
| 320 | foreach $arg (@ARGV) { |
| 321 | $cmd .= " " . "edString($arg); |
| 322 | } |
| 323 | $cmd .= " >> " . "edString($desktopLog) . " 2>&1"; |
| 324 | |
| 325 | # Run $cmd and record the process ID. |
| 326 | |
| 327 | $pidFile = "$vncUserDir/$host:$displayNumber.pid"; |
| 328 | system("$cmd & echo \$! >$pidFile"); |
| 329 | |
| 330 | # Give Xvnc a chance to start up |
| 331 | |
| 332 | sleep(3); |
DRC | d28792b | 2010-01-11 20:53:00 +0000 | [diff] [blame] | 333 | if ($fontPath ne $defFontPath) { |
| 334 | unless (kill 0, `cat $pidFile`) { |
| 335 | if ($fpArgSpecified) { |
| 336 | warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n"; |
| 337 | warn "path you specified using the -fp argument is incorrect. Attempting to\n"; |
| 338 | warn "determine an appropriate font path for this system and restart Xvnc using\n"; |
| 339 | warn "that font path ...\n"; |
| 340 | } else { |
| 341 | warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n"; |
| 342 | warn "catalog is not properly configured. Attempting to determine an appropriate\n"; |
| 343 | warn "font path for this system and restart Xvnc using that font path ...\n"; |
| 344 | } |
| 345 | $cmd =~ s@-fp [^ ]+@@; |
| 346 | $cmd .= " -fp $defFontPath" if ($defFontPath); |
| 347 | system("$cmd & echo \$! >$pidFile"); |
| 348 | sleep(3); |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 349 | } |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 350 | } |
| 351 | unless (kill 0, `cat $pidFile`) { |
| 352 | warn "Could not start Xvnc.\n\n"; |
Michal Srb | e6e11f9 | 2015-10-02 02:28:26 +0300 | [diff] [blame] | 353 | unlink $pidFile; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 354 | open(LOG, "<$desktopLog"); |
| 355 | while (<LOG>) { print; } |
| 356 | close(LOG); |
| 357 | die "\n"; |
| 358 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 359 | |
| 360 | warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n"; |
| 361 | |
| 362 | # Create the user's xstartup script if necessary. |
| 363 | |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 364 | if (! $skipxstartup) { |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 365 | if (!(-e "$xstartup")) { |
| 366 | warn "Creating default startup script $xstartup\n"; |
| 367 | open(XSTARTUP, ">$xstartup"); |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 368 | print XSTARTUP $defaultXStartup; |
| 369 | close(XSTARTUP); |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 370 | chmod 0755, "$xstartup"; |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 371 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 372 | } |
| 373 | |
graysky | ddff8d0 | 2015-10-19 08:24:14 -0400 | [diff] [blame] | 374 | # Create the user's config file if necessary. |
| 375 | |
| 376 | if (!(-e "$vncUserDir/config")) { |
| 377 | warn "Creating default config $vncUserDir/config\n"; |
| 378 | open(XSTARTUP, ">$vncUserDir/config"); |
| 379 | print XSTARTUP $defaultConfig; |
| 380 | close(XSTARTUP); |
| 381 | chmod 0644, "$vncUserDir/config"; |
| 382 | } |
| 383 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 384 | # Run the X startup script. |
| 385 | |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 386 | if (! $skipxstartup) { |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 387 | warn "Starting applications specified in $xstartup\n"; |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 388 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 389 | warn "Log file is $desktopLog\n\n"; |
| 390 | |
| 391 | # If the unix domain socket exists then use that (DISPLAY=:n) otherwise use |
| 392 | # TCP (DISPLAY=host:n) |
| 393 | |
| 394 | if (-e "/tmp/.X11-unix/X$displayNumber" || |
| 395 | -e "/usr/spool/sockets/X11/$displayNumber") |
| 396 | { |
| 397 | $ENV{DISPLAY}= ":$displayNumber"; |
| 398 | } else { |
| 399 | $ENV{DISPLAY}= "$host:$displayNumber"; |
| 400 | } |
| 401 | $ENV{VNCDESKTOP}= $desktopName; |
| 402 | |
Pierre Ossman | a5b37c0 | 2015-07-30 11:04:02 +0200 | [diff] [blame] | 403 | system($exedir."vncconfig -nowin >> " . "edString($desktopLog) . " 2>&1 &"); |
DRC | f6b5840 | 2011-10-05 21:28:03 +0000 | [diff] [blame] | 404 | |
DRC | 8fb1191 | 2011-03-03 10:42:14 +0000 | [diff] [blame] | 405 | if ($opt{'-fg'}) { |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 406 | if (! $skipxstartup) { |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 407 | system("$xstartup >> " . "edString($desktopLog) . " 2>&1"); |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 408 | } |
Adam Tkac | 38ba8cf | 2011-04-27 11:28:09 +0000 | [diff] [blame] | 409 | if (kill 0, `cat $pidFile`) { |
| 410 | $opt{'-kill'} = ':'.$displayNumber; |
| 411 | &Kill(); |
| 412 | } |
DRC | 8fb1191 | 2011-03-03 10:42:14 +0000 | [diff] [blame] | 413 | } else { |
Adam Tkac | 38ba8cf | 2011-04-27 11:28:09 +0000 | [diff] [blame] | 414 | if ($opt{'-autokill'}) { |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 415 | if (! $skipxstartup) { |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 416 | system("($xstartup; $0 -kill :$displayNumber) >> " |
| 417 | . "edString($desktopLog) . " 2>&1 &"); |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 418 | } |
Adam Tkac | 38ba8cf | 2011-04-27 11:28:09 +0000 | [diff] [blame] | 419 | } else { |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 420 | if (! $skipxstartup) { |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 421 | system("$xstartup >> " . "edString($desktopLog) |
| 422 | . " 2>&1 &"); |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 423 | } |
Adam Tkac | 38ba8cf | 2011-04-27 11:28:09 +0000 | [diff] [blame] | 424 | } |
DRC | 8fb1191 | 2011-03-03 10:42:14 +0000 | [diff] [blame] | 425 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 426 | |
| 427 | exit; |
| 428 | |
| 429 | |
| 430 | ############################################################################### |
| 431 | # |
| 432 | # CheckGeometryAndDepth simply makes sure that the geometry and depth values |
| 433 | # are sensible. |
| 434 | # |
| 435 | |
| 436 | sub CheckGeometryAndDepth |
| 437 | { |
| 438 | if ($geometry =~ /^(\d+)x(\d+)$/) { |
| 439 | $width = $1; $height = $2; |
| 440 | |
| 441 | if (($width<1) || ($height<1)) { |
| 442 | die "$prog: geometry $geometry is invalid\n"; |
| 443 | } |
| 444 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 445 | $geometry = "${width}x$height"; |
| 446 | } else { |
| 447 | die "$prog: geometry $geometry is invalid\n"; |
| 448 | } |
| 449 | |
DRC | e5b4f75 | 2009-03-26 18:23:29 +0000 | [diff] [blame] | 450 | if ($depth && (($depth < 8) || ($depth > 32))) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 451 | die "Depth must be between 8 and 32\n"; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | |
| 456 | # |
| 457 | # GetDisplayNumber gets the lowest available display number. A display number |
| 458 | # n is taken if something is listening on the VNC server port (5900+n) or the |
| 459 | # X server port (6000+n). |
| 460 | # |
| 461 | |
| 462 | sub GetDisplayNumber |
| 463 | { |
| 464 | foreach $n (1..99) { |
| 465 | if (&CheckDisplayNumber($n)) { |
| 466 | return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02 |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | die "$prog: no free display number on $host.\n"; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | # |
| 475 | # CheckDisplayNumber checks if the given display number is available. A |
| 476 | # display number n is taken if something is listening on the VNC server port |
| 477 | # (5900+n) or the X server port (6000+n). |
| 478 | # |
| 479 | |
| 480 | sub CheckDisplayNumber |
| 481 | { |
| 482 | local ($n) = @_; |
| 483 | |
| 484 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 485 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 486 | if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) { |
| 487 | close(S); |
| 488 | return 0; |
| 489 | } |
| 490 | close(S); |
| 491 | |
| 492 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 493 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 494 | if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) { |
| 495 | close(S); |
| 496 | return 0; |
| 497 | } |
| 498 | close(S); |
| 499 | |
| 500 | if (-e "/tmp/.X$n-lock") { |
| 501 | warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n"; |
| 502 | warn "Remove this file if there is no X server $host:$n\n"; |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | if (-e "/tmp/.X11-unix/X$n") { |
| 507 | warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n"; |
| 508 | warn "Remove this file if there is no X server $host:$n\n"; |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | if (-e "/usr/spool/sockets/X11/$n") { |
| 513 | warn("\nWarning: $host:$n is taken because of ". |
| 514 | "/usr/spool/sockets/X11/$n\n"); |
| 515 | warn "Remove this file if there is no X server $host:$n\n"; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | return 1; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | # |
| 524 | # GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel |
| 525 | # format of the current X display being used. If successful, it sets the |
| 526 | # options as appropriate so that the X VNC server will use the same settings |
| 527 | # (minus an allowance for window manager decorations on the geometry). Using |
| 528 | # the same depth and pixel format means that the VNC server won't have to |
| 529 | # translate pixels when the desktop is being viewed on this X display (for |
| 530 | # TrueColor displays anyway). |
| 531 | # |
| 532 | |
| 533 | sub GetXDisplayDefaults |
| 534 | { |
| 535 | local (@lines, @matchlines, $width, $height, $defaultVisualId, $i, |
| 536 | $red, $green, $blue); |
| 537 | |
| 538 | $wmDecorationWidth = 4; # a guess at typical size for window manager |
| 539 | $wmDecorationHeight = 24; # decoration size |
| 540 | |
| 541 | return if (!defined($ENV{DISPLAY})); |
| 542 | |
| 543 | @lines = `xdpyinfo 2>/dev/null`; |
| 544 | |
| 545 | return if ($? != 0); |
| 546 | |
| 547 | @matchlines = grep(/dimensions/, @lines); |
| 548 | if (@matchlines) { |
| 549 | ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/); |
| 550 | |
| 551 | $width -= $wmDecorationWidth; |
| 552 | $height -= $wmDecorationHeight; |
| 553 | |
| 554 | $geometry = "${width}x$height"; |
| 555 | } |
| 556 | |
| 557 | @matchlines = grep(/default visual id/, @lines); |
| 558 | if (@matchlines) { |
| 559 | ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/); |
| 560 | |
| 561 | for ($i = 0; $i < @lines; $i++) { |
| 562 | if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) { |
| 563 | if (($lines[$i+1] !~ /TrueColor/) || |
| 564 | ($lines[$i+2] !~ /depth/) || |
| 565 | ($lines[$i+4] !~ /red, green, blue masks/)) |
| 566 | { |
| 567 | return; |
| 568 | } |
| 569 | last; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return if ($i >= @lines); |
| 574 | |
| 575 | ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/); |
| 576 | ($red,$green,$blue) |
| 577 | = ($lines[$i+4] |
| 578 | =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/); |
| 579 | |
| 580 | $red = hex($red); |
| 581 | $green = hex($green); |
| 582 | $blue = hex($blue); |
| 583 | |
| 584 | if ($red > $blue) { |
| 585 | $red = int(log($red) / log(2)) - int(log($green) / log(2)); |
| 586 | $green = int(log($green) / log(2)) - int(log($blue) / log(2)); |
| 587 | $blue = int(log($blue) / log(2)) + 1; |
| 588 | $pixelformat = "rgb$red$green$blue"; |
| 589 | } else { |
| 590 | $blue = int(log($blue) / log(2)) - int(log($green) / log(2)); |
| 591 | $green = int(log($green) / log(2)) - int(log($red) / log(2)); |
| 592 | $red = int(log($red) / log(2)) + 1; |
| 593 | $pixelformat = "bgr$blue$green$red"; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | |
| 599 | # |
| 600 | # quotedString returns a string which yields the original string when parsed |
| 601 | # by a shell. |
| 602 | # |
| 603 | |
| 604 | sub quotedString |
| 605 | { |
| 606 | local ($in) = @_; |
| 607 | |
| 608 | $in =~ s/\'/\'\"\'\"\'/g; |
| 609 | |
| 610 | return "'$in'"; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | # |
| 615 | # removeSlashes turns slashes into underscores for use as a file name. |
| 616 | # |
| 617 | |
| 618 | sub removeSlashes |
| 619 | { |
| 620 | local ($in) = @_; |
| 621 | |
| 622 | $in =~ s|/|_|g; |
| 623 | |
| 624 | return "$in"; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | # |
| 629 | # Usage |
| 630 | # |
| 631 | |
| 632 | sub Usage |
| 633 | { |
| 634 | die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n". |
| 635 | " [-geometry <width>x<height>]\n". |
| 636 | " [-pixelformat rgbNNN|bgrNNN]\n". |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 637 | " [-fp <font-path>]\n". |
DRC | 8fb1191 | 2011-03-03 10:42:14 +0000 | [diff] [blame] | 638 | " [-fg]\n". |
Adam Tkac | 38ba8cf | 2011-04-27 11:28:09 +0000 | [diff] [blame] | 639 | " [-autokill]\n". |
Llorenç Garcia Martinez | 861cb06 | 2015-10-23 13:37:42 +0200 | [diff] [blame] | 640 | " [-noxstartup]\n". |
Llorenç Garcia Martinez | e76c2fb | 2015-10-30 11:07:40 +0100 | [diff] [blame] | 641 | " [-xstartup <file>]\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 642 | " <Xvnc-options>...\n\n". |
DRC | b9d8e76 | 2011-02-09 08:24:58 +0000 | [diff] [blame] | 643 | " $prog -kill <X-display>\n\n". |
| 644 | " $prog -list\n\n"); |
| 645 | } |
| 646 | |
| 647 | |
| 648 | # |
| 649 | # List |
| 650 | # |
| 651 | |
| 652 | sub List |
| 653 | { |
| 654 | opendir(dir, $vncUserDir); |
| 655 | my @filelist = readdir(dir); |
| 656 | closedir(dir); |
DRC | 075d9fa | 2011-02-10 04:19:46 +0000 | [diff] [blame] | 657 | print "\nTigerVNC server sessions:\n\n"; |
DRC | b9d8e76 | 2011-02-09 08:24:58 +0000 | [diff] [blame] | 658 | print "X DISPLAY #\tPROCESS ID\n"; |
| 659 | foreach my $file (@filelist) { |
| 660 | if ($file =~ /$host:(\d+)$\.pid/) { |
Michal Srb | e6e11f9 | 2015-10-02 02:28:26 +0300 | [diff] [blame] | 661 | chop($tmp_pid = `cat $vncUserDir/$file`); |
| 662 | if (kill 0, $tmp_pid) { |
| 663 | print ":".$1."\t\t".`cat $vncUserDir/$file`; |
| 664 | } else { |
| 665 | unlink ($vncUserDir . "/" . $file); |
| 666 | } |
DRC | b9d8e76 | 2011-02-09 08:24:58 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | exit 1; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
| 673 | # |
| 674 | # Kill |
| 675 | # |
| 676 | |
| 677 | sub Kill |
| 678 | { |
| 679 | $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1 |
| 680 | |
| 681 | if ($opt{'-kill'} =~ /^:\d+$/) { |
| 682 | $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid"; |
| 683 | } else { |
| 684 | if ($opt{'-kill'} !~ /^$host:/) { |
| 685 | die "\nCan't tell if $opt{'-kill'} is on $host\n". |
| 686 | "Use -kill :<number> instead\n\n"; |
| 687 | } |
| 688 | $pidFile = "$vncUserDir/$opt{'-kill'}.pid"; |
| 689 | } |
| 690 | |
| 691 | if (! -r $pidFile) { |
| 692 | die "\nCan't find file $pidFile\n". |
| 693 | "You'll have to kill the Xvnc process manually\n\n"; |
| 694 | } |
| 695 | |
| 696 | $SIG{'HUP'} = 'IGNORE'; |
| 697 | chop($pid = `cat $pidFile`); |
| 698 | warn "Killing Xvnc process ID $pid\n"; |
DRC | b9d8e76 | 2011-02-09 08:24:58 +0000 | [diff] [blame] | 699 | |
| 700 | if (kill 0, $pid) { |
| 701 | system("kill $pid"); |
| 702 | sleep(1); |
| 703 | if (kill 0, $pid) { |
| 704 | print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n"; |
| 705 | print " ".$0." -kill ".$opt{'-kill'}."\n"; |
| 706 | print "to clean up the socket files.\n"; |
| 707 | exit |
| 708 | } |
| 709 | |
| 710 | } else { |
| 711 | warn "Xvnc process ID $pid already killed\n"; |
| 712 | $opt{'-kill'} =~ s/://; |
| 713 | |
| 714 | if (-e "/tmp/.X11-unix/X$opt{'-kill'}") { |
| 715 | print "Xvnc did not appear to shut down cleanly."; |
| 716 | print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n"; |
| 717 | unlink "/tmp/.X11-unix/X$opt{'-kill'}"; |
| 718 | } |
| 719 | if (-e "/tmp/.X$opt{'-kill'}-lock") { |
| 720 | print "Xvnc did not appear to shut down cleanly."; |
| 721 | print " Removing /tmp/.X$opt{'-kill'}-lock\n"; |
| 722 | unlink "/tmp/.X$opt{'-kill'}-lock"; |
| 723 | } |
| 724 | } |
| 725 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 726 | unlink $pidFile; |
| 727 | exit; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | # |
| 732 | # ParseOptions takes a list of possible options and a boolean indicating |
| 733 | # whether the option has a value following, and sets up an associative array |
| 734 | # %opt of the values of the options given on the command line. It removes all |
| 735 | # the arguments it uses from @ARGV and returns them in @optArgs. |
| 736 | # |
| 737 | |
| 738 | sub ParseOptions |
| 739 | { |
| 740 | local (@optval) = @_; |
| 741 | local ($opt, @opts, %valFollows, @newargs); |
| 742 | |
| 743 | while (@optval) { |
| 744 | $opt = shift(@optval); |
| 745 | push(@opts,$opt); |
| 746 | $valFollows{$opt} = shift(@optval); |
| 747 | } |
| 748 | |
| 749 | @optArgs = (); |
| 750 | %opt = (); |
| 751 | |
| 752 | arg: while (defined($arg = shift(@ARGV))) { |
| 753 | foreach $opt (@opts) { |
| 754 | if ($arg eq $opt) { |
| 755 | push(@optArgs, $arg); |
| 756 | if ($valFollows{$opt}) { |
| 757 | if (@ARGV == 0) { |
| 758 | &Usage(); |
| 759 | } |
| 760 | $opt{$opt} = shift(@ARGV); |
| 761 | push(@optArgs, $opt{$opt}); |
| 762 | } else { |
| 763 | $opt{$opt} = 1; |
| 764 | } |
| 765 | next arg; |
| 766 | } |
| 767 | } |
| 768 | push(@newargs,$arg); |
| 769 | } |
| 770 | |
| 771 | @ARGV = @newargs; |
| 772 | } |
| 773 | |
| 774 | |
| 775 | # |
| 776 | # Routine to make sure we're operating in a sane environment. |
| 777 | # |
| 778 | |
| 779 | sub SanityCheck |
| 780 | { |
| 781 | local ($cmd); |
| 782 | |
| 783 | # |
| 784 | # Get the program name |
| 785 | # |
| 786 | |
| 787 | ($prog) = ($0 =~ m|([^/]+)$|); |
| 788 | |
| 789 | # |
| 790 | # Check we have all the commands we'll need on the path. |
| 791 | # |
| 792 | |
| 793 | cmd: |
Pierre Ossman | a7ac6a9 | 2015-12-29 14:24:55 +0100 | [diff] [blame^] | 794 | foreach $cmd ("uname","xauth") { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 795 | for (split(/:/,$ENV{PATH})) { |
| 796 | if (-x "$_/$cmd") { |
| 797 | next cmd; |
| 798 | } |
| 799 | } |
| 800 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 801 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 802 | |
| 803 | if($exedir eq "") { |
| 804 | cmd2: |
| 805 | foreach $cmd ("Xvnc","vncpasswd") { |
| 806 | for (split(/:/,$ENV{PATH})) { |
| 807 | if (-x "$_/$cmd") { |
| 808 | $vncClasses = "$_/../vnc/classes"; |
| 809 | next cmd2; |
| 810 | } |
| 811 | } |
| 812 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 813 | } |
| 814 | } |
| 815 | else { |
| 816 | cmd3: |
| 817 | foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") { |
| 818 | for (split(/:/,$ENV{PATH})) { |
| 819 | if (-x "$cmd") { |
| 820 | $vncClasses = $exedir."../vnc/classes"; |
| 821 | next cmd3; |
| 822 | } |
| 823 | } |
| 824 | die "$prog: couldn't find \"$cmd\".\n"; |
| 825 | } |
| 826 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 827 | |
| 828 | # |
| 829 | # Check the HOME environment variable is set |
| 830 | # |
| 831 | |
| 832 | if (!defined($ENV{HOME})) { |
| 833 | die "$prog: The HOME environment variable is not set.\n"; |
| 834 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 835 | # chdir($ENV{HOME}); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 836 | |
| 837 | # |
| 838 | # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an |
| 839 | # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails, |
| 840 | # we just guess at the values. If you find perl moaning here, just |
| 841 | # hard-code the values of AF_INET and SOCK_STREAM. You can find these out |
| 842 | # for your platform by looking in /usr/include/sys/socket.h and related |
| 843 | # files. |
| 844 | # |
| 845 | |
| 846 | chop($os = `uname`); |
| 847 | chop($osrev = `uname -r`); |
| 848 | |
| 849 | eval 'use Socket'; |
| 850 | if ($@) { |
| 851 | eval 'require "sys/socket.ph"'; |
| 852 | if ($@) { |
| 853 | if (($os eq "SunOS") && ($osrev !~ /^4/)) { |
| 854 | $AF_INET = 2; |
| 855 | $SOCK_STREAM = 2; |
| 856 | } else { |
| 857 | $AF_INET = 2; |
| 858 | $SOCK_STREAM = 1; |
| 859 | } |
| 860 | } else { |
| 861 | $AF_INET = &AF_INET; |
| 862 | $SOCK_STREAM = &SOCK_STREAM; |
| 863 | } |
| 864 | } else { |
| 865 | $AF_INET = &AF_INET; |
| 866 | $SOCK_STREAM = &SOCK_STREAM; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 867 | } |
| 868 | } |