nixio: Implement protable behaviour of signal(), export more error
[project/luci.git] / libs / nixio / src / process.c
1 /*
2 * nixio - Linux I/O library for lua
3 *
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 */
18
19 #include "nixio.h"
20 #include <pwd.h>
21 #include <grp.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <sys/types.h>
27 #include <signal.h>
28
29 static int nixio_fork(lua_State *L) {
30 pid_t pid = fork();
31 if (pid == -1) {
32 return nixio__perror(L);
33 } else {
34 lua_pushinteger(L, pid);
35 return 1;
36 }
37 }
38
39 static int nixio_signal(lua_State *L) {
40 int sig = luaL_checkinteger(L, 1);
41 const char *val = luaL_checkstring(L, 2);
42
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);
47 } else {
48 return luaL_argerror(L, 2, "supported values: ign, dfl");
49 }
50 }
51
52 static int nixio_wait(lua_State *L) {
53 pid_t pidin = luaL_optinteger(L, 1, -1), pidout;
54 int options = 0, status;
55
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")) {
60 options |= WNOHANG;
61 } else if (!strcmp(flag, "untraced")) {
62 options |= WUNTRACED;
63 } else if (!strcmp(flag, "continued")) {
64 options |= WCONTINUED;
65 } else {
66 return luaL_argerror(L, i,
67 "supported values: nohang, untraced, continued");
68 }
69 }
70
71 do {
72 pidout = waitpid(pidin, &status, options);
73 } while (pidout == -1 && errno == EINTR);
74
75 if (pidout == 0) {
76 lua_pushboolean(L, 0);
77 return 1;
78 } else if (pidout == -1) {
79 return nixio__perror(L);
80 } else {
81 lua_pushinteger(L, pidout);
82 }
83
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));
93 } else {
94 return 1;
95 }
96
97 return 3;
98 }
99
100 static int nixio_kill(lua_State *L) {
101 return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2)));
102 }
103
104 static int nixio_getpid(lua_State *L) {
105 lua_pushinteger(L, getpid());
106 return 1;
107 }
108
109 static int nixio_getppid(lua_State *L) {
110 lua_pushinteger(L, getppid());
111 return 1;
112 }
113
114 static int nixio_getuid(lua_State *L) {
115 lua_pushinteger(L, getuid());
116 return 1;
117 }
118
119 static int nixio_getgid(lua_State *L) {
120 lua_pushinteger(L, getgid());
121 return 1;
122 }
123
124 static int nixio_setgid(lua_State *L) {
125 gid_t gid;
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);
131 } else {
132 return luaL_argerror(L, 1, "supported values: <groupname>, <gid>");
133 }
134
135 return nixio__pstatus(L, !setgid(gid));
136 }
137
138 static int nixio_setuid(lua_State *L) {
139 uid_t uid;
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);
145 } else {
146 return luaL_argerror(L, 1, "supported values: <username>, <uid>");
147 }
148
149 return nixio__pstatus(L, !setuid(uid));
150 }
151
152
153 /* module table */
154 static const luaL_reg R[] = {
155 {"fork", nixio_fork},
156 {"wait", nixio_wait},
157 {"kill", nixio_kill},
158 {"getpid", nixio_getpid},
159 {"getppid", nixio_getppid},
160 {"getuid", nixio_getuid},
161 {"getgid", nixio_getgid},
162 {"setuid", nixio_setuid},
163 {"setgid", nixio_setgid},
164 {"signal", nixio_signal},
165 {NULL, NULL}
166 };
167
168 void nixio_open_process(lua_State *L) {
169 luaL_register(L, NULL, R);
170 }