blob: 43eb6f719e354c674d442db0af5f95c51efa3fe5 [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
Theodore Ts'o6e6b71d2009-04-22 15:12:40 -040052/*
53 * Helper function that does the right thing if write returns a
54 * partial write, or an EGAIN/EINTR error.
55 */
56static int write_all(int fd, const char *buf, size_t count)
57{
58 ssize_t ret;
59 int c = 0;
60
61 while (count > 0) {
62 ret = write(fd, buf, count);
63 if (ret < 0) {
64 if ((errno == EAGAIN) || (errno == EINTR))
65 continue;
66 return -1;
67 }
68 count -= ret;
69 buf += ret;
70 c += ret;
71 }
72 return c;
73}
74
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040075static void send_output(const char *buffer, int c, int flag)
Theodore Ts'of9ddad52003-04-14 18:05:12 -040076{
Theodore Ts'o427f95f2009-06-29 14:58:07 -040077 const char *cp;
78 char *n;
79 int cnt, d, del;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040080
Theodore Ts'of9ddad52003-04-14 18:05:12 -040081 if (c == 0)
82 c = strlen(buffer);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040083
Theodore Ts'o427f95f2009-06-29 14:58:07 -040084 if (flag & SEND_CONSOLE) {
85 cnt = c;
86 cp = buffer;
87 while (cnt) {
88 del = 0;
89 for (d=0; d < cnt; d++) {
90 if (skip_mode &&
91 (cp[d] == '\001' || cp[d] == '\002')) {
92 del = 1;
93 break;
94 }
95 }
96 write_all(1, cp, d);
97 if (del)
98 d++;
99 cnt -= d;
100 cp += d;
101 }
102 }
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400103 if (!(flag & SEND_LOG))
104 return;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400105 if (outfd > 0)
Theodore Ts'o6e6b71d2009-04-22 15:12:40 -0400106 write_all(outfd, buffer, c);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400107 else {
108 n = realloc(outbuf, outbufsize + c);
109 if (n) {
110 outbuf = n;
111 memcpy(((char *)outbuf)+outbufsize, buffer, c);
112 outbufsize += c;
113 }
114 }
115}
116
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400117static int do_read(int fd)
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400118{
119 int c;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400120 char buffer[4096], *cp, *sep;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400121
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400122 c = read(fd, buffer, sizeof(buffer)-1);
123 if (c <= 0)
124 return c;
125 if (do_skip) {
126 send_output(buffer, c, SEND_CONSOLE);
127 buffer[c] = 0;
128 cp = buffer;
129 while (*cp) {
130 if (skip_mode) {
131 cp = strchr(cp, '\002');
132 if (!cp)
133 return 0;
134 cp++;
135 skip_mode = 0;
136 continue;
137 }
138 sep = strchr(cp, '\001');
139 if (sep)
140 *sep = 0;
141 send_output(cp, 0, SEND_LOG);
142 if (sep) {
143 cp = sep + 1;
144 skip_mode = 1;
145 } else
146 break;
147 }
148 } else
149 send_output(buffer, c, SEND_BOTH);
150 return c;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400151}
152
Theodore Ts'o62653d92008-03-26 09:42:00 -0400153static void signal_term(int sig)
154{
155 if (child_pid > 0)
156 kill(child_pid, sig);
157}
158
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400159static int run_program(char **argv)
160{
161 int fds[2];
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400162 int status, rc, pid;
163 char buffer[80];
Theodore Ts'o62653d92008-03-26 09:42:00 -0400164#ifdef HAVE_SIGNAL_H
165 struct sigaction sa;
166#endif
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400167
168 if (pipe(fds) < 0) {
169 perror("pipe");
170 exit(1);
171 }
172
Theodore Ts'o62653d92008-03-26 09:42:00 -0400173#ifdef HAVE_SIGNAL_H
174 memset(&sa, 0, sizeof(struct sigaction));
175 sa.sa_handler = signal_term;
176 sigaction(SIGINT, &sa, 0);
177 sigaction(SIGTERM, &sa, 0);
178#ifdef SA_RESTART
179 sa.sa_flags = SA_RESTART;
180#endif
181#endif
182
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400183 pid = fork();
184 if (pid < 0) {
185 perror("vfork");
186 exit(1);
187 }
188 if (pid == 0) {
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 */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400192
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400193 execvp(argv[0], argv);
194 perror(argv[0]);
195 exit(1);
196 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400197 child_pid = pid;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400198 close(fds[1]);
199
200 while (!(waitpid(pid, &status, WNOHANG ))) {
201 do_read(fds[0]);
202 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400203 child_pid = -1;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400204 do_read(fds[0]);
205 close(fds[0]);
206
207 if ( WIFEXITED(status) ) {
208 rc = WEXITSTATUS(status);
209 if (rc) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400210 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500211 sprintf(buffer, " died with exit status %d\n", rc);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400212 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400213 }
214 } else {
215 if (WIFSIGNALED(status)) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400216 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500217 sprintf(buffer, "died with signal %d\n",
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400218 WTERMSIG(status));
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400219 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400220 rc = 1;
221 }
222 rc = 0;
223 }
224 return rc;
225}
226
227static int copy_from_stdin(void)
228{
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400229 int c, bad_read = 0;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400230
231 while (1) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400232 c = do_read(0);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400233 if ((c == 0 ) ||
234 ((c < 0) && ((errno == EAGAIN) || (errno == EINTR)))) {
235 if (bad_read++ > 3)
236 break;
237 continue;
238 }
239 if (c < 0) {
240 perror("read");
241 exit(1);
242 }
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400243 bad_read = 0;
244 }
245 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400246}
247
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400248
249
250int main(int argc, char **argv)
251{
252 int c, pid, rc;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400253 char *outfn, **cpp;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400254 int openflags = O_CREAT|O_WRONLY|O_TRUNC;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400255 int send_flag = SEND_LOG;
256 int do_stdin;
257 time_t t;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400258
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400259 while ((c = getopt(argc, argv, "+asv")) != EOF) {
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400260 switch (c) {
261 case 'a':
262 openflags &= ~O_TRUNC;
263 openflags |= O_APPEND;
264 break;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400265 case 's':
266 do_skip = 1;
267 break;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400268 case 'v':
269 verbose++;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400270 send_flag |= SEND_CONSOLE;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400271 break;
272 }
273 }
274 if (optind == argc || optind+1 == argc)
275 usage(argv[0]);
276 outfn = argv[optind];
277 optind++;
278 argv += optind;
279 argc -= optind;
280
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400281 outfd = open(outfn, openflags, 0644);
282 do_stdin = !strcmp(argv[0], "-");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400283
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400284 send_output("Log of ", 0, send_flag);
285 if (do_stdin)
286 send_output("stdin", 0, send_flag);
287 else {
288 for (cpp = argv; *cpp; cpp++) {
289 send_output(*cpp, 0, send_flag);
290 send_output(" ", 0, send_flag);
291 }
292 }
293 send_output("\n", 0, send_flag);
294 t = time(0);
295 send_output(ctime(&t), 0, send_flag);
296 send_output("\n", 0, send_flag);
297
298 if (do_stdin)
299 rc = copy_from_stdin();
300 else
301 rc = run_program(argv);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400302
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400303 send_output("\n", 0, send_flag);
304 t = time(0);
305 send_output(ctime(&t), 0, send_flag);
306 send_output("----------------\n", 0, send_flag);
307
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400308 if (outbuf) {
309 pid = fork();
310 if (pid < 0) {
311 perror("fork");
312 exit(1);
313 }
314 if (pid) {
315 if (verbose)
316 printf("Backgrounding to save %s later\n",
317 outfn);
318 exit(rc);
319 }
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500320 setsid(); /* To avoid getting killed by init */
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400321 while (outfd < 0) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400322 outfd = open(outfn, openflags, 0644);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400323 sleep(1);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400324 }
Theodore Ts'o6e6b71d2009-04-22 15:12:40 -0400325 write_all(outfd, outbuf, outbufsize);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400326 free(outbuf);
327 }
328 close(outfd);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400329
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400330 exit(rc);
331}