diff options
author | Christian Ruppert <idl0r@gentoo.org> | 2010-04-30 03:39:51 +0200 |
---|---|---|
committer | Christian Ruppert <idl0r@gentoo.org> | 2010-04-30 03:39:51 +0200 |
commit | f87ea9832c96495d72d36b67bf65af63832ca0e2 (patch) | |
tree | 9b54c9a1d93dd5f5cb8f524b49245b50102ef774 | |
parent | Search cronolog in PATH (diff) | |
download | fifo-cronolog-f87ea9832c96495d72d36b67bf65af63832ca0e2.tar.gz fifo-cronolog-f87ea9832c96495d72d36b67bf65af63832ca0e2.tar.bz2 fifo-cronolog-f87ea9832c96495d72d36b67bf65af63832ca0e2.zip |
Fix previous commit. Improvements.
Add missing cronolog pointer declaration.
Free the pointer where necessary.
Fix gcc complain about ignored return value of write.
Improve get_cronolog(), use stat's return value and check for ENOENT.
-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 */ |