Files
systemd/src/shared/dropin.c
T

212 lines
5.5 KiB
C
Raw Normal View History

/***
This file is part of systemd.
Copyright 2014 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
2015-12-03 21:13:37 +01:00
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "alloc-util.h"
2014-12-14 23:12:40 -05:00
#include "conf-files.h"
#include "dirent-util.h"
#include "dropin.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio-label.h"
#include "fs-util.h"
2015-12-03 21:13:37 +01:00
#include "hashmap.h"
#include "log.h"
#include "macro.h"
#include "mkdir.h"
#include "path-util.h"
2015-12-03 21:13:37 +01:00
#include "set.h"
#include "string-util.h"
#include "strv.h"
2015-12-03 21:13:37 +01:00
#include "unit-name.h"
2014-06-30 18:41:17 -04:00
int drop_in_file(const char *dir, const char *unit, unsigned level,
const char *name, char **_p, char **_q) {
char prefix[DECIMAL_STR_MAX(unsigned)];
_cleanup_free_ char *b = NULL;
char *p, *q;
assert(unit);
assert(name);
assert(_p);
assert(_q);
2014-06-30 18:41:17 -04:00
sprintf(prefix, "%u", level);
b = xescape(name, "/.");
if (!b)
return -ENOMEM;
if (!filename_is_valid(b))
return -EINVAL;
2016-10-23 11:43:27 -04:00
p = strjoin(dir, "/", unit, ".d");
if (!p)
return -ENOMEM;
2016-10-23 11:43:27 -04:00
q = strjoin(p, "/", prefix, "-", b, ".conf");
if (!q) {
free(p);
return -ENOMEM;
}
*_p = p;
*_q = q;
return 0;
}
2014-06-30 18:41:17 -04:00
int write_drop_in(const char *dir, const char *unit, unsigned level,
const char *name, const char *data) {
_cleanup_free_ char *p = NULL, *q = NULL;
int r;
assert(dir);
assert(unit);
assert(name);
assert(data);
2014-06-30 18:41:17 -04:00
r = drop_in_file(dir, unit, level, name, &p, &q);
if (r < 0)
return r;
2015-09-29 22:39:49 +02:00
(void) mkdir_p(p, 0755);
return write_string_file_atomic_label(q, data);
}
2014-06-30 18:41:17 -04:00
int write_drop_in_format(const char *dir, const char *unit, unsigned level,
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-06-30 18:41:17 -04:00
return write_drop_in(dir, unit, level, name, p);
}
2014-12-14 23:12:40 -05:00
2017-02-04 21:32:08 -05:00
static int unit_file_find_dir(
const char *original_root,
2017-02-04 21:32:08 -05:00
const char *path,
char ***dirs) {
2014-12-14 23:12:40 -05:00
_cleanup_free_ char *chased = NULL;
2014-12-14 23:12:40 -05:00
int r;
assert(path);
r = chase_symlinks(path, original_root, 0, &chased);
if (r == -ENOENT) /* Ignore -ENOENT, after all most units won't have a drop-in dir */
return 0;
if (r < 0)
return log_full_errno(LOG_WARNING, r, "Failed to canonicalize path %s: %m", path);
2017-02-04 21:32:08 -05:00
r = strv_push(dirs, chased);
if (r < 0)
return log_oom();
2014-12-14 23:12:40 -05:00
2017-02-04 21:32:08 -05:00
chased = NULL;
2014-12-14 23:12:40 -05:00
return 0;
}
2017-02-04 21:32:08 -05:00
static int unit_file_find_dirs(
const char *original_root,
Set *unit_path_cache,
2014-12-14 23:12:40 -05:00
const char *unit_path,
const char *name,
const char *suffix,
2017-02-04 21:32:08 -05:00
char ***dirs) {
2014-12-14 23:12:40 -05:00
char *path;
int r;
2014-12-14 23:12:40 -05:00
assert(unit_path);
assert(name);
assert(suffix);
path = strjoina(unit_path, "/", name, suffix);
2014-12-14 23:12:40 -05:00
2017-02-04 21:32:08 -05: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-14 23:12:40 -05:00
if (unit_name_is_valid(name, UNIT_NAME_INSTANCE)) {
2014-12-14 23:12:40 -05:00
/* Also try the template dir */
2017-02-09 20:08:58 +01:00
_cleanup_free_ char *template = NULL;
r = unit_name_template(name, &template);
if (r < 0)
return log_error_errno(r, "Failed to generate template from unit name: %m");
2014-12-14 23:12:40 -05:00
2017-02-09 20:08:58 +01:00
return unit_file_find_dirs(original_root, unit_path_cache, unit_path, template, suffix, dirs);
2014-12-14 23:12:40 -05:00
}
return 0;
}
int unit_file_find_dropin_paths(
const char *original_root,
2014-12-14 23:12:40 -05:00
char **lookup_path,
Set *unit_path_cache,
const char *dir_suffix,
const char *file_suffix,
2014-12-14 23:12:40 -05:00
Set *names,
char ***paths) {
2017-02-04 21:32:08 -05:00
_cleanup_strv_free_ char **dirs = NULL, **ans = NULL;
2014-12-14 23:12:40 -05:00
Iterator i;
2017-02-04 21:32:08 -05:00
char *t, **p;
2014-12-14 23:12:40 -05:00
int r;
assert(paths);
2017-02-04 21:32:08 -05:00
SET_FOREACH(t, names, i)
2014-12-14 23:12:40 -05:00
STRV_FOREACH(p, lookup_path)
2017-02-04 21:32:08 -05:00
unit_file_find_dirs(original_root, unit_path_cache, *p, t, dir_suffix, &dirs);
2014-12-14 23:12:40 -05:00
2017-02-04 21:32:08 -05:00
if (strv_isempty(dirs))
2014-12-14 23:12:40 -05:00
return 0;
2017-02-04 21:32:08 -05:00
r = conf_files_list_strv(&ans, file_suffix, NULL, (const char**) dirs);
2014-12-14 23:12:40 -05:00
if (r < 0)
return log_warning_errno(r, "Failed to sort the list of configuration files: %m");
2014-12-14 23:12:40 -05:00
*paths = ans;
ans = NULL;
return 1;
}