From 8e7e4a730ba40bbc46c9d1e84207fd35781ca05a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 22 Aug 2022 11:38:58 +0200 Subject: tree-wide: use path_join() instead of prefix_roota() in various cases prefix_roota() is something we should stop using. It is bad for three reasons: 1. As it names suggests it's supposed to be used when working relative to some root directory, but given it doesn't follow symlinks (and instead just stupidly joins paths) it is not a good choice for that. 2. More often than not it is currently used with inputs under control of the user, and that is icky given it typically allocates memory on the stack. 3. It's a redundant interface, where chase_symlinks() and path_join() already exist as better, safer interfaces. Hence, let's start moving things from prefix_roota() to path_join() for the cases where that's appropriate. --- src/sysv-generator/sysv-generator.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/sysv-generator') diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c index 14ae873dc0..3c5df6c3ec 100644 --- a/src/sysv-generator/sysv-generator.c +++ b/src/sysv-generator/sysv-generator.c @@ -80,16 +80,16 @@ static void free_sysvstub_hashmapp(Hashmap **h) { } static int add_alias(const char *service, const char *alias) { - const char *link; - int r; + _cleanup_free_ char *link = NULL; assert(service); assert(alias); - link = prefix_roota(arg_dest, alias); + link = path_join(arg_dest, alias); + if (!link) + return -ENOMEM; - r = symlink(service, link); - if (r < 0) { + if (symlink(service, link) < 0) { if (errno == EEXIST) return 0; @@ -100,9 +100,8 @@ static int add_alias(const char *service, const char *alias) { } static int generate_unit_file(SysvStub *s) { - _cleanup_free_ char *path_escaped = NULL; + _cleanup_free_ char *path_escaped = NULL, *unit = NULL; _cleanup_fclose_ FILE *f = NULL; - const char *unit; int r; assert(s); @@ -114,7 +113,9 @@ static int generate_unit_file(SysvStub *s) { if (!path_escaped) return log_oom(); - unit = prefix_roota(arg_dest, s->name); + unit = path_join(arg_dest, s->name); + if (!unit) + return log_oom(); /* We might already have a symlink with the same name from a Provides:, * or from backup files like /etc/init.d/foo.bak. Real scripts always win, -- cgit v1.2.3