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
26 static void alloc_string(struct string *s, int len)
29 /* e2fsck_allocate_memory(ctx, len, "logfile name"); */
34 static void append_string(struct string *s, const char *a, int len)
39 if (s->end + len >= s->len) {
40 char *n = realloc(s, s->len * 2);
46 len = s->len - s->end - 1;
51 memcpy(s->s + s->end, a, len);
56 #define FLAG_UTC 0x0001
58 static void expand_percent_expression(e2fsck_t ctx, char ch,
59 struct string *s, int *flags)
61 struct tm *tm = NULL, tm_struct;
62 struct passwd *pw = NULL, pw_struct;
66 if ((ch == 'D') || (ch == 'd') || (ch == 'm') || (ch == 'y') ||
68 (ch == 'T') || (ch == 'H') || (ch == 'M') || (ch == 'S')) {
70 tm = (*flags & FLAG_UTC) ? gmtime_r(&ctx->now, &tm_struct) :
71 localtime_r(&ctx->now, &tm_struct);
76 append_string(s, "%", 1);
79 sprintf(buf, "%02d", tm->tm_mday);
82 sprintf(buf, "%d%02d%02d", tm->tm_year + 1900, tm->tm_mon + 1,
87 strcpy(buf, "server");
90 gethostname(buf, sizeof(buf));
91 buf[sizeof(buf)-1] = 0;
95 sprintf(buf, "%02d", tm->tm_hour);
98 sprintf(buf, "%02d", tm->tm_mon + 1);
101 sprintf(buf, "%02d", tm->tm_min);
103 case 'N': /* block device name */
104 cp = strrchr(ctx->filesystem_name, '/');
108 cp = ctx->filesystem_name;
109 append_string(s, cp, 0);
112 sprintf(buf, "%lu", (unsigned long) getpid());
115 sprintf(buf, "%lu", (unsigned long) ctx->now);
118 sprintf(buf, "%02d", tm->tm_sec);
121 sprintf(buf, "%02d%02d%02d", tm->tm_hour, tm->tm_min,
126 strcpy(buf, "tytso");
129 #ifdef HAVE_GETPWUID_R
130 getpwuid_r(getuid(), &pw_struct, buf, sizeof(buf), &pw);
132 pw = getpwuid(getuid());
135 append_string(s, pw->pw_name, 0);
142 sprintf(buf, "%02d", tm->tm_year % 100);
145 sprintf(buf, "%d", tm->tm_year + 1900);
148 append_string(s, buf, 0);
151 static void expand_logfn(e2fsck_t ctx, const char *log_fn, struct string *s)
157 alloc_string(s, 100);
158 for (cp = log_fn; *cp; cp++) {
161 expand_percent_expression(ctx, *cp, s, &flags);
164 for (i = 0; cp[i]; i++)
167 append_string(s, cp, i);
172 static int outbufsize;
175 static int do_read(int fd)
181 c = read(fd, buffer, sizeof(buffer)-1);
185 n = realloc(outbuf, outbufsize + c);
188 memcpy(((char *)outbuf)+outbufsize, buffer, c);
195 * Fork a child process to save the output of the logfile until the
196 * appropriate file system is mounted read/write.
198 static FILE *save_output(const char *s0, const char *s1, const char *s2)
212 /* At least one potential output file name is valid */
213 if (!s0 && !s1 && !s2)
227 if (daemon(0, 0) < 0) {
232 * Grab the output from our parent
235 while (do_read(fds[0]) > 0)
239 /* OK, now let's try to open the output file */
243 fd = open(s0, O_WRONLY|O_CREAT|O_TRUNC, 0644);
245 fd = open(s1, O_WRONLY|O_CREAT|O_TRUNC, 0644);
247 fd = open(s2, O_WRONLY|O_CREAT|O_TRUNC, 0644);
254 while (outbufsize > 0) {
255 c = write(fd, cp, outbufsize);
257 if ((errno == EAGAIN) || (errno == EINTR))
268 ret = fdopen(fds[1], "w");
275 void set_up_logging(e2fsck_t ctx)
277 struct string s, s1, s2;
278 char *s0 = 0, *log_dir = 0, *log_fn = 0;
279 int log_dir_wait = 0;
281 s.s = s1.s = s2.s = 0;
283 profile_get_boolean(ctx->profile, "options", "log_dir_wait", 0, 0,
286 log_fn = string_copy(ctx, ctx->log_fn, 0);
288 profile_get_string(ctx->profile, "options", "log_filename",
290 profile_get_string(ctx->profile, "options", "log_dir", 0, 0, &log_dir);
292 if (!log_fn || !log_fn[0])
295 expand_logfn(ctx, log_fn, &s);
296 if ((log_fn[0] == '/') || !log_dir || !log_dir[0])
299 if (log_dir && log_dir[0]) {
300 alloc_string(&s1, strlen(log_dir) + strlen(s.s) + 2);
301 append_string(&s1, log_dir, 0);
302 append_string(&s1, "/", 1);
303 append_string(&s1, s.s, 0);
307 profile_get_string(ctx->profile, "options", "log_dir_fallback", 0, 0,
309 if (log_dir && log_dir[0]) {
310 alloc_string(&s2, strlen(log_dir) + strlen(s.s) + 2);
311 append_string(&s2, log_dir, 0);
312 append_string(&s2, "/", 1);
313 append_string(&s2, s.s, 0);
314 printf("%s\n", s2.s);
318 ctx->logf = fopen(s0, "w");
319 if (!ctx->logf && s1.s)
320 ctx->logf = fopen(s1.s, "w");
321 if (!ctx->logf && s2.s)
322 ctx->logf = fopen(s2.s, "w");
323 if (!ctx->logf && log_dir_wait)
324 ctx->logf = save_output(s0, s1.s, s2.s);
335 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
336 const char *description)
343 sprintf(buf, "Can't allocate %s\n", description);
346 memset(ret, 0, size);
350 errcode_t e2fsck_allocate_context(e2fsck_t *ret)
356 context = malloc(sizeof(struct e2fsck_struct));
360 memset(context, 0, sizeof(struct e2fsck_struct));
362 context->now = 1332006474;
364 context->filesystem_name = "/dev/sda3";
365 context->device_name = "fslabel";
371 int main(int argc, char **argv)
376 putenv("TZ=EST+5:00");
377 e2fsck_allocate_context(&ctx);
378 expand_logfn(ctx, "e2fsck-%N.%h.%u.%D-%T", &s);
381 expand_logfn(ctx, "e2fsck-%N.%h.%u.%Y%m%d-%H%M%S", &s);