httpclient: Added missing import
[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_wait(lua_State *L) {
40         pid_t pidin = luaL_optinteger(L, 1, -1), pidout;
41         int options = 0, status;
42
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")) {
47                         options |= WNOHANG;
48                 } else if (!strcmp(flag, "untraced")) {
49                         options |= WUNTRACED;
50                 } else if (!strcmp(flag, "continued")) {
51                         options |= WCONTINUED;
52                 } else {
53                         return luaL_argerror(L, i,
54                                         "supported values: nohang, untraced, continued");
55                 }
56         }
57
58         do {
59                 pidout = waitpid(pidin, &status, options);
60         } while (pidout == -1 && errno == EINTR);
61
62         if (pidout == 0) {
63                 lua_pushboolean(L, 0);
64                 return 1;
65         } else if (pidout == -1) {
66                 return nixio__perror(L);
67         } else {
68                 lua_pushinteger(L, pidout);
69         }
70
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));
80     } else {
81         return 1;
82     }
83
84     return 3;
85 }
86
87 static int nixio_kill(lua_State *L) {
88         return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2)));
89 }
90
91 static int nixio_getpid(lua_State *L) {
92         lua_pushinteger(L, getpid());
93         return 1;
94 }
95
96 static int nixio_getppid(lua_State *L) {
97         lua_pushinteger(L, getppid());
98         return 1;
99 }
100
101 static int nixio_getuid(lua_State *L) {
102         lua_pushinteger(L, getuid());
103         return 1;
104 }
105
106 static int nixio_getgid(lua_State *L) {
107         lua_pushinteger(L, getgid());
108         return 1;
109 }
110
111 static int nixio_setgid(lua_State *L) {
112         gid_t gid;
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);
118         } else {
119                 return luaL_argerror(L, 1, "supported values: <groupname>, <gid>");
120         }
121
122         return nixio__pstatus(L, !setgid(gid));
123 }
124
125 static int nixio_setuid(lua_State *L) {
126         uid_t uid;
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);
132         } else {
133                 return luaL_argerror(L, 1, "supported values: <username>, <uid>");
134         }
135
136         return nixio__pstatus(L, !setuid(uid));
137 }
138
139
140 /* module table */
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},
151         {NULL,                  NULL}
152 };
153
154 void nixio_open_process(lua_State *L) {
155         luaL_register(L, NULL, R);
156 }