Comparison

util-src/pposix.c @ 2436:ccc71b5d2e01

util.posix: Trailing whitespace
author Matthew Wild <mwild1@gmail.com>
date Sun, 10 Jan 2010 17:35:37 +0000
parent 2060:b23295b5428a
child 2437:b1ba2473fd91
comparison
equal deleted inserted replaced
2435:1ab73691b58e 2436:ccc71b5d2e01
1 /* Prosody IM v0.4 1 /* Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild 2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain 3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- Copyright (C) 2009 Tobias Markmann 4 -- Copyright (C) 2009 Tobias Markmann
5 -- 5 --
6 -- This project is MIT/X11 licensed. Please see the 6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information. 7 -- COPYING file in the source package for more information.
8 -- 8 --
9 */ 9 */
10 10
36 36
37 static int lc_daemonize(lua_State *L) 37 static int lc_daemonize(lua_State *L)
38 { 38 {
39 39
40 pid_t pid; 40 pid_t pid;
41 41
42 if ( getppid() == 1 ) 42 if ( getppid() == 1 )
43 { 43 {
44 lua_pushboolean(L, 0); 44 lua_pushboolean(L, 0);
45 lua_pushstring(L, "already-daemonized"); 45 lua_pushstring(L, "already-daemonized");
46 return 2; 46 return 2;
47 } 47 }
48 48
49 /* Attempt initial fork */ 49 /* Attempt initial fork */
50 if((pid = fork()) < 0) 50 if((pid = fork()) < 0)
51 { 51 {
52 /* Forking failed */ 52 /* Forking failed */
53 lua_pushboolean(L, 0); 53 lua_pushboolean(L, 0);
59 /* We are the parent process */ 59 /* We are the parent process */
60 lua_pushboolean(L, 1); 60 lua_pushboolean(L, 1);
61 lua_pushnumber(L, pid); 61 lua_pushnumber(L, pid);
62 return 2; 62 return 2;
63 } 63 }
64 64
65 /* and we are the child process */ 65 /* and we are the child process */
66 if(setsid() == -1) 66 if(setsid() == -1)
67 { 67 {
68 /* We failed to become session leader */ 68 /* We failed to become session leader */
69 /* (we probably already were) */ 69 /* (we probably already were) */
148 Thus, if the string it points to is changed, syslog() may start 148 Thus, if the string it points to is changed, syslog() may start
149 prepending the changed string, and if the string it points to ceases to 149 prepending the changed string, and if the string it points to ceases to
150 exist, the results are undefined. Most portable is to use a string 150 exist, the results are undefined. Most portable is to use a string
151 constant. 151 constant.
152 " -- syslog manpage 152 " -- syslog manpage
153 */ 153 */
154 char* syslog_ident = NULL; 154 char* syslog_ident = NULL;
155 155
156 int lc_syslog_open(lua_State* L) 156 int lc_syslog_open(lua_State* L)
157 { 157 {
158 int facility = luaL_checkoption(L, 2, "daemon", facility_strings); 158 int facility = luaL_checkoption(L, 2, "daemon", facility_strings);
159 facility = facility_constants[facility]; 159 facility = facility_constants[facility];
160 160
161 luaL_checkstring(L, 1); 161 luaL_checkstring(L, 1);
162 162
163 if(syslog_ident) 163 if(syslog_ident)
164 free(syslog_ident); 164 free(syslog_ident);
165 165
166 syslog_ident = strdup(lua_tostring(L, 1)); 166 syslog_ident = strdup(lua_tostring(L, 1));
167 167
168 openlog(syslog_ident, LOG_PID, facility); 168 openlog(syslog_ident, LOG_PID, facility);
169 return 0; 169 return 0;
170 } 170 }
171 171
172 const char * const level_strings[] = { 172 const char * const level_strings[] = {
262 } 262 }
263 else 263 else
264 { 264 {
265 uid = lua_tonumber(L, 1); 265 uid = lua_tonumber(L, 1);
266 } 266 }
267 267
268 if(uid>-1) 268 if(uid>-1)
269 { 269 {
270 /* Ok, attempt setuid */ 270 /* Ok, attempt setuid */
271 errno = 0; 271 errno = 0;
272 if(setuid(uid)) 272 if(setuid(uid))
291 /* Success! */ 291 /* Success! */
292 lua_pushboolean(L, 1); 292 lua_pushboolean(L, 1);
293 return 1; 293 return 1;
294 } 294 }
295 } 295 }
296 296
297 /* Seems we couldn't find a valid UID to switch to */ 297 /* Seems we couldn't find a valid UID to switch to */
298 lua_pushboolean(L, 0); 298 lua_pushboolean(L, 0);
299 lua_pushstring(L, "invalid-uid"); 299 lua_pushstring(L, "invalid-uid");
300 return 2; 300 return 2;
301 } 301 }
320 } 320 }
321 else 321 else
322 { 322 {
323 gid = lua_tonumber(L, 1); 323 gid = lua_tonumber(L, 1);
324 } 324 }
325 325
326 if(gid>-1) 326 if(gid>-1)
327 { 327 {
328 /* Ok, attempt setgid */ 328 /* Ok, attempt setgid */
329 errno = 0; 329 errno = 0;
330 if(setgid(gid)) 330 if(setgid(gid))
349 /* Success! */ 349 /* Success! */
350 lua_pushboolean(L, 1); 350 lua_pushboolean(L, 1);
351 return 1; 351 return 1;
352 } 352 }
353 } 353 }
354 354
355 /* Seems we couldn't find a valid GID to switch to */ 355 /* Seems we couldn't find a valid GID to switch to */
356 lua_pushboolean(L, 0); 356 lua_pushboolean(L, 0);
357 lua_pushstring(L, "invalid-gid"); 357 lua_pushstring(L, "invalid-gid");
358 return 2; 358 return 2;
359 } 359 }
360 360
361 /* Like POSIX's setrlimit()/getrlimit() API functions. 361 /* Like POSIX's setrlimit()/getrlimit() API functions.
362 * 362 *
363 * Syntax: 363 * Syntax:
364 * pposix.setrlimit( resource, soft limit, hard limit) 364 * pposix.setrlimit( resource, soft limit, hard limit)
365 * 365 *
366 * Any negative limit will be replace with the current limit by an additional call of getrlimit(). 366 * Any negative limit will be replace with the current limit by an additional call of getrlimit().
367 * 367 *
368 * Example usage: 368 * Example usage:
369 * pposix.setrlimit("NOFILE", 1000, 2000) 369 * pposix.setrlimit("NOFILE", 1000, 2000)
370 */ 370 */
371 int string2resource(const char *s) { 371 int string2resource(const char *s) {
372 if (!strcmp(s, "CORE")) return RLIMIT_CORE; 372 if (!strcmp(s, "CORE")) return RLIMIT_CORE;
391 int rid = -1; 391 int rid = -1;
392 if(arguments < 1 || arguments > 3) { 392 if(arguments < 1 || arguments > 3) {
393 lua_pushboolean(L, 0); 393 lua_pushboolean(L, 0);
394 lua_pushstring(L, "incorrect-arguments"); 394 lua_pushstring(L, "incorrect-arguments");
395 } 395 }
396 396
397 resource = luaL_checkstring(L, 1); 397 resource = luaL_checkstring(L, 1);
398 softlimit = luaL_checkinteger(L, 2); 398 softlimit = luaL_checkinteger(L, 2);
399 hardlimit = luaL_checkinteger(L, 3); 399 hardlimit = luaL_checkinteger(L, 3);
400 400
401 rid = string2resource(resource); 401 rid = string2resource(resource);
402 if (rid != -1) { 402 if (rid != -1) {
403 struct rlimit lim; 403 struct rlimit lim;
404 struct rlimit lim_current; 404 struct rlimit lim_current;
405 405
406 if (softlimit < 0 || hardlimit < 0) { 406 if (softlimit < 0 || hardlimit < 0) {
407 if (getrlimit(rid, &lim_current)) { 407 if (getrlimit(rid, &lim_current)) {
408 lua_pushboolean(L, 0); 408 lua_pushboolean(L, 0);
409 lua_pushstring(L, "getrlimit-failed"); 409 lua_pushstring(L, "getrlimit-failed");
410 return 2; 410 return 2;
411 } 411 }
412 } 412 }
413 413
414 if (softlimit < 0) lim.rlim_cur = lim_current.rlim_cur; 414 if (softlimit < 0) lim.rlim_cur = lim_current.rlim_cur;
415 else lim.rlim_cur = softlimit; 415 else lim.rlim_cur = softlimit;
416 if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max; 416 if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max;
417 else lim.rlim_max = hardlimit; 417 else lim.rlim_max = hardlimit;
418 418
419 if (setrlimit(rid, &lim)) { 419 if (setrlimit(rid, &lim)) {
420 lua_pushboolean(L, 0); 420 lua_pushboolean(L, 0);
421 lua_pushstring(L, "setrlimit-failed"); 421 lua_pushstring(L, "setrlimit-failed");
422 return 2; 422 return 2;
423 } 423 }
434 int lc_getrlimit(lua_State *L) { 434 int lc_getrlimit(lua_State *L) {
435 int arguments = lua_gettop(L); 435 int arguments = lua_gettop(L);
436 const char *resource = NULL; 436 const char *resource = NULL;
437 int rid = -1; 437 int rid = -1;
438 struct rlimit lim; 438 struct rlimit lim;
439 439
440 if (arguments != 1) { 440 if (arguments != 1) {
441 lua_pushboolean(L, 0); 441 lua_pushboolean(L, 0);
442 lua_pushstring(L, "invalid-arguments"); 442 lua_pushstring(L, "invalid-arguments");
443 return 2; 443 return 2;
444 } 444 }
445 445
446 resource = luaL_checkstring(L, 1); 446 resource = luaL_checkstring(L, 1);
447 rid = string2resource(resource); 447 rid = string2resource(resource);
448 if (rid != -1) { 448 if (rid != -1) {
449 if (getrlimit(rid, &lim)) { 449 if (getrlimit(rid, &lim)) {
450 lua_pushboolean(L, 0); 450 lua_pushboolean(L, 0);
502 502
503 lua_pushcfunction(L, lc_setuid); 503 lua_pushcfunction(L, lc_setuid);
504 lua_setfield(L, -2, "setuid"); 504 lua_setfield(L, -2, "setuid");
505 lua_pushcfunction(L, lc_setgid); 505 lua_pushcfunction(L, lc_setgid);
506 lua_setfield(L, -2, "setgid"); 506 lua_setfield(L, -2, "setgid");
507 507
508 lua_pushcfunction(L, lc_setrlimit); 508 lua_pushcfunction(L, lc_setrlimit);
509 lua_setfield(L, -2, "setrlimit"); 509 lua_setfield(L, -2, "setrlimit");
510 510
511 lua_pushcfunction(L, lc_getrlimit); 511 lua_pushcfunction(L, lc_getrlimit);
512 lua_setfield(L, -2, "getrlimit"); 512 lua_setfield(L, -2, "getrlimit");
513 513
514 lua_pushliteral(L, "pposix"); 514 lua_pushliteral(L, "pposix");
515 lua_setfield(L, -2, "_NAME"); 515 lua_setfield(L, -2, "_NAME");
516 516
517 lua_pushliteral(L, MODULE_VERSION); 517 lua_pushliteral(L, MODULE_VERSION);
518 lua_setfield(L, -2, "_VERSION"); 518 lua_setfield(L, -2, "_VERSION");
519 519
520 return 1; 520 return 1;
521 }; 521 };