]>
git.ipfire.org Git - thirdparty/systemd.git/blob - execute.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
10 #include <sys/socket.h>
19 static int close_fds(int except
[], unsigned n_except
) {
24 /* Modifies the fds array! (sorts it) */
26 if (!(d
= opendir("/proc/self/fd")))
29 while ((de
= readdir(d
))) {
32 if (de
->d_name
[0] == '.')
35 if ((r
= safe_atoi(de
->d_name
, &fd
)) < 0)
49 for (i
= 0; i
< n_except
; i
++)
50 if (except
[i
] == fd
) {
59 if ((r
= close_nointr(fd
)) < 0)
68 static int shift_fds(int fds
[], unsigned n_fds
) {
69 int start
, restart_from
;
82 for (i
= start
; i
< (int) n_fds
; i
++) {
85 /* Already at right index? */
89 if ((nfd
= fcntl(fds
[i
], F_DUPFD
, i
+3)) < 0)
92 assert_se(close_nointr(fds
[i
]) == 0);
95 /* Hmm, the fd we wanted isn't free? Then
96 * let's remember that and try again from here*/
97 if (nfd
!= i
+3 && restart_from
< 0)
101 if (restart_from
< 0)
104 start
= restart_from
;
110 static int flags_fds(int fds
[], unsigned n_fds
) {
118 /* Drops O_NONBLOCK and FD_CLOEXEC from the file flags */
120 for (i
= 0; i
< n_fds
; i
++) {
123 if ((flags
= fcntl(fds
[i
], F_GETFL
, 0)) < 0)
126 /* Since we are at it, let's make sure that nobody
127 * forgot setting O_NONBLOCK for all our fds */
129 if (fcntl(fds
[i
], F_SETFL
, flags
&~O_NONBLOCK
) < 0)
132 if ((flags
= fcntl(fds
[i
], F_GETFD
, 0)) < 0)
135 /* Also make sure nobody forgot O_CLOEXEC for all our
137 if (fcntl(fds
[i
], F_SETFD
, flags
&~FD_CLOEXEC
) < 0)
144 static int replace_null_fd(int fd
, int flags
) {
150 if ((nfd
= open("/dev/null", flags
|O_NOCTTY
)) < 0)
154 close_nointr_nofail(nfd
);
161 static int setup_output(const ExecContext
*context
, const char *ident
) {
166 switch (context
->output
) {
173 if ((r
= replace_null_fd(STDIN_FILENO
, O_RDONLY
)) < 0 ||
174 (r
= replace_null_fd(STDOUT_FILENO
, O_WRONLY
)) < 0 ||
175 (r
= replace_null_fd(STDERR_FILENO
, O_WRONLY
)) < 0)
186 struct sockaddr_un un
;
189 if ((r
= replace_null_fd(STDIN_FILENO
, O_RDONLY
)) < 0)
192 close_nointr(STDOUT_FILENO
);
193 close_nointr(STDERR_FILENO
);
195 if ((fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
198 if (fd
!= STDOUT_FILENO
) {
199 close_nointr_nofail(fd
);
204 sa
.sa
.sa_family
= AF_UNIX
;
205 strncpy(sa
.un
.sun_path
+1, LOGGER_SOCKET
, sizeof(sa
.un
.sun_path
)-1);
207 if (connect(fd
, &sa
.sa
, sizeof(sa
)) < 0) {
208 close_nointr_nofail(fd
);
212 if (shutdown(fd
, SHUT_RD
) < 0) {
213 close_nointr_nofail(fd
);
217 if ((fd
= dup(fd
)) < 0) {
218 close_nointr_nofail(fd
);
222 if (fd
!= STDERR_FILENO
) {
223 close_nointr_nofail(fd
);
227 /* We speak a very simple protocol between log server
228 * and client: one line for the log destination (kmsg
229 * or syslog), followed by the priority field,
230 * followed by the process name. Since we replaced
231 * stdin/stderr we simple use stdio to write to
232 * it. Note that we use stderr, to minimize buffer
233 * flushing issues. */
239 context
->output
== EXEC_KERNEL
? "kmsg" : "syslog",
240 context
->syslog_priority
,
241 context
->syslog_identifier
? context
->syslog_identifier
: ident
);
247 assert_not_reached("Unknown logging type");
250 int exec_spawn(const ExecCommand
*command
, const ExecContext
*context
, int *fds
, unsigned n_fds
, pid_t
*ret
) {
256 assert(fds
|| n_fds
<= 0);
258 log_debug("about to execute %s", command
->path
);
260 if ((pid
= fork()) < 0)
264 char **e
, **f
= NULL
;
270 if (sigemptyset(&ss
) < 0 ||
271 sigprocmask(SIG_SETMASK
, &ss
, NULL
) < 0) {
272 r
= EXIT_SIGNAL_MASK
;
276 umask(context
->umask
);
278 if (chdir(context
->directory
? context
->directory
: "/") < 0) {
283 if (setup_output(context
, file_name_from_path(command
->path
)) < 0) {
288 if (context
->oom_adjust_set
) {
291 snprintf(t
, sizeof(t
), "%i", context
->oom_adjust
);
294 if (write_one_line_file("/proc/self/oom_adj", t
) < 0) {
300 if (context
->nice_set
)
301 if (setpriority(PRIO_PROCESS
, 0, context
->nice
) < 0) {
306 if (close_fds(fds
, n_fds
) < 0 ||
307 shift_fds(fds
, n_fds
) < 0 ||
308 flags_fds(fds
, n_fds
) < 0) {
313 for (i
= 0; i
< RLIMIT_NLIMITS
; i
++) {
314 if (!context
->rlimit
[i
])
317 if (setrlimit(i
, context
->rlimit
[i
]) < 0) {
325 char *listen_env
[3] = {
331 snprintf(a
, sizeof(a
), "LISTEN_PID=%llu", (unsigned long long) getpid());
332 snprintf(b
, sizeof(b
), "LISTEN_FDS=%u", n_fds
);
337 if (context
->environment
) {
338 if (!(f
= strv_merge(listen_env
, context
->environment
))) {
347 e
= context
->environment
;
349 execve(command
->path
, command
->argv
, e
);
358 log_debug("executed %s as %llu", command
->path
, (unsigned long long) pid
);
364 void exec_context_init(ExecContext
*c
) {
368 cap_clear(c
->capabilities
);
373 c
->syslog_priority
= LOG_DAEMON
|LOG_INFO
;
376 void exec_context_done(ExecContext
*c
) {
381 strv_free(c
->environment
);
382 c
->environment
= NULL
;
384 for (l
= 0; l
< ELEMENTSOF(c
->rlimit
); l
++) {
392 free(c
->syslog_identifier
);
393 c
->syslog_identifier
= NULL
;
401 strv_free(c
->supplementary_groups
);
402 c
->supplementary_groups
= NULL
;
405 void exec_command_free_list(ExecCommand
*c
) {
409 LIST_REMOVE(ExecCommand
, command
, c
, i
);
417 void exec_command_free_array(ExecCommand
**c
, unsigned n
) {
420 for (i
= 0; i
< n
; i
++) {
421 exec_command_free_list(c
[i
]);
426 void exec_context_dump(ExecContext
*c
, FILE* f
, const char *prefix
) {
437 prefix
, c
->directory
? c
->directory
: "/");
444 if (c
->oom_adjust_set
)
447 prefix
, c
->oom_adjust
);
450 void exec_status_fill(ExecStatus
*s
, pid_t pid
, int code
, int status
) {
456 s
->timestamp
= now(CLOCK_REALTIME
);
459 char *exec_command_line(ExecCommand
*c
) {
468 STRV_FOREACH(a
, c
->argv
)
471 if (!(n
= new(char, k
)))
475 STRV_FOREACH(a
, c
->argv
) {
482 if (strpbrk(*a
, WHITESPACE
)) {
493 /* FIXME: this doesn't really handle arguments that have
494 * spaces and ticks in them */
499 void exec_command_dump(ExecCommand
*c
, FILE *f
, const char *prefix
) {
508 cmd
= exec_command_line(c
);
511 "%sCommand Line: %s\n",
512 prefix
, cmd
? cmd
: strerror(ENOMEM
));
517 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
) {
523 LIST_FOREACH(command
, c
, c
)
524 exec_command_dump(c
, f
, prefix
);