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
|
#include <u.h>
#include <libc.h>
static struct
{
int fd;
int consfd;
char *name;
Dir *d;
Dir *consd;
Lock lk;
} sl =
{
-1, -1,
};
static void
_syslogopen(void)
{
char buf[1024], *p;
if(sl.fd >= 0)
close(sl.fd);
snprint(buf, sizeof(buf), "#9/log/%s", sl.name);
p = unsharp(buf);
sl.fd = open(p, OWRITE|OCEXEC|OAPPEND);
free(p);
}
/*
* Print
* sysname: time: mesg
* on /sys/log/logname.
* If cons or log file can't be opened, print on the system console, too.
*/
void
syslog(int cons, char *logname, char *fmt, ...)
{
char buf[1024];
char *ctim, *p;
va_list arg;
int n;
Dir *d;
char err[ERRMAX];
err[0] = '\0';
errstr(err, sizeof err);
lock(&sl.lk);
/*
* paranoia makes us stat to make sure a fork+close
* hasn't broken our fd's
*/
d = dirfstat(sl.fd);
if(sl.fd < 0
|| sl.name == nil
|| strcmp(sl.name, logname)!=0
|| sl.d == nil
|| d == nil
|| d->dev != sl.d->dev
|| d->type != sl.d->type
|| d->qid.path != sl.d->qid.path){
free(sl.name);
sl.name = strdup(logname);
if(sl.name == nil)
cons = 1;
else{
_syslogopen();
if(sl.fd < 0)
cons = 1;
free(sl.d);
sl.d = d;
d = nil; /* don't free it */
}
}
free(d);
if(cons){
d = dirfstat(sl.consfd);
if(sl.consfd < 0
|| d == nil
|| sl.consd == nil
|| d->dev != sl.consd->dev
|| d->type != sl.consd->type
|| d->qid.path != sl.consd->qid.path){
sl.consfd = open("/dev/tty", OWRITE|OCEXEC);
free(sl.consd);
sl.consd = d;
d = nil; /* don't free it */
}
free(d);
}
if(fmt == nil){
unlock(&sl.lk);
return;
}
ctim = ctime(time(0));
werrstr(err);
p = buf + snprint(buf, sizeof(buf)-1, "%s ", sysname());
strncpy(p, ctim+4, 15);
p += 15;
*p++ = ' ';
va_start(arg, fmt);
p = vseprint(p, buf+sizeof(buf)-1, fmt, arg);
va_end(arg);
*p++ = '\n';
n = p - buf;
if(sl.fd >= 0){
seek(sl.fd, 0, 2);
write(sl.fd, buf, n);
}
if(cons && sl.consfd >=0)
write(sl.consfd, buf, n);
unlock(&sl.lk);
}
|