util-src/pposix.c

changeset 3471
482275e38224
parent 2925
692b3c6c5bd2
child 3481
72d3c8029178
equal deleted inserted replaced
3470:0e59b5cdd57b 3471:482275e38224
11 /* 11 /*
12 * pposix.c 12 * pposix.c
13 * POSIX support functions for Lua 13 * POSIX support functions for Lua
14 */ 14 */
15 15
16 #define MODULE_VERSION "0.3.3" 16 #define MODULE_VERSION "0.3.4"
17 17
18 #include <stdlib.h> 18 #include <stdlib.h>
19 #include <math.h> 19 #include <math.h>
20 #include <unistd.h> 20 #include <unistd.h>
21 #include <libgen.h> 21 #include <libgen.h>
357 lua_pushboolean(L, 0); 357 lua_pushboolean(L, 0);
358 lua_pushstring(L, "invalid-gid"); 358 lua_pushstring(L, "invalid-gid");
359 return 2; 359 return 2;
360 } 360 }
361 361
362 int lc_initgroups(lua_State* L)
363 {
364 int ret;
365 gid_t gid;
366 struct passwd *p;
367
368 if(!lua_isstring(L, 1))
369 {
370 lua_pushnil(L);
371 lua_pushstring(L, "invalid-username");
372 return 2;
373 }
374 p = getpwnam(lua_tostring(L, 1));
375 if(!p)
376 {
377 lua_pushnil(L);
378 lua_pushstring(L, "no-such-user");
379 return 2;
380 }
381 if(lua_gettop(L) < 2)
382 lua_pushnil(L);
383 switch(lua_type(L, 2))
384 {
385 case LUA_TNIL:
386 gid = p->pw_gid;
387 break;
388 case LUA_TNUMBER:
389 gid = lua_tointeger(L, 2);
390 break;
391 default:
392 lua_pushnil(L);
393 lua_pushstring(L, "invalid-gid");
394 return 2;
395 }
396 ret = initgroups(lua_tostring(L, 1), gid);
397 switch(errno)
398 {
399 case 0:
400 lua_pushboolean(L, 1);
401 lua_pushnil(L);
402 break;
403 case ENOMEM:
404 lua_pushnil(L);
405 lua_pushstring(L, "no-memory");
406 break;
407 case EPERM:
408 lua_pushnil(L);
409 lua_pushstring(L, "permission-denied");
410 break;
411 default:
412 lua_pushnil(L);
413 lua_pushstring(L, "unknown-error");
414 }
415 return 2;
416 }
417
362 int lc_umask(lua_State* L) 418 int lc_umask(lua_State* L)
363 { 419 {
364 char old_mode_string[7]; 420 char old_mode_string[7];
365 mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8)); 421 mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8));
366 422
515 { "getuid", lc_getuid }, 571 { "getuid", lc_getuid },
516 { "getgid", lc_getgid }, 572 { "getgid", lc_getgid },
517 573
518 { "setuid", lc_setuid }, 574 { "setuid", lc_setuid },
519 { "setgid", lc_setgid }, 575 { "setgid", lc_setgid },
576 { "initgroups", lc_initgroups },
520 577
521 { "umask", lc_umask }, 578 { "umask", lc_umask },
522 579
523 { "mkdir", lc_mkdir }, 580 { "mkdir", lc_mkdir },
524 581

mercurial