Leaked source code of windows server 2003
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.

199 lines
4.9 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. use warnings::register;
  31. $host = 'localhost' unless $host; # set $syslog'host to change
  32. if ($] >= 5 && warnings::enabled()) {
  33. warnings::warn("You should 'use Sys::Syslog' instead; continuing");
  34. }
  35. require 'syslog.ph';
  36. eval 'use Socket; 1' ||
  37. eval { require "socket.ph" } ||
  38. require "sys/socket.ph";
  39. $maskpri = &LOG_UPTO(&LOG_DEBUG);
  40. sub main'openlog {
  41. ($ident, $logopt, $facility) = @_; # package vars
  42. $lo_pid = $logopt =~ /\bpid\b/;
  43. $lo_ndelay = $logopt =~ /\bndelay\b/;
  44. $lo_cons = $logopt =~ /\bcons\b/;
  45. $lo_nowait = $logopt =~ /\bnowait\b/;
  46. &connect if $lo_ndelay;
  47. }
  48. sub main'closelog {
  49. $facility = $ident = '';
  50. &disconnect;
  51. }
  52. sub main'setlogmask {
  53. local($oldmask) = $maskpri;
  54. $maskpri = shift;
  55. $oldmask;
  56. }
  57. sub main'syslog {
  58. local($priority) = shift;
  59. local($mask) = shift;
  60. local($message, $whoami);
  61. local(@words, $num, $numpri, $numfac, $sum);
  62. local($facility) = $facility; # may need to change temporarily.
  63. die "syslog: expected both priority and mask" unless $mask && $priority;
  64. @words = split(/\W+/, $priority, 2);# Allow "level" or "level|facility".
  65. undef $numpri;
  66. undef $numfac;
  67. foreach (@words) {
  68. $num = &xlate($_); # Translate word to number.
  69. if (/^kern$/ || $num < 0) {
  70. die "syslog: invalid level/facility: $_\n";
  71. }
  72. elsif ($num <= &LOG_PRIMASK) {
  73. die "syslog: too many levels given: $_\n" if defined($numpri);
  74. $numpri = $num;
  75. return 0 unless &LOG_MASK($numpri) & $maskpri;
  76. }
  77. else {
  78. die "syslog: too many facilities given: $_\n" if defined($numfac);
  79. $facility = $_;
  80. $numfac = $num;
  81. }
  82. }
  83. die "syslog: level must be given\n" unless defined($numpri);
  84. if (!defined($numfac)) { # Facility not specified in this call.
  85. $facility = 'user' unless $facility;
  86. $numfac = &xlate($facility);
  87. }
  88. &connect unless $connected;
  89. $whoami = $ident;
  90. if (!$ident && $mask =~ /^(\S.*):\s?(.*)/) {
  91. $whoami = $1;
  92. $mask = $2;
  93. }
  94. unless ($whoami) {
  95. ($whoami = getlogin) ||
  96. ($whoami = getpwuid($<)) ||
  97. ($whoami = 'syslog');
  98. }
  99. $whoami .= "[$$]" if $lo_pid;
  100. $mask =~ s/%m/$!/g;
  101. $mask .= "\n" unless $mask =~ /\n$/;
  102. $message = sprintf ($mask, @_);
  103. $sum = $numpri + $numfac;
  104. unless (send(SYSLOG,"<$sum>$whoami: $message",0)) {
  105. if ($lo_cons) {
  106. if ($pid = fork) {
  107. unless ($lo_nowait) {
  108. do {$died = wait;} until $died == $pid || $died < 0;
  109. }
  110. }
  111. else {
  112. open(CONS,">/dev/console");
  113. print CONS "<$facility.$priority>$whoami: $message\r";
  114. exit if defined $pid; # if fork failed, we're parent
  115. close CONS;
  116. }
  117. }
  118. }
  119. }
  120. sub xlate {
  121. local($name) = @_;
  122. $name = uc $name;
  123. $name = "LOG_$name" unless $name =~ /^LOG_/;
  124. $name = "syslog'$name";
  125. defined &$name ? &$name : -1;
  126. }
  127. sub connect {
  128. $pat = 'S n C4 x8';
  129. $af_unix = &AF_UNIX;
  130. $af_inet = &AF_INET;
  131. $stream = &SOCK_STREAM;
  132. $datagram = &SOCK_DGRAM;
  133. ($name,$aliases,$proto) = getprotobyname('udp');
  134. $udp = $proto;
  135. ($name,$aliases,$port,$proto) = getservbyname('syslog','udp');
  136. $syslog = $port;
  137. if (chop($myname = `hostname`)) {
  138. ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($myname);
  139. die "Can't lookup $myname\n" unless $name;
  140. @bytes = unpack("C4",$addrs[0]);
  141. }
  142. else {
  143. @bytes = (0,0,0,0);
  144. }
  145. $this = pack($pat, $af_inet, 0, @bytes);
  146. if ($host =~ /^\d+\./) {
  147. @bytes = split(/\./,$host);
  148. }
  149. else {
  150. ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host);
  151. die "Can't lookup $host\n" unless $name;
  152. @bytes = unpack("C4",$addrs[0]);
  153. }
  154. $that = pack($pat,$af_inet,$syslog,@bytes);
  155. socket(SYSLOG,$af_inet,$datagram,$udp) || die "socket: $!\n";
  156. bind(SYSLOG,$this) || die "bind: $!\n";
  157. connect(SYSLOG,$that) || die "connect: $!\n";
  158. local($old) = select(SYSLOG); $| = 1; select($old);
  159. $connected = 1;
  160. }
  161. sub disconnect {
  162. close SYSLOG;
  163. $connected = 0;
  164. }
  165. 1;