authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-05 17:25:24-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-11-05 17:25:24-05:00
logcf7505da1f60757d9fabda20560191bba72ebdbe
tree709e6de0a46293752d9f3dcf67de9565eecc8a1b
parent78840c4ab20d844ca866fa0ab437d72fdec62d0d
parentb42ffbe9e82896c55c68412f99463f54c5e56f21
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6974 from daurnimator/prctl-cleanup

prctl improvements

2 files changed, 138 insertions(+), 53 deletions(-)

lib/std/os.zig+6-3
...@@ -5636,16 +5636,19 @@ pub const PrctlError = error{...@@ -5636,16 +5636,19 @@ pub const PrctlError = error{
5636 PermissionDenied,5636 PermissionDenied,
5637} || UnexpectedError;5637} || UnexpectedError;
56385638
5639pub fn prctl(option: i32, args: anytype) PrctlError!u31 {5639pub fn prctl(option: PR, args: anytype) PrctlError!u31 {
5640 if (@typeInfo(@TypeOf(args)) != .Struct)5640 if (@typeInfo(@TypeOf(args)) != .Struct)
5641 @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args)));5641 @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args)));
5642 if (args.len > 4)5642 if (args.len > 4)
5643 @compileError("prctl takes a maximum of 4 optional arguments");5643 @compileError("prctl takes a maximum of 4 optional arguments");
56445644
5645 var buf: [4]usize = undefined;5645 var buf: [4]usize = undefined;
5646 inline for (args) |arg, i| buf[i] = arg;5646 {
5647 comptime var i = 0;
5648 inline while (i < args.len) : (i += 1) buf[i] = args[i];
5649 }
56475650
5648 const rc = system.prctl(option, buf[0], buf[1], buf[2], buf[3]);5651 const rc = system.prctl(@enumToInt(option), buf[0], buf[1], buf[2], buf[3]);
5649 switch (errno(rc)) {5652 switch (errno(rc)) {
5650 0 => return @intCast(u31, rc),5653 0 => return @intCast(u31, rc),
5651 EACCES => return error.AccessDenied,5654 EACCES => return error.AccessDenied,
lib/std/os/bits/linux/prctl.zig+132-50
...@@ -4,27 +4,109 @@...@@ -4,27 +4,109 @@
4// The MIT license requires this copyright notice to be included in all copies4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.5// and substantial portions of the software.
66
7pub const PR_SET_PDEATHSIG = 1;7pub const PR = extern enum(i32) {
8pub const PR_GET_PDEATHSIG = 2;8 SET_PDEATHSIG = 1,
9 GET_PDEATHSIG = 2,
910
10pub const PR_GET_DUMPABLE = 3;11 GET_DUMPABLE = 3,
11pub const PR_SET_DUMPABLE = 4;12 SET_DUMPABLE = 4,
1213
13pub const PR_GET_UNALIGN = 5;14 GET_UNALIGN = 5,
14pub const PR_SET_UNALIGN = 6;15 SET_UNALIGN = 6,
16
17 GET_KEEPCAPS = 7,
18 SET_KEEPCAPS = 8,
19
20 GET_FPEMU = 9,
21 SET_FPEMU = 10,
22
23 GET_FPEXC = 11,
24 SET_FPEXC = 12,
25
26 GET_TIMING = 13,
27 SET_TIMING = 14,
28
29 SET_NAME = 15,
30 GET_NAME = 16,
31
32 GET_ENDIAN = 19,
33 SET_ENDIAN = 20,
34
35 GET_SECCOMP = 21,
36 SET_SECCOMP = 22,
37
38 CAPBSET_READ = 23,
39 CAPBSET_DROP = 24,
40
41 GET_TSC = 25,
42 SET_TSC = 26,
43
44 GET_SECUREBITS = 27,
45 SET_SECUREBITS = 28,
46
47 SET_TIMERSLACK = 29,
48 GET_TIMERSLACK = 30,
49
50 TASK_PERF_EVENTS_DISABLE = 31,
51 TASK_PERF_EVENTS_ENABLE = 32,
52
53 MCE_KILL = 33,
54
55 MCE_KILL_GET = 34,
56
57 SET_MM = 35,
58
59 SET_PTRACER = 0x59616d61,
60
61 SET_CHILD_SUBREAPER = 36,
62 GET_CHILD_SUBREAPER = 37,
63
64 SET_NO_NEW_PRIVS = 38,
65 GET_NO_NEW_PRIVS = 39,
66
67 GET_TID_ADDRESS = 40,
68
69 SET_THP_DISABLE = 41,
70 GET_THP_DISABLE = 42,
71
72 MPX_ENABLE_MANAGEMENT = 43,
73 MPX_DISABLE_MANAGEMENT = 44,
74
75 SET_FP_MODE = 45,
76 GET_FP_MODE = 46,
77
78 CAP_AMBIENT = 47,
79
80 SVE_SET_VL = 50,
81 SVE_GET_VL = 51,
82
83 GET_SPECULATION_CTRL = 52,
84 SET_SPECULATION_CTRL = 53,
85
86 _,
87};
88
89pub const PR_SET_PDEATHSIG = @enumToInt(PR.PR_SET_PDEATHSIG);
90pub const PR_GET_PDEATHSIG = @enumToInt(PR.PR_GET_PDEATHSIG);
91
92pub const PR_GET_DUMPABLE = @enumToInt(PR.PR_GET_DUMPABLE);
93pub const PR_SET_DUMPABLE = @enumToInt(PR.PR_SET_DUMPABLE);
94
95pub const PR_GET_UNALIGN = @enumToInt(PR.PR_GET_UNALIGN);
96pub const PR_SET_UNALIGN = @enumToInt(PR.PR_SET_UNALIGN);
15pub const PR_UNALIGN_NOPRINT = 1;97pub const PR_UNALIGN_NOPRINT = 1;
16pub const PR_UNALIGN_SIGBUS = 2;98pub const PR_UNALIGN_SIGBUS = 2;
1799
18pub const PR_GET_KEEPCAPS = 7;100pub const PR_GET_KEEPCAPS = @enumToInt(PR.PR_GET_KEEPCAPS);
19pub const PR_SET_KEEPCAPS = 8;101pub const PR_SET_KEEPCAPS = @enumToInt(PR.PR_SET_KEEPCAPS);
20102
21pub const PR_GET_FPEMU = 9;103pub const PR_GET_FPEMU = @enumToInt(PR.PR_GET_FPEMU);
22pub const PR_SET_FPEMU = 10;104pub const PR_SET_FPEMU = @enumToInt(PR.PR_SET_FPEMU);
23pub const PR_FPEMU_NOPRINT = 1;105pub const PR_FPEMU_NOPRINT = 1;
24pub const PR_FPEMU_SIGFPE = 2;106pub const PR_FPEMU_SIGFPE = 2;
25107
26pub const PR_GET_FPEXC = 11;108pub const PR_GET_FPEXC = @enumToInt(PR.PR_GET_FPEXC);
27pub const PR_SET_FPEXC = 12;109pub const PR_SET_FPEXC = @enumToInt(PR.PR_SET_FPEXC);
28pub const PR_FP_EXC_SW_ENABLE = 0x80;110pub const PR_FP_EXC_SW_ENABLE = 0x80;
29pub const PR_FP_EXC_DIV = 0x010000;111pub const PR_FP_EXC_DIV = 0x010000;
30pub const PR_FP_EXC_OVF = 0x020000;112pub const PR_FP_EXC_OVF = 0x020000;
...@@ -36,41 +118,41 @@ pub const PR_FP_EXC_NONRECOV = 1;...@@ -36,41 +118,41 @@ pub const PR_FP_EXC_NONRECOV = 1;
36pub const PR_FP_EXC_ASYNC = 2;118pub const PR_FP_EXC_ASYNC = 2;
37pub const PR_FP_EXC_PRECISE = 3;119pub const PR_FP_EXC_PRECISE = 3;
38120
39pub const PR_GET_TIMING = 13;121pub const PR_GET_TIMING = @enumToInt(PR.PR_GET_TIMING);
40pub const PR_SET_TIMING = 14;122pub const PR_SET_TIMING = @enumToInt(PR.PR_SET_TIMING);
41pub const PR_TIMING_STATISTICAL = 0;123pub const PR_TIMING_STATISTICAL = 0;
42pub const PR_TIMING_TIMESTAMP = 1;124pub const PR_TIMING_TIMESTAMP = 1;
43125
44pub const PR_SET_NAME = 15;126pub const PR_SET_NAME = @enumToInt(PR.PR_SET_NAME);
45pub const PR_GET_NAME = 16;127pub const PR_GET_NAME = @enumToInt(PR.PR_GET_NAME);
46128
47pub const PR_GET_ENDIAN = 19;129pub const PR_GET_ENDIAN = @enumToInt(PR.PR_GET_ENDIAN);
48pub const PR_SET_ENDIAN = 20;130pub const PR_SET_ENDIAN = @enumToInt(PR.PR_SET_ENDIAN);
49pub const PR_ENDIAN_BIG = 0;131pub const PR_ENDIAN_BIG = 0;
50pub const PR_ENDIAN_LITTLE = 1;132pub const PR_ENDIAN_LITTLE = 1;
51pub const PR_ENDIAN_PPC_LITTLE = 2;133pub const PR_ENDIAN_PPC_LITTLE = 2;
52134
53pub const PR_GET_SECCOMP = 21;135pub const PR_GET_SECCOMP = @enumToInt(PR.PR_GET_SECCOMP);
54pub const PR_SET_SECCOMP = 22;136pub const PR_SET_SECCOMP = @enumToInt(PR.PR_SET_SECCOMP);
55137
56pub const PR_CAPBSET_READ = 23;138pub const PR_CAPBSET_READ = @enumToInt(PR.PR_CAPBSET_READ);
57pub const PR_CAPBSET_DROP = 24;139pub const PR_CAPBSET_DROP = @enumToInt(PR.PR_CAPBSET_DROP);
58140
59pub const PR_GET_TSC = 25;141pub const PR_GET_TSC = @enumToInt(PR.PR_GET_TSC);
60pub const PR_SET_TSC = 26;142pub const PR_SET_TSC = @enumToInt(PR.PR_SET_TSC);
61pub const PR_TSC_ENABLE = 1;143pub const PR_TSC_ENABLE = 1;
62pub const PR_TSC_SIGSEGV = 2;144pub const PR_TSC_SIGSEGV = 2;
63145
64pub const PR_GET_SECUREBITS = 27;146pub const PR_GET_SECUREBITS = @enumToInt(PR.PR_GET_SECUREBITS);
65pub const PR_SET_SECUREBITS = 28;147pub const PR_SET_SECUREBITS = @enumToInt(PR.PR_SET_SECUREBITS);
66148
67pub const PR_SET_TIMERSLACK = 29;149pub const PR_SET_TIMERSLACK = @enumToInt(PR.PR_SET_TIMERSLACK);
68pub const PR_GET_TIMERSLACK = 30;150pub const PR_GET_TIMERSLACK = @enumToInt(PR.PR_GET_TIMERSLACK);
69151
70pub const PR_TASK_PERF_EVENTS_DISABLE = 31;152pub const PR_TASK_PERF_EVENTS_DISABLE = @enumToInt(PR.PR_TASK_PERF_EVENTS_DISABLE);
71pub const PR_TASK_PERF_EVENTS_ENABLE = 32;153pub const PR_TASK_PERF_EVENTS_ENABLE = @enumToInt(PR.PR_TASK_PERF_EVENTS_ENABLE);
72154
73pub const PR_MCE_KILL = 33;155pub const PR_MCE_KILL = @enumToInt(PR.PR_MCE_KILL);
74pub const PR_MCE_KILL_CLEAR = 0;156pub const PR_MCE_KILL_CLEAR = 0;
75pub const PR_MCE_KILL_SET = 1;157pub const PR_MCE_KILL_SET = 1;
76158
...@@ -78,9 +160,9 @@ pub const PR_MCE_KILL_LATE = 0;...@@ -78,9 +160,9 @@ pub const PR_MCE_KILL_LATE = 0;
78pub const PR_MCE_KILL_EARLY = 1;160pub const PR_MCE_KILL_EARLY = 1;
79pub const PR_MCE_KILL_DEFAULT = 2;161pub const PR_MCE_KILL_DEFAULT = 2;
80162
81pub const PR_MCE_KILL_GET = 34;163pub const PR_MCE_KILL_GET = @enumToInt(PR.PR_MCE_KILL_GET);
82164
83pub const PR_SET_MM = 35;165pub const PR_SET_MM = @enumToInt(PR.PR_SET_MM);
84pub const PR_SET_MM_START_CODE = 1;166pub const PR_SET_MM_START_CODE = 1;
85pub const PR_SET_MM_END_CODE = 2;167pub const PR_SET_MM_END_CODE = 2;
86pub const PR_SET_MM_START_DATA = 3;168pub const PR_SET_MM_START_DATA = 3;
...@@ -114,42 +196,42 @@ pub const prctl_mm_map = extern struct {...@@ -114,42 +196,42 @@ pub const prctl_mm_map = extern struct {
114 exe_fd: u32,196 exe_fd: u32,
115};197};
116198
117pub const PR_SET_PTRACER = 0x59616d61;199pub const PR_SET_PTRACER = @enumToInt(PR.PR_SET_PTRACER);
118pub const PR_SET_PTRACER_ANY = std.math.maxInt(c_ulong);200pub const PR_SET_PTRACER_ANY = std.math.maxInt(c_ulong);
119201
120pub const PR_SET_CHILD_SUBREAPER = 36;202pub const PR_SET_CHILD_SUBREAPER = @enumToInt(PR.PR_SET_CHILD_SUBREAPER);
121pub const PR_GET_CHILD_SUBREAPER = 37;203pub const PR_GET_CHILD_SUBREAPER = @enumToInt(PR.PR_GET_CHILD_SUBREAPER);
122204
123pub const PR_SET_NO_NEW_PRIVS = 38;205pub const PR_SET_NO_NEW_PRIVS = @enumToInt(PR.PR_SET_NO_NEW_PRIVS);
124pub const PR_GET_NO_NEW_PRIVS = 39;206pub const PR_GET_NO_NEW_PRIVS = @enumToInt(PR.PR_GET_NO_NEW_PRIVS);
125207
126pub const PR_GET_TID_ADDRESS = 40;208pub const PR_GET_TID_ADDRESS = @enumToInt(PR.PR_GET_TID_ADDRESS);
127209
128pub const PR_SET_THP_DISABLE = 41;210pub const PR_SET_THP_DISABLE = @enumToInt(PR.PR_SET_THP_DISABLE);
129pub const PR_GET_THP_DISABLE = 42;211pub const PR_GET_THP_DISABLE = @enumToInt(PR.PR_GET_THP_DISABLE);
130212
131pub const PR_MPX_ENABLE_MANAGEMENT = 43;213pub const PR_MPX_ENABLE_MANAGEMENT = @enumToInt(PR.PR_MPX_ENABLE_MANAGEMENT);
132pub const PR_MPX_DISABLE_MANAGEMENT = 44;214pub const PR_MPX_DISABLE_MANAGEMENT = @enumToInt(PR.PR_MPX_DISABLE_MANAGEMENT);
133215
134pub const PR_SET_FP_MODE = 45;216pub const PR_SET_FP_MODE = @enumToInt(PR.PR_SET_FP_MODE);
135pub const PR_GET_FP_MODE = 46;217pub const PR_GET_FP_MODE = @enumToInt(PR.PR_GET_FP_MODE);
136pub const PR_FP_MODE_FR = 1 << 0;218pub const PR_FP_MODE_FR = 1 << 0;
137pub const PR_FP_MODE_FRE = 1 << 1;219pub const PR_FP_MODE_FRE = 1 << 1;
138220
139pub const PR_CAP_AMBIENT = 47;221pub const PR_CAP_AMBIENT = @enumToInt(PR.PR_CAP_AMBIENT);
140pub const PR_CAP_AMBIENT_IS_SET = 1;222pub const PR_CAP_AMBIENT_IS_SET = 1;
141pub const PR_CAP_AMBIENT_RAISE = 2;223pub const PR_CAP_AMBIENT_RAISE = 2;
142pub const PR_CAP_AMBIENT_LOWER = 3;224pub const PR_CAP_AMBIENT_LOWER = 3;
143pub const PR_CAP_AMBIENT_CLEAR_ALL = 4;225pub const PR_CAP_AMBIENT_CLEAR_ALL = 4;
144226
145pub const PR_SVE_SET_VL = 50;227pub const PR_SVE_SET_VL = @enumToInt(PR.PR_SVE_SET_VL);
146pub const PR_SVE_SET_VL_ONEXEC = 1 << 18;228pub const PR_SVE_SET_VL_ONEXEC = 1 << 18;
147pub const PR_SVE_GET_VL = 51;229pub const PR_SVE_GET_VL = @enumToInt(PR.PR_SVE_GET_VL);
148pub const PR_SVE_VL_LEN_MASK = 0xffff;230pub const PR_SVE_VL_LEN_MASK = 0xffff;
149pub const PR_SVE_VL_INHERIT = 1 << 17;231pub const PR_SVE_VL_INHERIT = 1 << 17;
150232
151pub const PR_GET_SPECULATION_CTRL = 52;233pub const PR_GET_SPECULATION_CTRL = @enumToInt(PR.PR_GET_SPECULATION_CTRL);
152pub const PR_SET_SPECULATION_CTRL = 53;234pub const PR_SET_SPECULATION_CTRL = @enumToInt(PR.PR_SET_SPECULATION_CTRL);
153pub const PR_SPEC_STORE_BYPASS = 0;235pub const PR_SPEC_STORE_BYPASS = 0;
154pub const PR_SPEC_NOT_AFFECTED = 0;236pub const PR_SPEC_NOT_AFFECTED = 0;
155pub const PR_SPEC_PRCTL = 1 << 0;237pub const PR_SPEC_PRCTL = 1 << 0;