1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
10 #include <sys/socket.h>
20 static int close_fds(int except[], unsigned n_except) {
25 /* Modifies the fds array! (sorts it) */
27 if (!(d = opendir("/proc/self/fd")))
30 while ((de = readdir(d))) {
33 if (de->d_name[0] == '.')
36 if ((r = safe_atoi(de->d_name, &fd)) < 0)
50 for (i = 0; i < n_except; i++)
51 if (except[i] == fd) {
60 if ((r = close_nointr(fd)) < 0)
69 static int shift_fds(int fds[], unsigned n_fds) {
70 int start, restart_from;
83 for (i = start; i < (int) n_fds; i++) {
86 /* Already at right index? */
90 if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
93 assert_se(close_nointr(fds[i]) == 0);
96 /* Hmm, the fd we wanted isn't free? Then
97 * let's remember that and try again from here*/
98 if (nfd != i+3 && restart_from < 0)
102 if (restart_from < 0)
105 start = restart_from;
111 static int flags_fds(int fds[], unsigned n_fds) {
119 /* Drops O_NONBLOCK and FD_CLOEXEC from the file flags */
121 for (i = 0; i < n_fds; i++) {
124 if ((flags = fcntl(fds[i], F_GETFL, 0)) < 0)
127 /* Since we are at it, let's make sure that nobody
128 * forgot setting O_NONBLOCK for all our fds */
130 if (fcntl(fds[i], F_SETFL, flags &~O_NONBLOCK) < 0)
133 if ((flags = fcntl(fds[i], F_GETFD, 0)) < 0)
136 /* Also make sure nobody forgot O_CLOEXEC for all our
138 if (fcntl(fds[i], F_SETFD, flags &~FD_CLOEXEC) < 0)
145 static int replace_null_fd(int fd, int flags) {
151 if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0)
155 close_nointr_nofail(nfd);
162 static int setup_output(const ExecContext *context, const char *ident) {
167 switch (context->output) {
174 if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0 ||
175 (r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 ||
176 (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0)
187 struct sockaddr_un un;
190 if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0)
193 close_nointr(STDOUT_FILENO);
194 close_nointr(STDERR_FILENO);
196 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
199 if (fd != STDOUT_FILENO) {
200 close_nointr_nofail(fd);
205 sa.sa.sa_family = AF_UNIX;
206 strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
208 if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
209 close_nointr_nofail(fd);
213 if (shutdown(fd, SHUT_RD) < 0) {
214 close_nointr_nofail(fd);
218 if ((fd = dup(fd)) < 0) {
219 close_nointr_nofail(fd);
223 if (fd != STDERR_FILENO) {
224 close_nointr_nofail(fd);
228 /* We speak a very simple protocol between log server
229 * and client: one line for the log destination (kmsg
230 * or syslog), followed by the priority field,
231 * followed by the process name. Since we replaced
232 * stdin/stderr we simple use stdio to write to
233 * it. Note that we use stderr, to minimize buffer
234 * flushing issues. */
240 context->output == EXEC_KERNEL ? "kmsg" : "syslog",
241 context->syslog_priority,
242 context->syslog_identifier ? context->syslog_identifier : ident);
248 assert_not_reached("Unknown logging type");
251 int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
257 assert(fds || n_fds <= 0);
259 log_debug("about to execute %s", command->path);
261 if ((pid = fork()) < 0)
265 char **e, **f = NULL;
271 if (sigemptyset(&ss) < 0 ||
272 sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
273 r = EXIT_SIGNAL_MASK;
277 if (setpgid(0, 0) < 0) {
282 umask(context->umask);
284 if (setup_output(context, file_name_from_path(command->path)) < 0) {
289 if (context->oom_adjust_set) {
292 snprintf(t, sizeof(t), "%i", context->oom_adjust);
295 if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
301 if (context->root_directory)
302 if (chroot(context->root_directory) < 0) {
307 if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
312 if (context->nice_set)
313 if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
318 if (context->ioprio_set)
319 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
324 if (close_fds(fds, n_fds) < 0 ||
325 shift_fds(fds, n_fds) < 0 ||
326 flags_fds(fds, n_fds) < 0) {
331 for (i = 0; i < RLIMIT_NLIMITS; i++) {
332 if (!context->rlimit[i])
335 if (setrlimit(i, context->rlimit[i]) < 0) {
343 char *listen_env[3] = {
349 snprintf(a, sizeof(a), "LISTEN_PID=%llu", (unsigned long long) getpid());
350 snprintf(b, sizeof(b), "LISTEN_FDS=%u", n_fds);
355 if (context->environment) {
356 if (!(f = strv_merge(listen_env, context->environment))) {
365 e = context->environment;
367 execve(command->path, command->argv, e);
376 log_debug("executed %s as %llu", command->path, (unsigned long long) pid);
382 void exec_context_init(ExecContext *c) {
386 cap_clear(c->capabilities);
387 c->capabilities_set = false;
389 c->oom_adjust_set = false;
392 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0);
393 c->ioprio_set = false;
396 c->syslog_priority = LOG_DAEMON|LOG_INFO;
399 void exec_context_done(ExecContext *c) {
404 strv_free(c->environment);
405 c->environment = NULL;
407 for (l = 0; l < ELEMENTSOF(c->rlimit); l++) {
412 free(c->working_directory);
413 c->working_directory = NULL;
414 free(c->root_directory);
415 c->root_directory = NULL;
417 free(c->syslog_identifier);
418 c->syslog_identifier = NULL;
426 strv_free(c->supplementary_groups);
427 c->supplementary_groups = NULL;
430 void exec_command_free_list(ExecCommand *c) {
434 LIST_REMOVE(ExecCommand, command, c, i);
442 void exec_command_free_array(ExecCommand **c, unsigned n) {
445 for (i = 0; i < n; i++) {
446 exec_command_free_list(c[i]);
451 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
453 static const char * const table[] = {
454 [IOPRIO_CLASS_NONE] = "none",
455 [IOPRIO_CLASS_RT] = "realtime",
456 [IOPRIO_CLASS_BE] = "best-effort",
457 [IOPRIO_CLASS_IDLE] = "idle"
468 "%sWorking Directory: %s\n"
469 "%sRoot Directory: %s\n",
471 prefix, c->working_directory ? c->working_directory : "/",
472 prefix, c->root_directory ? c->root_directory : "/");
479 if (c->oom_adjust_set)
482 prefix, c->oom_adjust);
486 "%sIOSchedulingClass: %s\n"
487 "%sIOPriority: %i\n",
488 prefix, table[IOPRIO_PRIO_CLASS(c->ioprio)],
489 prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
492 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
498 s->timestamp = now(CLOCK_REALTIME);
501 char *exec_command_line(ExecCommand *c) {
510 STRV_FOREACH(a, c->argv)
513 if (!(n = new(char, k)))
517 STRV_FOREACH(a, c->argv) {
524 if (strpbrk(*a, WHITESPACE)) {
535 /* FIXME: this doesn't really handle arguments that have
536 * spaces and ticks in them */
541 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
550 cmd = exec_command_line(c);
553 "%sCommand Line: %s\n",
554 prefix, cmd ? cmd : strerror(ENOMEM));
559 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
565 LIST_FOREACH(command, c, c)
566 exec_command_dump(c, f, prefix);