Skip to content

Commit 5a05941

Browse files
committed
Write log messages to kernel log
This makes them accessible after boot and it provides time stamps that can be useful for rudimentary profiling. Note that the kernel log is also echoed on the console during early boot, so the only thing we lose is the coloring of the log messages. The syslog priority markers could be computed, but not without runtime overhead or very nasty build-time hacks, so I decided to hardcode them. I also tweaked the messages themselves a bit.
1 parent 6aada97 commit 5a05941

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

debug.h

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
21
#ifndef DEBUG_H
32
#define DEBUG_H
43

54
#include <stdio.h>
65

6+
// Compile time log filter:
7+
78
#define NODEBUG_L 0
89
#define ERROR_L 1
910
#define WARNING_L 2
@@ -16,64 +17,34 @@
1617

1718
// -------------
1819

19-
#ifndef COLOR_DEBUG
20-
#define COLOR_DEBUG "\e[0;34m"
21-
#endif
22-
#ifndef COLOR_WARNING
23-
#define COLOR_WARNING "\e[01;33m"
24-
#endif
25-
#ifndef COLOR_ERROR
26-
#define COLOR_ERROR "\e[01;31m"
27-
#endif
20+
extern FILE *logfile;
2821

29-
#define COLOR_END "\e[0m"
22+
#define OUTPUT_LOG_MSG(LEVEL, ...) do { \
23+
fprintf(logfile, LEVEL "mininit: " __VA_ARGS__); \
24+
} while (0)
3025

