Changeset 6356 in vbox for trunk/src/VBox/Additions
- Timestamp:
- Jan 15, 2008 1:27:51 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 27270
- Location:
- trunk/src/VBox/Additions
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3Lib.cpp
r6280 r6356 24 24 # define INCL_ERRORS 25 25 # include <os2.h> 26 27 # include <iprt/alloca.h> 28 # include <iprt/string.h> 29 #elif defined(RT_OS_LINUX) 30 # include <sys/stat.h> 31 # include <fcntl.h> 32 # include <stdlib.h> 33 # include <unistd.h> 34 # include <sys/time.h> 35 # include <sys/resource.h> 36 #elif defined(RT_OS_SOLARIS) 37 # include <sys/types.h> 38 # include <sys/stat.h> 39 # include <stdio.h> 40 # include <stdlib.h> 41 # include <unistd.h> 42 # include <signal.h> 43 # include <fcntl.h> 26 44 #endif 27 45 … … 290 308 } 291 309 310 292 311 /** 293 312 * Change the IRQ filter mask. … … 304 323 } 305 324 325 326 /** 327 * Daemonize the process for running in the background. 328 * 329 * @returns 0 on success 330 * 331 * @param nochdir Pass 0 to change working directory to root. 332 * @param noclose Pass 0 to redirect standard file streams to /dev/null. 333 */ 334 VBGLR3DECL(int) VbglR3Daemonize(int nochdir, int noclose) 335 { 336 #if defined(RT_OS_LINUX) 337 /** rlimit structure for finding out how many open files we may have. */ 338 struct rlimit rlim; 339 340 /* To make sure that we are not currently a session leader, we must first fork and let 341 the parent process exit, as a newly created child is never session leader. This will 342 allow us to call setsid() later. */ 343 if (fork() != 0) 344 { 345 exit(0); 346 } 347 /* Find the maximum number of files we can have open and close them all. */ 348 if (0 != getrlimit(RLIMIT_NOFILE, &rlim)) 349 { 350 /* For some reason the call failed. In that case we will just close the three 351 standard files and hope. */ 352 rlim.rlim_cur = 3; 353 } 354 for (unsigned int i = 0; i < rlim.rlim_cur; ++i) 355 { 356 close(i); 357 } 358 /* Change to the root directory to avoid keeping the one we were started in open. */ 359 chdir("/"); 360 /* Set our umask to zero. */ 361 umask(0); 362 /* And open /dev/null on stdin/out/err. */ 363 open("/dev/null", O_RDONLY); 364 open("/dev/null", O_WRONLY); 365 dup(1); 366 /* Detach from the controlling terminal by creating our own session, to avoid receiving 367 signals from the old session. */ 368 setsid(); 369 /* And fork again, letting the parent exit, to make us a child of init and avoid zombies. */ 370 if (fork() != 0) 371 { 372 exit(0); 373 } 374 NOREF(nochdir); 375 NOREF(noclose); 376 377 return 0; 378 379 #elif defined(RT_OS_OS2) 380 PPIB pPib; 381 PTIB pTib; 382 DosGetInfoBlocks(&pTib, &pPib); 383 384 /* Get the full path to the executable. */ 385 char szExe[CCHMAXPATH]; 386 APIRET rc = DosQueryModuleName(pPib->pib_hmte, sizeof(szExe), szExe); 387 if (rc) 388 { 389 errno = EDOOFUS; 390 return -1; 391 } 392 393 /* calc the length of the command line. */ 394 char *pch = pPib->pib_pchcmd; 395 size_t cch0 = strlen(pch); 396 pch += cch0 + 1; 397 size_t cch1 = strlen(pch); 398 pch += cch1 + 1; 399 char *pchArgs; 400 if (cch1 && *pch) 401 { 402 do pch = strchr(pch, '\0') + 1; 403 while (*pch); 404 405 size_t cchTotal = pch - pPib->pib_pchcmd; 406 pchArgs = (char *)alloca(cchTotal + sizeof("--daemonized\0\0")); 407 memcpy(pchArgs, pPib->pib_pchcmd, cchTotal - 1); 408 memcpy(pchArgs + cchTotal - 1, "--daemonized\0\0", sizeof("--daemonized\0\0")); 409 } 410 else 411 { 412 size_t cchTotal = pch - pPib->pib_pchcmd + 1; 413 pchArgs = (char *)alloca(cchTotal + sizeof(" --daemonized ")); 414 memcpy(pchArgs, pPib->pib_pchcmd, cch0 + 1); 415 pch = pchArgs + cch0 + 1; 416 memcpy(pch, " --daemonized ", sizeof(" --daemonized ") - 1); 417 pch += sizeof(" --daemonized ") - 1; 418 if (cch1) 419 memcpy(pch, pPib->pib_pchcmd + cch0 + 1, cch1 + 2); 420 else 421 pch[0] = pch[1] = '\0'; 422 } 423 424 /* spawn a detach process */ 425 char szObj[128]; 426 RESULTCODES ResCodes = { 0, 0 }; 427 szObj[0] = '\0'; 428 rc = DosExecPgm(szObj, sizeof(szObj), EXEC_BACKGROUND, (PCSZ)pchArgs, NULL, &ResCodes, (PCSZ)szExe); 429 if (rc) 430 { 431 /** @todo Change this to some standard log/print error?? */ 432 /* VBoxServiceError("DosExecPgm failed with rc=%d and szObj='%s'\n", rc, szObj); */ 433 errno = EDOOFUS; 434 return -1; 435 } 436 DosExit(EXIT_PROCESS, 0); 437 return -1; 438 439 #elif defined(RT_OS_SOLARIS) 440 if (getppid() == 1) /* We already belong to init process */ 441 return -1; 442 443 pid_t pid = fork(); 444 if (pid < 0) /* The fork() failed. Bad. */ 445 return -1; 446 447 if (pid > 0) /* Quit parent process */ 448 exit(0); 449 450 /* 451 * The orphaned child becomes a daemon after attaching to init. We need to get 452 * rid of signals, file descriptors & other stuff we inherited from the parent. 453 */ 454 pid_t newpgid = setsid(); 455 if (newpgid < 0) /* Failed to create new sesion */ 456 return -1; 457 458 /* BSD daemon style. */ 459 if (!noclose) 460 { 461 /* Open stdin(0), stdout(1) and stderr(2) to /dev/null */ 462 int fd = open("/dev/null", O_RDWR); 463 dup2(fd, STDIN_FILENO); 464 dup2(fd, STDOUT_FILENO); 465 dup2(fd, STDERR_FILENO); 466 if (fd > 2) 467 close(fd); 468 } 469 470 /* Switch our current directory to root */ 471 if (!nochdir) 472 chdir("/"); /* @todo Check if switching to '/' is the convention for Solaris daemons. */ 473 474 /* Set file permission to something secure, as we need to run as root on Solaris */ 475 umask(027); 476 return 0; 477 #endif 478 } 479 -
trunk/src/VBox/Additions/common/VBoxService/Makefile.kmk
r6139 r6356 31 31 VBoxServiceTimeSync.cpp 32 32 VBoxService_SOURCES.os2 = \ 33 VBoxService-os2.cpp \34 33 VBoxService-os2.def \ 35 34 VBoxServiceClipboard-os2.cpp 36 VBoxService_SOURCES.solaris = \37 VBoxService-solaris.cpp38 35 VBoxService_LIBS = \ 39 36 $(VBOX_LIB_VBGL_R3) \ 40 37 $(VBOX_LIB_IPRT_GUEST_R3) 41 #VBoxService_LIBS.solaris = daemon42 38 43 39 include $(PATH_KBUILD)/subfooter.kmk -
trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp
r6285 r6356 371 371 VBoxServiceVerbose(1, "Daemonizing...\n"); 372 372 errno = 0; 373 if ( daemon(0, 0) != 0)373 if (VbglR3Daemonize(0, 0) != 0) 374 374 return VBoxServiceError("daemon failed: %s\n", strerror(errno)); 375 375 /* in-child */ -
trunk/src/VBox/Additions/x11/Makefile.kmk
r6301 r6356 21 21 # Include sub-dirs. 22 22 SUBDIRS = 23 if1of ($(BUILD_TARGET),linux )23 if1of ($(BUILD_TARGET),linux solaris) 24 24 SUBDIRS += \ 25 25 xclient \ … … 28 28 endif 29 29 30 ifeq ($(BUILD_TARGET),solaris)31 SUBDIRS += \32 xmouse \33 xgraphics34 endif35 36 30 include $(PATH_KBUILD)/subfooter.kmk 37 31 -
trunk/src/VBox/Additions/x11/xclient/Makefile.kmk
r6290 r6356 19 19 include $(PATH_KBUILD)/header.kmk 20 20 21 if1of ($(BUILD_TARGET),linux l4) 21 22 ifneq ($(USERNAME),bird) # crap 22 23 PROGRAMS = vboxadd-xclient … … 27 28 vboxadd-xclient_TEMPLATE = VBOXLNX32GUESTR3EXE 28 29 vboxadd-xclient_SOURCES = clipboard.cpp main.cpp 29 vboxadd-xclient_DEFS += CLIPBOARD_LINUX30 vboxadd-xclient_DEFS += VBOX_X11_CLIPBOARD 30 31 31 32 ifdef LINUX_SEAMLESS_GUEST … … 78 79 endif 79 80 endif 81 endif # target linux l4 82 83 ifeq ($(BUILD_TARGET),solaris) 84 PROGRAMS = VBoxClient 85 86 VBoxClient_TEMPLATE = VBOXGUESTR3EXE 87 VBoxClient_DEFS += VBOX_X11_CLIPBOARD VBOX_HGCM 88 VBoxClient_SOURCES = \ 89 clipboard-new.cpp \ 90 main.cpp 91 VBoxClient_LIBS = \ 92 $(VBOX_LIB_VBGL_R3) \ 93 $(VBOX_LIB_IPRT_GUEST_R3) \ 94 X11 \ 95 Xt 96 endif # target solaris 80 97 81 98 include $(PATH_KBUILD)/footer.kmk -
trunk/src/VBox/Additions/x11/xclient/main.cpp
r6290 r6356 26 26 27 27 #include <sys/types.h> 28 #include <sys/stat.h> /* For umask */29 #include <fcntl.h> /* For open */30 28 #include <stdlib.h> /* For exit */ 31 29 #include <unistd.h> 32 30 #include <getopt.h> 33 34 #include <sys/time.h> /* For getrlimit */35 #include <sys/resource.h> /* For getrlimit */36 31 37 32 #include <X11/Xlib.h> … … 45 40 46 41 static bool gbDaemonise = true; 47 48 /**49 * Go through the long Un*x ritual required to become a daemon process.50 */51 void vboxDaemonise(void)52 {53 /** rlimit structure for finding out how many open files we may have. */54 struct rlimit rlim;55 56 /* To make sure that we are not currently a session leader, we must first fork and let57 the parent process exit, as a newly created child is never session leader. This will58 allow us to call setsid() later. */59 if (fork() != 0)60 {61 exit(0);62 }63 /* Find the maximum number of files we can have open and close them all. */64 if (0 != getrlimit(RLIMIT_NOFILE, &rlim))65 {66 /* For some reason the call failed. In that case we will just close the three67 standard files and hope. */68 rlim.rlim_cur = 3;69 }70 for (unsigned int i = 0; i < rlim.rlim_cur; ++i)71 {72 close(i);73 }74 /* Change to the root directory to avoid keeping the one we were started in open. */75 chdir("/");76 /* Set our umask to zero. */77 umask(0);78 /* And open /dev/null on stdin/out/err. */79 open("/dev/null", O_RDONLY);80 open("/dev/null", O_WRONLY);81 dup(1);82 /* Detach from the controlling terminal by creating our own session, to avoid receiving83 signals from the old session. */84 setsid();85 /* And fork again, letting the parent exit, to make us a child of init and avoid zombies. */86 if (fork() != 0)87 {88 exit(0);89 }90 }91 42 92 43 /** … … 110 61 return 0; 111 62 } 112 #ifdef CLIPBOARD_LINUX63 #ifdef VBOX_X11_CLIPBOARD 113 64 vboxClipboardDisconnect(); 114 65 #endif … … 157 108 } 158 109 } 110 gbDaemonise = false; // ram 159 111 if (gbDaemonise) 160 112 { 161 vboxDaemonise(); 113 if (VbglR3Daemonize(0, 0) != 0) 114 { 115 LogRel(("VBoxService: failed to daemonize. exiting.")); 116 return 1; 117 } 162 118 } 163 119 /* Initialise our runtime before all else. */ … … 178 134 /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */ 179 135 XSetErrorHandler(vboxClientXLibErrorHandler); 180 #ifdef CLIPBOARD_LINUX136 #ifdef VBOX_X11_CLIPBOARD 181 137 /* Connect to the host clipboard. */ 182 138 LogRel(("VBoxService: starting clipboard Guest Additions...\n")); … … 186 142 LogRel(("VBoxService: vboxClipboardConnect failed with rc = %Rrc\n", rc)); 187 143 } 188 #endif /* CLIPBOARD_LINUXdefined */144 #endif /* VBOX_X11_CLIPBOARD defined */ 189 145 #ifdef SEAMLESS_LINUX 190 146 try … … 208 164 } 209 165 #endif /* SEAMLESS_LINUX defined */ 210 #ifdef CLIPBOARD_LINUX166 #ifdef VBOX_X11_CLIPBOARD 211 167 LogRel(("VBoxService: connecting to the shared clipboard service.\n")); 212 168 vboxClipboardMain(); 213 169 vboxClipboardDisconnect(); 214 #else /* CLIPBOARD_LINUXnot defined */170 #else /* VBOX_X11_CLIPBOARD not defined */ 215 171 LogRel(("VBoxService: sleeping...\n")); 216 172 pause(); 217 173 LogRel(("VBoxService: exiting...\n")); 218 #endif /* CLIPBOARD_LINUXnot defined */174 #endif /* VBOX_X11_CLIPBOARD not defined */ 219 175 #ifdef SEAMLESS_LINUX 220 176 try
Note:
See TracChangeset
for help on using the changeset viewer.