1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "conf-files.h"
13 #include "exec-util.h"
18 #include "process-util.h"
19 #include "rlimit-util.h"
20 #include "serialize.h"
22 #include "signal-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
26 #include "terminal-util.h"
29 /* Put this test here for a lack of better place */
30 assert_cc(EAGAIN
== EWOULDBLOCK
);
32 static int do_spawn(const char *path
, char *argv
[], int stdout_fd
, pid_t
*pid
) {
37 if (null_or_empty_path(path
)) {
38 log_debug("%s is empty (a mask).", path
);
42 r
= safe_fork("(direxec)", FORK_DEATHSIG
|FORK_LOG
, &_pid
);
49 r
= rearrange_stdio(STDIN_FILENO
, stdout_fd
, STDERR_FILENO
);
54 (void) rlimit_nofile_safe();
57 _argv
[0] = (char*) path
;
61 argv
[0] = (char*) path
;
64 log_error_errno(errno
, "Failed to execute %s: %m", path
);
72 static int do_execute(
75 gather_stdout_callback_t
const callbacks
[_STDOUT_CONSUME_MAX
],
76 void* const callback_args
[_STDOUT_CONSUME_MAX
],
81 _cleanup_hashmap_free_free_ Hashmap
*pids
= NULL
;
82 _cleanup_strv_free_
char **paths
= NULL
;
86 /* We fork this all off from a child process so that we can somewhat cleanly make
87 * use of SIGALRM to set a time limit.
89 * If callbacks is nonnull, execution is serial. Otherwise, we default to parallel.
92 r
= conf_files_list_strv(&paths
, NULL
, NULL
, CONF_FILES_EXECUTABLE
|CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char* const*) directories
);
94 return log_error_errno(r
, "Failed to enumerate executables: %m");
97 pids
= hashmap_new(NULL
);
102 /* Abort execution of this process after the timout. We simply rely on SIGALRM as
103 * default action terminating the process, and turn on alarm(). */
105 if (timeout
!= USEC_INFINITY
)
106 alarm(DIV_ROUND_UP(timeout
, USEC_PER_SEC
));
108 STRV_FOREACH(e
, envp
)
110 return log_error_errno(errno
, "Failed to set environment variable: %m");
112 STRV_FOREACH(path
, paths
) {
113 _cleanup_free_
char *t
= NULL
;
114 _cleanup_close_
int fd
= -1;
122 fd
= open_serialization_fd(basename(*path
));
124 return log_error_errno(fd
, "Failed to open serialization file: %m");
127 r
= do_spawn(t
, argv
, fd
, &pid
);
132 r
= hashmap_put(pids
, PID_TO_PTR(pid
), t
);
137 r
= wait_for_terminate_and_check(t
, pid
, WAIT_LOG
);
141 if (lseek(fd
, 0, SEEK_SET
) < 0)
142 return log_error_errno(errno
, "Failed to seek on serialization fd: %m");
144 r
= callbacks
[STDOUT_GENERATE
](fd
, callback_args
[STDOUT_GENERATE
]);
147 return log_error_errno(r
, "Failed to process output from %s: %m", *path
);
152 r
= callbacks
[STDOUT_COLLECT
](output_fd
, callback_args
[STDOUT_COLLECT
]);
154 return log_error_errno(r
, "Callback two failed: %m");
157 while (!hashmap_isempty(pids
)) {
158 _cleanup_free_
char *t
= NULL
;
161 pid
= PTR_TO_PID(hashmap_first_key(pids
));
164 t
= hashmap_remove(pids
, PID_TO_PTR(pid
));
167 (void) wait_for_terminate_and_check(t
, pid
, WAIT_LOG
);
173 int execute_directories(
174 const char* const* directories
,
176 gather_stdout_callback_t
const callbacks
[_STDOUT_CONSUME_MAX
],
177 void* const callback_args
[_STDOUT_CONSUME_MAX
],
181 char **dirs
= (char**) directories
;
182 _cleanup_close_
int fd
= -1;
186 assert(!strv_isempty(dirs
));
188 name
= basename(dirs
[0]);
189 assert(!isempty(name
));
192 assert(callback_args
);
193 assert(callbacks
[STDOUT_GENERATE
]);
194 assert(callbacks
[STDOUT_COLLECT
]);
195 assert(callbacks
[STDOUT_CONSUME
]);
197 fd
= open_serialization_fd(name
);
199 return log_error_errno(fd
, "Failed to open serialization file: %m");
202 /* Executes all binaries in the directories serially or in parallel and waits for
203 * them to finish. Optionally a timeout is applied. If a file with the same name
204 * exists in more than one directory, the earliest one wins. */
206 r
= safe_fork("(sd-executor)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
, NULL
);
210 r
= do_execute(dirs
, timeout
, callbacks
, callback_args
, fd
, argv
, envp
);
211 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
217 if (lseek(fd
, 0, SEEK_SET
) < 0)
218 return log_error_errno(errno
, "Failed to rewind serialization fd: %m");
220 r
= callbacks
[STDOUT_CONSUME
](fd
, callback_args
[STDOUT_CONSUME
]);
223 return log_error_errno(r
, "Failed to parse returned data: %m");
227 static int gather_environment_generate(int fd
, void *arg
) {
228 char ***env
= arg
, **x
, **y
;
229 _cleanup_fclose_
FILE *f
= NULL
;
230 _cleanup_strv_free_
char **new = NULL
;
233 /* Read a series of VAR=value assignments from fd, use them to update the list of
234 * variables in env. Also update the exported environment.
236 * fd is always consumed, even on error.
247 r
= load_env_file_pairs(f
, NULL
, &new);
251 STRV_FOREACH_PAIR(x
, y
, new) {
254 if (!env_name_is_valid(*x
)) {
255 log_warning("Invalid variable assignment \"%s=...\", ignoring.", *x
);
259 p
= strjoin(*x
, "=", *y
);
263 r
= strv_env_replace(env
, p
);
267 if (setenv(*x
, *y
, true) < 0)
274 static int gather_environment_collect(int fd
, void *arg
) {
275 _cleanup_fclose_
FILE *f
= NULL
;
279 /* Write out a series of env=cescape(VAR=value) assignments to fd. */
289 r
= serialize_strv(f
, "env", *env
);
293 r
= fflush_and_check(f
);
300 static int gather_environment_consume(int fd
, void *arg
) {
301 _cleanup_fclose_
FILE *f
= NULL
;
305 /* Read a series of env=cescape(VAR=value) assignments from fd into env. */
309 f
= fdopen(fd
, "re");
316 _cleanup_free_
char *line
= NULL
;
320 k
= read_line(f
, LONG_LINE_MAX
, &line
);
326 v
= startswith(line
, "env=");
328 log_debug("Serialization line \"%s\" unexpectedly didn't start with \"env=\".", line
);
335 k
= deserialize_environment(v
, env
);
337 log_debug_errno(k
, "Invalid serialization line \"%s\": %m", line
);
347 const gather_stdout_callback_t gather_environment
[] = {
348 gather_environment_generate
,
349 gather_environment_collect
,
350 gather_environment_consume
,