util-src/pposix.c
author Kim Alvefur <zash@zash.se>
Wed, 17 Sep 2014 14:30:29 +0200
changeset 6416 a552f4170aed
parent 6414 6c8f6364bc48
child 6618 8e4572a642cb
permissions -rw-r--r--
util-src/*.c: Add macro for compiling with Lua 5.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2830
diff changeset
     1
/* Prosody IM
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2830
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2830
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
864
416c812acde5 pposix: Small fix for copyright notice
Matthew Wild <mwild1@gmail.com>
parents: 861
diff changeset
     4
-- Copyright (C) 2009 Tobias Markmann
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
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
5451
941ad88db8f3 mod_posix, util.pposix: Bump version for API change
Matthew Wild <mwild1@gmail.com>
parents: 5449
diff changeset
    16
#define MODULE_VERSION "0.3.6"
727
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>
2438
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
    19
#include <math.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
#include <unistd.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
#include <libgen.h>
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
    22
#include <sys/resource.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
#include <sys/types.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
#include <sys/stat.h>
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
    25
#include <sys/utsname.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
#include <fcntl.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    28
#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
    29
#include <pwd.h>
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
    30
#include <grp.h>
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    31
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    32
#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
    33
#include <errno.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
#include "lua.h"
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    35
#include "lualib.h"
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    36
#include "lauxlib.h"
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    37
6416
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
    38
#if (LUA_VERSION_NUM == 502)
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
    39
#define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
    40
#endif
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
    41
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    42
#include <fcntl.h>
5812
57ebf93ec720 util.pposix: Fix building on non-Linux with glibc
Kim Alvefur <zash@zash.se>
parents: 5717
diff changeset
    43
#if defined(__linux__) && defined(_GNU_SOURCE)
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    44
#include <linux/falloc.h>
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    45
#endif
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    46
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
    47
#if (defined(_SVID_SOURCE) && !defined(WITHOUT_MALLINFO))
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
    48
	#include <malloc.h>
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
    49
	#define WITH_MALLINFO
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
    50
#endif
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
    51
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    52
/* Daemonization support */
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
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
    54
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
    55
{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
	pid_t pid;
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
    58
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
	if ( getppid() == 1 )
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
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
		lua_pushstring(L, "already-daemonized");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
    65
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
	/* Attempt initial fork */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
	if((pid = fork()) < 0)
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
	{
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		/* Forking failed */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
		lua_pushstring(L, "fork-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
		return 2;
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
	else if(pid != 0)
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
		/* We are the parent process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
		lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
		lua_pushnumber(L, pid);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
    81
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	/* and we are the child process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	if(setsid() == -1)
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
		/* We failed to become session leader */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
		/* (we probably already were) */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
		lua_pushstring(L, "setsid-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
	/* Close stdin, stdout, stderr */
1195
6b91a2b39680 Close std{in,out,err} when daemonizing
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    93
	close(0);
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
	close(1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
	close(2);
5176
7d0a8b255a37 util.pposix: Prevent FDs 0, 1 and 2 from being assigned to connections
Kim Alvefur <zash@zash.se>
parents: 5068
diff changeset
    96
	/* Make sure accidental use of FDs 0, 1, 2 don't cause weirdness */
7d0a8b255a37 util.pposix: Prevent FDs 0, 1 and 2 from being assigned to connections
Kim Alvefur <zash@zash.se>
parents: 5068
diff changeset
    97
	open("/dev/null", O_RDONLY);
7d0a8b255a37 util.pposix: Prevent FDs 0, 1 and 2 from being assigned to connections
Kim Alvefur <zash@zash.se>
parents: 5068
diff changeset
    98
	open("/dev/null", O_WRONLY);
7d0a8b255a37 util.pposix: Prevent FDs 0, 1 and 2 from being assigned to connections
Kim Alvefur <zash@zash.se>
parents: 5068
diff changeset
    99
	open("/dev/null", O_WRONLY);
1195
6b91a2b39680 Close std{in,out,err} when daemonizing
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   100
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
	/* Final fork, use it wisely */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
	if(fork())
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
		exit(0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
	/* Show's over, let's continue */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
	lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
	lua_pushnil(L);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
	return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   111
/* Syslog support */
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   112
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   113
const char * const facility_strings[] = {
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   114
					"auth",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   115
#if !(defined(sun) || defined(__sun))
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   116
					"authpriv",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   117
#endif
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   118
					"cron",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   119
					"daemon",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   120
#if !(defined(sun) || defined(__sun))
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   121
					"ftp",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   122
#endif
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   123
					"kern",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   124
					"local0",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   125
					"local1",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   126
					"local2",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   127
					"local3",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   128
					"local4",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   129
					"local5",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   130
					"local6",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   131
					"local7",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   132
					"lpr",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   133
					"mail",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   134
					"syslog",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   135
					"user",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   136
					"uucp",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   137
					NULL
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   138
				};
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   139
int facility_constants[] =	{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   140
					LOG_AUTH,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   141
#if !(defined(sun) || defined(__sun))
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   142
					LOG_AUTHPRIV,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   143
#endif
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   144
					LOG_CRON,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   145
					LOG_DAEMON,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   146
#if !(defined(sun) || defined(__sun))
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   147
					LOG_FTP,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   148
#endif
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   149
					LOG_KERN,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   150
					LOG_LOCAL0,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   151
					LOG_LOCAL1,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   152
					LOG_LOCAL2,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   153
					LOG_LOCAL3,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   154
					LOG_LOCAL4,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   155
					LOG_LOCAL5,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   156
					LOG_LOCAL6,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   157
					LOG_LOCAL7,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   158
					LOG_LPR,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   159
					LOG_MAIL,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   160
					LOG_NEWS,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   161
					LOG_SYSLOG,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   162
					LOG_USER,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   163
					LOG_UUCP,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   164
					-1
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   165
				};
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   166
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   167
/* "
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   168
       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
   169
       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
   170
       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
   171
       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
   172
       constant.
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   173
   " -- syslog manpage
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   174
*/
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   175
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
   176
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
   177
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
   178
{
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   179
	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
   180
	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
   181
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   182
	luaL_checkstring(L, 1);
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   183
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   184
	if(syslog_ident)
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   185
		free(syslog_ident);
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   186
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   187
	syslog_ident = strdup(lua_tostring(L, 1));
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   188
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   189
	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
   190
	return 0;
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
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   193
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
   194
				"debug",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   195
				"info",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   196
				"notice",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   197
				"warn",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   198
				"error",
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   199
				NULL
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   200
			};
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   201
int level_constants[] = 	{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   202
				LOG_DEBUG,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   203
				LOG_INFO,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   204
				LOG_NOTICE,
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   205
				LOG_WARNING,
1565
f1eeb049a0a5 pposix: Change LOG_EMERG -> LOG_CRIT, which makes more sense, thanks to intosi for reporting and an initial patch
Matthew Wild <mwild1@gmail.com>
parents: 1195
diff changeset
   206
				LOG_CRIT,
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   207
				-1
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   208
			};
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
   209
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
   210
{
5449
35a271b4b920 util.pposix: syslog(): Support an optional source parameter (producing messages of the form '<source>: <message>'
Matthew Wild <mwild1@gmail.com>
parents: 5359
diff changeset
   211
	int level = level_constants[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
   212
5449
35a271b4b920 util.pposix: syslog(): Support an optional source parameter (producing messages of the form '<source>: <message>'
Matthew Wild <mwild1@gmail.com>
parents: 5359
diff changeset
   213
	if(lua_gettop(L) == 3)
35a271b4b920 util.pposix: syslog(): Support an optional source parameter (producing messages of the form '<source>: <message>'
Matthew Wild <mwild1@gmail.com>
parents: 5359
diff changeset
   214
		syslog(level, "%s: %s", luaL_checkstring(L, 2), luaL_checkstring(L, 3));
35a271b4b920 util.pposix: syslog(): Support an optional source parameter (producing messages of the form '<source>: <message>'
Matthew Wild <mwild1@gmail.com>
parents: 5359
diff changeset
   215
	else
35a271b4b920 util.pposix: syslog(): Support an optional source parameter (producing messages of the form '<source>: <message>'
Matthew Wild <mwild1@gmail.com>
parents: 5359
diff changeset
   216
		syslog(level, "%s", lua_tostring(L, 2));
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   217
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   218
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   219
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   220
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
   221
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
   222
{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   223
	closelog();
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   224
	if(syslog_ident)
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   225
	{
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   226
		free(syslog_ident);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   227
		syslog_ident = NULL;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   228
	}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   229
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   230
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   231
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   232
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
   233
{
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   234
	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
   235
	int mask = 0;
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   236
	do
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   237
	{
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   238
		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
   239
	} 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
   240
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   241
	setlogmask(mask);
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   242
	return 0;
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   243
}
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   244
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
   245
/* 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
   246
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
   247
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
   248
{
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
   249
	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
   250
	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
   251
}
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
   252
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   253
/* UID/GID functions */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   254
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   255
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
   256
{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   257
	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
   258
	return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   259
}
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
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
   262
{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   263
	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
   264
	return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   265
}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   266
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   267
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
   268
{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   269
	int uid = -1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   270
	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
   271
		return 0;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   272
	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
   273
	{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   274
		/* 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
   275
		struct passwd *p;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   276
		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
   277
		if(!p)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   278
		{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   279
			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
   280
			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
   281
			return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   282
		}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   283
		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
   284
	}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   285
	else
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
		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
   288
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   289
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   290
	if(uid>-1)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   291
	{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   292
		/* Ok, attempt setuid */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   293
		errno = 0;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   294
		if(setuid(uid))
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   295
		{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   296
			/* Fail */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   297
			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
   298
			switch(errno)
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   299
			{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   300
			case EINVAL:
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   301
				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
   302
				break;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   303
			case EPERM:
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   304
				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
   305
				break;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   306
			default:
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   307
				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
   308
			}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   309
			return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   310
		}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   311
		else
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   312
		{
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   313
			/* Success! */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   314
			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
   315
			return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   316
		}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   317
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   318
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   319
	/* 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
   320
	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
   321
	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
   322
	return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   323
}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   324
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   325
int lc_setgid(lua_State* L)
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   326
{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   327
	int gid = -1;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   328
	if(lua_gettop(L) < 1)
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   329
		return 0;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   330
	if(!lua_isnumber(L, 1) && lua_tostring(L, 1))
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   331
	{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   332
		/* Passed GID is actually a string, so look up the GID */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   333
		struct group *g;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   334
		g = getgrnam(lua_tostring(L, 1));
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   335
		if(!g)
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   336
		{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   337
			lua_pushboolean(L, 0);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   338
			lua_pushstring(L, "no-such-group");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   339
			return 2;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   340
		}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   341
		gid = g->gr_gid;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   342
	}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   343
	else
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   344
	{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   345
		gid = lua_tonumber(L, 1);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   346
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   347
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   348
	if(gid>-1)
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   349
	{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   350
		/* Ok, attempt setgid */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   351
		errno = 0;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   352
		if(setgid(gid))
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   353
		{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   354
			/* Fail */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   355
			lua_pushboolean(L, 0);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   356
			switch(errno)
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   357
			{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   358
			case EINVAL:
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   359
				lua_pushstring(L, "invalid-gid");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   360
				break;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   361
			case EPERM:
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   362
				lua_pushstring(L, "permission-denied");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   363
				break;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   364
			default:
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   365
				lua_pushstring(L, "unknown-error");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   366
			}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   367
			return 2;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   368
		}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   369
		else
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   370
		{
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   371
			/* Success! */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   372
			lua_pushboolean(L, 1);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   373
			return 1;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   374
		}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   375
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   376
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   377
	/* Seems we couldn't find a valid GID to switch to */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   378
	lua_pushboolean(L, 0);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   379
	lua_pushstring(L, "invalid-gid");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   380
	return 2;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   381
}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   382
3471
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   383
int lc_initgroups(lua_State* L)
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   384
{
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   385
	int ret;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   386
	gid_t gid;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   387
	struct passwd *p;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   388
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   389
	if(!lua_isstring(L, 1))
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   390
	{
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   391
		lua_pushnil(L);
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   392
		lua_pushstring(L, "invalid-username");
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   393
		return 2;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   394
	}
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   395
	p = getpwnam(lua_tostring(L, 1));
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   396
	if(!p)
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   397
	{
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   398
		lua_pushnil(L);
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   399
		lua_pushstring(L, "no-such-user");
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   400
		return 2;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   401
	}
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   402
	if(lua_gettop(L) < 2)
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   403
		lua_pushnil(L);
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   404
	switch(lua_type(L, 2))
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   405
	{
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   406
	case LUA_TNIL:
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   407
		gid = p->pw_gid;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   408
		break;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   409
	case LUA_TNUMBER:
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   410
		gid = lua_tointeger(L, 2);
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   411
		break;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   412
	default:
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   413
		lua_pushnil(L);
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   414
		lua_pushstring(L, "invalid-gid");
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   415
		return 2;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   416
	}
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   417
	ret = initgroups(lua_tostring(L, 1), gid);
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   418
	if(ret)
3471
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   419
	{
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   420
		switch(errno)
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   421
		{
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   422
		case ENOMEM:
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   423
			lua_pushnil(L);
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   424
			lua_pushstring(L, "no-memory");
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   425
			break;
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   426
		case EPERM:
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   427
			lua_pushnil(L);
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   428
			lua_pushstring(L, "permission-denied");
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   429
			break;
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   430
		default:
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   431
			lua_pushnil(L);
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   432
			lua_pushstring(L, "unknown-error");
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   433
		}
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   434
	}
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   435
	else
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   436
	{
3471
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   437
		lua_pushboolean(L, 1);
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   438
		lua_pushnil(L);
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   439
	}
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   440
	return 2;
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   441
}
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   442
2438
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   443
int lc_umask(lua_State* L)
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   444
{
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   445
	char old_mode_string[7];
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   446
	mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8));
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   447
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   448
	snprintf(old_mode_string, sizeof(old_mode_string), "%03o", old_mode);
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   449
	old_mode_string[sizeof(old_mode_string)-1] = 0;
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   450
	lua_pushstring(L, old_mode_string);
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   451
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   452
	return 1;
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   453
}
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   454
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   455
int lc_mkdir(lua_State* L)
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   456
{
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   457
	int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   458
		| S_IRGRP | S_IWGRP | S_IXGRP
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   459
		| S_IROTH | S_IXOTH); /* mode 775 */
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   460
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   461
	lua_pushboolean(L, ret==0);
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   462
	if(ret)
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   463
	{
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   464
		lua_pushstring(L, strerror(errno));
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   465
		return 2;
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   466
	}
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   467
	return 1;
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   468
}
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   469
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   470
/*	Like POSIX's setrlimit()/getrlimit() API functions.
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   471
 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   472
 *	Syntax:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   473
 *	pposix.setrlimit( resource, soft limit, hard limit)
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   474
 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   475
 *	Any negative limit will be replace with the current limit by an additional call of getrlimit().
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   476
 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   477
 *	Example usage:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   478
 *	pposix.setrlimit("NOFILE", 1000, 2000)
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   479
 */
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   480
int string2resource(const char *s) {
861
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   481
	if (!strcmp(s, "CORE")) return RLIMIT_CORE;
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   482
	if (!strcmp(s, "CPU")) return RLIMIT_CPU;
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   483
	if (!strcmp(s, "DATA")) return RLIMIT_DATA;
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   484
	if (!strcmp(s, "FSIZE")) return RLIMIT_FSIZE;
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   485
	if (!strcmp(s, "NOFILE")) return RLIMIT_NOFILE;
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   486
	if (!strcmp(s, "STACK")) return RLIMIT_STACK;
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   487
#if !(defined(sun) || defined(__sun))
861
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   488
	if (!strcmp(s, "MEMLOCK")) return RLIMIT_MEMLOCK;
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   489
	if (!strcmp(s, "NPROC")) return RLIMIT_NPROC;
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   490
	if (!strcmp(s, "RSS")) return RLIMIT_RSS;
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   491
#endif
5359
43517e649c53 util.pposix: Allow fetching RLIMIT_NICE when available
Matthew Wild <mwild1@gmail.com>
parents: 5176
diff changeset
   492
#ifdef RLIMIT_NICE
43517e649c53 util.pposix: Allow fetching RLIMIT_NICE when available
Matthew Wild <mwild1@gmail.com>
parents: 5176
diff changeset
   493
	if (!strcmp(s, "NICE")) return RLIMIT_NICE;
43517e649c53 util.pposix: Allow fetching RLIMIT_NICE when available
Matthew Wild <mwild1@gmail.com>
parents: 5176
diff changeset
   494
#endif
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   495
	return -1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   496
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   497
5777
c98f11bcd0ea util.pposix: Fix overflow in rlimit argument conversion (thanks gcc, now be quiet please)
Kim Alvefur <zash@zash.se>
parents: 5720
diff changeset
   498
unsigned long int arg_to_rlimit(lua_State* L, int idx, rlim_t current) {
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   499
	switch(lua_type(L, idx)) {
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   500
	case LUA_TSTRING:
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   501
		if(strcmp(lua_tostring(L, idx), "unlimited") == 0)
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   502
			return RLIM_INFINITY;
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   503
	case LUA_TNUMBER:
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   504
		return lua_tointeger(L, idx);
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   505
	case LUA_TNONE:
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   506
	case LUA_TNIL:
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   507
		return current;
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   508
	default:
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   509
		return luaL_argerror(L, idx, "unexpected type");
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   510
	}
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   511
}
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   512
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   513
int lc_setrlimit(lua_State *L) {
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   514
	struct rlimit lim;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   515
	int arguments = lua_gettop(L);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   516
	int rid = -1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   517
	if(arguments < 1 || arguments > 3) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   518
		lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   519
		lua_pushstring(L, "incorrect-arguments");
5717
bf98912a4f5b util.pposix: setrlimit(): Add missing return on error when an incorrect number of arguments are passed
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   520
		return 2;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   521
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   522
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   523
	rid = string2resource(luaL_checkstring(L, 1));
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   524
	if (rid == -1) {
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   525
		lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   526
		lua_pushstring(L, "invalid-resource");
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   527
		return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   528
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   529
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   530
	/* Fetch current values to use as defaults */
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   531
	if (getrlimit(rid, &lim)) {
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   532
		lua_pushboolean(L, 0);
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   533
		lua_pushstring(L, "getrlimit-failed");
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   534
		return 2;
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   535
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   536
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   537
	lim.rlim_cur = arg_to_rlimit(L, 2, lim.rlim_cur);
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   538
	lim.rlim_max = arg_to_rlimit(L, 3, lim.rlim_max);
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   539
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   540
	if (setrlimit(rid, &lim)) {
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   541
		lua_pushboolean(L, 0);
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   542
		lua_pushstring(L, "setrlimit-failed");
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   543
		return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   544
	}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   545
	lua_pushboolean(L, 1);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   546
	return 1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   547
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   548
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   549
int lc_getrlimit(lua_State *L) {
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   550
	int arguments = lua_gettop(L);
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   551
	const char *resource = NULL;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   552
	int rid = -1;
861
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   553
	struct rlimit lim;
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   554
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   555
	if (arguments != 1) {
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   556
		lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   557
		lua_pushstring(L, "invalid-arguments");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   558
		return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   559
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   560
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   561
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   562
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   563
	resource = luaL_checkstring(L, 1);
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   564
	rid = string2resource(resource);
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   565
	if (rid != -1) {
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   566
		if (getrlimit(rid, &lim)) {
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   567
			lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   568
			lua_pushstring(L, "getrlimit-failed.");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   569
			return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   570
		}
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   571
	} else {
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   572
		/* Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard. */
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   573
		lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   574
		lua_pushstring(L, "invalid-resource");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   575
		return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   576
	}
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   577
	lua_pushboolean(L, 1);
5719
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   578
	if(lim.rlim_cur == RLIM_INFINITY)
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   579
		lua_pushstring(L, "unlimited");
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   580
	else
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   581
		lua_pushnumber(L, lim.rlim_cur);
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   582
	if(lim.rlim_max == RLIM_INFINITY)
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   583
		lua_pushstring(L, "unlimited");
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   584
	else
84025249fc04 util.pposix: Correctly handle 'unlimited' limits (RLIM_INFINITY), by returning and accepting the string 'unlimited' in get/setrlimit()
Matthew Wild <mwild1@gmail.com>
parents: 5451
diff changeset
   585
		lua_pushnumber(L, lim.rlim_max);
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   586
	return 3;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   587
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   588
2830
1df57426263a util.pposix: Fix return type of lc_abort to shush compiler warning
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   589
int lc_abort(lua_State* L)
2060
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
   590
{
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
   591
	abort();
2830
1df57426263a util.pposix: Fix return type of lc_abort to shush compiler warning
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   592
	return 0;
2060
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
   593
}
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
   594
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   595
int lc_uname(lua_State* L)
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   596
{
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   597
	struct utsname uname_info;
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   598
	if(uname(&uname_info) != 0)
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   599
	{
3482
e1a4f7b15caf util.pposix: uname(): Fix to push nil,err in case of error
Matthew Wild <mwild1@gmail.com>
parents: 3481
diff changeset
   600
		lua_pushnil(L);
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   601
		lua_pushstring(L, strerror(errno));
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   602
		return 2;
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   603
	}
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   604
	lua_newtable(L);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   605
	lua_pushstring(L, uname_info.sysname);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   606
	lua_setfield(L, -2, "sysname");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   607
	lua_pushstring(L, uname_info.nodename);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   608
	lua_setfield(L, -2, "nodename");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   609
	lua_pushstring(L, uname_info.release);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   610
	lua_setfield(L, -2, "release");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   611
	lua_pushstring(L, uname_info.version);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   612
	lua_setfield(L, -2, "version");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   613
	lua_pushstring(L, uname_info.machine);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   614
	lua_setfield(L, -2, "machine");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   615
	return 1;
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   616
}
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   617
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   618
int lc_setenv(lua_State* L)
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   619
{
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   620
	const char *var = luaL_checkstring(L, 1);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   621
	const char *value;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   622
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   623
	/* If the second argument is nil or nothing, unset the var */
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   624
	if(lua_isnoneornil(L, 2))
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   625
	{
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   626
		if(unsetenv(var) != 0)
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   627
		{
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   628
			lua_pushnil(L);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   629
			lua_pushstring(L, strerror(errno));
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   630
			return 2;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   631
		}
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   632
		lua_pushboolean(L, 1);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   633
		return 1;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   634
	}
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   635
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   636
	value = luaL_checkstring(L, 2);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   637
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   638
	if(setenv(var, value, 1) != 0)
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   639
	{
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   640
		lua_pushnil(L);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   641
		lua_pushstring(L, strerror(errno));
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   642
		return 2;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   643
	}
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   644
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   645
	lua_pushboolean(L, 1);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   646
	return 1;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   647
}
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   648
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   649
#ifdef WITH_MALLINFO
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   650
int lc_meminfo(lua_State* L)
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   651
{
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   652
	struct mallinfo info = mallinfo();
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   653
	lua_newtable(L);
4950
02e5e9fa37b8 util.pposix: Add comments to mallinfo fields we use, so I don't forget tomorrow what they mean
Matthew Wild <mwild1@gmail.com>
parents: 4946
diff changeset
   654
	/* This is the total size of memory allocated with sbrk by malloc, in bytes. */
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   655
	lua_pushinteger(L, info.arena);
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   656
	lua_setfield(L, -2, "allocated");
4950
02e5e9fa37b8 util.pposix: Add comments to mallinfo fields we use, so I don't forget tomorrow what they mean
Matthew Wild <mwild1@gmail.com>
parents: 4946
diff changeset
   657
	/* This is the total size of memory allocated with mmap, in bytes. */
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   658
	lua_pushinteger(L, info.hblkhd);
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   659
	lua_setfield(L, -2, "allocated_mmap");
4950
02e5e9fa37b8 util.pposix: Add comments to mallinfo fields we use, so I don't forget tomorrow what they mean
Matthew Wild <mwild1@gmail.com>
parents: 4946
diff changeset
   660
	/* This is the total size of memory occupied by chunks handed out by malloc. */
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   661
	lua_pushinteger(L, info.uordblks);
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   662
	lua_setfield(L, -2, "used");
4950
02e5e9fa37b8 util.pposix: Add comments to mallinfo fields we use, so I don't forget tomorrow what they mean
Matthew Wild <mwild1@gmail.com>
parents: 4946
diff changeset
   663
	/* This is the total size of memory occupied by free (not in use) chunks. */
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   664
	lua_pushinteger(L, info.fordblks);
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   665
	lua_setfield(L, -2, "unused");
4950
02e5e9fa37b8 util.pposix: Add comments to mallinfo fields we use, so I don't forget tomorrow what they mean
Matthew Wild <mwild1@gmail.com>
parents: 4946
diff changeset
   666
	/* This is the size of the top-most releasable chunk that normally borders the
02e5e9fa37b8 util.pposix: Add comments to mallinfo fields we use, so I don't forget tomorrow what they mean
Matthew Wild <mwild1@gmail.com>
parents: 4946
diff changeset
   667
	   end of the heap (i.e., the high end of the virtual address space's data segment). */
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   668
	lua_pushinteger(L, info.keepcost);
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   669
	lua_setfield(L, -2, "returnable");
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   670
	return 1;
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   671
}
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   672
#endif
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   673
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   674
/* File handle extraction blatantly stolen from
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   675
 * https://github.com/rrthomas/luaposix/blob/master/lposix.c#L631
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   676
 * */
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   677
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   678
#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE)
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   679
int lc_fallocate(lua_State* L)
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   680
{
6154
dfe88a0e18fd util.pposix: Fix error reporting from posix_fallocate, it doesn't use errno (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 5927
diff changeset
   681
	int ret;
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   682
	off_t offset, len;
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   683
	FILE *f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE);
5927
49e3c49eb0d8 util.pposix: Verify that file handle is open
Kim Alvefur <zash@zash.se>
parents: 5812
diff changeset
   684
	if (f == NULL)
49e3c49eb0d8 util.pposix: Verify that file handle is open
Kim Alvefur <zash@zash.se>
parents: 5812
diff changeset
   685
		luaL_error(L, "attempt to use a closed file");
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   686
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   687
	offset = luaL_checkinteger(L, 2);
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   688
	len = luaL_checkinteger(L, 3);
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   689
5812
57ebf93ec720 util.pposix: Fix building on non-Linux with glibc
Kim Alvefur <zash@zash.se>
parents: 5717
diff changeset
   690
#if defined(__linux__) && defined(_GNU_SOURCE)
6155
dc3497041aca util.pposix: Fix error reporting from really old Linux fallocate() that did not use errno for some reason (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 6154
diff changeset
   691
	errno = 0;
dc3497041aca util.pposix: Fix error reporting from really old Linux fallocate() that did not use errno for some reason (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 6154
diff changeset
   692
	ret = fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len);
dc3497041aca util.pposix: Fix error reporting from really old Linux fallocate() that did not use errno for some reason (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 6154
diff changeset
   693
	if(ret == 0)
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   694
	{
5067
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   695
		lua_pushboolean(L, 1);
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   696
		return 1;
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   697
	}
6155
dc3497041aca util.pposix: Fix error reporting from really old Linux fallocate() that did not use errno for some reason (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 6154
diff changeset
   698
	/* Some old versions of Linux apparently use the return value instead of errno */
dc3497041aca util.pposix: Fix error reporting from really old Linux fallocate() that did not use errno for some reason (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 6154
diff changeset
   699
	if(errno == 0) errno = ret;
5067
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   700
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   701
	if(errno != ENOSYS && errno != EOPNOTSUPP)
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   702
	{
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   703
		lua_pushnil(L);
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   704
		lua_pushstring(L, strerror(errno));
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   705
		return 2;
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   706
	}
5068
14d4fc5859b9 util.pposix: Put the warning back, mention how we might still use posix_fallocate()
Kim Alvefur <zash@zash.se>
parents: 5067
diff changeset
   707
#else
14d4fc5859b9 util.pposix: Put the warning back, mention how we might still use posix_fallocate()
Kim Alvefur <zash@zash.se>
parents: 5067
diff changeset
   708
#warning Only using posix_fallocate() fallback.
14d4fc5859b9 util.pposix: Put the warning back, mention how we might still use posix_fallocate()
Kim Alvefur <zash@zash.se>
parents: 5067
diff changeset
   709
#warning Linux fallocate() is strongly recommended if available: recompile with -D_GNU_SOURCE
14d4fc5859b9 util.pposix: Put the warning back, mention how we might still use posix_fallocate()
Kim Alvefur <zash@zash.se>
parents: 5067
diff changeset
   710
#warning Note that posix_fallocate() will still be used on filesystems that dont support fallocate()
5067
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   711
#endif
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   712
6154
dfe88a0e18fd util.pposix: Fix error reporting from posix_fallocate, it doesn't use errno (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 5927
diff changeset
   713
	ret = posix_fallocate(fileno(f), offset, len);
dfe88a0e18fd util.pposix: Fix error reporting from posix_fallocate, it doesn't use errno (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 5927
diff changeset
   714
	if(ret == 0)
5067
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   715
	{
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   716
		lua_pushboolean(L, 1);
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   717
		return 1;
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   718
	}
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   719
	else
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   720
	{
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   721
		lua_pushnil(L);
6154
dfe88a0e18fd util.pposix: Fix error reporting from posix_fallocate, it doesn't use errno (thanks pro)
Kim Alvefur <zash@zash.se>
parents: 5927
diff changeset
   722
		lua_pushstring(L, strerror(ret));
5067
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   723
		/* posix_fallocate() can leave a bunch of NULs at the end, so we cut that
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   724
		 * this assumes that offset == length of the file */
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   725
		ftruncate(fileno(f), offset);
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   726
		return 2;
7db1056c63a9 util.pposix: Try posix_fallocate() if fallocate() is unsupported by the file system
Kim Alvefur <zash@zash.se>
parents: 5052
diff changeset
   727
	}
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   728
}
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   729
#endif
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   730
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
   731
/* 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
   732
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   733
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
   734
{
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   735
	luaL_Reg exports[] = {
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   736
		{ "abort", lc_abort },
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   737
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   738
		{ "daemonize", lc_daemonize },
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   739
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   740
		{ "syslog_open", lc_syslog_open },
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   741
		{ "syslog_close", lc_syslog_close },
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   742
		{ "syslog_log", lc_syslog_log },
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   743
		{ "syslog_setminlevel", lc_syslog_setmask },
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   744
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   745
		{ "getpid", lc_getpid },
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   746
		{ "getuid", lc_getuid },
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   747
		{ "getgid", lc_getgid },
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   748
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   749
		{ "setuid", lc_setuid },
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   750
		{ "setgid", lc_setgid },
3471
482275e38224 util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   751
		{ "initgroups", lc_initgroups },
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   752
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   753
		{ "umask", lc_umask },
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
   754
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   755
		{ "mkdir", lc_mkdir },
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   756
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   757
		{ "setrlimit", lc_setrlimit },
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   758
		{ "getrlimit", lc_getrlimit },
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   759
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   760
		{ "uname", lc_uname },
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   761
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   762
		{ "setenv", lc_setenv },
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   763
4946
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   764
#ifdef WITH_MALLINFO
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   765
		{ "meminfo", lc_meminfo },
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   766
#endif
2975c7008ccd util.pposix: Add meminfo() binding to memory allocation stats provided by mallinfo() [compilation tested on Ubuntu...]
Matthew Wild <mwild1@gmail.com>
parents: 4934
diff changeset
   767
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   768
#if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE)
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   769
		{ "fallocate", lc_fallocate },
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   770
#endif
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   771
2441
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   772
		{ NULL, NULL }
d72078946a16 util.pposix: Replace the unwieldy module table generation with luaL_register() call - side effect - it now sets the pposix global to the module table
Matthew Wild <mwild1@gmail.com>
parents: 2438
diff changeset
   773
	};
2438
819ba949c7bc util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents: 2437
diff changeset
   774
6414
6c8f6364bc48 util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents: 6156
diff changeset
   775
	lua_newtable(L);
6c8f6364bc48 util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents: 6156
diff changeset
   776
	luaL_register(L, NULL,  exports);
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   777
727
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   778
	lua_pushliteral(L, "pposix");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   779
	lua_setfield(L, -2, "_NAME");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   780
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   781
	lua_pushliteral(L, MODULE_VERSION);
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   782
	lua_setfield(L, -2, "_VERSION");
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   783
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   784
	return 1;
3966
e71c19dac1c7 util.pposix: Remove extraneous semicolon
Matthew Wild <mwild1@gmail.com>
parents: 3482
diff changeset
   785
}