]>
git.ipfire.org Git - people/ms/systemd.git/blob - execute.c
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
]) == 0);
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
);
197 log_debug("executed %s as %llu", command
->path
, (unsigned long long) pid
);
203 void exec_context_init(ExecContext
*c
) {
207 cap_clear(c
->capabilities
);
212 void exec_context_done(ExecContext
*c
) {
217 strv_free(c
->environment
);
218 c
->environment
= NULL
;
220 for (l
= 0; l
< ELEMENTSOF(c
->rlimit
); l
++) {
234 strv_free(c
->supplementary_groups
);
235 c
->supplementary_groups
= NULL
;
238 void exec_command_free_list(ExecCommand
*c
) {
242 LIST_REMOVE(ExecCommand
, command
, c
, i
);
250 void exec_command_free_array(ExecCommand
**c
, unsigned n
) {
253 for (i
= 0; i
< n
; i
++) {
254 exec_command_free_list(c
[i
]);
259 void exec_context_dump(ExecContext
*c
, FILE* f
, const char *prefix
) {
272 prefix
, c
->directory
? c
->directory
: "/",
274 prefix
, c
->oom_adjust
);
277 void exec_status_fill(ExecStatus
*s
, pid_t pid
, int code
, int status
) {
283 s
->timestamp
= now(CLOCK_REALTIME
);
286 char *exec_command_line(ExecCommand
*c
) {
295 STRV_FOREACH(a
, c
->argv
)
298 if (!(n
= new(char, k
)))
302 STRV_FOREACH(a
, c
->argv
) {
309 if (strpbrk(*a
, WHITESPACE
)) {
320 /* FIXME: this doesn't really handle arguments that have
321 * spaces and ticks in them */
326 void exec_command_dump(ExecCommand
*c
, FILE *f
, const char *prefix
) {
335 cmd
= exec_command_line(c
);
338 "%sCommand Line: %s\n",
339 prefix
, cmd
? cmd
: strerror(ENOMEM
));
344 void exec_command_dump_list(ExecCommand
*c
, FILE *f
, const char *prefix
) {
350 LIST_FOREACH(command
, c
, c
)
351 exec_command_dump(c
, f
, prefix
);