annotate util-src/pposix.c @ 14180:573ac422ca1d 13.0

util.pposix: Fix incorrect syslog facility mapping The array of Lua constants accidentally omitted "news", leading to later constants ("syslog", "user" and "uucp") being assigned to the wrong POSIX constants.
author Matthew Wild <mwild1@gmail.com>
date Mon, 25 May 2026 15:23:48 +0100
parents 904932fc525f
children d975d280baec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
13563
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
16 #define MODULE_VERSION "0.4.1"
727
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
17
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
18
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
19 #if defined(__linux__)
9150
c1d5f52274cf net, pposix, signal: Check for redefined defines, fix warnings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9028
diff changeset
20 #ifndef _GNU_SOURCE
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
21 #define _GNU_SOURCE
9150
c1d5f52274cf net, pposix, signal: Check for redefined defines, fix warnings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9028
diff changeset
22 #endif
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
23 #else
9150
c1d5f52274cf net, pposix, signal: Check for redefined defines, fix warnings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9028
diff changeset
24 #ifndef _DEFAULT_SOURCE
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
25 #define _DEFAULT_SOURCE
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
26 #endif
9150
c1d5f52274cf net, pposix, signal: Check for redefined defines, fix warnings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9028
diff changeset
27 #endif
9681
861d510e1876 util.pposix: Don't define POSIX_C_SOURCE on FreeBSD to ensure visibility of initgroups()
Matthew Wild <mwild1@gmail.com>
parents: 9282
diff changeset
28
9280
37b796cc366a util.pposix: Fix or silence a warning on OS X (#1202)
Kim Alvefur <zash@zash.se>
parents: 9278
diff changeset
29 #if defined(__APPLE__)
9282
778d2dbfc784 util.pposix: Allow _DARWIN_C_SOURCE to be overriden
Kim Alvefur <zash@zash.se>
parents: 9281
diff changeset
30 #ifndef _DARWIN_C_SOURCE
9280
37b796cc366a util.pposix: Fix or silence a warning on OS X (#1202)
Kim Alvefur <zash@zash.se>
parents: 9278
diff changeset
31 #define _DARWIN_C_SOURCE
37b796cc366a util.pposix: Fix or silence a warning on OS X (#1202)
Kim Alvefur <zash@zash.se>
parents: 9278
diff changeset
32 #endif
9282
778d2dbfc784 util.pposix: Allow _DARWIN_C_SOURCE to be overriden
Kim Alvefur <zash@zash.se>
parents: 9281
diff changeset
33 #endif
9681
861d510e1876 util.pposix: Don't define POSIX_C_SOURCE on FreeBSD to ensure visibility of initgroups()
Matthew Wild <mwild1@gmail.com>
parents: 9282
diff changeset
34
861d510e1876 util.pposix: Don't define POSIX_C_SOURCE on FreeBSD to ensure visibility of initgroups()
Matthew Wild <mwild1@gmail.com>
parents: 9282
diff changeset
35 #if ! defined(__FreeBSD__)
9150
c1d5f52274cf net, pposix, signal: Check for redefined defines, fix warnings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9028
diff changeset
36 #ifndef _POSIX_C_SOURCE
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
37 #define _POSIX_C_SOURCE 200809L
9150
c1d5f52274cf net, pposix, signal: Check for redefined defines, fix warnings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9028
diff changeset
38 #endif
9681
861d510e1876 util.pposix: Don't define POSIX_C_SOURCE on FreeBSD to ensure visibility of initgroups()
Matthew Wild <mwild1@gmail.com>
parents: 9282
diff changeset
39 #endif
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
40
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 #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
42 #include <math.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 #include <unistd.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 #include <libgen.h>
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
45 #include <sys/resource.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 #include <sys/types.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 #include <sys/stat.h>
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
48 #include <sys/utsname.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 #include <fcntl.h>
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
51 #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
52 #include <pwd.h>
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
53 #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
54
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
55 #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
56 #include <errno.h>
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 #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
58 #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
59 #include "lauxlib.h"
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
60
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
61 #if (LUA_VERSION_NUM < 503)
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
62 #define lua_isinteger(L, n) lua_isnumber(L, n)
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
63 #endif
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
64 #if (LUA_VERSION_NUM < 504)
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
65 #define luaL_pushfail lua_pushnil
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
66 #endif
6413
a552f4170aed util-src/*.c: Add macro for compiling with Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 6411
diff changeset
67
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
68 #include <fcntl.h>
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
69 #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
70 #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
71 #endif
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
72
9277
fa6a6f921405 util.pposix: Restrict use of malloc_info to glibc
Kim Alvefur <zash@zash.se>
parents: 9276
diff changeset
73 #if !defined(WITHOUT_MALLINFO) && defined(__linux__) && defined(__GLIBC__)
6615
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: 6413
diff changeset
74 #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: 6413
diff changeset
75 #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
76 #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
77
7789
93dd90309779 util.pposix: rfork() seems to be FreeBSD only (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 7440
diff changeset
78 #if defined(__FreeBSD__) && defined(RFPROC)
7440
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
79 /*
7789
93dd90309779 util.pposix: rfork() seems to be FreeBSD only (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 7440
diff changeset
80 * On FreeBSD, calling fork() is equivalent to rfork(RFPROC | RFFDG).
7440
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
81 *
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
82 * 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: 6942
diff changeset
83 * 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: 6942
diff changeset
84 * messed up.
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
85 *
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
86 * See issue #412
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
87 */
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
88 #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: 6942
diff changeset
89 #endif
5424e24cdcb1 util.pposix: Ask for shared file descriptor table using rfork() on *BSD (fixes #412)
Kim Alvefur <zash@zash.se>
parents: 6942
diff changeset
90
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
91 /* Daemonization support */
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
93 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
94
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 pid_t pid;
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
96
6615
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: 6413
diff changeset
97 if(getppid() == 1) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 lua_pushstring(L, "already-daemonized");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
102
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 /* Attempt initial fork */
6615
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: 6413
diff changeset
104 if((pid = fork()) < 0) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 /* Forking failed */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 lua_pushstring(L, "fork-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 return 2;
6615
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: 6413
diff changeset
109 } else if(pid != 0) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 /* We are the parent process */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 lua_pushboolean(L, 1);
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
112 lua_pushinteger(L, pid);
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
115
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 /* and we are the child process */
6615
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: 6413
diff changeset
117 if(setsid() == -1) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 /* We failed to become session leader */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 /* (we probably already were) */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 lua_pushboolean(L, 0);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 lua_pushstring(L, "setsid-failed");
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 }
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
5176
7d0a8b255a37 util.pposix: Prevent FDs 0, 1 and 2 from being assigned to connections
Kim Alvefur <zash@zash.se>
parents: 5068
diff changeset
125 /* Make sure accidental use of FDs 0, 1, 2 don't cause weirdness */
14054
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
126 if(!freopen("/dev/null", "r", stdin)) {
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
127 fprintf(stderr, "Failed to redirect stdin to /dev/null");
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
128 }
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
129
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
130 if(!freopen("/dev/null", "w", stdout)) {
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
131 fprintf(stderr, "Failed to redirect stdout to /dev/null");
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
132 }
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
133
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
134 if(!freopen("/dev/null", "w", stderr)) {
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
135 fprintf(stderr, "Failed to redirect stderr to /dev/null");
904932fc525f util.pposix: Silence compiler warnings about unused return values
Kim Alvefur <zash@zash.se>
parents: 13574
diff changeset
136 }
1195
6b91a2b39680 Close std{in,out,err} when daemonizing
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
137
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 /* Final fork, use it wisely */
6615
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: 6413
diff changeset
139 if(fork()) {
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 exit(0);
6615
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: 6413
diff changeset
141 }
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 /* Show's over, let's continue */
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 lua_pushboolean(L, 1);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 lua_pushnil(L);
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 return 2;
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 }
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
149 /* Syslog support */
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
150
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
151 static const char *const facility_strings[] = {
6615
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: 6413
diff changeset
152 "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))
6615
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: 6413
diff changeset
154 "authpriv",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
155 #endif
6615
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: 6413
diff changeset
156 "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: 6413
diff changeset
157 "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))
6615
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: 6413
diff changeset
159 "ftp",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
160 #endif
6615
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: 6413
diff changeset
161 "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: 6413
diff changeset
162 "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: 6413
diff changeset
163 "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: 6413
diff changeset
164 "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: 6413
diff changeset
165 "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: 6413
diff changeset
166 "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: 6413
diff changeset
167 "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: 6413
diff changeset
168 "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: 6413
diff changeset
169 "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: 6413
diff changeset
170 "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: 6413
diff changeset
171 "mail",
14180
573ac422ca1d util.pposix: Fix incorrect syslog facility mapping
Matthew Wild <mwild1@gmail.com>
parents: 14054
diff changeset
172 "news",
6615
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: 6413
diff changeset
173 "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: 6413
diff changeset
174 "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: 6413
diff changeset
175 "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: 6413
diff changeset
176 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: 6413
diff changeset
177 };
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
178 static int facility_constants[] = {
6615
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: 6413
diff changeset
179 LOG_AUTH,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
180 #if !(defined(sun) || defined(__sun))
6615
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: 6413
diff changeset
181 LOG_AUTHPRIV,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
182 #endif
6615
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: 6413
diff changeset
183 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: 6413
diff changeset
184 LOG_DAEMON,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
185 #if !(defined(sun) || defined(__sun))
6615
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: 6413
diff changeset
186 LOG_FTP,
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
187 #endif
6615
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: 6413
diff changeset
188 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: 6413
diff changeset
189 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: 6413
diff changeset
190 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: 6413
diff changeset
191 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: 6413
diff changeset
192 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: 6413
diff changeset
193 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: 6413
diff changeset
194 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: 6413
diff changeset
195 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: 6413
diff changeset
196 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: 6413
diff changeset
197 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: 6413
diff changeset
198 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: 6413
diff changeset
199 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: 6413
diff changeset
200 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: 6413
diff changeset
201 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: 6413
diff changeset
202 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: 6413
diff changeset
203 -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: 6413
diff changeset
204 };
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
205
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
206 /* "
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
207 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
208 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
209 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
210 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
211 constant.
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
212 " -- syslog manpage
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
213 */
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
214 static 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
215
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
216 static 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
217 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
218 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
219
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
220 luaL_checkstring(L, 1);
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
221
6615
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: 6413
diff changeset
222 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
223 free(syslog_ident);
6615
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: 6413
diff changeset
224 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
225
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
226 syslog_ident = strdup(lua_tostring(L, 1));
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
227
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
228 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
229 return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
230 }
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
231
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
232 static const char *const level_strings[] = {
6615
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: 6413
diff changeset
233 "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: 6413
diff changeset
234 "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: 6413
diff changeset
235 "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: 6413
diff changeset
236 "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: 6413
diff changeset
237 "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: 6413
diff changeset
238 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: 6413
diff changeset
239 };
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
240 static int level_constants[] = {
6615
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: 6413
diff changeset
241 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: 6413
diff changeset
242 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: 6413
diff changeset
243 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: 6413
diff changeset
244 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: 6413
diff changeset
245 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: 6413
diff changeset
246 -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: 6413
diff changeset
247 };
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
248 static 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
249 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
250
6615
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: 6413
diff changeset
251 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
252 syslog(level, "%s: %s", luaL_checkstring(L, 2), luaL_checkstring(L, 3));
6615
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: 6413
diff changeset
253 } 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
254 syslog(level, "%s", lua_tostring(L, 2));
6615
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: 6413
diff changeset
255 }
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
256
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
257 return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
258 }
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
259
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
260 static int lc_syslog_close(lua_State *L) {
9151
fee3933ff346 pposix, signal: Ignore unused arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9150
diff changeset
261 (void)L;
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
262 closelog();
6615
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: 6413
diff changeset
263
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: 6413
diff changeset
264 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
265 free(syslog_ident);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
266 syslog_ident = NULL;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
267 }
6615
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: 6413
diff changeset
268
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
269 return 0;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
270 }
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 588
diff changeset
271
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
272 static 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
273 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
274 int mask = 0;
6615
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: 6413
diff changeset
275
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: 6413
diff changeset
276 do {
729
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
277 mask |= LOG_MASK(level_constants[level_idx]);
6615
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: 6413
diff changeset
278 } 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
279
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
280 setlogmask(mask);
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
281 return 0;
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
282 }
f62ef65d5c01 pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents: 727
diff changeset
283
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
284 /* 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
285
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
286 static 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
287 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
288 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
289 }
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
290
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
291 /* UID/GID functions */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
292
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
293 static 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
294 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
295 return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
296 }
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
297
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
298 static 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
299 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
300 return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
301 }
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
302
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
303 static 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
304 int uid = -1;
6615
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: 6413
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: 6413
diff changeset
306 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
307 return 0;
6615
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: 6413
diff changeset
308 }
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: 6413
diff changeset
309
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
310 if(!lua_isinteger(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
311 /* Passed UID is actually a string, so look up the UID */
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
312 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
313 p = getpwnam(lua_tostring(L, 1));
6615
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: 6413
diff changeset
314
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: 6413
diff changeset
315 if(!p) {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
316 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
317 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
318 return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
319 }
6615
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: 6413
diff changeset
320
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
321 uid = p->pw_uid;
6615
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: 6413
diff changeset
322 } else {
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
323 uid = lua_tointeger(L, 1);
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
324 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
325
6615
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: 6413
diff changeset
326 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
327 /* Ok, attempt setuid */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
328 errno = 0;
6615
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: 6413
diff changeset
329
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: 6413
diff changeset
330 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
331 /* Fail */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
332 lua_pushboolean(L, 0);
6615
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: 6413
diff changeset
333
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: 6413
diff changeset
334 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: 6413
diff changeset
335 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: 6413
diff changeset
336 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: 6413
diff changeset
337 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
338
6615
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: 6413
diff changeset
339 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: 6413
diff changeset
340 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: 6413
diff changeset
341 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
342
6615
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: 6413
diff changeset
343 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: 6413
diff changeset
344 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
345 }
6615
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: 6413
diff changeset
346
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
347 return 2;
6615
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: 6413
diff changeset
348 } else {
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
349 /* Success! */
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
350 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
351 return 1;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
352 }
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
353 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
354
804
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
355 /* 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
356 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
357 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
358 return 2;
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
359 }
9bc1544c99b7 util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents: 796
diff changeset
360
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
361 static int lc_setgid(lua_State *L) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
362 int gid = -1;
6615
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: 6413
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: 6413
diff changeset
364 if(lua_gettop(L) < 1) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
365 return 0;
6615
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: 6413
diff changeset
366 }
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: 6413
diff changeset
367
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
368 if(!lua_isinteger(L, 1) && lua_tostring(L, 1)) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
369 /* Passed GID is actually a string, so look up the GID */
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
370 struct group *g;
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
371 g = getgrnam(lua_tostring(L, 1));
6615
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: 6413
diff changeset
372
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: 6413
diff changeset
373 if(!g) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
374 lua_pushboolean(L, 0);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
375 lua_pushstring(L, "no-such-group");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
376 return 2;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
377 }
6615
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: 6413
diff changeset
378
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
379 gid = g->gr_gid;
6615
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: 6413
diff changeset
380 } else {
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
381 gid = lua_tointeger(L, 1);
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
382 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
383
6615
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: 6413
diff changeset
384 if(gid > -1) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
385 /* Ok, attempt setgid */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
386 errno = 0;
6615
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: 6413
diff changeset
387
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: 6413
diff changeset
388 if(setgid(gid)) {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
389 /* Fail */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
390 lua_pushboolean(L, 0);
6615
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: 6413
diff changeset
391
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: 6413
diff changeset
392 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: 6413
diff changeset
393 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: 6413
diff changeset
394 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: 6413
diff changeset
395 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
396
6615
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: 6413
diff changeset
397 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: 6413
diff changeset
398 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: 6413
diff changeset
399 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
400
6615
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: 6413
diff changeset
401 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: 6413
diff changeset
402 lua_pushstring(L, "unknown-error");
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
403 }
6615
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: 6413
diff changeset
404
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
405 return 2;
6615
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: 6413
diff changeset
406 } else {
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
407 /* Success! */
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
408 lua_pushboolean(L, 1);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
409 return 1;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
410 }
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
411 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
412
1578
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
413 /* 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
414 lua_pushboolean(L, 0);
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
415 lua_pushstring(L, "invalid-gid");
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
416 return 2;
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
417 }
5bd8b3bdbfbc pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents: 1565
diff changeset
418
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
419 static 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
420 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
421 gid_t gid;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
422 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
423
6615
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: 6413
diff changeset
424 if(!lua_isstring(L, 1)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
425 luaL_pushfail(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
426 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
427 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
428 }
6615
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: 6413
diff changeset
429
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
430 p = getpwnam(lua_tostring(L, 1));
6615
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: 6413
diff changeset
431
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: 6413
diff changeset
432 if(!p) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
433 luaL_pushfail(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
434 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
435 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
436 }
6615
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: 6413
diff changeset
437
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: 6413
diff changeset
438 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
439 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
440 }
6615
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: 6413
diff changeset
441
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: 6413
diff changeset
442 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: 6413
diff changeset
443 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: 6413
diff changeset
444 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
445 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
446
6615
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: 6413
diff changeset
447 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: 6413
diff changeset
448 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
449 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
450
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
451 default:
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
452 luaL_pushfail(L);
6615
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: 6413
diff changeset
453 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: 6413
diff changeset
454 return 2;
4415
0091db139229 util.pposix: Don't trust errno for success. Thanks Quince
Kim Alvefur <zash@zash.se>
parents: 3966
diff changeset
455 }
6615
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: 6413
diff changeset
456
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: 6413
diff changeset
457 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: 6413
diff changeset
458
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: 6413
diff changeset
459 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: 6413
diff changeset
460 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: 6413
diff changeset
461 case ENOMEM:
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
462 luaL_pushfail(L);
6615
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: 6413
diff changeset
463 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: 6413
diff changeset
464 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
465
6615
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: 6413
diff changeset
466 case EPERM:
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
467 luaL_pushfail(L);
6615
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: 6413
diff changeset
468 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: 6413
diff changeset
469 break;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
470
6615
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: 6413
diff changeset
471 default:
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
472 luaL_pushfail(L);
6615
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: 6413
diff changeset
473 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: 6413
diff changeset
474 }
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: 6413
diff changeset
475 } 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
476 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
477 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
478 }
6615
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: 6413
diff changeset
479
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
480 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
481 }
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
482
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
483 static 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
484 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
485 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
486
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
487 snprintf(old_mode_string, sizeof(old_mode_string), "%03o", old_mode);
6615
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: 6413
diff changeset
488 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
489 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
490
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
491 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
492 }
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
493
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
494 static int lc_mkdir(lua_State *L) {
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
495 int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR
6615
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: 6413
diff changeset
496 | 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: 6413
diff changeset
497 | S_IROTH | S_IXOTH); /* mode 775 */
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
498
6615
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: 6413
diff changeset
499 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: 6413
diff changeset
500
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: 6413
diff changeset
501 if(ret) {
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
502 lua_pushstring(L, strerror(errno));
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
503 return 2;
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
504 }
6615
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: 6413
diff changeset
505
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
506 return 1;
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
507 }
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
508
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
509 /* Like POSIX's setrlimit()/getrlimit() API functions.
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
510 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
511 * Syntax:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
512 * pposix.setrlimit( resource, soft limit, hard limit)
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
513 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
514 * 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
515 *
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
516 * Example usage:
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
517 * pposix.setrlimit("NOFILE", 1000, 2000)
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
518 */
6615
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: 6413
diff changeset
519
13152
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
520 static const char *const resource_strings[] = {
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
521 /* Defined by POSIX */
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
522 "CORE",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
523 "CPU",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
524 "DATA",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
525 "FSIZE",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
526 "NOFILE",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
527 "STACK",
9278
8f9e18d4fe50 util.pposix: Fix building on OS X (#1202)
Kim Alvefur <zash@zash.se>
parents: 8012
diff changeset
528 #if !(defined(sun) || defined(__sun) || defined(__APPLE__))
13152
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
529 "MEMLOCK",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
530 "NPROC",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
531 "RSS",
1842
8337c0d4aee4 util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 1579
diff changeset
532 #endif
5359
43517e649c53 util.pposix: Allow fetching RLIMIT_NICE when available
Matthew Wild <mwild1@gmail.com>
parents: 5176
diff changeset
533 #ifdef RLIMIT_NICE
13152
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
534 "NICE",
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
535 #endif
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
536 NULL
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
537 };
6615
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: 6413
diff changeset
538
13152
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
539 static int resource_constants[] = {
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
540 RLIMIT_CORE,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
541 RLIMIT_CPU,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
542 RLIMIT_DATA,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
543 RLIMIT_FSIZE,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
544 RLIMIT_NOFILE,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
545 RLIMIT_STACK,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
546 #if !(defined(sun) || defined(__sun) || defined(__APPLE__))
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
547 RLIMIT_MEMLOCK,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
548 RLIMIT_NPROC,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
549 RLIMIT_RSS,
5359
43517e649c53 util.pposix: Allow fetching RLIMIT_NICE when available
Matthew Wild <mwild1@gmail.com>
parents: 5176
diff changeset
550 #endif
13152
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
551 #ifdef RLIMIT_NICE
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
552 RLIMIT_NICE,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
553 #endif
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
554 -1,
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
555 };
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
556
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
557 static 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
558 switch(lua_type(L, idx)) {
6615
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: 6413
diff changeset
559 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: 6413
diff changeset
560
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: 6413
diff changeset
561 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: 6413
diff changeset
562 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: 6413
diff changeset
563 }
9152
bad9e8b43f50 pposix: Generate an error when a passed string isn’t "unlimited".
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9151
diff changeset
564 return luaL_argerror(L, idx, "unexpected type");
6615
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: 6413
diff changeset
565
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: 6413
diff changeset
566 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: 6413
diff changeset
567 return lua_tointeger(L, idx);
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
568
6615
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: 6413
diff changeset
569 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: 6413
diff changeset
570 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: 6413
diff changeset
571 return current;
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
572
6615
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: 6413
diff changeset
573 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: 6413
diff changeset
574 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
575 }
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
576 }
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
577
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
578 static 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
579 struct rlimit lim;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
580 int arguments = lua_gettop(L);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
581 int rid = -1;
6615
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: 6413
diff changeset
582
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
583 if(arguments < 1 || arguments > 3) {
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
584 lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
585 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
586 return 2;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
587 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
588
13152
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
589 rid = resource_constants[luaL_checkoption(L, 1,NULL, resource_strings)];
6615
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: 6413
diff changeset
590
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: 6413
diff changeset
591 if(rid == -1) {
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
592 lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
593 lua_pushstring(L, "invalid-resource");
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
594 return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
595 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
596
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
597 /* Fetch current values to use as defaults */
6615
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: 6413
diff changeset
598 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
599 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
600 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
601 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
602 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
603
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
604 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
605 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
606
6615
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: 6413
diff changeset
607 if(setrlimit(rid, &lim)) {
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
608 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
609 lua_pushstring(L, "setrlimit-failed");
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
610 return 2;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
611 }
6615
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: 6413
diff changeset
612
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
613 lua_pushboolean(L, 1);
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
614 return 1;
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
615 }
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
616
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
617 static int lc_getrlimit(lua_State *L) {
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
618 int arguments = lua_gettop(L);
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
619 int rid = -1;
861
2a5373897128 Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents: 860
diff changeset
620 struct rlimit lim;
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
621
6615
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: 6413
diff changeset
622 if(arguments != 1) {
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-arguments");
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 }
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
627
13152
792f360a582b util.pposix: Use Lua enum API for resource limit name argument
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
628 rid = resource_constants[luaL_checkoption(L, 1, NULL, resource_strings)];
6615
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: 6413
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: 6413
diff changeset
630 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: 6413
diff changeset
631 if(getrlimit(rid, &lim)) {
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
632 lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
633 lua_pushstring(L, "getrlimit-failed.");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
634 return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
635 }
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
636 } else {
7919
251bf68922d9 util.pposix: Fix typo in comment
Kim Alvefur <zash@zash.se>
parents: 7889
diff changeset
637 /* Unsupported resource. Sorry I'm pretty limited by POSIX standard. */
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
638 lua_pushboolean(L, 0);
865
2dce34e9182d pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents: 864
diff changeset
639 lua_pushstring(L, "invalid-resource");
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
640 return 2;
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
641 }
6615
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: 6413
diff changeset
642
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
643 lua_pushboolean(L, 1);
6615
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: 6413
diff changeset
644
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: 6413
diff changeset
645 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
646 lua_pushstring(L, "unlimited");
6615
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: 6413
diff changeset
647 } else {
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
648 lua_pushinteger(L, lim.rlim_cur);
6615
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: 6413
diff changeset
649 }
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: 6413
diff changeset
650
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: 6413
diff changeset
651 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
652 lua_pushstring(L, "unlimited");
6615
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: 6413
diff changeset
653 } else {
10799
763bb2ce3f60 util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Kim Alvefur <zash@zash.se>
parents: 10480
diff changeset
654 lua_pushinteger(L, lim.rlim_max);
6615
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: 6413
diff changeset
655 }
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: 6413
diff changeset
656
860
048aaec22b57 Added missing code.
Tobias Markmann <tm@ayena.de>
parents: 859
diff changeset
657 return 3;
859
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
658 }
43f7e342135d Adding setrlimits() binding.
Tobias Markmann <tm@ayena.de>
parents: 804
diff changeset
659
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
660 static int lc_abort(lua_State *L) {
9151
fee3933ff346 pposix, signal: Ignore unused arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 9150
diff changeset
661 (void)L;
2060
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
662 abort();
2830
1df57426263a util.pposix: Fix return type of lc_abort to shush compiler warning
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
663 return 0;
2060
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
664 }
b23295b5428a util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents: 1842
diff changeset
665
13563
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
666 const char *pipe_flag_names[] = {
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
667 "cloexec",
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
668 "direct",
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
669 "nonblock"
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
670 };
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
671 const int pipe_flag_values[] = {
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
672 O_CLOEXEC,
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
673 O_DIRECT,
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
674 O_NONBLOCK
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
675 };
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
676
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
677
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
678 static int lc_pipe(lua_State *L) {
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
679 int fds[2];
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
680 int nflags = lua_gettop(L);
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
681
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
682 #if defined(__linux__)
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
683 int flags=0;
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
684 for(int i = 1; i<=nflags; i++) {
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
685 int flag_index = luaL_checkoption(L, i, NULL, pipe_flag_names);
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
686 flags |= pipe_flag_values[flag_index];
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
687 }
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
688
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
689 if(pipe2(fds, flags) == -1) {
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
690 #else
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
691 if(nflags != 0) {
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
692 luaL_argerror(L, 1, "Flags are not supported on this platform");
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
693 }
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
694 if(pipe(fds) == -1) {
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
695 #endif
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
696 luaL_pushfail(L);
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
697 lua_pushstring(L, strerror(errno));
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
698 return 2;
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
699 }
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
700
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
701 lua_pushinteger(L, fds[0]);
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
702 lua_pushinteger(L, fds[1]);
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
703 return 2;
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
704 }
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
705
13574
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
706 /* This helper function is adapted from Lua 5.3's liolib.c */
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
707 static int stdio_fclose (lua_State *L) {
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
708 int res = -1;
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
709 luaL_Stream *p = ((luaL_Stream *)luaL_checkudata(L, 1, LUA_FILEHANDLE));
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
710 if (p->f == NULL) {
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
711 return 0;
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
712 }
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
713 res = fclose(p->f);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
714 p->f = NULL;
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
715 return luaL_fileresult(L, (res == 0), NULL);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
716 }
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
717
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
718 static int lc_fdopen(lua_State *L) {
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
719 int fd = luaL_checkinteger(L, 1);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
720 const char *mode = luaL_checkstring(L, 2);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
721
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
722 luaL_Stream *file = (luaL_Stream *)lua_newuserdata(L, sizeof(luaL_Stream));
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
723 file->closef = stdio_fclose;
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
724 file->f = fdopen(fd, mode);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
725
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
726 if (!file->f) {
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
727 luaL_pushfail(L);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
728 lua_pushstring(L, strerror(errno));
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
729 return 2;
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
730 }
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
731
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
732 luaL_getmetatable(L, LUA_FILEHANDLE);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
733 lua_setmetatable(L, -2);
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
734 return 1;
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
735 }
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
736
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
737 static int lc_uname(lua_State *L) {
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
738 struct utsname uname_info;
6615
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: 6413
diff changeset
739
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: 6413
diff changeset
740 if(uname(&uname_info) != 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
741 luaL_pushfail(L);
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
742 lua_pushstring(L, strerror(errno));
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
743 return 2;
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
744 }
6615
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: 6413
diff changeset
745
7969
1c6a07606309 util-src: Specify size of various tables to be allocated
Kim Alvefur <zash@zash.se>
parents: 7966
diff changeset
746 lua_createtable(L, 0, 6);
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
747 lua_pushstring(L, uname_info.sysname);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
748 lua_setfield(L, -2, "sysname");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
749 lua_pushstring(L, uname_info.nodename);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
750 lua_setfield(L, -2, "nodename");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
751 lua_pushstring(L, uname_info.release);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
752 lua_setfield(L, -2, "release");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
753 lua_pushstring(L, uname_info.version);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
754 lua_setfield(L, -2, "version");
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
755 lua_pushstring(L, uname_info.machine);
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
756 lua_setfield(L, -2, "machine");
7931
b619b85e01aa util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents: 7926
diff changeset
757 #ifdef __USE_GNU
6942
f12deb882148 util.pposix: Expose the uname GNU extension field 'domainname'
Kim Alvefur <zash@zash.se>
parents: 6789
diff changeset
758 lua_pushstring(L, uname_info.domainname);
f12deb882148 util.pposix: Expose the uname GNU extension field 'domainname'
Kim Alvefur <zash@zash.se>
parents: 6789
diff changeset
759 lua_setfield(L, -2, "domainname");
f12deb882148 util.pposix: Expose the uname GNU extension field 'domainname'
Kim Alvefur <zash@zash.se>
parents: 6789
diff changeset
760 #endif
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
761 return 1;
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
762 }
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
763
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
764 static int lc_setenv(lua_State *L) {
7889
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
765 const char *var = luaL_checkstring(L, 1);
b8d694646597 util-src/*.c: Attach pointer * to name instead of type
Kim Alvefur <zash@zash.se>
parents: 7835
diff changeset
766 const char *value;
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
767
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
768 /* If the second argument is nil or nothing, unset the var */
6615
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: 6413
diff changeset
769 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: 6413
diff changeset
770 if(unsetenv(var) != 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
771 luaL_pushfail(L);
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
772 lua_pushstring(L, strerror(errno));
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
773 return 2;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
774 }
6615
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: 6413
diff changeset
775
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
776 lua_pushboolean(L, 1);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
777 return 1;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
778 }
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
779
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
780 value = luaL_checkstring(L, 2);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
781
6615
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: 6413
diff changeset
782 if(setenv(var, value, 1) != 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
783 luaL_pushfail(L);
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
784 lua_pushstring(L, strerror(errno));
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
785 return 2;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
786 }
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
787
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
788 lua_pushboolean(L, 1);
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
789 return 1;
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
790 }
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
791
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
792 #ifdef WITH_MALLINFO
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
793 static int lc_meminfo(lua_State *L) {
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
794 #if __GLIBC_PREREQ(2, 33)
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
795 struct mallinfo2 info = mallinfo2();
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
796 #define MALLINFO_T size_t
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
797 #else
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
798 struct mallinfo info = mallinfo();
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
799 #define MALLINFO_T unsigned
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
800 #endif
7969
1c6a07606309 util-src: Specify size of various tables to be allocated
Kim Alvefur <zash@zash.se>
parents: 7966
diff changeset
801 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
802 /* This is the total size of memory allocated with sbrk by malloc, in bytes. */
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
803 lua_pushinteger(L, (MALLINFO_T)info.arena);
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
804 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
805 /* This is the total size of memory allocated with mmap, in bytes. */
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
806 lua_pushinteger(L, (MALLINFO_T)info.hblkhd);
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
807 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
808 /* This is the total size of memory occupied by chunks handed out by malloc. */
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
809 lua_pushinteger(L, (MALLINFO_T)info.uordblks);
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
810 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
811 /* This is the total size of memory occupied by free (not in use) chunks. */
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
812 lua_pushinteger(L, (MALLINFO_T)info.fordblks);
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
813 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
814 /* 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
815 end of the heap (i.e., the high end of the virtual address space's data segment). */
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
816 lua_pushinteger(L, (MALLINFO_T)info.keepcost);
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
817 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
818 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
819 }
12154
760dd1fc3dc1 util.pposix: Use mallinfo2() on glibc 2.33, fix #1649
Kim Alvefur <zash@zash.se>
parents: 11656
diff changeset
820 #undef MALLINFO_T
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
821 #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
822
8010
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
823 /*
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
824 * 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: 7969
diff changeset
825 * Attempt to allocate space first
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
826 * Truncate to original size on failure
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
827 */
10480
94cacf9fd0ae util.*.c: Add static qualifiers everywhere
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 10411
diff changeset
828 static int lc_atomic_append(lua_State *L) {
8010
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
829 int err;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
830 size_t len;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
831
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
832 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: 7969
diff changeset
833 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: 7969
diff changeset
834
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
835 off_t offset = ftell(f);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
836
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
837 #if defined(__linux__)
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
838 /* 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: 7969
diff changeset
839 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: 7969
diff changeset
840 if(errno != 0) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
841 /* 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: 7969
diff changeset
842 err = errno;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
843 }
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
844 switch(err) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
845 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: 7969
diff changeset
846 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: 7969
diff changeset
847 /* 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: 7969
diff changeset
848 break;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
849
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
850 case ENOSPC: /* No space left */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
851 default: /* Other issues */
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
852 luaL_pushfail(L);
8010
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
853 lua_pushstring(L, strerror(err));
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
854 lua_pushinteger(L, err);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
855 return 3;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
856 }
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
857 }
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
858 #endif
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
859
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
860 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: 7969
diff changeset
861 if(fflush(f) == 0) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
862 lua_pushboolean(L, 1); /* Great success! */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
863 return 1;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
864 } else {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
865 err = errno;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
866 }
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
867 } else {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
868 err = ferror(f);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
869 }
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
870
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
871 fseek(f, offset, SEEK_SET);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
872
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
873 /* Cut partially written data */
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
874 if(ftruncate(fileno(f), offset)) {
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
875 /* 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: 7969
diff changeset
876 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: 7969
diff changeset
877 }
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
878
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
879 luaL_pushfail(L);
8010
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
880 lua_pushstring(L, strerror(err));
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
881 lua_pushinteger(L, err);
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
882 return 3;
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
883 }
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
884
13184
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
885 static int lc_remove_blocks(lua_State *L) {
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
886 #if defined(__linux__)
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
887 int err;
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
888
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
889 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
890 off_t offset = (off_t)luaL_checkinteger(L, 2);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
891 off_t length = (off_t)luaL_checkinteger(L, 3);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
892
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
893 errno = 0;
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
894
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
895 if((err = fallocate(fileno(f), FALLOC_FL_COLLAPSE_RANGE, offset, length))) {
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
896 if(errno != 0) {
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
897 /* Some old versions of Linux apparently use the return value instead of errno */
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
898 err = errno;
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
899 }
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
900
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
901 switch(err) {
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
902 default: /* Other issues */
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
903 luaL_pushfail(L);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
904 lua_pushstring(L, strerror(err));
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
905 lua_pushinteger(L, err);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
906 return 3;
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
907 }
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
908 }
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
909
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
910 lua_pushboolean(L, err == 0);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
911 return 1;
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
912 #else
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
913 luaL_pushfail(L);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
914 lua_pushstring(L, strerror(EOPNOTSUPP));
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
915 lua_pushinteger(L, EOPNOTSUPP);
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
916 return 3;
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
917 #endif
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
918 }
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
919
11656
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
920 static int lc_isatty(lua_State *L) {
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
921 FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
922 const int fd = fileno(f);
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
923 lua_pushboolean(L, isatty(fd));
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
924 return 1;
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
925 }
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
926
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
927 /* 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
928
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
929 int luaopen_prosody_util_pposix(lua_State *L) {
7818
54669df178c2 util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents: 7789
diff changeset
930 luaL_checkversion(L);
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
931 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
932 { "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
933
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
934 { "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
935
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
936 { "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
937 { "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
938 { "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
939 { "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
940
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
941 { "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
942 { "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
943 { "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
944
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
945 { "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
946 { "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
947 { "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
948
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
949 { "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
950
2443
b335ae55af77 util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents: 2441
diff changeset
951 { "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
952
13563
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
953 { "pipe", lc_pipe },
13574
f29d15aef6f8 util.pposix: Add fdopen() to return a Lua file object from an fd
Matthew Wild <mwild1@gmail.com>
parents: 13563
diff changeset
954 { "fdopen", lc_fdopen },
13563
5d4d7ed83f1a util.pposix: Add pipe() (with support for pipe2() flags on Linux)
Matthew Wild <mwild1@gmail.com>
parents: 13184
diff changeset
955
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
956 { "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
957 { "getrlimit", lc_getrlimit },
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
958
3481
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
959 { "uname", lc_uname },
72d3c8029178 util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents: 3471
diff changeset
960
4934
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
961 { "setenv", lc_setenv },
5a6a85719b7b util.pposix: Add setenv()
Kim Alvefur <zash@zash.se>
parents: 4415
diff changeset
962
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
963 #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
964 { "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
965 #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
966
8010
49feb0da29e1 util.pposix: Add function for atomically appending data to a file
Kim Alvefur <zash@zash.se>
parents: 7969
diff changeset
967 { "atomic_append", lc_atomic_append },
13184
d16845afb3e2 util.pposix: Add remove_blocks() for deleting parts of files
Kim Alvefur <zash@zash.se>
parents: 13152
diff changeset
968 { "remove_blocks", lc_remove_blocks },
5044
4ef0dbfead53 util.pposix: Add fallocate method, backed by either posix_fallocate() or Linux fallocate()
Kim Alvefur <zash@zash.se>
parents: 4950
diff changeset
969
11656
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
970 { "isatty", lc_isatty },
c368b4f6ee04 util.pposix: Bind isatty(3)
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
971
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
972 { 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
973 };
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
974
6411
6c8f6364bc48 util-src/*.c: Don't create globals when loaded
Kim Alvefur <zash@zash.se>
parents: 6156
diff changeset
975 lua_newtable(L);
6789
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: 6642
diff changeset
976 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
977
7926
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7919
diff changeset
978 #ifdef ENOENT
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7919
diff changeset
979 lua_pushinteger(L, ENOENT);
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7919
diff changeset
980 lua_setfield(L, -2, "ENOENT");
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7919
diff changeset
981 #endif
b009c27818c6 util.pposix: Expose ENOENT constant (usually 2, but you never know)
Kim Alvefur <zash@zash.se>
parents: 7919
diff changeset
982
727
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
983 lua_pushliteral(L, "pposix");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
984 lua_setfield(L, -2, "_NAME");
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
985
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
986 lua_pushliteral(L, MODULE_VERSION);
78c9542de94e pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents: 723
diff changeset
987 lua_setfield(L, -2, "_VERSION");
2436
ccc71b5d2e01 util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2060
diff changeset
988
586
b828d7d47973 Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
989 return 1;
3966
e71c19dac1c7 util.pposix: Remove extraneous semicolon
Matthew Wild <mwild1@gmail.com>
parents: 3482
diff changeset
990 }
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
991 int luaopen_util_pposix(lua_State *L) {
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
992 return luaopen_prosody_util_pposix(L);
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
993 }