-
Notifications
You must be signed in to change notification settings - Fork 4
/
procfs.c
441 lines (404 loc) · 10.6 KB
/
procfs.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
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
#include "types.h"
#include "defs.h"
#include "mmu.h"
#include "fs.h"
#include "file.h"
#include "proc.h"
#include "stat.h"
#include "err.h"
#include "errno.h"
static void
itoa(char* dst, int value)
{
if (value < 0) {
*dst = '-';
value = -value;
dst++;
}
char* start = dst;
*dst++ = '0' + value % 10;
value /= 10;
while (value > 0) {
*dst++ = '0' + value % 10;
value /= 10;
}
*dst = '\0';
dst--;
while (dst > start)
{
char temp = *dst;
*dst = *start;
*start = temp;
start++;
dst--;
}
}
uint
atoi(char* s, int len)
{
uint current = 0;
for (int i = 0; s[i] && i < len; ++i)
{
if (!('0' <= s[i] && s[i] <= '9')) {
return 0;
} else {
current *= 10;
current += s[i] - '0';
}
}
return current;
}
static void
procfs_inode_update(struct inode* ip)
{ }
static int
procfs_inode_write(struct inode* ip, char* dst, uint off, uint n)
{
return -1;
}
static int
procfs_proc_file_name_read(struct inode* ip, char* dst, uint off, uint n);
static int
procfs_proc_file_state_read(struct inode* ip, char* dst, uint off, uint n);
static int
procfs_proc_file_memory_read(struct inode* ip, char* dst, uint off, uint n);
static int
procfs_proc_file_pid_read(struct inode* ip, char* dst, uint off, uint n);
static int
procfs_proc_file_uid_read(struct inode* ip, char* dst, uint off, uint n);
struct {
char* name;
int (*read)(struct inode*, char*, uint, uint);
} procfs_proc_files_table[] = {
{ ".", 0 },
{ "..", 0 },
{ "parent", 0 },
{ "name", procfs_proc_file_name_read },
{ "state", procfs_proc_file_state_read },
{ "memory", procfs_proc_file_memory_read },
{ "pid", procfs_proc_file_pid_read },
{ "uid", procfs_proc_file_uid_read },
};
#define N_PROC_ENTRIES (NELEM(procfs_proc_files_table) - 2 + 1)
#define FREE_PAGES_INUM 2
static int
procfs_proc_file_write(struct inode* ip, char* dst, uint off, uint n)
{
return -1;
}
void
init_procfs_proc_file(struct inode* ip)
{
ip->ops.update = procfs_inode_update;
ip->ops.write = procfs_proc_file_write;
ip->mode = (0444 | S_IFREG);
ip->flags = I_VALID;
ip->size = 0;
ip->uid = 0;
ip->gid = 0;
}
static void
read_str(char* src, uint length, char** dst, uint* off, uint n, uint* written)
{
if (*off >= length) return;
for (; *written < n && *off < length; ++*off, ++*written)
{
*((*dst)++) = src[*off];
}
}
static int
procfs_proc_dir_read(struct inode* ip, char* dst, uint off, uint n)
{
uint pid = ip->inum / N_PROC_ENTRIES;
uint count = 0;
for (int i = 0; i < NELEM(procfs_proc_files_table); ++i)
{
struct dirent entry;
strncpy(entry.name, procfs_proc_files_table[i].name, 14);
uint inum = pid * N_PROC_ENTRIES + i - 1;
if (namecmp(entry.name, ".") == 0) {
inum = ip->inum;
} else if (namecmp(entry.name, "..") == 0) {
inum = (uint)ip->additional_info;
} else if (namecmp(entry.name, "parent") == 0) {
struct proc* p = get_proc_by_pid(pid);
if (p->parent == 0) {
continue;
}
inum = p->parent->pid * N_PROC_ENTRIES;
}
struct inode* node = iget(find_fs(PROCDEV), inum);
if (i >= 3) {
init_procfs_proc_file(node);
node->ops.read = procfs_proc_files_table[i].read;
}
entry.inum = node->inum;
read_str((char*)&entry, sizeof(struct dirent), &dst, &off, n, &count);
if (off >= sizeof(struct dirent)) {
off -= sizeof(struct dirent);
}
}
return count;
}
static int
procfs_proc_dir_write(struct inode* ip, char* dst, uint off, uint n)
{
return -1;
}
static int
read_string(char* src, uint limit, char* dst, uint off, uint n)
{
uint len = safestrlen(src, limit);
uint last = off + n;
int count = 0;
for (uint i = off; i < len && i < last; ++i) {
*dst++ = src[i];
count++;
}
*dst = 0;
if (last > len && off <= len) {
*dst++ = '\n';
*dst = 0;
count++;
}
return count;
}
static int
procfs_proc_file_name_read(struct inode* ip, char* dst, uint off, uint n)
{
struct proc* p = get_proc_by_pid(ip->inum / N_PROC_ENTRIES);
if (p == 0) return 0;
char* str = p->name;
int len = safestrlen(str, NELEM(p->name));
return read_string(str, len, dst, off, n);
}
static int
procfs_proc_file_state_read(struct inode* ip, char* dst, uint off, uint n)
{
struct proc* p = get_proc_by_pid(ip->inum / N_PROC_ENTRIES);
if (p == 0) return 0;
char result[16];
switch (p->state) {
case UNUSED:
strncpy(result, "unused", 16);
break;
case EMBRYO:
strncpy(result, "embryo", 16);
break;
case SLEEPING:
strncpy(result, "sleeping", 16);
break;
case RUNNABLE:
strncpy(result, "runnable", 16);
break;
case RUNNING:
strncpy(result, "running", 16);
break;
case ZOMBIE:
strncpy(result, "zombie", 16);
break;
}
uint len = safestrlen(result, 16);
return read_string(result, len, dst, off, n);
}
static int
procfs_proc_file_memory_read(struct inode* ip, char* dst, uint off, uint n)
{
struct proc* p = get_proc_by_pid(ip->inum / N_PROC_ENTRIES);
if (p == 0) return 0;
char result[16];
itoa(result, p->mm->sz);
int len = strlen(result);
return read_string(result, len, dst, off, n);
}
static int
procfs_proc_file_pid_read(struct inode* ip, char* dst, uint off, uint n)
{
struct proc* p = get_proc_by_pid(ip->inum / N_PROC_ENTRIES);
if (p == 0) return 0;
char result[16];
itoa(result, p->pid);
int len = strlen(result);
return read_string(result, len, dst, off, n);
}
static int
procfs_proc_file_uid_read(struct inode* ip, char* dst, uint off, uint n)
{
struct proc* p = get_proc_by_pid(ip->inum / N_PROC_ENTRIES);
if (p == 0) return 0;
char result[16];
itoa(result, p->uid);
int len = strlen(result);
return read_string(result, len, dst, off, n);
}
static int
procfs_free_pages_read(struct inode* ip, char* dst, uint off, uint n)
{
char result[16];
itoa(result, free_pages_count);
int len = strlen(result);
return read_string(result, len, dst, off, n);
}
static void
init_procfs_proc_dir(struct inode* ip);
struct inode*
procfs_proc_dir_lookup(struct inode* ip, char* name, uint* poff)
{
struct inode* result = ERR_PTR(-ENOENT);
if (namecmp(name, ".") == 0) {
return idup(ip);
} else if (namecmp(name, "..") == 0) {
return iget(ip->fs, (uint)ip->additional_info);
} else if (namecmp(name, "parent") == 0) {
struct proc* p = get_proc_by_pid(ip->inum / N_PROC_ENTRIES);
if (p->parent == 0) return ERR_PTR(-ENOENT);
struct inode* parent = iget(ip->fs, (uint)p->parent->pid * N_PROC_ENTRIES);
init_procfs_proc_dir(parent);
return parent;
}
for (int i = 2; i < NELEM(procfs_proc_files_table); ++i)
{
if (namecmp(name, procfs_proc_files_table[i].name) != 0) {
continue;
}
result = iget(ip->fs, ip->inum + i - 1);
init_procfs_proc_file(result);
result->ops.read = procfs_proc_files_table[i].read;
break;
}
return result;
}
static void
init_procfs_proc_dir(struct inode* ip)
{
ip->ops.read = procfs_proc_dir_read;
ip->ops.write = procfs_proc_dir_write;
ip->ops.lookup = procfs_proc_dir_lookup;
ip->ops.update = procfs_inode_update;
ip->size = 0;
ip->flags = I_VALID;
ip->mode = (0555 | S_IFDIR);
ip->uid = 0;
ip->gid = 0;
ip->additional_info = (void*)1;
}
static void
init_procfs_free_pages(struct inode* ip)
{
ip->ops.read = procfs_free_pages_read;
ip->ops.write = procfs_inode_write;
ip->ops.update = procfs_inode_update;
ip->size = 0;
ip->flags = I_VALID;
ip->mode = (0444 | S_IFREG);
ip->uid = 0;
ip->gid = 0;
ip->additional_info = (void*)1;
}
static int
procfs_root_read(struct inode* ip, char* dst, uint off, uint n)
{
uint written = 0;
struct dirent entry;
strncpy(entry.name, ".", 2);
entry.inum = ip->inum;
read_str((char*)&entry, sizeof(struct dirent), &dst, &off, n, &written);
if (off >= sizeof(struct dirent)) {
off -= sizeof(struct dirent);
}
strncpy(entry.name, "..", 3);
entry.inum = (uint)ip->additional_info;
read_str((char*)&entry, sizeof(struct dirent), &dst, &off, n, &written);
if (off >= sizeof(struct dirent)) {
off -= sizeof(struct dirent);
}
acquire(&ptable.lock);
struct proc* p;
list_for_each_entry(p, &ptable.list, list) {
if (p->state == UNUSED) continue;
itoa(entry.name, p->pid);
struct inode* node = iget(find_fs(PROCDEV), p->pid * N_PROC_ENTRIES);
init_procfs_proc_dir(node);
entry.inum = ip->inum;
read_str((char*)&entry, sizeof(struct dirent), &dst, &off, n, &written);
if (off >= sizeof(struct dirent)) {
off -= sizeof(struct dirent);
}
}
release(&ptable.lock);
strncpy(entry.name, "self", 5);
entry.inum = proc->pid * N_PROC_ENTRIES;
read_str((char*)&entry, sizeof(struct dirent), &dst, &off, n, &written);
if (off >= sizeof(struct dirent)) {
off -= sizeof(struct dirent);
}
strncpy(entry.name, "free_pages", 11);
entry.inum = FREE_PAGES_INUM;
read_str((char*)&entry, sizeof(struct dirent), &dst, &off, n, &written);
if (off >= sizeof(struct dirent)) {
off -= sizeof(struct dirent);
}
return written;
}
static int
procfs_root_write(struct inode* ip, char* dst, uint off, uint n)
{
return -1;
}
struct inode*
procfs_root_lookup(struct inode* ip, char* name, uint* poff)
{
if (namecmp(name, ".") == 0) {
return idup(ip);
} else if (namecmp(name, "..") == 0) {
return iget(find_fs(ROOTDEV), (uint)ip->additional_info);
}
uint pid = atoi(name, 10);
if (pid == 0) {
if (namecmp(name, "free_pages") == 0) {
struct filesystem* fs = find_fs(PROCDEV);
struct inode* node = iget(fs, FREE_PAGES_INUM);
init_procfs_free_pages(node);
return node;
}
if (namecmp(name, "self") != 0) {
return ERR_PTR(-ENOENT);
}
pid = proc->pid;
}
acquire(&ptable.lock);
struct proc* p;
list_for_each_entry(p, &ptable.list, list) {
if (p->pid == pid) {
struct filesystem* fs = find_fs(PROCDEV);
struct inode* node = iget(fs, pid * N_PROC_ENTRIES);
init_procfs_proc_dir(node);
release(&ptable.lock);
return node;
}
}
release(&ptable.lock);
return ERR_PTR(-ENOENT);
}
int
mount_proc_fs(struct inode* ip, struct inode* parent)
{
ilock(ip);
iget(ip->fs, ip->inum);
ip->ops.read = procfs_root_read;
ip->ops.write = procfs_root_write;
ip->ops.lookup = procfs_root_lookup;
ip->ops.update = procfs_inode_update;
ip->additional_info = (void*)parent->inum;
struct inode* temp_ip = iget(find_fs(PROCDEV), 1);
temp_ip->mode = ip->mode;
temp_ip->additional_info = (void*)parent->inum;
temp_ip->ops = ip->ops;
temp_ip->fs = find_fs(PROCDEV);
temp_ip->inum = 1;
temp_ip->flags = I_VALID;
iunlock(ip);
// We should not do iput here.
// Otherwise, ip will be flushed out of the memory.
return 0;
}