util-src/pposix.c
author Kim Alvefur <zash@zash.se>
Thu, 12 Jul 2018 20:42:22 +0200
changeset 9031 d4c2a3060e7e
parent 8015 e898c8fda986
child 9153 c1d5f52274cf
permissions -rw-r--r--
util.pposix: Use freopen(3) when replacing standard IO streams (thanks ratchetfreak) Man page says this is what freopen(3) is for.
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
8015
e898c8fda986 util.pposix: Remove fallocate
Kim Alvefur <zash@zash.se>
parents: 8013
diff changeset
    16
#define MODULE_VERSION "0.4.0"
727
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
    17
7934
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    18
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    19
#if defined(__linux__)
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    20
#define _GNU_SOURCE
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    21
#else
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    22
#define _DEFAULT_SOURCE
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    23
#endif
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    24
#define _POSIX_C_SOURCE 200809L
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    25
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
#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
    27
#include <math.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
#include <unistd.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
#include <libgen.h>
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
    30
#include <sys/resource.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
#include <sys/types.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
#include <sys/stat.h>
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
    33
#include <sys/utsname.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
#include <fcntl.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    36
#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
    37
#include <pwd.h>
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
    38
#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
    39
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    40
#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
    41
#include <errno.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
#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
    43
#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
    44
#include "lauxlib.h"
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    45
6792
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6645
diff changeset
    46
#if (LUA_VERSION_NUM == 501)
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6645
diff changeset
    47
#define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
6416
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
    48
#endif
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6414
diff changeset
    49
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    50
#include <fcntl.h>
7934
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
    51
#if defined(__linux__)
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    52
#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
    53
#endif
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
    54
7968
57f98394b830 util.pposix: Limit meminfo to Linux until we can figure out how to get this info on other platforms
Kim Alvefur <zash@zash.se>
parents: 7934
diff changeset
    55
#if !defined(WITHOUT_MALLINFO) && defined(__linux__)
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
    56
#include <malloc.h>
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
    57
#define WITH_MALLINFO
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
    58
#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
    59
