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
27 #include <sys/types.h>
33 static int daemonize(lua_State *L)
40 lua_pushboolean(L, 0);
41 lua_pushstring(L, "already-daemonized");
45 /* Attempt initial fork */
46 if((pid = fork()) < 0)
49 lua_pushboolean(L, 0);
50 lua_pushstring(L, "fork-failed");
55 /* We are the parent process */
56 printf("We are the parent, pid of child is %d\n", (int)pid);
57 lua_pushboolean(L, 1);
58 lua_pushnumber(L, pid);
62 printf("We are the child, pid reports %d\n", (int)pid);
63 /* and we are the child process */
66 /* We failed to become session leader */
67 /* (we probably already were) */
68 lua_pushboolean(L, 0);
69 lua_pushstring(L, "setsid-failed");
73 /* Close stdin, stdout, stderr */
78 /* Final fork, use it wisely */
82 /* Show's over, let's continue */
83 lua_pushboolean(L, 1);
88 int luaopen_util_pposix(lua_State *L)
91 lua_pushcfunction(L, daemonize);
92 lua_setfield(L, -2, "daemonize");