2 * logfile.c --- set up e2fsck log files
4 * Copyright 1996, 1997 by Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
16 #include <sys/types.h>
29 static void alloc_string(struct string *s, int len)
32 /* e2fsck_allocate_memory(ctx, len, "logfile name"); */
37 static void append_string(struct string *s, const char *a, int len)
42 if (s->end + len >= s->len) {
43 char *n = realloc(s, s->len * 2);
49 len = s->len - s->end - 1;
54 memcpy(s->s + s->end, a, len);
59 #define FLAG_UTC 0x0001
61 static void expand_percent_expression(e2fsck_t ctx, char ch,
62 struct string *s, int *flags)
64 struct tm *tm = NULL, tm_struct;
65 struct passwd *pw = NULL, pw_struct;
69 if ((ch == 'D') || (ch == 'd') || (ch == 'm') || (ch == 'y') ||
71 (ch == 'T') || (ch == 'H') || (ch == 'M') || (ch == 'S')) {
73 tm = (*flags & FLAG_UTC) ? gmtime_r(&ctx->now, &tm_struct) :
74 localtime_r(&ctx->now, &tm_struct);
79 append_string(s, "%", 1);
82 sprintf(buf, "%02d", tm->tm_mday);
85 sprintf(buf, "%d%02d%02d", tm->tm_year + 1900, tm->tm_mon + 1,
90 strcpy(buf, "server");
93 gethostname(buf, sizeof(buf));
94 buf[sizeof(buf)-1] = 0;
98 sprintf(buf, "%02d", tm->tm_hour);
101 sprintf(buf, "%02d", tm->tm_mon + 1);
104 sprintf(buf, "%02d", tm->tm_min);
106 case 'N': /* block device name */
107 cp = strrchr(ctx->filesystem_name, '/');
111 cp = ctx->filesystem_name;
112 append_string(s, cp, 0);
115 sprintf(buf, "%lu", (unsigned long) getpid());
118 sprintf(buf, "%lu", (unsigned long) ctx->now);
121 sprintf(buf, "%02d", tm->tm_sec);
124 sprintf(buf, "%02d%02d%02d", tm->tm_hour, tm->tm_min,
129 strcpy(buf, "tytso");
132 #ifdef HAVE_GETPWUID_R
133 getpwuid_r(getuid(), &pw_struct, buf, sizeof(buf), &pw);
135 pw = getpwuid(getuid());
138 append_string(s, pw->pw_name, 0);
145 sprintf(buf, "%02d", tm->tm_year % 100);
148 sprintf(buf, "%d", tm->tm_year + 1900);
151 append_string(s, buf, 0);
154 static void expand_logfn(e2fsck_t ctx, const char *log_fn, struct string *s)
160 alloc_string(s, 100);
161 for (cp = log_fn; *cp; cp++) {
164 expand_percent_expression(ctx, *cp, s, &flags);
167 for (i = 0; cp[i]; i++)
170 append_string(s, cp, i);
175 static int outbufsize;
178 static int do_read(int fd)
184 c = read(fd, buffer, sizeof(buffer)-1);
188 n = realloc(outbuf, outbufsize + c);
191 memcpy(((char *)outbuf)+outbufsize, buffer, c);
198 * Fork a child process to save the output of the logfile until the
199 * appropriate file system is mounted read/write.
201 static FILE *save_output(const char *s0, const char *s1, const char *s2)
215 /* At least one potential output file name is valid */
216 if (!s0 && !s1 && !s2)
230 if (daemon(0, 0) < 0) {
235 * Grab the output from our parent
238 while (do_read(fds[0]) > 0)
242 /* OK, now let's try to open the output file */
246 fd = open(s0, O_WRONLY|O_CREAT|O_TRUNC, 0644);
248 fd = open(s1, O_WRONLY|O_CREAT|O_TRUNC, 0644);
250 fd = open(s2, O_WRONLY|O_CREAT|O_TRUNC, 0644);
257 while (outbufsize > 0) {
258 c = write(fd, cp, outbufsize);
260 if ((errno == EAGAIN) || (errno == EINTR))
271 ret = fdopen(fds[1], "w");
278 void set_up_logging(e2fsck_t ctx)
280 struct string s, s1, s2;
281 char *s0 = 0, *log_dir = 0, *log_fn = 0;
282 int log_dir_wait = 0;
284 s.s = s1.s = s2.s = 0;
286 profile_get_boolean(ctx->profile, "options", "log_dir_wait", 0, 0,
289 log_fn = string_copy(ctx, ctx->log_fn, 0);
291 profile_get_string(ctx->profile, "options", "log_filename",
293 profile_get_string(ctx->profile, "options", "log_dir", 0, 0, &log_dir);
295 if (!log_fn || !log_fn[0])
298 expand_logfn(ctx, log_fn, &s);
299 if ((log_fn[0] == '/') || !log_dir || !log_dir[0])
302 if (log_dir && log_dir[0]) {
303 alloc_string(&s1, strlen(log_dir) + strlen(s.s) + 2);
304 append_string(&s1, log_dir, 0);
305 append_string(&s1, "/", 1);
306 append_string(&s1, s.s, 0);
310 profile_get_string(ctx->profile, "options", "log_dir_fallback", 0, 0,
312 if (log_dir && log_dir[0]) {
313 alloc_string(&s2, strlen(log_dir) + strlen(s.s) + 2);
314 append_string(&s2, log_dir, 0);
315 append_string(&s2, "/", 1);
316 append_string(&s2, s.s, 0);
317 printf("%s\n", s2.s);
321 ctx->logf = fopen(s0, "w");
322 if (!ctx->logf && s1.s)
323 ctx->logf = fopen(s1.s, "w");
324 if (!ctx->logf && s2.s)
325 ctx->logf = fopen(s2.s, "w");
326 if (!ctx->logf && log_dir_wait)
327 ctx->logf = save_output(s0, s1.s, s2.s);
338 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
339 const char *description)
346 sprintf(buf, "Can't allocate %s\n", description);
349 memset(ret, 0, size);
353 errcode_t e2fsck_allocate_context(e2fsck_t *ret)
359 context = malloc(sizeof(struct e2fsck_struct));
363 memset(context, 0, sizeof(struct e2fsck_struct));
365 context->now = 1332006474;
367 context->filesystem_name = "/dev/sda3";
368 context->device_name = "fslabel";
374 int main(int argc, char **argv)
379 putenv("TZ=EST+5:00");
380 e2fsck_allocate_context(&ctx);
381 expand_logfn(ctx, "e2fsck-%N.%h.%u.%D-%T", &s);
384 expand_logfn(ctx, "e2fsck-%N.%h.%u.%Y%m%d-%H%M%S", &s);