7792
93dd90309779 util.pposix: rfork() seems to be FreeBSD only (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 7443
diff changeset
    60
#if defined(__FreeBSD__) && defined(RFPROC)
7443
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    61
/*
7792
93dd90309779 util.pposix: rfork() seems to be FreeBSD only (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 7443
diff changeset
    62
 * On FreeBSD, calling fork() is equivalent to rfork(RFPROC | RFFDG).
7443
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    63
 *
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    64
 * RFFDG being set means that the file descriptor table is copied,
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    65
 * otherwise it's shared. We want the later, otherwise libevent gets
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    66
 * messed up.
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    67
 *
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    68
 * See issue #412
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    69
 */
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    70
#define fork() rfork(RFPROC)
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    71
#endif
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6945
diff changeset
    72
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
    73
/* Daemonization support */
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
    75
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
    76
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
	pid_t pid;
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
    78
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
    79
	if(getppid() == 1) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
		lua_pushstring(L, "already-daemonized");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
    84
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	/* Attempt initial fork */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
    86
	if((pid = fork()) < 0) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
		/* Forking failed */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
		lua_pushstring(L, "fork-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
		return 2;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
    91
	} else if(pid != 0) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
		/* We are the parent process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
		lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
		lua_pushnumber(L, pid);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
    97
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
	/* and we are the child process */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
    99
	if(setsid() == -1) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
		/* We failed to become session leader */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
		/* (we probably already were) */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
		lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
		lua_pushstring(L, "setsid-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
		return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
	}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
5176
7d0a8b255a37 util.pposix: Prevent FDs 0, 1 and 2 from being assigned to connections
Kim Alvefur <zash@zash.se>
parents: 5068
diff changeset
   107
	/* Make sure accidental use of FDs 0, 1, 2 don't cause weirdness */
9031
d4c2a3060e7e util.pposix: Use freopen(3) when replacing standard IO streams (thanks ratchetfreak)
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
   108
	stdin = freopen("/dev/null", "r", stdin);
d4c2a3060e7e util.pposix: Use freopen(3) when replacing standard IO streams (thanks ratchetfreak)
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
   109
	stdout = freopen("/dev/null", "w", stdout);
d4c2a3060e7e util.pposix: Use freopen(3) when replacing standard IO streams (thanks ratchetfreak)
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
   110
	stderr = freopen("/dev/null", "w", stderr);
1195
6b91a2b39680 Close std{in,out,err} when daemonizing
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
   111
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
	/* Final fork, use it wisely */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   113
	if(fork()) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
		exit(0);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   115
	}
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
	/* Show's over, let's continue */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
	lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
	lua_pushnil(L);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
	return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
}
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   123
/* Syslog support */
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   124
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   125
const char *const facility_strings[] = {
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   126
	"auth",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   127
#if !(defined(sun) || defined(__sun))
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   128
	"authpriv",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   129
#endif
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   130
	"cron",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   131
	"daemon",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   132
#if !(defined(sun) || defined(__sun))
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   133
	"ftp",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   134
#endif
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   135
	"kern",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   136
	"local0",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   137
	"local1",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   138
	"local2",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   139
	"local3",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   140
	"local4",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   141
	"local5",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   142
	"local6",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   143
	"local7",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   144
	"lpr",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   145
	"mail",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   146
	"syslog",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   147
	"user",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   148
	"uucp",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   149
	NULL
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   150
};
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   151
int facility_constants[] =	{
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   152
	LOG_AUTH,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   153
#if !(defined(sun) || defined(__sun))
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   154
	LOG_AUTHPRIV,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   155
#endif
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   156
	LOG_CRON,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   157
	LOG_DAEMON,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   158
#if !(defined(sun) || defined(__sun))
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   159
	LOG_FTP,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   160
#endif
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   161
	LOG_KERN,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   162
	LOG_LOCAL0,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   163
	LOG_LOCAL1,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   164
	LOG_LOCAL2,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   165
	LOG_LOCAL3,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   166
	LOG_LOCAL4,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   167
	LOG_LOCAL5,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   168
	LOG_LOCAL6,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   169
	LOG_LOCAL7,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   170
	LOG_LPR,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   171
	LOG_MAIL,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   172
	LOG_NEWS,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   173
	LOG_SYSLOG,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   174
	LOG_USER,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   175
	LOG_UUCP,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   176
	-1
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   177
};
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   178
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   179
/* "
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   180
       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
   181
       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
   182
       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
   183
       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
   184
       constant.
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   185
   " -- syslog manpage
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   186
*/
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   187
char *syslog_ident = NULL;
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   188
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   189
int lc_syslog_open(lua_State *L) {
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   190
	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
   191
	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
   192
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   193
	luaL_checkstring(L, 1);
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   194
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   195
	if(syslog_ident) {
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   196
		free(syslog_ident);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   197
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   198
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   199
	syslog_ident = strdup(lua_tostring(L, 1));
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   200
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   201
	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
   202
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   203
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   204
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   205
const char *const level_strings[] = {
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   206
	"debug",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   207
	"info",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   208
	"notice",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   209
	"warn",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   210
	"error",
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   211
	NULL
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   212
};
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   213
int level_constants[] = 	{
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   214
	LOG_DEBUG,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   215
	LOG_INFO,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   216
	LOG_NOTICE,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   217
	LOG_WARNING,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   218
	LOG_CRIT,
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   219
	-1
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   220
};
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   221
int lc_syslog_log(lua_State *L) {
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
   222
	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
   223
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   224
	if(lua_gettop(L) == 3) {
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
   225
		syslog(level, "%s: %s", luaL_checkstring(L, 2), luaL_checkstring(L, 3));
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   226
	} else {
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
   227
		syslog(level, "%s", lua_tostring(L, 2));
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   228
	}
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   229
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   230
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   231
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   232
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   233
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
   234
	closelog();
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   235
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   236
	if(syslog_ident) {
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   237
		free(syslog_ident);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   238
		syslog_ident = NULL;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   239
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   240
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   241
	return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   242
}
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
   243
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   244
int lc_syslog_setmask(lua_State *L) {
796
63f56696c66c util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents: 766
diff changeset
   245
	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
   246
	int mask = 0;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   247
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   248
	do {
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   249
		mask |= LOG_MASK(level_constants[level_idx]);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   250
	} while(++level_idx <= 4);
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   251
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   252
	setlogmask(mask);
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   253
	return 0;
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   254
}
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
   255
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
   256
/* 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
   257
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   258
int lc_getpid(lua_State *L) {
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
   259
	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
   260
	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
   261
}
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
   262
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   263
/* UID/GID functions */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   264
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   265
int lc_getuid(lua_State *L) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   266
	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
   267
	return 1;
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
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   270
int lc_getgid(lua_State *L) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   271
	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
   272
	return 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
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   275
int lc_setuid(lua_State *L) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   276
	int uid = -1;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   277
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   278
	if(lua_gettop(L) < 1) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   279
		return 0;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   280
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   281
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   282
	if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   283
		/* Passed UID is actually a string, so look up the UID */
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   284
		struct passwd *p;
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   285
		p = getpwnam(lua_tostring(L, 1));
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   286
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   287
		if(!p) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   288
			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
   289
			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
   290
			return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   291
		}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   292
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   293
		uid = p->pw_uid;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   294
	} else {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   295
		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
   296
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   297
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   298
	if(uid > -1) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   299
		/* Ok, attempt setuid */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   300
		errno = 0;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   301
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   302
		if(setuid(uid)) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   303
			/* Fail */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   304
			lua_pushboolean(L, 0);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   305
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   306
			switch(errno) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   307
				case EINVAL:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   308
					lua_pushstring(L, "invalid-uid");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   309
					break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   310
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   311
				case EPERM:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   312
					lua_pushstring(L, "permission-denied");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   313
					break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   314
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   315
				default:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   316
					lua_pushstring(L, "unknown-error");
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   317
			}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
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
			return 2;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   320
		} else {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   321
			/* Success! */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   322
			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
   323
			return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   324
		}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   325
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   326
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   327
	/* 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
   328
	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
   329
	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
   330
	return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   331
}
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   332
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   333
int lc_setgid(lua_State *L) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   334
	int gid = -1;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   335
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   336
	if(lua_gettop(L) < 1) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   337
		return 0;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   338
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   339
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   340
	if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   341
		/* Passed GID is actually a string, so look up the GID */
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   342
		struct group *g;
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   343
		g = getgrnam(lua_tostring(L, 1));
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   344
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   345
		if(!g) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   346
			lua_pushboolean(L, 0);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   347
			lua_pushstring(L, "no-such-group");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   348
			return 2;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   349
		}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   350
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   351
		gid = g->gr_gid;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   352
	} else {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   353
		gid = lua_tonumber(L, 1);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   354
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   355
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   356
	if(gid > -1) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   357
		/* Ok, attempt setgid */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   358
		errno = 0;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   359
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   360
		if(setgid(gid)) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   361
			/* Fail */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   362
			lua_pushboolean(L, 0);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   363
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   364
			switch(errno) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   365
				case EINVAL:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   366
					lua_pushstring(L, "invalid-gid");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   367
					break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   368
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   369
				case EPERM:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   370
					lua_pushstring(L, "permission-denied");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   371
					break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   372
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   373
				default:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   374
					lua_pushstring(L, "unknown-error");
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   375
			}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   376
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   377
			return 2;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   378
		} else {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   379
			/* Success! */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   380
			lua_pushboolean(L, 1);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   381
			return 1;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   382
		}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   383
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   384
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   385
	/* 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
   386
	lua_pushboolean(L, 0);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   387
	lua_pushstring(L, "invalid-gid");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   388
	return 2;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   389
}
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
   390
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   391
int lc_initgroups(lua_State *L) {
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
   392
	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
   393
	gid_t gid;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   394
	struct passwd *p;
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
   395
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   396
	if(!lua_isstring(L, 1)) {
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
   397
		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
   398
		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
   399
		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
   400
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   401
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
   402
	p = getpwnam(lua_tostring(L, 1));
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   403
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   404
	if(!p) {
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
   405
		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
   406
		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
   407
		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
   408
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   409
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   410
	if(lua_gettop(L) < 2) {
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
   411
		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
   412
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   413
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   414
	switch(lua_type(L, 2)) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   415
		case LUA_TNIL:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   416
			gid = p->pw_gid;
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   417
			break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   418
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   419
		case LUA_TNUMBER:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   420
			gid = lua_tointeger(L, 2);
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   421
			break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   422
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   423
		default:
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   424
			lua_pushnil(L);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   425
			lua_pushstring(L, "invalid-gid");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   426
			return 2;
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
   427
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   428
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   429
	ret = initgroups(lua_tostring(L, 1), gid);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   430
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   431
	if(ret) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   432
		switch(errno) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   433
			case ENOMEM:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   434
				lua_pushnil(L);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   435
				lua_pushstring(L, "no-memory");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   436
				break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   437
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   438
			case EPERM:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   439
				lua_pushnil(L);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   440
				lua_pushstring(L, "permission-denied");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   441
				break;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   442
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   443
			default:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   444
				lua_pushnil(L);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   445
				lua_pushstring(L, "unknown-error");
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   446
		}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   447
	} else {
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
   448
		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
   449
		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
   450
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   451
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
   452
	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
   453
}
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
   454
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   455
int lc_umask(lua_State *L) {
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
   456
	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
   457
	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
   458
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
   459
	snprintf(old_mode_string, sizeof(old_mode_string), "%03o", old_mode);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   460
	old_mode_string[sizeof(old_mode_string) - 1] = 0;
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
   461
	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
   462
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
   463
	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
   464
}
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
   465
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   466
int lc_mkdir(lua_State *L) {
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   467
	int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   468
	                | S_IRGRP | S_IWGRP | S_IXGRP
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   469
	                | S_IROTH | S_IXOTH); /* mode 775 */
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   470
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   471
	lua_pushboolean(L, ret == 0);
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   472
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   473
	if(ret) {
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   474
		lua_pushstring(L, strerror(errno));
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   475
		return 2;
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   476
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   477
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   478
	return 1;
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   479
}
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   480
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   481
/*	Like POSIX's setrlimit()/getrlimit() API functions.
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   482
 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   483
 *	Syntax:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   484
 *	pposix.setrlimit( resource, soft limit, hard limit)
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   485
 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   486
 *	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
   487
 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   488
 *	Example usage:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   489
 *	pposix.setrlimit("NOFILE", 1000, 2000)
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   490
 */
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   491
int string2resource(const char *s) {
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   492
	if(!strcmp(s, "CORE")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   493
		return RLIMIT_CORE;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   494
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   495
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   496
	if(!strcmp(s, "CPU")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   497
		return RLIMIT_CPU;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   498
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   499
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   500
	if(!strcmp(s, "DATA")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   501
		return RLIMIT_DATA;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   502
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   503
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   504
	if(!strcmp(s, "FSIZE")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   505
		return RLIMIT_FSIZE;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   506
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   507
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   508
	if(!strcmp(s, "NOFILE")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   509
		return RLIMIT_NOFILE;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   510
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   511
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   512
	if(!strcmp(s, "STACK")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   513
		return RLIMIT_STACK;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   514
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   515
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   516
#if !(defined(sun) || defined(__sun))
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   517
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   518
	if(!strcmp(s, "MEMLOCK")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   519
		return RLIMIT_MEMLOCK;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   520
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   521
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   522
	if(!strcmp(s, "NPROC")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   523
		return RLIMIT_NPROC;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   524
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   525
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   526
	if(!strcmp(s, "RSS")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   527
		return RLIMIT_RSS;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   528
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   529
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
   530
#endif
5359
43517e649c53 util.pposix: Allow fetching RLIMIT_NICE when available
Matthew Wild <mwild1@gmail.com>
parents: 5176
diff changeset
   531
#ifdef RLIMIT_NICE
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   532
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   533
	if(!strcmp(s, "NICE")) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   534
		return RLIMIT_NICE;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   535
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   536
5359
43517e649c53 util.pposix: Allow fetching RLIMIT_NICE when available
Matthew Wild <mwild1@gmail.com>
parents: 5176
diff changeset
   537
#endif
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   538
	return -1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   539
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   540
7969
30309fd01d76 util.pposix: Use correct type for limits [-Wconstant-conversion]
Kim Alvefur <zash@zash.se>
parents: 7968
diff changeset
   541
rlim_t 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
   542
	switch(lua_type(L, idx)) {
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   543
		case LUA_TSTRING:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   544
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   545
			if(strcmp(lua_tostring(L, idx), "unlimited") == 0) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   546
				return RLIM_INFINITY;
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   547
			}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   548
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   549
		case LUA_TNUMBER:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   550
			return lua_tointeger(L, idx);
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   551
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   552
		case LUA_TNONE:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   553
		case LUA_TNIL:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   554
			return current;
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   555
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   556
		default:
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   557
			return luaL_argerror(L, idx, "unexpected type");
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
   558
	}
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
   559
}
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
   560
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   561
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
   562
	struct rlimit lim;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   563
	int arguments = lua_gettop(L);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   564
	int rid = -1;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   565
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   566
	if(arguments < 1 || arguments > 3) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
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, "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
   569
		return 2;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   570
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   571
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
   572
	rid = string2resource(luaL_checkstring(L, 1));
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   573
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   574
	if(rid == -1) {
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   575
		lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   576
		lua_pushstring(L, "invalid-resource");
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   577
		return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   578
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   579
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
   580
	/* Fetch current values to use as defaults */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   581
	if(getrlimit(rid, &lim)) {
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
   582
		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
   583
		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
   584
		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
   585
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   586
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
   587
	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
   588
	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
   589
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   590
	if(setrlimit(rid, &lim)) {
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   591
		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
   592
		lua_pushstring(L, "setrlimit-failed");
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   593
		return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   594
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   595
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   596
	lua_pushboolean(L, 1);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   597
	return 1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   598
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   599
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   600
int lc_getrlimit(lua_State *L) {
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   601
	int arguments = lua_gettop(L);
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   602
	const char *resource = NULL;
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   603
	int rid = -1;
861
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
   604
	struct rlimit lim;
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   605
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   606
	if(arguments != 1) {
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   607
		lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   608
		lua_pushstring(L, "invalid-arguments");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   609
		return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   610
	}
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   611
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   612
	resource = luaL_checkstring(L, 1);
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   613
	rid = string2resource(resource);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   614
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   615
	if(rid != -1) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   616
		if(getrlimit(rid, &lim)) {
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   617
			lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   618
			lua_pushstring(L, "getrlimit-failed.");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   619
			return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   620
		}
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   621
	} else {
7922
251bf68922d9 util.pposix: Fix typo in comment
Kim Alvefur <zash@zash.se>
parents: 7892
diff changeset
   622
		/* Unsupported resource. Sorry I'm pretty limited by POSIX standard. */
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   623
		lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
   624
		lua_pushstring(L, "invalid-resource");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   625
		return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   626
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   627
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   628
	lua_pushboolean(L, 1);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   629
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   630
	if(lim.rlim_cur == RLIM_INFINITY) {
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
   631
		lua_pushstring(L, "unlimited");
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   632
	} else {
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
   633
		lua_pushnumber(L, lim.rlim_cur);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   634
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   635
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   636
	if(lim.rlim_max == RLIM_INFINITY) {
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
   637
		lua_pushstring(L, "unlimited");
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   638
	} else {
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
   639
		lua_pushnumber(L, lim.rlim_max);
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   640
	}
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   641
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
   642
	return 3;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   643
}
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
   644
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   645
int lc_abort(lua_State *L) {
2060
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
   646
	abort();
2830
1df57426263a util.pposix: Fix return type of lc_abort to shush compiler warning
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   647
	return 0;
2060
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
   648
}
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
   649
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   650
int lc_uname(lua_State *L) {
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   651
	struct utsname uname_info;
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   652
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   653
	if(uname(&uname_info) != 0) {
3482
e1a4f7b15caf util.pposix: uname(): Fix to push nil,err in case of error
Matthew Wild <mwild1@gmail.com>
parents: 3481
diff changeset
   654
		lua_pushnil(L);
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   655
		lua_pushstring(L, strerror(errno));
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   656
		return 2;
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   657
	}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   658
7972
1c6a07606309 util-src: Specify size of various tables to be allocated
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
   659
	lua_createtable(L, 0, 6);
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   660
	lua_pushstring(L, uname_info.sysname);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   661
	lua_setfield(L, -2, "sysname");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   662
	lua_pushstring(L, uname_info.nodename);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   663
	lua_setfield(L, -2, "nodename");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   664
	lua_pushstring(L, uname_info.release);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   665
	lua_setfield(L, -2, "release");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   666
	lua_pushstring(L, uname_info.version);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   667
	lua_setfield(L, -2, "version");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   668
	lua_pushstring(L, uname_info.machine);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   669
	lua_setfield(L, -2, "machine");
7934
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7929
diff changeset
   670
#ifdef __USE_GNU
6945
f12deb882148 util.pposix: Expose the uname GNU extension field 'domainname'
Kim Alvefur <zash@zash.se>
parents: 6792
diff changeset
   671
	lua_pushstring(L, uname_info.domainname);
f12deb882148 util.pposix: Expose the uname GNU extension field 'domainname'
Kim Alvefur <zash@zash.se>
parents: 6792
diff changeset
   672
	lua_setfield(L, -2, "domainname");
f12deb882148 util.pposix: Expose the uname GNU extension field 'domainname'
Kim Alvefur <zash@zash.se>
parents: 6792
diff changeset
   673
#endif
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   674
	return 1;
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   675
}
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   676
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   677
int lc_setenv(lua_State *L) {
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   678
	const char *var = luaL_checkstring(L, 1);
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   679
	const char *value;
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   680
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   681
	/* If the second argument is nil or nothing, unset the var */
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   682
	if(lua_isnoneornil(L, 2)) {
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   683
		if(unsetenv(var) != 0) {
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   684
			lua_pushnil(L);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   685
			lua_pushstring(L, strerror(errno));
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   686
			return 2;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   687
		}
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   688
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   689
		lua_pushboolean(L, 1);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   690
		return 1;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   691
	}
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   692
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   693
	value = luaL_checkstring(L, 2);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   694
6618
8e4572a642cb util-src/*.c: astyle --indent=tab --brackets=attach --indent-switches --break-blocks --pad-oper --unpad-paren --add-brackets --align-pointer=type --lineend=linux
Kim Alvefur <zash@zash.se>
parents: 6416
diff changeset
   695
	if(setenv(var, value, 1) != 0) {
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   696
		lua_pushnil(L);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   697
		lua_pushstring(L, strerror(errno));
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   698
		return 2;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   699
	}
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   700
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   701
	lua_pushboolean(L, 1);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   702
	return 1;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   703
}
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   704
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
   705
#ifdef WITH_MALLINFO
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   706
int lc_meminfo(lua_State *L) {
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
   707
	struct mallinfo info = mallinfo();
7972
1c6a07606309 util-src: Specify size of various tables to be allocated
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
   708
	lua_createtable(L, 0, 5);
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
   709
	/* 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
   710
	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
   711
	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
   712
	/* 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
   713
	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
   714
	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
   715
	/* 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
   716
	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
   717
	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
   718
	/* 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
   719
	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
   720
	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
   721
	/* 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
   722
	   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
   723
	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
   724
	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
   725
	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
   726
}
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
   727
#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
   728
8013
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   729
/*
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   730
 * Append some data to a file handle
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   731
 * Attempt to allocate space first
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   732
 * Truncate to original size on failure
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   733
 */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   734
int lc_atomic_append(lua_State *L) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   735
	int err;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   736
	size_t len;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   737
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   738
	FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   739
	const char *data = luaL_checklstring(L, 2, &len);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   740
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   741
	off_t offset = ftell(f);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   742
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   743
#if defined(__linux__)
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   744
	/* Try to allocate space without changing the file size. */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   745
	if((err = fallocate(fileno(f), FALLOC_FL_KEEP_SIZE, offset, len))) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   746
		if(errno != 0) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   747
			/* Some old versions of Linux apparently use the return value instead of errno */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   748
			err = errno;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   749
		}
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   750
		switch(err) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   751
			case ENOSYS: /* Kernel doesn't implement fallocate */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   752
			case EOPNOTSUPP: /* Filesystem doesn't support it */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   753
				/* Ignore and proceed to try to write */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   754
				break;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   755
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   756
			case ENOSPC: /* No space left */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   757
			default: /* Other issues */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   758
				lua_pushnil(L);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   759
				lua_pushstring(L, strerror(err));
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   760
				lua_pushinteger(L, err);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   761
				return 3;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   762
		}
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   763
	}
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   764
#endif
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   765
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   766
	if(fwrite(data, sizeof(char), len, f) == len) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   767
		if(fflush(f) == 0) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   768
			lua_pushboolean(L, 1); /* Great success! */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   769
			return 1;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   770
		} else {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   771
			err = errno;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   772
		}
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   773
	} else {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   774
		err = ferror(f);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   775
	}
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   776
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   777
	fseek(f, offset, SEEK_SET);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   778
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   779
	/* Cut partially written data */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   780
	if(ftruncate(fileno(f), offset)) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   781
		/* The file is now most likely corrupted, throw hard error */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   782
		return luaL_error(L, "atomic_append() failed in ftruncate(): %s", strerror(errno));
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   783
	}
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   784
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   785
	lua_pushnil(L);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   786
	lua_pushstring(L, strerror(err));
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   787
	lua_pushinteger(L, err);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   788
	return 3;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   789
}
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   790
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
   791
/* 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
   792
7892
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7838
diff changeset
   793
int luaopen_util_pposix(lua_State *L) {
7821
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 7792
diff changeset
   794
#if (LUA_VERSION_NUM > 501)
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 7792
diff changeset
   795
	luaL_checkversion(L);
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 7792
diff changeset
   796
#endif
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
   797
	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
   798
		{ "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
   799
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
   800
		{ "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
   801
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
   802
		{ "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
   803
		{ "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
   804
		{ "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
   805
		{ "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
   806
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
   807
		{ "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
   808
		{ "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
   809
		{ "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
   810
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
   811
		{ "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
   812
		{ "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
   813
		{ "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
   814
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
   815
		{ "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
   816
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
   817
		{ "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
   818
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
   819
		{ "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
   820
		{ "getrlimit", lc_getrlimit },
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   821
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   822
		{ "uname", lc_uname },
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
   823
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   824
		{ "setenv", lc_setenv },
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
   825
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
   826
#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
   827
		{ "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
   828
#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
   829
8013
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7972
diff changeset
   830
		{ "atomic_append", lc_atomic_append },
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
   831
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
   832
		{ 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
   833
	};
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
   834
6414
6c8f6364bc48 util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents: 6156
diff changeset
   835
	lua_newtable(L);
6792
6b180e77c97a util-src/*.c: Invert Lua 5.2 compat to be 5.2+ by default and a macro to support 5.1
Kim Alvefur <zash@zash.se>
parents: 6645
diff changeset
   836
	luaL_setfuncs(L, exports, 0);
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
   837
7929
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7922
diff changeset
   838
#ifdef ENOENT
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7922
diff changeset
   839
	lua_pushinteger(L, ENOENT);
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7922
diff changeset
   840
	lua_setfield(L, -2, "ENOENT");
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7922
diff changeset
   841
#endif
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7922
diff changeset
   842
727
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   843
	lua_pushliteral(L, "pposix");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   844
	lua_setfield(L, -2, "_NAME");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   845
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   846
	lua_pushliteral(L, MODULE_VERSION);
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
   847
	lua_setfield(L, -2, "_VERSION");
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
   848
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   849
	return 1;
3966
e71c19dac1c7 util.pposix: Remove extraneous semicolon
Matthew Wild <mwild1@gmail.com>
parents: 3482
diff changeset
   850
}