blob: f0011f81d7b321df9730b3b0fe1070246a06344f [file] [log] [blame]
Theodore Ts'of9ddad52003-04-14 18:05:12 -04001/*
2 * logsave.c --- A program which saves the output of a program until
3 * /var/log is mounted.
4 *
5 * Copyright (C) 2003 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
Theodore Ts'oebabf2a2008-07-13 15:32:37 -040013#define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
14
Theodore Ts'of9ddad52003-04-14 18:05:12 -040015#include <stdio.h>
16#include <stdlib.h>
17#include <unistd.h>
18#include <string.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21#include <fcntl.h>
22#include <time.h>
23#include <errno.h>
Theodore Ts'o62653d92008-03-26 09:42:00 -040024#ifdef HAVE_SIGNAL_H
25#include <signal.h>
26#endif
Theodore Ts'of9ddad52003-04-14 18:05:12 -040027#ifdef HAVE_GETOPT_H
28#include <getopt.h>
29#else
30extern char *optarg;
31extern int optind;
32#endif
33
34int outfd = -1;
35int outbufsize = 0;
36void *outbuf = 0;
37int verbose = 0;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040038int do_skip = 0;
39int skip_mode = 0;
Theodore Ts'o62653d92008-03-26 09:42:00 -040040pid_t child_pid = -1;
Theodore Ts'of9ddad52003-04-14 18:05:12 -040041
42static void usage(char *progname)
43{
44 printf("Usage: %s [-v] [-d dir] logfile program\n", progname);
45 exit(1);
46}
47
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040048#define SEND_LOG 0x01
49#define SEND_CONSOLE 0x02
50#define SEND_BOTH 0x03
51
52static void send_output(const char *buffer, int c, int flag)
Theodore Ts'of9ddad52003-04-14 18:05:12 -040053{
54 char *n;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040055
Theodore Ts'of9ddad52003-04-14 18:05:12 -040056 if (c == 0)
57 c = strlen(buffer);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040058
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040059 if (flag & SEND_CONSOLE)
60 write(1, buffer, c);
61 if (!(flag & SEND_LOG))
62 return;
Theodore Ts'of9ddad52003-04-14 18:05:12 -040063 if (outfd > 0)
64 write(outfd, buffer, c);
65 else {
66 n = realloc(outbuf, outbufsize + c);
67 if (n) {
68 outbuf = n;
69 memcpy(((char *)outbuf)+outbufsize, buffer, c);
70 outbufsize += c;
71 }
72 }
73}
74
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040075static int do_read(int fd)
Theodore Ts'of9ddad52003-04-14 18:05:12 -040076{
77 int c;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040078 char buffer[4096], *cp, *sep;
Theodore Ts'of9ddad52003-04-14 18:05:12 -040079
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040080 c = read(fd, buffer, sizeof(buffer)-1);
81 if (c <= 0)
82 return c;
83 if (do_skip) {
84 send_output(buffer, c, SEND_CONSOLE);
85 buffer[c] = 0;
86 cp = buffer;
87 while (*cp) {
88 if (skip_mode) {
89 cp = strchr(cp, '\002');
90 if (!cp)
91 return 0;
92 cp++;
93 skip_mode = 0;
94 continue;
95 }
96 sep = strchr(cp, '\001');
97 if (sep)
98 *sep = 0;
99 send_output(cp, 0, SEND_LOG);
100 if (sep) {
101 cp = sep + 1;
102 skip_mode = 1;
103 } else
104 break;
105 }
106 } else
107 send_output(buffer, c, SEND_BOTH);
108 return c;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400109}
110
Theodore Ts'o62653d92008-03-26 09:42:00 -0400111static void signal_term(int sig)
112{
113 if (child_pid > 0)
114 kill(child_pid, sig);
115}
116
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400117static int run_program(char **argv)
118{
119 int fds[2];
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400120 int status, rc, pid;
121 char buffer[80];
Theodore Ts'o62653d92008-03-26 09:42:00 -0400122#ifdef HAVE_SIGNAL_H
123 struct sigaction sa;
124#endif
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400125
126 if (pipe(fds) < 0) {
127 perror("pipe");
128 exit(1);
129 }
130
Theodore Ts'o62653d92008-03-26 09:42:00 -0400131#ifdef HAVE_SIGNAL_H
132 memset(&sa, 0, sizeof(struct sigaction));
133 sa.sa_handler = signal_term;
134 sigaction(SIGINT, &sa, 0);
135 sigaction(SIGTERM, &sa, 0);
136#ifdef SA_RESTART
137 sa.sa_flags = SA_RESTART;
138#endif
139#endif
140
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400141 pid = fork();
142 if (pid < 0) {
143 perror("vfork");
144 exit(1);
145 }
146 if (pid == 0) {
147 dup2(fds[1],1); /* fds[1] replaces stdout */
148 dup2(fds[1],2); /* fds[1] replaces stderr */
149 close(fds[0]); /* don't need this here */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400150
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400151 execvp(argv[0], argv);
152 perror(argv[0]);
153 exit(1);
154 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400155 child_pid = pid;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400156 close(fds[1]);
157
158 while (!(waitpid(pid, &status, WNOHANG ))) {
159 do_read(fds[0]);
160 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400161 child_pid = -1;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400162 do_read(fds[0]);
163 close(fds[0]);
164
165 if ( WIFEXITED(status) ) {
166 rc = WEXITSTATUS(status);
167 if (rc) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400168 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500169 sprintf(buffer, " died with exit status %d\n", rc);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400170 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400171 }
172 } else {
173 if (WIFSIGNALED(status)) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400174 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500175 sprintf(buffer, "died with signal %d\n",
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400176 WTERMSIG(status));
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400177 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400178 rc = 1;
179 }
180 rc = 0;
181 }
182 return rc;
183}
184
185static int copy_from_stdin(void)
186{
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400187 int c, bad_read = 0;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400188
189 while (1) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400190 c = do_read(0);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400191 if ((c == 0 ) ||
192 ((c < 0) && ((errno == EAGAIN) || (errno == EINTR)))) {
193 if (bad_read++ > 3)
194 break;
195 continue;
196 }
197 if (c < 0) {
198 perror("read");
199 exit(1);
200 }
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400201 bad_read = 0;
202 }
203 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400204}
205
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400206
207
208int main(int argc, char **argv)
209{
210 int c, pid, rc;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400211 char *outfn, **cpp;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400212 int openflags = O_CREAT|O_WRONLY|O_TRUNC;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400213 int send_flag = SEND_LOG;
214 int do_stdin;
215 time_t t;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400216
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400217 while ((c = getopt(argc, argv, "+asv")) != EOF) {
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400218 switch (c) {
219 case 'a':
220 openflags &= ~O_TRUNC;
221 openflags |= O_APPEND;
222 break;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400223 case 's':
224 do_skip = 1;
225 break;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400226 case 'v':
227 verbose++;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400228 send_flag |= SEND_CONSOLE;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400229 break;
230 }
231 }
232 if (optind == argc || optind+1 == argc)
233 usage(argv[0]);
234 outfn = argv[optind];
235 optind++;
236 argv += optind;
237 argc -= optind;
238
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400239 outfd = open(outfn, openflags, 0644);
240 do_stdin = !strcmp(argv[0], "-");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400241
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400242 send_output("Log of ", 0, send_flag);
243 if (do_stdin)
244 send_output("stdin", 0, send_flag);
245 else {
246 for (cpp = argv; *cpp; cpp++) {
247 send_output(*cpp, 0, send_flag);
248 send_output(" ", 0, send_flag);
249 }
250 }
251 send_output("\n", 0, send_flag);
252 t = time(0);
253 send_output(ctime(&t), 0, send_flag);
254 send_output("\n", 0, send_flag);
255
256 if (do_stdin)
257 rc = copy_from_stdin();
258 else
259 rc = run_program(argv);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400260
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400261 send_output("\n", 0, send_flag);
262 t = time(0);
263 send_output(ctime(&t), 0, send_flag);
264 send_output("----------------\n", 0, send_flag);
265
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400266 if (outbuf) {
267 pid = fork();
268 if (pid < 0) {
269 perror("fork");
270 exit(1);
271 }
272 if (pid) {
273 if (verbose)
274 printf("Backgrounding to save %s later\n",
275 outfn);
276 exit(rc);
277 }
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500278 setsid(); /* To avoid getting killed by init */
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400279 while (outfd < 0) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400280 outfd = open(outfn, openflags, 0644);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400281 sleep(1);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400282 }
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400283 write(outfd, outbuf, outbufsize);
284 free(outbuf);
285 }
286 close(outfd);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400287
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400288 exit(rc);
289}