This repository was archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux.c
336 lines (281 loc) · 8.41 KB
/
linux.c
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
#include "lib.h"
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h> // malloc
#include <stdint.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h> // PATH_MAX
#include <fcntl.h> // openat
#define LEN(s) sizeof(s) - 1
#define S_PROC_PATH "/proc/"
#define S_MAX_PID "4194304"
#ifndef TASK_COMM_LEN
#define TASK_COMM_LEN 16
#define S_I32_MAX "2147483647"
#define S_U32_MAX "4294967295"
#define S_I64_MAX "9223372036854775807"
#define S_U64_MAX "18446744073709551615"
#endif
static char proc_path[LEN(S_PROC_PATH) + LEN(S_MAX_PID) + 1] = "/proc/";
char *path_buf;
napi_value init_all(napi_env const env, napi_value exports) {
path_buf = malloc(PATH_MAX);
if (path_buf == NULL) {
napi_throw_error(env, "ENOMEM", "Allocating PATH_MAX bytes failed!");
return NULL;
}
if (napi_create_function(env, "ps_list", sizeof("ps_list"), ps_list, NULL, &exports) != napi_ok) {
napi_throw_error(env, "ENOCREATE", "Could not export ps_list!");
return NULL;
}
return exports;
}
napi_value ps_list(napi_env const restrict env, napi_callback_info const restrict info) {
(void) info;
napi_value ary;
if (napi_create_array(env, &ary) != napi_ok) {
napi_throw_error(env, "ENOCREATE", "Could not make output array!");
return NULL;
}
DIR *const restrict proc = opendir(proc_path);
if (proc == NULL) {
napi_throw_error(env, "EACCESS", "Could not open /proc/!");
return NULL;
}
size_t ary_idx = 0;
napi_value obj;
napi_value val;
struct dirent *restrict dirent;
while ((dirent = readdir(proc))) {
if (napi_create_object(env, &obj) != napi_ok) {
return ary;
}
// pid
{
uint32_t pid = 0;
// simultaniously check if the directory is a PID, copy it into the
// proc_path, and parse it's name as a u32.
char *c = dirent->d_name;
char *proc_path6 = proc_path + 6;
while (*c) {
if (*c < '0' ||'9' < *c) {
goto next_dirent;
}
pid *= 10;
pid += *c - '0';
*proc_path6++ = *c++;
}
*proc_path6 = '\0';
if (napi_create_uint32(env, pid, &val) != napi_ok) {
return ary;
}
if (napi_set_named_property(env, obj, "id", val) != napi_ok) {
return ary;
}
}
DIR *const restrict cur_dir = opendir(proc_path);
if (cur_dir == NULL) {
goto next_dirent;
}
int const cur_dirfd = dirfd(cur_dir);
int const statfd = openat(cur_dirfd, "stat", O_RDONLY);
if (statfd < 0) {
goto die_cur_dir;
}
char stat_buf[0
+ LEN(S_MAX_PID) // 01 pid
+ LEN(" ")
+ LEN("(")
+ TASK_COMM_LEN // 02 comm
+ LEN(")")
+ LEN(" ")
+ LEN("?") // 03 state
+ LEN(" ")
+ LEN(S_MAX_PID) // 04 ppid
+ LEN(" ")
+ LEN(S_MAX_PID) // 05 pgid
+ LEN(" ")
+ LEN(S_I32_MAX) // 06 session id
+ LEN(" ")
+ LEN(S_I32_MAX) // 07 tty_nr
+ LEN(" ")
+ LEN(S_I32_MAX) // 08 tpgid
+ LEN(" ")
+ LEN(S_U32_MAX) // 09 flags
+ LEN(" ")
+ LEN(S_U32_MAX) // 10 minflt
+ LEN(" ")
+ LEN(S_U32_MAX) // 11 cminflt
+ LEN(" ")
+ LEN(S_U32_MAX) // 12 majflt
+ LEN(" ")
+ LEN(S_U32_MAX) // 13 cmajflt
+ LEN(" ")
+ LEN(S_U32_MAX) // 14 utime
+ LEN(" ")
+ LEN(S_U32_MAX) // 15 stime
+ LEN(" ")
+ LEN(S_I32_MAX) // 16 cutime
+ LEN(" ")
+ LEN(S_I32_MAX) // 17 cstime
+ LEN(" ")
+ LEN(S_I32_MAX) // 18 priority
+ LEN(" ")
+ LEN(S_I32_MAX) // 19 nice
+ LEN(" ")
+ LEN(S_I32_MAX) // 20 num_threads
];
char *stat_buf_end;
{
ssize_t bytes_read = read(statfd, stat_buf, sizeof(stat_buf) - 1);
if (bytes_read == -1) {
goto die_statfd;
}
stat_buf_end = stat_buf + bytes_read;
}
// manually inlined function
// to invoke, set delim and return_address
// result is stored in peek_count
char const *stat_cursor = stat_buf;
void *return_address = &&comm;
next_word:;
while (*stat_cursor++ != ' ') {
if (stat_cursor == stat_buf_end) {
// you're not supposed to do that
napi_throw_error(env, "EMEM", "stat_cursor == stat_buf_end");
return NULL;
}
}
goto *return_address;
// 02 comm
{
comm:
//if (stat_cursor[0] == '(') {
stat_cursor++;
//} else {
//goto die_statfd;
//}
size_t comm_len = 0;
while (stat_cursor[comm_len] != ')') comm_len++;
if (napi_create_string_utf8(env, stat_cursor, comm_len, &val) != napi_ok) {
goto die_statfd;
}
if (napi_set_named_property(env, obj, "name", val) != napi_ok) {
goto die_statfd;
}
// (init) S
// ^ ^
// ^ stat_cursor + comm_len
// stat_cursor
// first go to closing paren, then jump twice to land on status
stat_cursor += comm_len + LEN(") ");
}
// 03 state
{
if (napi_create_string_utf8(env, stat_cursor, 1, &val) != napi_ok) {
goto die_statfd;
}
if (napi_set_named_property(env, obj, "state", val) != napi_ok) {
goto die_statfd;
}
// (init) S 1
// ^
// stat_cursor
// increment twice to land on ppid
stat_cursor += LEN("S ");
}
// 04 ppid
{
uint32_t ppid = 0;
while (*stat_cursor != ' ') {
// assumed: '0' <= *stat_cursor <= '9'
ppid *= 10;
ppid += *stat_cursor - '0';
stat_cursor++;
}
// cursor is currently on space, increment once to land on pgrp
stat_cursor += LEN(" ");
if (napi_create_uint32(env, ppid, &val) != napi_ok) {
goto die_statfd;
}
if (napi_set_named_property(env, obj, "ppid", val) != napi_ok) {
goto die_statfd;
}
}
// 05 pgrp
// 06 session
// 07 tty_nr
// 08 tpgid
// 09 flags
// 10 minflt
// 11 cminflt
// 12 majflt
// 13 cmajflt
// 14 utime
// 15 stime
// 16 cutime
// 17 cstime
{
size_t words_to_skip = 13;
pgrp_to_cstime:
return_address = &&pgrp_to_cstime;
// FIXME: extra compare for no reason
while (words_to_skip --> 0) goto next_word;
}
// 18 priority
{
int32_t priority = 0;
while (*stat_cursor != ' ') {
// assumed: '0' <= *stat_cursor <= '9'
priority *= 10;
priority += *stat_cursor - '0';
stat_cursor++;
}
// cursor is currently on space, increment once to get to nice
stat_cursor += LEN(" ");
if (napi_create_int32(env, priority, &val) != napi_ok) {
goto die_statfd;
}
if (napi_set_named_property(env, obj, "priority", val) != napi_ok) {
goto die_statfd;
}
}
// 19 nice
return_address = &&num_threads;
goto next_word;
// 20 num_threads
num_threads: {
int32_t threads = 0;
while (*stat_cursor != ' ') {
// assumed: '0' <= *stat_cursor <= '9'
threads *= 10;
threads += *stat_cursor - '0';
stat_cursor++;
}
if (napi_create_int32(env, threads, &val) != napi_ok) {
goto die_statfd;
}
if (napi_set_named_property(env, obj, "threads", val) != napi_ok) {
goto die_statfd;
}
}
ssize_t len = readlinkat(cur_dirfd, "exe", path_buf, PATH_MAX);
if (len == -1) {
goto die_statfd;
}
path_buf[len] = '\0';
if (napi_create_string_utf8(env, path_buf, len, &val) != napi_ok) {
goto die_statfd;
}
if (napi_set_named_property(env, obj, "path", val) != napi_ok) {
goto die_statfd;
}
napi_set_element(env, ary, ary_idx++, obj);
die_statfd:
close(statfd);
die_cur_dir:
closedir(cur_dir);
next_dirent:;
}
return ary;
}