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