VirtualBox

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

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

Autostart: Updates, implemented config parser for the autostart daemon

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* $Id: AutostartDb-generic.cpp 42118 2012-07-11 19:43:11Z 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 else
57 fOpen |= RTFILE_O_OPEN;
58
59 rc = RTStrAPrintf(&pszFile, "%s/%s.%s",
60 AUTOSTART_DATABASE, pszUser, fAutostart ? "start" : "stop");
61 if (RT_SUCCESS(rc))
62 {
63 rc = RTFileOpen(&hAutostartFile, pszFile, fOpen);
64 if (RT_SUCCESS(rc))
65 {
66 uint64_t cbFile;
67
68 /*
69 * Files with more than 16 bytes are rejected because they just contain
70 * a number of the amount of VMs with autostart configured, so they
71 * should be really really small. Anything else is bogus.
72 */
73 rc = RTFileGetSize(hAutostartFile, &cbFile);
74 if ( RT_SUCCESS(rc)
75 && cbFile <= 16)
76 {
77 char abBuf[16 + 1]; /* trailing \0 */
78 uint32_t cAutostartVms = 0;
79
80 memset(abBuf, 0, sizeof(abBuf));
81
82 /* Check if the file was just created. */
83 if (cbFile)
84 {
85 rc = RTFileRead(hAutostartFile, abBuf, cbFile, NULL);
86 if (RT_SUCCESS(rc))
87 {
88 rc = RTStrToUInt32Ex(abBuf, NULL, 10 /* uBase */, &cAutostartVms);
89 if ( rc == VWRN_TRAILING_CHARS
90 || rc == VWRN_TRAILING_SPACES)
91 rc = VINF_SUCCESS;
92 }
93 }
94
95 if (RT_SUCCESS(rc))
96 {
97 size_t cbBuf;
98
99 /* Modify VM counter and write back. */
100 if (fAddVM)
101 cAutostartVms++;
102 else
103 cAutostartVms--;
104
105 if (cAutostartVms > 0)
106 {
107 cbBuf = RTStrPrintf(abBuf, sizeof(abBuf), "%u", cAutostartVms);
108 rc = RTFileSetSize(hAutostartFile, cbBuf);
109 if (RT_SUCCESS(rc))
110 rc = RTFileWriteAt(hAutostartFile, 0, abBuf, cbBuf, NULL);
111 }
112 else
113 {
114 /* Just delete the file if there are no VMs left. */
115 RTFileClose(hAutostartFile);
116 RTFileDelete(pszFile);
117 hAutostartFile = NIL_RTFILE;
118 }
119 }
120 }
121 else if (RT_SUCCESS(rc))
122 rc = VERR_FILE_TOO_BIG;
123
124 if (hAutostartFile != NIL_RTFILE)
125 RTFileClose(hAutostartFile);
126 }
127 RTStrFree(pszFile);
128 }
129
130 RTStrFree(pszUser);
131 }
132
133 return rc;
134}
135
136#endif
137
138AutostartDb::AutostartDb()
139{
140#ifdef RT_OS_LINUX
141 int rc = RTCritSectInit(&this->CritSect);
142 NOREF(rc);
143#endif
144}
145
146AutostartDb::~AutostartDb()
147{
148#ifdef RT_OS_LINUX
149 RTCritSectDelete(&this->CritSect);
150#endif
151}
152
153int AutostartDb::addAutostartVM(const char *pszVMId)
154{
155 int rc = VERR_NOT_SUPPORTED;
156
157#if defined(RT_OS_LINUX)
158 NOREF(pszVMId); /* Not needed */
159
160 RTCritSectEnter(&this->CritSect);
161 rc = autostartModifyDb(true /* fAutostart */, true /* fAddVM */);
162 RTCritSectLeave(&this->CritSect);
163#elif defined(RT_OS_DARWIN)
164 NOREF(pszVMId); /* Not needed */
165 rc = VINF_SUCCESS;
166#else
167 NOREF(pszVMId);
168 rc = VERR_NOT_SUPPORTED;
169#endif
170
171 return rc;
172}
173
174int AutostartDb::removeAutostartVM(const char *pszVMId)
175{
176 int rc = VINF_SUCCESS;
177
178#if defined(RT_OS_LINUX)
179 NOREF(pszVMId); /* Not needed */
180 RTCritSectEnter(&this->CritSect);
181 rc = autostartModifyDb(true /* fAutostart */, false /* fAddVM */);
182 RTCritSectLeave(&this->CritSect);
183#elif defined(RT_OS_DARWIN)
184 NOREF(pszVMId); /* Not needed */
185 rc = VINF_SUCCESS;
186#else
187 NOREF(pszVMId);
188 rc = VERR_NOT_SUPPORTED;
189#endif
190
191 return rc;
192}
193
194int AutostartDb::addAutostopVM(const char *pszVMId)
195{
196 int rc = VINF_SUCCESS;
197
198#if defined(RT_OS_LINUX)
199 NOREF(pszVMId); /* Not needed */
200 RTCritSectEnter(&this->CritSect);
201 rc = autostartModifyDb(false /* fAutostart */, true /* fAddVM */);
202 RTCritSectLeave(&this->CritSect);
203#elif defined(RT_OS_DARWIN)
204 NOREF(pszVMId); /* Not needed */
205 rc = VINF_SUCCESS;
206#else
207 NOREF(pszVMId);
208 rc = VERR_NOT_SUPPORTED;
209#endif
210
211 return rc;
212}
213
214int AutostartDb::removeAutostopVM(const char *pszVMId)
215{
216 int rc = VINF_SUCCESS;
217
218#if defined(RT_OS_LINUX)
219 NOREF(pszVMId); /* Not needed */
220 RTCritSectEnter(&this->CritSect);
221 rc = autostartModifyDb(false /* fAutostart */, false /* fAddVM */);
222 RTCritSectLeave(&this->CritSect);
223#elif defined(RT_OS_DARWIN)
224 NOREF(pszVMId); /* Not needed */
225 rc = VINF_SUCCESS;
226#else
227 NOREF(pszVMId);
228 rc = VERR_NOT_SUPPORTED;
229#endif
230
231 return rc;
232}
233
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