From aa2345321187878e381926b7d519ec33db3c4b6c Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Wed, 27 Sep 2023 15:52:56 +0200 Subject: [PATCH] Change: Ensure dir exists in pidfile_create --- base/pidfile.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/base/pidfile.c b/base/pidfile.c index 323308ed..c8fc1d29 100644 --- a/base/pidfile.c +++ b/base/pidfile.c @@ -13,6 +13,7 @@ #include /* for errno */ #include /* for g_free, gchar */ #include /* for g_unlink, g_fopen */ +#include /* for libgen */ #include /* for fclose, FILE */ #include /* for atoi */ #include /* for strerror */ @@ -38,12 +39,34 @@ int pidfile_create (gchar *pid_file_path) { - FILE *pidfile = g_fopen (pid_file_path, "w"); + FILE *pidfile; + gchar *copy, *dir; + + if (pid_file_path == NULL) + return -1; + + copy = g_strdup (pid_file_path); + dir = dirname (copy); + + /* Ensure directory exists. */ + + if (g_mkdir_with_parents (dir, 0755)) /* "rwxr-xr-x" */ + { + g_free (copy); + g_warning ("Failed to create PID file directory %s: %s", dir, + strerror (errno)); + return 1; + } + g_free (copy); + + /* Create file. */ + + pidfile = g_fopen (pid_file_path, "w"); if (pidfile == NULL) { - g_critical ("%s: failed to open pidfile: %s\n", __func__, - strerror (errno)); + g_critical ("%s: failed to open pidfile %s: %s\n", __func__, + pid_file_path, strerror (errno)); return 1; } else