1 | /** $Id$ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions Service Skeleton.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <sys/types.h>
|
---|
22 | #include <sys/stat.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <unistd.h>
|
---|
26 | #include <signal.h>
|
---|
27 | #include <fcntl.h>
|
---|
28 | #ifdef USE_LIBDAEMON
|
---|
29 | #include <libdaemon/dfork.h>
|
---|
30 | #include <libdaemon/dsignal.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <VBox/VBoxGuest.h>
|
---|
34 | #include "VBoxServiceInternal.h"
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Solaris daemon() call (currently does not use libdaemon).
|
---|
38 | *
|
---|
39 | * @returns 0 on success
|
---|
40 | */
|
---|
41 | int daemon(int nochdir, int noclose)
|
---|
42 | {
|
---|
43 | #ifdef USE_LIBDAEMON
|
---|
44 | pid_t pid = daemon_fork();
|
---|
45 | if (pid < 0)
|
---|
46 | {
|
---|
47 | daemon_retval_done();
|
---|
48 | return -1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (pid)
|
---|
52 | exit(0);
|
---|
53 |
|
---|
54 | daemon_close_all(-1);
|
---|
55 | return 0;
|
---|
56 | #else
|
---|
57 | if (getppid() == 1) /* We already belong to init process */
|
---|
58 | return -1;
|
---|
59 |
|
---|
60 | pid_t pid = fork();
|
---|
61 | if (pid < 0) /* The fork() failed. Bad. */
|
---|
62 | return -1;
|
---|
63 |
|
---|
64 | if (pid > 0) /* Quit parent process */
|
---|
65 | exit(0);
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * The orphaned child becomes a daemon after attaching to init. We need to get
|
---|
69 | * rid of signals, file descriptors & other stuff we inherited from the parent.
|
---|
70 | */
|
---|
71 | pid_t newpgid = setsid();
|
---|
72 | if (newpgid < 0) /* Failed to create new sesion */
|
---|
73 | return -1;
|
---|
74 |
|
---|
75 | /* BSD daemon style. */
|
---|
76 | if (!noclose)
|
---|
77 | {
|
---|
78 | /* Open stdin(0), stdout(1) and stderr(2) to /dev/null */
|
---|
79 | int fd = open("/dev/null", O_RDWR);
|
---|
80 | dup2(fd, STDIN_FILENO);
|
---|
81 | dup2(fd, STDOUT_FILENO);
|
---|
82 | dup2(fd, STDERR_FILENO);
|
---|
83 | if (fd > 2)
|
---|
84 | close(fd);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /* Switch our current directory to root */
|
---|
88 | if (!nochdir)
|
---|
89 | chdir("/"); /* @todo Check if switching to '/' is the convention for Solaris daemons. */
|
---|
90 |
|
---|
91 | /* Set file permission to something secure, as we need to run as root on Solaris */
|
---|
92 | umask(027);
|
---|
93 | return 0;
|
---|
94 | #endif
|
---|
95 | }
|
---|
96 |
|
---|