-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlcwatchman.c
186 lines (157 loc) · 3.72 KB
/
lcwatchman.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2011 Kamil Klimkiewicz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/*
* setpgid(pid, pgid)
* Set process group ID for job control.
*
* [-0, +1, v]
*/
static int l_setpgid(lua_State *L) {
pid_t pid = luaL_checkint(L, 1);
pid_t pigd = luaL_checkint(L, 2);
lua_pushinteger(L, setpgid(pid, pigd));
return 1;
}
/*
* setpgrp()
* Set the process group ID.
*
* [-0, +0, v]
*/
static int l_setpgrp(lua_State *L) {
setpgrp();
return 0;
}
/*
* chdir(path)
* Change working directory.
*
* [-0, +0, v]
*/
static int l_chdir(lua_State *L) {
chdir(luaL_checkstring(L, 1));
return 0;
}
/*
* umask(cmask)
* Set and get the file mode creation mask.
*
* [-0, +1, v]
*/
static int l_umask(lua_State *L) {
lua_pushinteger(L, umask(luaL_checkint(L, 1)));
return 1;
}
/*
* exec(path, [args])
* Execute a file.
*
* [-0, +1, v]
*/
static int l_exec(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
int i, n = lua_gettop(L);
char **argv = lua_newuserdata(L, (n+1)*sizeof(char*));
argv[0] = (char*)path;
for (i=1; i<n; i++) {
argv[i] = (char*)luaL_checkstring(L, i+1);
}
argv[n] = NULL;
lua_pushinteger(L, execv(path, argv));
return 1;
}
/*
* sleep(seconds)
* Sleep for the specified number of seconds.
*
* [-0, +1, v]
*/
static int l_sleep(lua_State *L) {
lua_pushinteger(L, sleep(luaL_checkint(L, 1)));
return 1;
}
/*
* fork()
* Create a new process.
*
* [-0, +1, v]
*/
static int l_fork(lua_State *L) {
lua_pushinteger(L, fork());
return 1;
}
/*
* vfork()
* Create a new process; share virtual memory.
*
* [-0, +1, v]
*/
static int l_vfork(lua_State *L) {
lua_pushinteger(L, vfork());
return 1;
}
/*
* kill(pid, sig)
* Send a signal to a process or a group of processes.
*
* [-0, +0, v]
*/
static int l_kill(lua_State *L) {
pid_t pid = luaL_checkint(L, 1);
int sig = luaL_checkint(L, 2);
kill(pid, sig);
return 0;
}
/*
* killpg(pgrp, sig)
* Send a signal to a process group.
*
* [-0, +0, v]
*/
static int l_killpg(lua_State *L) { /* killpg(pgrp, sig) */
pid_t pgrp = luaL_checkint(L, 1);
int sig = luaL_checkint(L, 2);
killpg(pgrp, sig);
return 0;
}
static const luaL_reg R[] = {
{"setpgid", l_setpgid},
{"setpgrp", l_setpgrp},
{"chdir", l_chdir},
{"exec", l_exec},
{"sleep", l_sleep},
{"fork", l_fork},
{"vfork", l_vfork},
{"kill", l_kill},
{"killpg", l_killpg},
{NULL, NULL},
};
LUALIB_API int luaopen_cwatchman (lua_State *L) {
luaL_register(L, "cwatchman", R);
return 1;
}