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
15 #include <sys/types.h>
28 static void alloc_string(struct string *s, int len)
31 /* e2fsck_allocate_memory(ctx, len, "logfile name"); */
36 static void append_string(struct string *s, const char *a, int len)
43 needlen = s->end + len + 1;
44 if (needlen > s->len) {
47 if (s->len * 2 > needlen)
49 n = realloc(s->s, needlen);
55 /* Don't append if we ran out of memory */
59 memcpy(s->s + s->end, a, len);
64 #define FLAG_UTC 0x0001
66 static void expand_percent_expression(e2fsck_t ctx, char ch,
67 struct string *s, int *flags)
69 struct tm *tm = NULL, tm_struct;
70 struct passwd *pw = NULL, pw_struct;
74 if ((ch == 'D') || (ch == 'd') || (ch == 'm') || (ch == 'y') ||
76 (ch == 'T') || (ch == 'H') || (ch == 'M') || (ch == 'S')) {
78 tm = (*flags & FLAG_UTC) ? gmtime_r(&ctx->now, &tm_struct) :
79 localtime_r(&ctx->now, &tm_struct);
84 append_string(s, "%", 1);
87 sprintf(buf, "%02d", tm->tm_mday);
90 sprintf(buf, "%d%02d%02d", tm->tm_year + 1900, tm->tm_mon + 1,
95 strcpy(buf, "server");
98 gethostname(buf, sizeof(buf));
99 buf[sizeof(buf)-1] = 0;
103 sprintf(buf, "%02d", tm->tm_hour);
106 sprintf(buf, "%02d", tm->tm_mon + 1);
109 sprintf(buf, "%02d", tm->tm_min);
111 case 'N': /* block device name */
112 cp = strrchr(ctx->filesystem_name, '/');
116 cp = ctx->filesystem_name;
117 append_string(s, cp, 0);
120 sprintf(buf, "%lu", (unsigned long) getpid());
123 sprintf(buf, "%lu", (unsigned long) ctx->now);
126 sprintf(buf, "%02d", tm->tm_sec);
129 sprintf(buf, "%02d%02d%02d", tm->tm_hour, tm->tm_min,
134 strcpy(buf, "tytso");
137 #ifdef HAVE_GETPWUID_R
138 getpwuid_r(getuid(), &pw_struct, buf, sizeof(buf), &pw);
140 pw = getpwuid(getuid());
143 append_string(s, pw->pw_name, 0);
150 sprintf(buf, "%02d", tm->tm_year % 100);
153 sprintf(buf, "%d", tm->tm_year + 1900);
156 append_string(s, buf, 0);
159 static void expand_logfn(e2fsck_t ctx, const char *log_fn, struct string *s)
165 alloc_string(s, 100);
166 for (cp = log_fn; *cp; cp++) {
169 expand_percent_expression(ctx, *cp, s, &flags);
172 for (i = 0; cp[i]; i++)
175 append_string(s, cp, i);
180 static int outbufsize;
183 static int do_read(int fd)
189 c = read(fd, buffer, sizeof(buffer)-1);
193 n = realloc(outbuf, outbufsize + c);
196 memcpy(((char *)outbuf)+outbufsize, buffer, c);
203 * Fork a child process to save the output of the logfile until the
204 * appropriate file system is mounted read/write.
206 static FILE *save_output(const char *s0, const char *s1, const char *s2)
220 /* At least one potential output file name is valid */
221 if (!s0 && !s1 && !s2)
235 if (daemon(0, 0) < 0) {
240 * Grab the output from our parent
243 while (do_read(fds[0]) > 0)
247 /* OK, now let's try to open the output file */
251 fd = open(s0, O_WRONLY|O_CREAT|O_TRUNC, 0644);
253 fd = open(s1, O_WRONLY|O_CREAT|O_TRUNC, 0644);
255 fd = open(s2, O_WRONLY|O_CREAT|O_TRUNC, 0644);
262 while (outbufsize > 0) {
263 c = write(fd, cp, outbufsize);
265 if ((errno == EAGAIN) || (errno == EINTR))
276 ret = fdopen(fds[1], "w");
283 void set_up_logging(e2fsck_t ctx)
285 struct string s, s1, s2;
286 char *s0 = 0, *log_dir = 0, *log_fn = 0;
287 int log_dir_wait = 0;
289 s.s = s1.s = s2.s = 0;
291 profile_get_boolean(ctx->profile, "options", "log_dir_wait", 0, 0,
294 log_fn = string_copy(ctx, ctx->log_fn, 0);
296 profile_get_string(ctx->profile, "options", "log_filename",
298 profile_get_string(ctx->profile, "options", "log_dir", 0, 0, &log_dir);
300 if (!log_fn || !log_fn[0])
303 expand_logfn(ctx, log_fn, &s);
304 if ((log_fn[0] == '/') || !log_dir || !log_dir[0])
307 if (log_dir && log_dir[0]) {
308 alloc_string(&s1, strlen(log_dir) + strlen(s.s) + 2);
309 append_string(&s1, log_dir, 0);
310 append_string(&s1, "/", 1);
311 append_string(&s1, s.s, 0);
315 profile_get_string(ctx->profile, "options", "log_dir_fallback", 0, 0,
317 if (log_dir && log_dir[0]) {
318 alloc_string(&s2, strlen(log_dir) + strlen(s.s) + 2);
319 append_string(&s2, log_dir, 0);
320 append_string(&s2, "/", 1);
321 append_string(&s2, s.s, 0);
322 printf("%s\n", s2.s);
326 ctx->logf = fopen(s0, "w");
327 if (!ctx->logf && s1.s)
328 ctx->logf = fopen(s1.s, "w");
329 if (!ctx->logf && s2.s)
330 ctx->logf = fopen(s2.s, "w");
331 if (!ctx->logf && log_dir_wait)
332 ctx->logf = save_output(s0, s1.s, s2.s);
343 void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
344 const char *description)
351 sprintf(buf, "Can't allocate %s\n", description);
354 memset(ret, 0, size);
358 errcode_t e2fsck_allocate_context(e2fsck_t *ret)
364 context = malloc(sizeof(struct e2fsck_struct));
368 memset(context, 0, sizeof(struct e2fsck_struct));
370 context->now = 1332006474;
372 context->filesystem_name = "/dev/sda3";
373 context->device_name = "fslabel";
379 int main(int argc, char **argv)
384 putenv("TZ=EST+5:00");
385 e2fsck_allocate_context(&ctx);
386 expand_logfn(ctx, "e2fsck-%N.%h.%u.%D-%T", &s);
389 expand_logfn(ctx, "e2fsck-%N.%h.%u.%Y%m%d-%H%M%S", &s);