1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* virdaemon.c: shared daemon setup code
*
* Copyright (C) 2020 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
# include <sys/wait.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include "virdaemon.h"
#include "virutil.h"
#include "virfile.h"
#include "virlog.h"
#include "viralloc.h"
#include "configmake.h"
#ifndef WIN32
int
virDaemonForkIntoBackground(const char *argv0)
{
int statuspipe[2];
pid_t pid;
if (virPipeQuiet(statuspipe) < 0)
return -1;
pid = fork();
switch (pid) {
case 0:
{
/* intermediate child */
int stdinfd = -1;
int stdoutfd = -1;
int nextpid;
VIR_FORCE_CLOSE(statuspipe[0]);
if ((stdinfd = open("/dev/null", O_RDONLY)) < 0)
goto cleanup;
if ((stdoutfd = open("/dev/null", O_WRONLY)) < 0)
goto cleanup;
if (dup2(stdinfd, STDIN_FILENO) != STDIN_FILENO)
goto cleanup;
if (dup2(stdoutfd, STDOUT_FILENO) != STDOUT_FILENO)
goto cleanup;
if (dup2(stdoutfd, STDERR_FILENO) != STDERR_FILENO)
goto cleanup;
if (VIR_CLOSE(stdinfd) < 0)
goto cleanup;
if (VIR_CLOSE(stdoutfd) < 0)
goto cleanup;
if (setsid() < 0)
goto cleanup;
nextpid = fork();
switch (nextpid) {
case 0: /* grandchild */
return statuspipe[1];
case -1: /* error */
goto cleanup;
default: /* intermediate child succeeded */
_exit(EXIT_SUCCESS);
}
cleanup:
VIR_FORCE_CLOSE(stdoutfd);
VIR_FORCE_CLOSE(stdinfd);
VIR_FORCE_CLOSE(statuspipe[1]);
_exit(EXIT_FAILURE);
}
case -1: /* error in parent */
goto error;
default:
{
/* parent */
int got, exitstatus = 0;
int ret;
char status;
VIR_FORCE_CLOSE(statuspipe[1]);
/* We wait to make sure the first child forked successfully */
if ((got = waitpid(pid, &exitstatus, 0)) < 0 ||
got != pid ||
exitstatus != 0) {
goto error;
}
/* If we got here, then the grandchild was spawned, so we
* must exit. Block until the second child initializes
* successfully */
ret = saferead(statuspipe[0], &status, 1);
VIR_FORCE_CLOSE(statuspipe[0]);
if (ret != 1) {
fprintf(stderr,
_("%1$s: error: unable to determine if daemon is running: %2$s\n"),
argv0,
g_strerror(errno));
exit(EXIT_FAILURE);
} else if (status != 0) {
fprintf(stderr,
_("%1$s: error: %2$s. Check /var/log/messages or run without --daemon for more info.\n"),
argv0,
virDaemonErrTypeToString(status));
exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
}
}
error:
VIR_FORCE_CLOSE(statuspipe[0]);
VIR_FORCE_CLOSE(statuspipe[1]);
return -1;
}
/*
* Set up the logging environment
* By default if daemonized all errors go to the logfile libvirtd.log,
* but if verbose or error debugging is asked for then also output
* informational and debug messages. Default size if 64 kB.
*/
int
virDaemonSetupLogging(const char *daemon_name,
unsigned int log_level,
char *log_filters,
char *log_outputs,
bool privileged,
bool verbose,
bool godaemon)
{
if (virLogReset() < 0)
return -1;
/*
* Libvirtd's order of precedence is:
* cmdline > environment > config
*
* Given the precedence, we must process the variables in the opposite
* order, each one overriding the previous.
*/
if (log_level != 0 &&
virLogSetDefaultPriority(log_level) < 0)
return -1;
/* In case the config is empty, both filters and outputs will become empty,
* however we can't start with empty outputs, thus we'll need to define and
* setup a default one.
*/
if (virLogSetFilters(log_filters) < 0 ||
virLogSetOutputs(log_outputs) < 0)
return -1;
/* If there are some environment variables defined, use those instead */
if (virLogSetFromEnv() < 0)
return -1;
/*
* Command line override for --verbose
*/
if (verbose &&
virLogGetDefaultPriority() > VIR_LOG_INFO &&
virLogSetDefaultPriority(VIR_LOG_INFO) < 0)
return -1;
/* Define the default output. This is only applied if there was no setting
* from either the config or the environment.
*/
if (virLogSetDefaultOutput(daemon_name, godaemon, privileged) < 0)
return -1;
if (virLogGetNbOutputs() == 0 &&
virLogSetOutputs(virLogGetDefaultOutput()) < 0)
return -1;
return 0;
}
int
virDaemonUnixSocketPaths(const char *sock_prefix,
bool privileged,
char *unix_sock_dir,
char **sockfile,
char **rosockfile,
char **admsockfile)
{
int ret = -1;
char *rundir = NULL;
if (unix_sock_dir) {
if (sockfile)
*sockfile = g_strdup_printf("%s/%s-sock", unix_sock_dir, sock_prefix);
if (privileged) {
if (rosockfile)
*rosockfile = g_strdup_printf("%s/%s-sock-ro",
unix_sock_dir, sock_prefix);
if (admsockfile)
*admsockfile = g_strdup_printf("%s/%s-admin-sock",
unix_sock_dir, sock_prefix);
}
} else {
if (privileged) {
if (sockfile)
*sockfile = g_strdup_printf("%s/libvirt/%s-sock",
RUNSTATEDIR, sock_prefix);
if (rosockfile)
*rosockfile = g_strdup_printf("%s/libvirt/%s-sock-ro",
RUNSTATEDIR, sock_prefix);
if (admsockfile)
*admsockfile = g_strdup_printf("%s/libvirt/%s-admin-sock",
RUNSTATEDIR, sock_prefix);
} else {
mode_t old_umask;
rundir = virGetUserRuntimeDirectory();
old_umask = umask(077);
if (g_mkdir_with_parents(rundir, 0777) < 0) {
umask(old_umask);
goto cleanup;
}
umask(old_umask);
if (sockfile)
*sockfile = g_strdup_printf("%s/%s-sock", rundir, sock_prefix);
if (admsockfile)
*admsockfile = g_strdup_printf("%s/%s-admin-sock", rundir, sock_prefix);
}
}
ret = 0;
cleanup:
VIR_FREE(rundir);
return ret;
}
#else /* WIN32 */
int virDaemonForkIntoBackground(const char *argv0 G_GNUC_UNUSED)
{
errno = ENOTSUP;
return -1;
}
int virDaemonSetupLogging(const char *daemon_name G_GNUC_UNUSED,
unsigned int log_level G_GNUC_UNUSED,
char *log_filters G_GNUC_UNUSED,
char *log_outputs G_GNUC_UNUSED,
bool privileged G_GNUC_UNUSED,
bool verbose G_GNUC_UNUSED,
bool godaemon G_GNUC_UNUSED)
{
/* NOOP */
errno = ENOTSUP;
return -1;
}
int virDaemonUnixSocketPaths(const char *sock_prefix G_GNUC_UNUSED,
bool privileged G_GNUC_UNUSED,
char *unix_sock_dir G_GNUC_UNUSED,
char **sockfile G_GNUC_UNUSED,
char **rosockfile G_GNUC_UNUSED,
char **adminSockfile G_GNUC_UNUSED)
{
errno = ENOTSUP;
return -1;
}
#endif /* WIN32 */
|