31-
#if (LOG_LEVEL >= DEBUG_L)
32-
# ifdef COLOR_DEBUG
33-
# define DEBUG(str, ...) \
34-
fprintf(stdout, COLOR_DEBUG "DEBUG: " str COLOR_END, ##__VA_ARGS__)
35-
# else
36-
# define DEBUG(...) \
37-
fprintf(stdout, "DEBUG: " __VA_ARGS__)
38-
# endif
26+
#if LOG_LEVEL >= DEBUG_L
27+
#define DEBUG(...) OUTPUT_LOG_MSG("<15>", __VA_ARGS__)
3928
#else
4029
#define DEBUG(...)
4130
#endif
4231

43-
#if (LOG_LEVEL >= INFO_L)
44-
# ifdef COLOR_INFO
45-
# define INFO(str, ...) \
46-
fprintf(stdout, COLOR_INFO str COLOR_END, ##__VA_ARGS__)
47-
# else
48-
# define INFO(...) \
49-
fprintf(stdout, __VA_ARGS__)
50-
# endif
32+
#if LOG_LEVEL >= INFO_L
33+
#define INFO(...) OUTPUT_LOG_MSG("<14>", __VA_ARGS__)
5134
#else
5235
#define INFO(...)
5336
#endif
5437

55-
#if (LOG_LEVEL >= WARNING_L)
56-
# ifdef COLOR_WARNING
57-
# define WARNING(str, ...) \
58-
fprintf(stderr, COLOR_WARNING "WARNING: " str COLOR_END, ##__VA_ARGS__)
59-
# else
60-
# define WARNING(...) \
61-
fprintf(stderr, "WARNING: " __VA_ARGS__)
62-
# endif
38+
#if LOG_LEVEL >= WARNING_L
39+
#define WARNING(...) OUTPUT_LOG_MSG("<12>", "WARNING: " __VA_ARGS__)
6340
#else
6441
#define WARNING(...)
6542
#endif
6643

67-
#if (LOG_LEVEL >= ERROR_L)
68-
# ifdef COLOR_ERROR
69-
# define ERROR(str, ...) \
70-
fprintf(stderr, COLOR_ERROR "ERROR: " str COLOR_END, ##__VA_ARGS__)
71-
# else
72-
# define ERROR(...) \
73-
fprintf(stderr, "ERROR: " __VA_ARGS__)
74-
# endif
44+
#if LOG_LEVEL >= ERROR_L
45+
#define ERROR(...) OUTPUT_LOG_MSG("<11>", "ERROR: " __VA_ARGS__)
7546
#else
7647
#define ERROR(...)
7748
#endif
7849

79-
#endif
50+
#endif // DEBUG_H

mininit.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,30 @@ void perform_updates(bool is_backup)
8383
}
8484

8585

86+
FILE *logfile;
87+
8688
int main(int argc, char **argv, char **envp)
8789
{
88-
INFO("OpenDingux mininit 1.1.0 "
89-
"by Ignacio Garcia Perez <[email protected]>, "
90-
"Paul Cercueil <[email protected]> and "
91-
"Maarten ter Huurne <[email protected]>\n");
90+
logfile = stderr;
9291

9392
/* Mount devtmpfs to get a full set of device nodes. */
94-
DEBUG("Mounting /dev\n");
9593
if (mount("devtmpfs", "/dev", "devtmpfs", 0, NULL)) {
9694
INFO("Couldn't mount devtmpfs on /dev: %d\n", errno);
9795
/* If there are sufficient static device nodes in the fs containing
9896
* mininit, we can boot without devtmpfs, so don't give up yet. */
9997
}
10098

99+
/* Write our log messages to the kernel log. */
100+
FILE *kmsg = fopen("/dev/kmsg", "w");
101+
if (kmsg) {
102+
setlinebuf(kmsg);
103+
logfile = kmsg;
104+
}
105+
INFO("OpenDingux mininit 1.1.0\n");
106+
if (!kmsg) {
107+
WARNING("Failed to open '/dev/kmsg': %d\n", errno);
108+
}
109+
101110
/* Look for "rootfs_bak" parameter. */
102111
bool is_backup = false;
103112
for (int i = 1; i < argc; i++) {
@@ -137,10 +146,12 @@ int main(int argc, char **argv, char **envp)
137146
losetup(loop_dev, rootfs_img);
138147

139148
/* Mount the loop device that was just set up. */
149+
DEBUG("Loop-mounting '%s' on '/root'\n", rootfs_img);
140150
if (mount(loop_dev, "/root", ROOTFS_TYPE, MS_RDONLY, NULL)) {
141151
ERROR("Failed to mount the rootfs image: %d\n", errno);
142152
return -1;
143153
}
154+
INFO("%s mounted on /root\n", rootfs_img);
144155

145156
/* Make the freshly mounted rootfs image the working directory. */
146157
if (chdir("/root")) {
@@ -190,6 +201,8 @@ int main(int argc, char **argv, char **envp)
190201
return -1;
191202
}
192203

204+
INFO("root switch done\n");
205+
193206
/* Clean up the initramfs and then release it. */
194207
if (fd >= 0) {
195208
DEBUG("Removing initramfs contents\n");
@@ -228,15 +241,21 @@ int main(int argc, char **argv, char **envp)
228241
ERROR("Unable to find the 'init' executable\n");
229242
return -1;
230243
}
244+
DEBUG("Checking for 'init' executable: %s\n", inits[i]);
231245
if (!access(inits[i], X_OK)) {
232-
DEBUG("Found 'init' executable: %s\n", inits[i]);
233246
argv[0] = (char *)inits[i];
234247
break;
235248
}
236249
}
250+
INFO("starting %s\n", argv[0]);
251+
252+
if (kmsg) {
253+
logfile = stderr;
254+
fclose(kmsg);
255+
}
237256

238257
/* Execute the 'init' executable */
239258
execvpe(argv[0], argv, envp);
240259
ERROR("Exec of 'init' failed: %d\n", errno);
241-
return 0;
260+
return -1;
242261
}

splashkill.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ static int waitForEnter()
4747
return 0;
4848
}
4949

50+
FILE *logfile;
51+
5052
int main(int argc, char **argv)
5153
{
54+
logfile = stderr;
55+
5256
signal(SIGINT, &quit_hdl);
5357
signal(SIGSEGV, &quit_hdl);
5458
signal(SIGTERM, &quit_hdl);

0 commit comments

Comments
 (0)