-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslogd
executable file
·106 lines (94 loc) · 2.19 KB
/
syslogd
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
#!/bin/bash
CONFFILE=/etc/syslogd.conf
LOGDIR=/var/log
OPTIONS=()
PIDFILE=/run/syslogd.pid
RESTART=0
SYNC=0
classify() {
if (( LEVEL > 6 )); then
FACILITY=debug
elif [[ $FACILITY == auth* ]]; then
FACILITY=auth
elif [[ $FACILITY != @(kern|mail) ]]; then
FACILITY=daemon
fi
}
log() {
if [[ ! -d $LOGDIR/$FACILITY ]]; then
[[ $FACILITY == debug ]] && return
mkdir -m 0700 -p "$LOGDIR/$FACILITY"
fi
printf '%s %s\n' "$TIME" "$ENTRY" >>"$LOGDIR/$FACILITY/$DATE"
(( SYNC )) && sync "$LOGDIR/$FACILITY/$DATE"
}
usage() {
cat >&2 <<EOF
Usage: ${0##*/} [OPTIONS]
Options:
-b catch up with old kernel messages in the ring buffer
-d LOGDIR set the root log directory, /var/log by default
-f CONFFILE set the configuration file, /etc/syslogd.conf by default
-k relay syslog messages to the kernel ring buffer
-p PIDFILE set the pidfile location, /run/syslogd.pid by default
-s sync log files to disk after writing each entry
EOF
exit 64
}
while getopts :bd:f:kp:s OPTION; do
case $OPTION in
b)
OPTIONS+=('-b')
;;
d)
LOGDIR=$OPTARG
;;
f)
CONFFILE=$OPTARG
;;
k)
classify() (( PEERPID ))
log() {
printf '<%d>%s\n' $((FACILITY + LEVEL)) "$ENTRY" >>/dev/kmsg
}
OPTIONS+=('-n')
;;
p)
PIDFILE=$OPTARG
;;
s)
SYNC=1
;;
*)
usage
;;
esac
done
(( OPTIND <= $# )) && usage
if [[ -f $CONFFILE ]] && ! source "$CONFFILE"; then
exit 1
fi
if ! { exec 3>>"$PIDFILE" && flock -n 3; } 2>/dev/null; then
echo "Failed to lock $PIDFILE; is ${0##*/} already running?" >&2
exit 1
fi
if read -a STAT </proc/self/stat && (( $$ != STAT[5] )); then
if exec 3>&- && daemon -- "$0" "$@"; then
if timeout 2 daemon -w /dev/log; then
exit 0
fi
echo "Background syslogd failed to start" >&2
else
rm -f "$PIDFILE"
fi
exit 1
fi
echo $$ >"$PIDFILE"
trap 'trap "" TERM && kill -TERM 0 && rm -f "$PIDFILE"' EXIT
trap 'exec -- "$0" "$@"' HUP
if [[ ! -p /dev/stdin ]]; then
exec < <(syslog "${OPTIONS[@]}" 3>&-)
fi
while read -r PEERPID PEERUID PEERGID FACILITY LEVEL DATE TIME ENTRY; do
classify && log
done