From 2241cea76264ab3b466ac5ace8f683cfe6de20d4 Mon Sep 17 00:00:00 2001 From: Christian Ruppert Date: Mon, 26 Apr 2010 22:29:03 +0200 Subject: Clean up Use tabs instead of spaces. Fix nesting. Add vim modeline. --- squid-cronolog.c | 158 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 80 insertions(+), 78 deletions(-) diff --git a/squid-cronolog.c b/squid-cronolog.c index bfc5346..58bd6a9 100644 --- a/squid-cronolog.c +++ b/squid-cronolog.c @@ -23,84 +23,86 @@ void die(int sig) { struct sigaction sigdie = { .sa_handler = die }; int main(int argc, char *argv[]) { + time_t t = 0; + pid_t pid = 0; + int fd, pidfd, pidlen = 16; + char *fifo, *log, pidstr[16]; - time_t t = 0; - pid_t pid = 0; - int fd, pidfd, pidlen = 16; - char *fifo, *log, pidstr[16]; - - if (argc != 4) { - fprintf(stderr, "Usage: %s /path/to/pidfile /path/to/fifo" - " /path/to/logs/%%Y%%m%%d.log\n", argv[0]); - exit(1); - } - - pidfile = argv[1]; - fifo = argv[2]; - log = argv[3]; - - /* Test for fifo access, leave open for use */ - /* O_NONBLOCK allows us to fork into the background */ - fd = open(fifo, O_RDONLY|O_NONBLOCK); - if (fd == -1) { - fprintf(stderr, "Cannot open fifo %s: %s\n", fifo, strerror(errno)); - exit(1); - } - - pid = fork(); - - if (pid == 0) { - /* CHILD 0 */ - - /* Record pid to pidfile */ - pidfd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL, 0644); - if (pidfd == -1) { - fprintf(stderr, "Cannot create and open pid file %s: %s\n", - pidfile, strerror(errno)); - exit(1); - } else { - pidlen = snprintf(pidstr, pidlen, "%d\n", getpid()); - write(pidfd, pidstr, pidlen); - close(pidfd); - } - - /* close shell output */ - close(0); - close(1); - close(2); - - /* set a process group for easy cleanup */ - setpgrp(); - sigaction(SIGTERM, &sigdie, NULL); - - /* keep the cronolog process open */ - while (1) { - t = time(NULL); - pid = fork(); - if (pid == 0) { - /* CHILD 1 */ - - /* replace stdin with fifo */ - dup2(fd, 0); - /* unset O_NONBLOCK */ - fcntl(0, F_SETFL, 0); - /* exec cronolog */ - execl(CRONOLOG, CRONOLOG, log, (char *) 0); - /* filure! give up on life */ + if (argc != 4) { + fprintf(stderr, "Usage: %s /path/to/pidfile /path/to/fifo" + " /path/to/logs/%%Y%%m%%d.log\n", argv[0]); exit(1); - } else { - /* PARENT 1 */ - /* watch child 1 */ - waitpid(pid, NULL, 0); - if (time(NULL) - t < 2) { - /* restarting too fast, wait 10 seconds */ - sleep(10); - } - } - } - } - - /* PARENT 0 exits */ - close(fd); - return 0; + } + + pidfile = argv[1]; + fifo = argv[2]; + log = argv[3]; + + /* Test for fifo access, leave open for use */ + /* O_NONBLOCK allows us to fork into the background */ + fd = open(fifo, O_RDONLY|O_NONBLOCK); + if (fd == -1) { + fprintf(stderr, "Cannot open fifo %s: %s\n", fifo, strerror(errno)); + exit(1); + } + + pid = fork(); + + if (pid == 0) { + /* CHILD 0 */ + + /* Record pid to pidfile */ + pidfd = open(pidfile, O_WRONLY|O_CREAT|O_EXCL, 0644); + if (pidfd == -1) { + fprintf(stderr, "Cannot create and open pid file %s: %s\n", + pidfile, strerror(errno)); + exit(1); + } else { + pidlen = snprintf(pidstr, pidlen, "%d\n", getpid()); + write(pidfd, pidstr, pidlen); + close(pidfd); + } + + /* close shell output */ + close(0); + close(1); + close(2); + + /* set a process group for easy cleanup */ + setpgrp(); + sigaction(SIGTERM, &sigdie, NULL); + + /* keep the cronolog process open */ + while (1) { + t = time(NULL); + pid = fork(); + if (pid == 0) { + /* CHILD 1 */ + + /* replace stdin with fifo */ + dup2(fd, 0); + /* unset O_NONBLOCK */ + fcntl(0, F_SETFL, 0); + /* exec cronolog */ + execl(CRONOLOG, CRONOLOG, log, (char *) 0); + /* filure! give up on life */ + exit(1); + } else { + /* PARENT 1 */ + /* watch child 1 */ + waitpid(pid, NULL, 0); + if (time(NULL) - t < 2) { + /* restarting too fast, wait 10 seconds */ + sleep(10); + } + } + } + } + + /* PARENT 0 exits */ + close(fd); + + return 0; } + +/* vim: set ts=4 sw=4 tw=80: */ -- cgit v1.2.3-65-gdbad