Changeset 10635 in vbox for trunk/src/VBox
- Timestamp:
- Jul 15, 2008 9:36:16 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibDaemonize.cpp
r10316 r10635 50 50 51 51 #include <iprt/string.h> 52 #include <iprt/file.h> 52 53 #include <VBox/VBoxGuest.h> 53 54 … … 62 63 * @param fNoChDir Pass false to change working directory to root. 63 64 * @param fNoClose Pass false to redirect standard file streams to /dev/null. 65 * @param pszPidfile Path to a file to write the pid of the daemon process 66 * to. Daemonising will fail if this file already exists 67 * or cannot be written. Optional. 64 68 */ 65 VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose) 69 VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose, 70 char const *pszPidfile) 66 71 { 67 72 #if defined(RT_OS_DARWIN) … … 69 74 70 75 #elif defined(RT_OS_OS2) 76 /** @todo create a pidfile if this is (/was :) ) usual on OS/2 */ 77 noref(pszPidfile); 71 78 PPIB pPib; 72 79 PTIB pTib; … … 142 149 * fork() once more on Linux to get rid of the session leadership role. 143 150 */ 151 152 /* We start off by opening the pidfile, so that we can fail straight away 153 * if it already exists. */ 154 RTFILE hPidfile = NULL; 155 if (pszPidfile != NULL) 156 { 157 /* @note the exclusive create is not guaranteed on all file 158 * systems (e.g. NFSv2) */ 159 int rc = RTFileOpen(&hPidfile, pszPidfile, 160 RTFILE_O_READWRITE | RTFILE_O_CREATE 161 | (0644 << RTFILE_O_CREATE_MODE_SHIFT)); 162 if (!RT_SUCCESS(rc)) 163 return rc; 164 } 165 144 166 struct sigaction OldSigAct; 145 167 struct sigaction SigAct; … … 152 174 return RTErrConvertFromErrno(errno); 153 175 if (pid != 0) 176 { 177 # ifndef RT_OS_LINUX /* On Linux we do another fork later */ 178 if (hPidfile != NULL) 179 { 180 char szBuf[256]; 181 size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", pid); 182 RTFileWrite(hPidfile, szBuf, cbPid, NULL); 183 RTFileClose(hPidfile); 184 } 185 # endif 154 186 exit(0); 187 } 155 188 156 189 /* … … 204 237 return RTErrConvertFromErrno(errno); 205 238 if (pid != 0) 239 { 240 if (hPidfile != NULL) 241 { 242 char szBuf[256]; 243 size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", pid); 244 RTFileWrite(hPidfile, szBuf, cbPid, NULL); 245 RTFileClose(hPidfile); 246 } 206 247 exit(0); 248 } 207 249 # endif /* RT_OS_LINUX */ 208 250
Note:
See TracChangeset
for help on using the changeset viewer.