Repository navigation
Expand file tree
/
Copy pathmisc.c
More file actions
539 lines (485 loc) · 13.4 KB
/
Copy pathmisc.c
File metadata and controls
539 lines (485 loc) · 13.4 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
* Copyright 2014 The Luvit Authors. All Rights Reserved.
*
* 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
*
* 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 "luv.h"
#ifdef _WIN32
#include <process.h>
#endif
static int luv_guess_handle(lua_State* L) {
uv_file file = luaL_checkinteger(L, 1);
switch (uv_guess_handle(file)) {
#define XX(uc, lc) case UV_##uc: lua_pushstring(L, #lc); break;
UV_HANDLE_TYPE_MAP(XX)
#undef XX
case UV_FILE: lua_pushstring(L, "file"); break;
default: return 0;
}
return 1;
}
static int luv_version(lua_State* L) {
lua_pushinteger(L, uv_version());
return 1;
}
static int luv_version_string(lua_State* L) {
lua_pushstring(L, uv_version_string());
return 1;
}
static int luv_get_process_title(lua_State* L) {
char title[MAX_TITLE_LENGTH];
int ret = uv_get_process_title(title, MAX_TITLE_LENGTH);
if (ret < 0) return luv_error(L, ret);
lua_pushstring(L, title);
return 1;
}
static int luv_set_process_title(lua_State* L) {
const char* title = luaL_checkstring(L, 1);
int ret = uv_set_process_title(title);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_resident_set_memory(lua_State* L) {
size_t rss;
int ret = uv_resident_set_memory(&rss);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, rss);
return 1;
}
static int luv_uptime(lua_State* L) {
double uptime;
int ret = uv_uptime(&uptime);
if (ret < 0) return luv_error(L, ret);
lua_pushnumber(L, uptime);
return 1;
}
static void luv_push_timeval_table(lua_State* L, const uv_timeval_t* t) {
lua_createtable(L, 0, 2);
lua_pushinteger(L, t->tv_sec);
lua_setfield(L, -2, "sec");
lua_pushinteger(L, t->tv_usec);
lua_setfield(L, -2, "usec");
}
static int luv_getrusage(lua_State* L) {
uv_rusage_t rusage;
int ret = uv_getrusage(&rusage);
if (ret < 0) return luv_error(L, ret);
lua_createtable(L, 0, 16);
// user CPU time used
luv_push_timeval_table(L, &rusage.ru_utime);
lua_setfield(L, -2, "utime");
// system CPU time used
luv_push_timeval_table(L, &rusage.ru_stime);
lua_setfield(L, -2, "stime");
// maximum resident set size
lua_pushinteger(L, rusage.ru_maxrss);
lua_setfield(L, -2, "maxrss");
// integral shared memory size
lua_pushinteger(L, rusage.ru_ixrss);
lua_setfield(L, -2, "ixrss");
// integral unshared data size
lua_pushinteger(L, rusage.ru_idrss);
lua_setfield(L, -2, "idrss");
// integral unshared stack size
lua_pushinteger(L, rusage.ru_isrss);
lua_setfield(L, -2, "isrss");
// page reclaims (soft page faults)
lua_pushinteger(L, rusage.ru_minflt);
lua_setfield(L, -2, "minflt");
// page faults (hard page faults)
lua_pushinteger(L, rusage.ru_majflt);
lua_setfield(L, -2, "majflt");
// swaps
lua_pushinteger(L, rusage.ru_nswap);
lua_setfield(L, -2, "nswap");
// block input operations
lua_pushinteger(L, rusage.ru_inblock);
lua_setfield(L, -2, "inblock");
// block output operations
lua_pushinteger(L, rusage.ru_oublock);
lua_setfield(L, -2, "oublock");
// IPC messages sent
lua_pushinteger(L, rusage.ru_msgsnd);
lua_setfield(L, -2, "msgsnd");
// IPC messages received
lua_pushinteger(L, rusage.ru_msgrcv);
lua_setfield(L, -2, "msgrcv");
// signals received
lua_pushinteger(L, rusage.ru_nsignals);
lua_setfield(L, -2, "nsignals");
// voluntary context switches
lua_pushinteger(L, rusage.ru_nvcsw);
lua_setfield(L, -2, "nvcsw");
// involuntary context switches
lua_pushinteger(L, rusage.ru_nivcsw);
lua_setfield(L, -2, "nivcsw");
return 1;
}
static int luv_cpu_info(lua_State* L) {
uv_cpu_info_t* cpu_infos;
int count, i;
int ret = uv_cpu_info(&cpu_infos, &count);
if (ret < 0) return luv_error(L, ret);
lua_newtable(L);
for (i = 0; i < count; i++) {
lua_newtable(L);
lua_pushstring(L, cpu_infos[i].model);
lua_setfield(L, -2, "model");
lua_pushnumber(L, cpu_infos[i].speed);
lua_setfield(L, -2, "speed");
lua_newtable(L);
lua_pushnumber(L, cpu_infos[i].cpu_times.user);
lua_setfield(L, -2, "user");
lua_pushnumber(L, cpu_infos[i].cpu_times.nice);
lua_setfield(L, -2, "nice");
lua_pushnumber(L, cpu_infos[i].cpu_times.sys);
lua_setfield(L, -2, "sys");
lua_pushnumber(L, cpu_infos[i].cpu_times.idle);
lua_setfield(L, -2, "idle");
lua_pushnumber(L, cpu_infos[i].cpu_times.irq);
lua_setfield(L, -2, "irq");
lua_setfield(L, -2, "times");
lua_rawseti(L, -2, i + 1);
}
uv_free_cpu_info(cpu_infos, count);
return 1;
}
static int luv_interface_addresses(lua_State* L) {
uv_interface_address_t* interfaces;
int count, i;
char ip[INET6_ADDRSTRLEN];
char netmask[INET6_ADDRSTRLEN];
uv_interface_addresses(&interfaces, &count);
lua_newtable(L);
for (i = 0; i < count; i++) {
lua_getfield(L, -1, interfaces[i].name);
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, -3, interfaces[i].name);
}
lua_newtable(L);
lua_pushboolean(L, interfaces[i].is_internal);
lua_setfield(L, -2, "internal");
lua_pushlstring(L, interfaces[i].phys_addr, sizeof(interfaces[i].phys_addr));
lua_setfield(L, -2, "mac");
if (interfaces[i].address.address4.sin_family == AF_INET) {
uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
} else if (interfaces[i].address.address4.sin_family == AF_INET6) {
uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
} else {
strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
strncpy(netmask, "<unknown sa family>", INET6_ADDRSTRLEN);
}
lua_pushstring(L, ip);
lua_setfield(L, -2, "ip");
lua_pushstring(L, netmask);
lua_setfield(L, -2, "netmask");
lua_pushstring(L, luv_af_num_to_string(interfaces[i].address.address4.sin_family));
lua_setfield(L, -2, "family");
lua_rawseti(L, -2, lua_rawlen (L, -2) + 1);
lua_pop(L, 1);
}
uv_free_interface_addresses(interfaces, count);
return 1;
}
static int luv_loadavg(lua_State* L) {
double avg[3];
uv_loadavg(avg);
lua_pushnumber(L, avg[0]);
lua_pushnumber(L, avg[1]);
lua_pushnumber(L, avg[2]);
return 3;
}
static int luv_exepath(lua_State* L) {
size_t size = 2*PATH_MAX;
char exe_path[2*PATH_MAX];
int ret = uv_exepath(exe_path, &size);
if (ret < 0) return luv_error(L, ret);
lua_pushlstring(L, exe_path, size);
return 1;
}
static int luv_cwd(lua_State* L) {
size_t size = 2*PATH_MAX;
char path[2*PATH_MAX];
int ret = uv_cwd(path, &size);
if (ret < 0) return luv_error(L, ret);
lua_pushlstring(L, path, size);
return 1;
}
static int luv_chdir(lua_State* L) {
int ret = uv_chdir(luaL_checkstring(L, 1));
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
static int luv_os_tmpdir(lua_State* L) {
size_t size = 2*PATH_MAX;
char tmpdir[2*PATH_MAX];
int ret = uv_os_tmpdir(tmpdir, &size);
if (ret < 0) return luv_error(L, ret);
lua_pushlstring(L, tmpdir, size);
return 1;
}
static int luv_os_homedir(lua_State* L) {
size_t size = 2*PATH_MAX;
char homedir[2*PATH_MAX];
int ret = uv_os_homedir(homedir, &size);
if (ret < 0) return luv_error(L, ret);
lua_pushlstring(L, homedir, size);
return 1;
}
static int luv_os_get_passwd(lua_State* L) {
uv_passwd_t pwd;
int ret = uv_os_get_passwd(&pwd);
if (ret < 0) return luv_error(L, ret);
lua_newtable(L);
if (pwd.username) {
lua_pushstring(L, pwd.username);
lua_setfield(L, -2, "username");
}
if (pwd.uid >= 0) {
lua_pushinteger(L, pwd.uid);
lua_setfield(L, -2, "uid");
}
if (pwd.gid >= 0) {
lua_pushinteger(L, pwd.gid);
lua_setfield(L, -2, "gid");
}
if (pwd.shell) {
lua_pushstring(L, pwd.shell);
lua_setfield(L, -2, "shell");
}
if (pwd.homedir) {
lua_pushstring(L, pwd.homedir);
lua_setfield(L, -2, "homedir");
}
uv_os_free_passwd(&pwd);
return 1;
}
static int luv_get_total_memory(lua_State* L) {
lua_pushnumber(L, uv_get_total_memory());
return 1;
}
static int luv_get_free_memory(lua_State* L) {
lua_pushnumber(L, uv_get_free_memory());
return 1;
}
static int luv_hrtime(lua_State* L) {
lua_pushnumber(L, uv_hrtime());
return 1;
}
static int luv_getpid(lua_State* L){
int pid = getpid();
lua_pushinteger(L, pid);
return 1;
}
#ifndef _WIN32
static int luv_getuid(lua_State* L){
int uid = getuid();
lua_pushinteger(L, uid);
return 1;
}
static int luv_getgid(lua_State* L){
int gid = getgid();
lua_pushinteger(L, gid);
return 1;
}
static int luv_setuid(lua_State* L){
int uid = luaL_checkinteger(L, 1);
int r = setuid(uid);
if (-1 == r) {
luaL_error(L, "Error setting UID");
}
return 0;
}
static int luv_setgid(lua_State* L){
int gid = luaL_checkinteger(L, 1);
int r = setgid(gid);
if (-1 == r) {
luaL_error(L, "Error setting GID");
}
return 0;
}
static int luv_print_all_handles(lua_State* L){
uv_print_all_handles(luv_loop(L), stderr);
return 0;
}
static int luv_print_active_handles(lua_State* L){
uv_print_active_handles(luv_loop(L), stderr);
return 0;
}
#endif
#if LUV_UV_VERSION_GEQ(1, 12, 0)
static int luv_os_getenv(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
size_t size = luaL_optinteger(L, 2, LUAL_BUFFERSIZE);
char *buff = malloc(size);
int ret = uv_os_getenv(name, buff, &size);
if (ret == 0) {
lua_pushlstring(L, buff, size);
ret = 1;
} else
ret = luv_error(L, ret);
free(buff);
return ret;
}
static int luv_os_setenv(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
const char* value = luaL_checkstring(L, 2);
int ret = uv_os_setenv(name, value);
if (ret == 0)
return luv_error(L, ret);
else
lua_pushboolean(L, 1);
return 1;
}
static int luv_os_unsetenv(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
int ret = uv_os_unsetenv(name);
if (ret == 0)
return luv_error(L, ret);
else
lua_pushboolean(L, 1);
return 1;
}
static int luv_os_gethostname(lua_State* L) {
#if LUV_UV_VERSION_GEQ(1, 26, 0)
char hostname[UV_MAXHOSTNAMESIZE];
#else
char hostname[PATH_MAX];
#endif
size_t size = sizeof(hostname);
int ret = uv_os_gethostname(hostname, &size);
if (ret == 0) {
lua_pushlstring(L, hostname, size);
ret = 1;
}
else
ret = luv_error(L, ret);
return ret;
}
#endif
#if LUV_UV_VERSION_GEQ(1, 16, 0)
static int luv_if_indextoname(lua_State* L) {
/* 40 bytes address, 16 bytes device name, plus reserve. */
char scoped_addr[128];
size_t scoped_addr_len = sizeof(scoped_addr);
unsigned int ifindex = (unsigned int)luaL_checkinteger(L, 1);
int ret = uv_if_indextoname(ifindex - 1, scoped_addr, &scoped_addr_len);
if (ret == 0) {
lua_pushlstring(L, scoped_addr, scoped_addr_len);
ret = 1;
}
else
ret = luv_error(L, ret);
return ret;
}
static int luv_if_indextoiid(lua_State* L) {
char interface_id[UV_IF_NAMESIZE];
size_t interface_id_len = sizeof(interface_id);
unsigned int ifindex = (unsigned int)luaL_checkinteger(L, 1);
int ret = uv_if_indextoiid(ifindex - 1, interface_id, &interface_id_len);
if (ret == 0) {
lua_pushlstring(L, interface_id, interface_id_len);
ret = 1;
}
else
ret = luv_error(L, ret);
return ret;
}
static int luv_os_getppid(lua_State* L) {
lua_pushnumber(L, uv_os_getppid());
return 1;
}
#endif
#if LUV_UV_VERSION_GEQ(1, 18, 0)
static int luv_os_getpid(lua_State* L) {
lua_pushnumber(L, uv_os_getpid());
return 1;
}
#endif
#if LUV_UV_VERSION_GEQ(1, 23, 0)
static int luv_os_getpriority(lua_State* L) {
int priority;
uv_pid_t pid = luaL_checkinteger(L, 1);
int ret = uv_os_getpriority(pid, &priority);
if (ret == 0) {
lua_pushnumber(L, priority);
ret = 1;
}
else {
ret = luv_error(L, ret);
}
return ret;
}
#endif
#if LUV_UV_VERSION_GEQ(1, 23, 0)
static int luv_os_setpriority(lua_State* L) {
uv_pid_t pid = luaL_checkinteger(L, 1);
int priority= luaL_checkinteger(L, 2);
int ret = uv_os_setpriority(pid, priority);
if (ret == 0) {
lua_pushboolean(L, 1);
ret = 1;
}
else
ret = luv_error(L, ret);
return ret;
}
#endif
#if LUV_UV_VERSION_GEQ(1, 25, 0)
static int luv_os_uname(lua_State* L) {
uv_utsname_t uname;
int ret = uv_os_uname(&uname);
if (ret == 0) {
lua_newtable(L);
lua_pushstring(L, uname.sysname);
lua_setfield(L, -2, "sysname");
lua_pushstring(L, uname.release);
lua_setfield(L, -2, "release");
lua_pushstring(L, uname.version);
lua_setfield(L, -2, "version");
lua_pushstring(L, uname.machine);
lua_setfield(L, -2, "machine");
ret = 1;
}
else
ret = luv_error(L, ret);
return ret;
}
#endif
#if LUV_UV_VERSION_GEQ(1, 28, 0)
static int luv_gettimeofday(lua_State* L) {
uv_timeval64_t tv = { 0 };
int ret = uv_gettimeofday(&tv);
if (ret == 0)
{
#if defined(__LP64__)
lua_pushinteger(L, tv.tv_sec);
#else
lua_pushnumber(L, tv.tv_sec);
#endif
lua_pushinteger(L, tv.tv_usec);
return 2;
}
else
ret = luv_error(L, ret);
return ret;
}
#endif