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_signal(lua_State *L) {
40 int sig = luaL_checkinteger(L, 1);
41 const char *val = luaL_checkstring(L, 2);
43 if (!strcmp(val, "ign") || !strcmp(val, "ignore")) {
44 return nixio__pstatus(L, signal(sig, SIG_IGN) != SIG_ERR);
45 } else if (!strcmp(val, "dfl") || !strcmp(val, "default")) {
46 return nixio__pstatus(L, signal(sig, SIG_DFL) != SIG_ERR);
48 return luaL_argerror(L, 2, "supported values: ign, dfl");
52 static int nixio_wait(lua_State *L) {
53 pid_t pidin = luaL_optinteger(L, 1, -1), pidout;
54 int options = 0, status;
56 const int j = lua_gettop(L);
57 for (int i=2; i<=j; i++) {
58 const char *flag = luaL_checkstring(L, i);
59 if (!strcmp(flag, "nohang")) {
61 } else if (!strcmp(flag, "untraced")) {
63 } else if (!strcmp(flag, "continued")) {
64 options |= WCONTINUED;
66 return luaL_argerror(L, i,
67 "supported values: nohang, untraced, continued");
72 pidout = waitpid(pidin, &status, options);
73 } while (pidout == -1 && errno == EINTR);
76 lua_pushboolean(L, 0);
78 } else if (pidout == -1) {
79 return nixio__perror(L);
81 lua_pushinteger(L, pidout);
84 if (WIFEXITED(status)) {
85 lua_pushliteral(L, "exited");
86 lua_pushinteger(L, WEXITSTATUS(status));
87 } else if (WIFSIGNALED(status)) {
88 lua_pushliteral(L, "signaled");
89 lua_pushinteger(L, WTERMSIG(status));
90 } else if (WIFSTOPPED(status)) {
91 lua_pushliteral(L, "stopped");
92 lua_pushinteger(L, WSTOPSIG(status));
100 static int nixio_kill(lua_State *L) {
101 return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2)));
104 static int nixio_getpid(lua_State *L) {
105 lua_pushinteger(L, getpid());
109 static int nixio_getppid(lua_State *L) {
110 lua_pushinteger(L, getppid());
114 static int nixio_getuid(lua_State *L) {
115 lua_pushinteger(L, getuid());
119 static int nixio_getgid(lua_State *L) {
120 lua_pushinteger(L, getgid());
124 static int nixio_setgid(lua_State *L) {
126 if (lua_isstring(L, 1)) {
127 struct group *g = getgrnam(lua_tostring(L, 1));
128 gid = (!g) ? -1 : g->gr_gid;
129 } else if (lua_isnumber(L, 1)) {
130 gid = lua_tointeger(L, 1);
132 return luaL_argerror(L, 1, "supported values: <groupname>, <gid>");
135 return nixio__pstatus(L, !setgid(gid));
138 static int nixio_setuid(lua_State *L) {
140 if (lua_isstring(L, 1)) {
141 struct passwd *p = getpwnam(lua_tostring(L, 1));
142 uid = (!p) ? -1 : p->pw_uid;
143 } else if (lua_isnumber(L, 1)) {
144 uid = lua_tointeger(L, 1);
146 return luaL_argerror(L, 1, "supported values: <username>, <uid>");
149 return nixio__pstatus(L, !setuid(uid));
152 static int nixio_nice(lua_State *L) {
153 int nval = luaL_checkint(L, 1);
158 if (nval == -1 && errno) {
159 return nixio__perror(L);
161 lua_pushinteger(L, nval);
168 static const luaL_reg R[] = {
169 {"fork", nixio_fork},
170 {"wait", nixio_wait},
171 {"kill", nixio_kill},
172 {"nice", nixio_nice},
173 {"getpid", nixio_getpid},
174 {"getppid", nixio_getppid},
175 {"getuid", nixio_getuid},
176 {"getgid", nixio_getgid},
177 {"setuid", nixio_setuid},
178 {"setgid", nixio_setgid},
179 {"signal", nixio_signal},
183 void nixio_open_process(lua_State *L) {
184 luaL_register(L, NULL, R);