Software /
code /
prosody
Annotate
util-src/pposix.c @ 4032:ee7f2aa5dc3b
util-src/Makefile: Change parameter order so that LDFLAGS always comes later than the input files
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 26 Dec 2010 05:37:27 +0000 |
parent | 3482:e1a4f7b15caf |
child | 3966:e71c19dac1c7 |
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 |
3481
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
16 #define MODULE_VERSION "0.3.5" |
727
78c9542de94e
pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
17 |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 #include <stdlib.h> |
2438
819ba949c7bc
util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents:
2437
diff
changeset
|
19 #include <math.h> |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 #include <unistd.h> |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 #include <libgen.h> |
859 | 22 #include <sys/resource.h> |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 #include <sys/types.h> |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 #include <sys/stat.h> |
3481
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
25 #include <sys/utsname.h> |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 #include <fcntl.h> |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
28 #include <syslog.h> |
804
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
29 #include <pwd.h> |
1578
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
30 #include <grp.h> |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
31 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
32 #include <string.h> |
804
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
33 #include <errno.h> |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 #include "lua.h" |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
35 #include "lauxlib.h" |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
36 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
37 /* Daemonization support */ |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
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
|
39 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
|
40 { |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 pid_t pid; |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
43 |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 if ( getppid() == 1 ) |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 { |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 lua_pushboolean(L, 0); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 lua_pushstring(L, "already-daemonized"); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 return 2; |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 } |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
50 |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 /* Attempt initial fork */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if((pid = fork()) < 0) |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 { |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 /* Forking failed */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 lua_pushboolean(L, 0); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 lua_pushstring(L, "fork-failed"); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 return 2; |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 } |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 else if(pid != 0) |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 { |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 /* We are the parent process */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 lua_pushboolean(L, 1); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 lua_pushnumber(L, pid); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 return 2; |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 } |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
66 |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 /* and we are the child process */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 if(setsid() == -1) |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 { |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 /* We failed to become session leader */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 /* (we probably already were) */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 lua_pushboolean(L, 0); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 lua_pushstring(L, "setsid-failed"); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 return 2; |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 } |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 /* Close stdin, stdout, stderr */ |
1195
6b91a2b39680
Close std{in,out,err} when daemonizing
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
78 close(0); |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 close(1); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 close(2); |
1195
6b91a2b39680
Close std{in,out,err} when daemonizing
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
81 |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 /* Final fork, use it wisely */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 if(fork()) |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 exit(0); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 /* Show's over, let's continue */ |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 lua_pushboolean(L, 1); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 lua_pushnil(L); |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 return 2; |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 } |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
92 /* Syslog support */ |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
93 |
796
63f56696c66c
util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents:
766
diff
changeset
|
94 const char * const facility_strings[] = { |
63f56696c66c
util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents:
766
diff
changeset
|
95 "auth", |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
96 #if !(defined(sun) || defined(__sun)) |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
97 "authpriv", |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
98 #endif |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
99 "cron", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
100 "daemon", |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
101 #if !(defined(sun) || defined(__sun)) |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
102 "ftp", |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
103 #endif |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
104 "kern", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
105 "local0", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
106 "local1", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
107 "local2", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
108 "local3", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
109 "local4", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
110 "local5", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
111 "local6", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
112 "local7", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
113 "lpr", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
114 "mail", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
115 "syslog", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
116 "user", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
117 "uucp", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
118 NULL |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
119 }; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
120 int facility_constants[] = { |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
121 LOG_AUTH, |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
122 #if !(defined(sun) || defined(__sun)) |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
123 LOG_AUTHPRIV, |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
124 #endif |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
125 LOG_CRON, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
126 LOG_DAEMON, |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
127 #if !(defined(sun) || defined(__sun)) |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
128 LOG_FTP, |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
129 #endif |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
130 LOG_KERN, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
131 LOG_LOCAL0, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
132 LOG_LOCAL1, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
133 LOG_LOCAL2, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
134 LOG_LOCAL3, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
135 LOG_LOCAL4, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
136 LOG_LOCAL5, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
137 LOG_LOCAL6, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
138 LOG_LOCAL7, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
139 LOG_LPR, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
140 LOG_MAIL, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
141 LOG_NEWS, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
142 LOG_SYSLOG, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
143 LOG_USER, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
144 LOG_UUCP, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
145 -1 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
146 }; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
147 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
148 /* " |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
149 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
|
150 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
|
151 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
|
152 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
|
153 constant. |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
154 " -- syslog manpage |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
155 */ |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
156 char* syslog_ident = NULL; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
157 |
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
|
158 int lc_syslog_open(lua_State* L) |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
159 { |
796
63f56696c66c
util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents:
766
diff
changeset
|
160 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
|
161 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
|
162 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
163 luaL_checkstring(L, 1); |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
164 |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
165 if(syslog_ident) |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
166 free(syslog_ident); |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
167 |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
168 syslog_ident = strdup(lua_tostring(L, 1)); |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
169 |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
170 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
|
171 return 0; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
172 } |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
173 |
796
63f56696c66c
util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents:
766
diff
changeset
|
174 const char * const level_strings[] = { |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
175 "debug", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
176 "info", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
177 "notice", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
178 "warn", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
179 "error", |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
180 NULL |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
181 }; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
182 int level_constants[] = { |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
183 LOG_DEBUG, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
184 LOG_INFO, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
185 LOG_NOTICE, |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
186 LOG_WARNING, |
1565
f1eeb049a0a5
pposix: Change LOG_EMERG -> LOG_CRIT, which makes more sense, thanks to intosi for reporting and an initial patch
Matthew Wild <mwild1@gmail.com>
parents:
1195
diff
changeset
|
187 LOG_CRIT, |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
188 -1 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
189 }; |
723
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
190 int lc_syslog_log(lua_State* L) |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
191 { |
796
63f56696c66c
util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents:
766
diff
changeset
|
192 int level = luaL_checkoption(L, 1, "notice", level_strings); |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
193 level = level_constants[level]; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
194 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
195 luaL_checkstring(L, 2); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
196 |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
197 syslog(level, "%s", lua_tostring(L, 2)); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
198 return 0; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
199 } |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
200 |
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
|
201 int lc_syslog_close(lua_State* L) |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
202 { |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
203 closelog(); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
204 if(syslog_ident) |
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 free(syslog_ident); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
207 syslog_ident = NULL; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
208 } |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
209 return 0; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
210 } |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
588
diff
changeset
|
211 |
729
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
212 int lc_syslog_setmask(lua_State* L) |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
213 { |
796
63f56696c66c
util.pposix: Fix incompatible pointer type compiler warnings
Matthew Wild <mwild1@gmail.com>
parents:
766
diff
changeset
|
214 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
|
215 int mask = 0; |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
216 do |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
217 { |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
218 mask |= LOG_MASK(level_constants[level_idx]); |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
219 } while (++level_idx<=4); |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
220 |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
221 setlogmask(mask); |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
222 return 0; |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
223 } |
f62ef65d5c01
pposix: Add syslog_setmask (use config: minimum_log_level = 'warn' etc.)
Matthew Wild <mwild1@gmail.com>
parents:
727
diff
changeset
|
224 |
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
|
225 /* 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
|
226 |
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
|
227 int lc_getpid(lua_State* L) |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
228 { |
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
|
229 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
|
230 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
|
231 } |
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
|
232 |
804
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
233 /* UID/GID functions */ |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
234 |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
235 int lc_getuid(lua_State* L) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
236 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
237 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
|
238 return 1; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
239 } |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
240 |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
241 int lc_getgid(lua_State* L) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
242 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
243 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
|
244 return 1; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
245 } |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
246 |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
247 int lc_setuid(lua_State* L) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
248 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
249 int uid = -1; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
250 if(lua_gettop(L) < 1) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
251 return 0; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
252 if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
253 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
254 /* Passed UID is actually a string, so look up the UID */ |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
255 struct passwd *p; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
256 p = getpwnam(lua_tostring(L, 1)); |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
257 if(!p) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
258 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
259 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
|
260 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
|
261 return 2; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
262 } |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
263 uid = p->pw_uid; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
264 } |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
265 else |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
266 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
267 uid = lua_tonumber(L, 1); |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
268 } |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
269 |
804
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
270 if(uid>-1) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
271 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
272 /* Ok, attempt setuid */ |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
273 errno = 0; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
274 if(setuid(uid)) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
275 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
276 /* Fail */ |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
277 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
|
278 switch(errno) |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
279 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
280 case EINVAL: |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
281 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
|
282 break; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
283 case EPERM: |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
284 lua_pushstring(L, "permission-denied"); |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
285 break; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
286 default: |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
287 lua_pushstring(L, "unknown-error"); |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
288 } |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
289 return 2; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
290 } |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
291 else |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
292 { |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
293 /* Success! */ |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
294 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
|
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 } |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
298 |
804
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
299 /* 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
|
300 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
|
301 lua_pushstring(L, "invalid-uid"); |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
302 return 2; |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
303 } |
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
304 |
1578
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
305 int lc_setgid(lua_State* L) |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
306 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
307 int gid = -1; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
308 if(lua_gettop(L) < 1) |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
309 return 0; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
310 if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
311 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
312 /* Passed GID is actually a string, so look up the GID */ |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
313 struct group *g; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
314 g = getgrnam(lua_tostring(L, 1)); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
315 if(!g) |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
316 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
317 lua_pushboolean(L, 0); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
318 lua_pushstring(L, "no-such-group"); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
319 return 2; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
320 } |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
321 gid = g->gr_gid; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
322 } |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
323 else |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
324 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
325 gid = lua_tonumber(L, 1); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
326 } |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
327 |
1578
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
328 if(gid>-1) |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
329 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
330 /* Ok, attempt setgid */ |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
331 errno = 0; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
332 if(setgid(gid)) |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
333 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
334 /* Fail */ |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
335 lua_pushboolean(L, 0); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
336 switch(errno) |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
337 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
338 case EINVAL: |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
339 lua_pushstring(L, "invalid-gid"); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
340 break; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
341 case EPERM: |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
342 lua_pushstring(L, "permission-denied"); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
343 break; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
344 default: |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
345 lua_pushstring(L, "unknown-error"); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
346 } |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
347 return 2; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
348 } |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
349 else |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
350 { |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
351 /* Success! */ |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
352 lua_pushboolean(L, 1); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
353 return 1; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
354 } |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
355 } |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
356 |
1578
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
357 /* 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
|
358 lua_pushboolean(L, 0); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
359 lua_pushstring(L, "invalid-gid"); |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
360 return 2; |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
361 } |
5bd8b3bdbfbc
pposix: Add setgid() function
Matthew Wild <mwild1@gmail.com>
parents:
1565
diff
changeset
|
362 |
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
|
363 int lc_initgroups(lua_State* L) |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
364 { |
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
|
365 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
|
366 gid_t gid; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
367 struct passwd *p; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
368 |
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
|
369 if(!lua_isstring(L, 1)) |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
370 { |
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
|
371 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
|
372 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
|
373 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
|
374 } |
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
|
375 p = getpwnam(lua_tostring(L, 1)); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
376 if(!p) |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
377 { |
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
|
378 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
|
379 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
|
380 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
|
381 } |
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
|
382 if(lua_gettop(L) < 2) |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
383 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
|
384 switch(lua_type(L, 2)) |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
385 { |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
386 case LUA_TNIL: |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
387 gid = p->pw_gid; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
388 break; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
389 case LUA_TNUMBER: |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
390 gid = lua_tointeger(L, 2); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
391 break; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
392 default: |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
393 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
|
394 lua_pushstring(L, "invalid-gid"); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
395 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
|
396 } |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
397 ret = initgroups(lua_tostring(L, 1), gid); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
398 switch(errno) |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
399 { |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
400 case 0: |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
401 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
|
402 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
|
403 break; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
404 case ENOMEM: |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
405 lua_pushnil(L); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
406 lua_pushstring(L, "no-memory"); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
407 break; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
408 case EPERM: |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
409 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
|
410 lua_pushstring(L, "permission-denied"); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
411 break; |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
412 default: |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
413 lua_pushnil(L); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
414 lua_pushstring(L, "unknown-error"); |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
415 } |
482275e38224
util.pposix, prosodyctl, mod_posix: Add initgroups() function, and bump module version. prosodyctl inits groups with the groups of prosody_user. (thanks dbb)
Matthew Wild <mwild1@gmail.com>
parents:
2925
diff
changeset
|
416 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
|
417 } |
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
|
418 |
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
|
419 int lc_umask(lua_State* L) |
819ba949c7bc
util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents:
2437
diff
changeset
|
420 { |
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
|
421 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
|
422 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
|
423 |
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
|
424 snprintf(old_mode_string, sizeof(old_mode_string), "%03o", old_mode); |
819ba949c7bc
util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents:
2437
diff
changeset
|
425 old_mode_string[sizeof(old_mode_string)-1] = 0; |
819ba949c7bc
util.pposix: Add pposix.umask(), bump version to 0.3.2 (and do the same in mod_posix)
Matthew Wild <mwild1@gmail.com>
parents:
2437
diff
changeset
|
426 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
|
427 |
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
|
428 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
|
429 } |
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
|
430 |
2443
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
431 int lc_mkdir(lua_State* L) |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
432 { |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
433 int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
434 | S_IRGRP | S_IWGRP | S_IXGRP |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
435 | S_IROTH | S_IXOTH); /* mode 775 */ |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
436 |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
437 lua_pushboolean(L, ret==0); |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
438 if(ret) |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
439 { |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
440 lua_pushstring(L, strerror(errno)); |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
441 return 2; |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
442 } |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
443 return 1; |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
444 } |
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
445 |
859 | 446 /* Like POSIX's setrlimit()/getrlimit() API functions. |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
447 * |
859 | 448 * Syntax: |
449 * pposix.setrlimit( resource, soft limit, hard limit) | |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
450 * |
859 | 451 * 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
|
452 * |
859 | 453 * Example usage: |
454 * pposix.setrlimit("NOFILE", 1000, 2000) | |
455 */ | |
456 int string2resource(const char *s) { | |
861
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
457 if (!strcmp(s, "CORE")) return RLIMIT_CORE; |
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
458 if (!strcmp(s, "CPU")) return RLIMIT_CPU; |
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
459 if (!strcmp(s, "DATA")) return RLIMIT_DATA; |
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
460 if (!strcmp(s, "FSIZE")) return RLIMIT_FSIZE; |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
461 if (!strcmp(s, "NOFILE")) return RLIMIT_NOFILE; |
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
462 if (!strcmp(s, "STACK")) return RLIMIT_STACK; |
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
463 #if !(defined(sun) || defined(__sun)) |
861
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
464 if (!strcmp(s, "MEMLOCK")) return RLIMIT_MEMLOCK; |
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
465 if (!strcmp(s, "NPROC")) return RLIMIT_NPROC; |
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
466 if (!strcmp(s, "RSS")) return RLIMIT_RSS; |
1842
8337c0d4aee4
util.pposix: Compatibility with Solaris systems (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents:
1579
diff
changeset
|
467 #endif |
859 | 468 return -1; |
469 } | |
470 | |
471 int lc_setrlimit(lua_State *L) { | |
472 int arguments = lua_gettop(L); | |
473 int softlimit = -1; | |
474 int hardlimit = -1; | |
475 const char *resource = NULL; | |
476 int rid = -1; | |
477 if(arguments < 1 || arguments > 3) { | |
478 lua_pushboolean(L, 0); | |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
479 lua_pushstring(L, "incorrect-arguments"); |
859 | 480 } |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
481 |
859 | 482 resource = luaL_checkstring(L, 1); |
483 softlimit = luaL_checkinteger(L, 2); | |
484 hardlimit = luaL_checkinteger(L, 3); | |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
485 |
859 | 486 rid = string2resource(resource); |
487 if (rid != -1) { | |
861
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
488 struct rlimit lim; |
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
489 struct rlimit lim_current; |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
490 |
859 | 491 if (softlimit < 0 || hardlimit < 0) { |
492 if (getrlimit(rid, &lim_current)) { | |
493 lua_pushboolean(L, 0); | |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
494 lua_pushstring(L, "getrlimit-failed"); |
859 | 495 return 2; |
496 } | |
497 } | |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
498 |
861
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
499 if (softlimit < 0) lim.rlim_cur = lim_current.rlim_cur; |
859 | 500 else lim.rlim_cur = softlimit; |
501 if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max; | |
502 else lim.rlim_max = hardlimit; | |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
503 |
859 | 504 if (setrlimit(rid, &lim)) { |
505 lua_pushboolean(L, 0); | |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
506 lua_pushstring(L, "setrlimit-failed"); |
859 | 507 return 2; |
508 } | |
509 } else { | |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
510 /* Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard. */ |
859 | 511 lua_pushboolean(L, 0); |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
512 lua_pushstring(L, "invalid-resource"); |
859 | 513 return 2; |
514 } | |
515 lua_pushboolean(L, 1); | |
516 return 1; | |
517 } | |
518 | |
519 int lc_getrlimit(lua_State *L) { | |
860 | 520 int arguments = lua_gettop(L); |
521 const char *resource = NULL; | |
522 int rid = -1; | |
861
2a5373897128
Make the code actually build.
Tobias Markmann <tm@ayena.de>
parents:
860
diff
changeset
|
523 struct rlimit lim; |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
524 |
860 | 525 if (arguments != 1) { |
526 lua_pushboolean(L, 0); | |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
527 lua_pushstring(L, "invalid-arguments"); |
860 | 528 return 2; |
529 } | |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
530 |
860 | 531 resource = luaL_checkstring(L, 1); |
532 rid = string2resource(resource); | |
533 if (rid != -1) { | |
534 if (getrlimit(rid, &lim)) { | |
535 lua_pushboolean(L, 0); | |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
536 lua_pushstring(L, "getrlimit-failed."); |
860 | 537 return 2; |
538 } | |
539 } else { | |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
540 /* Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard. */ |
860 | 541 lua_pushboolean(L, 0); |
865
2dce34e9182d
pposix: Standardize error messages
Matthew Wild <mwild1@gmail.com>
parents:
864
diff
changeset
|
542 lua_pushstring(L, "invalid-resource"); |
860 | 543 return 2; |
544 } | |
545 lua_pushboolean(L, 1); | |
546 lua_pushnumber(L, lim.rlim_cur); | |
547 lua_pushnumber(L, lim.rlim_max); | |
548 return 3; | |
859 | 549 } |
550 | |
2830
1df57426263a
util.pposix: Fix return type of lc_abort to shush compiler warning
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
551 int lc_abort(lua_State* L) |
2060
b23295b5428a
util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents:
1842
diff
changeset
|
552 { |
b23295b5428a
util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents:
1842
diff
changeset
|
553 abort(); |
2830
1df57426263a
util.pposix: Fix return type of lc_abort to shush compiler warning
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
554 return 0; |
2060
b23295b5428a
util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents:
1842
diff
changeset
|
555 } |
b23295b5428a
util.pposix: Add abort() function
Matthew Wild <mwild1@gmail.com>
parents:
1842
diff
changeset
|
556 |
3481
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
557 int lc_uname(lua_State* L) |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
558 { |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
559 struct utsname uname_info; |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
560 if(uname(&uname_info) != 0) |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
561 { |
3482
e1a4f7b15caf
util.pposix: uname(): Fix to push nil,err in case of error
Matthew Wild <mwild1@gmail.com>
parents:
3481
diff
changeset
|
562 lua_pushnil(L); |
3481
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
563 lua_pushstring(L, strerror(errno)); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
564 return 2; |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
565 } |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
566 lua_newtable(L); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
567 lua_pushstring(L, uname_info.sysname); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
568 lua_setfield(L, -2, "sysname"); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
569 lua_pushstring(L, uname_info.nodename); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
570 lua_setfield(L, -2, "nodename"); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
571 lua_pushstring(L, uname_info.release); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
572 lua_setfield(L, -2, "release"); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
573 lua_pushstring(L, uname_info.version); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
574 lua_setfield(L, -2, "version"); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
575 lua_pushstring(L, uname_info.machine); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
576 lua_setfield(L, -2, "machine"); |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
577 return 1; |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
578 } |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
579 |
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
|
580 /* 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
|
581 |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
582 int luaopen_util_pposix(lua_State *L) |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
583 { |
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
|
584 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
|
585 { "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
|
586 |
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
|
587 { "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
|
588 |
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
|
589 { "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
|
590 { "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
|
591 { "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
|
592 { "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
|
593 |
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
|
594 { "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
|
595 { "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
|
596 { "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
|
597 |
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
|
598 { "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
|
599 { "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
|
600 { "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
|
601 |
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
|
602 { "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
|
603 |
2443
b335ae55af77
util.pposix: Add mkdir(path)
Matthew Wild <mwild1@gmail.com>
parents:
2441
diff
changeset
|
604 { "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
|
605 |
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
|
606 { "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
|
607 { "getrlimit", lc_getrlimit }, |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
608 |
3481
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
609 { "uname", lc_uname }, |
72d3c8029178
util.pposix: Add pposix.uname(), bump version
Matthew Wild <mwild1@gmail.com>
parents:
3471
diff
changeset
|
610 |
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
|
611 { 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
|
612 }; |
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
|
613 |
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
|
614 luaL_register(L, "pposix", exports); |
804
9bc1544c99b7
util.pposix: Add getuid/setuid (we don't use them yet)
Matthew Wild <mwild1@gmail.com>
parents:
796
diff
changeset
|
615 |
727
78c9542de94e
pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
616 lua_pushliteral(L, "pposix"); |
78c9542de94e
pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
617 lua_setfield(L, -2, "_NAME"); |
78c9542de94e
pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
618 |
78c9542de94e
pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
619 lua_pushliteral(L, MODULE_VERSION); |
78c9542de94e
pposix: Add _NAME and _VERSION
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
620 lua_setfield(L, -2, "_VERSION"); |
2436
ccc71b5d2e01
util.posix: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2060
diff
changeset
|
621 |
586
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
622 return 1; |
b828d7d47973
Add posix support library, and adjust makefiles for it
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
623 }; |