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