1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "conf-files.h"
14 #include "exec-util.h"
19 #include "process-util.h"
20 #include "rlimit-util.h"
21 #include "serialize.h"
23 #include "signal-util.h"
24 #include "stat-util.h"
25 #include "string-util.h"
27 #include "terminal-util.h"
28 #include "tmpfile-util.h"
31 /* Put this test here for a lack of better place */
32 assert_cc(EAGAIN
== EWOULDBLOCK
);
34 static int do_spawn(const char *path
, char *argv
[], int stdout_fd
, pid_t
*pid
) {
39 if (null_or_empty_path(path
)) {
40 log_debug("%s is empty (a mask).", path
);
44 r
= safe_fork("(direxec)", FORK_DEATHSIG
|FORK_LOG
, &_pid
);
51 r
= rearrange_stdio(STDIN_FILENO
, stdout_fd
, STDERR_FILENO
);
56 (void) rlimit_nofile_safe();
59 _argv
[0] = (char*) path
;
63 argv
[0] = (char*) path
;
66 log_error_errno(errno
, "Failed to execute %s: %m", path
);
74 static int do_execute(
77 gather_stdout_callback_t
const callbacks
[_STDOUT_CONSUME_MAX
],
78 void* const callback_args
[_STDOUT_CONSUME_MAX
],
84 _cleanup_hashmap_free_free_ Hashmap
*pids
= NULL
;
85 _cleanup_strv_free_
char **paths
= NULL
;
88 bool parallel_execution
;
90 /* We fork this all off from a child process so that we can somewhat cleanly make
91 * use of SIGALRM to set a time limit.
93 * We attempt to perform parallel execution if configured by the user, however
94 * if `callbacks` is nonnull, execution must be serial.
96 parallel_execution
= FLAGS_SET(flags
, EXEC_DIR_PARALLEL
) && !callbacks
;
98 r
= conf_files_list_strv(&paths
, NULL
, NULL
, CONF_FILES_EXECUTABLE
|CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char* const*) directories
);
100 return log_error_errno(r
, "Failed to enumerate executables: %m");
102 if (parallel_execution
) {
103 pids
= hashmap_new(NULL
);
108 /* Abort execution of this process after the timeout. We simply rely on SIGALRM as
109 * default action terminating the process, and turn on alarm(). */
111 if (timeout
!= USEC_INFINITY
)
112 alarm(DIV_ROUND_UP(timeout
, USEC_PER_SEC
));
114 STRV_FOREACH(e
, envp
)
116 return log_error_errno(errno
, "Failed to set environment variable: %m");
118 STRV_FOREACH(path
, paths
) {
119 _cleanup_free_
char *t
= NULL
;
120 _cleanup_close_
int fd
= -1;
128 fd
= open_serialization_fd(basename(*path
));
130 return log_error_errno(fd
, "Failed to open serialization file: %m");
133 r
= do_spawn(t
, argv
, fd
, &pid
);
137 if (parallel_execution
) {
138 r
= hashmap_put(pids
, PID_TO_PTR(pid
), t
);
143 r
= wait_for_terminate_and_check(t
, pid
, WAIT_LOG
);
144 if (FLAGS_SET(flags
, EXEC_DIR_IGNORE_ERRORS
)) {
151 if (lseek(fd
, 0, SEEK_SET
) < 0)
152 return log_error_errno(errno
, "Failed to seek on serialization fd: %m");
154 r
= callbacks
[STDOUT_GENERATE
](fd
, callback_args
[STDOUT_GENERATE
]);
157 return log_error_errno(r
, "Failed to process output from %s: %m", *path
);
163 r
= callbacks
[STDOUT_COLLECT
](output_fd
, callback_args
[STDOUT_COLLECT
]);
165 return log_error_errno(r
, "Callback two failed: %m");
168 while (!hashmap_isempty(pids
)) {
169 _cleanup_free_
char *t
= NULL
;
172 pid
= PTR_TO_PID(hashmap_first_key(pids
));
175 t
= hashmap_remove(pids
, PID_TO_PTR(pid
));
178 r
= wait_for_terminate_and_check(t
, pid
, WAIT_LOG
);
179 if (!FLAGS_SET(flags
, EXEC_DIR_IGNORE_ERRORS
) && r
> 0)
186 int execute_directories(
187 const char* const* directories
,
189 gather_stdout_callback_t
const callbacks
[_STDOUT_CONSUME_MAX
],
190 void* const callback_args
[_STDOUT_CONSUME_MAX
],
193 ExecDirFlags flags
) {
195 char **dirs
= (char**) directories
;
196 _cleanup_close_
int fd
= -1;
201 assert(!strv_isempty(dirs
));
203 name
= basename(dirs
[0]);
204 assert(!isempty(name
));
207 assert(callback_args
);
208 assert(callbacks
[STDOUT_GENERATE
]);
209 assert(callbacks
[STDOUT_COLLECT
]);
210 assert(callbacks
[STDOUT_CONSUME
]);
212 fd
= open_serialization_fd(name
);
214 return log_error_errno(fd
, "Failed to open serialization file: %m");
217 /* Executes all binaries in the directories serially or in parallel and waits for
218 * them to finish. Optionally a timeout is applied. If a file with the same name
219 * exists in more than one directory, the earliest one wins. */
221 r
= safe_fork("(sd-executor)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &executor_pid
);
225 r
= do_execute(dirs
, timeout
, callbacks
, callback_args
, fd
, argv
, envp
, flags
);
226 _exit(r
< 0 ? EXIT_FAILURE
: r
);
229 r
= wait_for_terminate_and_check("(sd-executor)", executor_pid
, 0);
232 if (!FLAGS_SET(flags
, EXEC_DIR_IGNORE_ERRORS
) && r
> 0)
238 if (lseek(fd
, 0, SEEK_SET
) < 0)
239 return log_error_errno(errno
, "Failed to rewind serialization fd: %m");
241 r
= callbacks
[STDOUT_CONSUME
](fd
, callback_args
[STDOUT_CONSUME
]);
244 return log_error_errno(r
, "Failed to parse returned data: %m");
248 static int gather_environment_generate(int fd
, void *arg
) {
249 char ***env
= arg
, **x
, **y
;
250 _cleanup_fclose_
FILE *f
= NULL
;
251 _cleanup_strv_free_
char **new = NULL
;
254 /* Read a series of VAR=value assignments from fd, use them to update the list of
255 * variables in env. Also update the exported environment.
257 * fd is always consumed, even on error.
268 r
= load_env_file_pairs(f
, NULL
, &new);
272 STRV_FOREACH_PAIR(x
, y
, new) {
275 if (!env_name_is_valid(*x
)) {
276 log_warning("Invalid variable assignment \"%s=...\", ignoring.", *x
);
280 p
= strjoin(*x
, "=", *y
);
284 r
= strv_env_replace(env
, p
);
288 if (setenv(*x
, *y
, true) < 0)
295 static int gather_environment_collect(int fd
, void *arg
) {
296 _cleanup_fclose_
FILE *f
= NULL
;
300 /* Write out a series of env=cescape(VAR=value) assignments to fd. */
310 r
= serialize_strv(f
, "env", *env
);
314 r
= fflush_and_check(f
);
321 static int gather_environment_consume(int fd
, void *arg
) {
322 _cleanup_fclose_
FILE *f
= NULL
;
326 /* Read a series of env=cescape(VAR=value) assignments from fd into env. */
337 _cleanup_free_
char *line
= NULL
;
341 k
= read_line(f
, LONG_LINE_MAX
, &line
);
347 v
= startswith(line
, "env=");
349 log_debug("Serialization line \"%s\" unexpectedly didn't start with \"env=\".", line
);
356 k
= deserialize_environment(v
, env
);
358 log_debug_errno(k
, "Invalid serialization line \"%s\": %m", line
);
368 const gather_stdout_callback_t gather_environment
[] = {
369 gather_environment_generate
,
370 gather_environment_collect
,
371 gather_environment_consume
,