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