Comparison

util-src/pposix.c @ 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.
author Matthew Wild <mwild1@gmail.com>
date Thu, 15 Jan 2009 20:59:36 +0000
parent 722:63456c9d0522
child 727:78c9542de94e
comparison
equal deleted inserted replaced
722:63456c9d0522 723:c1e7d280c174
35 #include "lua.h" 35 #include "lua.h"
36 #include "lauxlib.h" 36 #include "lauxlib.h"
37 37
38 /* Daemonization support */ 38 /* Daemonization support */
39 39
40 static int daemonize(lua_State *L) 40 static int lc_daemonize(lua_State *L)
41 { 41 {
42 42
43 pid_t pid; 43 pid_t pid;
44 44
45 if ( getppid() == 1 ) 45 if ( getppid() == 1 )
145 constant. 145 constant.
146 " -- syslog manpage 146 " -- syslog manpage
147 */ 147 */
148 char* syslog_ident = NULL; 148 char* syslog_ident = NULL;
149 149
150 int syslog_open(lua_State* L) 150 int lc_syslog_open(lua_State* L)
151 { 151 {
152 int facility = luaL_checkoption(L, 2, "daemon", &facility_strings); 152 int facility = luaL_checkoption(L, 2, "daemon", &facility_strings);
153 facility = facility_constants[facility]; 153 facility = facility_constants[facility];
154 154
155 luaL_checkstring(L, 1); 155 luaL_checkstring(L, 1);
177 LOG_NOTICE, 177 LOG_NOTICE,
178 LOG_WARNING, 178 LOG_WARNING,
179 LOG_EMERG, 179 LOG_EMERG,
180 -1 180 -1
181 }; 181 };
182 int syslog_log(lua_State* L) 182 int lc_syslog_log(lua_State* L)
183 { 183 {
184 int level = luaL_checkoption(L, 1, "notice", &level_strings); 184 int level = luaL_checkoption(L, 1, "notice", &level_strings);
185 level = level_constants[level]; 185 level = level_constants[level];
186 186
187 luaL_checkstring(L, 2); 187 luaL_checkstring(L, 2);
188 188
189 syslog(level, "%s", lua_tostring(L, 2)); 189 syslog(level, "%s", lua_tostring(L, 2));
190 return 0; 190 return 0;
191 } 191 }
192 192
193 int syslog_close(lua_State* L) 193 int lc_syslog_close(lua_State* L)
194 { 194 {
195 closelog(); 195 closelog();
196 if(syslog_ident) 196 if(syslog_ident)
197 { 197 {
198 free(syslog_ident); 198 free(syslog_ident);
199 syslog_ident = NULL; 199 syslog_ident = NULL;
200 } 200 }
201 return 0; 201 return 0;
202 } 202 }
203 203
204 /* getpid */
205
206 int lc_getpid(lua_State* L)
207 {
208 lua_pushinteger(L, getpid());
209 return 1;
210 }
211
212 /* Register functions */
213
204 int luaopen_util_pposix(lua_State *L) 214 int luaopen_util_pposix(lua_State *L)
205 { 215 {
206 lua_newtable(L); 216 lua_newtable(L);
207 217
208 lua_pushcfunction(L, daemonize); 218 lua_pushcfunction(L, lc_daemonize);
209 lua_setfield(L, -2, "daemonize"); 219 lua_setfield(L, -2, "daemonize");
210 220
211 lua_pushcfunction(L, syslog_open); 221 lua_pushcfunction(L, lc_syslog_open);
212 lua_setfield(L, -2, "syslog_open"); 222 lua_setfield(L, -2, "syslog_open");
213 223
214 lua_pushcfunction(L, syslog_close); 224 lua_pushcfunction(L, lc_syslog_close);
215 lua_setfield(L, -2, "syslog_close"); 225 lua_setfield(L, -2, "syslog_close");
216 226
217 lua_pushcfunction(L, syslog_log); 227 lua_pushcfunction(L, lc_syslog_log);
218 lua_setfield(L, -2, "syslog_log"); 228 lua_setfield(L, -2, "syslog_log");
229
230 lua_pushcfunction(L, lc_getpid);
231 lua_setfield(L, -2, "getpid");
219 232
220 return 1; 233 return 1;
221 }; 234 };