1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
16 static int close_fds(int except[], unsigned n_except) {
21 /* Modifies the fds array! (sorts it) */
23 if (!(d = opendir("/proc/self/fd")))
26 while ((de = readdir(d))) {
29 if (de->d_name[0] == '.')
32 if ((r = safe_atoi(de->d_name, &fd)) < 0)
46 for (i = 0; i < n_except; i++)
47 if (except[i] == fd) {
56 if ((r = close_nointr(fd)) < 0)
65 static int shift_fds(int fds[], unsigned n_fds) {
66 int start, restart_from;
79 for (i = start; i < (int) n_fds; i++) {
82 /* Already at right index? */
86 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
89 assert_se(close_nointr(fds[i]));
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)
101 start = restart_from;
107 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
113 assert(fds || n_fds <= 0);
115 log_debug("About to execute %s", command->path);
117 if ((pid = fork()) < 0)
121 char **e, **f = NULL;
126 umask(context->umask);
128 if (chdir(context->directory ? context->directory : "/") < 0) {
133 snprintf(t, sizeof(t), "%i", context->oom_adjust);
136 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
141 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
146 if (close_fds(fds, n_fds) < 0 ||
147 shift_fds(fds, n_fds) < 0) {
152 for (i = 0; i < RLIMIT_NLIMITS; i++) {
153 if (!context->rlimit[i])
156 if (setrlimit(i, context->rlimit[i]) < 0) {
164 char *listen_env[3] = {
170 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
171 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
176 if (context->environment) {
177 if (!(f = strv_merge(listen_env, context->environment))) {
186 e = context->environment;
188 execve(command->path, command->argv, e);
200 void exec_context_init(ExecContext *c) {
204 cap_clear(c->capabilities);
209 void exec_context_done(ExecContext *c) {
214 strv_free(c->environment);
215 c->environment = NULL;
217 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
231 strv_free(c->supplementary_groups);
232 c->supplementary_groups = NULL;
235 void exec_command_free_list(ExecCommand *c) {
239 LIST_REMOVE(ExecCommand, command, c, i);
247 void exec_command_free_array(ExecCommand **c, unsigned n) {
250 for (i = 0; i < n; i++) {
251 exec_command_free_list(c[i]);
256 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
269 prefix, c->directory ? c->directory : "/",
271 prefix, c->oom_adjust);
274 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
280 s->timestamp = now(CLOCK_REALTIME);
283 char *exec_command_line(ExecCommand *c) {
292 STRV_FOREACH(a, c->argv)
295 if (!(n = new(char, k)))
299 STRV_FOREACH(a, c->argv) {
306 if (strpbrk(*a, WHITESPACE)) {
317 /* FIXME: this doesn't really handle arguments that have
318 * spaces and ticks in them */
323 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
332 cmd = exec_command_line(c);
335 "%sCommand Line: %s\n",
336 prefix, cmd ? cmd : strerror(ENOMEM));
341 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
347 LIST_FOREACH(command, c, c)
348 exec_command_dump(c, f, prefix);