From f6fbac6f4cc84bb8b4b721cd92f85e10506cf9ca Mon Sep 17 00:00:00 2001 From: Nichamon Naksinehaboon Date: Mon, 4 Mar 2024 10:58:23 -0600 Subject: [PATCH] Make LDMSD create a PID file only when a path is specified Without the patch, LDMSD always creates a PID file. It determines the PID file location in the following order: the command-line option `-r`, the environment variables `LDMSD_PIDFILE`, and the default path `/var/run/ldmsd.pid`. Running LDMSD as a non-root user withput specifying a PID file path always causes a permission-denied error message because a non-root user cannot access `/var/run` directory. The patch changes LDMSD so that it only create a PID file when either the command-line option `-r` or the config command `pid_path` is specified. Regarding starting LDMSD via `systemd`, `systemd` can be configured to create a PID file for LDMSD. --- ldms/src/ldmsd/ldmsd.c | 44 ++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/ldms/src/ldmsd/ldmsd.c b/ldms/src/ldmsd/ldmsd.c index 9f2ff2dde..d2a6e8dc7 100644 --- a/ldms/src/ldmsd/ldmsd.c +++ b/ldms/src/ldmsd/ldmsd.c @@ -2208,38 +2208,24 @@ int main(int argc, char *argv[]) if (!foreground) { /* Create pidfile for daemon that usually goes away on exit. */ /* user arg, then env, then default to get pidfile name */ - if (!pidfile) { - char *pidpath = getenv("LDMSD_PIDFILE"); - if (!pidpath) { - pidfile = malloc(strlen(LDMSD_PIDFILE_FMT) - + strlen(basename(argv[0]) + 1)); - if (pidfile) - sprintf(pidfile, LDMSD_PIDFILE_FMT, basename(argv[0])); - } else { - pidfile = strdup(pidpath); + if (pidfile) { + if( !access( pidfile, F_OK ) ) { + ovis_log(NULL, OVIS_LERROR, "Existing pid file named '%s': %s\n", + pidfile, "overwritten if writable"); } - if (!pidfile) { - ovis_log(NULL, OVIS_LERROR, "Out of memory\n"); - av_free(auth_opt); - exit(1); + FILE *pfile = fopen_perm(pidfile,"w", LDMSD_DEFAULT_FILE_PERM); + if (!pfile) { + int piderr = errno; + ovis_log(NULL, OVIS_LERROR, "Could not open the pid file named '%s': %s\n", + pidfile, STRERROR(piderr)); + free(pidfile); + pidfile = NULL; + } else { + pid_t mypid = getpid(); + fprintf(pfile,"%ld\n",(long)mypid); + fclose(pfile); } } - if( !access( pidfile, F_OK ) ) { - ovis_log(NULL, OVIS_LERROR, "Existing pid file named '%s': %s\n", - pidfile, "overwritten if writable"); - } - FILE *pfile = fopen_perm(pidfile,"w", LDMSD_DEFAULT_FILE_PERM); - if (!pfile) { - int piderr = errno; - ovis_log(NULL, OVIS_LERROR, "Could not open the pid file named '%s': %s\n", - pidfile, STRERROR(piderr)); - free(pidfile); - pidfile = NULL; - } else { - pid_t mypid = getpid(); - fprintf(pfile,"%ld\n",(long)mypid); - fclose(pfile); - } if (pidfile && banner) { char *suffix = ".version"; bannerfile = malloc(strlen(suffix)+strlen(pidfile)+1);