Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 1 | /* |
| 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'o | ebabf2a | 2008-07-13 15:32:37 -0400 | [diff] [blame] | 13 | #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */ |
| 14 | |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 15 | #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'o | 62653d9 | 2008-03-26 09:42:00 -0400 | [diff] [blame] | 24 | #ifdef HAVE_SIGNAL_H |
| 25 | #include <signal.h> |
| 26 | #endif |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 27 | #ifdef HAVE_GETOPT_H |
| 28 | #include <getopt.h> |
| 29 | #else |
| 30 | extern char *optarg; |
| 31 | extern int optind; |
| 32 | #endif |
| 33 | |
| 34 | int outfd = -1; |
| 35 | int outbufsize = 0; |
| 36 | void *outbuf = 0; |
| 37 | int verbose = 0; |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 38 | int do_skip = 0; |
| 39 | int skip_mode = 0; |
Theodore Ts'o | 62653d9 | 2008-03-26 09:42:00 -0400 | [diff] [blame] | 40 | pid_t child_pid = -1; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 41 | |
| 42 | static void usage(char *progname) |
| 43 | { |
| 44 | printf("Usage: %s [-v] [-d dir] logfile program\n", progname); |
| 45 | exit(1); |
| 46 | } |
| 47 | |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 48 | #define SEND_LOG 0x01 |
| 49 | #define SEND_CONSOLE 0x02 |
| 50 | #define SEND_BOTH 0x03 |
| 51 | |
Theodore Ts'o | 6e6b71d | 2009-04-22 15:12:40 -0400 | [diff] [blame^] | 52 | /* |
| 53 | * Helper function that does the right thing if write returns a |
| 54 | * partial write, or an EGAIN/EINTR error. |
| 55 | */ |
| 56 | static 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'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 75 | static void send_output(const char *buffer, int c, int flag) |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 76 | { |
| 77 | char *n; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 78 | |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 79 | if (c == 0) |
| 80 | c = strlen(buffer); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 81 | |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 82 | if (flag & SEND_CONSOLE) |
Theodore Ts'o | 6e6b71d | 2009-04-22 15:12:40 -0400 | [diff] [blame^] | 83 | write_all(1, buffer, c); |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 84 | if (!(flag & SEND_LOG)) |
| 85 | return; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 86 | if (outfd > 0) |
Theodore Ts'o | 6e6b71d | 2009-04-22 15:12:40 -0400 | [diff] [blame^] | 87 | write_all(outfd, buffer, c); |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 88 | 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'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 98 | static int do_read(int fd) |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 99 | { |
| 100 | int c; |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 101 | char buffer[4096], *cp, *sep; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 102 | |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 103 | 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'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Theodore Ts'o | 62653d9 | 2008-03-26 09:42:00 -0400 | [diff] [blame] | 134 | static void signal_term(int sig) |
| 135 | { |
| 136 | if (child_pid > 0) |
| 137 | kill(child_pid, sig); |
| 138 | } |
| 139 | |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 140 | static int run_program(char **argv) |
| 141 | { |
| 142 | int fds[2]; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 143 | int status, rc, pid; |
| 144 | char buffer[80]; |
Theodore Ts'o | 62653d9 | 2008-03-26 09:42:00 -0400 | [diff] [blame] | 145 | #ifdef HAVE_SIGNAL_H |
| 146 | struct sigaction sa; |
| 147 | #endif |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 148 | |
| 149 | if (pipe(fds) < 0) { |
| 150 | perror("pipe"); |
| 151 | exit(1); |
| 152 | } |
| 153 | |
Theodore Ts'o | 62653d9 | 2008-03-26 09:42:00 -0400 | [diff] [blame] | 154 | #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'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 164 | 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 173 | |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 174 | execvp(argv[0], argv); |
| 175 | perror(argv[0]); |
| 176 | exit(1); |
| 177 | } |
Theodore Ts'o | 62653d9 | 2008-03-26 09:42:00 -0400 | [diff] [blame] | 178 | child_pid = pid; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 179 | close(fds[1]); |
| 180 | |
| 181 | while (!(waitpid(pid, &status, WNOHANG ))) { |
| 182 | do_read(fds[0]); |
| 183 | } |
Theodore Ts'o | 62653d9 | 2008-03-26 09:42:00 -0400 | [diff] [blame] | 184 | child_pid = -1; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 185 | do_read(fds[0]); |
| 186 | close(fds[0]); |
| 187 | |
| 188 | if ( WIFEXITED(status) ) { |
| 189 | rc = WEXITSTATUS(status); |
| 190 | if (rc) { |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 191 | send_output(argv[0], 0, SEND_BOTH); |
Theodore Ts'o | 5bec574 | 2004-03-04 20:30:16 -0500 | [diff] [blame] | 192 | sprintf(buffer, " died with exit status %d\n", rc); |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 193 | send_output(buffer, 0, SEND_BOTH); |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 194 | } |
| 195 | } else { |
| 196 | if (WIFSIGNALED(status)) { |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 197 | send_output(argv[0], 0, SEND_BOTH); |
Theodore Ts'o | 5bec574 | 2004-03-04 20:30:16 -0500 | [diff] [blame] | 198 | sprintf(buffer, "died with signal %d\n", |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 199 | WTERMSIG(status)); |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 200 | send_output(buffer, 0, SEND_BOTH); |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 201 | rc = 1; |
| 202 | } |
| 203 | rc = 0; |
| 204 | } |
| 205 | return rc; |
| 206 | } |
| 207 | |
| 208 | static int copy_from_stdin(void) |
| 209 | { |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 210 | int c, bad_read = 0; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 211 | |
| 212 | while (1) { |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 213 | c = do_read(0); |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 214 | 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'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 224 | bad_read = 0; |
| 225 | } |
| 226 | return 0; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 229 | |
| 230 | |
| 231 | int main(int argc, char **argv) |
| 232 | { |
| 233 | int c, pid, rc; |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 234 | char *outfn, **cpp; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 235 | int openflags = O_CREAT|O_WRONLY|O_TRUNC; |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 236 | int send_flag = SEND_LOG; |
| 237 | int do_stdin; |
| 238 | time_t t; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 239 | |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 240 | while ((c = getopt(argc, argv, "+asv")) != EOF) { |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 241 | switch (c) { |
| 242 | case 'a': |
| 243 | openflags &= ~O_TRUNC; |
| 244 | openflags |= O_APPEND; |
| 245 | break; |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 246 | case 's': |
| 247 | do_skip = 1; |
| 248 | break; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 249 | case 'v': |
| 250 | verbose++; |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 251 | send_flag |= SEND_CONSOLE; |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 252 | 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'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 262 | outfd = open(outfn, openflags, 0644); |
| 263 | do_stdin = !strcmp(argv[0], "-"); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 264 | |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 265 | 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 283 | |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 284 | 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'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 289 | 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'o | 5bec574 | 2004-03-04 20:30:16 -0500 | [diff] [blame] | 301 | setsid(); /* To avoid getting killed by init */ |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 302 | while (outfd < 0) { |
Theodore Ts'o | bc34d6b | 2003-04-16 14:05:06 -0400 | [diff] [blame] | 303 | outfd = open(outfn, openflags, 0644); |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 304 | sleep(1); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 305 | } |
Theodore Ts'o | 6e6b71d | 2009-04-22 15:12:40 -0400 | [diff] [blame^] | 306 | write_all(outfd, outbuf, outbufsize); |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 307 | free(outbuf); |
| 308 | } |
| 309 | close(outfd); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 310 | |
Theodore Ts'o | f9ddad5 | 2003-04-14 18:05:12 -0400 | [diff] [blame] | 311 | exit(rc); |
| 312 | } |