pidfile(): ensure prefix path ends with slash

For some builds using Buildroot, the system default _PATH_VARRUN did not
include a trailing slas '/', which cause the pidfile() function to fail.

Example:
  pidfile("arg") => "/var/runarg.pid"

Since the __pidfile_path can be overridden we add a check if that prefix
path ends with '/', if not we add one in the path composition.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-03-25 05:23:41 +01:00
parent 960e7f69ed
commit 9bfe54f959
+5 -1
View File
@@ -45,6 +45,7 @@
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef pidfile
@@ -93,7 +94,10 @@ int pidfile(const char *basename)
}
if (basename[0] != '/') {
if (asprintf(&pidfile_path, "%s%s.pid", __pidfile_path, basename) == -1)
size_t len = strlen(__pidfile_path);
int slash = __pidfile_path[len > 0 ? len - 1 : 0] != '/';
if (asprintf(&pidfile_path, "%s%s%s.pid", __pidfile_path, slash ? "/" : "", basename) == -1)
return (-1);
} else {
if (asprintf(&pidfile_path, "%s", basename) == -1)