2 /* Jim - POSIX extension
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * A copy of the license is also included in the source distribution
12 * of Jim, as a TXT file name called LICENSE.
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #include <sys/types.h>
30 #include "jimautoconf.h"
32 #ifdef HAVE_SYS_SYSINFO_H
33 #include <sys/sysinfo.h>
36 static void Jim_PosixSetError(Jim_Interp
*interp
)
38 Jim_SetResultString(interp
, strerror(errno
), -1);
41 #if defined(HAVE_FORK)
42 static int Jim_PosixForkCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
49 Jim_WrongNumArgs(interp
, 1, argv
, "");
52 if ((pid
= fork()) == -1) {
53 Jim_PosixSetError(interp
);
56 Jim_SetResultInt(interp
, (jim_wide
) pid
);
62 * os.wait ?-nohang? pid
64 * An interface to waitpid(2)
66 * Returns a 3 element list.
68 * If -nohang is specified, and the process is still alive, returns
72 * If the process does not exist or has already been waited for, returns:
74 * {-1 error <error-description>}
76 * If the process exited normally, returns:
78 * {<pid> exit <exit-status>}
80 * If the process terminated on a signal, returns:
82 * {<pid> signal <signal-number>}
84 * Otherwise (core dump, stopped, continued, ...), returns:
88 static int Jim_PosixWaitCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
97 if (argc
> 1 && Jim_CompareStringImmediate(interp
, argv
[1], "-nohang")) {
100 if (argc
!= nohang
+ 2) {
101 Jim_WrongNumArgs(interp
, 1, argv
, "?-nohang? pid");
104 if (Jim_GetLong(interp
, argv
[nohang
+ 1], &pid
) != JIM_OK
) {
108 pid
= waitpid(pid
, &status
, nohang
? WNOHANG
: 0);
109 listObj
= Jim_NewListObj(interp
, NULL
, 0);
110 Jim_ListAppendElement(interp
, listObj
, Jim_NewIntObj(interp
, pid
));
119 else if (WIFEXITED(status
)) {
121 value
= WEXITSTATUS(status
);
123 else if (WIFSIGNALED(status
)) {
125 value
= WTERMSIG(status
);
132 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, type
, -1));
134 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, strerror(value
), -1));
137 Jim_ListAppendElement(interp
, listObj
, Jim_NewIntObj(interp
, value
));
139 Jim_SetResult(interp
, listObj
);
143 static int Jim_PosixGetidsCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
148 Jim_WrongNumArgs(interp
, 1, argv
, "");
151 objv
[0] = Jim_NewStringObj(interp
, "uid", -1);
152 objv
[1] = Jim_NewIntObj(interp
, getuid());
153 objv
[2] = Jim_NewStringObj(interp
, "euid", -1);
154 objv
[3] = Jim_NewIntObj(interp
, geteuid());
155 objv
[4] = Jim_NewStringObj(interp
, "gid", -1);
156 objv
[5] = Jim_NewIntObj(interp
, getgid());
157 objv
[6] = Jim_NewStringObj(interp
, "egid", -1);
158 objv
[7] = Jim_NewIntObj(interp
, getegid());
159 Jim_SetResult(interp
, Jim_NewListObj(interp
, objv
, 8));
163 #define JIM_HOST_NAME_MAX 1024
164 static int Jim_PosixGethostnameCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
170 Jim_WrongNumArgs(interp
, 1, argv
, "");
173 buf
= Jim_Alloc(JIM_HOST_NAME_MAX
);
174 if (gethostname(buf
, JIM_HOST_NAME_MAX
) == -1) {
175 Jim_PosixSetError(interp
);
179 Jim_SetResult(interp
, Jim_NewStringObjNoAlloc(interp
, buf
, -1));
184 static int Jim_PosixUptimeCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
186 #ifdef HAVE_STRUCT_SYSINFO_UPTIME
190 Jim_WrongNumArgs(interp
, 1, argv
, "");
194 if (sysinfo(&info
) == -1) {
195 Jim_PosixSetError(interp
);
199 Jim_SetResultInt(interp
, info
.uptime
);
201 Jim_SetResultInt(interp
, (long)time(NULL
));
206 static int Jim_PosixPidCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
209 Jim_WrongNumArgs(interp
, 1, argv
, "");
213 Jim_SetResultInt(interp
, getpid());
217 int Jim_posixInit(Jim_Interp
*interp
)
219 if (Jim_PackageProvide(interp
, "posix", "1.0", JIM_ERRMSG
))
223 Jim_CreateCommand(interp
, "os.fork", Jim_PosixForkCommand
, NULL
, NULL
);
225 Jim_CreateCommand(interp
, "os.wait", Jim_PosixWaitCommand
, NULL
, NULL
);
226 Jim_CreateCommand(interp
, "os.getids", Jim_PosixGetidsCommand
, NULL
, NULL
);
227 Jim_CreateCommand(interp
, "os.gethostname", Jim_PosixGethostnameCommand
, NULL
, NULL
);
228 Jim_CreateCommand(interp
, "os.uptime", Jim_PosixUptimeCommand
, NULL
, NULL
);
229 Jim_CreateCommand(interp
, "pid", Jim_PosixPidCommand
, NULL
, NULL
);