root / os-posix.c @ feature-archipelago
History | View | Annotate | Download (7.8 kB)
1 | 86b645e7 | Jes Sorensen | /*
|
---|---|---|---|
2 | 86b645e7 | Jes Sorensen | * os-posix.c
|
3 | 86b645e7 | Jes Sorensen | *
|
4 | 86b645e7 | Jes Sorensen | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 86b645e7 | Jes Sorensen | * Copyright (c) 2010 Red Hat, Inc.
|
6 | 86b645e7 | Jes Sorensen | *
|
7 | 86b645e7 | Jes Sorensen | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | 86b645e7 | Jes Sorensen | * of this software and associated documentation files (the "Software"), to deal
|
9 | 86b645e7 | Jes Sorensen | * in the Software without restriction, including without limitation the rights
|
10 | 86b645e7 | Jes Sorensen | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | 86b645e7 | Jes Sorensen | * copies of the Software, and to permit persons to whom the Software is
|
12 | 86b645e7 | Jes Sorensen | * furnished to do so, subject to the following conditions:
|
13 | 86b645e7 | Jes Sorensen | *
|
14 | 86b645e7 | Jes Sorensen | * The above copyright notice and this permission notice shall be included in
|
15 | 86b645e7 | Jes Sorensen | * all copies or substantial portions of the Software.
|
16 | 86b645e7 | Jes Sorensen | *
|
17 | 86b645e7 | Jes Sorensen | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | 86b645e7 | Jes Sorensen | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | 86b645e7 | Jes Sorensen | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | 86b645e7 | Jes Sorensen | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | 86b645e7 | Jes Sorensen | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | 86b645e7 | Jes Sorensen | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | 86b645e7 | Jes Sorensen | * THE SOFTWARE.
|
24 | 86b645e7 | Jes Sorensen | */
|
25 | 86b645e7 | Jes Sorensen | |
26 | 86b645e7 | Jes Sorensen | #include <unistd.h> |
27 | 86b645e7 | Jes Sorensen | #include <fcntl.h> |
28 | 86b645e7 | Jes Sorensen | #include <signal.h> |
29 | 8d963e6a | Jes Sorensen | #include <sys/types.h> |
30 | 8d963e6a | Jes Sorensen | #include <sys/wait.h> |
31 | 69e8b162 | Jes Sorensen | /*needed for MAP_POPULATE before including qemu-options.h */
|
32 | 69e8b162 | Jes Sorensen | #include <sys/mman.h> |
33 | 8847cfe8 | Jes Sorensen | #include <pwd.h> |
34 | cc4662f9 | Stefan Hajnoczi | #include <grp.h> |
35 | 6170540b | Jes Sorensen | #include <libgen.h> |
36 | 86b645e7 | Jes Sorensen | |
37 | 86b645e7 | Jes Sorensen | /* Needed early for CONFIG_BSD etc. */
|
38 | 86b645e7 | Jes Sorensen | #include "config-host.h" |
39 | 9c17d615 | Paolo Bonzini | #include "sysemu/sysemu.h" |
40 | 59a5264b | Jes Sorensen | #include "net/slirp.h" |
41 | 59a5264b | Jes Sorensen | #include "qemu-options.h" |
42 | 86b645e7 | Jes Sorensen | |
43 | ce798cf2 | Jes Sorensen | #ifdef CONFIG_LINUX
|
44 | ce798cf2 | Jes Sorensen | #include <sys/prctl.h> |
45 | 949d31e6 | Jes Sorensen | #endif
|
46 | 949d31e6 | Jes Sorensen | |
47 | 15fdaee3 | Nathan Whitehorn | #ifdef __FreeBSD__
|
48 | 15fdaee3 | Nathan Whitehorn | #include <sys/sysctl.h> |
49 | 15fdaee3 | Nathan Whitehorn | #endif
|
50 | 15fdaee3 | Nathan Whitehorn | |
51 | 8847cfe8 | Jes Sorensen | static struct passwd *user_pwd; |
52 | 0766379d | Jes Sorensen | static const char *chroot_dir; |
53 | eb505be1 | Jes Sorensen | static int daemonize; |
54 | eb505be1 | Jes Sorensen | static int fds[2]; |
55 | 8847cfe8 | Jes Sorensen | |
56 | fe98ac14 | Jes Sorensen | void os_setup_early_signal_handling(void) |
57 | 86b645e7 | Jes Sorensen | { |
58 | 86b645e7 | Jes Sorensen | struct sigaction act;
|
59 | 86b645e7 | Jes Sorensen | sigfillset(&act.sa_mask); |
60 | 86b645e7 | Jes Sorensen | act.sa_flags = 0;
|
61 | 86b645e7 | Jes Sorensen | act.sa_handler = SIG_IGN; |
62 | 86b645e7 | Jes Sorensen | sigaction(SIGPIPE, &act, NULL);
|
63 | 86b645e7 | Jes Sorensen | } |
64 | 8d963e6a | Jes Sorensen | |
65 | f64622c4 | Gleb Natapov | static void termsig_handler(int signal, siginfo_t *info, void *c) |
66 | 8d963e6a | Jes Sorensen | { |
67 | f64622c4 | Gleb Natapov | qemu_system_killed(info->si_signo, info->si_pid); |
68 | 8d963e6a | Jes Sorensen | } |
69 | 8d963e6a | Jes Sorensen | |
70 | 8d963e6a | Jes Sorensen | void os_setup_signal_handling(void) |
71 | 8d963e6a | Jes Sorensen | { |
72 | 8d963e6a | Jes Sorensen | struct sigaction act;
|
73 | 8d963e6a | Jes Sorensen | |
74 | 8d963e6a | Jes Sorensen | memset(&act, 0, sizeof(act)); |
75 | f64622c4 | Gleb Natapov | act.sa_sigaction = termsig_handler; |
76 | f64622c4 | Gleb Natapov | act.sa_flags = SA_SIGINFO; |
77 | 8d963e6a | Jes Sorensen | sigaction(SIGINT, &act, NULL);
|
78 | 8d963e6a | Jes Sorensen | sigaction(SIGHUP, &act, NULL);
|
79 | 8d963e6a | Jes Sorensen | sigaction(SIGTERM, &act, NULL);
|
80 | 8d963e6a | Jes Sorensen | } |
81 | 6170540b | Jes Sorensen | |
82 | 6170540b | Jes Sorensen | /* Find a likely location for support files using the location of the binary.
|
83 | 6170540b | Jes Sorensen | For installed binaries this will be "$bindir/../share/qemu". When
|
84 | 6170540b | Jes Sorensen | running from the build tree this will be "$bindir/../pc-bios". */
|
85 | 6170540b | Jes Sorensen | #define SHARE_SUFFIX "/share/qemu" |
86 | 6170540b | Jes Sorensen | #define BUILD_SUFFIX "/pc-bios" |
87 | 10f5bff6 | Fam Zheng | char *os_find_datadir(void) |
88 | 6170540b | Jes Sorensen | { |
89 | 10f5bff6 | Fam Zheng | char *dir, *exec_dir;
|
90 | 6170540b | Jes Sorensen | char *res;
|
91 | 6170540b | Jes Sorensen | size_t max_len; |
92 | 6170540b | Jes Sorensen | |
93 | 10f5bff6 | Fam Zheng | exec_dir = qemu_get_exec_dir(); |
94 | 10f5bff6 | Fam Zheng | if (exec_dir == NULL) { |
95 | 10f5bff6 | Fam Zheng | return NULL; |
96 | 6170540b | Jes Sorensen | } |
97 | 10f5bff6 | Fam Zheng | dir = dirname(exec_dir); |
98 | 6170540b | Jes Sorensen | |
99 | 6170540b | Jes Sorensen | max_len = strlen(dir) + |
100 | 6170540b | Jes Sorensen | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
|
101 | 7267c094 | Anthony Liguori | res = g_malloc0(max_len); |
102 | 6170540b | Jes Sorensen | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
|
103 | 6170540b | Jes Sorensen | if (access(res, R_OK)) {
|
104 | 6170540b | Jes Sorensen | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
|
105 | 6170540b | Jes Sorensen | if (access(res, R_OK)) {
|
106 | 7267c094 | Anthony Liguori | g_free(res); |
107 | 6170540b | Jes Sorensen | res = NULL;
|
108 | 6170540b | Jes Sorensen | } |
109 | 6170540b | Jes Sorensen | } |
110 | 6170540b | Jes Sorensen | |
111 | 10f5bff6 | Fam Zheng | g_free(exec_dir); |
112 | 6170540b | Jes Sorensen | return res;
|
113 | 6170540b | Jes Sorensen | } |
114 | 6170540b | Jes Sorensen | #undef SHARE_SUFFIX
|
115 | 6170540b | Jes Sorensen | #undef BUILD_SUFFIX
|
116 | 59a5264b | Jes Sorensen | |
117 | ce798cf2 | Jes Sorensen | void os_set_proc_name(const char *s) |
118 | ce798cf2 | Jes Sorensen | { |
119 | ce798cf2 | Jes Sorensen | #if defined(PR_SET_NAME)
|
120 | ce798cf2 | Jes Sorensen | char name[16]; |
121 | ce798cf2 | Jes Sorensen | if (!s)
|
122 | ce798cf2 | Jes Sorensen | return;
|
123 | 3eadc68e | Jim Meyering | pstrcpy(name, sizeof(name), s);
|
124 | ce798cf2 | Jes Sorensen | /* Could rewrite argv[0] too, but that's a bit more complicated.
|
125 | ce798cf2 | Jes Sorensen | This simple way is enough for `top'. */
|
126 | ce798cf2 | Jes Sorensen | if (prctl(PR_SET_NAME, name)) {
|
127 | ce798cf2 | Jes Sorensen | perror("unable to change process name");
|
128 | ce798cf2 | Jes Sorensen | exit(1);
|
129 | ce798cf2 | Jes Sorensen | } |
130 | ce798cf2 | Jes Sorensen | #else
|
131 | ce798cf2 | Jes Sorensen | fprintf(stderr, "Change of process name not supported by your OS\n");
|
132 | ce798cf2 | Jes Sorensen | exit(1);
|
133 | ce798cf2 | Jes Sorensen | #endif
|
134 | ce798cf2 | Jes Sorensen | } |
135 | ce798cf2 | Jes Sorensen | |
136 | 59a5264b | Jes Sorensen | /*
|
137 | 59a5264b | Jes Sorensen | * Parse OS specific command line options.
|
138 | 59a5264b | Jes Sorensen | * return 0 if option handled, -1 otherwise
|
139 | 59a5264b | Jes Sorensen | */
|
140 | 59a5264b | Jes Sorensen | void os_parse_cmd_args(int index, const char *optarg) |
141 | 59a5264b | Jes Sorensen | { |
142 | 59a5264b | Jes Sorensen | switch (index) {
|
143 | 59a5264b | Jes Sorensen | #ifdef CONFIG_SLIRP
|
144 | 59a5264b | Jes Sorensen | case QEMU_OPTION_smb:
|
145 | 59a5264b | Jes Sorensen | if (net_slirp_smb(optarg) < 0) |
146 | 59a5264b | Jes Sorensen | exit(1);
|
147 | 59a5264b | Jes Sorensen | break;
|
148 | 59a5264b | Jes Sorensen | #endif
|
149 | 8847cfe8 | Jes Sorensen | case QEMU_OPTION_runas:
|
150 | 8847cfe8 | Jes Sorensen | user_pwd = getpwnam(optarg); |
151 | 8847cfe8 | Jes Sorensen | if (!user_pwd) {
|
152 | 8847cfe8 | Jes Sorensen | fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
|
153 | 8847cfe8 | Jes Sorensen | exit(1);
|
154 | 8847cfe8 | Jes Sorensen | } |
155 | 8847cfe8 | Jes Sorensen | break;
|
156 | 0766379d | Jes Sorensen | case QEMU_OPTION_chroot:
|
157 | 0766379d | Jes Sorensen | chroot_dir = optarg; |
158 | 0766379d | Jes Sorensen | break;
|
159 | eb505be1 | Jes Sorensen | case QEMU_OPTION_daemonize:
|
160 | eb505be1 | Jes Sorensen | daemonize = 1;
|
161 | eb505be1 | Jes Sorensen | break;
|
162 | 70678b82 | Anthony Liguori | #if defined(CONFIG_LINUX)
|
163 | 70678b82 | Anthony Liguori | case QEMU_OPTION_enablefips:
|
164 | 70678b82 | Anthony Liguori | fips_set_state(true);
|
165 | 70678b82 | Anthony Liguori | break;
|
166 | 70678b82 | Anthony Liguori | #endif
|
167 | 59a5264b | Jes Sorensen | } |
168 | 59a5264b | Jes Sorensen | } |
169 | 8847cfe8 | Jes Sorensen | |
170 | e06eb601 | Jes Sorensen | static void change_process_uid(void) |
171 | 8847cfe8 | Jes Sorensen | { |
172 | 8847cfe8 | Jes Sorensen | if (user_pwd) {
|
173 | 8847cfe8 | Jes Sorensen | if (setgid(user_pwd->pw_gid) < 0) { |
174 | 8847cfe8 | Jes Sorensen | fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
|
175 | 8847cfe8 | Jes Sorensen | exit(1);
|
176 | 8847cfe8 | Jes Sorensen | } |
177 | cc4662f9 | Stefan Hajnoczi | if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) { |
178 | cc4662f9 | Stefan Hajnoczi | fprintf(stderr, "Failed to initgroups(\"%s\", %d)\n",
|
179 | cc4662f9 | Stefan Hajnoczi | user_pwd->pw_name, user_pwd->pw_gid); |
180 | cc4662f9 | Stefan Hajnoczi | exit(1);
|
181 | cc4662f9 | Stefan Hajnoczi | } |
182 | 8847cfe8 | Jes Sorensen | if (setuid(user_pwd->pw_uid) < 0) { |
183 | 8847cfe8 | Jes Sorensen | fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
|
184 | 8847cfe8 | Jes Sorensen | exit(1);
|
185 | 8847cfe8 | Jes Sorensen | } |
186 | 8847cfe8 | Jes Sorensen | if (setuid(0) != -1) { |
187 | 8847cfe8 | Jes Sorensen | fprintf(stderr, "Dropping privileges failed\n");
|
188 | 8847cfe8 | Jes Sorensen | exit(1);
|
189 | 8847cfe8 | Jes Sorensen | } |
190 | 8847cfe8 | Jes Sorensen | } |
191 | 8847cfe8 | Jes Sorensen | } |
192 | 0766379d | Jes Sorensen | |
193 | e06eb601 | Jes Sorensen | static void change_root(void) |
194 | 0766379d | Jes Sorensen | { |
195 | 0766379d | Jes Sorensen | if (chroot_dir) {
|
196 | 0766379d | Jes Sorensen | if (chroot(chroot_dir) < 0) { |
197 | 0766379d | Jes Sorensen | fprintf(stderr, "chroot failed\n");
|
198 | 0766379d | Jes Sorensen | exit(1);
|
199 | 0766379d | Jes Sorensen | } |
200 | 0766379d | Jes Sorensen | if (chdir("/")) { |
201 | 0766379d | Jes Sorensen | perror("not able to chdir to /");
|
202 | 0766379d | Jes Sorensen | exit(1);
|
203 | 0766379d | Jes Sorensen | } |
204 | 0766379d | Jes Sorensen | } |
205 | 0766379d | Jes Sorensen | |
206 | 0766379d | Jes Sorensen | } |
207 | eb505be1 | Jes Sorensen | |
208 | eb505be1 | Jes Sorensen | void os_daemonize(void) |
209 | eb505be1 | Jes Sorensen | { |
210 | eb505be1 | Jes Sorensen | if (daemonize) {
|
211 | eb505be1 | Jes Sorensen | pid_t pid; |
212 | eb505be1 | Jes Sorensen | |
213 | eb505be1 | Jes Sorensen | if (pipe(fds) == -1) |
214 | eb505be1 | Jes Sorensen | exit(1);
|
215 | eb505be1 | Jes Sorensen | |
216 | eb505be1 | Jes Sorensen | pid = fork(); |
217 | eb505be1 | Jes Sorensen | if (pid > 0) { |
218 | eb505be1 | Jes Sorensen | uint8_t status; |
219 | eb505be1 | Jes Sorensen | ssize_t len; |
220 | eb505be1 | Jes Sorensen | |
221 | eb505be1 | Jes Sorensen | close(fds[1]);
|
222 | eb505be1 | Jes Sorensen | |
223 | eb505be1 | Jes Sorensen | again:
|
224 | eb505be1 | Jes Sorensen | len = read(fds[0], &status, 1); |
225 | eb505be1 | Jes Sorensen | if (len == -1 && (errno == EINTR)) |
226 | eb505be1 | Jes Sorensen | goto again;
|
227 | eb505be1 | Jes Sorensen | |
228 | eb505be1 | Jes Sorensen | if (len != 1) |
229 | eb505be1 | Jes Sorensen | exit(1);
|
230 | eb505be1 | Jes Sorensen | else if (status == 1) { |
231 | eb505be1 | Jes Sorensen | fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
|
232 | eb505be1 | Jes Sorensen | exit(1);
|
233 | eb505be1 | Jes Sorensen | } else
|
234 | eb505be1 | Jes Sorensen | exit(0);
|
235 | eb505be1 | Jes Sorensen | } else if (pid < 0) |
236 | eb505be1 | Jes Sorensen | exit(1);
|
237 | eb505be1 | Jes Sorensen | |
238 | eb505be1 | Jes Sorensen | close(fds[0]);
|
239 | eb505be1 | Jes Sorensen | qemu_set_cloexec(fds[1]);
|
240 | eb505be1 | Jes Sorensen | |
241 | eb505be1 | Jes Sorensen | setsid(); |
242 | eb505be1 | Jes Sorensen | |
243 | eb505be1 | Jes Sorensen | pid = fork(); |
244 | eb505be1 | Jes Sorensen | if (pid > 0) |
245 | eb505be1 | Jes Sorensen | exit(0);
|
246 | eb505be1 | Jes Sorensen | else if (pid < 0) |
247 | eb505be1 | Jes Sorensen | exit(1);
|
248 | eb505be1 | Jes Sorensen | |
249 | eb505be1 | Jes Sorensen | umask(027);
|
250 | eb505be1 | Jes Sorensen | |
251 | eb505be1 | Jes Sorensen | signal(SIGTSTP, SIG_IGN); |
252 | eb505be1 | Jes Sorensen | signal(SIGTTOU, SIG_IGN); |
253 | eb505be1 | Jes Sorensen | signal(SIGTTIN, SIG_IGN); |
254 | eb505be1 | Jes Sorensen | } |
255 | eb505be1 | Jes Sorensen | } |
256 | eb505be1 | Jes Sorensen | |
257 | eb505be1 | Jes Sorensen | void os_setup_post(void) |
258 | eb505be1 | Jes Sorensen | { |
259 | eb505be1 | Jes Sorensen | int fd = 0; |
260 | eb505be1 | Jes Sorensen | |
261 | eb505be1 | Jes Sorensen | if (daemonize) {
|
262 | eb505be1 | Jes Sorensen | uint8_t status = 0;
|
263 | eb505be1 | Jes Sorensen | ssize_t len; |
264 | eb505be1 | Jes Sorensen | |
265 | eb505be1 | Jes Sorensen | again1:
|
266 | eb505be1 | Jes Sorensen | len = write(fds[1], &status, 1); |
267 | eb505be1 | Jes Sorensen | if (len == -1 && (errno == EINTR)) |
268 | eb505be1 | Jes Sorensen | goto again1;
|
269 | eb505be1 | Jes Sorensen | |
270 | eb505be1 | Jes Sorensen | if (len != 1) |
271 | eb505be1 | Jes Sorensen | exit(1);
|
272 | eb505be1 | Jes Sorensen | |
273 | eb505be1 | Jes Sorensen | if (chdir("/")) { |
274 | eb505be1 | Jes Sorensen | perror("not able to chdir to /");
|
275 | eb505be1 | Jes Sorensen | exit(1);
|
276 | eb505be1 | Jes Sorensen | } |
277 | eb505be1 | Jes Sorensen | TFR(fd = qemu_open("/dev/null", O_RDWR));
|
278 | eb505be1 | Jes Sorensen | if (fd == -1) |
279 | eb505be1 | Jes Sorensen | exit(1);
|
280 | eb505be1 | Jes Sorensen | } |
281 | eb505be1 | Jes Sorensen | |
282 | e06eb601 | Jes Sorensen | change_root(); |
283 | e06eb601 | Jes Sorensen | change_process_uid(); |
284 | eb505be1 | Jes Sorensen | |
285 | eb505be1 | Jes Sorensen | if (daemonize) {
|
286 | eb505be1 | Jes Sorensen | dup2(fd, 0);
|
287 | eb505be1 | Jes Sorensen | dup2(fd, 1);
|
288 | eb505be1 | Jes Sorensen | dup2(fd, 2);
|
289 | eb505be1 | Jes Sorensen | |
290 | eb505be1 | Jes Sorensen | close(fd); |
291 | eb505be1 | Jes Sorensen | } |
292 | eb505be1 | Jes Sorensen | } |
293 | eb505be1 | Jes Sorensen | |
294 | eb505be1 | Jes Sorensen | void os_pidfile_error(void) |
295 | eb505be1 | Jes Sorensen | { |
296 | eb505be1 | Jes Sorensen | if (daemonize) {
|
297 | eb505be1 | Jes Sorensen | uint8_t status = 1;
|
298 | eb505be1 | Jes Sorensen | if (write(fds[1], &status, 1) != 1) { |
299 | eb505be1 | Jes Sorensen | perror("daemonize. Writing to pipe\n");
|
300 | eb505be1 | Jes Sorensen | } |
301 | eb505be1 | Jes Sorensen | } else
|
302 | eb505be1 | Jes Sorensen | fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
|
303 | eb505be1 | Jes Sorensen | } |
304 | 9156d763 | Jes Sorensen | |
305 | 9156d763 | Jes Sorensen | void os_set_line_buffering(void) |
306 | 9156d763 | Jes Sorensen | { |
307 | 9156d763 | Jes Sorensen | setvbuf(stdout, NULL, _IOLBF, 0); |
308 | 9156d763 | Jes Sorensen | } |
309 | 949d31e6 | Jes Sorensen | |
310 | bc4a957c | Jes Sorensen | int qemu_create_pidfile(const char *filename) |
311 | bc4a957c | Jes Sorensen | { |
312 | bc4a957c | Jes Sorensen | char buffer[128]; |
313 | bc4a957c | Jes Sorensen | int len;
|
314 | bc4a957c | Jes Sorensen | int fd;
|
315 | bc4a957c | Jes Sorensen | |
316 | bc4a957c | Jes Sorensen | fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
|
317 | bc4a957c | Jes Sorensen | if (fd == -1) { |
318 | bc4a957c | Jes Sorensen | return -1; |
319 | bc4a957c | Jes Sorensen | } |
320 | bc4a957c | Jes Sorensen | if (lockf(fd, F_TLOCK, 0) == -1) { |
321 | 1bbd1592 | Markus Armbruster | close(fd); |
322 | bc4a957c | Jes Sorensen | return -1; |
323 | bc4a957c | Jes Sorensen | } |
324 | 953ffe0f | Andreas Färber | len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid()); |
325 | bc4a957c | Jes Sorensen | if (write(fd, buffer, len) != len) {
|
326 | 1bbd1592 | Markus Armbruster | close(fd); |
327 | bc4a957c | Jes Sorensen | return -1; |
328 | bc4a957c | Jes Sorensen | } |
329 | bc4a957c | Jes Sorensen | |
330 | 93dd748b | Laszlo Ersek | /* keep pidfile open & locked forever */
|
331 | bc4a957c | Jes Sorensen | return 0; |
332 | bc4a957c | Jes Sorensen | } |
333 | 995ee2bf | Hitoshi Mitake | |
334 | 995ee2bf | Hitoshi Mitake | bool is_daemonized(void) |
335 | 995ee2bf | Hitoshi Mitake | { |
336 | 995ee2bf | Hitoshi Mitake | return daemonize;
|
337 | 995ee2bf | Hitoshi Mitake | } |
338 | 888a6bc6 | Satoru Moriya | |
339 | 888a6bc6 | Satoru Moriya | int os_mlock(void) |
340 | 888a6bc6 | Satoru Moriya | { |
341 | 888a6bc6 | Satoru Moriya | int ret = 0; |
342 | 888a6bc6 | Satoru Moriya | |
343 | 888a6bc6 | Satoru Moriya | ret = mlockall(MCL_CURRENT | MCL_FUTURE); |
344 | 888a6bc6 | Satoru Moriya | if (ret < 0) { |
345 | 888a6bc6 | Satoru Moriya | perror("mlockall");
|
346 | 888a6bc6 | Satoru Moriya | } |
347 | 888a6bc6 | Satoru Moriya | |
348 | 888a6bc6 | Satoru Moriya | return ret;
|
349 | 888a6bc6 | Satoru Moriya | } |