Ruby  2.4.2p198(2017-09-14revision59899)
syslog.c
Go to the documentation of this file.
1 /*
2  * UNIX Syslog extension for Ruby
3  * Amos Gouaux, University of Texas at Dallas
4  * <amos+ruby@utdallas.edu>
5  * Documented by mathew <meta@pobox.com>
6  *
7  * $RoughId: syslog.c,v 1.21 2002/02/25 12:21:17 knu Exp $
8  * $Id: syslog.c 52275 2015-10-25 00:43:06Z nobu $
9  */
10 
11 #include "ruby/ruby.h"
12 #include "ruby/util.h"
13 #include <syslog.h>
14 
15 /* Syslog class */
16 static VALUE mSyslog;
17 /*
18  * Module holding all Syslog constants. See Syslog::log and
19  * Syslog::open for constant descriptions.
20  */
22 /* Module holding Syslog option constants */
24 /* Module holding Syslog facility constants */
26 /* Module holding Syslog level constants */
28 /* Module holding Syslog utility macros */
30 
31 static const char *syslog_ident = NULL;
32 static int syslog_options = -1, syslog_facility = -1, syslog_mask = -1;
33 static int syslog_opened = 0;
34 
35 /* Package helper routines */
36 static void syslog_write(int pri, int argc, VALUE *argv)
37 {
38  VALUE str;
39 
40  if (argc < 1) {
41  rb_raise(rb_eArgError, "no log message supplied");
42  }
43 
44  if (!syslog_opened) {
45  rb_raise(rb_eRuntimeError, "must open syslog before write");
46  }
47 
48  str = rb_f_sprintf(argc, argv);
49 
50  syslog(pri, "%s", RSTRING_PTR(str));
51 }
52 
53 /* Closes the syslog facility.
54  * Raises a runtime exception if it is not open.
55  */
57 {
58  if (!syslog_opened) {
59  rb_raise(rb_eRuntimeError, "syslog not opened");
60  }
61 
62  closelog();
63 
64  xfree((void *)syslog_ident);
65  syslog_ident = NULL;
67  syslog_opened = 0;
68 
69  return Qnil;
70 }
71 
72 /* call-seq:
73  * open(ident, options, facility) => syslog
74  *
75  * :yields: syslog
76  *
77  * Open the syslog facility.
78  * Raises a runtime exception if it is already open.
79  *
80  * Can be called with or without a code block. If called with a block, the
81  * Syslog object created is passed to the block.
82  *
83  * If the syslog is already open, raises a RuntimeError.
84  *
85  * +ident+ is a String which identifies the calling program.
86  *
87  * +options+ is the logical OR of any of the following:
88  *
89  * LOG_CONS:: If there is an error while sending to the system logger,
90  * write directly to the console instead.
91  *
92  * LOG_NDELAY:: Open the connection now, rather than waiting for the first
93  * message to be written.
94  *
95  * LOG_NOWAIT:: Don't wait for any child processes created while logging
96  * messages. (Has no effect on Linux.)
97  *
98  * LOG_ODELAY:: Opposite of LOG_NDELAY; wait until a message is sent before
99  * opening the connection. (This is the default.)
100  *
101  * LOG_PERROR:: Print the message to stderr as well as sending it to syslog.
102  * (Not in POSIX.1-2001.)
103  *
104  * LOG_PID:: Include the current process ID with each message.
105  *
106  * +facility+ describes the type of program opening the syslog, and is
107  * the logical OR of any of the following which are defined for the host OS:
108  *
109  * LOG_AUTH:: Security or authorization. Deprecated, use LOG_AUTHPRIV
110  * instead.
111  *
112  * LOG_AUTHPRIV:: Security or authorization messages which should be kept
113  * private.
114  *
115  * LOG_CONSOLE:: System console message.
116  *
117  * LOG_CRON:: System task scheduler (cron or at).
118  *
119  * LOG_DAEMON:: A system daemon which has no facility value of its own.
120  *
121  * LOG_FTP:: An FTP server.
122  *
123  * LOG_KERN:: A kernel message (not sendable by user processes, so not of
124  * much use to Ruby, but listed here for completeness).
125  *
126  * LOG_LPR:: Line printer subsystem.
127  *
128  * LOG_MAIL:: Mail delivery or transport subsystem.
129  *
130  * LOG_NEWS:: Usenet news system.
131  *
132  * LOG_NTP:: Network Time Protocol server.
133  *
134  * LOG_SECURITY:: General security message.
135  *
136  * LOG_SYSLOG:: Messages generated internally by syslog.
137  *
138  * LOG_USER:: Generic user-level message.
139  *
140  * LOG_UUCP:: UUCP subsystem.
141  *
142  * LOG_LOCAL0 to LOG_LOCAL7:: Locally-defined facilities.
143  *
144  * Example:
145  *
146  * Syslog.open("webrick", Syslog::LOG_PID,
147  * Syslog::LOG_DAEMON | Syslog::LOG_LOCAL3)
148  *
149  */
150 static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
151 {
152  VALUE ident, opt, fac;
153 
154  if (syslog_opened) {
155  rb_raise(rb_eRuntimeError, "syslog already open");
156  }
157 
158  rb_scan_args(argc, argv, "03", &ident, &opt, &fac);
159 
160  if (NIL_P(ident)) {
161  ident = rb_gv_get("$0");
162  }
163  SafeStringValue(ident);
164  syslog_ident = strdup(RSTRING_PTR(ident));
165 
166  if (NIL_P(opt)) {
167  syslog_options = LOG_PID | LOG_CONS;
168  } else {
169  syslog_options = NUM2INT(opt);
170  }
171 
172  if (NIL_P(fac)) {
173  syslog_facility = LOG_USER;
174  } else {
175  syslog_facility = NUM2INT(fac);
176  }
177 
179 
180  syslog_opened = 1;
181 
182  setlogmask(syslog_mask = setlogmask(0));
183 
184  /* be like File.new.open {...} */
185  if (rb_block_given_p()) {
186  rb_ensure(rb_yield, self, mSyslog_close, self);
187  }
188 
189  return self;
190 }
191 
192 /* call-seq:
193  * reopen(ident, options, facility) => syslog
194  *
195  * :yields: syslog
196  *
197  * Closes and then reopens the syslog.
198  *
199  * Arguments are the same as for open().
200  */
202 {
203  mSyslog_close(self);
204 
205  return mSyslog_open(argc, argv, self);
206 }
207 
208 /* call-seq:
209  * opened?
210  *
211  * Returns true if the syslog is open.
212  */
214 {
215  return syslog_opened ? Qtrue : Qfalse;
216 }
217 
218 /* Returns the identity string used in the last call to open()
219  */
221 {
223 }
224 
225 /* Returns the options bitmask used in the last call to open()
226  */
228 {
230 }
231 
232 /* Returns the facility number used in the last call to open()
233  */
235 {
237 }
238 
239 /* Returns the log priority mask in effect. The mask is not reset by opening
240  * or closing syslog.
241  */
243 {
245 }
246 
247 /* call-seq:
248  * mask=(priority_mask)
249  *
250  * Sets the log priority mask. A method LOG_UPTO is defined to make it easier
251  * to set mask values. Example:
252  *
253  * Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
254  *
255  * Alternatively, specific priorities can be selected and added together using
256  * binary OR. Example:
257  *
258  * Syslog.mask = Syslog::LOG_MASK(Syslog::LOG_ERR) | Syslog::LOG_MASK(Syslog::LOG_CRIT)
259  *
260  * The priority mask persists through calls to open() and close().
261  */
262 static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
263 {
264  if (!syslog_opened) {
265  rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
266  }
267 
268  setlogmask(syslog_mask = NUM2INT(mask));
269 
270  return mask;
271 }
272 
273 /* call-seq:
274  * log(priority, format_string, *format_args)
275  *
276  * Log a message with the specified priority. Example:
277  *
278  * Syslog.log(Syslog::LOG_CRIT, "Out of disk space")
279  * Syslog.log(Syslog::LOG_CRIT, "User %s logged in", ENV['USER'])
280  *
281  * The priority levels, in descending order, are:
282  *
283  * LOG_EMERG:: System is unusable
284  * LOG_ALERT:: Action needs to be taken immediately
285  * LOG_CRIT:: A critical condition has occurred
286  * LOG_ERR:: An error occurred
287  * LOG_WARNING:: Warning of a possible problem
288  * LOG_NOTICE:: A normal but significant condition occurred
289  * LOG_INFO:: Informational message
290  * LOG_DEBUG:: Debugging information
291  *
292  * Each priority level also has a shortcut method that logs with it's named priority.
293  * As an example, the two following statements would produce the same result:
294  *
295  * Syslog.log(Syslog::LOG_ALERT, "Out of memory")
296  * Syslog.alert("Out of memory")
297  *
298  * Format strings are as for printf/sprintf, except that in addition %m is
299  * replaced with the error message string that would be returned by
300  * strerror(errno).
301  *
302  */
303 static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
304 {
305  VALUE pri;
306 
308 
309  argc--;
310  pri = *argv++;
311 
312  if (!FIXNUM_P(pri)) {
313  rb_raise(rb_eTypeError, "type mismatch: %"PRIsVALUE" given", rb_obj_class(pri));
314  }
315 
316  syslog_write(FIX2INT(pri), argc, argv);
317 
318  return self;
319 }
320 
321 /* Returns an inspect() string summarizing the object state.
322  */
324 {
325  Check_Type(self, T_MODULE);
326 
327  if (!syslog_opened)
328  return rb_sprintf("<#%"PRIsVALUE": opened=false>", self);
329 
330  return rb_sprintf("<#%"PRIsVALUE": opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>",
331  self,
332  syslog_ident,
335  syslog_mask);
336 }
337 
338 /* Returns self, for backward compatibility.
339  */
341 {
342  return self;
343 }
344 
345 #define define_syslog_shortcut_method(pri, name) \
346 static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \
347 { \
348  syslog_write((pri), argc, argv); \
349 \
350  return self; \
351 }
352 
353 #ifdef LOG_EMERG
354 define_syslog_shortcut_method(LOG_EMERG, emerg)
355 #endif
356 #ifdef LOG_ALERT
357 define_syslog_shortcut_method(LOG_ALERT, alert)
358 #endif
359 #ifdef LOG_CRIT
360 define_syslog_shortcut_method(LOG_CRIT, crit)
361 #endif
362 #ifdef LOG_ERR
364 #endif
365 #ifdef LOG_WARNING
366 define_syslog_shortcut_method(LOG_WARNING, warning)
367 #endif
368 #ifdef LOG_NOTICE
369 define_syslog_shortcut_method(LOG_NOTICE, notice)
370 #endif
371 #ifdef LOG_INFO
372 define_syslog_shortcut_method(LOG_INFO, info)
373 #endif
374 #ifdef LOG_DEBUG
376 #endif
377 
378 /* call-seq:
379  * LOG_MASK(priority_level) => priority_mask
380  *
381  * Generates a mask bit for a priority level. See #mask=
382  */
384 {
385  return INT2FIX(LOG_MASK(NUM2INT(pri)));
386 }
387 
388 /* call-seq:
389  * LOG_UPTO(priority_level) => priority_mask
390  *
391  * Generates a mask value for priority levels at or below the level specified.
392  * See #mask=
393  */
395 {
396  return INT2FIX(LOG_UPTO(NUM2INT(pri)));
397 }
398 
400 {
402  return mod;
403 }
404 
405 /* The syslog package provides a Ruby interface to the POSIX system logging
406  * facility.
407  *
408  * Syslog messages are typically passed to a central logging daemon.
409  * The daemon may filter them; route them into different files (usually
410  * found under /var/log); place them in SQL databases; forward
411  * them to centralized logging servers via TCP or UDP; or even alert the
412  * system administrator via email, pager or text message.
413  *
414  * Unlike application-level logging via Logger or Log4r, syslog is designed
415  * to allow secure tamper-proof logging.
416  *
417  * The syslog protocol is standardized in RFC 5424.
418  */
419 void Init_syslog(void)
420 {
421  mSyslog = rb_define_module("Syslog");
422 
424 
429 
434 
438 
443 
446 
447  /* Syslog options */
448 
449 #define rb_define_syslog_option(c) \
450  rb_define_const(mSyslogOption, #c, INT2NUM(c))
451 
452 #ifdef LOG_PID
453  rb_define_syslog_option(LOG_PID);
454 #endif
455 #ifdef LOG_CONS
456  rb_define_syslog_option(LOG_CONS);
457 #endif
458 #ifdef LOG_ODELAY
459  rb_define_syslog_option(LOG_ODELAY); /* deprecated */
460 #endif
461 #ifdef LOG_NDELAY
462  rb_define_syslog_option(LOG_NDELAY);
463 #endif
464 #ifdef LOG_NOWAIT
465  rb_define_syslog_option(LOG_NOWAIT); /* deprecated */
466 #endif
467 #ifdef LOG_PERROR
468  rb_define_syslog_option(LOG_PERROR);
469 #endif
470 
471  /* Syslog facilities */
472 
473 #define rb_define_syslog_facility(c) \
474  rb_define_const(mSyslogFacility, #c, INT2NUM(c))
475 
476 #ifdef LOG_AUTH
477  rb_define_syslog_facility(LOG_AUTH);
478 #endif
479 #ifdef LOG_AUTHPRIV
480  rb_define_syslog_facility(LOG_AUTHPRIV);
481 #endif
482 #ifdef LOG_CONSOLE
483  rb_define_syslog_facility(LOG_CONSOLE);
484 #endif
485 #ifdef LOG_CRON
486  rb_define_syslog_facility(LOG_CRON);
487 #endif
488 #ifdef LOG_DAEMON
489  rb_define_syslog_facility(LOG_DAEMON);
490 #endif
491 #ifdef LOG_FTP
492  rb_define_syslog_facility(LOG_FTP);
493 #endif
494 #ifdef LOG_KERN
495  rb_define_syslog_facility(LOG_KERN);
496 #endif
497 #ifdef LOG_LPR
498  rb_define_syslog_facility(LOG_LPR);
499 #endif
500 #ifdef LOG_MAIL
501  rb_define_syslog_facility(LOG_MAIL);
502 #endif
503 #ifdef LOG_NEWS
504  rb_define_syslog_facility(LOG_NEWS);
505 #endif
506 #ifdef LOG_NTP
507  rb_define_syslog_facility(LOG_NTP);
508 #endif
509 #ifdef LOG_SECURITY
510  rb_define_syslog_facility(LOG_SECURITY);
511 #endif
512 #ifdef LOG_SYSLOG
513  rb_define_syslog_facility(LOG_SYSLOG);
514 #endif
515 #ifdef LOG_USER
516  rb_define_syslog_facility(LOG_USER);
517 #endif
518 #ifdef LOG_UUCP
519  rb_define_syslog_facility(LOG_UUCP);
520 #endif
521 #ifdef LOG_LOCAL0
522  rb_define_syslog_facility(LOG_LOCAL0);
523 #endif
524 #ifdef LOG_LOCAL1
525  rb_define_syslog_facility(LOG_LOCAL1);
526 #endif
527 #ifdef LOG_LOCAL2
528  rb_define_syslog_facility(LOG_LOCAL2);
529 #endif
530 #ifdef LOG_LOCAL3
531  rb_define_syslog_facility(LOG_LOCAL3);
532 #endif
533 #ifdef LOG_LOCAL4
534  rb_define_syslog_facility(LOG_LOCAL4);
535 #endif
536 #ifdef LOG_LOCAL5
537  rb_define_syslog_facility(LOG_LOCAL5);
538 #endif
539 #ifdef LOG_LOCAL6
540  rb_define_syslog_facility(LOG_LOCAL6);
541 #endif
542 #ifdef LOG_LOCAL7
543  rb_define_syslog_facility(LOG_LOCAL7);
544 #endif
545 
546  /* Syslog levels and the shortcut methods */
547 
548 #define rb_define_syslog_level(c, m) \
549  rb_define_const(mSyslogLevel, #c, INT2NUM(c)); \
550  rb_define_module_function(mSyslog, #m, mSyslog_##m, -1)
551 
552 #ifdef LOG_EMERG
553  rb_define_syslog_level(LOG_EMERG, emerg);
554 #endif
555 #ifdef LOG_ALERT
556  rb_define_syslog_level(LOG_ALERT, alert);
557 #endif
558 #ifdef LOG_CRIT
559  rb_define_syslog_level(LOG_CRIT, crit);
560 #endif
561 #ifdef LOG_ERR
562  rb_define_syslog_level(LOG_ERR, err);
563 #endif
564 #ifdef LOG_WARNING
565  rb_define_syslog_level(LOG_WARNING, warning);
566 #endif
567 #ifdef LOG_NOTICE
568  rb_define_syslog_level(LOG_NOTICE, notice);
569 #endif
570 #ifdef LOG_INFO
571  rb_define_syslog_level(LOG_INFO, info);
572 #endif
573 #ifdef LOG_DEBUG
574  rb_define_syslog_level(LOG_DEBUG, debug);
575 #endif
576 
577  /* Syslog macros */
578 
582 
587 
589  rb_funcall(mSyslog, rb_intern("include"), 1, mSyslogConstants);
590 }
#define rb_define_syslog_facility(c)
#define INT2NUM(x)
Definition: ruby.h:1538
void Init_syslog(void)
Definition: syslog.c:419
#define NUM2INT(x)
Definition: ruby.h:684
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
VALUE rb_f_sprintf(int, const VALUE *)
Definition: sprintf.c:455
#define T_MODULE
Definition: ruby.h:494
#define Qtrue
Definition: ruby.h:437
static VALUE mSyslog_isopen(VALUE self)
Definition: syslog.c:213
VALUE rb_eTypeError
Definition: error.c:762
#define rb_check_arity
Definition: intern.h:303
static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
Definition: syslog.c:201
#define debug(lvl, x...)
Definition: ffi.c:52
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
static int syslog_opened
Definition: syslog.c:33
#define Check_Type(v, t)
Definition: ruby.h:562
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
static VALUE mSyslogLevel
Definition: syslog.c:27
static void syslog_write(int pri, int argc, VALUE *argv)
Definition: syslog.c:36
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
static VALUE mSyslog_instance(VALUE self)
Definition: syslog.c:340
#define FIXNUM_P(f)
Definition: ruby.h:365
static VALUE mSyslog_ident(VALUE self)
Definition: syslog.c:220
VALUE rb_gv_get(const char *)
Definition: variable.c:850
static int syslog_mask
Definition: syslog.c:32
int rb_block_given_p(void)
Definition: eval.c:797
VALUE rb_eRuntimeError
Definition: error.c:761
#define NIL_P(v)
Definition: ruby.h:451
static VALUE mSyslog
Definition: syslog.c:16
VALUE str
Definition: strscan.c:33
int argc
Definition: ruby.c:183
#define Qfalse
Definition: ruby.h:436
static const char * syslog_ident
Definition: syslog.c:31
#define rb_str_new2
Definition: intern.h:857
int err
Definition: win32.c:135
static VALUE mSyslog_close(VALUE self)
Definition: syslog.c:56
static int syslog_facility
Definition: syslog.c:32
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1731
VALUE rb_yield(VALUE)
Definition: vm_eval.c:1020
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
static VALUE mSyslogMacros
Definition: syslog.c:29
static VALUE mSyslog_facility(VALUE self)
Definition: syslog.c:234
#define strdup(s)
Definition: util.h:70
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
#define PRIsVALUE
Definition: ruby.h:135
#define Qnil
Definition: ruby.h:438
static int syslog_options
Definition: syslog.c:32
static VALUE mSyslogFacility
Definition: syslog.c:25
unsigned long VALUE
Definition: ruby.h:85
void rb_extend_object(VALUE obj, VALUE module)
Definition: eval.c:1429
#define FIX2INT(x)
Definition: ruby.h:686
static VALUE mSyslogOption
Definition: syslog.c:23
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:923
static VALUE mSyslog_inspect(VALUE self)
Definition: syslog.c:323
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
#define rb_define_syslog_option(c)
#define RSTRING_PTR(str)
Definition: ruby.h:982
static VALUE mSyslogConstants
Definition: syslog.c:21
#define INT2FIX(i)
Definition: ruby.h:232
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
#define rb_define_syslog_level(c, m)
static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
Definition: syslog.c:262
static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
Definition: syslog.c:303
#define SafeStringValue(v)
Definition: ruby.h:574
static VALUE mSyslogMacros_LOG_UPTO(VALUE mod, VALUE pri)
Definition: syslog.c:394
static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
Definition: syslog.c:150
void void xfree(void *)
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define rb_intern(str)
#define mod(x, y)
Definition: date_strftime.c:28
#define NULL
Definition: _sdbm.c:102
#define define_syslog_shortcut_method(pri, name)
Definition: syslog.c:345
static VALUE mSyslogMacros_included(VALUE mod, VALUE target)
Definition: syslog.c:399
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
VALUE rb_eArgError
Definition: error.c:763
static VALUE mSyslog_get_mask(VALUE self)
Definition: syslog.c:242
static VALUE mSyslog_options(VALUE self)
Definition: syslog.c:227
static VALUE mSyslogMacros_LOG_MASK(VALUE mod, VALUE pri)
Definition: syslog.c:383
char ** argv
Definition: ruby.c:184
VALUE rb_obj_class(VALUE)
Definition: object.c:229