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
+22 -18
View File
@@ -99,7 +99,7 @@ static int parse_argv(int argc, char *argv[]) {
case ARG_TEMPLATE:
if (!unit_name_is_valid(optarg, true) || !unit_name_is_template(optarg)) {
if (!unit_name_is_valid(optarg, UNIT_NAME_TEMPLATE)) {
log_error("Template name %s is not valid.", optarg);
return -EINVAL;
}
@@ -166,22 +166,26 @@ int main(int argc, char *argv[]) {
switch (arg_action) {
case ACTION_ESCAPE:
if (arg_path)
e = unit_name_path_escape(*i);
else
if (arg_path) {
r = unit_name_path_escape(*i, &e);
if (r < 0) {
log_error_errno(r, "Failed to escape string: %m");
goto finish;
}
} else {
e = unit_name_escape(*i);
if (!e) {
r = log_oom();
goto finish;
if (!e) {
r = log_oom();
goto finish;
}
}
if (arg_template) {
char *x;
x = unit_name_replace_instance(arg_template, e);
if (!x) {
r = log_oom();
r = unit_name_replace_instance(arg_template, e, &x);
if (r < 0) {
log_error_errno(r, "Failed to replace instance: %m");
goto finish;
}
@@ -204,20 +208,20 @@ int main(int argc, char *argv[]) {
case ACTION_UNESCAPE:
if (arg_path)
e = unit_name_path_unescape(*i);
r = unit_name_path_unescape(*i, &e);
else
e = unit_name_unescape(*i);
r = unit_name_unescape(*i, &e);
if (!e) {
r = log_oom();
if (r < 0) {
log_error_errno(r, "Failed to unescape string: %m");
goto finish;
}
break;
case ACTION_MANGLE:
e = unit_name_mangle(*i, MANGLE_NOGLOB);
if (!e) {
r = log_oom();
r = unit_name_mangle(*i, UNIT_NAME_NOGLOB, &e);
if (r < 0) {
log_error_errno(r, "Failed to mangle name: %m");
goto finish;
}
break;