#include #include #include #include #include #include #define LOGFILE_BUF_LEN 65536 int do_rotate = 0; void signal_hup(int unused) { do_rotate = 1; } int main(int argc, char *argv[]) { int t; int fd, r; char buf[LOGFILE_BUF_LEN]; if (argc < 2) { printf("Error: usage: %s \n", argv[0]); exit(1); } fd = open(argv[1], O_WRONLY | O_APPEND | O_CREAT, 0644); if (fd < 0) { perror("open"); exit(1); } signal(SIGHUP, signal_hup); setbuf(stdin, NULL); setbuf(stdout, NULL); close(2); t = open("/dev/null", O_RDWR); assert(t > -1); dup2(t, 2); while( (r = read(0, buf, LOGFILE_BUF_LEN)) > 0) { if (write(fd, buf, r) != r) { perror("write"); break; } fsync(fd); /* Check for rotate (which is really just 'reopen') */ if (do_rotate == 1) { do_rotate = 0; close(fd); fd = -1; fd = open(argv[1], O_WRONLY | O_APPEND | O_CREAT, 0644); if (fd < 0) { perror("open"); exit(1); } } } close(fd); fd = -1; exit(0); }