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