VirtualBox

Changeset 18526 in vbox for trunk/src


Ignore:
Timestamp:
Mar 30, 2009 11:47:27 AM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
45391
Message:

VbglR3PidFile,VbglR3PidFileClose: Made it build everywhere.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibDaemonize.cpp

    r18359 r18526  
    55
    66/*
    7  * Copyright (C) 2007 Sun Microsystems, Inc.
     7 * Copyright (C) 2007-2009 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    214214}
    215215
     216
    216217/**
    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 */
     236VBGLR3DECL(int) VbglR3PidFile(const char *pszPath, PRTFILE phFile)
    231237{
    232238    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;
    236243    int rc = RTFileOpen(&hPidFile, pszPath,
    237                           RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE
     244                        RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE
    238245                        | (0644 << RTFILE_O_CREATE_MODE_SHIFT));
    239246    if (RT_SUCCESS(rc))
     247    {
     248#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
    240249        /** @todo using size 0 for locking means lock all on Posix.
    241250         * We should adopt this as our convention too, or something
    242251         * similar. */
    243252        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    }
    252265    return rc;
    253 #else  /* !RT_OS_LINUX and !RT_OS_SOLARIS */
    254 # error portme
    255 #endif  /* !RT_OS_LINUX and !RT_OS_SOLARIS */
    256266}
    257267
     268
    258269/**
    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 */
     275VBGLR3DECL(void) VbglR3ClosePidFile(const char *pszPath, RTFILE hFile)
    264276{
    265277    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    }
    273287}
     288
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette