From 7c2e91ac48b20e6699d5c98c9912ea6427453c95 Mon Sep 17 00:00:00 2001 From: Michal Zidek Date: Mon, 10 Sep 2012 18:16:26 +0200 Subject: tools_util.h provides signal_sssd function. --- src/tools/tools_util.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'src/tools/tools_util.c') diff --git a/src/tools/tools_util.c b/src/tools/tools_util.c index fbb1d81b..049a4f58 100644 --- a/src/tools/tools_util.c +++ b/src/tools/tools_util.c @@ -578,3 +578,96 @@ done: talloc_free(conf_path); return ret; } + +static pid_t parse_pid(const char *strpid) +{ + long value; + char *endptr; + + errno = 0; + value = strtol(strpid, &endptr, 10); + if ((errno != 0) || (endptr == strpid) + || ((*endptr != '\0') && (*endptr != '\n'))) { + return 0; + } + + return value; +} + +static errno_t get_sssd_pid(pid_t *out_pid) +{ + int ret; + size_t fsize; + FILE *pid_file = NULL; + char pid_str[MAX_PID_LENGTH] = {'\0'}; + + *out_pid = 0; + + errno = 0; + pid_file = fopen(SSSD_PIDFILE, "r"); + if (pid_file == NULL) { + ret = errno; + DEBUG(SSSDBG_MINOR_FAILURE, ("Unable to open pid file \"%s\": %s\n", + SSSD_PIDFILE, strerror(ret))); + goto done; + } + + fsize = fread(pid_str, sizeof(char), MAX_PID_LENGTH * sizeof(char), + pid_file); + if (!feof(pid_file)) { + /* eof not reached */ + ret = ferror(pid_file); + if (ret != 0) { + DEBUG(SSSDBG_CRIT_FAILURE, ("Unable to read from file \"%s\": %s\n", + SSSD_PIDFILE, strerror(ret))); + } else { + DEBUG(SSSDBG_CRIT_FAILURE, ("File \"%s\" contains invalid pid.\n", + SSSD_PIDFILE)); + } + goto done; + } + if (fsize == 0) { + DEBUG(SSSDBG_CRIT_FAILURE, ("File \"%s\" contains no pid.\n", + SSSD_PIDFILE)); + ret = EINVAL; + goto done; + } + + pid_str[MAX_PID_LENGTH-1] = '\0'; + *out_pid = parse_pid(pid_str); + if (*out_pid == 0) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("File \"%s\" contains invalid pid.\n", SSSD_PIDFILE)); + ret = EINVAL; + goto done; + } + + ret = EOK; + +done: + if (pid_file != NULL) { + fclose(pid_file); + } + return ret; +} + +errno_t signal_sssd(int signum) +{ + int ret; + pid_t pid; + + ret = get_sssd_pid(&pid); + if (ret != EOK) { + return ret; + } + + if (kill(pid, signum) != 0) { + ret = errno; + DEBUG(SSSDBG_CRIT_FAILURE, + ("Could not send signal %d to process %d: %s\n", + signum, pid, strerror(errno))); + return ret; + } + + return EOK; +} -- cgit