]>
git.ipfire.org Git - people/ms/systemd.git/blob - execute.c
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 static int flags_fds(int fds
[], unsigned n_fds
) {
116 /* Drops O_NONBLOCK and FD_CLOEXEC from the file flags */
118 for (i
= 0; i
< n_fds
; i
++) {
121 if ((flags
= fcntl(fds
[i
], F_GETFL
, 0)) < 0)
124 /* Since we are at it, let's make sure that nobody
125 * forgot setting O_NONBLOCK for all our fds */
127 if (fcntl(fds
[i
], F_SETFL
, flags
&~O_NONBLOCK
) < 0)
130 if ((flags
= fcntl(fds
[i
], F_GETFD
, 0)) < 0)
133 /* Also make sure nobody forgot O_CLOEXEC for all our
135 if (fcntl(fds
[i
], F_SETFD
, flags
&~FD_CLOEXEC
) < 0)
142 int exec_spawn(const ExecCommand
*command
, const ExecContext
*context
, int *fds
, unsigned n_fds
, pid_t
*ret
) {
148 assert(fds
|| n_fds
<= 0);
150 log_debug("about to execute %s", command
->path
);
152 if ((pid
= fork()) < 0)
156 char **e
, **f
= NULL
;
163 if (sigemptyset(&ss
) < 0 ||
164 sigprocmask(SIG_SETMASK
, &ss
, NULL
) < 0) {
165 r
= EXIT_SIGNAL_MASK
;
169 umask(context
->umask
);
171 if (chdir(context
->directory
? context
->directory
: "/") < 0) {
176 snprintf(t
, sizeof(t
), "%i", context
->oom_adjust
);
179 if (write_one_line_file("/proc/self/oom_adj", t
) < 0) {
184 if (setpriority(PRIO_PROCESS
, 0, context
->nice
) < 0) {
189 if (close_fds(fds
, n_fds
) < 0 ||
190 shift_fds(fds
, n_fds
) < 0 ||
191 flags_fds(fds
, n_fds
) < 0) {
196 for (i
= 0; i
< RLIMIT_NLIMITS
; i
++) {
197 if (!context
->rlimit
[i
])
200 if (setrlimit(i
, context
->rlimit
[i
]) < 0) {
208 char *listen_env
[3] = {
214 snprintf(a
, sizeof(a
), "LISTEN_PID=%llu", (unsigned long long) getpid());
215 snprintf(b
, sizeof(b
), "LISTEN_FDS=%u", n_fds
);
220 if (context
->environment
) {
221 if (!(f
= strv_merge(listen_env
, context
->environment
))) {
230 e
= context
->environment
;
232 execve(command
->path
, command
->argv
, e
);
241 log_debug("executed %s as %llu", command
->path
, (unsigned long long) pid
);
247 void exec_context_init(ExecContext
*c
) {
251 cap_clear(c
->capabilities
);
256 void exec_context_done(ExecContext
*c
) {
261 strv_free(c
->environment
);
262 c
->environment
= NULL
;
264 for (l
= 0; l
< ELEMENTSOF(c
->rlimit
); l
++) {
278 strv_free(c
->supplementary_groups
);
279 c
->supplementary_groups
= NULL
;
282 void exec_command_free_list(ExecCommand
*c
) {
286 LIST_REMOVE(ExecCommand
, command
, c
, i
);
294 void exec_command_free_array(ExecCommand
**c
, unsigned n
) {
297 for (i
= 0; i
< n
; i
++) {
298 exec_command_free_list(c
[i
]);
303 void exec_context_dump(ExecContext
*c
, FILE* f
, const char *prefix
) {
316 prefix
, c
->directory
? c
->directory
: "/",
318 prefix
, c
->oom_adjust
);
321 void exec_status_fill(ExecStatus
*s
, pid_t pid
, int code
, int status
) {
327 s
->timestamp
= now(CLOCK_REALTIME
);
330 char *exec_command_line(ExecCommand
*c
) {
339 STRV_FOREACH(a
, c
->argv
)
342 if (!(n
= new(char, k
)))
346 STRV_FOREACH(a
, c
->argv
) {
353 if (strpbrk(*a
, WHITESPACE
)) {
364 /* FIXME: this doesn't really handle arguments that have
365 * spaces and ticks in them */
370 void exec_command_dump(ExecCommand
*c
, FILE *f
, const char *prefix
) {
379 cmd
= exec_command_line(c
);
382 "%sCommand Line: %s\n",
383 prefix
, cmd
? cmd
: strerror(ENOMEM
));
388 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
) {
394 LIST_FOREACH(command
, c
, c
)
395 exec_command_dump(c
, f
, prefix
);