Changeset 18320 in vbox for trunk/src/VBox
- Timestamp:
- Mar 26, 2009 2:53:12 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 45092
- Location:
- trunk/src/VBox/Additions
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibDaemonize.cpp
r11179 r18320 51 51 #include <iprt/string.h> 52 52 #include <iprt/file.h> 53 #include <iprt/process.h> 53 54 #include <VBox/VBoxGuest.h> 54 55 … … 63 64 * @param fNoChDir Pass false to change working directory to root. 64 65 * @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. 68 */ 69 VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose, 70 char const *pszPidfile) 66 */ 67 VBGLR3DECL(int) VbglR3Daemonize(bool fNoChDir, bool fNoClose) 71 68 { 72 69 #if defined(RT_OS_DARWIN) … … 74 71 75 72 #elif defined(RT_OS_OS2) 76 /** @todo create a pidfile if this is (/was :) ) usual on OS/2 */77 NOREF(pszPidfile);78 73 PPIB pPib; 79 74 PTIB pTib; … … 150 145 */ 151 146 152 /* We start off by opening the pidfile, so that we can fail straight away153 * if it already exists. */154 RTFILE hPidfile = NIL_RTFILE;155 if (pszPidfile != NULL)156 {157 /* @note the exclusive create is not guaranteed on all file158 * systems (e.g. NFSv2) */159 int rc = RTFileOpen(&hPidfile, pszPidfile,160 RTFILE_O_READWRITE | RTFILE_O_CREATE161 | (0644 << RTFILE_O_CREATE_MODE_SHIFT));162 if (!RT_SUCCESS(rc))163 return rc;164 }165 166 147 struct sigaction OldSigAct; 167 148 struct sigaction SigAct; … … 174 155 return RTErrConvertFromErrno(errno); 175 156 if (pid != 0) 176 {177 # ifndef RT_OS_LINUX /* On Linux we do another fork later */178 if (hPidfile != NIL_RTFILE)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 # endif186 157 exit(0); 187 }188 158 189 159 /* … … 237 207 return RTErrConvertFromErrno(errno); 238 208 if (pid != 0) 239 {240 if (hPidfile != NIL_RTFILE)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 }247 209 exit(0); 248 }249 210 # endif /* RT_OS_LINUX */ 250 211 … … 253 214 } 254 215 216 /** 217 * Creates a pidfile and returns the open file descriptor. 218 * On Unix-y systems, this call also places an advisory lock on the open 219 * file. It will overwrite any existing pidfiles without a lock on them, 220 * on the assumption that they are stale files which an old process did not 221 * properly clean up. 222 * 223 * @returns iprt status code 224 * @param pszPath the path and filename to create the pidfile under 225 * @param pFile where to store the file descriptor of the open 226 * (and locked on Unix-y systems) pidfile. 227 * On failure, or if another process owns the pidfile, 228 * this will be set to NIL_RTFILE. 229 */ 230 VBGLR3DECL(int) VbglR3PidFile(const char *pszPath, PRTFILE pFile) 231 { 232 AssertPtrReturn(pszPath, VERR_INVALID_PARAMETER); 233 AssertPtrReturn(pFile, VERR_INVALID_PARAMETER); 234 #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS) 235 RTFILE hPidFile = NIL_RTFILE; 236 int rc = RTFileOpen(&hPidFile, pszPath, 237 RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE 238 | (0644 << RTFILE_O_CREATE_MODE_SHIFT)); 239 if (RT_SUCCESS(rc)) 240 rc = RTFileLock(hPidFile, RTFILE_LOCK_WRITE, 0, ~0); 241 if (RT_SUCCESS(rc)) 242 { 243 char szBuf[256]; 244 size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", 245 RTProcSelf()); 246 RTFileWrite(hPidFile, szBuf, cbPid, NULL); 247 } 248 *pFile = hPidFile; 249 return rc; 250 #else /* !RT_OS_LINUX and !RT_OS_SOLARIS */ 251 # error portme 252 #endif /* !RT_OS_LINUX and !RT_OS_SOLARIS */ 253 } 254 255 /** 256 * Close and remove an open pidfile. 257 * @param pszPath the path to the pidfile 258 * @param File the open file descriptor 259 */ 260 VBGLR3DECL(void) VbglR3ClosePidFile(const char *pszPath, RTFILE File) 261 { 262 AssertPtrReturnVoid(pszPath); 263 AssertReturnVoid(File != NIL_RTFILE); 264 #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS) 265 RTFileDelete(pszPath); 266 RTFileClose(File); 267 #else /* !RT_OS_LINUX and !RT_OS_SOLARIS */ 268 # error portme 269 #endif /* !RT_OS_LINUX and !RT_OS_SOLARIS */ 270 } -
trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp
r11822 r18320 374 374 { 375 375 VBoxServiceVerbose(1, "Daemonizing...\n"); 376 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */ , NULL);376 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */); 377 377 if (RT_FAILURE(rc)) 378 378 return VBoxServiceError("daemon failed: %Rrc\n", rc); -
trunk/src/VBox/Additions/x11/VBoxClient/main.cpp
r11822 r18320 218 218 if (fDaemonise) 219 219 { 220 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */, 221 NULL); 220 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */); 222 221 if (RT_FAILURE(rc)) 223 222 {
Note:
See TracChangeset
for help on using the changeset viewer.