2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2017-01-22 18:35:08 +01:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2017-01-22 21:22:37 +01:00
|
|
|
#include <stdio.h>
|
2017-01-22 18:35:08 +01:00
|
|
|
|
|
|
|
#include "alloc-util.h"
|
2017-01-22 20:44:34 +01:00
|
|
|
#include "conf-files.h"
|
2017-01-22 21:22:37 +01:00
|
|
|
#include "env-util.h"
|
2017-01-22 18:35:08 +01:00
|
|
|
#include "exec-util.h"
|
2017-01-22 21:22:37 +01:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "fileio.h"
|
2017-01-22 18:35:08 +01:00
|
|
|
#include "hashmap.h"
|
|
|
|
#include "macro.h"
|
|
|
|
#include "process-util.h"
|
2018-10-17 20:40:09 +02:00
|
|
|
#include "serialize.h"
|
2017-01-22 18:35:08 +01:00
|
|
|
#include "set.h"
|
|
|
|
#include "signal-util.h"
|
|
|
|
#include "stat-util.h"
|
|
|
|
#include "string-util.h"
|
|
|
|
#include "strv.h"
|
2017-01-22 21:22:37 +01:00
|
|
|
#include "terminal-util.h"
|
2017-01-22 18:35:08 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
/* Put this test here for a lack of better place */
|
|
|
|
assert_cc(EAGAIN == EWOULDBLOCK);
|
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) {
|
|
|
|
|
2017-01-22 19:06:27 +01:00
|
|
|
pid_t _pid;
|
2017-12-22 13:08:14 +01:00
|
|
|
int r;
|
2017-01-22 19:06:27 +01:00
|
|
|
|
|
|
|
if (null_or_empty_path(path)) {
|
|
|
|
log_debug("%s is empty (a mask).", path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-27 21:49:19 +01:00
|
|
|
r = safe_fork("(direxec)", FORK_DEATHSIG|FORK_LOG, &_pid);
|
2017-12-22 13:08:14 +01:00
|
|
|
if (r < 0)
|
2017-12-27 21:49:19 +01:00
|
|
|
return r;
|
2017-12-22 13:08:14 +01:00
|
|
|
if (r == 0) {
|
2017-01-22 19:06:27 +01:00
|
|
|
char *_argv[2];
|
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
if (stdout_fd >= 0) {
|
2018-02-28 23:32:49 +01:00
|
|
|
r = rearrange_stdio(STDIN_FILENO, stdout_fd, STDERR_FILENO);
|
|
|
|
if (r < 0)
|
2018-02-25 21:07:18 +01:00
|
|
|
_exit(EXIT_FAILURE);
|
2017-01-22 21:22:37 +01:00
|
|
|
}
|
|
|
|
|
2017-01-22 19:06:27 +01:00
|
|
|
if (!argv) {
|
|
|
|
_argv[0] = (char*) path;
|
|
|
|
_argv[1] = NULL;
|
|
|
|
argv = _argv;
|
|
|
|
} else
|
|
|
|
argv[0] = (char*) path;
|
|
|
|
|
|
|
|
execv(path, argv);
|
|
|
|
log_error_errno(errno, "Failed to execute %s: %m", path);
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pid = _pid;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
static int do_execute(
|
|
|
|
char **directories,
|
|
|
|
usec_t timeout,
|
|
|
|
gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
|
|
|
|
void* const callback_args[_STDOUT_CONSUME_MAX],
|
|
|
|
int output_fd,
|
2018-09-12 19:19:13 +02:00
|
|
|
char *argv[],
|
|
|
|
char *envp[]) {
|
2017-01-22 21:22:37 +01:00
|
|
|
|
2017-01-22 18:35:08 +01:00
|
|
|
_cleanup_hashmap_free_free_ Hashmap *pids = NULL;
|
2017-01-22 20:44:34 +01:00
|
|
|
_cleanup_strv_free_ char **paths = NULL;
|
2018-09-12 19:19:13 +02:00
|
|
|
char **path, **e;
|
2017-01-22 20:44:34 +01:00
|
|
|
int r;
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
/* We fork this all off from a child process so that we can somewhat cleanly make
|
|
|
|
* use of SIGALRM to set a time limit.
|
|
|
|
*
|
|
|
|
* If callbacks is nonnull, execution is serial. Otherwise, we default to parallel.
|
|
|
|
*/
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
r = conf_files_list_strv(&paths, NULL, NULL, CONF_FILES_EXECUTABLE|CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, (const char* const*) directories);
|
2017-01-22 20:44:34 +01:00
|
|
|
if (r < 0)
|
2018-09-25 12:03:06 +02:00
|
|
|
return log_error_errno(r, "Failed to enumerate executables: %m");
|
2017-01-22 20:44:34 +01:00
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
if (!callbacks) {
|
|
|
|
pids = hashmap_new(NULL);
|
|
|
|
if (!pids)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Abort execution of this process after the timout. We simply rely on SIGALRM as
|
|
|
|
* default action terminating the process, and turn on alarm(). */
|
|
|
|
|
|
|
|
if (timeout != USEC_INFINITY)
|
2018-03-20 20:36:09 +01:00
|
|
|
alarm(DIV_ROUND_UP(timeout, USEC_PER_SEC));
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2018-09-12 19:19:13 +02:00
|
|
|
STRV_FOREACH(e, envp)
|
2018-10-13 13:12:32 +02:00
|
|
|
if (putenv(*e) != 0)
|
2018-09-25 12:02:26 +02:00
|
|
|
return log_error_errno(errno, "Failed to set environment variable: %m");
|
2018-09-12 19:19:13 +02:00
|
|
|
|
2017-01-22 20:44:34 +01:00
|
|
|
STRV_FOREACH(path, paths) {
|
|
|
|
_cleanup_free_ char *t = NULL;
|
2017-01-22 21:22:37 +01:00
|
|
|
_cleanup_close_ int fd = -1;
|
2017-01-22 20:44:34 +01:00
|
|
|
pid_t pid;
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2017-01-22 20:44:34 +01:00
|
|
|
t = strdup(*path);
|
|
|
|
if (!t)
|
|
|
|
return log_oom();
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
if (callbacks) {
|
|
|
|
fd = open_serialization_fd(basename(*path));
|
|
|
|
if (fd < 0)
|
|
|
|
return log_error_errno(fd, "Failed to open serialization file: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
r = do_spawn(t, argv, fd, &pid);
|
2017-01-22 20:44:34 +01:00
|
|
|
if (r <= 0)
|
|
|
|
continue;
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
if (pids) {
|
|
|
|
r = hashmap_put(pids, PID_TO_PTR(pid), t);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
t = NULL;
|
|
|
|
} else {
|
2017-12-28 00:51:19 +01:00
|
|
|
r = wait_for_terminate_and_check(t, pid, WAIT_LOG);
|
2017-01-22 21:22:37 +01:00
|
|
|
if (r < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to seek on serialization fd: %m");
|
|
|
|
|
|
|
|
r = callbacks[STDOUT_GENERATE](fd, callback_args[STDOUT_GENERATE]);
|
|
|
|
fd = -1;
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to process output from %s: %m", *path);
|
|
|
|
}
|
2017-01-22 18:35:08 +01:00
|
|
|
}
|
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
if (callbacks) {
|
|
|
|
r = callbacks[STDOUT_COLLECT](output_fd, callback_args[STDOUT_COLLECT]);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Callback two failed: %m");
|
|
|
|
}
|
2017-01-22 18:35:08 +01:00
|
|
|
|
|
|
|
while (!hashmap_isempty(pids)) {
|
2017-01-22 20:44:34 +01:00
|
|
|
_cleanup_free_ char *t = NULL;
|
2017-01-22 18:35:08 +01:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
pid = PTR_TO_PID(hashmap_first_key(pids));
|
|
|
|
assert(pid > 0);
|
|
|
|
|
2017-01-22 20:44:34 +01:00
|
|
|
t = hashmap_remove(pids, PID_TO_PTR(pid));
|
|
|
|
assert(t);
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2017-12-28 00:51:19 +01:00
|
|
|
(void) wait_for_terminate_and_check(t, pid, WAIT_LOG);
|
2017-01-22 18:35:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
int execute_directories(
|
|
|
|
const char* const* directories,
|
|
|
|
usec_t timeout,
|
|
|
|
gather_stdout_callback_t const callbacks[_STDOUT_CONSUME_MAX],
|
|
|
|
void* const callback_args[_STDOUT_CONSUME_MAX],
|
2018-09-12 19:19:13 +02:00
|
|
|
char *argv[],
|
|
|
|
char *envp[]) {
|
2017-01-22 21:22:37 +01:00
|
|
|
|
2017-01-22 18:35:08 +01:00
|
|
|
char **dirs = (char**) directories;
|
2017-01-22 21:22:37 +01:00
|
|
|
_cleanup_close_ int fd = -1;
|
2017-12-29 18:01:37 +01:00
|
|
|
char *name;
|
2017-01-22 21:22:37 +01:00
|
|
|
int r;
|
2017-01-22 18:35:08 +01:00
|
|
|
|
|
|
|
assert(!strv_isempty(dirs));
|
|
|
|
|
|
|
|
name = basename(dirs[0]);
|
|
|
|
assert(!isempty(name));
|
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
if (callbacks) {
|
|
|
|
assert(callback_args);
|
|
|
|
assert(callbacks[STDOUT_GENERATE]);
|
|
|
|
assert(callbacks[STDOUT_COLLECT]);
|
|
|
|
assert(callbacks[STDOUT_CONSUME]);
|
|
|
|
|
|
|
|
fd = open_serialization_fd(name);
|
|
|
|
if (fd < 0)
|
|
|
|
return log_error_errno(fd, "Failed to open serialization file: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Executes all binaries in the directories serially or in parallel and waits for
|
|
|
|
* them to finish. Optionally a timeout is applied. If a file with the same name
|
|
|
|
* exists in more than one directory, the earliest one wins. */
|
2017-01-22 18:35:08 +01:00
|
|
|
|
2017-12-29 18:01:37 +01:00
|
|
|
r = safe_fork("(sd-executor)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG|FORK_WAIT, NULL);
|
2017-12-22 13:08:14 +01:00
|
|
|
if (r < 0)
|
2017-12-27 21:49:19 +01:00
|
|
|
return r;
|
2017-12-22 13:08:14 +01:00
|
|
|
if (r == 0) {
|
2018-09-12 19:19:13 +02:00
|
|
|
r = do_execute(dirs, timeout, callbacks, callback_args, fd, argv, envp);
|
2017-01-22 18:35:08 +01:00
|
|
|
_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:22:37 +01:00
|
|
|
if (!callbacks)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to rewind serialization fd: %m");
|
|
|
|
|
|
|
|
r = callbacks[STDOUT_CONSUME](fd, callback_args[STDOUT_CONSUME]);
|
|
|
|
fd = -1;
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse returned data: %m");
|
|
|
|
return 0;
|
2017-01-22 18:35:08 +01:00
|
|
|
}
|
2017-02-11 03:49:01 +01:00
|
|
|
|
|
|
|
static int gather_environment_generate(int fd, void *arg) {
|
|
|
|
char ***env = arg, **x, **y;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2017-11-26 12:46:56 +01:00
|
|
|
_cleanup_strv_free_ char **new = NULL;
|
2017-02-11 03:49:01 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Read a series of VAR=value assignments from fd, use them to update the list of
|
|
|
|
* variables in env. Also update the exported environment.
|
|
|
|
*
|
|
|
|
* fd is always consumed, even on error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(env);
|
|
|
|
|
|
|
|
f = fdopen(fd, "r");
|
|
|
|
if (!f) {
|
|
|
|
safe_close(fd);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:04:47 +01:00
|
|
|
r = load_env_file_pairs(f, NULL, &new);
|
2017-02-11 03:49:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
STRV_FOREACH_PAIR(x, y, new) {
|
|
|
|
char *p;
|
|
|
|
|
2017-02-18 04:56:28 +01:00
|
|
|
if (!env_name_is_valid(*x)) {
|
|
|
|
log_warning("Invalid variable assignment \"%s=...\", ignoring.", *x);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-11 03:49:01 +01:00
|
|
|
p = strjoin(*x, "=", *y);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = strv_env_replace(env, p);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (setenv(*x, *y, true) < 0)
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gather_environment_collect(int fd, void *arg) {
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2018-10-17 20:14:51 +02:00
|
|
|
char ***env = arg;
|
2017-02-11 03:49:01 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
/* Write out a series of env=cescape(VAR=value) assignments to fd. */
|
|
|
|
|
|
|
|
assert(env);
|
|
|
|
|
|
|
|
f = fdopen(fd, "w");
|
|
|
|
if (!f) {
|
|
|
|
safe_close(fd);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:40:09 +02:00
|
|
|
r = serialize_strv(f, "env", *env);
|
2017-02-11 03:49:01 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-10-17 20:14:51 +02:00
|
|
|
r = fflush_and_check(f);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2017-02-11 03:49:01 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gather_environment_consume(int fd, void *arg) {
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2018-10-17 20:40:09 +02:00
|
|
|
char ***env = arg;
|
|
|
|
int r = 0;
|
2017-02-11 03:49:01 +01:00
|
|
|
|
|
|
|
/* Read a series of env=cescape(VAR=value) assignments from fd into env. */
|
|
|
|
|
|
|
|
assert(env);
|
|
|
|
|
2018-10-17 20:40:09 +02:00
|
|
|
f = fdopen(fd, "re");
|
2017-02-11 03:49:01 +01:00
|
|
|
if (!f) {
|
|
|
|
safe_close(fd);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:40:09 +02:00
|
|
|
for (;;) {
|
|
|
|
_cleanup_free_ char *line = NULL;
|
|
|
|
const char *v;
|
|
|
|
int k;
|
2017-02-11 03:49:01 +01:00
|
|
|
|
2018-10-17 20:40:09 +02:00
|
|
|
k = read_line(f, LONG_LINE_MAX, &line);
|
2017-02-11 03:49:01 +01:00
|
|
|
if (k < 0)
|
2018-10-17 20:40:09 +02:00
|
|
|
return k;
|
|
|
|
if (k == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
v = startswith(line, "env=");
|
|
|
|
if (!v) {
|
|
|
|
log_debug("Serialization line \"%s\" unexpectedly didn't start with \"env=\".", line);
|
|
|
|
if (r == 0)
|
|
|
|
r = -EINVAL;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = deserialize_environment(v, env);
|
|
|
|
if (k < 0) {
|
|
|
|
log_debug_errno(k, "Invalid serialization line \"%s\": %m", line);
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
r = k;
|
|
|
|
}
|
2017-02-11 03:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gather_stdout_callback_t gather_environment[] = {
|
|
|
|
gather_environment_generate,
|
|
|
|
gather_environment_collect,
|
|
|
|
gather_environment_consume,
|
|
|
|
};
|