xref: /minix3/lib/lua/syslog/syslog.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: syslog.c,v 1.2 2015/02/02 14:03:05 lneto Exp $ */
284d9c625SLionel Sambuc 
384d9c625SLionel Sambuc /*
484d9c625SLionel Sambuc  * Copyright (c) 2013 Marc Balmer <marc@msys.ch>
584d9c625SLionel Sambuc  * All rights reserved.
684d9c625SLionel Sambuc  *
784d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
884d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
984d9c625SLionel Sambuc  * are met:
1084d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1184d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1284d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1384d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1484d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1584d9c625SLionel Sambuc  *
1684d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1784d9c625SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1884d9c625SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1984d9c625SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2084d9c625SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2184d9c625SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2284d9c625SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2384d9c625SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2484d9c625SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2584d9c625SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2684d9c625SLionel Sambuc  */
2784d9c625SLionel Sambuc 
2884d9c625SLionel Sambuc /* Lua binding for syslog */
2984d9c625SLionel Sambuc 
3084d9c625SLionel Sambuc #include <sys/types.h>
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc #include <errno.h>
3384d9c625SLionel Sambuc #include <lua.h>
3484d9c625SLionel Sambuc #include <lauxlib.h>
3584d9c625SLionel Sambuc #include <stdlib.h>
3684d9c625SLionel Sambuc #include <syslog.h>
3784d9c625SLionel Sambuc #include <unistd.h>
3884d9c625SLionel Sambuc 
3984d9c625SLionel Sambuc int luaopen_syslog(lua_State *);
4084d9c625SLionel Sambuc 
4184d9c625SLionel Sambuc static int
syslog_openlog(lua_State * L)4284d9c625SLionel Sambuc syslog_openlog(lua_State *L)
4384d9c625SLionel Sambuc {
4484d9c625SLionel Sambuc 	const char *ident;
4584d9c625SLionel Sambuc 	int option;
4684d9c625SLionel Sambuc 	int facility;
4784d9c625SLionel Sambuc 
4884d9c625SLionel Sambuc 	ident = luaL_checkstring(L, 1);
4984d9c625SLionel Sambuc 	option = luaL_checkinteger(L, 2);
5084d9c625SLionel Sambuc 	facility = luaL_checkinteger(L, 3);
5184d9c625SLionel Sambuc 	openlog(ident, option, facility);
5284d9c625SLionel Sambuc 	return 0;
5384d9c625SLionel Sambuc }
5484d9c625SLionel Sambuc 
5584d9c625SLionel Sambuc static int
syslog_syslog(lua_State * L)5684d9c625SLionel Sambuc syslog_syslog(lua_State *L)
5784d9c625SLionel Sambuc {
58*0a6a1f1dSLionel Sambuc 	syslog((int) luaL_checkinteger(L, 1), "%s", luaL_checkstring(L, 2));
5984d9c625SLionel Sambuc 	return 0;
6084d9c625SLionel Sambuc }
6184d9c625SLionel Sambuc 
6284d9c625SLionel Sambuc static int
syslog_closelog(lua_State * L)6384d9c625SLionel Sambuc syslog_closelog(lua_State *L)
6484d9c625SLionel Sambuc {
6584d9c625SLionel Sambuc 	closelog();
6684d9c625SLionel Sambuc 	return 0;
6784d9c625SLionel Sambuc }
6884d9c625SLionel Sambuc 
6984d9c625SLionel Sambuc static int
syslog_setlogmask(lua_State * L)7084d9c625SLionel Sambuc syslog_setlogmask(lua_State *L)
7184d9c625SLionel Sambuc {
72*0a6a1f1dSLionel Sambuc 	lua_pushinteger(L, setlogmask((int) luaL_checkinteger(L, 1)));
7384d9c625SLionel Sambuc 	return 1;
7484d9c625SLionel Sambuc }
7584d9c625SLionel Sambuc 
7684d9c625SLionel Sambuc static void
syslog_set_info(lua_State * L)7784d9c625SLionel Sambuc syslog_set_info(lua_State *L)
7884d9c625SLionel Sambuc {
7984d9c625SLionel Sambuc 	lua_pushliteral(L, "_COPYRIGHT");
8084d9c625SLionel Sambuc 	lua_pushliteral(L, "Copyright (C) 2013 by "
8184d9c625SLionel Sambuc 	    "Marc Balmer <marc@msys.ch>");
8284d9c625SLionel Sambuc 	lua_settable(L, -3);
8384d9c625SLionel Sambuc 	lua_pushliteral(L, "_DESCRIPTION");
8484d9c625SLionel Sambuc 	lua_pushliteral(L, "syslog binding for Lua");
8584d9c625SLionel Sambuc 	lua_settable(L, -3);
8684d9c625SLionel Sambuc 	lua_pushliteral(L, "_VERSION");
8784d9c625SLionel Sambuc 	lua_pushliteral(L, "syslog 1.0.0");
8884d9c625SLionel Sambuc 	lua_settable(L, -3);
8984d9c625SLionel Sambuc }
9084d9c625SLionel Sambuc 
9184d9c625SLionel Sambuc struct constant {
9284d9c625SLionel Sambuc 	const char *name;
9384d9c625SLionel Sambuc 	int value;
9484d9c625SLionel Sambuc };
9584d9c625SLionel Sambuc 
9684d9c625SLionel Sambuc #define CONSTANT(NAME)		{ #NAME, NAME }
9784d9c625SLionel Sambuc 
9884d9c625SLionel Sambuc static struct constant syslog_constant[] = {
9984d9c625SLionel Sambuc 	/* syslog options */
10084d9c625SLionel Sambuc 	CONSTANT(LOG_CONS),
10184d9c625SLionel Sambuc 	CONSTANT(LOG_NDELAY),
10284d9c625SLionel Sambuc 	CONSTANT(LOG_NOWAIT),
10384d9c625SLionel Sambuc 	CONSTANT(LOG_ODELAY),
10484d9c625SLionel Sambuc 	CONSTANT(LOG_PERROR),
10584d9c625SLionel Sambuc 	CONSTANT(LOG_PID),
10684d9c625SLionel Sambuc 
10784d9c625SLionel Sambuc 	/* syslog facilities */
10884d9c625SLionel Sambuc 	CONSTANT(LOG_AUTH),
10984d9c625SLionel Sambuc 	CONSTANT(LOG_AUTHPRIV),
11084d9c625SLionel Sambuc 	CONSTANT(LOG_CRON),
11184d9c625SLionel Sambuc 	CONSTANT(LOG_DAEMON),
11284d9c625SLionel Sambuc 	CONSTANT(LOG_FTP),
11384d9c625SLionel Sambuc 	CONSTANT(LOG_KERN),
11484d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL0),
11584d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL1),
11684d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL2),
11784d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL3),
11884d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL4),
11984d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL5),
12084d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL6),
12184d9c625SLionel Sambuc 	CONSTANT(LOG_LOCAL7),
12284d9c625SLionel Sambuc 	CONSTANT(LOG_LPR),
12384d9c625SLionel Sambuc 	CONSTANT(LOG_MAIL),
12484d9c625SLionel Sambuc 	CONSTANT(LOG_NEWS),
12584d9c625SLionel Sambuc 	CONSTANT(LOG_SYSLOG),
12684d9c625SLionel Sambuc 	CONSTANT(LOG_USER),
12784d9c625SLionel Sambuc 	CONSTANT(LOG_UUCP),
12884d9c625SLionel Sambuc 
12984d9c625SLionel Sambuc 	/* syslog levels */
13084d9c625SLionel Sambuc 	CONSTANT(LOG_EMERG),
13184d9c625SLionel Sambuc 	CONSTANT(LOG_ALERT),
13284d9c625SLionel Sambuc 	CONSTANT(LOG_CRIT),
13384d9c625SLionel Sambuc 	CONSTANT(LOG_ERR),
13484d9c625SLionel Sambuc 	CONSTANT(LOG_WARNING),
13584d9c625SLionel Sambuc 	CONSTANT(LOG_NOTICE),
13684d9c625SLionel Sambuc 	CONSTANT(LOG_INFO),
13784d9c625SLionel Sambuc 	CONSTANT(LOG_DEBUG),
13884d9c625SLionel Sambuc 
13984d9c625SLionel Sambuc 	{ NULL, 0 }
14084d9c625SLionel Sambuc };
14184d9c625SLionel Sambuc 
14284d9c625SLionel Sambuc int
luaopen_syslog(lua_State * L)14384d9c625SLionel Sambuc luaopen_syslog(lua_State *L)
14484d9c625SLionel Sambuc {
14584d9c625SLionel Sambuc 	int n;
14684d9c625SLionel Sambuc 	struct luaL_Reg luasyslog[] = {
14784d9c625SLionel Sambuc 		{ "openlog",	syslog_openlog },
14884d9c625SLionel Sambuc 		{ "syslog",	syslog_syslog },
14984d9c625SLionel Sambuc 		{ "closelog",	syslog_closelog },
15084d9c625SLionel Sambuc 		{ "setlogmask",	syslog_setlogmask },
15184d9c625SLionel Sambuc 		{ NULL, NULL }
15284d9c625SLionel Sambuc 	};
15384d9c625SLionel Sambuc 
15484d9c625SLionel Sambuc #if LUA_VERSION_NUM >= 502
15584d9c625SLionel Sambuc 	luaL_newlib(L, luasyslog);
15684d9c625SLionel Sambuc #else
15784d9c625SLionel Sambuc 	luaL_register(L, "syslog", luasyslog);
15884d9c625SLionel Sambuc #endif
15984d9c625SLionel Sambuc 	syslog_set_info(L);
16084d9c625SLionel Sambuc 	for (n = 0; syslog_constant[n].name != NULL; n++) {
16184d9c625SLionel Sambuc 		lua_pushinteger(L, syslog_constant[n].value);
16284d9c625SLionel Sambuc 		lua_setfield(L, -2, syslog_constant[n].name);
16384d9c625SLionel Sambuc 	};
16484d9c625SLionel Sambuc 	return 1;
16584d9c625SLionel Sambuc }
166