summaryrefslogtreecommitdiff
path: root/src/core/unit-printf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/unit-printf.c')
-rw-r--r--src/core/unit-printf.c200
1 files changed, 133 insertions, 67 deletions
diff --git a/src/core/unit-printf.c b/src/core/unit-printf.c
index ffc203dd92..1a29a986e9 100644
--- a/src/core/unit-printf.c
+++ b/src/core/unit-printf.c
@@ -30,98 +30,158 @@
#include "cgroup-util.h"
#include "special.h"
-static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
+static int specifier_prefix_and_instance(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
+ char *n;
+
assert(u);
- return unit_name_to_prefix_and_instance(u->id);
+ n = unit_name_to_prefix_and_instance(u->id);
+ if (!n)
+ return -ENOMEM;
+
+ *ret = n;
+ return 0;
}
-static char *specifier_prefix(char specifier, void *data, void *userdata) {
+static int specifier_prefix(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
+ char *n;
+
assert(u);
- return unit_name_to_prefix(u->id);
+ n = unit_name_to_prefix(u->id);
+ if (!n)
+ return -ENOMEM;
+
+ *ret = n;
+ return 0;
}
-static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
+static int specifier_prefix_unescaped(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
- char *p, *r;
+ _cleanup_free_ char *p = NULL;
+ char *n;
assert(u);
p = unit_name_to_prefix(u->id);
if (!p)
- return NULL;
+ return -ENOMEM;
- r = unit_name_unescape(p);
- free(p);
+ n = unit_name_unescape(p);
+ if (!n)
+ return -ENOMEM;
- return r;
+ *ret = n;
+ return 0;
}
-static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
+static int specifier_instance_unescaped(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
+ char *n;
+
assert(u);
if (u->instance)
- return unit_name_unescape(u->instance);
+ n = unit_name_unescape(u->instance);
+ else
+ n = strdup("");
+
+ if (!n)
+ return -ENOMEM;
- return strdup("");
+ *ret = n;
+ return 0;
}
-static char *specifier_filename(char specifier, void *data, void *userdata) {
+static int specifier_filename(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
+ char *n;
+
assert(u);
if (u->instance)
- return unit_name_path_unescape(u->instance);
+ n = unit_name_path_unescape(u->instance);
+ else
+ n = unit_name_to_path(u->id);
- return unit_name_to_path(u->id);
+ if (!n)
+ return -ENOMEM;
+
+ *ret = n;
+ return 0;
}
-static char *specifier_cgroup(char specifier, void *data, void *userdata) {
+static int specifier_cgroup(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
+ char *n;
+
assert(u);
- return unit_default_cgroup_path(u);
+ n = unit_default_cgroup_path(u);
+ if (!n)
+ return -ENOMEM;
+
+ *ret = n;
+ return 0;
}
-static char *specifier_cgroup_root(char specifier, void *data, void *userdata) {
- _cleanup_free_ char *p = NULL;
+static int specifier_cgroup_root(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
const char *slice;
+ char *n;
int r;
assert(u);
slice = unit_slice_name(u);
if (specifier == 'R' || !slice)
- return strdup(u->manager->cgroup_root);
+ n = strdup(u->manager->cgroup_root);
+ else {
+ _cleanup_free_ char *p = NULL;
- r = cg_slice_to_path(slice, &p);
- if (r < 0)
- return NULL;
+ r = cg_slice_to_path(slice, &p);
+ if (r < 0)
+ return r;
+
+ n = strjoin(u->manager->cgroup_root, "/", p, NULL);
+ if (!n)
+ return -ENOMEM;
+ }
- return strjoin(u->manager->cgroup_root, "/", p, NULL);
+ *ret = n;
+ return 0;
}
-static char *specifier_runtime(char specifier, void *data, void *userdata) {
+static int specifier_runtime(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
+ char *n = NULL;
+
assert(u);
if (u->manager->running_as == SYSTEMD_USER) {
const char *e;
e = getenv("XDG_RUNTIME_DIR");
- if (e)
- return strdup(e);
+ if (e) {
+ n = strdup(e);
+ if (!n)
+ return -ENOMEM;
+ }
}
- return strdup("/run");
+ if (!n) {
+ n = strdup("/run");
+ if (!n)
+ return -ENOMEM;
+ }
+
+ *ret = n;
+ return 0;
}
-static char *specifier_user_name(char specifier, void *data, void *userdata) {
+static int specifier_user_name(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
ExecContext *c;
int r;
@@ -143,26 +203,31 @@ static char *specifier_user_name(char specifier, void *data, void *userdata) {
/* fish username from passwd */
r = get_user_creds(&username, &uid, NULL, NULL, NULL);
if (r < 0)
- return NULL;
+ return r;
switch (specifier) {
case 'U':
if (asprintf(&printed, "%d", uid) < 0)
- return NULL;
+ return -ENOMEM;
break;
case 'u':
printed = strdup(username);
break;
}
- return printed;
+ if (!printed)
+ return -ENOMEM;
+
+ *ret = printed;
+ return 0;
}
-static char *specifier_user_home(char specifier, void *data, void *userdata) {
+static int specifier_user_home(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
ExecContext *c;
int r;
const char *username, *home;
+ char *n;
assert(u);
@@ -174,25 +239,31 @@ static char *specifier_user_home(char specifier, void *data, void *userdata) {
r = get_home_dir(&h);
if (r < 0)
- return NULL;
+ return r;
- return h;
+ *ret = h;
+ return 0;
}
username = c->user;
r = get_user_creds(&username, NULL, NULL, &home, NULL);
if (r < 0)
- return NULL;
+ return r;
- return strdup(home);
+ n = strdup(home);
+ if (!n)
+ return -ENOMEM;
+
+ *ret = n;
+ return 0;
}
-static char *specifier_user_shell(char specifier, void *data, void *userdata) {
+static int specifier_user_shell(char specifier, void *data, void *userdata, char **ret) {
Unit *u = userdata;
ExecContext *c;
int r;
const char *username, *shell;
- char *ret;
+ char *n;
assert(u);
@@ -205,27 +276,18 @@ static char *specifier_user_shell(char specifier, void *data, void *userdata) {
/* return /bin/sh for root, otherwise the value from passwd */
r = get_user_creds(&username, NULL, NULL, NULL, &shell);
- if (r < 0) {
- log_warning_unit(u->id,
- "Failed to determine shell: %s",
- strerror(-r));
- return NULL;
- }
-
- if (!path_is_absolute(shell)) {
- log_warning_unit(u->id,
- "Shell %s is not absolute, ignoring.",
- shell);
- }
+ if (r < 0)
+ return r;
- ret = strdup(shell);
- if (!ret)
- log_oom();
+ n = strdup(shell);
+ if (!n)
+ return -ENOMEM;
- return ret;
+ *ret = n;
+ return 0;
}
-char *unit_name_printf(Unit *u, const char* format) {
+int unit_name_printf(Unit *u, const char* format, char **ret) {
/*
* This will use the passed string as format string and
@@ -247,11 +309,12 @@ char *unit_name_printf(Unit *u, const char* format) {
assert(u);
assert(format);
+ assert(ret);
- return specifier_printf(format, table, u);
+ return specifier_printf(format, table, u, ret);
}
-char *unit_full_printf(Unit *u, const char *format) {
+int unit_full_printf(Unit *u, const char *format, char **ret) {
/* This is similar to unit_name_printf() but also supports
* unescaping. Also, adds a couple of additional codes:
@@ -296,14 +359,17 @@ char *unit_full_printf(Unit *u, const char *format) {
{}
};
+ assert(u);
assert(format);
+ assert(ret);
- return specifier_printf(format, table, u);
+ return specifier_printf(format, table, u, ret);
}
-char **unit_full_printf_strv(Unit *u, char **l) {
+int unit_full_printf_strv(Unit *u, char **l, char ***ret) {
size_t n;
char **r, **i, **j;
+ int q;
/* Applies unit_full_printf to every entry in l */
@@ -312,22 +378,22 @@ char **unit_full_printf_strv(Unit *u, char **l) {
n = strv_length(l);
r = new(char*, n+1);
if (!r)
- return NULL;
+ return -ENOMEM;
for (i = l, j = r; *i; i++, j++) {
- *j = unit_full_printf(u, *i);
- if (!*j)
+ q = unit_full_printf(u, *i, j);
+ if (q < 0)
goto fail;
}
*j = NULL;
- return r;
+ *ret = r;
+ return 0;
fail:
for (j--; j >= r; j--)
free(*j);
free(r);
-
- return NULL;
+ return q;
}