chiark / gitweb /
reset signal mask when forking
[elogind.git] / execute.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <assert.h>
4 #include <dirent.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <signal.h>
10
11 #include "execute.h"
12 #include "strv.h"
13 #include "macro.h"
14 #include "util.h"
15 #include "log.h"
16
17 static int close_fds(int except[], unsigned n_except) {
18         DIR *d;
19         struct dirent *de;
20         int r = 0;
21
22         /* Modifies the fds array! (sorts it) */
23
24         if (!(d = opendir("/proc/self/fd")))
25                 return -errno;
26
27         while ((de = readdir(d))) {
28                 int fd;
29
30                 if (de->d_name[0] == '.')
31                         continue;
32
33                 if ((r = safe_atoi(de->d_name, &fd)) < 0)
34                         goto finish;
35
36                 if (fd < 3)
37                         continue;
38
39                 if (fd == dirfd(d))
40                         continue;
41
42                 if (except) {
43                         bool found;
44                         unsigned i;
45
46                         found = false;
47                         for (i = 0; i < n_except; i++)
48                                 if (except[i] == fd) {
49                                         found = true;
50                                         break;
51                                 }
52
53                         if (found)
54                                 continue;
55                 }
56
57                 if ((r = close_nointr(fd)) < 0)
58                         goto finish;
59         }
60
61 finish:
62         closedir(d);
63         return r;
64 }
65
66 static int shift_fds(int fds[], unsigned n_fds) {
67         int start, restart_from;
68
69         if (n_fds <= 0)
70                 return 0;
71
72         assert(fds);
73
74         start = 0;
75         for (;;) {
76                 int i;
77
78                 restart_from = -1;
79
80                 for (i = start; i < (int) n_fds; i++) {
81                         int nfd;
82
83                         /* Already at right index? */
84                         if (fds[i] == i+3)
85                                 continue;
86
87                         if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
88                                 return -errno;
89
90                         assert_se(close_nointr(fds[i]) == 0);
91                         fds[i] = nfd;
92
93                         /* Hmm, the fd we wanted isn't free? Then
94                          * let's remember that and try again from here*/
95                         if (nfd != i+3 && restart_from < 0)
96                                 restart_from = i;
97                 }
98
99                 if (restart_from < 0)
100                         break;
101
102                 start = restart_from;
103         }
104
105         return 0;
106 }
107
108 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
109         pid_t pid;
110
111         assert(command);
112         assert(context);
113         assert(ret);
114         assert(fds || n_fds <= 0);
115
116         log_debug("about to execute %s", command->path);
117
118         if ((pid = fork()) < 0)
119                 return -errno;
120
121         if (pid == 0) {
122                 char **e, **f = NULL;
123                 int i, r;
124                 char t[16];
125                 sigset_t ss;
126
127                 /* child */
128
129                 if (sigemptyset(&ss) < 0 ||
130                     sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
131                         r = EXIT_SIGNAL_MASK;
132                         goto fail;
133                 }
134
135                 umask(context->umask);
136
137                 if (chdir(context->directory ? context->directory : "/") < 0) {
138                         r = EXIT_CHDIR;
139                         goto fail;
140                 }
141
142                 snprintf(t, sizeof(t), "%i", context->oom_adjust);
143                 char_array_0(t);
144
145                 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
146                         r = EXIT_OOM_ADJUST;
147                         goto fail;
148                 }
149
150                 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
151                         r = EXIT_NICE;
152                         goto fail;
153                 }
154
155                 if (close_fds(fds, n_fds) < 0 ||
156                     shift_fds(fds, n_fds) < 0) {
157                         r = EXIT_FDS;
158                         goto fail;
159                 }
160
161                 for (i = 0; i < RLIMIT_NLIMITS; i++) {
162                         if (!context->rlimit[i])
163                                 continue;
164
165                         if (setrlimit(i, context->rlimit[i]) < 0) {
166                                 r = EXIT_LIMITS;
167                                 goto fail;
168                         }
169                 }
170
171                 if (n_fds > 0) {
172                         char a[64], b[64];
173                         char *listen_env[3] = {
174                                 a,
175                                 b,
176                                 NULL
177                         };
178
179                         snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
180                         snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
181
182                         a[sizeof(a)-1] = 0;
183                         b[sizeof(b)-1] = 0;
184
185                         if (context->environment) {
186                                 if (!(f = strv_merge(listen_env, context->environment))) {
187                                         r = EXIT_MEMORY;
188                                         goto fail;
189                                 }
190                                 e = f;
191                         } else
192                                 e = listen_env;
193
194                 } else
195                         e = context->environment;
196
197                 execve(command->path, command->argv, e);
198                 r = EXIT_EXEC;
199
200         fail:
201                 strv_free(f);
202                 _exit(r);
203         }
204
205
206         log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
207
208         *ret = pid;
209         return 0;
210 }
211
212 void exec_context_init(ExecContext *c) {
213         assert(c);
214
215         c->umask = 0002;
216         cap_clear(c->capabilities);
217         c->oom_adjust = 0;
218         c->nice = 0;
219 }
220
221 void exec_context_done(ExecContext *c) {
222         unsigned l;
223
224         assert(c);
225
226         strv_free(c->environment);
227         c->environment = NULL;
228
229         for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
230                 free(c->rlimit[l]);
231                 c->rlimit[l] = NULL;
232         }
233
234         free(c->directory);
235         c->directory = NULL;
236
237         free(c->user);
238         c->user = NULL;
239
240         free(c->group);
241         c->group = NULL;
242
243         strv_free(c->supplementary_groups);
244         c->supplementary_groups = NULL;
245 }
246
247 void exec_command_free_list(ExecCommand *c) {
248         ExecCommand *i;
249
250         while ((i = c)) {
251                 LIST_REMOVE(ExecCommand, command, c, i);
252
253                 free(i->path);
254                 strv_free(i->argv);
255                 free(i);
256         }
257 }
258
259 void exec_command_free_array(ExecCommand **c, unsigned n) {
260         unsigned i;
261
262         for (i = 0; i < n; i++) {
263                 exec_command_free_list(c[i]);
264                 c[i] = NULL;
265         }
266 }
267
268 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
269         assert(c);
270         assert(f);
271
272         if (!prefix)
273                 prefix = "";
274
275         fprintf(f,
276                 "%sUmask: %04o\n"
277                 "%sDirectory: %s\n"
278                 "%sNice: %i\n"
279                 "%sOOMAdjust: %i\n",
280                 prefix, c->umask,
281                 prefix, c->directory ? c->directory : "/",
282                 prefix, c->nice,
283                 prefix, c->oom_adjust);
284 }
285
286 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
287         assert(s);
288
289         s->pid = pid;
290         s->code = code;
291         s->status = status;
292         s->timestamp = now(CLOCK_REALTIME);
293 }
294
295 char *exec_command_line(ExecCommand *c) {
296         size_t k;
297         char *n, *p, **a;
298         bool first = true;
299
300         assert(c);
301         assert(c->argv);
302
303         k = 1;
304         STRV_FOREACH(a, c->argv)
305                 k += strlen(*a)+3;
306
307         if (!(n = new(char, k)))
308                 return NULL;
309
310         p = n;
311         STRV_FOREACH(a, c->argv) {
312
313                 if (!first)
314                         *(p++) = ' ';
315                 else
316                         first = false;
317
318                 if (strpbrk(*a, WHITESPACE)) {
319                         *(p++) = '\'';
320                         p = stpcpy(p, *a);
321                         *(p++) = '\'';
322                 } else
323                         p = stpcpy(p, *a);
324
325         }
326
327         *p = 0;
328
329         /* FIXME: this doesn't really handle arguments that have
330          * spaces and ticks in them */
331
332         return n;
333 }
334
335 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
336         char *cmd;
337
338         assert(c);
339         assert(f);
340
341         if (!prefix)
342                 prefix = "";
343
344         cmd = exec_command_line(c);
345
346         fprintf(f,
347                 "%sCommand Line: %s\n",
348                 prefix, cmd ? cmd : strerror(ENOMEM));
349
350         free(cmd);
351 }
352
353 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
354         assert(f);
355
356         if (!prefix)
357                 prefix = "";
358
359         LIST_FOREACH(command, c, c)
360                 exec_command_dump(c, f, prefix);
361 }