2 Unix SMB/CIFS implementation.
4 provide access to system functions
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "system/time.h"
27 #include "system/network.h"
28 #include "lib/socket/netif.h"
31 return the list of configured network interfaces
33 static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv)
36 struct MprVar ret = mprArray("interfaces");
37 struct interface *ifaces;
39 load_interfaces(NULL, lp_interfaces(global_loadparm), &ifaces);
41 count = iface_count(ifaces);
42 for (i=0;i<count;i++) {
43 mprAddArray(&ret, i, mprString(iface_n_ip(ifaces, i)));
52 return the hostname from gethostname()
54 static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
57 if (gethostname(name, sizeof(name)-1) == -1) {
58 ejsSetErrorMsg(eid, "gethostname failed - %s", strerror(errno));
61 mpr_Return(eid, mprString(name));
67 return current time as seconds and microseconds
69 static int ejs_sys_gettimeofday(MprVarHandle eid, int argc, struct MprVar **argv)
71 struct timeval tv = timeval_current();
72 struct MprVar v = mprObject("timeval");
73 struct MprVar sec = mprCreateIntegerVar(tv.tv_sec);
74 struct MprVar usec = mprCreateIntegerVar(tv.tv_usec);
76 mprCreateProperty(&v, "sec", &sec);
77 mprCreateProperty(&v, "usec", &usec);
83 return current time as a 64 bit nttime value
85 static int ejs_sys_nttime(MprVarHandle eid, int argc, struct MprVar **argv)
87 struct timeval tv = timeval_current();
88 struct MprVar v = mprCreateNumberVar(timeval_to_nttime(&tv));
94 return time as a 64 bit nttime value from a 32 bit time_t value
96 static int ejs_sys_unix2nttime(MprVarHandle eid, int argc, struct MprVar **argv)
100 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
101 ejsSetErrorMsg(eid, "sys_unix2nttime invalid arguments");
104 unix_to_nt_time(&nt, mprVarToNumber(argv[0]));
105 v = mprCreateNumberVar(nt);
111 return the GMT time represented by the struct tm argument, as a time_t value
113 static int ejs_sys_gmmktime(MprVarHandle eid, int argc, struct MprVar **argv)
117 if (argc != 1 || !mprVarIsObject(argv[0]->type)) {
118 ejsSetErrorMsg(eid, "sys_gmmktime invalid arguments");
123 #define TM_EL(n) tm.n = mprVarToNumber(mprGetProperty(o, #n, NULL))
135 mpr_Return(eid, mprCreateIntegerVar(mktime(&tm)));
140 return the given time as a gmtime structure
142 static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
147 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
148 ejsSetErrorMsg(eid, "sys_gmtime invalid arguments");
151 t = (time_t) mprVarToNumber(argv[0]);
154 mpr_Return(eid, mprCreateUndefinedVar());
157 ret = mprObject("gmtime");
158 #define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
170 mpr_Return(eid, ret);
175 return the given NT time as a time_t value
177 static int ejs_sys_nttime2unix(MprVarHandle eid, int argc, struct MprVar **argv)
181 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
182 ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments");
185 t = nt_time_to_unix(mprVarToNumber(argv[0]));
186 v = mprCreateNumberVar(t);
192 return the given NT time as a gmtime structure
194 static int ejs_sys_ntgmtime(MprVarHandle eid, int argc, struct MprVar **argv)
199 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
200 ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments");
203 t = nt_time_to_unix(mprVarToNumber(argv[0]));
206 mpr_Return(eid, mprCreateUndefinedVar());
209 ret = mprObject("gmtime");
210 #define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
222 mpr_Return(eid, ret);
227 return a ldap time string from a nttime
229 static int ejs_sys_ldaptime(MprVarHandle eid, int argc, struct MprVar **argv)
233 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
234 ejsSetErrorMsg(eid, "sys_ldaptime invalid arguments");
237 t = nt_time_to_unix(mprVarToNumber(argv[0]));
238 s = ldb_timestring(mprMemCtx(), t);
239 mpr_Return(eid, mprString(s));
245 return a http time string from a nttime
247 static int ejs_sys_httptime(MprVarHandle eid, int argc, struct MprVar **argv)
251 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
252 ejsSetErrorMsg(eid, "sys_httptime invalid arguments");
255 t = nt_time_to_unix(mprVarToNumber(argv[0]));
256 s = http_timestring(mprMemCtx(), t);
257 mpr_Return(eid, mprString(s));
264 ok = sys.unlink(fname);
266 static int ejs_sys_unlink(MprVarHandle eid, int argc, char **argv)
270 ejsSetErrorMsg(eid, "sys_unlink invalid arguments");
273 ret = unlink(argv[0]);
274 mpr_Return(eid, mprCreateBoolVar(ret == 0));
279 load a file as a string
281 string = sys.file_load(filename);
283 static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
287 ejsSetErrorMsg(eid, "sys_file_load invalid arguments");
291 s = file_load(argv[0], NULL, mprMemCtx());
292 mpr_Return(eid, mprString(s));
298 save a file from a string
300 ok = sys.file_save(filename, str);
302 static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
306 ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
309 ret = file_save(argv[0], argv[1], strlen(argv[1]));
310 mpr_Return(eid, mprCreateBoolVar(ret));
317 ok = sys.mkdir(dirname, mode);
319 static int ejs_sys_mkdir(MprVarHandle eid, int argc, struct MprVar **argv)
324 ejsSetErrorMsg(eid, "sys_mkdir invalid arguments, need mkdir(dirname, mode)");
327 if (!mprVarIsString(argv[0]->type)) {
328 ejsSetErrorMsg(eid, "sys_mkdir dirname not a string");
331 if (!mprVarIsNumber(argv[1]->type)) {
332 ejsSetErrorMsg(eid, "sys_mkdir mode not a number");
335 mprVarToString(&name, 0, NULL, argv[0]);
336 ret = mkdir(name, mprVarToNumber(argv[1]));
337 mpr_Return(eid, mprCreateBoolVar(ret == 0));
343 return fields of a stat() call
345 static struct MprVar mpr_stat(struct stat *st)
348 ret = mprObject("stat");
350 #define ST_EL(n) mprSetVar(&ret, #n, mprCreateNumberVar(st->n))
370 var st = sys.stat(filename);
371 returns an object containing struct stat elements
373 static int ejs_sys_stat(MprVarHandle eid, int argc, char **argv)
376 /* validate arguments */
378 ejsSetErrorMsg(eid, "sys.stat invalid arguments");
381 if (stat(argv[0], &st) != 0) {
382 mpr_Return(eid, mprCreateUndefinedVar());
384 mpr_Return(eid, mpr_stat(&st));
391 var st = sys.lstat(filename);
392 returns an object containing struct stat elements
394 static int ejs_sys_lstat(MprVarHandle eid, int argc, char **argv)
397 /* validate arguments */
399 ejsSetErrorMsg(eid, "sys.stat invalid arguments");
402 if (lstat(argv[0], &st) != 0) {
403 mpr_Return(eid, mprCreateUndefinedVar());
405 mpr_Return(eid, mpr_stat(&st));
413 var z = sys.bitAND(x, 0x70);
415 static int ejs_sys_bitAND(MprVarHandle eid, int argc, struct MprVar **argv)
420 !mprVarIsNumber(argv[0]->type) ||
421 !mprVarIsNumber(argv[1]->type)) {
422 ejsSetErrorMsg(eid, "bitand invalid arguments");
425 x = mprToInt(argv[0]);
426 y = mprToInt(argv[1]);
429 mpr_Return(eid, mprCreateIntegerVar(z));
436 var z = sys.bitOR(x, 0x70);
438 static int ejs_sys_bitOR(MprVarHandle eid, int argc, struct MprVar **argv)
443 !mprVarIsNumber(argv[0]->type) ||
444 !mprVarIsNumber(argv[1]->type)) {
445 ejsSetErrorMsg(eid, "bitand invalid arguments");
448 x = mprToInt(argv[0]);
449 y = mprToInt(argv[1]);
452 mpr_Return(eid, mprCreateIntegerVar(z));
457 initialise sys ejs subsystem
459 static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
461 struct MprVar *obj = mprInitObject(eid, "sys", argc, argv);
463 mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
464 mprSetCFunction(obj, "hostname", ejs_sys_hostname);
465 mprSetCFunction(obj, "nttime", ejs_sys_nttime);
466 mprSetCFunction(obj, "gettimeofday", ejs_sys_gettimeofday);
467 mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime);
468 mprSetCFunction(obj, "gmmktime", ejs_sys_gmmktime);
469 mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
470 mprSetCFunction(obj, "nttime2unix", ejs_sys_nttime2unix);
471 mprSetCFunction(obj, "ntgmtime", ejs_sys_ntgmtime);
472 mprSetCFunction(obj, "ldaptime", ejs_sys_ldaptime);
473 mprSetCFunction(obj, "httptime", ejs_sys_httptime);
474 mprSetCFunction(obj, "mkdir", ejs_sys_mkdir);
475 mprSetStringCFunction(obj, "unlink", ejs_sys_unlink);
476 mprSetStringCFunction(obj, "file_load", ejs_sys_file_load);
477 mprSetStringCFunction(obj, "file_save", ejs_sys_file_save);
478 mprSetStringCFunction(obj, "stat", ejs_sys_stat);
479 mprSetStringCFunction(obj, "lstat", ejs_sys_lstat);
480 mprSetCFunction(obj, "bitAND", ejs_sys_bitAND);
481 mprSetCFunction(obj, "bitOR", ejs_sys_bitOR);
488 setup C functions that be called from ejs
490 NTSTATUS smb_setup_ejs_system(void)
492 ejsDefineCFunction(-1, "sys_init", ejs_sys_init, NULL, MPR_VAR_SCRIPT_HANDLE);