Contents of /trunk/grubby/log.c
Parent Directory | Revision Log
Revision 2236 -
(show annotations)
(download)
Mon Oct 21 13:19:07 2013 UTC (11 years ago) by niro
File MIME type: text/plain
File size: 2186 byte(s)
Mon Oct 21 13:19:07 2013 UTC (11 years ago) by niro
File MIME type: text/plain
File size: 2186 byte(s)
Add logging when things fail.
1 | /* |
2 | * log.c |
3 | * |
4 | * Copyright 2013 Red Hat, Inc. |
5 | * All rights reserved. |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | */ |
20 | |
21 | #ifndef _GNU_SOURCE |
22 | #define _GNU_SOURCE |
23 | #endif |
24 | |
25 | #include <errno.h> |
26 | #include <fcntl.h> |
27 | #include <stdarg.h> |
28 | #include <stdio.h> |
29 | #include <string.h> |
30 | #include <sys/types.h> |
31 | #include <sys/stat.h> |
32 | #include <time.h> |
33 | #include <unistd.h> |
34 | |
35 | #include "log.h" |
36 | |
37 | static int log_fd = -1; |
38 | static FILE *f = NULL; |
39 | |
40 | static int |
41 | open_log(void) |
42 | { |
43 | if (log_fd > -1) |
44 | return 0; |
45 | log_fd = open("/var/log/grubby", O_RDWR|O_APPEND|O_CREAT|O_CLOEXEC, 0600); |
46 | if (log_fd < 0) |
47 | return log_fd; |
48 | |
49 | f = fdopen(log_fd, "a+"); |
50 | if (f == NULL) { |
51 | typeof(errno) saved_errno = errno; |
52 | close(log_fd); |
53 | log_fd = -1; |
54 | errno = saved_errno; |
55 | return -1; |
56 | } |
57 | |
58 | setbuf(f, NULL); |
59 | return 0; |
60 | } |
61 | |
62 | int |
63 | log_time(FILE *log) |
64 | { |
65 | if (!log) { |
66 | int rc = open_log(); |
67 | if (rc < 0) |
68 | return rc; |
69 | } |
70 | |
71 | time_t t = time(NULL); |
72 | char timestr[27]; |
73 | |
74 | ctime_r(&t, timestr); |
75 | timestr[26] = '\0'; |
76 | for (int i = 26; i >= 0; i--) |
77 | if (timestr[i] == '\n') |
78 | timestr[i] = '\0'; |
79 | |
80 | return log_message(log, "DBG: %d: %s: ", getpid(), timestr); |
81 | } |
82 | |
83 | int |
84 | log_vmessage(FILE *log, const char *msg, va_list ap) |
85 | { |
86 | int rc; |
87 | |
88 | if (!msg) |
89 | return -1; |
90 | if (msg[0] == '\0') |
91 | return 0; |
92 | |
93 | if (!log) { |
94 | rc = open_log(); |
95 | if (rc < 0) |
96 | return rc; |
97 | } |
98 | |
99 | va_list aq; |
100 | va_copy(aq, ap); |
101 | |
102 | vfprintf(log ? log : f, msg, aq); |
103 | va_end(aq); |
104 | fdatasync(log ? fileno(log) : log_fd); |
105 | |
106 | return 0; |
107 | } |
108 | |
109 | int |
110 | log_message(FILE *log, const char *msg, ...) |
111 | { |
112 | va_list argp; |
113 | |
114 | va_start(argp, msg); |
115 | int rc = log_vmessage(log, msg, argp); |
116 | va_end(argp); |
117 | return rc; |
118 | } |