diff options
Diffstat (limited to 'squid-cronolog.c')
-rw-r--r-- | squid-cronolog.c | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/squid-cronolog.c b/squid-cronolog.c index c1cfc5d..c24ffc9 100644 --- a/squid-cronolog.c +++ b/squid-cronolog.c @@ -13,7 +13,7 @@ char *pidfile; char *get_cronolog(void) { int len = 0; - char *path, *tmp; + char *path, *tmp = NULL; struct stat st; path = strtok(getenv("PATH"), ":"); @@ -22,14 +22,29 @@ char *get_cronolog(void) { tmp = (char *)malloc(len); snprintf(tmp, len, "%s%s", path, "/cronolog"); - stat(tmp, &st); + if(stat(tmp, &st) == -1) { + if(errno == ENOENT) { + free(tmp); + path = strtok(NULL, ":"); + continue; + } + + fprintf(stderr, "stat(): Error: %s\n", strerror(errno)); + free(tmp); + return NULL; + } + if(st.st_mode & S_IFREG) return tmp; - else + else { free(tmp); - - path = strtok(NULL, ":"); + return NULL; + } } + + if(tmp != NULL) + free(tmp); + return NULL; } @@ -47,7 +62,7 @@ int main(int argc, char *argv[]) { time_t t = 0; pid_t pid = 0; int fd, pidfd, pidlen = 16; - char *fifo, *log, pidstr[16]; + char *fifo, *log, *cronolog, pidstr[16]; if (argc != 4) { fprintf(stderr, "Usage: %s /path/to/pidfile /path/to/fifo" @@ -70,6 +85,7 @@ int main(int argc, char *argv[]) { fd = open(fifo, O_RDONLY|O_NONBLOCK); if (fd == -1) { fprintf(stderr, "Cannot open fifo %s: %s\n", fifo, strerror(errno)); + free(cronolog); exit(1); } @@ -83,10 +99,15 @@ int main(int argc, char *argv[]) { if (pidfd == -1) { fprintf(stderr, "Cannot create and open pid file %s: %s\n", pidfile, strerror(errno)); + free(cronolog); exit(1); } else { pidlen = snprintf(pidstr, pidlen, "%d\n", getpid()); - write(pidfd, pidstr, pidlen); + if(write(pidfd, pidstr, pidlen) == -1) { + fprintf(stderr, "Cannot write pid file %s: %s\n", pidfile, strerror(errno)); + free(cronolog); + exit(1); + } close(pidfd); } @@ -113,6 +134,7 @@ int main(int argc, char *argv[]) { /* exec cronolog */ execl(cronolog, cronolog, log, (char *) 0); /* filure! give up on life */ + free(cronolog); exit(1); } else { /* PARENT 1 */ |