2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 POSIX support functions for Lua
24 #define MODULE_VERSION "0.3.0"
29 #include <sys/types.h>
40 /* Daemonization support */
42 static int lc_daemonize(lua_State *L)
49 lua_pushboolean(L, 0);
50 lua_pushstring(L, "already-daemonized");
54 /* Attempt initial fork */
55 if((pid = fork()) < 0)
58 lua_pushboolean(L, 0);
59 lua_pushstring(L, "fork-failed");
64 /* We are the parent process */
65 lua_pushboolean(L, 1);
66 lua_pushnumber(L, pid);
70 /* and we are the child process */
73 /* We failed to become session leader */
74 /* (we probably already were) */
75 lua_pushboolean(L, 0);
76 lua_pushstring(L, "setsid-failed");
80 /* Close stdin, stdout, stderr */
85 /* Final fork, use it wisely */
89 /* Show's over, let's continue */
90 lua_pushboolean(L, 1);
97 char *facility_strings[] = { "auth",
118 int facility_constants[] = {
143 The parameter ident in the call of openlog() is probably stored as-is.
144 Thus, if the string it points to is changed, syslog() may start
145 prepending the changed string, and if the string it points to ceases to
146 exist, the results are undefined. Most portable is to use a string
150 char* syslog_ident = NULL;
152 int lc_syslog_open(lua_State* L)
154 int facility = luaL_checkoption(L, 2, "daemon", &facility_strings);
155 facility = facility_constants[facility];
157 luaL_checkstring(L, 1);
162 syslog_ident = strdup(lua_tostring(L, 1));
164 openlog(syslog_ident, LOG_PID, facility);
168 char *level_strings[] = {
176 int level_constants[] = {
184 int lc_syslog_log(lua_State* L)
186 int level = luaL_checkoption(L, 1, "notice", &level_strings);
187 level = level_constants[level];
189 luaL_checkstring(L, 2);
191 syslog(level, "%s", lua_tostring(L, 2));
195 int lc_syslog_close(lua_State* L)
206 int lc_syslog_setmask(lua_State* L)
208 int level_idx = luaL_checkoption(L, 1, "notice", &level_strings);
212 mask |= LOG_MASK(level_constants[level_idx]);
213 } while (++level_idx<=4);
221 int lc_getpid(lua_State* L)
223 lua_pushinteger(L, getpid());
227 /* Register functions */
229 int luaopen_util_pposix(lua_State *L)
233 lua_pushcfunction(L, lc_daemonize);
234 lua_setfield(L, -2, "daemonize");
236 lua_pushcfunction(L, lc_syslog_open);
237 lua_setfield(L, -2, "syslog_open");
239 lua_pushcfunction(L, lc_syslog_close);
240 lua_setfield(L, -2, "syslog_close");
242 lua_pushcfunction(L, lc_syslog_log);
243 lua_setfield(L, -2, "syslog_log");
245 lua_pushcfunction(L, lc_syslog_setmask);
246 lua_setfield(L, -2, "syslog_setminlevel");
248 lua_pushcfunction(L, lc_getpid);
249 lua_setfield(L, -2, "getpid");
251 lua_pushliteral(L, "pposix");
252 lua_setfield(L, -2, "_NAME");
254 lua_pushliteral(L, MODULE_VERSION);
255 lua_setfield(L, -2, "_VERSION");