From badbef825d120f71e120f6fa6cd6c0c93a25225b Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Tue, 29 Nov 2022 17:54:19 -0600 Subject: [PATCH] roken: Add trailing / to PID file DIR path Otherwise if $HEIM_PIDFILE_DIR doesn't end in / then the pidfile gets created in the parent. --- lib/roken/write_pid.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/roken/write_pid.c b/lib/roken/write_pid.c index cf5299b64..c01091236 100644 --- a/lib/roken/write_pid.c +++ b/lib/roken/write_pid.c @@ -43,6 +43,8 @@ ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL pid_file_write(const char *progname) { const char *pidfile_dir = NULL; + const char *sep = "/"; + size_t pidfile_dir_len; char *ret = NULL; FILE *fp; @@ -57,7 +59,17 @@ pid_file_write(const char *progname) if (pidfile_dir == NULL) pidfile_dir = _PATH_VARRUN; - if (asprintf(&ret, "%s%s.pid", pidfile_dir, progname) < 0 || ret == NULL) + pidfile_dir_len = strlen(pidfile_dir); + if (pidfile_dir_len > 1 && + pidfile_dir[pidfile_dir_len - 1] == '/') + sep = ""; +#ifdef WIN32 + if (pidfile_dir_len > 1 && + pidfile_dir[pidfile_dir_len - 1] == '\\') + sep = ""; +#endif + + if (asprintf(&ret, "%s%s%s.pid", pidfile_dir, sep, progname) < 0 || ret == NULL) return NULL; fp = fopen(ret, "w"); if (fp == NULL) {