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