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