core: rework unit name validation and manipulation logic

A variety of changes:

- Make sure all our calls distuingish OOM from other errors if OOM is
  not the only error possible.

- Be much stricter when parsing escaped paths, do not accept trailing or
  leading escaped slashes.

- Change unit validation to take a bit mask for allowing plain names,
  instance names or template names or an combination thereof.

- Refuse manipulating invalid unit name
This commit is contained in:
Lennart Poettering
2015-05-05 15:06:42 -07:00
parent 6442185ab6
commit 7410616cd9
34 changed files with 1066 additions and 867 deletions
+11 -11
View File
@@ -50,13 +50,11 @@ static int add_symlink(const char *fservice, const char *tservice) {
r = symlink(from, to);
if (r < 0) {
/* In case console=hvc0 is passed this will very likely result in EEXIST */
if (errno == EEXIST)
/* In case console=hvc0 is passed this will very likely result in EEXIST */
return 0;
else {
log_error_errno(errno, "Failed to create symlink %s: %m", to);
return -errno;
}
return log_error_errno(errno, "Failed to create symlink %s: %m", to);
}
return 0;
@@ -64,28 +62,30 @@ static int add_symlink(const char *fservice, const char *tservice) {
static int add_serial_getty(const char *tty) {
_cleanup_free_ char *n = NULL;
int r;
assert(tty);
log_debug("Automatically adding serial getty for /dev/%s.", tty);
n = unit_name_from_path_instance("serial-getty", tty, ".service");
if (!n)
return log_oom();
r = unit_name_from_path_instance("serial-getty", tty, ".service", &n);
if (r < 0)
return log_error_errno(r, "Failed to generate service name: %m");
return add_symlink("serial-getty@.service", n);
}
static int add_container_getty(const char *tty) {
_cleanup_free_ char *n = NULL;
int r;
assert(tty);
log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
n = unit_name_from_path_instance("container-getty", tty, ".service");
if (!n)
return log_oom();
r = unit_name_from_path_instance("container-getty", tty, ".service", &n);
if (r < 0)
return log_error_errno(r, "Failed to generate service name: %m");
return add_symlink("container-getty@.service", n);
}