From bed11923d2f443124020261a0c7dc00641ac0546 Mon Sep 17 00:00:00 2001 From: Kacper Wysocki Date: Tue, 16 Aug 2011 14:06:41 +0200 Subject: [PATCH] bugfix: pidfile creation --- src/prads.c | 8 ++++++-- src/sys_func.c | 19 +++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/prads.c b/src/prads.c index 98a480b..49d9069 100644 --- a/src/prads.c +++ b/src/prads.c @@ -1439,7 +1439,7 @@ int main(int argc, char *argv[]) if(config.pidfile){ if (!is_valid_path(config.pidfile)){ - elog("[*] Unable to create pidfile '%s'\n", config.pidfile); + elog("[!] Pidfile '%s' is not writable.\n", config.pidfile); exit(ENOENT); } } @@ -1448,7 +1448,11 @@ int main(int argc, char *argv[]) daemonize(NULL); } if (config.pidfile) { - create_pid_file(config.pidfile); + int rc; + if((rc=create_pid_file(config.pidfile))) { + elog("[!] pidfile error, wrong permissions or prads already running? %s: %s\n", config.pidfile, strerror(rc)); + exit(ENOENT); + } } diff --git a/src/sys_func.c b/src/sys_func.c index 6019492..1d61c97 100644 --- a/src/sys_func.c +++ b/src/sys_func.c @@ -188,12 +188,11 @@ int is_valid_path(const char *path) if (path == NULL) { return 0; } - if (stat(path, &st) != 0) { - return 0; - } - - if (S_ISREG(st.st_mode) && access(path, W_OK) != -1) { - return 1; + if (stat(path, &st) == 0) { + // path already exists. is it regular and writable? + if (!S_ISREG(st.st_mode) || access(path, W_OK) != -1) { + return 1; + } } memcpy(dir, path, strnlen(path, STDBUF)); @@ -287,7 +286,7 @@ int create_pid_file(const char *path) if (fcntl(fd, F_SETLK, &lock) == -1) { if (errno == EACCES || errno == EAGAIN) { - rval = ERROR; + rval = errno; } else { rval = ERROR; } @@ -296,10 +295,10 @@ int create_pid_file(const char *path) } snprintf(pid_buffer, sizeof(pid_buffer), "%d\n", (int)getpid()); if (ftruncate(fd, 0) != 0) { - return ERROR; + return errno; } - if (write(fd, pid_buffer, strlen(pid_buffer)) != 0) { - return ERROR; + if (write(fd, pid_buffer, strlen(pid_buffer)) == -1) { + return errno; } return SUCCESS; } -- 2.11.4.GIT