blob: 81ac9f6e01a5d4d836f2168d5c0e2967d9039b90 [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{
77 char *n;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040078
Theodore Ts'of9ddad52003-04-14 18:05:12 -040079 if (c == 0)
80 c = strlen(buffer);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040081
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040082 if (flag & SEND_CONSOLE)
Theodore Ts'o6e6b71d2009-04-22 15:12:40 -040083 write_all(1, buffer, c);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040084 if (!(flag & SEND_LOG))
85 return;
Theodore Ts'of9ddad52003-04-14 18:05:12 -040086 if (outfd > 0)
Theodore Ts'o6e6b71d2009-04-22 15:12:40 -040087 write_all(outfd, buffer, c);
Theodore Ts'of9ddad52003-04-14 18:05:12 -040088 else {
89 n = realloc(outbuf, outbufsize + c);
90 if (n) {
91 outbuf = n;
92 memcpy(((char *)outbuf)+outbufsize, buffer, c);
93 outbufsize += c;
94 }
95 }
96}
97
Theodore Ts'obc34d6b2003-04-16 14:05:06 -040098static int do_read(int fd)
Theodore Ts'of9ddad52003-04-14 18:05:12 -040099{
100 int c;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400101 char buffer[4096], *cp, *sep;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400102
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400103 c = read(fd, buffer, sizeof(buffer)-1);
104 if (c <= 0)
105 return c;
106 if (do_skip) {
107 send_output(buffer, c, SEND_CONSOLE);
108 buffer[c] = 0;
109 cp = buffer;
110 while (*cp) {
111 if (skip_mode) {
112 cp = strchr(cp, '\002');
113 if (!cp)
114 return 0;
115 cp++;
116 skip_mode = 0;
117 continue;
118 }
119 sep = strchr(cp, '\001');
120 if (sep)
121 *sep = 0;
122 send_output(cp, 0, SEND_LOG);
123 if (sep) {
124 cp = sep + 1;
125 skip_mode = 1;
126 } else
127 break;
128 }
129 } else
130 send_output(buffer, c, SEND_BOTH);
131 return c;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400132}
133
Theodore Ts'o62653d92008-03-26 09:42:00 -0400134static void signal_term(int sig)
135{
136 if (child_pid > 0)
137 kill(child_pid, sig);
138}
139
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400140static int run_program(char **argv)
141{
142 int fds[2];
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400143 int status, rc, pid;
144 char buffer[80];
Theodore Ts'o62653d92008-03-26 09:42:00 -0400145#ifdef HAVE_SIGNAL_H
146 struct sigaction sa;
147#endif
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400148
149 if (pipe(fds) < 0) {
150 perror("pipe");
151 exit(1);
152 }
153
Theodore Ts'o62653d92008-03-26 09:42:00 -0400154#ifdef HAVE_SIGNAL_H
155 memset(&sa, 0, sizeof(struct sigaction));
156 sa.sa_handler = signal_term;
157 sigaction(SIGINT, &sa, 0);
158 sigaction(SIGTERM, &sa, 0);
159#ifdef SA_RESTART
160 sa.sa_flags = SA_RESTART;
161#endif
162#endif
163
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400164 pid = fork();
165 if (pid < 0) {
166 perror("vfork");
167 exit(1);
168 }
169 if (pid == 0) {
170 dup2(fds[1],1); /* fds[1] replaces stdout */
171 dup2(fds[1],2); /* fds[1] replaces stderr */
172 close(fds[0]); /* don't need this here */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400173
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400174 execvp(argv[0], argv);
175 perror(argv[0]);
176 exit(1);
177 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400178 child_pid = pid;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400179 close(fds[1]);
180
181 while (!(waitpid(pid, &status, WNOHANG ))) {
182 do_read(fds[0]);
183 }
Theodore Ts'o62653d92008-03-26 09:42:00 -0400184 child_pid = -1;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400185 do_read(fds[0]);
186 close(fds[0]);
187
188 if ( WIFEXITED(status) ) {
189 rc = WEXITSTATUS(status);
190 if (rc) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400191 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500192 sprintf(buffer, " died with exit status %d\n", rc);
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400193 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400194 }
195 } else {
196 if (WIFSIGNALED(status)) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400197 send_output(argv[0], 0, SEND_BOTH);
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500198 sprintf(buffer, "died with signal %d\n",
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400199 WTERMSIG(status));
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400200 send_output(buffer, 0, SEND_BOTH);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400201 rc = 1;
202 }
203 rc = 0;
204 }
205 return rc;
206}
207
208static int copy_from_stdin(void)
209{
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400210 int c, bad_read = 0;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400211
212 while (1) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400213 c = do_read(0);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400214 if ((c == 0 ) ||
215 ((c < 0) && ((errno == EAGAIN) || (errno == EINTR)))) {
216 if (bad_read++ > 3)
217 break;
218 continue;
219 }
220 if (c < 0) {
221 perror("read");
222 exit(1);
223 }
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400224 bad_read = 0;
225 }
226 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400227}
228
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400229
230
231int main(int argc, char **argv)
232{
233 int c, pid, rc;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400234 char *outfn, **cpp;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400235 int openflags = O_CREAT|O_WRONLY|O_TRUNC;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400236 int send_flag = SEND_LOG;
237 int do_stdin;
238 time_t t;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400239
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400240 while ((c = getopt(argc, argv, "+asv")) != EOF) {
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400241 switch (c) {
242 case 'a':
243 openflags &= ~O_TRUNC;
244 openflags |= O_APPEND;
245 break;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400246 case 's':
247 do_skip = 1;
248 break;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400249 case 'v':
250 verbose++;
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400251 send_flag |= SEND_CONSOLE;
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400252 break;
253 }
254 }
255 if (optind == argc || optind+1 == argc)
256 usage(argv[0]);
257 outfn = argv[optind];
258 optind++;
259 argv += optind;
260 argc -= optind;
261
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400262 outfd = open(outfn, openflags, 0644);
263 do_stdin = !strcmp(argv[0], "-");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400264
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400265 send_output("Log of ", 0, send_flag);
266 if (do_stdin)
267 send_output("stdin", 0, send_flag);
268 else {
269 for (cpp = argv; *cpp; cpp++) {
270 send_output(*cpp, 0, send_flag);
271 send_output(" ", 0, send_flag);
272 }
273 }
274 send_output("\n", 0, send_flag);
275 t = time(0);
276 send_output(ctime(&t), 0, send_flag);
277 send_output("\n", 0, send_flag);
278
279 if (do_stdin)
280 rc = copy_from_stdin();
281 else
282 rc = run_program(argv);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400283
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400284 send_output("\n", 0, send_flag);
285 t = time(0);
286 send_output(ctime(&t), 0, send_flag);
287 send_output("----------------\n", 0, send_flag);
288
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400289 if (outbuf) {
290 pid = fork();
291 if (pid < 0) {
292 perror("fork");
293 exit(1);
294 }
295 if (pid) {
296 if (verbose)
297 printf("Backgrounding to save %s later\n",
298 outfn);
299 exit(rc);
300 }
Theodore Ts'o5bec5742004-03-04 20:30:16 -0500301 setsid(); /* To avoid getting killed by init */
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400302 while (outfd < 0) {
Theodore Ts'obc34d6b2003-04-16 14:05:06 -0400303 outfd = open(outfn, openflags, 0644);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400304 sleep(1);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400305 }
Theodore Ts'o6e6b71d2009-04-22 15:12:40 -0400306 write_all(outfd, outbuf, outbufsize);
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400307 free(outbuf);
308 }
309 close(outfd);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400310
Theodore Ts'of9ddad52003-04-14 18:05:12 -0400311 exit(rc);
312}