2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-06-27 18:12:07 +02:00
|
|
|
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-12-15 05:12:40 +01:00
|
|
|
#include "conf-files.h"
|
2016-12-09 10:04:30 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "dropin.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "escape.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "fileio-label.h"
|
2017-02-02 18:17:20 +01:00
|
|
|
#include "fs-util.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "hashmap.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "mkdir.h"
|
2015-10-26 18:59:36 +01:00
|
|
|
#include "path-util.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "set.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "strv.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "unit-name.h"
|
2014-06-27 18:12:07 +02:00
|
|
|
|
2014-07-01 00:41:17 +02:00
|
|
|
int drop_in_file(const char *dir, const char *unit, unsigned level,
|
2014-06-27 18:12:07 +02:00
|
|
|
const char *name, char **_p, char **_q) {
|
|
|
|
|
2017-02-09 20:07:00 +01:00
|
|
|
char prefix[DECIMAL_STR_MAX(unsigned)];
|
2014-06-27 18:12:07 +02:00
|
|
|
_cleanup_free_ char *b = NULL;
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
assert(unit);
|
|
|
|
assert(name);
|
|
|
|
assert(_p);
|
|
|
|
assert(_q);
|
|
|
|
|
2014-07-01 00:41:17 +02:00
|
|
|
sprintf(prefix, "%u", level);
|
|
|
|
|
2014-06-27 18:12:07 +02:00
|
|
|
b = xescape(name, "/.");
|
|
|
|
if (!b)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-12-12 02:32:33 +01:00
|
|
|
if (!filename_is_valid(b))
|
2014-06-27 18:12:07 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
p = strjoin(dir, "/", unit, ".d");
|
2014-06-27 18:12:07 +02:00
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
q = strjoin(p, "/", prefix, "-", b, ".conf");
|
2014-06-27 18:12:07 +02:00
|
|
|
if (!q) {
|
|
|
|
free(p);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_p = p;
|
|
|
|
*_q = q;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-01 00:41:17 +02:00
|
|
|
int write_drop_in(const char *dir, const char *unit, unsigned level,
|
2014-06-27 18:12:07 +02:00
|
|
|
const char *name, const char *data) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *p = NULL, *q = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(dir);
|
|
|
|
assert(unit);
|
|
|
|
assert(name);
|
|
|
|
assert(data);
|
|
|
|
|
2014-07-01 00:41:17 +02:00
|
|
|
r = drop_in_file(dir, unit, level, name, &p, &q);
|
2014-06-27 18:12:07 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-09-29 22:39:49 +02:00
|
|
|
(void) mkdir_p(p, 0755);
|
2014-06-27 18:12:07 +02:00
|
|
|
return write_string_file_atomic_label(q, data);
|
|
|
|
}
|
|
|
|
|
2014-07-01 00:41:17 +02:00
|
|
|
int write_drop_in_format(const char *dir, const char *unit, unsigned level,
|
2014-06-27 18:12:07 +02:00
|
|
|
const char *name, const char *format, ...) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(dir);
|
|
|
|
assert(unit);
|
|
|
|
assert(name);
|
|
|
|
assert(format);
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
r = vasprintf(&p, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-07-01 00:41:17 +02:00
|
|
|
return write_drop_in(dir, unit, level, name, p);
|
2014-06-27 18:12:07 +02:00
|
|
|
}
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2017-02-05 03:32:08 +01:00
|
|
|
static int unit_file_find_dir(
|
2017-02-02 18:17:20 +01:00
|
|
|
const char *original_root,
|
2017-02-05 03:32:08 +01:00
|
|
|
const char *path,
|
|
|
|
char ***dirs) {
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2017-02-02 18:17:20 +01:00
|
|
|
_cleanup_free_ char *chased = NULL;
|
2014-12-15 05:12:40 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(path);
|
|
|
|
|
2017-02-02 18:17:20 +01:00
|
|
|
r = chase_symlinks(path, original_root, 0, &chased);
|
2018-03-21 19:08:46 +01:00
|
|
|
if (r == -ENOENT) /* Ignore -ENOENT, after all most units won't have a drop-in dir. */
|
2017-02-09 20:07:00 +01:00
|
|
|
return 0;
|
2018-03-21 19:08:46 +01:00
|
|
|
if (r == -ENAMETOOLONG) {
|
|
|
|
/* Also, ignore -ENAMETOOLONG but log about it. After all, users are not even able to create the
|
|
|
|
* drop-in dir in such case. This mostly happens for device units with an overly long /sys path. */
|
|
|
|
log_debug_errno(r, "Path '%s' too long, couldn't canonicalize, ignoring.", path);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-02 18:17:20 +01:00
|
|
|
if (r < 0)
|
2018-03-21 19:08:46 +01:00
|
|
|
return log_warning_errno(r, "Failed to canonicalize path '%s': %m", path);
|
2017-02-02 18:17:20 +01:00
|
|
|
|
2017-02-05 03:32:08 +01:00
|
|
|
r = strv_push(dirs, chased);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2017-02-05 03:32:08 +01:00
|
|
|
chased = NULL;
|
2014-12-15 05:12:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-05 03:32:08 +01:00
|
|
|
static int unit_file_find_dirs(
|
2017-02-02 18:17:20 +01:00
|
|
|
const char *original_root,
|
2015-04-30 20:21:00 +02:00
|
|
|
Set *unit_path_cache,
|
2014-12-15 05:12:40 +01:00
|
|
|
const char *unit_path,
|
|
|
|
const char *name,
|
|
|
|
const char *suffix,
|
2017-02-05 03:32:08 +01:00
|
|
|
char ***dirs) {
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2018-02-23 12:32:23 +01:00
|
|
|
_cleanup_free_ char *prefix = NULL, *instance = NULL, *built = NULL;
|
|
|
|
bool is_instance, chopped;
|
|
|
|
const char *dash;
|
|
|
|
UnitType type;
|
2017-02-09 20:08:44 +01:00
|
|
|
char *path;
|
2018-02-23 12:32:23 +01:00
|
|
|
size_t n;
|
2015-04-30 20:21:00 +02:00
|
|
|
int r;
|
2014-12-15 05:12:40 +01:00
|
|
|
|
|
|
|
assert(unit_path);
|
|
|
|
assert(name);
|
|
|
|
assert(suffix);
|
|
|
|
|
2017-02-09 20:08:44 +01:00
|
|
|
path = strjoina(unit_path, "/", name, suffix);
|
2017-02-05 03:32:08 +01:00
|
|
|
if (!unit_path_cache || set_get(unit_path_cache, path)) {
|
|
|
|
r = unit_file_find_dir(original_root, path, dirs);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2018-02-23 12:32:23 +01:00
|
|
|
is_instance = unit_name_is_valid(name, UNIT_NAME_INSTANCE);
|
|
|
|
if (is_instance) { /* Also try the template dir */
|
2017-02-09 20:08:58 +01:00
|
|
|
_cleanup_free_ char *template = NULL;
|
|
|
|
|
2015-04-30 20:21:00 +02:00
|
|
|
r = unit_name_template(name, &template);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to generate template from unit name: %m");
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2018-02-23 12:32:23 +01:00
|
|
|
r = unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-15 05:12:40 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 12:32:23 +01:00
|
|
|
/* Let's see if there's a "-" prefix for this unit name. If so, let's invoke ourselves for it. This will then
|
|
|
|
* recursively do the same for all our prefixes. i.e. this means given "foo-bar-waldo.service" we'll also
|
|
|
|
* search "foo-bar-.service" and "foo-.service".
|
|
|
|
*
|
|
|
|
* Note the order in which we do it: we traverse up adding drop-ins on each step. This means the more specific
|
|
|
|
* drop-ins may override the more generic drop-ins, which is the intended behaviour. */
|
|
|
|
|
|
|
|
r = unit_name_to_prefix(name, &prefix);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to derive unit name prefix from unit name: %m");
|
|
|
|
|
|
|
|
chopped = false;
|
|
|
|
for (;;) {
|
|
|
|
dash = strrchr(prefix, '-');
|
|
|
|
if (!dash) /* No dash? if so we are done */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
n = (size_t) (dash - prefix);
|
|
|
|
if (n == 0) /* Leading dash? If so, we are done */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (prefix[n+1] != 0 || chopped) {
|
|
|
|
prefix[n+1] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trailing dash? If so, chop it off and try again, but not more than once. */
|
|
|
|
prefix[n] = 0;
|
|
|
|
chopped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unit_prefix_is_valid(prefix))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
type = unit_name_to_type(name);
|
|
|
|
if (type < 0) {
|
2018-06-05 20:18:47 +02:00
|
|
|
log_error("Failed to to derive unit type from unit name: %s", name);
|
2018-02-23 12:32:23 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_instance) {
|
|
|
|
r = unit_name_to_instance(name, &instance);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to derive unit name instance from unit name: %m");
|
|
|
|
}
|
|
|
|
|
|
|
|
r = unit_name_build_from_type(prefix, instance, type, &built);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to build prefix unit name: %m");
|
|
|
|
|
|
|
|
return unit_file_find_dirs(original_root, unit_path_cache, unit_path, built, suffix, dirs);
|
2014-12-15 05:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int unit_file_find_dropin_paths(
|
2017-02-02 18:17:20 +01:00
|
|
|
const char *original_root,
|
2014-12-15 05:12:40 +01:00
|
|
|
char **lookup_path,
|
|
|
|
Set *unit_path_cache,
|
2017-02-05 02:50:44 +01:00
|
|
|
const char *dir_suffix,
|
|
|
|
const char *file_suffix,
|
2014-12-15 05:12:40 +01:00
|
|
|
Set *names,
|
2017-02-09 20:10:03 +01:00
|
|
|
char ***ret) {
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2017-07-22 14:54:27 +02:00
|
|
|
_cleanup_strv_free_ char **dirs = NULL;
|
2017-02-05 03:32:08 +01:00
|
|
|
char *t, **p;
|
2018-02-23 12:32:23 +01:00
|
|
|
Iterator i;
|
2014-12-15 05:12:40 +01:00
|
|
|
int r;
|
|
|
|
|
2017-02-09 20:10:03 +01:00
|
|
|
assert(ret);
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2017-02-05 03:32:08 +01:00
|
|
|
SET_FOREACH(t, names, i)
|
2014-12-15 05:12:40 +01:00
|
|
|
STRV_FOREACH(p, lookup_path)
|
2018-02-23 12:32:23 +01:00
|
|
|
(void) unit_file_find_dirs(original_root, unit_path_cache, *p, t, dir_suffix, &dirs);
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2017-02-09 20:10:03 +01:00
|
|
|
if (strv_isempty(dirs)) {
|
|
|
|
*ret = NULL;
|
2014-12-15 05:12:40 +01:00
|
|
|
return 0;
|
2017-02-09 20:10:03 +01:00
|
|
|
}
|
2014-12-15 05:12:40 +01:00
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
r = conf_files_list_strv(ret, file_suffix, NULL, 0, (const char**) dirs);
|
2014-12-15 05:12:40 +01:00
|
|
|
if (r < 0)
|
2017-07-22 14:54:27 +02:00
|
|
|
return log_warning_errno(r, "Failed to create the list of configuration files: %m");
|
2017-02-09 20:10:03 +01:00
|
|
|
|
2014-12-15 05:12:40 +01:00
|
|
|
return 1;
|
|
|
|
}
|