- Timestamp:
- Mar 30, 2009 11:47:27 AM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 45391
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibDaemonize.cpp
r18359 r18526 5 5 6 6 /* 7 * Copyright (C) 2007 Sun Microsystems, Inc.7 * Copyright (C) 2007-2009 Sun Microsystems, Inc. 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 214 214 } 215 215 216 216 217 /** 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) 218 * Creates a PID File and returns the open file descriptor. 219 * 220 * On DOS based system, file sharing (deny write) is used for locking the PID 221 * file. 222 * 223 * On Unix-y systems, an exclusive advisory lock is used for locking the PID 224 * file since the file sharing support is usually missing there. 225 * 226 * This API will overwrite any existing PID Files without a lock on them, on the 227 * assumption that they are stale files which an old process did not properly 228 * clean up. 229 * 230 * @returns IPRT status code. 231 * @param pszPath The path and filename to create the PID File under 232 * @param phFile Where to store the file descriptor of the open (and locked 233 * on Unix-y systems) PID File. On failure, or if another 234 * process owns the PID File, this will be set to NIL_RTFILE. 235 */ 236 VBGLR3DECL(int) VbglR3PidFile(const char *pszPath, PRTFILE phFile) 231 237 { 232 238 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; 239 AssertPtrReturn(phFile, VERR_INVALID_PARAMETER); 240 *phFile = NIL_RTFILE; 241 242 RTFILE hPidFile; 236 243 int rc = RTFileOpen(&hPidFile, pszPath, 237 RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE244 RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE 238 245 | (0644 << RTFILE_O_CREATE_MODE_SHIFT)); 239 246 if (RT_SUCCESS(rc)) 247 { 248 #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2) 240 249 /** @todo using size 0 for locking means lock all on Posix. 241 250 * We should adopt this as our convention too, or something 242 251 * similar. */ 243 252 rc = RTFileLock(hPidFile, RTFILE_LOCK_WRITE, 0, 0); 244 if (RT_SUCCESS(rc)) 245 { 246 char szBuf[256]; 247 size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", 248 RTProcSelf()); 249 RTFileWrite(hPidFile, szBuf, cbPid, NULL); 250 } 251 *pFile = hPidFile; 253 if (RT_FAILURE(rc)) 254 RTFileClose(hPidFile); 255 else 256 #endif 257 { 258 char szBuf[256]; 259 size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n", 260 RTProcSelf()); 261 RTFileWrite(hPidFile, szBuf, cbPid, NULL); 262 *phFile = hPidFile; 263 } 264 } 252 265 return rc; 253 #else /* !RT_OS_LINUX and !RT_OS_SOLARIS */254 # error portme255 #endif /* !RT_OS_LINUX and !RT_OS_SOLARIS */256 266 } 257 267 268 258 269 /** 259 * Close and remove an open pidfile. 260 * @param pszPath the path to the pidfile 261 * @param File the open file descriptor 262 */ 263 VBGLR3DECL(void) VbglR3ClosePidFile(const char *pszPath, RTFILE File) 270 * Close and remove an open PID File. 271 * 272 * @param pszPath The path to the PID File, 273 * @param hFile The handle for the file. NIL_RTFILE is ignored as usual. 274 */ 275 VBGLR3DECL(void) VbglR3ClosePidFile(const char *pszPath, RTFILE hFile) 264 276 { 265 277 AssertPtrReturnVoid(pszPath); 266 AssertReturnVoid(File != NIL_RTFILE); 267 #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS) 268 RTFileDelete(pszPath); 269 RTFileClose(File); 270 #else /* !RT_OS_LINUX and !RT_OS_SOLARIS */ 271 # error portme 272 #endif /* !RT_OS_LINUX and !RT_OS_SOLARIS */ 278 if (hFile != NIL_RTFILE) 279 { 280 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) 281 RTFileWriteAt(hFile, 0, "-1", 2, NULL); 282 #else 283 RTFileDelete(pszPath); 284 #endif 285 RTFileClose(hFile); 286 } 273 287 } 288
Note:
See TracChangeset
for help on using the changeset viewer.