VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibPidFile.cpp@ 97436

Last change on this file since 97436 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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