This source file includes following definitions.
- proc_register
- proc_unregister
- proc_self_followlink
- proc_self_readlink
- proc_root_init
- proc_match
- proc_lookup
- proc_root_lookup
- proc_readdir
- proc_root_readdir
1
2
3
4
5
6
7
8
9 #include <asm/segment.h>
10
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/proc_fs.h>
14 #include <linux/stat.h>
15 #include <linux/config.h>
16 #ifdef CONFIG_APM
17 #include <linux/apm_bios.h>
18 #endif
19
20
21
22
23 #define FIRST_PROCESS_ENTRY 256
24
25 static int proc_root_readdir(struct inode *, struct file *, void *, filldir_t);
26 static int proc_root_lookup(struct inode *,const char *,int,struct inode **);
27
28
29
30
31
32
33
34
35
36 static struct file_operations proc_dir_operations = {
37 NULL,
38 NULL,
39 NULL,
40 proc_readdir,
41 NULL,
42 NULL,
43 NULL,
44 NULL,
45 NULL,
46 NULL
47 };
48
49
50
51
52 static struct inode_operations proc_dir_inode_operations = {
53 &proc_dir_operations,
54 NULL,
55 proc_lookup,
56 NULL,
57 NULL,
58 NULL,
59 NULL,
60 NULL,
61 NULL,
62 NULL,
63 NULL,
64 NULL,
65 NULL,
66 NULL,
67 NULL,
68 NULL,
69 NULL
70 };
71
72
73
74
75
76
77 static struct file_operations proc_root_operations = {
78 NULL,
79 NULL,
80 NULL,
81 proc_root_readdir,
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 NULL,
87 NULL
88 };
89
90
91
92
93 static struct inode_operations proc_root_inode_operations = {
94 &proc_root_operations,
95 NULL,
96 proc_root_lookup,
97 NULL,
98 NULL,
99 NULL,
100 NULL,
101 NULL,
102 NULL,
103 NULL,
104 NULL,
105 NULL,
106 NULL,
107 NULL,
108 NULL,
109 NULL,
110 NULL
111 };
112
113
114
115
116 struct proc_dir_entry proc_root = {
117 PROC_ROOT_INO, 5, "/proc",
118 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
119 0, &proc_root_inode_operations,
120 NULL, NULL,
121 NULL,
122 &proc_root, NULL
123 };
124
125 struct proc_dir_entry proc_net = {
126 PROC_NET, 3, "net",
127 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
128 0, &proc_dir_inode_operations,
129 NULL, NULL,
130 NULL,
131 NULL, NULL
132 };
133
134 struct proc_dir_entry proc_scsi = {
135 PROC_SCSI, 4, "scsi",
136 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
137 0, &proc_dir_inode_operations,
138 NULL, NULL,
139 NULL, &proc_root, NULL
140 };
141
142 int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
143 {
144 dp->next = dir->subdir;
145 dp->parent = dir;
146 dir->subdir = dp;
147 if (S_ISDIR(dp->mode))
148 dir->nlink++;
149 return 0;
150 }
151
152 int proc_unregister(struct proc_dir_entry * dir, int ino)
153 {
154 struct proc_dir_entry **p = &dir->subdir, *dp;
155
156 while ((dp = *p) != NULL) {
157 if (dp->low_ino == ino) {
158 *p = dp->next;
159 dp->next = NULL;
160 if (S_ISDIR(dp->mode))
161 dir->nlink--;
162 return 0;
163 }
164 p = &dp->next;
165 }
166 return -EINVAL;
167 }
168
169
170
171
172 static int proc_self_followlink(struct inode * dir, struct inode * inode,
173 int flag, int mode, struct inode ** res_inode)
174 {
175 iput(dir);
176 *res_inode = proc_get_inode(inode->i_sb, (current->pid << 16) + PROC_PID_INO, &proc_pid);
177 iput(inode);
178 if (!*res_inode)
179 return -ENOENT;
180 return 0;
181 }
182
183 static int proc_self_readlink(struct inode * inode, char * buffer, int buflen)
184 {
185 int len;
186 char tmp[30];
187
188 iput(inode);
189 len = 1 + sprintf(tmp, "%d", current->pid);
190 if (buflen < len)
191 len = buflen;
192 memcpy_tofs(buffer, tmp, len);
193 return len;
194 }
195
196 static struct inode_operations proc_self_inode_operations = {
197 NULL,
198 NULL,
199 NULL,
200 NULL,
201 NULL,
202 NULL,
203 NULL,
204 NULL,
205 NULL,
206 NULL,
207 proc_self_readlink,
208 proc_self_followlink,
209 NULL,
210 NULL,
211 NULL,
212 NULL,
213 NULL
214 };
215
216 void proc_root_init(void)
217 {
218 static int done = 0;
219
220 if (done)
221 return;
222 done = 1;
223 proc_base_init();
224 proc_register(&proc_root, &(struct proc_dir_entry) {
225 PROC_LOADAVG, 7, "loadavg",
226 S_IFREG | S_IRUGO, 1, 0, 0,
227 });
228 proc_register(&proc_root, &(struct proc_dir_entry) {
229 PROC_UPTIME, 6, "uptime",
230 S_IFREG | S_IRUGO, 1, 0, 0,
231 });
232 proc_register(&proc_root, &(struct proc_dir_entry) {
233 PROC_MEMINFO, 7, "meminfo",
234 S_IFREG | S_IRUGO, 1, 0, 0,
235 });
236 proc_register(&proc_root, &(struct proc_dir_entry) {
237 PROC_KMSG, 4, "kmsg",
238 S_IFREG | S_IRUSR, 1, 0, 0,
239 });
240 proc_register(&proc_root, &(struct proc_dir_entry) {
241 PROC_VERSION, 7, "version",
242 S_IFREG | S_IRUGO, 1, 0, 0,
243 });
244 #ifdef CONFIG_PCI
245 proc_register(&proc_root, &(struct proc_dir_entry) {
246 PROC_PCI, 3, "pci",
247 S_IFREG | S_IRUGO, 1, 0, 0,
248 });
249 #endif
250 proc_register(&proc_root, &(struct proc_dir_entry) {
251 PROC_CPUINFO, 7, "cpuinfo",
252 S_IFREG | S_IRUGO, 1, 0, 0,
253 });
254 proc_register(&proc_root, &(struct proc_dir_entry) {
255 PROC_SELF, 4, "self",
256 S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO, 1, 0, 0,
257 64, &proc_self_inode_operations,
258 });
259 proc_register(&proc_root, &proc_net);
260
261 proc_register(&proc_root, &proc_scsi);
262
263 #ifdef CONFIG_DEBUG_MALLOC
264 proc_register(&proc_root, &(struct proc_dir_entry) {
265 PROC_MALLOC, 6, "malloc",
266 S_IFREG | S_IRUGO, 1, 0, 0,
267 });
268 #endif
269 proc_register(&proc_root, &(struct proc_dir_entry) {
270 PROC_KCORE, 5, "kcore",
271 S_IFREG | S_IRUSR, 1, 0, 0,
272 });
273 proc_register(&proc_root, &(struct proc_dir_entry) {
274 PROC_MODULES, 7, "modules",
275 S_IFREG | S_IRUGO, 1, 0, 0,
276 });
277 proc_register(&proc_root, &(struct proc_dir_entry) {
278 PROC_STAT, 4, "stat",
279 S_IFREG | S_IRUGO, 1, 0, 0,
280 });
281 proc_register(&proc_root, &(struct proc_dir_entry) {
282 PROC_DEVICES, 7, "devices",
283 S_IFREG | S_IRUGO, 1, 0, 0,
284 });
285 proc_register(&proc_root, &(struct proc_dir_entry) {
286 PROC_INTERRUPTS, 10,"interrupts",
287 S_IFREG | S_IRUGO, 1, 0, 0,
288 });
289 proc_register(&proc_root, &(struct proc_dir_entry) {
290 PROC_FILESYSTEMS, 11,"filesystems",
291 S_IFREG | S_IRUGO, 1, 0, 0,
292 });
293 proc_register(&proc_root, &(struct proc_dir_entry) {
294 PROC_KSYMS, 5, "ksyms",
295 S_IFREG | S_IRUGO, 1, 0, 0,
296 });
297 proc_register(&proc_root, &(struct proc_dir_entry) {
298 PROC_DMA, 3, "dma",
299 S_IFREG | S_IRUGO, 1, 0, 0,
300 });
301 proc_register(&proc_root, &(struct proc_dir_entry) {
302 PROC_IOPORTS, 7, "ioports",
303 S_IFREG | S_IRUGO, 1, 0, 0,
304 });
305 #ifdef CONFIG_APM
306 proc_register(&proc_root, &(struct proc_dir_entry) {
307 PROC_APM, 3, "apm",
308 S_IFREG | S_IRUGO, 1, 0, 0,
309 });
310 #endif
311 proc_register(&proc_root, &(struct proc_dir_entry) {
312 PROC_CMDLINE, 7, "cmdline",
313 S_IFREG | S_IRUGO, 1, 0, 0,
314 });
315
316 if (prof_shift) {
317 proc_register(&proc_root, &(struct proc_dir_entry) {
318 PROC_PROFILE, 7, "profile",
319 S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0,
320 });
321 }
322 }
323
324
325 int proc_match(int len,const char * name,struct proc_dir_entry * de)
326 {
327 if (!de || !de->low_ino)
328 return 0;
329
330 if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
331 return 1;
332 if (de->namelen != len)
333 return 0;
334 return !memcmp(name, de->name, len);
335 }
336
337 int proc_lookup(struct inode * dir,const char * name, int len,
338 struct inode ** result)
339 {
340 struct proc_dir_entry * de;
341 int ino;
342
343 *result = NULL;
344 if (!dir || !S_ISDIR(dir->i_mode)) {
345 iput(dir);
346 return -ENOTDIR;
347 }
348
349 de = (struct proc_dir_entry *) dir->u.generic_ip;
350 if (!de) {
351 iput(dir);
352 return -EINVAL;
353 }
354
355
356 *result = dir;
357 if (!len)
358 return 0;
359 if (name[0] == '.') {
360 if (len == 1)
361 return 0;
362 if (name[1] == '.' && len == 2) {
363 struct inode * inode;
364 inode = proc_get_inode(dir->i_sb, de->parent->low_ino, de->parent);
365 iput(dir);
366 if (!inode)
367 return -EINVAL;
368 *result = inode;
369 return 0;
370 }
371 }
372
373 *result = NULL;
374 for (de = de->subdir; de ; de = de->next) {
375 if (proc_match(len, name, de))
376 break;
377 }
378 if (!de) {
379 iput(dir);
380 return -ENOENT;
381 }
382
383 ino = de->low_ino | (dir->i_ino & ~(0xffff));
384
385 if (!(*result = proc_get_inode(dir->i_sb, ino, de))) {
386 iput(dir);
387 return -EINVAL;
388 }
389 iput(dir);
390 return 0;
391 }
392
393 static int proc_root_lookup(struct inode * dir,const char * name, int len,
394 struct inode ** result)
395 {
396 unsigned int pid, c;
397 int i, ino, retval;
398
399 dir->i_count++;
400 retval = proc_lookup(dir, name, len, result);
401 if (retval != -ENOENT) {
402 iput(dir);
403 return retval;
404 }
405
406 pid = 0;
407 while (len-- > 0) {
408 c = *name - '0';
409 name++;
410 if (c > 9) {
411 pid = 0;
412 break;
413 }
414 pid *= 10;
415 pid += c;
416 if (pid & 0xffff0000) {
417 pid = 0;
418 break;
419 }
420 }
421 for (i = 0 ; i < NR_TASKS ; i++)
422 if (task[i] && task[i]->pid == pid)
423 break;
424 if (!pid || i >= NR_TASKS) {
425 iput(dir);
426 return -ENOENT;
427 }
428 ino = (pid << 16) + PROC_PID_INO;
429 if (!(*result = proc_get_inode(dir->i_sb, ino, &proc_pid))) {
430 iput(dir);
431 return -EINVAL;
432 }
433 iput(dir);
434 return 0;
435 }
436
437
438
439
440
441
442
443
444
445
446 int proc_readdir(struct inode * inode, struct file * filp,
447 void * dirent, filldir_t filldir)
448 {
449 struct proc_dir_entry * de;
450 unsigned int ino;
451 int i;
452
453 if (!inode || !S_ISDIR(inode->i_mode))
454 return -ENOTDIR;
455 ino = inode->i_ino;
456 de = (struct proc_dir_entry *) inode->u.generic_ip;
457 if (!de)
458 return -EINVAL;
459 i = filp->f_pos;
460 switch (i) {
461 case 0:
462 if (filldir(dirent, ".", 1, i, ino) < 0)
463 return 0;
464 i++;
465 filp->f_pos++;
466
467 case 1:
468 if (filldir(dirent, "..", 2, i, de->parent->low_ino) < 0)
469 return 0;
470 i++;
471 filp->f_pos++;
472
473 default:
474 ino &= ~0xffff;
475 de = de->subdir;
476 i -= 2;
477 for (;;) {
478 if (!de)
479 return 1;
480 if (!i)
481 break;
482 de = de->next;
483 i--;
484 }
485
486 do {
487 if (filldir(dirent, de->name, de->namelen, filp->f_pos, ino | de->low_ino) < 0)
488 return 0;
489 filp->f_pos++;
490 de = de->next;
491 } while (de);
492 }
493 return 1;
494 }
495
496 #define NUMBUF 10
497
498 static int proc_root_readdir(struct inode * inode, struct file * filp,
499 void * dirent, filldir_t filldir)
500 {
501 char buf[NUMBUF];
502 unsigned int nr,pid;
503 unsigned long i,j;
504
505 nr = filp->f_pos;
506 if (nr < FIRST_PROCESS_ENTRY) {
507 int error = proc_readdir(inode, filp, dirent, filldir);
508 if (error <= 0)
509 return error;
510 filp->f_pos = nr = FIRST_PROCESS_ENTRY;
511 }
512
513 for (nr -= FIRST_PROCESS_ENTRY; nr < NR_TASKS; nr++, filp->f_pos++) {
514 struct task_struct * p = task[nr];
515
516 if (!p || !(pid = p->pid))
517 continue;
518
519 j = NUMBUF;
520 i = pid;
521 do {
522 j--;
523 buf[j] = '0' + (i % 10);
524 i /= 10;
525 } while (i);
526
527 if (filldir(dirent, buf+j, NUMBUF-j, filp->f_pos, (pid << 16) + PROC_PID_INO) < 0)
528 break;
529 }
530 return 0;
531 }