2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <sys/types.h>
28 static int nixio_fork(lua_State *L) {
31 return nixio__perror(L);
33 lua_pushinteger(L, pid);
38 static int nixio_wait(lua_State *L) {
39 pid_t pidin = luaL_optinteger(L, 1, -1), pidout;
40 int options = 0, status;
42 const int j = lua_gettop(L);
43 for (int i=2; i<=j; i++) {
44 const char *flag = luaL_checkstring(L, i);
45 if (!strcmp(flag, "nohang")) {
47 } else if (!strcmp(flag, "untraced")) {
49 } else if (!strcmp(flag, "continued")) {
50 options |= WCONTINUED;
52 return luaL_argerror(L, i,
53 "supported values: nohang, untraced, continued");
58 pidout = waitpid(pidin, &status, options);
59 } while (pidout == -1 && errno == EINTR);
62 return nixio__perror(L);
64 lua_pushinteger(L, pidout);
67 if (WIFEXITED(status)) {
68 lua_pushliteral(L, "exited");
69 lua_pushinteger(L, WEXITSTATUS(status));
70 } else if (WIFSIGNALED(status)) {
71 lua_pushliteral(L, "signaled");
72 lua_pushinteger(L, WTERMSIG(status));
73 } else if (WIFSTOPPED(status)) {
74 lua_pushliteral(L, "stopped");
75 lua_pushinteger(L, WSTOPSIG(status));
83 static int nixio_kill(lua_State *L) {
84 return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2)));
87 static int nixio_getpid(lua_State *L) {
88 lua_pushinteger(L, getpid());
92 static int nixio_getppid(lua_State *L) {
93 lua_pushinteger(L, getppid());
97 static int nixio_getuid(lua_State *L) {
98 lua_pushinteger(L, getuid());
102 static int nixio_getgid(lua_State *L) {
103 lua_pushinteger(L, getgid());
107 static int nixio_setgid(lua_State *L) {
109 if (lua_isstring(L, 1)) {
110 struct group *g = getgrnam(lua_tostring(L, 1));
111 gid = (!g) ? -1 : g->gr_gid;
112 } else if (lua_isnumber(L, 1)) {
113 gid = lua_tointeger(L, 1);
115 return luaL_argerror(L, 1, "supported values: <groupname>, <gid>");
118 return nixio__pstatus(L, !setgid(gid));
121 static int nixio_setuid(lua_State *L) {
123 if (lua_isstring(L, 1)) {
124 struct passwd *p = getpwnam(lua_tostring(L, 1));
125 uid = (!p) ? -1 : p->pw_uid;
126 } else if (lua_isnumber(L, 1)) {
127 uid = lua_tointeger(L, 1);
129 return luaL_argerror(L, 1, "supported values: <username>, <uid>");
132 return nixio__pstatus(L, !setuid(uid));
137 static const luaL_reg R[] = {
138 {"fork", nixio_fork},
139 {"wait", nixio_wait},
140 {"kill", nixio_kill},
141 {"getpid", nixio_getpid},
142 {"getppid", nixio_getppid},
143 {"getuid", nixio_getuid},
144 {"getgid", nixio_getgid},
145 {"setuid", nixio_setuid},
146 {"setgid", nixio_setgid},
150 void nixio_open_process(lua_State *L) {
151 luaL_register(L, NULL, R);