home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / unixtool.zip / TOOLS / lib / perl / syslog.pl < prev    next >
Perl Script  |  2000-04-21  |  6KB  |  231 lines

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