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.
25 static int nixio_fork(lua_State *L) {
28 return nixio__perror(L);
30 lua_pushinteger(L, pid);
35 static int nixio_wait(lua_State *L) {
36 pid_t pidin = luaL_optinteger(L, 1, -1), pidout;
37 int options = 0, status;
39 const int j = lua_gettop(L);
40 for (int i=2; i<=j; i++) {
41 const char *flag = luaL_checkstring(L, i);
42 if (!strcmp(flag, "nohang")) {
44 } else if (!strcmp(flag, "untraced")) {
46 } else if (!strcmp(flag, "continued")) {
47 options |= WCONTINUED;
49 return luaL_argerror(L, i,
50 "supported values: nohang, untraced, continued");
55 pidout = waitpid(pidin, &status, options);
56 } while (pidout == -1 && errno == EINTR);
59 return nixio__perror(L);
61 lua_pushinteger(L, pidout);
64 if (WIFEXITED(status)) {
65 lua_pushliteral(L, "exited");
66 lua_pushinteger(L, WEXITSTATUS(status));
67 } else if (WIFSIGNALED(status)) {
68 lua_pushliteral(L, "signaled");
69 lua_pushinteger(L, WTERMSIG(status));
70 } else if (WIFSTOPPED(status)) {
71 lua_pushliteral(L, "stopped");
72 lua_pushinteger(L, WSTOPSIG(status));
80 static int nixio_kill(lua_State *L) {
81 return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2)));
86 static const luaL_reg R[] = {
93 void nixio_open_process(lua_State *L) {
94 luaL_register(L, NULL, R);