util-src/pposix.c
author Matthew Wild <mwild1@gmail.com>
Sat, 06 Dec 2008 23:22:35 +0000
changeset 588 e743cb742ca6
parent 586 b828d7d47973
child 722 63456c9d0522
permissions -rw-r--r--
Remove some debugging from pposix.c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
/* Prosody IM v0.1
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
-- Copyright (C) 2008 Matthew Wild
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
-- Copyright (C) 2008 Waqas Hussain
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
-- 
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- This program is free software; you can redistribute it and/or
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
-- modify it under the terms of the GNU General Public License
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
-- as published by the Free Software Foundation; either version 2
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
-- of the License, or (at your option) any later version.
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
-- 
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
-- This program is distributed in the hope that it will be useful,
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
-- GNU General Public License for more details.
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
-- 
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
-- You should have received a copy of the GNU General Public License
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
-- along with this program; if not, write to the Free Software
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
*/
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
/* pposix.c
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
   POSIX support functions for Lua
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
*/
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
#include <stdlib.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
#include <unistd.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
#include <libgen.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
#include <sys/types.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
#include <sys/stat.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
#include <fcntl.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
#include "lua.h"
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
static int daemonize(lua_State *L)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
	pid_t pid;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
	
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	if ( getppid() == 1 )
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
		lua_pushstring(L, "already-daemonized");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
	/* Attempt initial fork */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	if((pid = fork()) < 0)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
		/* Forking failed */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
		lua_pushstring(L, "fork-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
	else if(pid != 0)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
		/* We are the parent process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
		lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
		lua_pushnumber(L, pid);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
	/* and we are the child process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	if(setsid() == -1)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
		/* We failed to become session leader */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
		/* (we probably already were) */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
		lua_pushstring(L, "setsid-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	/* Close stdin, stdout, stderr */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
/*	close(0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
	close(1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
	close(2);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
*/
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	/* Final fork, use it wisely */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
	if(fork())
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
		exit(0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	/* Show's over, let's continue */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
	lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	lua_pushnil(L);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
int luaopen_util_pposix(lua_State *L)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
	lua_newtable(L);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
	lua_pushcfunction(L, daemonize);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
	lua_setfield(L, -2, "daemonize");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
	return 1;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
};