chiark / gitweb /
exit cleanly on SIGINT
[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]));
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         *ret = pid;
197         return 0;
198 }
199
200 void exec_context_init(ExecContext *c) {
201         assert(c);
202
203         c->umask = 0002;
204         cap_clear(c->capabilities);
205         c->oom_adjust = 0;
206         c->nice = 0;
207 }
208
209 void exec_context_done(ExecContext *c) {
210         unsigned l;
211
212         assert(c);
213
214         strv_free(c->environment);
215         c->environment = NULL;
216
217         for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
218                 free(c->rlimit[l]);
219                 c->rlimit[l] = NULL;
220         }
221
222         free(c->directory);
223         c->directory = NULL;
224
225         free(c->user);
226         c->user = NULL;
227
228         free(c->group);
229         c->group = NULL;
230
231         strv_free(c->supplementary_groups);
232         c->supplementary_groups = NULL;
233 }
234
235 void exec_command_free_list(ExecCommand *c) {
236         ExecCommand *i;
237
238         while ((i = c)) {
239                 LIST_REMOVE(ExecCommand, command, c, i);
240
241                 free(i->path);
242                 strv_free(i->argv);
243                 free(i);
244         }
245 }
246
247 void exec_command_free_array(ExecCommand **c, unsigned n) {
248         unsigned i;
249
250         for (i = 0; i < n; i++) {
251                 exec_command_free_list(c[i]);
252                 c[i] = NULL;
253         }
254 }
255
256 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
257         assert(c);
258         assert(f);
259
260         if (!prefix)
261                 prefix = "";
262
263         fprintf(f,
264                 "%sUmask: %04o\n"
265                 "%sDirectory: %s\n"
266                 "%sNice: %i\n"
267                 "%sOOMAdjust: %i\n",
268                 prefix, c->umask,
269                 prefix, c->directory ? c->directory : "/",
270                 prefix, c->nice,
271                 prefix, c->oom_adjust);
272 }
273
274 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
275         assert(s);
276
277         s->pid = pid;
278         s->code = code;
279         s->status = status;
280         s->timestamp = now(CLOCK_REALTIME);
281 }
282
283 char *exec_command_line(ExecCommand *c) {
284         size_t k;
285         char *n, *p, **a;
286         bool first = true;
287
288         assert(c);
289         assert(c->argv);
290
291         k = 1;
292         STRV_FOREACH(a, c->argv)
293                 k += strlen(*a)+3;
294
295         if (!(n = new(char, k)))
296                 return NULL;
297
298         p = n;
299         STRV_FOREACH(a, c->argv) {
300
301                 if (!first)
302                         *(p++) = ' ';
303                 else
304                         first = false;
305
306                 if (strpbrk(*a, WHITESPACE)) {
307                         *(p++) = '\'';
308                         p = stpcpy(p, *a);
309                         *(p++) = '\'';
310                 } else
311                         p = stpcpy(p, *a);
312
313         }
314
315         *p = 0;
316
317         /* FIXME: this doesn't really handle arguments that have
318          * spaces and ticks in them */
319
320         return n;
321 }
322
323 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
324         char *cmd;
325
326         assert(c);
327         assert(f);
328
329         if (!prefix)
330                 prefix = "";
331
332         cmd = exec_command_line(c);
333
334         fprintf(f,
335                 "%sCommand Line: %s\n",
336                 prefix, cmd ? cmd : strerror(ENOMEM));
337
338         free(cmd);
339 }
340
341 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
342         assert(f);
343
344         if (!prefix)
345                 prefix = "";
346
347         LIST_FOREACH(command, c, c)
348                 exec_command_dump(c, f, prefix);
349 }