8e64a0028dfb3dd4bce2b077565018d61400295e
[p5sagit/p5-mst-13.2.git] / lib / syslog.pl
1 #
2 # syslog.pl
3 #
4 # $Log: syslog.pl,v $
5 # Revision 4.1  92/08/07  18:24:15  lwall
6
7 # Revision 4.0.1.1  92/06/08  13:48:05  lwall
8 # patch20: new warning for ambiguous use of unary operators
9
10 # Revision 4.0  91/03/20  01:26:24  lwall
11 # 4.0 baseline.
12
13 # Revision 3.0.1.4  90/11/10  01:41:11  lwall
14 # patch38: syslog.pl was referencing an absolute path
15
16 # Revision 3.0.1.3  90/10/15  17:42:18  lwall
17 # patch29: various portability fixes
18
19 # Revision 3.0.1.1  90/08/09  03:57:17  lwall
20 # patch19: Initial revision
21
22 # Revision 1.2  90/06/11  18:45:30  18:45:30  root ()
23 # - Changed 'warn' to 'mail|warning' in test call (to give example of
24 #   facility specification, and because 'warn' didn't work on HP-UX).
25 # - Fixed typo in &openlog ("ncons" should be "cons").
26 # - Added (package-global) $maskpri, and &setlogmask.
27 # - In &syslog:
28 #   - put argument test ahead of &connect (why waste cycles?),
29 #   - allowed facility to be specified in &syslog's first arg (temporarily
30 #     overrides any $facility set in &openlog), just as in syslog(3C),
31 #   - do a return 0 when bit for $numpri not set in log mask (see syslog(3C)),
32 #   - changed $whoami code to use getlogin, getpwuid($<) and 'syslog'
33 #     (in that order) when $ident is null,
34 #   - made PID logging consistent with syslog(3C) and subject to $lo_pid only,
35 #   - fixed typo in "print CONS" statement ($<facility should be <$facility).
36 #   - changed \n to \r in print CONS (\r is useful, $message already has a \n).
37 # - Changed &xlate to return -1 for an unknown name, instead of croaking.
38
39 #
40 # tom christiansen <tchrist@convex.com>
41 # modified to use sockets by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
42 # NOTE: openlog now takes three arguments, just like openlog(3)
43 #
44 # call syslog() with a string priority and a list of printf() args
45 # like syslog(3)
46 #
47 #  usage: require 'syslog.pl';
48 #
49 #  then (put these all in a script to test function)
50 #               
51 #
52 #       do openlog($program,'cons,pid','user');
53 #       do syslog('info','this is another test');
54 #       do syslog('mail|warning','this is a better test: %d', time);
55 #       do closelog();
56 #       
57 #       do syslog('debug','this is the last test');
58 #       do openlog("$program $$",'ndelay','user');
59 #       do syslog('notice','fooprogram: this is really done');
60 #
61 #       $! = 55;
62 #       do syslog('info','problem was %m'); # %m == $! in syslog(3)
63
64 package syslog;
65
66 $host = 'localhost' unless $host;       # set $syslog'host to change
67
68 require 'syslog.ph';
69
70 $maskpri = &LOG_UPTO(&LOG_DEBUG);
71
72 sub main'openlog {
73     ($ident, $logopt, $facility) = @_;  # package vars
74     $lo_pid = $logopt =~ /\bpid\b/;
75     $lo_ndelay = $logopt =~ /\bndelay\b/;
76     $lo_cons = $logopt =~ /\bcons\b/;
77     $lo_nowait = $logopt =~ /\bnowait\b/;
78     &connect if $lo_ndelay;
79
80
81 sub main'closelog {
82     $facility = $ident = '';
83     &disconnect;
84
85
86 sub main'setlogmask {
87     local($oldmask) = $maskpri;
88     $maskpri = shift;
89     $oldmask;
90 }
91  
92 sub main'syslog {
93     local($priority) = shift;
94     local($mask) = shift;
95     local($message, $whoami);
96     local(@words, $num, $numpri, $numfac, $sum);
97     local($facility) = $facility;       # may need to change temporarily.
98
99     die "syslog: expected both priority and mask" unless $mask && $priority;
100
101     @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
102     undef $numpri;
103     undef $numfac;
104     foreach (@words) {
105         $num = &xlate($_);              # Translate word to number.
106         if (/^kern$/ || $num < 0) {
107             die "syslog: invalid level/facility: $_\n";
108         }
109         elsif ($num <= &LOG_PRIMASK) {
110             die "syslog: too many levels given: $_\n" if defined($numpri);
111             $numpri = $num;
112             return 0 unless &LOG_MASK($numpri) & $maskpri;
113         }
114         else {
115             die "syslog: too many facilities given: $_\n" if defined($numfac);
116             $facility = $_;
117             $numfac = $num;
118         }
119     }
120
121     die "syslog: level must be given\n" unless defined($numpri);
122
123     if (!defined($numfac)) {    # Facility not specified in this call.
124         $facility = 'user' unless $facility;
125         $numfac = &xlate($facility);
126     }
127
128     &connect unless $connected;
129
130     $whoami = $ident;
131
132     if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
133         $whoami = $1;
134         $mask = $2;
135     } 
136
137     unless ($whoami) {
138         ($whoami = getlogin) ||
139             ($whoami = getpwuid($<)) ||
140                 ($whoami = 'syslog');
141     }
142
143     $whoami .= "[$$]" if $lo_pid;
144
145     $mask =~ s/%m/$!/g;
146     $mask .= "\n" unless $mask =~ /\n$/;
147     $message = sprintf ($mask, @_);
148
149     $sum = $numpri + $numfac;
150     unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
151         if ($lo_cons) {
152             if ($pid = fork) {
153                 unless ($lo_nowait) {
154                     do {$died = wait;} until $died == $pid || $died < 0;
155                 }
156             }
157             else {
158                 open(CONS,">/dev/console");
159                 print CONS "<$facility.$priority>$whoami: $message\r";
160                 exit if defined $pid;           # if fork failed, we're parent
161                 close CONS;
162             }
163         }
164     }
165 }
166
167 sub xlate {
168     local($name) = @_;
169     $name =~ y/a-z/A-Z/;
170     $name = "LOG_$name" unless $name =~ /^LOG_/;
171     $name = "syslog'$name";
172     eval(&$name) || -1;
173 }
174
175 sub connect {
176     $pat = 'S n C4 x8';
177
178     $af_unix = 1;
179     $af_inet = 2;
180
181     $stream = 1;
182     $datagram = 2;
183
184     ($name,$aliases,$proto) = getprotobyname('udp');
185     $udp = $proto;
186
187     ($name,$aliase,$port,$proto) = getservbyname('syslog','udp');
188     $syslog = $port;
189
190     if (chop($myname = `hostname`)) {
191         ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
192         die "Can't lookup $myname\n" unless $name;
193         @bytes = unpack("C4",$addrs[0]);
194     }
195     else {
196         @bytes = (0,0,0,0);
197     }
198     $this = pack($pat, $af_inet, 0, @bytes);
199
200     if ($host =~ /^\d+\./) {
201         @bytes = split(/\./,$host);
202     }
203     else {
204         ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
205         die "Can't lookup $host\n" unless $name;
206         @bytes = unpack("C4",$addrs[0]);
207     }
208     $that = pack($pat,$af_inet,$syslog,@bytes);
209
210     socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
211     bind(SYSLOG,$this) || die "bind: $!\n";
212     connect(SYSLOG,$that) || die "connect: $!\n";
213
214     local($old) = select(SYSLOG); $| = 1; select($old);
215     $connected = 1;
216 }
217
218 sub disconnect {
219     close SYSLOG;
220     $connected = 0;
221 }
222
223 1;