VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/generic/AutostartDb-generic.cpp@ 42069

Last change on this file since 42069 was 42069, checked in by vboxsync, 13 years ago

Autostart: Simplify getting the username, use the new IPRT API

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/* $Id: AutostartDb-generic.cpp 42069 2012-07-09 18:10:08Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Autostart implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <VBox/err.h>
19#include <VBox/log.h>
20#include <iprt/assert.h>
21#include <iprt/process.h>
22#include <iprt/path.h>
23#include <iprt/mem.h>
24#include <iprt/file.h>
25#include <iprt/string.h>
26
27#include "AutostartDb.h"
28
29/** @todo: Make configurable through kmk/installer. */
30#define AUTOSTART_DATABASE "/etc/vbox/autostart.d"
31
32#if defined(RT_OS_LINUX)
33/**
34 * Modifies the autostart database.
35 *
36 * @returns VBox status code.
37 * @param fAutostart Flag whether the autostart or autostop database is modified.
38 * @param fAddVM Flag whether a VM is added or removed from the database.
39 */
40static int autostartModifyDb(bool fAutostart, bool fAddVM)
41{
42 int rc = VINF_SUCCESS;
43 char *pszUser = NULL;
44
45 rc = RTProcQueryUsernameA(RTProcSelf(), &pszUser);
46 if (RT_SUCCESS(rc))
47 {
48 char *pszFile;
49 uint64_t fOpen = RTFILE_O_DENY_ALL | RTFILE_O_READWRITE;
50 RTFILE hAutostartFile;
51
52 AssertPtr(pszUser);
53
54 if (fAddVM)
55 fOpen |= RTFILE_O_OPEN_CREATE;
56
57 rc = RTStrAPrintf(&pszFile, "%s/%s.%s",
58 AUTOSTART_DATABASE, pszUser, fAutostart ? "start" : "stop");
59 if (RT_SUCCESS(rc))
60 {
61 rc = RTFileOpen(&hAutostartFile, pszFile, fOpen);
62 if (RT_SUCCESS(rc))
63 {
64 uint64_t cbFile;
65
66 /*
67 * Files with more than 16 bytes are rejected because they just contain
68 * a number of the amount of VMs with autostart configured, so they
69 * should be really really small. Anything else is bogus.
70 */
71 rc = RTFileGetSize(hAutostartFile, &cbFile);
72 if ( RT_SUCCESS(rc)
73 && cbFile <= 16)
74 {
75 char abBuf[16 + 1]; /* trailing \0 */
76 uint32_t cAutostartVms = 0;
77
78 memset(abBuf, 0, sizeof(abBuf));
79
80 /* Check if the file was just created. */
81 if (cbFile)
82 {
83 rc = RTFileRead(hAutostartFile, abBuf, cbFile, NULL);
84 if (RT_SUCCESS(rc))
85 {
86 rc = RTStrToUInt32Ex(abBuf, NULL, 10 /* uBase */, &cAutostartVms);
87 if ( rc == VWRN_TRAILING_CHARS
88 || rc == VWRN_TRAILING_SPACES)
89 rc = VINF_SUCCESS;
90 }
91 }
92
93 if (RT_SUCCESS(rc))
94 {
95 size_t cbBuf;
96
97 /* Modify VM counter and write back. */
98 if (fAddVM)
99 cAutostartVms++;
100 else
101 cAutostartVms--;
102
103 if (cAutostartVms > 0)
104 {
105 cbBuf = RTStrPrintf(abBuf, sizeof(abBuf), "%u", cAutostartVms);
106 rc = RTFileSetSize(hAutostartFile, cbBuf);
107 if (RT_SUCCESS(rc))
108 rc = RTFileWriteAt(hAutostartFile, 0, abBuf, cbBuf, NULL);
109 }
110 else
111 {
112 /* Just delete the file if there are no VMs left. */
113 RTFileClose(hAutostartFile);
114 RTFileDelete(pszFile);
115 hAutostartFile = NIL_RTFILE;
116 }
117 }
118 }
119 else if (RT_SUCCESS(rc))
120 rc = VERR_FILE_TOO_BIG;
121
122 if (hAutostartFile != NIL_RTFILE)
123 RTFileClose(hAutostartFile);
124 }
125 RTStrFree(pszFile);
126 }
127
128 RTStrFree(pszUser);
129 }
130
131 return rc;
132}
133
134#endif
135
136AutostartDb::AutostartDb()
137{
138#ifdef RT_OS_LINUX
139 int rc = RTCritSectInit(&this->CritSect);
140 NOREF(rc);
141#endif
142}
143
144AutostartDb::~AutostartDb()
145{
146#ifdef RT_OS_LINUX
147 RTCritSectDelete(&this->CritSect);
148#endif
149}
150
151int AutostartDb::addAutostartVM(const char *pszVMId)
152{
153 int rc = VERR_NOT_SUPPORTED;
154
155#if defined(RT_OS_LINUX)
156 NOREF(pszVMId); /* Not needed */
157
158 RTCritSectEnter(&this->CritSect);
159 rc = autostartModifyDb(true /* fAutostart */, true /* fAddVM */);
160 RTCritSectLeave(&this->CritSect);
161#elif defined(RT_OS_DARWIN)
162 NOREF(pszVMId); /* Not needed */
163 rc = VINF_SUCCESS;
164#else
165 NOREF(pszVMId);
166 rc = VERR_NOT_SUPPORTED;
167#endif
168
169 return rc;
170}
171
172int AutostartDb::removeAutostartVM(const char *pszVMId)
173{
174 int rc = VINF_SUCCESS;
175
176#if defined(RT_OS_LINUX)
177 NOREF(pszVMId); /* Not needed */
178 RTCritSectEnter(&this->CritSect);
179 rc = autostartModifyDb(true /* fAutostart */, false /* fAddVM */);
180 RTCritSectLeave(&this->CritSect);
181#elif defined(RT_OS_DARWIN)
182 NOREF(pszVMId); /* Not needed */
183 rc = VINF_SUCCESS;
184#else
185 NOREF(pszVMId);
186 rc = VERR_NOT_SUPPORTED;
187#endif
188
189 return rc;
190}
191
192int AutostartDb::addAutostopVM(const char *pszVMId)
193{
194 int rc = VINF_SUCCESS;
195
196#if defined(RT_OS_LINUX)
197 NOREF(pszVMId); /* Not needed */
198 RTCritSectEnter(&this->CritSect);
199 rc = autostartModifyDb(false /* fAutostart */, true /* fAddVM */);
200 RTCritSectLeave(&this->CritSect);
201#elif defined(RT_OS_DARWIN)
202 NOREF(pszVMId); /* Not needed */
203 rc = VINF_SUCCESS;
204#else
205 NOREF(pszVMId);
206 rc = VERR_NOT_SUPPORTED;
207#endif
208
209 return rc;
210}
211
212int AutostartDb::removeAutostopVM(const char *pszVMId)
213{
214 int rc = VINF_SUCCESS;
215
216#if defined(RT_OS_LINUX)
217 NOREF(pszVMId); /* Not needed */
218 RTCritSectEnter(&this->CritSect);
219 rc = autostartModifyDb(false /* fAutostart */, false /* fAddVM */);
220 RTCritSectLeave(&this->CritSect);
221#elif defined(RT_OS_DARWIN)
222 NOREF(pszVMId); /* Not needed */
223 rc = VINF_SUCCESS;
224#else
225 NOREF(pszVMId);
226 rc = VERR_NOT_SUPPORTED;
227#endif
228
229 return rc;
230}
231
Note: See TracBrowser for help on using the repository browser.

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