Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.6 KiB

  1. #
  2. # syslog.pl
  3. #
  4. # $Log: syslog.pl,v $
  5. #
  6. # tom christiansen <[email protected]>
  7. # modified to use sockets by Larry Wall <[email protected]>
  8. # NOTE: openlog now takes three arguments, just like openlog(3)
  9. #
  10. # call syslog() with a string priority and a list of printf() args
  11. # like syslog(3)
  12. #
  13. # usage: require 'syslog.pl';
  14. #
  15. # then (put these all in a script to test function)
  16. #
  17. #
  18. # do openlog($program,'cons,pid','user');
  19. # do syslog('info','this is another test');
  20. # do syslog('mail|warning','this is a better test: %d', time);
  21. # do closelog();
  22. #
  23. # do syslog('debug','this is the last test');
  24. # do openlog("$program $$",'ndelay','user');
  25. # do syslog('notice','fooprogram: this is really done');
  26. #
  27. # $! = 55;
  28. # do syslog('info','problem was %m'); # %m == $! in syslog(3)
  29. package syslog;
  30. $host = 'localhost' unless $host; # set $syslog'host to change
  31. if ($] >= 5) {
  32. warn "You should 'use Sys::Syslog' instead; continuing" # if $^W
  33. }
  34. require 'syslog.ph';
  35. eval 'use Socket; 1' ||
  36. eval { require "socket.ph" } ||
  37. require "sys/socket.ph";
  38. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  39. sub main'openlog {
  40. ($ident, $logopt, $facility) = @_; # package vars
  41. $lo_pid = $logopt =~ /\bpid\b/;
  42. $lo_ndelay = $logopt =~ /\bndelay\b/;
  43. $lo_cons = $logopt =~ /\bcons\b/;
  44. $lo_nowait = $logopt =~ /\bnowait\b/;
  45. &connect if $lo_ndelay;
  46. }
  47. sub main'closelog {
  48. $facility = $ident = '';
  49. &disconnect;
  50. }
  51. sub main'setlogmask {
  52. local($oldmask) = $maskpri;
  53. $maskpri = shift;
  54. $oldmask;
  55. }
  56. sub main'syslog {
  57. local($priority) = shift;
  58. local($mask) = shift;
  59. local($message, $whoami);
  60. local(@words, $num, $numpri, $numfac, $sum);
  61. local($facility) = $facility; # may need to change temporarily.
  62. die "syslog: expected both priority and mask" unless $mask && $priority;
  63. @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  64. undef $numpri;
  65. undef $numfac;
  66. foreach (@words) {
  67. $num = &xlate($_); # Translate word to number.
  68. if (/^kern$/ || $num < 0) {
  69. die "syslog: invalid level/facility: $_\n";
  70. }
  71. elsif ($num <= &LOG_PRIMASK) {
  72. die "syslog: too many levels given: $_\n" if defined($numpri);
  73. $numpri = $num;
  74. return 0 unless &LOG_MASK($numpri) & $maskpri;
  75. }
  76. else {
  77. die "syslog: too many facilities given: $_\n" if defined($numfac);
  78. $facility = $_;
  79. $numfac = $num;
  80. }
  81. }
  82. die "syslog: level must be given\n" unless defined($numpri);
  83. if (!defined($numfac)) { # Facility not specified in this call.
  84. $facility = 'user' unless $facility;
  85. $numfac = &xlate($facility);
  86. }
  87. &connect unless $connected;
  88. $whoami = $ident;
  89. if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  90. $whoami = $1;
  91. $mask = $2;
  92. }
  93. unless ($whoami) {
  94. ($whoami = getlogin) ||
  95. ($whoami = getpwuid($<)) ||
  96. ($whoami = 'syslog');
  97. }
  98. $whoami .= "[$$]" if $lo_pid;
  99. $mask =~ s/%m/$!/g;
  100. $mask .= "\n" unless $mask =~ /\n$/;
  101. $message = sprintf ($mask, @_);
  102. $sum = $numpri + $numfac;
  103. unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  104. if ($lo_cons) {
  105. if ($pid = fork) {
  106. unless ($lo_nowait) {
  107. do {$died = wait;} until $died == $pid || $died < 0;
  108. }
  109. }
  110. else {
  111. open(CONS,">/dev/console");
  112. print CONS "<$facility.$priority>$whoami: $message\r";
  113. exit if defined $pid; # if fork failed, we're parent
  114. close CONS;
  115. }
  116. }
  117. }
  118. }
  119. sub xlate {
  120. local($name) = @_;
  121. $name = uc $name;
  122. $name = "LOG_$name" unless $name =~ /^LOG_/;
  123. $name = "syslog'$name";
  124. defined &$name ? &$name : -1;
  125. }
  126. sub connect {
  127. $pat = 'S n C4 x8';
  128. $af_unix = &AF_UNIX;
  129. $af_inet = &AF_INET;
  130. $stream = &SOCK_STREAM;
  131. $datagram = &SOCK_DGRAM;
  132. ($name,$aliases,$proto) = getprotobyname('udp');
  133. $udp = $proto;
  134. ($name,$aliases,$port,$proto) = getservbyname('syslog','udp');
  135. $syslog = $port;
  136. if (chop($myname = `hostname`)) {
  137. ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
  138. die "Can't lookup $myname\n" unless $name;
  139. @bytes = unpack("C4",$addrs[0]);
  140. }
  141. else {
  142. @bytes = (0,0,0,0);
  143. }
  144. $this = pack($pat, $af_inet, 0, @bytes);
  145. if ($host =~ /^\d+\./) {
  146. @bytes = split(/\./,$host);
  147. }
  148. else {
  149. ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
  150. die "Can't lookup $host\n" unless $name;
  151. @bytes = unpack("C4",$addrs[0]);
  152. }
  153. $that = pack($pat,$af_inet,$syslog,@bytes);
  154. socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
  155. bind(SYSLOG,$this) || die "bind: $!\n";
  156. connect(SYSLOG,$that) || die "connect: $!\n";
  157. local($old) = select(SYSLOG); $| = 1; select($old);
  158. $connected = 1;
  159. }
  160. sub disconnect {
  161. close SYSLOG;
  162. $connected = 0;
  163. }
  164. 1;