1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
17 static int close_fds(int except[], unsigned n_except) {
22 /* Modifies the fds array! (sorts it) */
24 if (!(d = opendir("/proc/self/fd")))
27 while ((de = readdir(d))) {
30 if (de->d_name[0] == '.')
33 if ((r = safe_atoi(de->d_name, &fd)) < 0)
47 for (i = 0; i < n_except; i++)
48 if (except[i] == fd) {
57 if ((r = close_nointr(fd)) < 0)
66 static int shift_fds(int fds[], unsigned n_fds) {
67 int start, restart_from;
80 for (i = start; i < (int) n_fds; i++) {
83 /* Already at right index? */
87 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
90 assert_se(close_nointr(fds[i]) == 0);
93 /* Hmm, the fd we wanted isn't free? Then
94 * let's remember that and try again from here*/
95 if (nfd != i+3 && restart_from < 0)
102 start = restart_from;
108 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
114 assert(fds || n_fds <= 0);
116 log_debug("about to execute %s", command->path);
118 if ((pid = fork()) < 0)
122 char **e, **f = NULL;
129 if (sigemptyset(&ss) < 0 ||
130 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
131 r = EXIT_SIGNAL_MASK;
135 umask(context->umask);
137 if (chdir(context->directory ? context->directory : "/") < 0) {
142 snprintf(t, sizeof(t), "%i", context->oom_adjust);
145 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
150 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
155 if (close_fds(fds, n_fds) < 0 ||
156 shift_fds(fds, n_fds) < 0) {
161 for (i = 0; i < RLIMIT_NLIMITS; i++) {
162 if (!context->rlimit[i])
165 if (setrlimit(i, context->rlimit[i]) < 0) {
173 char *listen_env[3] = {
179 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
180 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
185 if (context->environment) {
186 if (!(f = strv_merge(listen_env, context->environment))) {
195 e = context->environment;
197 execve(command->path, command->argv, e);
206 log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
212 void exec_context_init(ExecContext *c) {
216 cap_clear(c->capabilities);
221 void exec_context_done(ExecContext *c) {
226 strv_free(c->environment);
227 c->environment = NULL;
229 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
243 strv_free(c->supplementary_groups);
244 c->supplementary_groups = NULL;
247 void exec_command_free_list(ExecCommand *c) {
251 LIST_REMOVE(ExecCommand, command, c, i);
259 void exec_command_free_array(ExecCommand **c, unsigned n) {
262 for (i = 0; i < n; i++) {
263 exec_command_free_list(c[i]);
268 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
281 prefix, c->directory ? c->directory : "/",
283 prefix, c->oom_adjust);
286 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
292 s->timestamp = now(CLOCK_REALTIME);
295 char *exec_command_line(ExecCommand *c) {
304 STRV_FOREACH(a, c->argv)
307 if (!(n = new(char, k)))
311 STRV_FOREACH(a, c->argv) {
318 if (strpbrk(*a, WHITESPACE)) {
329 /* FIXME: this doesn't really handle arguments that have
330 * spaces and ticks in them */
335 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
344 cmd = exec_command_line(c);
347 "%sCommand Line: %s\n",
348 prefix, cmd ? cmd : strerror(ENOMEM));
353 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
359 LIST_FOREACH(command, c, c)
360 exec_command_dump(c, f, prefix);