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