blob: c6fd3f8ced4fe5022aae73ea5f4eb0f611197040 [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
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <string.h>
17#include <sys/types.h>
18#include <sys/wait.h>
19#include <fcntl.h>
20#include <time.h>
21#include <errno.h>
Theodore Ts'o62653d92008-03-26 09:42:00 -040022#ifdef HAVE_SIGNAL_H
23#include <signal.h>
24#endif
Theodore Ts'of9ddad52003-04-14 18:05:12 -040025#ifdef HAVE_GETOPT_H
26#include <getopt.h>
27#else
28extern char *optarg;
29extern int optind;
30#endif
31
32int outfd = -1;
33int outbufsize = 0;
34void *outbuf = 0;
35int verbose = 0;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040036int do_skip = 0;
37int skip_mode = 0;
Theodore Ts'o62653d92008-03-26 09:42:00 -040038pid_t child_pid = -1;
Theodore Ts'of9ddad52003-04-14 18:05:12 -040039
40static void usage(char *progname)
41{
42 printf("Usage: %s [-v] [-d dir] logfile program\n", progname);
43 exit(1);
44}
45
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040046#define SEND_LOG 0x01
47#define SEND_CONSOLE 0x02
48#define SEND_BOTH 0x03
49
50static void send_output(const char *buffer, int c, int flag)
Theodore Ts'of9ddad52003-04-14 18:05:12 -040051{
52 char *n;
53
54 if (c == 0)
55 c = strlen(buffer);
56
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040057 if (flag & SEND_CONSOLE)
58 write(1, buffer, c);
59 if (!(flag & SEND_LOG))
60 return;
Theodore Ts'of9ddad52003-04-14 18:05:12 -040061 if (outfd > 0)
62 write(outfd, buffer, c);
63 else {
64 n = realloc(outbuf, outbufsize + c);
65 if (n) {
66 outbuf = n;
67 memcpy(((char *)outbuf)+outbufsize, buffer, c);
68 outbufsize += c;
69 }
70 }
71}
72
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040073static int do_read(int fd)
Theodore Ts'of9ddad52003-04-14 18:05:12 -040074{
75 int c;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040076 char buffer[4096], *cp, *sep;
Theodore Ts'of9ddad52003-04-14 18:05:12 -040077
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040078 c = read(fd, buffer, sizeof(buffer)-1);
79 if (c <= 0)
80 return c;
81 if (do_skip) {
82 send_output(buffer, c, SEND_CONSOLE);
83 buffer[c] = 0;
84 cp = buffer;
85 while (*cp) {
86 if (skip_mode) {
87 cp = strchr(cp, '\002');
88 if (!cp)
89 return 0;
90 cp++;
91 skip_mode = 0;
92 continue;
93 }
94 sep = strchr(cp, '\001');
95 if (sep)
96 *sep = 0;
97 send_output(cp, 0, SEND_LOG);
98 if (sep) {
99 cp = sep + 1;
100 skip_mode = 1;
101 } else
102 break;
103 }
104 } else
105 send_output(buffer, c, SEND_BOTH);
106 return c;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400107}
108
Theodore Ts'o62653d92008-03-26 09:42:00 -0400109static void signal_term(int sig)
110{
111 if (child_pid > 0)
112 kill(child_pid, sig);
113}
114
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400115static int run_program(char **argv)
116{
117 int fds[2];
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400118 int status, rc, pid;
119 char buffer[80];
Theodore Ts'o62653d92008-03-26 09:42:00 -0400120#ifdef HAVE_SIGNAL_H
121 struct sigaction sa;
122#endif
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400123
124 if (pipe(fds) < 0) {
125 perror("pipe");
126 exit(1);
127 }
128
Theodore Ts'o62653d92008-03-26 09:42:00 -0400129#ifdef HAVE_SIGNAL_H
130 memset(&sa, 0, sizeof(struct sigaction));
131 sa.sa_handler = signal_term;
132 sigaction(SIGINT, &sa, 0);
133 sigaction(SIGTERM, &sa, 0);
134#ifdef SA_RESTART
135 sa.sa_flags = SA_RESTART;
136#endif
137#endif
138
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400139 pid = fork();
140 if (pid < 0) {
141 perror("vfork");
142 exit(1);
143 }
144 if (pid == 0) {
145 dup2(fds[1],1); /* fds[1] replaces stdout */
146 dup2(fds[1],2); /* fds[1] replaces stderr */
147 close(fds[0]); /* don't need this here */
148
149 execvp(argv[0], argv);
150 perror(argv[0]);
151 exit(1);
152 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400153 child_pid = pid;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400154 close(fds[1]);
155
156 while (!(waitpid(pid, &status, WNOHANG ))) {
157 do_read(fds[0]);
158 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400159 child_pid = -1;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400160 do_read(fds[0]);
161 close(fds[0]);
162
163 if ( WIFEXITED(status) ) {
164 rc = WEXITSTATUS(status);
165 if (rc) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400166 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500167 sprintf(buffer, " died with exit status %d\n", rc);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400168 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400169 }
170 } else {
171 if (WIFSIGNALED(status)) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400172 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500173 sprintf(buffer, "died with signal %d\n",
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400174 WTERMSIG(status));
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400175 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400176 rc = 1;
177 }
178 rc = 0;
179 }
180 return rc;
181}
182
183static int copy_from_stdin(void)
184{
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400185 int c, bad_read = 0;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400186
187 while (1) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400188 c = do_read(0);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400189 if ((c == 0 ) ||
190 ((c < 0) && ((errno == EAGAIN) || (errno == EINTR)))) {
191 if (bad_read++ > 3)
192 break;
193 continue;
194 }
195 if (c < 0) {
196 perror("read");
197 exit(1);
198 }
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400199 bad_read = 0;
200 }
201 return 0;
202}
203
204
205
206int main(int argc, char **argv)
207{
208 int c, pid, rc;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400209 char *outfn, **cpp;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400210 int openflags = O_CREAT|O_WRONLY|O_TRUNC;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400211 int send_flag = SEND_LOG;
212 int do_stdin;
213 time_t t;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400214
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400215 while ((c = getopt(argc, argv, "+asv")) != EOF) {
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400216 switch (c) {
217 case 'a':
218 openflags &= ~O_TRUNC;
219 openflags |= O_APPEND;
220 break;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400221 case 's':
222 do_skip = 1;
223 break;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400224 case 'v':
225 verbose++;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400226 send_flag |= SEND_CONSOLE;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400227 break;
228 }
229 }
230 if (optind == argc || optind+1 == argc)
231 usage(argv[0]);
232 outfn = argv[optind];
233 optind++;
234 argv += optind;
235 argc -= optind;
236
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400237 outfd = open(outfn, openflags, 0644);
238 do_stdin = !strcmp(argv[0], "-");
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400239
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400240 send_output("Log of ", 0, send_flag);
241 if (do_stdin)
242 send_output("stdin", 0, send_flag);
243 else {
244 for (cpp = argv; *cpp; cpp++) {
245 send_output(*cpp, 0, send_flag);
246 send_output(" ", 0, send_flag);
247 }
248 }
249 send_output("\n", 0, send_flag);
250 t = time(0);
251 send_output(ctime(&t), 0, send_flag);
252 send_output("\n", 0, send_flag);
253
254 if (do_stdin)
255 rc = copy_from_stdin();
256 else
257 rc = run_program(argv);
258
259 send_output("\n", 0, send_flag);
260 t = time(0);
261 send_output(ctime(&t), 0, send_flag);
262 send_output("----------------\n", 0, send_flag);
263
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400264 if (outbuf) {
265 pid = fork();
266 if (pid < 0) {
267 perror("fork");
268 exit(1);
269 }
270 if (pid) {
271 if (verbose)
272 printf("Backgrounding to save %s later\n",
273 outfn);
274 exit(rc);
275 }
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500276 setsid(); /* To avoid getting killed by init */
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400277 while (outfd < 0) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400278 outfd = open(outfn, openflags, 0644);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400279 sleep(1);
280 }
281 write(outfd, outbuf, outbufsize);
282 free(outbuf);
283 }
284 close(outfd);
285
286 exit(rc);
287}