VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibPidFile.cpp@ 58029

Last change on this file since 58029 was 57730, checked in by vboxsync, 9 years ago

Windows build fix

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id Revision
File size: 3.9 KB
Line 
1/** $Id: VBoxGuestR3LibPidFile.cpp 57730 2015-09-14 08:27:42Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions,
4 * Create a PID file.
5 */
6
7/*
8 * Copyright (C) 2015 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * The contents of this file may alternatively be used under the terms
19 * of the Common Development and Distribution License Version 1.0
20 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
21 * VirtualBox OSE distribution, in which case the provisions of the
22 * CDDL are applicable instead of those of the GPL.
23 *
24 * You may elect to license modified versions of this file under the
25 * terms and conditions of either the GPL or the CDDL or both.
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <iprt/file.h>
33#include <iprt/string.h>
34#include <iprt/process.h>
35#include "VBGLR3Internal.h"
36
37/**
38 * Creates a PID File and returns the open file descriptor.
39 *
40 * On DOS based system, file sharing (deny write) is used for locking the PID
41 * file.
42 *
43 * On Unix-y systems, an exclusive advisory lock is used for locking the PID
44 * file since the file sharing support is usually missing there.
45 *
46 * This API will overwrite any existing PID Files without a lock on them, on the
47 * assumption that they are stale files which an old process did not properly
48 * clean up.
49 *
50 * @returns IPRT status code.
51 * @param pszPath The path and filename to create the PID File under
52 * @param phFile Where to store the file descriptor of the open (and locked
53 * on Unix-y systems) PID File. On failure, or if another
54 * process owns the PID File, this will be set to NIL_RTFILE.
55 */
56VBGLR3DECL(int) VbglR3PidFile(const char *pszPath, PRTFILE phFile)
57{
58 AssertPtrReturn(pszPath, VERR_INVALID_PARAMETER);
59 AssertPtrReturn(phFile, VERR_INVALID_PARAMETER);
60 *phFile = NIL_RTFILE;
61
62 RTFILE hPidFile;
63 int rc = RTFileOpen(&hPidFile, pszPath,
64 RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_WRITE
65 | (0644 << RTFILE_O_CREATE_MODE_SHIFT));
66 if (RT_SUCCESS(rc))
67 {
68#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
69 /** @todo using size 0 for locking means lock all on Posix.
70 * We should adopt this as our convention too, or something
71 * similar. */
72 rc = RTFileLock(hPidFile, RTFILE_LOCK_WRITE, 0, 0);
73 if (RT_FAILURE(rc))
74 RTFileClose(hPidFile);
75 else
76#endif
77 {
78 char szBuf[256];
79 size_t cbPid = RTStrPrintf(szBuf, sizeof(szBuf), "%d\n",
80 RTProcSelf());
81 RTFileWrite(hPidFile, szBuf, cbPid, NULL);
82 *phFile = hPidFile;
83 }
84 }
85 return rc;
86}
87
88
89/**
90 * Close and remove an open PID File.
91 *
92 * @param pszPath The path to the PID File,
93 * @param hFile The handle for the file. NIL_RTFILE is ignored as usual.
94 */
95VBGLR3DECL(void) VbglR3ClosePidFile(const char *pszPath, RTFILE hFile)
96{
97 AssertPtrReturnVoid(pszPath);
98 if (hFile != NIL_RTFILE)
99 {
100#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
101 RTFileWriteAt(hFile, 0, "-1", 2, NULL);
102#else
103 RTFileDelete(pszPath);
104#endif
105 RTFileClose(hFile);
106 }
107}
108
Note: See TracBrowser for help on using the repository browser.

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