util-src/pposix.c
author Tobias Markmann <tm@ayena.de>
Sun, 22 Feb 2009 20:35:41 +0100
changeset 859 43f7e342135d
parent 804 9bc1544c99b7
child 860 048aaec22b57
permissions -rw-r--r--
Adding setrlimits() binding.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
     1
/* Prosody IM v0.3
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
     4
-- Copyright 2009 Tobias Markmann
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- 
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
     7
-- COPYING file in the source package for more information.
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
     8
--
586
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
766
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
    11
/*
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
    12
* pposix.c
433a5226267f Licensing/version updates for some files (forgot to commit, doh...)
Matthew Wild <mwild1@gmail.com>
parents: 730
diff changeset
    13
* POSIX support functions for Lua
586
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
727
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
    16
#define MODULE_VERSION "0.3.0"
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
    17
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
#include <stdlib.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
#include <unistd.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
#include <libgen.h>
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
    21
#include <sys/resource.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
#include <sys/types.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
#include <sys/stat.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
#include <fcntl.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    26
#include <syslog.h>
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
    27
#include <pwd.h>
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    28
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    29
#include <string.h>
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
    30
#include <errno.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
#include "lua.h"
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    32
#include "lauxlib.h"
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    33
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    34
/* Daemonization support */
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
    36
