Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/linux.zig
1067 views
1
// Functionality that is only on linux. Might not even be in posix!
2
const node = @import("node.zig");
3
const unistd = @cImport({
4
@cDefine("__USE_GNU", "1");
5
@cInclude("unistd.h");
6
});
7
const c = @import("c.zig");
8
9
pub fn register(env: c.napi_env, exports: c.napi_value) !void {
10
try node.registerFunction(env, exports, "getresuid", getresuid_impl);
11
try node.registerFunction(env, exports, "getresgid", getresgid_impl);
12
try node.registerFunction(env, exports, "setresuid", setresuid_impl);
13
try node.registerFunction(env, exports, "setresgid", setresgid_impl);
14
}
15
16
// These uid_t and gid_t are actually u32.
17
// Also, for some reason unistd.getresuid, etc., isn't available in headers, which is
18
// maybe a bug in zig (?).
19
extern fn getresuid(ruid: *unistd.uid_t, euid: *unistd.uid_t, suid: *unistd.uid_t) c_int;
20
21
fn getresuid_impl(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
22
_ = info;
23
var ruid: unistd.uid_t = undefined;
24
var euid: unistd.uid_t = undefined;
25
var suid: unistd.uid_t = undefined;
26
if (getresuid(&ruid, &euid, &suid) == -1) {
27
node.throwErrno(env, "getresuid failed");
28
return null;
29
}
30
var object = node.createObject(env, "") catch return null;
31
const _ruid = node.create_u32(env, ruid, "ruid") catch return null;
32
node.setNamedProperty(env, object, "ruid", _ruid, "") catch return null;
33
const _euid = node.create_u32(env, euid, "euid") catch return null;
34
node.setNamedProperty(env, object, "euid", _euid, "") catch return null;
35
const _suid = node.create_u32(env, suid, "suid") catch return null;
36
node.setNamedProperty(env, object, "suid", _suid, "") catch return null;
37
return object;
38
}
39
// // int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
40
extern fn getresgid(rgid: *unistd.gid_t, egid: *unistd.gid_t, sgid: *unistd.gid_t) c_int;
41
42
fn getresgid_impl(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
43
_ = info;
44
var rgid: unistd.gid_t = undefined;
45
var egid: unistd.gid_t = undefined;
46
var sgid: unistd.gid_t = undefined;
47
if (getresgid(&rgid, &egid, &sgid) == -1) {
48
node.throwErrno(env, "getresgid failed");
49
return null;
50
}
51
var object = node.createObject(env, "") catch return null;
52
const _rgid = node.create_u32(env, rgid, "rgid") catch return null;
53
node.setNamedProperty(env, object, "rgid", _rgid, "") catch return null;
54
const _egid = node.create_u32(env, egid, "egid") catch return null;
55
node.setNamedProperty(env, object, "egid", _egid, "") catch return null;
56
const _sgid = node.create_u32(env, sgid, "sgid") catch return null;
57
node.setNamedProperty(env, object, "sgid", _sgid, "") catch return null;
58
return object;
59
}
60
61
// // int setresuid(uid_t ruid, uid_t euid, uid_t suid);
62
extern fn setresuid(ruid: unistd.uid_t, euid: unistd.uid_t, suid: unistd.uid_t) c_int;
63
64
fn setresuid_impl(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
65
const argv = node.getArgv(env, info, 3) catch return null;
66
const ruid = node.u32FromValue(env, argv[0], "ruid") catch return null;
67
const euid = node.u32FromValue(env, argv[1], "euid") catch return null;
68
const suid = node.u32FromValue(env, argv[1], "suid") catch return null;
69
if (setresuid(ruid, euid, suid) == -1) {
70
node.throwErrno(env, "error in setresuid");
71
}
72
return null;
73
}
74
75
// int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
76
extern fn setresgid(rgid: unistd.gid_t, egid: unistd.gid_t, sgid: unistd.gid_t) c_int;
77
78
fn setresgid_impl(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
79
const argv = node.getArgv(env, info, 3) catch return null;
80
const rgid = node.u32FromValue(env, argv[0], "rgid") catch return null;
81
const egid = node.u32FromValue(env, argv[1], "egid") catch return null;
82
const sgid = node.u32FromValue(env, argv[1], "sgid") catch return null;
83
if (setresgid(rgid, egid, sgid) == -1) {
84
node.throwErrno(env, "error in setresgid");
85
}
86
return null;
87
}
88
89