1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/syslog/syslog.c

395 lines
9.1 KiB
C
Raw Normal View History

/*
* UNIX Syslog extension for Ruby
* Amos Gouaux, University of Texas at Dallas
* <amos+ruby@utdallas.edu>
*
* $RoughId: syslog.c,v 1.21 2002/02/25 12:21:17 knu Exp $
* $Id$
*/
#include "ruby.h"
* gc.c (Init_stack): stack region is far smaller than usual if pthread is used. * marshal.c (w_extended): singleton methods should not be checked when dumping via marshal_dump() or _dump(). [ruby-talk:85909] * file.c (getcwdofdrv): avoid using getcwd() directly, use my_getcwd() instead. * merged NeXT, OpenStep, Rhapsody ports patch from Eric Sunshine <sunshine@sunshineco.com>. [ruby-core:01596] * marshal.c (w_object): LINK check earlier than anything else, i.e. do not dump TYPE_IVAR for already dumped objects. (ruby-bugs PR#1220) * eval.c (rb_eval): call "inherited" only when a new class is generated; not on reopening. * eval.c (eval): prepend error position in evaluating string to * configure.in: revived NextStep, OpenStep, and Rhapsody ports which had become unbuildable; enhanced --enable-fat-binary option so that it accepts a list of desired architectures (rather than assuming a fixed list), or defaults to a platform-appropriate list if user does not provide an explicit list; made the default list of architectures for MAB (fat binary) more comprehensive; now uses -fno-common even when building the interpreter (in addition to using it for extensions), thus allowing the interpreter to be embedded into a plugin module of an external project (in addition to allowing embedding directly into an application); added checks for <netinet/in_systm.h> (needed by `socket' extension) and getcwd(); now ensures that -I/usr/local/include is employed when extensions' extconf.rb scripts invoke have_header() since extension checks on NextStep and OpenStep will fail without it if the desired resource resides in the /usr/local tree; fixed formatting of --help message. * Makefile.in: $(LIBRUBY_A) rule now deletes the archive before invoking $(AR) since `ar' on Apple/NeXT can not "update" MAB archives (see configure's --enable-fat-binary option); added rule for new missing/getcwd.c. * defines.h: fixed endian handling during MAB build (see configure's --enable-fat-binary option) to ensure that all portions of the project see the correct WORDS_BIGENDIAN value (some extension modules were getting the wrong endian setting); added missing constants GETPGRP_VOID, WNOHANG, WUNTRACED, X_OK, and type pid_t for NextStep and OpenStep; removed unnecessary and problematic HAVE_SYS_WAIT_H define in NeXT section. * dir.c: do not allow NAMLEN() macro to trust dirent::d_namlen on NextStep since, on some installations, this value always resolves uselessly to zero. * dln.c: added error reporting to NextStep extension loader since the previous behavior of failing silently was not useful; now ensures that NSLINKMODULE_OPTION_BINDNOW compatibility constant is defined for OpenStep and Rhapsody; no longer includes <mach-o/dyld.h> twice on Rhapsody since this header lacks multiple-include protection, which resulted in "redefinition" compilation errors. * main.c: also create hard reference to objc_msgSend() on NeXT platforms (in addition to Apple platforms). * lib/mkmf.rb: now exports XCFLAGS from configure script to extension makefiles so that extensions can be built MAB (see configure's --enable-fat-binary option); also utilize XCFLAGS in cc_command() (but not cpp_command() because MAB flags are incompatible with direct invocation of `cpp'). * ext/curses/extconf.rb: now additionally checks for presence of these curses functions which are not present on NextStep or Openstep: bkgd(), bkgdset(), color(), curs(), getbkgd(), init(), scrl(), set(), setscrreg(), wattroff(), wattron(), wattrset(), wbkgd(), wbkgdset(), wscrl(), wsetscrreg() * ext/curses/curses.c: added appropriate #ifdef's for additional set of curses functions now checked by extconf.rb; fixed curses_bkgd() and window_bkgd() to correctly return boolean result rather than numeric result; fixed window_getbkgd() to correctly signal an error by returning nil rather than -1. * ext/etc/etc.c: setup_passwd() and setup_group() now check for null pointers before invoking rb_tainted_str_new2() upon fields extracted from `struct passwd' and `struct group' since null pointers in some fields are common on NextStep/OpenStep (especially so for the `pw_comment' field) and rb_tainted_str_new2() throws an exception when it receives a null pointer. * ext/pty/pty.c: include "util.h" for strdup()/ruby_strdup() for platforms such as NextStep and OpenStep which lack strdup(). * ext/socket/getaddrinfo.c: cast first argument of getservbyname(), gethostbyaddr(), and gethostbyname() from (const char*) to non-const (char*) for older platforms such as NextStep and OpenStep. * ext/socket/socket.c: include "util.h" for strdup()/ruby_strdup() for platforms such as NextStep and OpenStep which lack strdup(); include <netinet/in_systm.h> if present for NextStep and OpenStep; cast first argument of gethostbyaddr() and getservbyname() from (const char*) to non-const (char*) for older platforms. * ext/syslog/syslog.c: include "util.h" for strdup()/ruby_strdup() for platforms such as NextStep and OpenStep which lack strdup(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5002 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2003-11-21 23:00:03 -05:00
#include "util.h"
#include <syslog.h>
/* Syslog class */
static VALUE mSyslog, mSyslogConstants;
static const char *syslog_ident = NULL;
static int syslog_options = -1, syslog_facility = -1, syslog_mask = -1;
static int syslog_opened = 0;
/* Package helper routines */
static void syslog_write(int pri, int argc, VALUE *argv)
{
VALUE str;
if (argc < 1) {
rb_raise(rb_eArgError, "no log message supplied");
}
if (!syslog_opened) {
rb_raise(rb_eRuntimeError, "must open syslog before write");
}
str = rb_f_sprintf(argc, argv);
syslog(pri, "%s", RSTRING_PTR(str));
}
/* Syslog module methods */
static VALUE mSyslog_close(VALUE self)
{
if (!syslog_opened) {
rb_raise(rb_eRuntimeError, "syslog not opened");
}
closelog();
free((void *)syslog_ident);
syslog_ident = NULL;
syslog_options = syslog_facility = syslog_mask = -1;
syslog_opened = 0;
return Qnil;
}
static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
{
VALUE ident, opt, fac;
if (syslog_opened) {
rb_raise(rb_eRuntimeError, "syslog already open");
}
rb_scan_args(argc, argv, "03", &ident, &opt, &fac);
if (NIL_P(ident)) {
ident = rb_gv_get("$0");
}
#ifdef SafeStringValue
SafeStringValue(ident);
#else
Check_SafeStr(ident);
#endif
syslog_ident = strdup(RSTRING_PTR(ident));
if (NIL_P(opt)) {
syslog_options = LOG_PID | LOG_CONS;
} else {
syslog_options = NUM2INT(opt);
}
if (NIL_P(fac)) {
syslog_facility = LOG_USER;
} else {
syslog_facility = NUM2INT(fac);
}
openlog(syslog_ident, syslog_options, syslog_facility);
syslog_opened = 1;
setlogmask(syslog_mask = setlogmask(0));
/* be like File.new.open {...} */
if (rb_block_given_p()) {
rb_ensure(rb_yield, self, mSyslog_close, self);
}
return self;
}
static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
{
mSyslog_close(self);
return mSyslog_open(argc, argv, self);
}
static VALUE mSyslog_isopen(VALUE self)
{
return syslog_opened ? Qtrue : Qfalse;
}
static VALUE mSyslog_ident(VALUE self)
{
return syslog_opened ? rb_str_new2(syslog_ident) : Qnil;
}
static VALUE mSyslog_options(VALUE self)
{
return syslog_opened ? INT2NUM(syslog_options) : Qnil;
}
static VALUE mSyslog_facility(VALUE self)
{
return syslog_opened ? INT2NUM(syslog_facility) : Qnil;
}
static VALUE mSyslog_get_mask(VALUE self)
{
return syslog_opened ? INT2NUM(syslog_mask) : Qnil;
}
static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
{
if (!syslog_opened) {
rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
}
setlogmask(syslog_mask = NUM2INT(mask));
return mask;
}
static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
{
VALUE pri;
if (argc < 2) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2+)", argc);
}
argc--;
pri = *argv++;
if (!FIXNUM_P(pri)) {
rb_raise(rb_eTypeError, "type mismatch: %s given", rb_class2name(CLASS_OF(pri)));
}
syslog_write(FIX2INT(pri), argc, argv);
return self;
}
static VALUE mSyslog_inspect(VALUE self)
{
char buf[1024];
if (syslog_opened) {
snprintf(buf, sizeof(buf),
"<#%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>",
rb_class2name(self),
syslog_ident,
syslog_options,
syslog_facility,
syslog_mask);
} else {
snprintf(buf, sizeof(buf),
"<#%s: opened=false>", rb_class2name(self));
}
return rb_str_new2(buf);
}
static VALUE mSyslog_instance(VALUE self)
{
return self;
}
#define define_syslog_shortcut_method(pri, name) \
static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \
{ \
syslog_write(pri, argc, argv); \
\
return self; \
}
#ifdef LOG_EMERG
define_syslog_shortcut_method(LOG_EMERG, emerg)
#endif
#ifdef LOG_ALERT
define_syslog_shortcut_method(LOG_ALERT, alert)
#endif
#ifdef LOG_CRIT
define_syslog_shortcut_method(LOG_CRIT, crit)
#endif
#ifdef LOG_ERR
define_syslog_shortcut_method(LOG_ERR, err)
#endif
#ifdef LOG_WARNING
define_syslog_shortcut_method(LOG_WARNING, warning)
#endif
#ifdef LOG_NOTICE
define_syslog_shortcut_method(LOG_NOTICE, notice)
#endif
#ifdef LOG_INFO
define_syslog_shortcut_method(LOG_INFO, info)
#endif
#ifdef LOG_DEBUG
define_syslog_shortcut_method(LOG_DEBUG, debug)
#endif
static VALUE mSyslogConstants_LOG_MASK(VALUE klass, VALUE pri)
{
return INT2FIX(LOG_MASK(FIX2INT(pri)));
}
static VALUE mSyslogConstants_LOG_UPTO(VALUE klass, VALUE pri)
{
return INT2FIX(LOG_UPTO(FIX2INT(pri)));
}
/* Init for package syslog */
void Init_syslog()
{
mSyslog = rb_define_module("Syslog");
mSyslogConstants = rb_define_module_under(mSyslog, "Constants");
rb_include_module(mSyslog, mSyslogConstants);
rb_define_module_function(mSyslog, "open", mSyslog_open, -1);
rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1);
rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1);
rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0);
rb_define_module_function(mSyslog, "ident", mSyslog_ident, 0);
rb_define_module_function(mSyslog, "options", mSyslog_options, 0);
rb_define_module_function(mSyslog, "facility", mSyslog_facility, 0);
rb_define_module_function(mSyslog, "log", mSyslog_log, -1);
rb_define_module_function(mSyslog, "close", mSyslog_close, 0);
rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0);
rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1);
rb_define_module_function(mSyslog, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
rb_define_module_function(mSyslog, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
rb_define_module_function(mSyslog, "inspect", mSyslog_inspect, 0);
rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0);
rb_define_module_function(mSyslogConstants, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
rb_define_module_function(mSyslogConstants, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
#define rb_define_syslog_const(id) \
rb_define_const(mSyslogConstants, #id, INT2NUM(id))
/* Various options when opening log */
#ifdef LOG_PID
rb_define_syslog_const(LOG_PID);
#endif
#ifdef LOG_CONS
rb_define_syslog_const(LOG_CONS);
#endif
#ifdef LOG_ODELAY
rb_define_syslog_const(LOG_ODELAY); /* deprecated */
#endif
#ifdef LOG_NDELAY
rb_define_syslog_const(LOG_NDELAY);
#endif
#ifdef LOG_NOWAIT
rb_define_syslog_const(LOG_NOWAIT); /* deprecated */
#endif
#ifdef LOG_PERROR
rb_define_syslog_const(LOG_PERROR);
#endif
/* Various syslog facilities */
#ifdef LOG_AUTH
rb_define_syslog_const(LOG_AUTH);
#endif
#ifdef LOG_AUTHPRIV
rb_define_syslog_const(LOG_AUTHPRIV);
#endif
#ifdef LOG_CONSOLE
rb_define_syslog_const(LOG_CONSOLE);
#endif
#ifdef LOG_CRON
rb_define_syslog_const(LOG_CRON);
#endif
#ifdef LOG_DAEMON
rb_define_syslog_const(LOG_DAEMON);
#endif
#ifdef LOG_FTP
rb_define_syslog_const(LOG_FTP);
#endif
#ifdef LOG_KERN
rb_define_syslog_const(LOG_KERN);
#endif
#ifdef LOG_LPR
rb_define_syslog_const(LOG_LPR);
#endif
#ifdef LOG_MAIL
rb_define_syslog_const(LOG_MAIL);
#endif
#ifdef LOG_NEWS
rb_define_syslog_const(LOG_NEWS);
#endif
#ifdef LOG_NTP
rb_define_syslog_const(LOG_NTP);
#endif
#ifdef LOG_SECURITY
rb_define_syslog_const(LOG_SECURITY);
#endif
#ifdef LOG_SYSLOG
rb_define_syslog_const(LOG_SYSLOG);
#endif
#ifdef LOG_USER
rb_define_syslog_const(LOG_USER);
#endif
#ifdef LOG_UUCP
rb_define_syslog_const(LOG_UUCP);
#endif
#ifdef LOG_LOCAL0
rb_define_syslog_const(LOG_LOCAL0);
#endif
#ifdef LOG_LOCAL1
rb_define_syslog_const(LOG_LOCAL1);
#endif
#ifdef LOG_LOCAL2
rb_define_syslog_const(LOG_LOCAL2);
#endif
#ifdef LOG_LOCAL3
rb_define_syslog_const(LOG_LOCAL3);
#endif
#ifdef LOG_LOCAL4
rb_define_syslog_const(LOG_LOCAL4);
#endif
#ifdef LOG_LOCAL5
rb_define_syslog_const(LOG_LOCAL5);
#endif
#ifdef LOG_LOCAL6
rb_define_syslog_const(LOG_LOCAL6);
#endif
#ifdef LOG_LOCAL7
rb_define_syslog_const(LOG_LOCAL7);
#endif
#define rb_define_syslog_shortcut(name) \
rb_define_module_function(mSyslog, #name, mSyslog_##name, -1)
/* Various syslog priorities and the shortcut methods */
#ifdef LOG_EMERG
rb_define_syslog_const(LOG_EMERG);
rb_define_syslog_shortcut(emerg);
#endif
#ifdef LOG_ALERT
rb_define_syslog_const(LOG_ALERT);
rb_define_syslog_shortcut(alert);
#endif
#ifdef LOG_CRIT
rb_define_syslog_const(LOG_CRIT);
rb_define_syslog_shortcut(crit);
#endif
#ifdef LOG_ERR
rb_define_syslog_const(LOG_ERR);
rb_define_syslog_shortcut(err);
#endif
#ifdef LOG_WARNING
rb_define_syslog_const(LOG_WARNING);
rb_define_syslog_shortcut(warning);
#endif
#ifdef LOG_NOTICE
rb_define_syslog_const(LOG_NOTICE);
rb_define_syslog_shortcut(notice);
#endif
#ifdef LOG_INFO
rb_define_syslog_const(LOG_INFO);
rb_define_syslog_shortcut(info);
#endif
#ifdef LOG_DEBUG
rb_define_syslog_const(LOG_DEBUG);
rb_define_syslog_shortcut(debug);
#endif
}