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