1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
15 static int close_fds(int except[], unsigned n_except) {
20 /* Modifies the fds array! (sorts it) */
22 if (!(d = opendir("/proc/self/fd")))
25 while ((de = readdir(d))) {
28 if (de->d_name[0] == '.')
31 if ((r = safe_atoi(de->d_name, &fd)) < 0)
45 for (i = 0; i < n_except; i++)
46 if (except[i] == fd) {
55 if ((r = close_nointr(fd)) < 0)
64 static int shift_fds(int fds[], unsigned n_fds) {
65 int start, restart_from;
78 for (i = start; i < (int) n_fds; i++) {
81 /* Already at right index? */
85 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
88 assert_se(close_nointr(fds[i]));
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)
100 start = restart_from;
106 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
112 assert(fds || n_fds <= 0);
114 if ((pid = fork()) < 0)
118 char **e, **f = NULL;
123 umask(context->umask);
125 if (chdir(context->directory ? context->directory : "/") < 0) {
130 snprintf(t, sizeof(t), "%i", context->oom_adjust);
133 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
138 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
143 if (close_fds(fds, n_fds) < 0 ||
144 shift_fds(fds, n_fds) < 0) {
149 for (i = 0; i < RLIMIT_NLIMITS; i++) {
150 if (!context->rlimit[i])
153 if (setrlimit(i, context->rlimit[i]) < 0) {
161 char *listen_env[3] = {
167 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
168 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
173 if (context->environment) {
174 if (!(f = strv_merge(listen_env, context->environment))) {
183 e = context->environment;
185 execve(command->path, command->argv, e);
197 void exec_context_init(ExecContext *c) {
201 cap_clear(c->capabilities);
206 void exec_context_done(ExecContext *c) {
211 strv_free(c->environment);
212 c->environment = NULL;
214 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
228 strv_free(c->supplementary_groups);
229 c->supplementary_groups = NULL;
232 void exec_command_free_list(ExecCommand *c) {
236 LIST_REMOVE(ExecCommand, command, c, i);
244 void exec_command_free_array(ExecCommand **c, unsigned n) {
247 for (i = 0; i < n; i++) {
248 exec_command_free_list(c[i]);
253 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
266 prefix, c->directory ? c->directory : "/",
268 prefix, c->oom_adjust);
271 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
277 s->timestamp = now(CLOCK_REALTIME);
280 char *exec_command_line(ExecCommand *c) {
289 STRV_FOREACH(a, c->argv)
292 if (!(n = new(char, k)))
296 STRV_FOREACH(a, c->argv) {
303 if (strpbrk(*a, WHITESPACE)) {
314 /* FIXME: this doesn't really handle arguments that have
315 * spaces and ticks in them */
320 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
329 cmd = exec_command_line(c);
332 "%sCommand Line: %s\n",
333 prefix, cmd ? cmd : strerror(ENOMEM));
338 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
344 LIST_FOREACH(command, c, c)
345 exec_command_dump(c, f, prefix);