X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=libs%2Fnixio%2Fsrc%2Fprocess.c;h=19fa10ef2cdfd7e7e658e8c1e51f5c0abbacd3ba;hp=1f2ae153fb78b6c6084375fad353ef56b8eb4c69;hb=f151bb804f558f5132a5b39feb38116ea2e95b21;hpb=d6e251adbe71dfeac69290b8905f444e19feaadd diff --git a/libs/nixio/src/process.c b/libs/nixio/src/process.c index 1f2ae153f..19fa10ef2 100644 --- a/libs/nixio/src/process.c +++ b/libs/nixio/src/process.c @@ -17,10 +17,13 @@ */ #include "nixio.h" +#include +#include #include #include #include #include +#include static int nixio_fork(lua_State *L) { pid_t pid = fork(); @@ -81,12 +84,66 @@ static int nixio_kill(lua_State *L) { return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2))); } +static int nixio_getpid(lua_State *L) { + lua_pushinteger(L, getpid()); + return 1; +} + +static int nixio_getppid(lua_State *L) { + lua_pushinteger(L, getppid()); + return 1; +} + +static int nixio_getuid(lua_State *L) { + lua_pushinteger(L, getuid()); + return 1; +} + +static int nixio_getgid(lua_State *L) { + lua_pushinteger(L, getgid()); + return 1; +} + +static int nixio_setgid(lua_State *L) { + gid_t gid; + if (lua_isstring(L, 1)) { + struct group *g = getgrnam(lua_tostring(L, 1)); + gid = (!g) ? -1 : g->gr_gid; + } else if (lua_isnumber(L, 1)) { + gid = lua_tointeger(L, 1); + } else { + return luaL_argerror(L, 1, "supported values: , "); + } + + return nixio__pstatus(L, !setgid(gid)); +} + +static int nixio_setuid(lua_State *L) { + uid_t uid; + if (lua_isstring(L, 1)) { + struct passwd *p = getpwnam(lua_tostring(L, 1)); + uid = (!p) ? -1 : p->pw_uid; + } else if (lua_isnumber(L, 1)) { + uid = lua_tointeger(L, 1); + } else { + return luaL_argerror(L, 1, "supported values: , "); + } + + return nixio__pstatus(L, !setuid(uid)); +} + /* module table */ static const luaL_reg R[] = { {"fork", nixio_fork}, {"wait", nixio_wait}, {"kill", nixio_kill}, + {"getpid", nixio_getpid}, + {"getppid", nixio_getppid}, + {"getuid", nixio_getuid}, + {"getgid", nixio_getgid}, + {"setuid", nixio_setuid}, + {"setgid", nixio_setgid}, {NULL, NULL} };