-
Notifications
You must be signed in to change notification settings - Fork 3
/
syscall.lua
2269 lines (1986 loc) · 74.1 KB
/
syscall.lua
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Linux syscall ABI ffi interface
local function syscall()
local S = {} -- exported functions
local ffi = require "ffi"
local bit = require "bit"
require "include.headers"
local oldsm = setmetatable
local function setmetatable(t, mt)
assert(mt, "BUG: nil metatable")
return oldsm(t, mt)
end
S.C = setmetatable({}, {__index = ffi.C})
local C = S.C
local CC = {} -- functions that might not be in C, may use syscalls
local c = require "include.constants"
S.c = c
S.bits_to_speed, S.speed_to_bits = c.bits_to_speed, c.speed_to_bits -- should be in metatables
local function split(delimiter, text)
if delimiter == "" then return {text} end
if #text == 0 then return {} end
local list = {}
local pos = 1
while true do
local first, last = text:find(delimiter, pos)
if first then
list[#list + 1] = text:sub(pos, first - 1)
pos = last + 1
else
list[#list + 1] = text:sub(pos)
break
end
end
return list
end
local types = require("include.types")
S.t, S.pt, S.s, S.ctypes = types.t, types.pt, types.s, types.ctypes -- types, pointer types and sizes tables and ctypes map
local t, pt, s, ctypes = S.t, S.pt, S.s, S.ctypes
local mt = {} -- metatables
local meth = {}
-- convenience so user need not require ffi
S.string = ffi.string
S.sizeof = ffi.sizeof
S.cast = ffi.cast
S.copy = ffi.copy
S.fill = ffi.fill
S.istype = ffi.istype
function S.nogc(d) return ffi.gc(d, nil) end
-- reverse lookup tables from constants
local errsyms = {} -- reverse lookup
for k, v in pairs(c.E) do
errsyms[v] = k
end
-- use 64 bit fileops on 32 bit always
if ffi.abi("32bit") then
C.truncate = ffi.C.truncate64
C.ftruncate = ffi.C.ftruncate64
end
-- makes code tidier
local function istype(tp, x)
if ffi.istype(tp, x) then return x else return false end
end
local function getfd(fd)
if type(fd) == "number" or ffi.istype(t.int, fd) then return fd end
return fd:getfd()
end
local function getfd_at(fd)
if not fd then return c.AT_FDCWD.FDCWD end
if type(fd) == "string" then return c.AT_FDCWD[fd] end
return getfd(fd)
end
-- metatables for Lua types not ffi types - convert to ffi types
-- convert to ffi. note missing some macros eg WCOREDUMP()
mt.wait = {
__index = function(w, k)
local WTERMSIG = bit.band(w.status, 0x7f)
local EXITSTATUS = bit.rshift(bit.band(w.status, 0xff00), 8)
local WIFEXITED = (WTERMSIG == 0)
local tab = {
WIFEXITED = WIFEXITED,
WIFSTOPPED = bit.band(w.status, 0xff) == 0x7f,
WIFSIGNALED = not WIFEXITED and bit.band(w.status, 0x7f) ~= 0x7f -- I think this is right????? TODO recheck, cleanup
}
if tab.WIFEXITED then tab.EXITSTATUS = EXITSTATUS end
if tab.WIFSTOPPED then tab.WSTOPSIG = EXITSTATUS end
if tab.WIFSIGNALED then tab.WTERMSIG = WTERMSIG end
if tab[k] then return tab[k] end
local uc = 'W' .. k:upper()
if tab[uc] then return tab[uc] end
end
}
-- TODO convert to ffi metatype
mt.timex = {
__index = function(timex, k)
if c.TIME[k] then return timex.state == c.TIME[k] end
return nil
end
}
-- TODO convert to ffi metatype
mt.epoll = {
__index = function(tab, k)
if c.EPOLL[k] then return bit.band(tab.events, c.EPOLL[k]) ~= 0 end
end
}
-- TODO convert to ffi metatype
mt.inotify = {
__index = function(tab, k)
if c.IN[k] then return bit.band(tab.mask, c.IN[k]) ~= 0 end
end
}
-- TODO convert to ffi metatype
mt.dents = {
__index = function(tab, k)
if c.DT[k] then return tab.type == c.DT[k] end
return nil
end
}
-- misc
-- typed values for pointer comparison
local zeropointer = pt.void(0)
local errpointer = pt.void(-1)
local function div(a, b) return math.floor(tonumber(a) / tonumber(b)) end -- would be nicer if replaced with shifts, as only powers of 2
-- return helpers.
-- straight passthrough, only needed for real 64 bit quantities. Used eg for seek (file might have giant holes!)
local function ret64(ret)
if ret == t.uint64(-1) then return nil, t.error() end
return ret
end
local function retnum(ret) -- return Lua number where double precision ok, eg file ops etc
ret = tonumber(ret)
if ret == -1 then return nil, t.error() end
return ret
end
local function retfd(ret)
if ret == -1 then return nil, t.error() end
return t.fd(ret)
end
-- used for no return value, return true for use of assert
local function retbool(ret)
if ret == -1 then return nil, t.error() end
return true
end
-- used for pointer returns, -1 is failure
local function retptr(ret)
if ret == errpointer then return nil, t.error() end
return ret
end
local function retwait(ret, status) -- TODO metatype
if ret == -1 then return nil, t.error() end
return setmetatable({pid = ret, status = status}, mt.wait)
end
local function retnume(f, ...) -- for cases where need to explicitly set and check errno, ie signed int return
ffi.errno(0)
local ret = f(...)
local errno = ffi.errno()
if errno ~= 0 then return nil, t.error() end
return ret
end
-- TODO add tests
mt.sockaddr_un = {
__index = function(un, k)
local sa = un.addr
if k == 'family' then return tonumber(sa.sun_family) end
local namelen = un.addrlen - s.sun_family
if namelen > 0 then
if sa.sun_path[0] == 0 then
if k == 'abstract' then return true end
if k == 'name' then return ffi.string(rets.addr.sun_path, namelen) end -- should we also remove leading \0?
else
if k == 'name' then return ffi.string(rets.addr.sun_path) end
end
else
if k == 'unnamed' then return true end
end
end
}
local function sa(addr, addrlen)
local family = addr.family
if family == c.AF.UNIX then -- we return Lua metatable not metatype, as need length to decode
local sa = t.sockaddr_un()
ffi.copy(sa, addr, addrlen)
return setmetatable({addr = sa, addrlen = addrlen}, mt.sockaddr_un)
end
return addr
end
-- these functions might not be in libc, or are buggy so provide direct syscall fallbacks
local function inlibc(f) return ffi.C[f] end
-- glibc caches pid, but this fails to work eg after clone(). Musl is fine TODO test for this?
function C.getpid()
return C.syscall(c.SYS.getpid)
end
-- clone interface provided is not same as system one, and is less convenient
function C.clone(flags, signal, stack, ptid, tls, ctid)
return C.syscall(c.SYS.clone, t.int(flags), pt.void(stack), pt.void(ptid), pt.void(tls), pt.void(ctid))
end
-- getdents is not provided by glibc. Musl has weak alias so not visible.
function C.getdents(fd, buf, size)
return C.syscall(c.SYS.getdents64, t.int(fd), buf, t.uint(size))
end
-- getcwd will allocate memory, so use syscall
function C.getcwd(buf, size)
return C.syscall(c.SYS.getcwd, pt.void(buf), t.ulong(size))
end
-- for stat we use the syscall as libc might have a different struct stat for compatibility
-- TODO see if we can avoid this, at least for reasonable libc. Musl returns the right struct.
if ffi.abi("64bit") then
function C.stat(path, buf)
return C.syscall(c.SYS.stat, path, pt.void(buf))
end
function C.lstat(path, buf)
return C.syscall(c.SYS.lstat, path, pt.void(buf))
end
function C.fstat(fd, buf)
return C.syscall(c.SYS.fstat, t.int(fd), pt.void(buf))
end
function C.fstatat(fd, path, buf, flags)
return C.syscall(c.SYS.fstatat, t.int(fd), path, pt.void(buf), t.int(flags))
end
else
function C.stat(path, buf)
return C.syscall(c.SYS.stat64, path, pt.void(buf))
end
function C.lstat(path, buf)
return C.syscall(c.SYS.lstat64, path, pt.void(buf))
end
function C.fstat(fd, buf)
return C.syscall(c.SYS.fstat64, t.int(fd), pt.void(buf))
end
function C.fstatat(fd, path, buf, flags)
return C.syscall(c.SYS.fstatat64, t.int(fd), path, pt.void(buf), t.int(flags))
end
end
-- lseek is a mess in 32 bit, use _llseek syscall to get clean result
if ffi.abi("32bit") then
function C.lseek(fd, offset, whence)
local result = t.loff1()
local off1, off2 = S.u64(offset)
local ret = C.syscall(c.SYS._llseek, t.int(fd), t.ulong(off1), t.ulong(off2), pt.void(result), t.uint(whence))
if ret == -1 then return -1 end
return result[0]
end
end
-- native Linux aio not generally supported, only posix API TODO these are not working
function C.io_setup(nr_events, ctx)
return C.syscall(c.SYS.io_setup, t.uint(nr_events), ctx)
end
function C.io_destroy(ctx)
return C.syscall(c.SYS.io_destroy, ctx)
end
function C.io_cancel(ctx, iocb, result)
return C.syscall(c.SYS.io_cancel, ctx, iocb, result)
end
function C.io_getevents(ctx, min, nr, events, timeout)
return C.syscall(c.SYS.io_getevents, ctx, t.long(min), t.long(nr), events, timeout)
end
function C.io_submit(ctx, iocb, nr)
return C.syscall(c.SYS.io_submit, ctx, t.long(nr), iocb)
end
-- note dev_t not passed as 64 bits to this syscall
function CC.mknod(pathname, mode, dev)
return C.syscall(c.SYS.mknod, pathname, t.mode(mode), t.long(dev))
end
function CC.mknodat(fd, pathname, mode, dev)
return C.syscall(c.SYS.mknodat, t.int(fd), pathname, t.mode(mode), t.long(dev))
end
-- pivot_root is not provided by glibc, is provided by Musl
function CC.pivot_root(new_root, put_old)
return C.syscall(C.SYS.pivot_root, new_root, put_old)
end
--[[ if you need to split 64 bit args on 32 bit syscalls use code like this
if ffi.abi("64bit") then
function CC.fallocate(fd, mode, offset, len)
return C.syscall(c.SYS.fallocate, t.int(fd), t.uint(mode), t.loff(offset), t.loff(len))
end
else -- 32 bit uses splits for 64 bit args
function CC.fallocate(fd, mode, offset, len)
local off2, off1 = S.u64(offset)
local len2, len1 = S.u64(len)
return C.syscall(c.SYS.fallocate, t.int(fd), t.uint(mode), t.uint32(off1), t.uint32(off2), t.uint32(len1), t.uint32(len2))
end
end
]]
-- if not in libc replace
-- with glibc in -rt
if not pcall(inlibc, "clock_getres") then
local rt = ffi.load "rt"
C.clock_getres = rt.clock_getres
C.clock_settime = rt.clock_settime
C.clock_gettime = rt.clock_gettime
C.clock_nanosleep = rt.clock_nanosleep
end
-- not in eglibc
if not pcall(inlibc, "mknod") then C.mknod = CC.mknod end
if not pcall(inlibc, "mknodat") then C.mknodat = CC.mknodat end
if not pcall(inlibc, "pivot_root") then C.pivot_root = CC.pivot_root end
-- main definitions start here
function S.open(pathname, flags, mode)
return retfd(C.open(pathname, c.O[flags], c.MODE[mode]))
end
function S.openat(dirfd, pathname, flags, mode)
return retfd(C.openat(getfd_at(dirfd), pathname, c.O[flags], c.MODE[mode]))
end
-- TODO dup3 can have a race condition (see man page) although Musl fixes, appears eglibc does not
function S.dup(oldfd, newfd, flags)
if not newfd then return retfd(C.dup(getfd(oldfd))) end
return retfd(C.dup3(getfd(oldfd), getfd(newfd), flags or 0))
end
mt.pipe = {
__index = {
close = function(p)
local ok1, err1 = p[1]:close()
local ok2, err2 = p[2]:close()
if not ok1 then return nil, err1 end
if not ok2 then return nil, err2 end
return true
end,
read = function(p, ...) return p[1]:read(...) end,
write = function(p, ...) return p[2]:write(...) end,
pread = function(p, ...) return p[1]:pread(...) end,
pwrite = function(p, ...) return p[2]:pwrite(...) end,
nonblock = function(p)
local ok, err = p[1]:nonblock()
if not ok then return nil, err end
local ok, err = p[2]:nonblock()
if not ok then return nil, err end
return true
end,
block = function(p)
local ok, err = p[1]:block()
if not ok then return nil, err end
local ok, err = p[2]:block()
if not ok then return nil, err end
return true
end,
setblocking = function(p, b)
local ok, err = p[1]:setblocking(b)
if not ok then return nil, err end
local ok, err = p[2]:setblocking(b)
if not ok then return nil, err end
return true
end,
-- TODO many useful methods still missing
}
}
function S.pipe(flags)
local fd2 = t.int2()
local ret = C.pipe2(fd2, c.OPIPE[flags])
if ret == -1 then return nil, t.error() end
return setmetatable({t.fd(fd2[0]), t.fd(fd2[1])}, mt.pipe)
end
function S.close(fd) return retbool(C.close(getfd(fd))) end
function S.creat(pathname, mode) return retfd(C.creat(pathname, c.MODE[mode])) end
function S.unlink(pathname) return retbool(C.unlink(pathname)) end
function S.unlinkat(dirfd, path, flags)
return retbool(C.unlinkat(getfd_at(dirfd), path, c.AT_REMOVEDIR[flags]))
end
function S.rename(oldpath, newpath) return retbool(C.rename(oldpath, newpath)) end
function S.renameat(olddirfd, oldpath, newdirfd, newpath)
return retbool(C.renameat(getfd_at(olddirfd), oldpath, getfd_at(newdirfd), newpath))
end
function S.chdir(path) return retbool(C.chdir(path)) end
function S.mkdir(path, mode) return retbool(C.mkdir(path, c.MODE[mode])) end
function S.mkdirat(fd, path, mode) return retbool(C.mkdirat(getfd_at(fd), path, c.MODE[mode])) end
function S.rmdir(path) return retbool(C.rmdir(path)) end
function S.acct(filename) return retbool(C.acct(filename)) end
function S.chmod(path, mode) return retbool(C.chmod(path, c.MODE[mode])) end
function S.link(oldpath, newpath) return retbool(C.link(oldpath, newpath)) end
function S.linkat(olddirfd, oldpath, newdirfd, newpath, flags)
return retbool(C.linkat(getfd_at(olddirfd), oldpath, getfd_at(newdirfd), newpath, c.AT_SYMLINK_FOLLOW[flags]))
end
function S.symlink(oldpath, newpath) return retbool(C.symlink(oldpath, newpath)) end
function S.symlinkat(oldpath, newdirfd, newpath) return retbool(C.symlinkat(oldpath, getfd_at(newdirfd), newpath)) end
function S.pause() return retbool(C.pause()) end
function S.chown(path, owner, group) return retbool(C.chown(path, owner or -1, group or -1)) end
function S.fchown(fd, owner, group) return retbool(C.fchown(getfd(fd), owner or -1, group or -1)) end
function S.lchown(path, owner, group) return retbool(C.lchown(path, owner or -1, group or -1)) end
function S.fchownat(dirfd, path, owner, group, flags)
return retbool(C.fchownat(getfd_at(dirfd), path, owner or -1, group or -1, c.AT_SYMLINK_NOFOLLOW[flags]))
end
function S.truncate(path, length) return retbool(C.truncate(path, length)) end
function S.ftruncate(fd, length) return retbool(C.ftruncate(getfd(fd), length)) end
function S.access(pathname, mode) return retbool(C.access(pathname, c.OK[mode])) end
function S.faccessat(dirfd, pathname, mode, flags)
return retbool(C.faccessat(getfd_at(dirfd), pathname, c.OK[mode], c.AT_ACCESSAT[flags]))
end
function S.readlink(path, buffer, size)
size = size or c.PATH_MAX
buffer = buffer or t.buffer(size)
local ret = tonumber(C.readlink(path, buffer, size))
if ret == -1 then return nil, t.error() end
return ffi.string(buffer, ret)
end
function S.readlinkat(dirfd, path, buffer, size)
size = size or c.PATH_MAX
buffer = buffer or t.buffer(size)
local ret = tonumber(C.readlinkat(getfd_at(dirfd), path, buffer, size))
if ret == -1 then return nil, t.error() end
return ffi.string(buffer, ret)
end
function S.mknod(pathname, mode, dev)
if type(dev) == "table" then dev = dev.dev end
return retbool(C.mknod(pathname, c.S[mode], dev or 0))
end
function S.mknodat(fd, pathname, mode, dev)
if type(dev) == "table" then dev = dev.dev end
return retbool(C.mknodat(getfd_at(fd), pathname, c.S[mode], dev or 0))
end
-- mkfifo is from man(3), add for convenience
function S.mkfifo(path, mode) return S.mknod(path, bit.bor(c.S[mode], c.S.IFIFO)) end
function S.mkfifoat(fd, path, mode) return S.mknodat(fd, path, bit.bor(c.S[mode], c.S.IFIFO), 0) end
function S.nice(inc) return retnume(C.nice, inc) end
-- NB glibc is shifting these values from what strace shows, as per man page, kernel adds 20 to make these values positive...
-- might cause issues with other C libraries in which case may shift to using system call
function S.getpriority(which, who) return retnume(C.getpriority, c.PRIO[which], who or 0) end
function S.setpriority(which, who, prio) return retnume(C.setpriority, c.PRIO[which], who or 0, prio) end
-- we could allocate ptid, ctid, tls if required in flags instead. TODO add signal into flag parsing directly
function S.clone(flags, signal, stack, ptid, tls, ctid)
flags = c.CLONE[flags] + c.SIG[signal]
return retnum(C.clone(flags, stack, ptid, tls, ctid))
end
function S.unshare(flags) return retbool(C.unshare(c.CLONE[flags])) end
function S.setns(fd, nstype) return retbool(C.setns(getfd(fd), c.CLONE[nstype])) end
function S.fork() return retnum(C.fork()) end
function S.execve(filename, argv, envp)
local cargv = t.string_array(#argv + 1, argv)
cargv[#argv] = nil -- LuaJIT does not zero rest of a VLA
local cenvp = t.string_array(#envp + 1, envp)
cenvp[#envp] = nil
return retbool(C.execve(filename, cargv, cenvp))
end
function S.ioctl(d, request, argp)
local ret = C.ioctl(getfd(d), request, argp)
if ret == -1 then return nil, t.error() end
-- some different return types may need to be handled
return true
end
-- note that this is not strictly the syscall that has some other arguments, but has same functionality
function S.reboot(cmd) return retbool(C.reboot(c.LINUX_REBOOT_CMD[cmd])) end
-- ffi metatype on dirent?
function S.getdents(fd, buf, size, noiter) -- default behaviour is to iterate over whole directory, use noiter if you have very large directories
if not buf then
size = size or 4096
buf = t.buffer(size)
end
local d = {}
local ret
repeat
ret = C.getdents(getfd(fd), buf, size)
if ret == -1 then return nil, t.error() end
local i = 0
while i < ret do
local dp = pt.dirent(buf + i)
local dd = setmetatable({inode = tonumber(dp.d_ino), offset = tonumber(dp.d_off), type = tonumber(dp.d_type)}, mt.dents)
d[ffi.string(dp.d_name)] = dd -- could calculate length
i = i + dp.d_reclen
end
until noiter or ret == 0
return d
end
function S.wait()
local status = t.int1()
return retwait(C.wait(status), status[0])
end
function S.waitpid(pid, options)
local status = t.int1()
return retwait(C.waitpid(pid, status, c.W[options]), status[0])
end
function S.waitid(idtype, id, options, infop) -- note order of args, as usually dont supply infop
if not infop then infop = t.siginfo() end
infop.si_pid = 0 -- see notes on man page
local ret = C.waitid(c.P[idtype], id or 0, infop, c.W[options])
if ret == -1 then return nil, t.error() end
return infop -- return table here?
end
function S._exit(status) C._exit(c.EXIT[status]) end
function S.exit(status) C.exit(c.EXIT[status]) end
function S.read(fd, buf, count)
if buf then return retnum(C.read(getfd(fd), buf, count)) end -- user supplied a buffer, standard usage
if not count then count = 4096 end
buf = t.buffer(count)
local ret = C.read(getfd(fd), buf, count)
if ret == -1 then return nil, t.error() end
return ffi.string(buf, tonumber(ret)) -- user gets a string back, can get length from #string
end
function S.write(fd, buf, count) return retnum(C.write(getfd(fd), buf, count or #buf)) end
function S.pread(fd, buf, count, offset) return retnum(C.pread64(getfd(fd), buf, count, offset)) end
function S.pwrite(fd, buf, count, offset) return retnum(C.pwrite64(getfd(fd), buf, count or #buf, offset)) end
function S.lseek(fd, offset, whence)
return ret64(C.lseek(getfd(fd), offset or 0, c.SEEK[whence]))
end
function S.send(fd, buf, count, flags) return retnum(C.send(getfd(fd), buf, count or #buf, c.MSG[flags])) end
function S.sendto(fd, buf, count, flags, addr, addrlen)
return retnum(C.sendto(getfd(fd), buf, count or #buf, c.MSG[flags], addr, addrlen or ffi.sizeof(addr)))
end
function S.sendmsg(fd, msg, flags)
if not msg then -- send a single byte message, eg enough to send credentials
local buf1 = t.buffer(1)
local io = t.iovecs{{buf1, 1}}
msg = t.msghdr{msg_iov = io.iov, msg_iovlen = #io}
end
return retbool(C.sendmsg(getfd(fd), msg, c.MSG[flags]))
end
function S.readv(fd, iov)
iov = istype(t.iovecs, iov) or t.iovecs(iov)
return retnum(C.readv(getfd(fd), iov.iov, #iov))
end
function S.writev(fd, iov)
iov = istype(t.iovecs, iov) or t.iovecs(iov)
return retnum(C.writev(getfd(fd), iov.iov, #iov))
end
function S.preadv(fd, iov, offset)
iov = istype(t.iovecs, iov) or t.iovecs(iov)
return retnum(C.preadv64(getfd(fd), iov.iov, #iov, offset))
end
function S.pwritev(fd, iov, offset)
iov = istype(t.iovecs, iov) or t.iovecs(iov)
return retnum(C.pwritev64(getfd(fd), iov.iov, #iov, offset))
end
function S.recv(fd, buf, count, flags) return retnum(C.recv(getfd(fd), buf, count or #buf, c.MSG[flags])) end
function S.recvfrom(fd, buf, count, flags, ss, addrlen)
if not ss then
ss = t.sockaddr_storage()
addrlen = t.socklen1(s.sockaddr_storage)
end
local ret = C.recvfrom(getfd(fd), buf, count, c.MSG[flags], ss, addrlen)
if ret == -1 then return nil, t.error() end
return {count = tonumber(ret), addr = sa(ss, addrlen[0])}
end
function S.setsockopt(fd, level, optname, optval, optlen)
-- allocate buffer for user, from Lua type if know how, int and bool so far
if not optlen and type(optval) == 'boolean' then if optval then optval = 1 else optval = 0 end end
if not optlen and type(optval) == 'number' then
optval = t.int1(optval)
optlen = s.int
end
return retbool(C.setsockopt(getfd(fd), c.SOL[level], c.SO[optname], optval, optlen))
end
function S.getsockopt(fd, level, optname) -- will need fixing for non int/bool options
local optval, optlen = t.int1(), t.socklen1()
optlen[0] = s.int
local ret = C.getsockopt(getfd(fd), level, optname, optval, optlen)
if ret == -1 then return nil, t.error() end
return tonumber(optval[0]) -- no special case for bool
end
function S.fchdir(fd) return retbool(C.fchdir(getfd(fd))) end
function S.fsync(fd) return retbool(C.fsync(getfd(fd))) end
function S.fdatasync(fd) return retbool(C.fdatasync(getfd(fd))) end
function S.fchmod(fd, mode) return retbool(C.fchmod(getfd(fd), c.MODE[mode])) end
function S.fchmodat(dirfd, pathname, mode)
return retbool(C.fchmodat(getfd_at(dirfd), pathname, c.MODE[mode], 0)) -- no flags actually supported
end
function S.sync_file_range(fd, offset, count, flags)
return retbool(C.sync_file_range(getfd(fd), offset, count, c.SYNC_FILE_RANGE[flags]))
end
function S.stat(path, buf)
if not buf then buf = t.stat() end
local ret = C.stat(path, buf)
if ret == -1 then return nil, t.error() end
return buf
end
function S.lstat(path, buf)
if not buf then buf = t.stat() end
local ret = C.lstat(path, buf)
if ret == -1 then return nil, t.error() end
return buf
end
function S.fstat(fd, buf)
if not buf then buf = t.stat() end
local ret = C.fstat(getfd(fd), buf)
if ret == -1 then return nil, t.error() end
return buf
end
function S.fstatat(fd, path, buf, flags)
if not buf then buf = t.stat() end
local ret = C.fstatat(getfd_at(fd), path, buf, c.AT_FSTATAT[flags])
if ret == -1 then return nil, t.error() end
return buf
end
local function gettimespec2(ts)
if ts and (not ffi.istype(t.timespec2, ts)) then
local s1, s2 = ts[1], ts[2]
ts = t.timespec2()
if type(s1) == 'string' then ts[0].tv_nsec = c.UTIME[s1] else ts[0] = t.timespec(s1) end
if type(s2) == 'string' then ts[1].tv_nsec = c.UTIME[s2] else ts[1] = t.timespec(s2) end
end
return ts
end
function S.futimens(fd, ts)
return retbool(C.futimens(getfd(fd), gettimespec2(ts)))
end
function S.utimensat(dirfd, path, ts, flags)
return retbool(C.utimensat(getfd_at(dirfd), path, gettimespec2(ts), c.AT_SYMLINK_NOFOLLOW[flags]))
end
-- because you can just pass floats to all the time functions, just use the same one, but provide different templates
function S.utime(path, actime, modtime)
local ts
if not modtime then modtime = actime end
if actime and modtime then ts = {actime, modtime} end
return S.utimensat(nil, path, ts)
end
S.utimes = S.utime
function S.chroot(path) return retbool(C.chroot(path)) end
function S.getcwd(buf, size)
size = size or c.PATH_MAX
buf = buf or t.buffer(size)
local ret = C.getcwd(buf, size)
if ret == -1 then return nil, t.error() end
return ffi.string(buf)
end
function S.statfs(path)
local st = t.statfs()
local ret = C.statfs(path, st)
if ret == -1 then return nil, t.error() end
return st
end
function S.fstatfs(fd)
local st = t.statfs()
local ret = C.fstatfs(getfd(fd), st)
if ret == -1 then return nil, t.error() end
return st
end
function S.nanosleep(req, rem)
req = istype(t.timespec, req) or t.timespec(req)
rem = rem or t.timespec()
local ret = C.nanosleep(req, rem)
if ret == -1 then
if ffi.errno() == c.E.INTR then return rem else return nil, t.error() end
end
return true
end
function S.sleep(sec) -- standard libc function
local rem, err = S.nanosleep(sec)
if not rem then return nil, err end
if rem == true then return 0 end
return tonumber(rem.tv_sec)
end
function S.mmap(addr, length, prot, flags, fd, offset)
return retptr(C.mmap(addr, length, c.PROT[prot], c.MAP[flags], getfd(fd), offset))
end
function S.munmap(addr, length)
return retbool(C.munmap(addr, length))
end
function S.msync(addr, length, flags) return retbool(C.msync(addr, length, c.MSYNC[flags])) end
function S.mlock(addr, len) return retbool(C.mlock(addr, len)) end
function S.munlock(addr, len) return retbool(C.munlock(addr, len)) end
function S.mlockall(flags) return retbool(C.mlockall(c.MCL[flags])) end
function S.munlockall() return retbool(C.munlockall()) end
function S.mremap(old_address, old_size, new_size, flags, new_address)
return retptr(C.mremap(old_address, old_size, new_size, c.MREMAP[flags], new_address))
end
function S.madvise(addr, length, advice) return retbool(C.madvise(addr, length, c.MADV[advice])) end
function S.fadvise(fd, advice, offset, len) -- note argument order
return retbool(C.posix_fadvise(getfd(fd), offset or 0, len or 0, c.POSIX_FADV[advice]))
end
function S.fallocate(fd, mode, offset, len)
return retbool(C.fallocate(getfd(fd), c.FALLOC_FL[mode], offset or 0, len))
end
function S.posix_fallocate(fd, offset, len) return S.fallocate(fd, 0, offset, len) end
function S.readahead(fd, offset, count) return retbool(C.readahead(getfd(fd), offset, count)) end
local function sproto(domain, protocol) -- helper function to lookup protocol type depending on domain
if domain == c.AF.NETLINK then return c.NETLINK[protocol] end
return protocol or 0
end
function S.socket(domain, stype, protocol)
domain = c.AF[domain]
local ret = C.socket(domain, c.SOCK[stype], sproto(domain, protocol))
if ret == -1 then return nil, t.error() end
return t.fd(ret)
end
mt.socketpair = {
__index = {
close = function(s)
local ok1, err1 = s[1]:close()
local ok2, err2 = s[2]:close()
if not ok1 then return nil, err1 end
if not ok2 then return nil, err2 end
return true
end,
nonblock = function(s)
local ok, err = s[1]:nonblock()
if not ok then return nil, err end
local ok, err = s[2]:nonblock()
if not ok then return nil, err end
return true
end,
block = function(s)
local ok, err = s[1]:block()
if not ok then return nil, err end
local ok, err = s[2]:block()
if not ok then return nil, err end
return true
end,
setblocking = function(s, b)
local ok, err = s[1]:setblocking(b)
if not ok then return nil, err end
local ok, err = s[2]:setblocking(b)
if not ok then return nil, err end
return true
end,
}
}
function S.socketpair(domain, stype, protocol)
domain = c.AF[domain]
local sv2 = t.int2()
local ret = C.socketpair(domain, c.SOCK[stype], sproto(domain, protocol), sv2)
if ret == -1 then return nil, t.error() end
return setmetatable({t.fd(sv2[0]), t.fd(sv2[1])}, mt.socketpair)
end
function S.bind(sockfd, addr, addrlen)
return retbool(C.bind(getfd(sockfd), addr, addrlen or ffi.sizeof(addr)))
end
function S.listen(sockfd, backlog) return retbool(C.listen(getfd(sockfd), backlog or c.SOMAXCONN)) end
function S.connect(sockfd, addr, addrlen)
return retbool(C.connect(getfd(sockfd), addr, addrlen or ffi.sizeof(addr)))
end
function S.shutdown(sockfd, how) return retbool(C.shutdown(getfd(sockfd), c.SHUT[how])) end
function S.accept(sockfd, flags, addr, addrlen)
if not addr then addr = t.sockaddr_storage() end
if not addrlen then addrlen = t.socklen1(addrlen or ffi.sizeof(addr)) end
local ret
if not flags
then ret = C.accept(getfd(sockfd), addr, addrlen)
else ret = C.accept4(getfd(sockfd), addr, addrlen, c.SOCK[flags])
end
if ret == -1 then return nil, t.error() end
return {fd = t.fd(ret), addr = sa(addr, addrlen[0])}
end
function S.getsockname(sockfd, ss, addrlen)
if not ss then
ss = t.sockaddr_storage()
addrlen = t.socklen1(s.sockaddr_storage)
end
local ret = C.getsockname(getfd(sockfd), ss, addrlen)
if ret == -1 then return nil, t.error() end
return sa(ss, addrlen[0])
end
function S.getpeername(sockfd, ss, addrlen)
if not ss then
ss = t.sockaddr_storage()
addrlen = t.socklen1(s.sockaddr_storage)
end
local ret = C.getpeername(getfd(sockfd), ss, addrlen)
if ret == -1 then return nil, t.error() end
return sa(ss, addrlen[0])
end
local function getflock(arg)
if not arg then arg = t.flock() end
if not ffi.istype(t.flock, arg) then
for _, v in pairs {"type", "whence", "start", "len", "pid"} do -- allow use of short names
if arg[v] then
arg["l_" .. v] = arg[v] -- TODO cleanup this to use table?
arg[v] = nil
end
end
arg.l_type = c.FCNTL_LOCK[arg.l_type]
arg.l_whence = c.SEEK[arg.l_whence]
arg = t.flock(arg)
end
return arg
end
local fcntl_commands = {
[c.F.SETFL] = function(arg) return c.O[arg] end,
[c.F.SETFD] = function(arg) return c.FD[arg] end,
[c.F.GETLK] = getflock,
[c.F.SETLK] = getflock,
[c.F.SETLKW] = getflock,
}
local fcntl_ret = {
[c.F.DUPFD] = function(ret) return t.fd(ret) end,
[c.F.DUPFD_CLOEXEC] = function(ret) return t.fd(ret) end,
[c.F.GETFD] = function(ret) return tonumber(ret) end,
[c.F.GETFL] = function(ret) return tonumber(ret) end,
[c.F.GETLEASE] = function(ret) return tonumber(ret) end,
[c.F.GETOWN] = function(ret) return tonumber(ret) end,
[c.F.GETSIG] = function(ret) return tonumber(ret) end,
[c.F.GETPIPE_SZ] = function(ret) return tonumber(ret) end,
[c.F.GETLK] = function(ret, arg) return arg end,
}
function S.fcntl(fd, cmd, arg)
cmd = c.F[cmd]
if fcntl_commands[cmd] then arg = fcntl_commands[cmd](arg) end
local ret = C.fcntl(getfd(fd), cmd, pt.void(arg or 0))
if ret == -1 then return nil, t.error() end
if fcntl_ret[cmd] then return fcntl_ret[cmd](ret, arg) end
return true
end
function S.uname()
local u = t.utsname()
local ret = C.uname(u)
if ret == -1 then return nil, t.error() end
return {sysname = ffi.string(u.sysname), nodename = ffi.string(u.nodename), release = ffi.string(u.release),
version = ffi.string(u.version), machine = ffi.string(u.machine), domainname = ffi.string(u.domainname)}
end
function S.gethostname()
local u, err = S.uname()
if not u then return nil, err end
return u.nodename
end
function S.getdomainname()
local u, err = S.uname()
if not u then return nil, err end
return u.domainname
end
function S.sethostname(s) -- only accept Lua string, do not see use case for buffer as well
return retbool(C.sethostname(s, #s))
end
function S.setdomainname(s)
return retbool(C.setdomainname(s, #s))
end
-- does not support passing a function as a handler, use sigaction instead
-- actualy glibc does not call the syscall anyway, defines in terms of sigaction; TODO we should too
function S.signal(signum, handler) return retbool(C.signal(c.SIG[signum], c.SIGACT[handler])) end
-- missing siginfo functionality for now, only supports getting signum TODO
-- NOTE I do not think it is safe to call this with a function argument as the jit compiler will not know when it is going to
-- be called, so have removed this functionality again
-- recommend using signalfd to handle signals if you need to do anything complex.
-- note arguments can be different TODO should we change
function S.sigaction(signum, handler, mask, flags)
local sa
if ffi.istype(t.sigaction, handler) then sa = handler
else
if type(handler) == 'string' then
handler = ffi.cast(t.sighandler, t.int1(c.SIGACT[handler]))
--elseif
-- type(handler) == 'function' then handler = ffi.cast(t.sighandler, handler) -- TODO check if gc problem here? need to copy?
end
sa = t.sigaction{sa_handler = handler, sa_mask = t.sigset(mask), sa_flags = c.SA[flags]}
end
local old = t.sigaction()
local ret = C.sigaction(c.SIG[signum], sa, old)
if ret == -1 then return nil, t.error() end
return old
end
function S.kill(pid, sig) return retbool(C.kill(pid, c.SIG[sig])) end
function S.killpg(pgrp, sig) return S.kill(-pgrp, sig) end
function S.gettimeofday(tv)
if not tv then tv = t.timeval() end -- note it is faster to pass your own tv if you call a lot
local ret = C.gettimeofday(tv, nil)
if ret == -1 then return nil, t.error() end
return tv
end
function S.settimeofday(tv) return retbool(C.settimeofday(tv, nil)) end
function S.time()
return tonumber(C.time(nil))
end
function S.sysinfo(info)
if not info then info = t.sysinfo() end
local ret = C.sysinfo(info)
if ret == -1 then return nil, t.error() end
return info
end
local function growattrbuf(f, a, b)
local len = 512
local buffer = t.buffer(len)
local ret
repeat
if b then
ret = tonumber(f(a, b, buffer, len))
else
ret = tonumber(f(a, buffer, len))
end
if ret == -1 and ffi.errno ~= c.E.ERANGE then return nil, t.error() end
if ret == -1 then
len = len * 2
buffer = t.buffer(len)