-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
91 lines (73 loc) · 1.51 KB
/
log.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
/* SPDX-License-Identifier: MIT */
#include "config.h"
#define SYSLOG_NAMES
#include "log.h"
#include <libite/lite.h>
#include <err.h>
#include <stdarg.h>
#include <stdlib.h>
struct syslog_data sd = SYSLOG_DATA_INIT;
extern char *ident;
/* Dumpster dive in container for process ID */
static void dumpster_dive(int *log_pid)
{
char buf[512];
char *docker;
FILE *fp;
docker = which("podman");
if (!docker)
docker = which("docker");
if (!docker)
goto err;
snprintf(buf, sizeof(buf), "%s inspect %s", docker, ident);
fp = popen(buf, "r");
if (fp) {
int state = 0;
while (fgets(buf, sizeof(buf), fp)) {
char *ptr = chomp(buf);
if (!state) {
if (strstr(buf, "\"State\":"))
state = 1;
continue;
}
ptr = strstr(buf, "\"Pid\":");
if (!ptr)
continue;
ptr += 6;
*log_pid = atoi(ptr);
break;
}
pclose(fp);
if (*log_pid != -1)
return;
}
err:
*log_pid = getpid();
}
void log_open(const char *ident, int option, int facility)
{
openlog_r(ident, option, facility, &sd);
}
void log_close(void)
{
closelog_r(&sd);
}
void logit(int severity, const char *fmt, ...)
{
va_list ap;
if (sd.log_pid == -1)
dumpster_dive(&sd.log_pid);
va_start(ap, fmt);
vsyslogp_r(severity, &sd, NULL, NULL, fmt, ap);
va_end(ap);
}
int log_facility(const char *arg)
{
for (size_t i = 0; facilitynames[i].c_name; i++) {
if (strcmp(facilitynames[i].c_name, arg))
continue;
return facilitynames[i].c_val;
}
warnx("unknown facility '%s', falling back to 'user'", arg);
return LOG_USER;
}