-
Notifications
You must be signed in to change notification settings - Fork 4
/
jim-posix.c
378 lines (346 loc) · 9.49 KB
/
jim-posix.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Jim - POSIX extension
* Copyright 2005 Salvatore Sanfilippo <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* A copy of the license is also included in the source distribution
* of Jim, as a TXT file name called LICENSE.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#define JIM_EXTENSION
#include "jim.h"
static void Jim_PosixSetError(Jim_Interp *interp)
{
Jim_SetResultString(interp, strerror(errno), -1);
}
static int Jim_PosixForkCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
pid_t pid;
JIM_NOTUSED(argv);
if (argc != 1) {
Jim_WrongNumArgs(interp, 1, argv, "");
return JIM_ERR;
}
if ((pid = fork()) == -1) {
Jim_SetResultString(interp, strerror(errno), -1);
return JIM_ERR;
}
Jim_SetResult(interp, Jim_NewIntObj(interp, (jim_wide)pid));
return JIM_OK;
}
static int Jim_PosixSleepCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
long longValue;
if (argc != 2) {
Jim_WrongNumArgs(interp, 1, argv, "?seconds?");
return JIM_ERR;
}
if (Jim_GetLong(interp, argv[1], &longValue) != JIM_OK)
return JIM_ERR;
sleep(longValue);
return JIM_OK;
}
static int Jim_PosixGetidsCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
Jim_Obj *objv[8];
if (argc != 1) {
Jim_WrongNumArgs(interp, 1, argv, "");
return JIM_ERR;
}
objv[0] = Jim_NewStringObj(interp, "uid", -1);
objv[1] = Jim_NewIntObj(interp, getuid());
objv[2] = Jim_NewStringObj(interp, "euid", -1);
objv[3] = Jim_NewIntObj(interp, geteuid());
objv[4] = Jim_NewStringObj(interp, "gid", -1);
objv[5] = Jim_NewIntObj(interp, getgid());
objv[6] = Jim_NewStringObj(interp, "egid", -1);
objv[7] = Jim_NewIntObj(interp, getegid());
Jim_SetResult(interp, Jim_NewListObj(interp, objv, 8));
return JIM_OK;
}
#define JIM_HOST_NAME_MAX 1024
static int Jim_PosixGethostnameCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
char buf[JIM_HOST_NAME_MAX];
if (argc != 1) {
Jim_WrongNumArgs(interp, 1, argv, "");
return JIM_ERR;
}
if (gethostname(buf, JIM_HOST_NAME_MAX) == -1) {
Jim_PosixSetError(interp);
return JIM_ERR;
}
Jim_SetResultString(interp, buf, -1);
return JIM_OK;
}
static int Jim_PosixSethostnameCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
const char *hostname;
int len;
if (argc != 2) {
Jim_WrongNumArgs(interp, 1, argv, "hostname");
return JIM_ERR;
}
hostname = Jim_GetString(argv[1], &len);
if (sethostname(hostname, len) == -1) {
Jim_PosixSetError(interp);
return JIM_ERR;
}
return JIM_OK;
}
// added Commands, some linux specific
// uses POSIX.1b
#define S_NS 1000000000
#define S_US 1000000
#define MSNS 1000000
#define USNS 1000
#define NSNS 1
static int Jim_LinuxUSleepCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
int mult = NSNS ;
double floatValue ;
const char *units ;
long long delay ;
int len ;
struct timespec tv;
switch (argc) {
case 3:
units = Jim_GetString(argv[2], &len);
if (units == NULL)
return JIM_ERR;
switch (*units) {
case 's': mult = S_NS; break;
case 'm': mult = MSNS; break;
case 'u': mult = USNS; break;
case 'n': mult = NSNS; break;
default:
Jim_WrongNumArgs(interp, 1, argv, "arg3: ms us ns or empty");
return JIM_ERR;
}
// fallthrough
case 2: if (Jim_GetDouble(interp, argv[1], &floatValue) != JIM_OK)
return JIM_ERR;
delay = (long long)(floatValue * mult);
break;
default:
Jim_WrongNumArgs(interp, 1, argv, "?useconds?");
return JIM_ERR;
}
tv.tv_sec = delay / S_NS;
tv.tv_nsec = delay % S_NS;
fprintf(stderr,"delay: %lld mult: %d sec: %ld ns: %ld\n", delay, mult, tv.tv_sec,tv.tv_nsec );
nanosleep(&tv,NULL);
return JIM_OK;
}
static int Jim_PointInTimeCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
struct timezone tz = { 0, 0 };
double pit ;
struct timeval tv;
gettimeofday(&tv,&tz);
pit = (double)tv.tv_usec;
pit /= (double)S_US;
pit +=(double)tv.tv_sec;
Jim_SetResult(interp, Jim_NewDoubleObj(interp, pit));
return JIM_OK;
}
static int Jim_PointInTimeJulianCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
struct timezone tz = { 0, 0 };
double pit ;
struct timeval tv;
gettimeofday(&tv,&tz);
pit = (double)tv.tv_usec;
pit /= (double)S_US;
pit += (double)tv.tv_sec;
pit /= (double)( 60 * 60 * 24 );
pit += 2440587.5;
Jim_SetResult(interp, Jim_NewDoubleObj(interp, pit));
return JIM_OK;
}
// signal stuff
// signal <signame>
static const char *signames[] = {
"SIGHUP", "SIGINT", "SIGQUIT", "SIGILL",
"SIGABRT", "SIGFPE", "SIGKILL", "SIGSEGV",
"SIGPIPE", "SIGALRM", "SIGTERM",
"SIGUSR1", "SIGUSR2",
"SIGCHLD", "SIGCONT", "SIGSTOP",
"SIGTSTP", "SIGTTIN" "SIGTTOU",
"SIGBUS", "SIGPOLL", "SIGPROF", "SIGSYS",
"SIGTRAP", "SIGURG", "SIGVTALRM", "SIGXCPU",
"SIGXFSZ",
"SIGIOT",
#ifdef SIGEMT
"SIGEMT",
#endif
"SIGSTKFLT", "SIGIO",
"SIGCLD", "SIGPWR",
#ifdef SIGINFO
"SIGINFO",
#endif
#ifdef SIGLOST
"SIGLOST",
#endif
"SIGWINCH", "SIGUNUSED",
"SIGRT32", "SIGRT33", "SIGRT34", "SIGRT35",
"SIGRT36", "SIGRT37", "SIGRT38", "SIGRT39",
NULL
} ;
static int signums[] = {
SIGHUP, SIGINT, SIGQUIT, SIGILL,
SIGABRT, SIGFPE, SIGKILL, SIGSEGV,
SIGPIPE, SIGALRM, SIGTERM,
SIGUSR1, SIGUSR2,
SIGCHLD, SIGCONT, SIGSTOP,
SIGTSTP, SIGTTIN, SIGTTOU,
SIGBUS, SIGPOLL, SIGPROF, SIGSYS,
SIGTRAP, SIGURG, SIGVTALRM, SIGXCPU,
SIGXFSZ,
SIGIOT,
#ifdef SIGEMT
SIGEMT,
#endif
SIGSTKFLT, SIGIO,
SIGCLD, SIGPWR,
#ifdef SIGINFO
SIGINFO,
#endif
#ifdef SIGLOST
SIGLOST,
#endif
SIGWINCH, SIGUNUSED,
#ifdef SIGRT
SIGRT32, SIGRT33, SIGRT34, SIGRT35,
SIGRT36, SIGRT37, SIGRT38, SIGRT39,
#endif
0
} ;
enum {
HUP, INT, QUIT, ILL,
ABRT, FPE, KILL, SEGV,
PIPE, ALRM, TERM,
USR1, USR2,
CHLD, CONT, STOP,
TSTP, TTIN, TTOU,
BUS, POLL, PROF, SYS,
TRAP, URG, VTALRM, XCPU,
XFSZ,
IOT,
#ifdef SIGEMT
EMT,
#endif
STKFLT, IO,
CLD, PWR,
#ifdef SIGINFO
INFO,
#endif
#ifdef SIGLOST
LOST,
#endif
WINCH, UNUSED,
#ifdef SIGRT
RT32, RT33, RT34, RT35,
RT36, RT37, RT38, RT39,
#endif
ISEND
} ;
static void Jim_Posix_SigHandler(int signal)
{
int i;
for (i=0; ((i<ISEND) && (signums[i] != signal));i++) ;
fprintf(stderr,"signal %d %s\n", signal,signames[i]);
}
typedef void (*sighandler_t)(int);
static int Jim_PosixSignalCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
int sig;
int signum;
sighandler_t lastaction;
sighandler_t nextaction = SIG_DFL;
const char *op;
int strlen = 0;
if (argc < 2) {
Jim_WrongNumArgs(interp, 1, argv, "signame ?action ...?");
return JIM_ERR;
}
if (Jim_GetEnum(interp, argv[1], signames, &sig, "Signal Names",
JIM_ERRMSG) != JIM_OK)
return JIM_ERR;
signum = signums[sig];
switch (argc) {
case 3:
if (op = Jim_GetString(argv[2], &strlen),strlen == 0) {
return JIM_ERR;
}
if (strcmp("default",op) == 0) {
nextaction = SIG_DFL;
} else if (strcmp("ignore",op) == 0) {
nextaction = SIG_IGN;
} else if (strcmp("debug",op) == 0) {
nextaction = Jim_Posix_SigHandler;
} else {
// this is the place to insert a script! UK
}
// fall through to query case:
case 2:
lastaction = signal(signum, nextaction);
if (argc==2)
signal(signum, lastaction);
if (lastaction == SIG_ERR) {
return JIM_ERR;
}
if (lastaction == SIG_DFL) {
Jim_SetResultString(interp, "default", -1);
return JIM_OK;
}
if (lastaction == SIG_IGN) {
Jim_SetResultString(interp, "ignore", -1);
return JIM_OK;
}
Jim_SetResultString(interp, "function", -1);
return JIM_OK;
}
return JIM_OK;
}
// end added
int Jim_OnLoad(Jim_Interp *interp)
{
Jim_InitExtension(interp);
if (Jim_PackageProvide(interp, "posix", "1.0", JIM_ERRMSG) != JIM_OK)
return JIM_ERR;
Jim_CreateCommand(interp, "os.fork", Jim_PosixForkCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.sleep", Jim_PosixSleepCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.getids", Jim_PosixGetidsCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.gethostname", Jim_PosixGethostnameCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.sethostname", Jim_PosixSethostnameCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.usleep", Jim_LinuxUSleepCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.signal", Jim_PosixSignalCommand, NULL, NULL);
Jim_CreateCommand(interp, "pit", Jim_PointInTimeCommand, NULL, NULL);
Jim_CreateCommand(interp, "Jpit", Jim_PointInTimeJulianCommand, NULL, NULL);
return JIM_OK;
}