forked from bitstreamout/showconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libblogger.c
149 lines (129 loc) · 3.14 KB
/
libblogger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* libblogger.c
*
* Copyright 2000 Werner Fink, 2000 SuSE GmbH Nuernberg, Germany.
*
* This source is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef _PATH_BLOG_FIFO
# define _PATH_BLOG_FIFO "/dev/blog"
#endif
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "libblogger.h"
#include "libconsole.h"
/*
* Use Esacape sequences which are handled by console
* driver of linux kernel but not used. We transport
* with this our informations to the parser of blogd
* whereas the linux console throw them away.
* Problem: Does not work on serial console.
*/
#define ESNN "notice" /* Notice */
#define ESND "done" /* Done */
#define ESNF "failed" /* Failed */
#define ESNS "skipped" /* Skipped */
#define ESNU "unused" /* Unused */
static struct sigaction saved_sigpipe;
static volatile sig_atomic_t broken = 0;
static int fdfifo = -1;
static char * fifo_name = _PATH_BLOG_FIFO;
static void sigpipe(int sig __attribute__((__unused__)))
{
broken++;
}
static int bootlog_init(const int lvl __attribute__((__unused__)))
{
int ret = -1;
struct stat st;
if (stat(fifo_name, &st))
goto out;
if (!S_ISFIFO(st.st_mode))
goto out;
if ((fdfifo = open(fifo_name, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC)) < 0)
goto out;
set_signal(SIGPIPE, &saved_sigpipe, sigpipe);
ret = 0;
out:
return ret;
}
void closeblog()
{
if (fdfifo < 0)
goto out;
reset_signal(SIGPIPE, &saved_sigpipe);
(void)close(fdfifo);
out:
return;
}
int bootlog(const int lvl, const char *fmt, ...)
{
va_list ap;
int ret = -1;
char * head = ESNN;
char buf[4096];
sigset_t blockpipe, oldpipe;
if (fdfifo < 0 && bootlog_init(lvl) < 0)
goto out;
ret = 0;
switch (lvl) {
case -1:
head = NULL;
break;
case B_NOTICE:
head = ESNN;
break;
case B_DONE:
head = ESND;
break;
case B_FAILED:
head = ESNF;
break;
case B_SKIPPED:
head = ESNS;
break;
case B_UNUSED:
head = ESNU;
break;
default:
head = ESNN;
break;
}
sigprocmask(SIG_BLOCK, &blockpipe, &oldpipe);
if (broken)
goto pipe;
if (head) {
const struct tm *local;
struct timeval now;
char stamp[72], strnow[56];
if ((gettimeofday(&now, (struct timezone*)0) == 0) &&
((local = localtime(&now.tv_sec)) != (struct tm*)0) &&
(strftime(strnow, sizeof(strnow), "%b %e %T", local) > 0))
snprintf(stamp, sizeof(stamp), "<%s -- %s.%lu> ", head, strnow, now.tv_usec*1000UL);
else
snprintf(stamp, sizeof(stamp), "<%s> ", head);
ret = write(fdfifo, stamp, strlen(stamp));
}
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
ret = write(fdfifo, buf, strlen(buf));
pipe:
sigprocmask(SIG_SETMASK, &oldpipe, NULL);
out:
return ret;
}