Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 3 | # Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 4 | # Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 5 | # 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 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 32 | $exedir = ""; |
| 33 | $slashndx = rindex($0, "/"); |
| 34 | if($slashndx>=0) { |
| 35 | $exedir = substr($0, 0, $slashndx+1); |
| 36 | } |
| 37 | |
| 38 | $vncClasses = ""; |
| 39 | |
| 40 | $xauth = "xauth"; |
| 41 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 42 | &SanityCheck(); |
| 43 | |
| 44 | # |
| 45 | # Global variables. You may want to configure some of these for your site. |
| 46 | # |
| 47 | |
| 48 | $geometry = "1024x768"; |
DRC | 9d1c157 | 2009-03-26 18:18:51 +0000 | [diff] [blame^] | 49 | #$depth = 16; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 50 | $vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") || |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 51 | ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes") || |
| 52 | ((-d "$vncClasses") && "$vncClasses")); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 53 | $vncUserDir = "$ENV{HOME}/.vnc"; |
| 54 | $xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority"; |
| 55 | |
| 56 | $defaultXStartup |
| 57 | = ("#!/bin/sh\n\n". |
DRC | 9324898 | 2009-03-26 18:14:38 +0000 | [diff] [blame] | 58 | "unset SESSION_MANAGER\n". |
| 59 | "OS=`uname -s`\n". |
| 60 | "if [ \$OS = 'Linux' ]; then\n". |
| 61 | " case \"\$WINDOWMANAGER\" in\n". |
| 62 | " \*gnome\*)\n". |
| 63 | " if [ -e /etc/SuSE-release ]; then\n". |
| 64 | " PATH=\$PATH:/opt/gnome/bin\n". |
| 65 | " export PATH\n". |
| 66 | " fi\n". |
| 67 | " ;;\n". |
| 68 | " esac\n". |
| 69 | "fi\n". |
| 70 | "if [ -x /etc/X11/xinit/xinitrc ]; then\n". |
| 71 | " exec /etc/X11/xinit/xinitrc\n". |
| 72 | "fi\n". |
| 73 | "if [ -f /etc/X11/xinit/xinitrc ]; then\n". |
| 74 | " exec sh /etc/X11/xinit/xinitrc\n". |
| 75 | "fi\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 76 | "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n". |
| 77 | "xsetroot -solid grey\n". |
| 78 | "vncconfig -iconic &\n". |
| 79 | "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n". |
| 80 | "twm &\n"); |
| 81 | |
| 82 | chop($host = `uname -n`); |
| 83 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 84 | @fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/'); |
| 85 | if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');} |
| 86 | if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');} |
| 87 | if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');} |
| 88 | push(@fontpaths, '/usr/share/fonts/default'); |
| 89 | |
| 90 | @fonttypes = ('misc', |
| 91 | '75dpi', |
| 92 | '100dpi', |
| 93 | 'Speedo', |
| 94 | 'Type1'); |
| 95 | |
| 96 | foreach $_fpath (@fontpaths) { |
| 97 | foreach $_ftype (@fonttypes) { |
| 98 | if (-f "$_fpath/$_ftype/fonts.dir") { |
| 99 | if (! -l "$_fpath/$_ftype") { |
| 100 | $fontPath .= "$_fpath/$_ftype,"; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | if ($fontPath) { |
| 106 | if (substr($fontPath, -1, 1) == ',') { |
| 107 | chop $fontPath; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | $defFontPath = "unix/:7100"; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 112 | |
| 113 | # Check command line options |
| 114 | |
| 115 | &ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1, |
| 116 | "-help",0,"-h",0,"--help",0); |
| 117 | |
| 118 | &Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'}); |
| 119 | |
| 120 | &Kill() if ($opt{'-kill'}); |
| 121 | |
| 122 | # Uncomment this line if you want default geometry, depth and pixelformat |
| 123 | # to match the current X display: |
| 124 | # &GetXDisplayDefaults(); |
| 125 | |
| 126 | if ($opt{'-geometry'}) { |
| 127 | $geometry = $opt{'-geometry'}; |
| 128 | } |
| 129 | if ($opt{'-depth'}) { |
| 130 | $depth = $opt{'-depth'}; |
| 131 | $pixelformat = ""; |
| 132 | } |
| 133 | if ($opt{'-pixelformat'}) { |
| 134 | $pixelformat = $opt{'-pixelformat'}; |
| 135 | } |
| 136 | |
| 137 | &CheckGeometryAndDepth(); |
| 138 | |
| 139 | |
| 140 | # Create the user's vnc directory if necessary. |
| 141 | |
| 142 | if (!(-e $vncUserDir)) { |
| 143 | if (!mkdir($vncUserDir,0755)) { |
| 144 | die "$prog: Could not create $vncUserDir.\n"; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | # Make sure the user has a password. |
| 149 | |
| 150 | ($z,$z,$mode) = stat("$vncUserDir/passwd"); |
| 151 | if (!(-e "$vncUserDir/passwd") || ($mode & 077)) { |
| 152 | warn "\nYou will require a password to access your desktops.\n\n"; |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 153 | system($exedir."vncpasswd -q $vncUserDir/passwd"); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 154 | if (($? >> 8) != 0) { |
| 155 | exit 1; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | # Find display number. |
| 160 | |
| 161 | if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) { |
| 162 | $displayNumber = $1; |
| 163 | shift(@ARGV); |
| 164 | if (!&CheckDisplayNumber($displayNumber)) { |
| 165 | die "A VNC server is already running as :$displayNumber\n"; |
| 166 | } |
| 167 | } elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/)) { |
| 168 | &Usage(); |
| 169 | } else { |
| 170 | $displayNumber = &GetDisplayNumber(); |
| 171 | } |
| 172 | |
| 173 | $vncPort = 5900 + $displayNumber; |
| 174 | |
| 175 | $desktopLog = "$vncUserDir/$host:$displayNumber.log"; |
| 176 | unlink($desktopLog); |
| 177 | |
| 178 | # Make an X server cookie - use as the seed the sum of the current time, our |
| 179 | # PID and part of the encrypted form of the password. Ideally we'd use |
| 180 | # /dev/urandom, but that's only available on Linux. |
| 181 | |
| 182 | srand(time+$$+unpack("L",`cat $vncUserDir/passwd`)); |
| 183 | $cookie = ""; |
| 184 | for (1..16) { |
| 185 | $cookie .= sprintf("%02x", int(rand(256)) % 256); |
| 186 | } |
| 187 | |
| 188 | system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie"); |
| 189 | system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie"); |
| 190 | |
| 191 | if ($opt{'-name'}) { |
| 192 | $desktopName = $opt{'-name'}; |
| 193 | } else { |
| 194 | $desktopName = "$host:$displayNumber ($ENV{USER})"; |
| 195 | } |
| 196 | |
| 197 | # Now start the X VNC Server |
| 198 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 199 | $cmd = $exedir."Xvnc :$displayNumber"; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 200 | $cmd .= " -desktop " . "edString($desktopName); |
| 201 | $cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles); |
| 202 | $cmd .= " -auth $xauthorityFile"; |
| 203 | $cmd .= " -geometry $geometry" if ($geometry); |
| 204 | $cmd .= " -depth $depth" if ($depth); |
| 205 | $cmd .= " -pixelformat $pixelformat" if ($pixelformat); |
| 206 | $cmd .= " -rfbwait 30000"; |
| 207 | $cmd .= " -rfbauth $vncUserDir/passwd"; |
| 208 | $cmd .= " -rfbport $vncPort"; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 209 | $cmd .= " -fp $fontPath" if ($fontPath); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 210 | $cmd .= " -pn"; |
| 211 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 212 | # Add color database stuff here, e.g.: |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 213 | # |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 214 | # $cmd .= " -co /usr/lib/X11/rgb"; |
| 215 | # |
| 216 | |
| 217 | foreach $arg (@ARGV) { |
| 218 | $cmd .= " " . "edString($arg); |
| 219 | } |
| 220 | $cmd .= " >> " . "edString($desktopLog) . " 2>&1"; |
| 221 | |
| 222 | # Run $cmd and record the process ID. |
| 223 | |
| 224 | $pidFile = "$vncUserDir/$host:$displayNumber.pid"; |
| 225 | system("$cmd & echo \$! >$pidFile"); |
| 226 | |
| 227 | # Give Xvnc a chance to start up |
| 228 | |
| 229 | sleep(3); |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 230 | unless (kill 0, `cat $pidFile`) { |
| 231 | warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the vncserver\n"; |
| 232 | warn "script was not able to figure out an appropriate X11 font path for this system.\n"; |
| 233 | warn "Attempting to restart Xvnc using the X Font Server (xfs) ...\n"; |
| 234 | $cmd =~ s@-fp [^ ]+@@; |
| 235 | $cmd .= " -fp $defFontPath" if ($defFontPath); |
| 236 | system("$cmd & echo \$! >$pidFile"); |
| 237 | sleep(3); |
| 238 | } |
| 239 | unless (kill 0, `cat $pidFile`) { |
| 240 | warn "Could not start Xvnc.\n\n"; |
| 241 | open(LOG, "<$desktopLog"); |
| 242 | while (<LOG>) { print; } |
| 243 | close(LOG); |
| 244 | die "\n"; |
| 245 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 246 | |
| 247 | warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n"; |
| 248 | |
| 249 | # Create the user's xstartup script if necessary. |
| 250 | |
| 251 | if (!(-e "$vncUserDir/xstartup")) { |
| 252 | warn "Creating default startup script $vncUserDir/xstartup\n"; |
| 253 | open(XSTARTUP, ">$vncUserDir/xstartup"); |
| 254 | print XSTARTUP $defaultXStartup; |
| 255 | close(XSTARTUP); |
| 256 | chmod 0755, "$vncUserDir/xstartup"; |
| 257 | } |
| 258 | |
| 259 | # Run the X startup script. |
| 260 | |
| 261 | warn "Starting applications specified in $vncUserDir/xstartup\n"; |
| 262 | warn "Log file is $desktopLog\n\n"; |
| 263 | |
| 264 | # If the unix domain socket exists then use that (DISPLAY=:n) otherwise use |
| 265 | # TCP (DISPLAY=host:n) |
| 266 | |
| 267 | if (-e "/tmp/.X11-unix/X$displayNumber" || |
| 268 | -e "/usr/spool/sockets/X11/$displayNumber") |
| 269 | { |
| 270 | $ENV{DISPLAY}= ":$displayNumber"; |
| 271 | } else { |
| 272 | $ENV{DISPLAY}= "$host:$displayNumber"; |
| 273 | } |
| 274 | $ENV{VNCDESKTOP}= $desktopName; |
| 275 | |
| 276 | system("$vncUserDir/xstartup >> " . "edString($desktopLog) . " 2>&1 &"); |
| 277 | |
| 278 | exit; |
| 279 | |
| 280 | |
| 281 | ############################################################################### |
| 282 | # |
| 283 | # CheckGeometryAndDepth simply makes sure that the geometry and depth values |
| 284 | # are sensible. |
| 285 | # |
| 286 | |
| 287 | sub CheckGeometryAndDepth |
| 288 | { |
| 289 | if ($geometry =~ /^(\d+)x(\d+)$/) { |
| 290 | $width = $1; $height = $2; |
| 291 | |
| 292 | if (($width<1) || ($height<1)) { |
| 293 | die "$prog: geometry $geometry is invalid\n"; |
| 294 | } |
| 295 | |
| 296 | while (($width % 4)!=0) { |
| 297 | $width = $width + 1; |
| 298 | } |
| 299 | |
| 300 | while (($height % 2)!=0) { |
| 301 | $height = $height + 1; |
| 302 | } |
| 303 | |
| 304 | $geometry = "${width}x$height"; |
| 305 | } else { |
| 306 | die "$prog: geometry $geometry is invalid\n"; |
| 307 | } |
| 308 | |
| 309 | if (($depth < 8) || ($depth > 32)) { |
| 310 | die "Depth must be between 8 and 32\n"; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | # |
| 316 | # GetDisplayNumber gets the lowest available display number. A display number |
| 317 | # n is taken if something is listening on the VNC server port (5900+n) or the |
| 318 | # X server port (6000+n). |
| 319 | # |
| 320 | |
| 321 | sub GetDisplayNumber |
| 322 | { |
| 323 | foreach $n (1..99) { |
| 324 | if (&CheckDisplayNumber($n)) { |
| 325 | return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02 |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | die "$prog: no free display number on $host.\n"; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | # |
| 334 | # CheckDisplayNumber checks if the given display number is available. A |
| 335 | # display number n is taken if something is listening on the VNC server port |
| 336 | # (5900+n) or the X server port (6000+n). |
| 337 | # |
| 338 | |
| 339 | sub CheckDisplayNumber |
| 340 | { |
| 341 | local ($n) = @_; |
| 342 | |
| 343 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 344 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 345 | if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) { |
| 346 | close(S); |
| 347 | return 0; |
| 348 | } |
| 349 | close(S); |
| 350 | |
| 351 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 352 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 353 | if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) { |
| 354 | close(S); |
| 355 | return 0; |
| 356 | } |
| 357 | close(S); |
| 358 | |
| 359 | if (-e "/tmp/.X$n-lock") { |
| 360 | warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n"; |
| 361 | warn "Remove this file if there is no X server $host:$n\n"; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | if (-e "/tmp/.X11-unix/X$n") { |
| 366 | warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n"; |
| 367 | warn "Remove this file if there is no X server $host:$n\n"; |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | if (-e "/usr/spool/sockets/X11/$n") { |
| 372 | warn("\nWarning: $host:$n is taken because of ". |
| 373 | "/usr/spool/sockets/X11/$n\n"); |
| 374 | warn "Remove this file if there is no X server $host:$n\n"; |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | # |
| 383 | # GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel |
| 384 | # format of the current X display being used. If successful, it sets the |
| 385 | # options as appropriate so that the X VNC server will use the same settings |
| 386 | # (minus an allowance for window manager decorations on the geometry). Using |
| 387 | # the same depth and pixel format means that the VNC server won't have to |
| 388 | # translate pixels when the desktop is being viewed on this X display (for |
| 389 | # TrueColor displays anyway). |
| 390 | # |
| 391 | |
| 392 | sub GetXDisplayDefaults |
| 393 | { |
| 394 | local (@lines, @matchlines, $width, $height, $defaultVisualId, $i, |
| 395 | $red, $green, $blue); |
| 396 | |
| 397 | $wmDecorationWidth = 4; # a guess at typical size for window manager |
| 398 | $wmDecorationHeight = 24; # decoration size |
| 399 | |
| 400 | return if (!defined($ENV{DISPLAY})); |
| 401 | |
| 402 | @lines = `xdpyinfo 2>/dev/null`; |
| 403 | |
| 404 | return if ($? != 0); |
| 405 | |
| 406 | @matchlines = grep(/dimensions/, @lines); |
| 407 | if (@matchlines) { |
| 408 | ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/); |
| 409 | |
| 410 | $width -= $wmDecorationWidth; |
| 411 | $height -= $wmDecorationHeight; |
| 412 | |
| 413 | $geometry = "${width}x$height"; |
| 414 | } |
| 415 | |
| 416 | @matchlines = grep(/default visual id/, @lines); |
| 417 | if (@matchlines) { |
| 418 | ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/); |
| 419 | |
| 420 | for ($i = 0; $i < @lines; $i++) { |
| 421 | if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) { |
| 422 | if (($lines[$i+1] !~ /TrueColor/) || |
| 423 | ($lines[$i+2] !~ /depth/) || |
| 424 | ($lines[$i+4] !~ /red, green, blue masks/)) |
| 425 | { |
| 426 | return; |
| 427 | } |
| 428 | last; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return if ($i >= @lines); |
| 433 | |
| 434 | ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/); |
| 435 | ($red,$green,$blue) |
| 436 | = ($lines[$i+4] |
| 437 | =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/); |
| 438 | |
| 439 | $red = hex($red); |
| 440 | $green = hex($green); |
| 441 | $blue = hex($blue); |
| 442 | |
| 443 | if ($red > $blue) { |
| 444 | $red = int(log($red) / log(2)) - int(log($green) / log(2)); |
| 445 | $green = int(log($green) / log(2)) - int(log($blue) / log(2)); |
| 446 | $blue = int(log($blue) / log(2)) + 1; |
| 447 | $pixelformat = "rgb$red$green$blue"; |
| 448 | } else { |
| 449 | $blue = int(log($blue) / log(2)) - int(log($green) / log(2)); |
| 450 | $green = int(log($green) / log(2)) - int(log($red) / log(2)); |
| 451 | $red = int(log($red) / log(2)) + 1; |
| 452 | $pixelformat = "bgr$blue$green$red"; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | # |
| 459 | # quotedString returns a string which yields the original string when parsed |
| 460 | # by a shell. |
| 461 | # |
| 462 | |
| 463 | sub quotedString |
| 464 | { |
| 465 | local ($in) = @_; |
| 466 | |
| 467 | $in =~ s/\'/\'\"\'\"\'/g; |
| 468 | |
| 469 | return "'$in'"; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | # |
| 474 | # removeSlashes turns slashes into underscores for use as a file name. |
| 475 | # |
| 476 | |
| 477 | sub removeSlashes |
| 478 | { |
| 479 | local ($in) = @_; |
| 480 | |
| 481 | $in =~ s|/|_|g; |
| 482 | |
| 483 | return "$in"; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | # |
| 488 | # Usage |
| 489 | # |
| 490 | |
| 491 | sub Usage |
| 492 | { |
| 493 | die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n". |
| 494 | " [-geometry <width>x<height>]\n". |
| 495 | " [-pixelformat rgbNNN|bgrNNN]\n". |
| 496 | " <Xvnc-options>...\n\n". |
| 497 | " $prog -kill <X-display>\n\n"); |
| 498 | } |
| 499 | |
| 500 | |
| 501 | # |
| 502 | # Kill |
| 503 | # |
| 504 | |
| 505 | sub Kill |
| 506 | { |
| 507 | $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1 |
| 508 | |
| 509 | if ($opt{'-kill'} =~ /^:\d+$/) { |
| 510 | $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid"; |
| 511 | } else { |
| 512 | if ($opt{'-kill'} !~ /^$host:/) { |
| 513 | die "\nCan't tell if $opt{'-kill'} is on $host\n". |
| 514 | "Use -kill :<number> instead\n\n"; |
| 515 | } |
| 516 | $pidFile = "$vncUserDir/$opt{'-kill'}.pid"; |
| 517 | } |
| 518 | |
| 519 | if (! -r $pidFile) { |
| 520 | die "\nCan't find file $pidFile\n". |
| 521 | "You'll have to kill the Xvnc process manually\n\n"; |
| 522 | } |
| 523 | |
| 524 | $SIG{'HUP'} = 'IGNORE'; |
| 525 | chop($pid = `cat $pidFile`); |
| 526 | warn "Killing Xvnc process ID $pid\n"; |
| 527 | system("kill $pid"); |
| 528 | unlink $pidFile; |
| 529 | exit; |
| 530 | } |
| 531 | |
| 532 | |
| 533 | # |
| 534 | # ParseOptions takes a list of possible options and a boolean indicating |
| 535 | # whether the option has a value following, and sets up an associative array |
| 536 | # %opt of the values of the options given on the command line. It removes all |
| 537 | # the arguments it uses from @ARGV and returns them in @optArgs. |
| 538 | # |
| 539 | |
| 540 | sub ParseOptions |
| 541 | { |
| 542 | local (@optval) = @_; |
| 543 | local ($opt, @opts, %valFollows, @newargs); |
| 544 | |
| 545 | while (@optval) { |
| 546 | $opt = shift(@optval); |
| 547 | push(@opts,$opt); |
| 548 | $valFollows{$opt} = shift(@optval); |
| 549 | } |
| 550 | |
| 551 | @optArgs = (); |
| 552 | %opt = (); |
| 553 | |
| 554 | arg: while (defined($arg = shift(@ARGV))) { |
| 555 | foreach $opt (@opts) { |
| 556 | if ($arg eq $opt) { |
| 557 | push(@optArgs, $arg); |
| 558 | if ($valFollows{$opt}) { |
| 559 | if (@ARGV == 0) { |
| 560 | &Usage(); |
| 561 | } |
| 562 | $opt{$opt} = shift(@ARGV); |
| 563 | push(@optArgs, $opt{$opt}); |
| 564 | } else { |
| 565 | $opt{$opt} = 1; |
| 566 | } |
| 567 | next arg; |
| 568 | } |
| 569 | } |
| 570 | push(@newargs,$arg); |
| 571 | } |
| 572 | |
| 573 | @ARGV = @newargs; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | # |
| 578 | # Routine to make sure we're operating in a sane environment. |
| 579 | # |
| 580 | |
| 581 | sub SanityCheck |
| 582 | { |
| 583 | local ($cmd); |
| 584 | |
| 585 | # |
| 586 | # Get the program name |
| 587 | # |
| 588 | |
| 589 | ($prog) = ($0 =~ m|([^/]+)$|); |
| 590 | |
| 591 | # |
| 592 | # Check we have all the commands we'll need on the path. |
| 593 | # |
| 594 | |
| 595 | cmd: |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 596 | foreach $cmd ("uname") { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 597 | for (split(/:/,$ENV{PATH})) { |
| 598 | if (-x "$_/$cmd") { |
| 599 | next cmd; |
| 600 | } |
| 601 | } |
| 602 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 603 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 604 | if (-x "/usr/X11R6/bin/xauth") { |
| 605 | $xauth = "/usr/X11R6/bin/xauth"; |
| 606 | } |
| 607 | else { |
| 608 | cmd1: |
| 609 | foreach $cmd ("xauth") { |
| 610 | for (split(/:/,$ENV{PATH})) { |
| 611 | if (-x "$_/$cmd") { |
| 612 | next cmd1; |
| 613 | } |
| 614 | } |
| 615 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | if($exedir eq "") { |
| 620 | cmd2: |
| 621 | foreach $cmd ("Xvnc","vncpasswd") { |
| 622 | for (split(/:/,$ENV{PATH})) { |
| 623 | if (-x "$_/$cmd") { |
| 624 | $vncClasses = "$_/../vnc/classes"; |
| 625 | next cmd2; |
| 626 | } |
| 627 | } |
| 628 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 629 | } |
| 630 | } |
| 631 | else { |
| 632 | cmd3: |
| 633 | foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") { |
| 634 | for (split(/:/,$ENV{PATH})) { |
| 635 | if (-x "$cmd") { |
| 636 | $vncClasses = $exedir."../vnc/classes"; |
| 637 | next cmd3; |
| 638 | } |
| 639 | } |
| 640 | die "$prog: couldn't find \"$cmd\".\n"; |
| 641 | } |
| 642 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 643 | |
| 644 | # |
| 645 | # Check the HOME environment variable is set |
| 646 | # |
| 647 | |
| 648 | if (!defined($ENV{HOME})) { |
| 649 | die "$prog: The HOME environment variable is not set.\n"; |
| 650 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 651 | # chdir($ENV{HOME}); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 652 | |
| 653 | # |
| 654 | # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an |
| 655 | # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails, |
| 656 | # we just guess at the values. If you find perl moaning here, just |
| 657 | # hard-code the values of AF_INET and SOCK_STREAM. You can find these out |
| 658 | # for your platform by looking in /usr/include/sys/socket.h and related |
| 659 | # files. |
| 660 | # |
| 661 | |
| 662 | chop($os = `uname`); |
| 663 | chop($osrev = `uname -r`); |
| 664 | |
| 665 | eval 'use Socket'; |
| 666 | if ($@) { |
| 667 | eval 'require "sys/socket.ph"'; |
| 668 | if ($@) { |
| 669 | if (($os eq "SunOS") && ($osrev !~ /^4/)) { |
| 670 | $AF_INET = 2; |
| 671 | $SOCK_STREAM = 2; |
| 672 | } else { |
| 673 | $AF_INET = 2; |
| 674 | $SOCK_STREAM = 1; |
| 675 | } |
| 676 | } else { |
| 677 | $AF_INET = &AF_INET; |
| 678 | $SOCK_STREAM = &SOCK_STREAM; |
| 679 | } |
| 680 | } else { |
| 681 | $AF_INET = &AF_INET; |
| 682 | $SOCK_STREAM = &SOCK_STREAM; |
| 683 | } |
| 684 | } |