Make nixio compile on OpenSolaris
[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 == -1) {
63                 return nixio__perror(L);
64         } else {
65                 lua_pushinteger(L, pidout);
66         }
67
68         if (WIFEXITED(status)) {
69                 lua_pushliteral(L, "exited");
70                 lua_pushinteger(L, WEXITSTATUS(status));
71     } else if (WIFSIGNALED(status)) {
72         lua_pushliteral(L, "signaled");
73         lua_pushinteger(L, WTERMSIG(status));
74     } else if (WIFSTOPPED(status)) {
75         lua_pushliteral(L, "stopped");
76         lua_pushinteger(L, WSTOPSIG(status));
77     } else {
78         return 1;
79     }
80
81     return 3;
82 }
83
84 static int nixio_kill(lua_State *L) {
85         return nixio__pstatus(L, !kill(luaL_checkint(L, 1), luaL_checkint(L, 2)));
86 }
87
88 static int nixio_getpid(lua_State *L) {
89         lua_pushinteger(L, getpid());
90         return 1;
91 }
92
93 static int nixio_getppid(lua_State *L) {
94         lua_pushinteger(L, getppid());
95         return 1;
96 }
97
98 static int nixio_getuid(lua_State *L) {
99         lua_pushinteger(L, getuid());
100         return 1;
101 }
102
103 static int nixio_getgid(lua_State *L) {
104         lua_pushinteger(L, getgid());
105         return 1;
106 }
107
108 static int nixio_setgid(lua_State *L) {
109         gid_t gid;
110         if (lua_isstring(L, 1)) {
111                 struct group *g = getgrnam(lua_tostring(L, 1));
112                 gid = (!g) ? -1 : g->gr_gid;
113         } else if (lua_isnumber(L, 1)) {
114                 gid = lua_tointeger(L, 1);
115         } else {
116                 return luaL_argerror(L, 1, "supported values: <groupname>, <gid>");
117         }
118
119         return nixio__pstatus(L, !setgid(gid));
120 }
121
122 static int nixio_setuid(lua_State *L) {
123         uid_t uid;
124         if (lua_isstring(L, 1)) {
125                 struct passwd *p = getpwnam(lua_tostring(L, 1));
126                 uid = (!p) ? -1 : p->pw_uid;
127         } else if (lua_isnumber(L, 1)) {
128                 uid = lua_tointeger(L, 1);
129         } else {
130                 return luaL_argerror(L, 1, "supported values: <username>, <uid>");
131         }
132
133         return nixio__pstatus(L, !setuid(uid));
134 }
135
136
137 /* module table */
138 static const luaL_reg R[] = {
139         {"fork",                nixio_fork},
140         {"wait",                nixio_wait},
141         {"kill",                nixio_kill},
142         {"getpid",              nixio_getpid},
143         {"getppid",             nixio_getppid},
144         {"getuid",              nixio_getuid},
145         {"getgid",              nixio_getgid},
146         {"setuid",              nixio_setuid},
147         {"setgid",              nixio_setgid},
148         {NULL,                  NULL}
149 };
150
151 void nixio_open_process(lua_State *L) {
152         luaL_register(L, NULL, R);
153 }