chiark / gitweb /
properly implement target unit
[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 static int flags_fds(int fds[], unsigned n_fds) {
109         unsigned i;
110
111         if (n_fds <= 0)
112                 return 0;
113
114         assert(fds);
115
116         /* Drops O_NONBLOCK and FD_CLOEXEC from the file flags */
117
118         for (i = 0; i < n_fds; i++) {
119                 int flags;
120
121                 if ((flags = fcntl(fds[i], F_GETFL, 0)) < 0)
122                         return -errno;
123
124                 /* Since we are at it, let's make sure that nobody
125                  * forgot setting O_NONBLOCK for all our fds */
126
127                 if (fcntl(fds[i], F_SETFL, flags &~O_NONBLOCK) < 0)
128                         return -errno;
129
130                 if ((flags = fcntl(fds[i], F_GETFD, 0)) < 0)
131                         return -errno;
132
133                 /* Also make sure nobody forgot O_CLOEXEC for all our
134                  * fds */
135                 if (fcntl(fds[i], F_SETFD, flags &~FD_CLOEXEC) < 0)
136                         return -errno;
137         }
138
139         return 0;
140 }
141
142 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
143         pid_t pid;
144
145         assert(command);
146         assert(context);
147         assert(ret);
148         assert(fds || n_fds <= 0);
149
150         log_debug("about to execute %s", command->path);
151
152         if ((pid = fork()) < 0)
153                 return -errno;
154
155         if (pid == 0) {
156                 char **e, **f = NULL;
157                 int i, r;
158                 char t[16];
159                 sigset_t ss;
160
161                 /* child */
162
163                 if (sigemptyset(&ss) < 0 ||
164                     sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
165                         r = EXIT_SIGNAL_MASK;
166                         goto fail;
167                 }
168
169                 umask(context->umask);
170
171                 if (chdir(context->directory ? context->directory : "/") < 0) {
172                         r = EXIT_CHDIR;
173                         goto fail;
174                 }
175
176                 snprintf(t, sizeof(t), "%i", context->oom_adjust);
177                 char_array_0(t);
178
179                 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
180                         r = EXIT_OOM_ADJUST;
181                         goto fail;
182                 }
183
184                 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
185                         r = EXIT_NICE;
186                         goto fail;
187                 }
188
189                 if (close_fds(fds, n_fds) < 0 ||
190                     shift_fds(fds, n_fds) < 0 ||
191                     flags_fds(fds, n_fds) < 0) {
192                         r = EXIT_FDS;
193                         goto fail;
194                 }
195
196                 for (i = 0; i < RLIMIT_NLIMITS; i++) {
197                         if (!context->rlimit[i])
198                                 continue;
199
200                         if (setrlimit(i, context->rlimit[i]) < 0) {
201                                 r = EXIT_LIMITS;
202                                 goto fail;
203                         }
204                 }
205
206                 if (n_fds > 0) {
207                         char a[64], b[64];
208                         char *listen_env[3] = {
209                                 a,
210                                 b,
211                                 NULL
212                         };
213
214                         snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
215                         snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
216
217                         a[sizeof(a)-1] = 0;
218                         b[sizeof(b)-1] = 0;
219
220                         if (context->environment) {
221                                 if (!(f = strv_merge(listen_env, context->environment))) {
222                                         r = EXIT_MEMORY;
223                                         goto fail;
224                                 }
225                                 e = f;
226                         } else
227                                 e = listen_env;
228
229                 } else
230                         e = context->environment;
231
232                 execve(command->path, command->argv, e);
233                 r = EXIT_EXEC;
234
235         fail:
236                 strv_free(f);
237                 _exit(r);
238         }
239
240
241         log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
242
243         *ret = pid;
244         return 0;
245 }
246
247 void exec_context_init(ExecContext *c) {
248         assert(c);
249
250         c->umask = 0002;
251         cap_clear(c->capabilities);
252         c->oom_adjust = 0;
253         c->nice = 0;
254 }
255
256 void exec_context_done(ExecContext *c) {
257         unsigned l;
258
259         assert(c);
260
261         strv_free(c->environment);
262         c->environment = NULL;
263
264         for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
265                 free(c->rlimit[l]);
266                 c->rlimit[l] = NULL;
267         }
268
269         free(c->directory);
270         c->directory = NULL;
271
272         free(c->user);
273         c->user = NULL;
274
275         free(c->group);
276         c->group = NULL;
277
278         strv_free(c->supplementary_groups);
279         c->supplementary_groups = NULL;
280 }
281
282 void exec_command_free_list(ExecCommand *c) {
283         ExecCommand *i;
284
285         while ((i = c)) {
286                 LIST_REMOVE(ExecCommand, command, c, i);
287
288                 free(i->path);
289                 strv_free(i->argv);
290                 free(i);
291         }
292 }
293
294 void exec_command_free_array(ExecCommand **c, unsigned n) {
295         unsigned i;
296
297         for (i = 0; i < n; i++) {
298                 exec_command_free_list(c[i]);
299                 c[i] = NULL;
300         }
301 }
302
303 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
304         assert(c);
305         assert(f);
306
307         if (!prefix)
308                 prefix = "";
309
310         fprintf(f,
311                 "%sUmask: %04o\n"
312                 "%sDirectory: %s\n"
313                 "%sNice: %i\n"
314                 "%sOOMAdjust: %i\n",
315                 prefix, c->umask,
316                 prefix, c->directory ? c->directory : "/",
317                 prefix, c->nice,
318                 prefix, c->oom_adjust);
319 }
320
321 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
322         assert(s);
323
324         s->pid = pid;
325         s->code = code;
326         s->status = status;
327         s->timestamp = now(CLOCK_REALTIME);
328 }
329
330 char *exec_command_line(ExecCommand *c) {
331         size_t k;
332         char *n, *p, **a;
333         bool first = true;
334
335         assert(c);
336         assert(c->argv);
337
338         k = 1;
339         STRV_FOREACH(a, c->argv)
340                 k += strlen(*a)+3;
341
342         if (!(n = new(char, k)))
343                 return NULL;
344
345         p = n;
346         STRV_FOREACH(a, c->argv) {
347
348                 if (!first)
349                         *(p++) = ' ';
350                 else
351                         first = false;
352
353                 if (strpbrk(*a, WHITESPACE)) {
354                         *(p++) = '\'';
355                         p = stpcpy(p, *a);
356                         *(p++) = '\'';
357                 } else
358                         p = stpcpy(p, *a);
359
360         }
361
362         *p = 0;
363
364         /* FIXME: this doesn't really handle arguments that have
365          * spaces and ticks in them */
366
367         return n;
368 }
369
370 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
371         char *cmd;
372
373         assert(c);
374         assert(f);
375
376         if (!prefix)
377                 prefix = "";
378
379         cmd = exec_command_line(c);
380
381         fprintf(f,
382                 "%sCommand Line: %s\n",
383                 prefix, cmd ? cmd : strerror(ENOMEM));
384
385         free(cmd);
386 }
387
388 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
389         assert(f);
390
391         if (!prefix)
392                 prefix = "";
393
394         LIST_FOREACH(command, c, c)
395                 exec_command_dump(c, f, prefix);
396 }