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>
29 static int nixio_fork(lua_State *L) {
32 return nixio__perror(L);
34 lua_pushinteger(L, pid);
39 static int nixio_wait(lua_State *L) {
40 pid_t pidin = luaL_optinteger(L, 1, -1), pidout;
41 int options = 0, status;
43 const int j = lua_gettop(L);
44 for (int i=2; i<=j; i++) {
45 const char *flag = luaL_checkstring(L, i);
46 if (!strcmp(flag, "nohang")) {
48 } else if (!strcmp(flag, "untraced")) {
50 } else if (!strcmp(flag, "continued")) {
51 options |= WCONTINUED;
53 return luaL_argerror(L, i,
54 "supported values: nohang, untraced, continued");
59 pidout = waitpid(pidin, &status, options);
60 } while (pidout == -1 && errno == EINTR);
63 lua_pushboolean(L, 0);
65 } else if (pidout == -1) {
66 return nixio__perror(L);
68 lua_pushinteger(L, pidout);
71 if (WIFEXITED(status)) {
72 lua_pushliteral(L, "exited");
73 lua_pushinteger(L, WEXITSTATUS(status));
74 } else if (WIFSIGNALED(status)) {
75 lua_pushliteral(L, "signaled");
76 lua_pushinteger(L, WTERMSIG(status));
77 } else if (WIFSTOPPED(status)) {
78 lua_pushliteral(L, "stopped");
79 lua_pushinteger(L, WSTOPSIG(status));
87 static int nixio_kill(lua_State *L) {
88 return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2)));
91 static int nixio_getpid(lua_State *L) {
92 lua_pushinteger(L, getpid());
96 static int nixio_getppid(lua_State *L) {
97 lua_pushinteger(L, getppid());
101 static int nixio_getuid(lua_State *L) {
102 lua_pushinteger(L, getuid());
106 static int nixio_getgid(lua_State *L) {
107 lua_pushinteger(L, getgid());
111 static int nixio_setgid(lua_State *L) {
113 if (lua_isstring(L, 1)) {
114 struct group *g = getgrnam(lua_tostring(L, 1));
115 gid = (!g) ? -1 : g->gr_gid;
116 } else if (lua_isnumber(L, 1)) {
117 gid = lua_tointeger(L, 1);
119 return luaL_argerror(L, 1, "supported values: <groupname>, <gid>");
122 return nixio__pstatus(L, !setgid(gid));
125 static int nixio_setuid(lua_State *L) {
127 if (lua_isstring(L, 1)) {
128 struct passwd *p = getpwnam(lua_tostring(L, 1));
129 uid = (!p) ? -1 : p->pw_uid;
130 } else if (lua_isnumber(L, 1)) {
131 uid = lua_tointeger(L, 1);
133 return luaL_argerror(L, 1, "supported values: <username>, <uid>");
136 return nixio__pstatus(L, !setuid(uid));
141 static const luaL_reg R[] = {
142 {"fork", nixio_fork},
143 {"wait", nixio_wait},
144 {"kill", nixio_kill},
145 {"getpid", nixio_getpid},
146 {"getppid", nixio_getppid},
147 {"getuid", nixio_getuid},
148 {"getgid", nixio_getgid},
149 {"setuid", nixio_setuid},
150 {"setgid", nixio_setgid},
154 void nixio_open_process(lua_State *L) {
155 luaL_register(L, NULL, R);