Home | History | Annotate | Line # | Download | only in syslog
      1 /*	$NetBSD: syslog.c,v 1.2 2015/02/02 14:03:05 lneto Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2013 Marc Balmer <marc (at) msys.ch>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /* Lua binding for syslog */
     29 
     30 #include <sys/types.h>
     31 
     32 #include <errno.h>
     33 #include <lua.h>
     34 #include <lauxlib.h>
     35 #include <stdlib.h>
     36 #include <syslog.h>
     37 #include <unistd.h>
     38 
     39 int luaopen_syslog(lua_State *);
     40 
     41 static int
     42 syslog_openlog(lua_State *L)
     43 {
     44 	const char *ident;
     45 	int option;
     46 	int facility;
     47 
     48 	ident = luaL_checkstring(L, 1);
     49 	option = luaL_checkinteger(L, 2);
     50 	facility = luaL_checkinteger(L, 3);
     51 	openlog(ident, option, facility);
     52 	return 0;
     53 }
     54 
     55 static int
     56 syslog_syslog(lua_State *L)
     57 {
     58 	syslog((int) luaL_checkinteger(L, 1), "%s", luaL_checkstring(L, 2));
     59 	return 0;
     60 }
     61 
     62 static int
     63 syslog_closelog(lua_State *L)
     64 {
     65 	closelog();
     66 	return 0;
     67 }
     68 
     69 static int
     70 syslog_setlogmask(lua_State *L)
     71 {
     72 	lua_pushinteger(L, setlogmask((int) luaL_checkinteger(L, 1)));
     73 	return 1;
     74 }
     75 
     76 static void
     77 syslog_set_info(lua_State *L)
     78 {
     79 	lua_pushliteral(L, "_COPYRIGHT");
     80 	lua_pushliteral(L, "Copyright (C) 2013 by "
     81 	    "Marc Balmer <marc (at) msys.ch>");
     82 	lua_settable(L, -3);
     83 	lua_pushliteral(L, "_DESCRIPTION");
     84 	lua_pushliteral(L, "syslog binding for Lua");
     85 	lua_settable(L, -3);
     86 	lua_pushliteral(L, "_VERSION");
     87 	lua_pushliteral(L, "syslog 1.0.0");
     88 	lua_settable(L, -3);
     89 }
     90 
     91 struct constant {
     92 	const char *name;
     93 	int value;
     94 };
     95 
     96 #define CONSTANT(NAME)		{ #NAME, NAME }
     97 
     98 static struct constant syslog_constant[] = {
     99 	/* syslog options */
    100 	CONSTANT(LOG_CONS),
    101 	CONSTANT(LOG_NDELAY),
    102 	CONSTANT(LOG_NOWAIT),
    103 	CONSTANT(LOG_ODELAY),
    104 	CONSTANT(LOG_PERROR),
    105 	CONSTANT(LOG_PID),
    106 
    107 	/* syslog facilities */
    108 	CONSTANT(LOG_AUTH),
    109 	CONSTANT(LOG_AUTHPRIV),
    110 	CONSTANT(LOG_CRON),
    111 	CONSTANT(LOG_DAEMON),
    112 	CONSTANT(LOG_FTP),
    113 	CONSTANT(LOG_KERN),
    114 	CONSTANT(LOG_LOCAL0),
    115 	CONSTANT(LOG_LOCAL1),
    116 	CONSTANT(LOG_LOCAL2),
    117 	CONSTANT(LOG_LOCAL3),
    118 	CONSTANT(LOG_LOCAL4),
    119 	CONSTANT(LOG_LOCAL5),
    120 	CONSTANT(LOG_LOCAL6),
    121 	CONSTANT(LOG_LOCAL7),
    122 	CONSTANT(LOG_LPR),
    123 	CONSTANT(LOG_MAIL),
    124 	CONSTANT(LOG_NEWS),
    125 	CONSTANT(LOG_SYSLOG),
    126 	CONSTANT(LOG_USER),
    127 	CONSTANT(LOG_UUCP),
    128 
    129 	/* syslog levels */
    130 	CONSTANT(LOG_EMERG),
    131 	CONSTANT(LOG_ALERT),
    132 	CONSTANT(LOG_CRIT),
    133 	CONSTANT(LOG_ERR),
    134 	CONSTANT(LOG_WARNING),
    135 	CONSTANT(LOG_NOTICE),
    136 	CONSTANT(LOG_INFO),
    137 	CONSTANT(LOG_DEBUG),
    138 
    139 	{ NULL, 0 }
    140 };
    141 
    142 int
    143 luaopen_syslog(lua_State *L)
    144 {
    145 	int n;
    146 	struct luaL_Reg luasyslog[] = {
    147 		{ "openlog",	syslog_openlog },
    148 		{ "syslog",	syslog_syslog },
    149 		{ "closelog",	syslog_closelog },
    150 		{ "setlogmask",	syslog_setlogmask },
    151 		{ NULL, NULL }
    152 	};
    153 
    154 #if LUA_VERSION_NUM >= 502
    155 	luaL_newlib(L, luasyslog);
    156 #else
    157 	luaL_register(L, "syslog", luasyslog);
    158 #endif
    159 	syslog_set_info(L);
    160 	for (n = 0; syslog_constant[n].name != NULL; n++) {
    161 		lua_pushinteger(L, syslog_constant[n].value);
    162 		lua_setfield(L, -2, syslog_constant[n].name);
    163 	};
    164 	return 1;
    165 }
    166