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