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)
44 needlen = s->end + len + 1;
45 if (needlen > s->len) {
48 if (s->len * 2 > needlen)
50 n = realloc(s->s, needlen);
56 /* Don't append if we ran out of memory */
60 memcpy(s->s + s->end, a, len);
65 #define FLAG_UTC 0x0001
67 static void expand_percent_expression(e2fsck_t ctx, char ch,
68 struct string *s, int *flags)
70 struct tm *tm = NULL, tm_struct;
71 struct passwd *pw = NULL, pw_struct;
75 if ((ch == 'D') || (ch == 'd') || (ch == 'm') || (ch == 'y') ||
77 (ch == 'T') || (ch == 'H') || (ch == 'M') || (ch == 'S')) {
79 tm = (*flags & FLAG_UTC) ? gmtime_r(&ctx->now, &tm_struct) :
80 localtime_r(&ctx->now, &tm_struct);
85 append_string(s, "%", 1);
88 sprintf(buf, "%02d", tm->tm_mday);
91 sprintf(buf, "%d%02d%02d", tm->tm_year + 1900, tm->tm_mon + 1,
96 strcpy(buf, "server");
99 gethostname(buf, sizeof(buf));
100 buf[sizeof(buf)-1] = 0;
104 sprintf(buf, "%02d", tm->tm_hour);
107 sprintf(buf, "%02d", tm->tm_mon + 1);
110 sprintf(buf, "%02d", tm->tm_min);
112 case 'N': /* block device name */
113 cp = strrchr(ctx->filesystem_name, '/');
117 cp = ctx->filesystem_name;
118 append_string(s, cp, 0);
121 sprintf(buf, "%lu", (unsigned long) getpid());
124 sprintf(buf, "%lu", (unsigned long) ctx->now);
127 sprintf(buf, "%02d", tm->tm_sec);
130 sprintf(buf, "%02d%02d%02d", tm->tm_hour, tm->tm_min,
135 strcpy(buf, "tytso");
138 #ifdef HAVE_GETPWUID_R
139 getpwuid_r(getuid(), &pw_struct, buf, sizeof(buf), &pw);
141 pw = getpwuid(getuid());
144 append_string(s, pw->pw_name, 0);
151 sprintf(buf, "%02d", tm->tm_year % 100);
154 sprintf(buf, "%d", tm->tm_year + 1900);
157 append_string(s, buf, 0);
160 static void expand_logfn(e2fsck_t ctx, const char *log_fn, struct string *s)
166 alloc_string(s, 100);
167 for (cp = log_fn; *cp; cp++) {
170 expand_percent_expression(ctx, *cp, s, &flags);
173 for (i = 0; cp[i]; i++)
176 append_string(s, cp, i);
181 static int outbufsize;
184 static int do_read(int fd)
190 c = read(fd, buffer, sizeof(buffer)-1);
194 n = realloc(outbuf, outbufsize + c);
197 memcpy(((char *)outbuf)+outbufsize, buffer, c);
204 * Fork a child process to save the output of the logfile until the
205 * appropriate file system is mounted read/write.
207 static FILE *save_output(const char *s0, const char *s1, const char *s2)
221 /* At least one potential output file name is valid */
222 if (!s0 && !s1 && !s2)
236 if (daemon(0, 0) < 0) {
241 * Grab the output from our parent
244 while (do_read(fds[0]) > 0)
248 /* OK, now let's try to open the output file */
252 fd = open(s0, O_WRONLY|O_CREAT|O_TRUNC, 0644);
254 fd = open(s1, O_WRONLY|O_CREAT|O_TRUNC, 0644);
256 fd = open(s2, O_WRONLY|O_CREAT|O_TRUNC, 0644);
263 while (outbufsize > 0) {
264 c = write(fd, cp, outbufsize);
266 if ((errno == EAGAIN) || (errno == EINTR))
277 ret = fdopen(fds[1], "w");
284 void set_up_logging(e2fsck_t ctx)
286 struct string s, s1, s2;
287 char *s0 = 0, *log_dir = 0, *log_fn = 0;
288 int log_dir_wait = 0;
290 s.s = s1.s = s2.s = 0;
292 profile_get_boolean(ctx->profile, "options", "log_dir_wait", 0, 0,
295 log_fn = string_copy(ctx, ctx->log_fn, 0);
297 profile_get_string(ctx->profile, "options", "log_filename",
299 profile_get_string(ctx->profile, "options", "log_dir", 0, 0, &log_dir);
301 if (!log_fn || !log_fn[0])
304 expand_logfn(ctx, log_fn, &s);
305 if ((log_fn[0] == '/') || !log_dir || !log_dir[0])
308 if (log_dir && log_dir[0]) {
309 alloc_string(&s1, strlen(log_dir) + strlen(s.s) + 2);
310 append_string(&s1, log_dir, 0);
311 append_string(&s1, "/", 1);
312 append_string(&s1, s.s, 0);
316 profile_get_string(ctx->profile, "options", "log_dir_fallback", 0, 0,
318 if (log_dir && log_dir[0]) {
319 alloc_string(&s2, strlen(log_dir) + strlen(s.s) + 2);
320 append_string(&s2, log_dir, 0);
321 append_string(&s2, "/", 1);
322 append_string(&s2, s.s, 0);
323 printf("%s\n", s2.s);
327 ctx->logf = fopen(s0, "w");
328 if (!ctx->logf && s1.s)
329 ctx->logf = fopen(s1.s, "w");
330 if (!ctx->logf && s2.s)
331 ctx->logf = fopen(s2.s, "w");
332 if (!ctx->logf && log_dir_wait)
333 ctx->logf = save_output(s0, s1.s, s2.s);
344 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
345 const char *description)
352 sprintf(buf, "Can't allocate %s\n", description);
355 memset(ret, 0, size);
359 errcode_t e2fsck_allocate_context(e2fsck_t *ret)
365 context = malloc(sizeof(struct e2fsck_struct));
369 memset(context, 0, sizeof(struct e2fsck_struct));
371 context->now = 1332006474;
373 context->filesystem_name = "/dev/sda3";
374 context->device_name = "fslabel";
380 int main(int argc, char **argv)
385 putenv("TZ=EST+5:00");
386 e2fsck_allocate_context(&ctx);
387 expand_logfn(ctx, "e2fsck-%N.%h.%u.%D-%T", &s);
390 expand_logfn(ctx, "e2fsck-%N.%h.%u.%Y%m%d-%H%M%S", &s);