2 * logsave.c --- A program which saves the output of a program until
5 * Copyright (C) 2003 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
13 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
19 #include <sys/types.h>
42 static void usage(char *progname)
44 printf("Usage: %s [-v] [-d dir] logfile program\n", progname);
49 #define SEND_CONSOLE 0x02
50 #define SEND_BOTH 0x03
53 * Helper function that does the right thing if write returns a
54 * partial write, or an EGAIN/EINTR error.
56 static int write_all(int fd, const char *buf, size_t count)
62 ret = write(fd, buf, count);
64 if ((errno == EAGAIN) || (errno == EINTR))
75 static void send_output(const char *buffer, int c, int flag)
84 if (flag & SEND_CONSOLE) {
89 for (d=0; d < cnt; d++) {
91 (cp[d] == '\001' || cp[d] == '\002')) {
103 if (!(flag & SEND_LOG))
106 write_all(outfd, buffer, c);
108 n = realloc(outbuf, outbufsize + c);
111 memcpy(((char *)outbuf)+outbufsize, buffer, c);
117 static int do_read(int fd)
120 char buffer[4096], *cp, *sep;
122 c = read(fd, buffer, sizeof(buffer)-1);
126 send_output(buffer, c, SEND_CONSOLE);
131 cp = strchr(cp, '\002');
138 sep = strchr(cp, '\001');
141 send_output(cp, 0, SEND_LOG);
149 send_output(buffer, c, SEND_BOTH);
153 static void signal_term(int sig)
156 kill(child_pid, sig);
159 static int run_program(char **argv)
174 memset(&sa, 0, sizeof(struct sigaction));
175 sa.sa_handler = signal_term;
176 sigaction(SIGINT, &sa, 0);
177 sigaction(SIGTERM, &sa, 0);
179 sa.sa_flags = SA_RESTART;
189 dup2(fds[1],1); /* fds[1] replaces stdout */
190 dup2(fds[1],2); /* fds[1] replaces stderr */
191 close(fds[0]); /* don't need this here */
193 execvp(argv[0], argv);
200 while (!(waitpid(pid, &status, WNOHANG ))) {
207 if ( WIFEXITED(status) ) {
208 rc = WEXITSTATUS(status);
210 send_output(argv[0], 0, SEND_BOTH);
211 sprintf(buffer, " died with exit status %d\n", rc);
212 send_output(buffer, 0, SEND_BOTH);
215 if (WIFSIGNALED(status)) {
216 send_output(argv[0], 0, SEND_BOTH);
217 sprintf(buffer, "died with signal %d\n",
219 send_output(buffer, 0, SEND_BOTH);
227 static int copy_from_stdin(void)
234 ((c < 0) && ((errno == EAGAIN) || (errno == EINTR)))) {
250 int main(int argc, char **argv)
254 int openflags = O_CREAT|O_WRONLY|O_TRUNC;
255 int send_flag = SEND_LOG;
259 while ((c = getopt(argc, argv, "+asv")) != EOF) {
262 openflags &= ~O_TRUNC;
263 openflags |= O_APPEND;
270 send_flag |= SEND_CONSOLE;
274 if (optind == argc || optind+1 == argc)
276 outfn = argv[optind];
281 outfd = open(outfn, openflags, 0644);
282 do_stdin = !strcmp(argv[0], "-");
284 send_output("Log of ", 0, send_flag);
286 send_output("stdin", 0, send_flag);
288 for (cpp = argv; *cpp; cpp++) {
289 send_output(*cpp, 0, send_flag);
290 send_output(" ", 0, send_flag);
293 send_output("\n", 0, send_flag);
295 send_output(ctime(&t), 0, send_flag);
296 send_output("\n", 0, send_flag);
299 rc = copy_from_stdin();
301 rc = run_program(argv);
303 send_output("\n", 0, send_flag);
305 send_output(ctime(&t), 0, send_flag);
306 send_output("----------------\n", 0, send_flag);
316 printf("Backgrounding to save %s later\n",
320 setsid(); /* To avoid getting killed by init */
322 outfd = open(outfn, openflags, 0644);
325 write_all(outfd, outbuf, outbufsize);