static int lc_daemonize(lua_State *L)
586
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
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
	pid_t pid;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	if ( getppid() == 1 )
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
		lua_pushstring(L, "already-daemonized");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	}
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
	/* Attempt initial fork */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
	if((pid = fork()) < 0)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		/* Forking failed */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
		lua_pushstring(L, "fork-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
	else if(pid != 0)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
		/* We are the parent process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
		lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
		lua_pushnumber(L, pid);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	}
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
	/* and we are the child process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
	if(setsid() == -1)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
		/* We failed to become session leader */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
		/* (we probably already were) */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		lua_pushstring(L, "setsid-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
	/* Close stdin, stdout, stderr */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
/*	close(0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	close(1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
	close(2);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
*/
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
	/* Final fork, use it wisely */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	if(fork())
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		exit(0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	/* Show's over, let's continue */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	lua_pushnil(L);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
	return 2;
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
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    89
/* Syslog support */
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    90
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
    91
const char * const facility_strings[] = {
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
    92
					"auth",
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    93
					"authpriv",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    94
					"cron",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    95
					"daemon",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    96
					"ftp",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    97
					"kern",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    98
					"local0",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    99
					"local1",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   100
					"local2",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   101
					"local3",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   102
					"local4",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   103
					"local5",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   104
					"local6",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   105
					"local7",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   106
					"lpr",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   107
					"mail",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   108
					"syslog",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   109
					"user",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   110
					"uucp",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   111
					NULL
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   112
				};
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   113
int facility_constants[] =	{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   114
					LOG_AUTH,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   115
					LOG_AUTHPRIV,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   116
					LOG_CRON,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   117
					LOG_DAEMON,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   118
					LOG_FTP,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   119
					LOG_KERN,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   120
					LOG_LOCAL0,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   121
					LOG_LOCAL1,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   122
					LOG_LOCAL2,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   123
					LOG_LOCAL3,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   124
					LOG_LOCAL4,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   125
					LOG_LOCAL5,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   126
					LOG_LOCAL6,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   127
					LOG_LOCAL7,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   128
					LOG_LPR,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   129
					LOG_MAIL,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   130
					LOG_NEWS,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   131
					LOG_SYSLOG,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   132
					LOG_USER,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   133
					LOG_UUCP,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   134
					-1
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   135
				};
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   136
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   137
/* "
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   138
       The parameter ident in the call of openlog() is probably stored  as-is.
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   139
       Thus,  if  the  string  it  points  to  is  changed, syslog() may start
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   140
       prepending the changed string, and if the string it points to ceases to
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   141
       exist,  the  results  are  undefined.  Most portable is to use a string
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   142
       constant.
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   143
   " -- syslog manpage
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   144
*/ 
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   145
char* syslog_ident = NULL;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   146
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   147
int lc_syslog_open(lua_State* L)
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   148
{
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   149
	int facility = luaL_checkoption(L, 2, "daemon", facility_strings);
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   150
	facility = facility_constants[facility];
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   151
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   152
	luaL_checkstring(L, 1);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   153
	
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   154
	if(syslog_ident)
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   155
		free(syslog_ident);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   156
	
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   157
	syslog_ident = strdup(lua_tostring(L, 1));
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   158
	
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   159
	openlog(syslog_ident, LOG_PID, facility);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   160
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   161
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   162
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   163
const char * const level_strings[] = {
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   164
				"debug",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   165
				"info",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   166
				"notice",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   167
				"warn",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   168
				"error",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   169
				NULL
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   170
			};
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   171
int level_constants[] = 	{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   172
				LOG_DEBUG,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   173
				LOG_INFO,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   174
				LOG_NOTICE,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   175
				LOG_WARNING,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   176
				LOG_EMERG,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   177
				-1
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   178
			};
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   179
int lc_syslog_log(lua_State* L)
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   180
{
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   181
	int level = luaL_checkoption(L, 1, "notice", level_strings);
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   182
	level = level_constants[level];
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   183
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   184
	luaL_checkstring(L, 2);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   185
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   186
	syslog(level, "%s", lua_tostring(L, 2));
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   187
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   188
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   189
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   190
int lc_syslog_close(lua_State* L)
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   191
{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   192
	closelog();
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   193
	if(syslog_ident)
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   194
	{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   195
		free(syslog_ident);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   196
		syslog_ident = NULL;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   197
	}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   198
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   199
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   200
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   201
int lc_syslog_setmask(lua_State* L)
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   202
{
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   203
	int level_idx = luaL_checkoption(L, 1, "notice", level_strings);
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   204
	int mask = 0;
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   205
	do
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   206
	{
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   207
		mask |= LOG_MASK(level_constants[level_idx]);
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   208
	} while (++level_idx<=4);
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   209
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   210
	setlogmask(mask);
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   211
	return 0;
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   212
}
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   213
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   214
/* getpid */
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   215
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   216
int lc_getpid(lua_State* L)
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   217
{
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   218
	lua_pushinteger(L, getpid());
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   219
	return 1;
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   220
}
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   221
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   222
/* UID/GID functions */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   223
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   224
int lc_getuid(lua_State* L)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   225
{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   226
	lua_pushinteger(L, getuid());
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   227
	return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   228
}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   229
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   230
int lc_getgid(lua_State* L)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   231
{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   232
	lua_pushinteger(L, getgid());
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   233
	return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   234
}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   235
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   236
int lc_setuid(lua_State* L)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   237
{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   238
	int uid = -1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   239
	if(lua_gettop(L) < 1)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   240
		return 0;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   241
	if(!lua_isnumber(L, 1) && lua_tostring(L, 1))
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   242
	{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   243
		/* Passed UID is actually a string, so look up the UID */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   244
		struct passwd *p;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   245
		p = getpwnam(lua_tostring(L, 1));
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   246
		if(!p)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   247
		{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   248
			lua_pushboolean(L, 0);
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   249
			lua_pushstring(L, "no-such-user");
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   250
			return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   251
		}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   252
		uid = p->pw_uid;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   253
	}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   254
	else
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   255
	{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   256
		uid = lua_tonumber(L, 1);
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   257
	}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   258
	
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   259
	if(uid>-1)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   260
	{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   261
		/* Ok, attempt setuid */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   262
		errno = 0;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   263
		if(setuid(uid))
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   264
		{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   265
			/* Fail */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   266
			lua_pushboolean(L, 0);
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   267
			switch(errno)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   268
			{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   269
			case EINVAL:
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   270
				lua_pushstring(L, "invalid-uid");
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   271
				break;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   272
			case EPERM:
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   273
				lua_pushstring(L, "permission-denied");
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   274
				break;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   275
			default:
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   276
				lua_pushstring(L, "unknown-error");
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   277
			}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   278
			return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   279
		}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   280
		else
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   281
		{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   282
			/* Success! */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   283
			lua_pushboolean(L, 1);
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   284
			return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   285
		}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   286
	}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   287
	
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   288
	/* Seems we couldn't find a valid UID to switch to */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   289
	lua_pushboolean(L, 0);
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   290
	lua_pushstring(L, "invalid-uid");
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   291
	return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   292
}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   293
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   294
/*	Like POSIX's setrlimit()/getrlimit() API functions.
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   295
 *	
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   296
 *	Syntax:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   297
 *	pposix.setrlimit( resource, soft limit, hard limit)
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   298
 *	
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   299
 *	Any negative limit will be replace with the current limit by an additional call of getrlimit().
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   300
 *	
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   301
 *	Example usage:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   302
 *	pposix.setrlimit("NOFILE", 1000, 2000)
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   303
 */
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   304
int string2resource(const char *s) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   305
	if (!strcmp(resource, "CORE")) return RLIMIT_CORE;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   306
	if (!strcmp(resource, "CPU")) return RLIMIT_CPU;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   307
	if (!strcmp(resource, "DATA")) return RLIMIT_DATA;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   308
	if (!strcmp(resource, "FSIZE")) return RLIMIT_FSIZE;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   309
	if (!strcmp(resource, "MEMLOCK")) return RLIMIT_MEMLOCK;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   310
	if (!strcmp(resource, "NOFILE")) return RLIMIT_NOFILE;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   311
	if (!strcmp(resource, "NPROC")) return RLIMIT_NPROC;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   312
	if (!strcmp(resource, "RSS")) return RLIMIT_RSS;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   313
	if (!strcmp(resource, "STACK")) return RLIMIT_STACK;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   314
	return -1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   315
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   316
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   317
int lc_setrlimit(lua_State *L) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   318
	int arguments = lua_gettop(L);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   319
	int softlimit = -1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   320
	int hardlimit = -1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   321
	const char *resource = NULL;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   322
	int rid = -1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   323
	if(arguments < 1 || arguments > 3) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   324
		lua_pushboolean(L, 0);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   325
		lua_pushstring(L, "Wrong number of arguments");
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   326
	}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   327
	
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   328
	resource = luaL_checkstring(L, 1);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   329
	softlimit = luaL_checkinteger(L, 2);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   330
	hardlimit = luaL_checkinteger(L, 3);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   331
	
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   332
	rid = string2resource(resource);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   333
	if (rid != -1) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   334
		rlimit lim;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   335
		rlimit lim_current;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   336
		
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   337
		if (softlimit < 0 || hardlimit < 0) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   338
			if (getrlimit(rid, &lim_current)) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   339
				lua_pushboolean(L, 0);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   340
				lua_pushstring(L, "getrlimit() failed.");
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   341
				return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   342
			}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   343
		}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   344
		
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   345
		if (softimit < 0) lim.rlim_cur = lim_current.rlim_cur;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   346
			else lim.rlim_cur = softlimit;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   347
		if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   348
			else lim.rlim_max = hardlimit;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   349
		
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   350
		if (setrlimit(rid, &lim)) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   351
			lua_pushboolean(L, 0);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   352
			lua_pushstring(L, "setrlimit() failed.");
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   353
			return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   354
		}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   355
	} else {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   356
		lua_pushboolean(L, 0);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   357
		lua_pushstring(L, "Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard.");
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   358
		return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   359
	}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   360
	lua_pushboolean(L, 1);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   361
	return 1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   362
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   363
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   364
int lc_getrlimit(lua_State *L) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   365
	return 0;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   366
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   367
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   368
/* Register functions */
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   369
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   370
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
   371
{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   372
	lua_newtable(L);
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   373
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   374
	lua_pushcfunction(L, lc_daemonize);
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
	lua_setfield(L, -2, "daemonize");
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   376
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   377
	lua_pushcfunction(L, lc_syslog_open);
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   378
	lua_setfield(L, -2, "syslog_open");
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   379
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   380
	lua_pushcfunction(L, lc_syslog_close);
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   381
	lua_setfield(L, -2, "syslog_close");
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   382
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   383
	lua_pushcfunction(L, lc_syslog_log);
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   384
	lua_setfield(L, -2, "syslog_log");
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   385
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   386
	lua_pushcfunction(L, lc_syslog_setmask);
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   387
	lua_setfield(L, -2, "syslog_setminlevel");
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   388
723
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   389
	lua_pushcfunction(L, lc_getpid);
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   390
	lua_setfield(L, -2, "getpid");
c1e7d280c174 mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents: 722
diff changeset
   391
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   392
	lua_pushcfunction(L, lc_getuid);
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   393
	lua_setfield(L, -2, "getuid");
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   394
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   395
	lua_pushcfunction(L, lc_setuid);
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   396
	lua_setfield(L, -2, "setuid");
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   397
	
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   398
	lua_pushcfunction(L, lc_setrlimit);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   399
	lua_setfield(L, -2, "setrlimit");
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   400
	
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   401
	lua_pushcfunction(L, lc_getrlimit);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   402
	lua_setfield(L, -2, "getrlimit");
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   403
727
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   404
	lua_pushliteral(L, "pposix");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   405
	lua_setfield(L, -2, "_NAME");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   406
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   407
	lua_pushliteral(L, MODULE_VERSION);
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   408
	lua_setfield(L, -2, "_VERSION");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   409
	
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   410
	return 1;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   411
};