root/fs/proc/root.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. proc_lookuproot
  2. proc_readroot

   1 /*
   2  *  linux/fs/proc/root.c
   3  *
   4  *  Copyright (C) 1991, 1992 Linus Torvalds
   5  *
   6  *  proc root directory handling functions
   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 
  17 static int proc_readroot(struct inode *, struct file *, struct dirent *, int);
  18 static int proc_lookuproot(struct inode *,const char *,int,struct inode **);
  19 
  20 static struct file_operations proc_root_operations = {
  21         NULL,                   /* lseek - default */
  22         NULL,                   /* read - bad */
  23         NULL,                   /* write - bad */
  24         proc_readroot,          /* readdir */
  25         NULL,                   /* select - default */
  26         NULL,                   /* ioctl - default */
  27         NULL,                   /* mmap */
  28         NULL,                   /* no special open code */
  29         NULL,                   /* no special release code */
  30         NULL                    /* no fsync */
  31 };
  32 
  33 /*
  34  * proc directories can do almost nothing..
  35  */
  36 struct inode_operations proc_root_inode_operations = {
  37         &proc_root_operations,  /* default base directory file-ops */
  38         NULL,                   /* create */
  39         proc_lookuproot,        /* lookup */
  40         NULL,                   /* link */
  41         NULL,                   /* unlink */
  42         NULL,                   /* symlink */
  43         NULL,                   /* mkdir */
  44         NULL,                   /* rmdir */
  45         NULL,                   /* mknod */
  46         NULL,                   /* rename */
  47         NULL,                   /* readlink */
  48         NULL,                   /* follow_link */
  49         NULL,                   /* bmap */
  50         NULL,                   /* truncate */
  51         NULL                    /* permission */
  52 };
  53 
  54 static struct proc_dir_entry root_dir[] = {
  55         { PROC_ROOT_INO,        1, "." },
  56         { PROC_ROOT_INO,        2, ".." },
  57         { PROC_LOADAVG,         7, "loadavg" },
  58         { PROC_UPTIME,          6, "uptime" },
  59         { PROC_MEMINFO,         7, "meminfo" },
  60         { PROC_KMSG,            4, "kmsg" },
  61         { PROC_VERSION,         7, "version" },
  62         { PROC_CPUINFO,         7, "cpuinfo" },
  63         { PROC_SELF,            4, "self" },    /* will change inode # */
  64         { PROC_NET,             3, "net" },
  65 #ifdef CONFIG_DEBUG_MALLOC
  66         { PROC_MALLOC,          6, "malloc" },
  67 #endif
  68         { PROC_KCORE,           5, "kcore" },
  69         { PROC_MODULES,         7, "modules" },
  70         { PROC_STAT,            4, "stat" },
  71         { PROC_DEVICES,         7, "devices" },
  72         { PROC_INTERRUPTS,      10,"interrupts" },
  73         { PROC_FILESYSTEMS,     11,"filesystems" },
  74         { PROC_KSYMS,           5, "ksyms" },
  75         { PROC_DMA,             3, "dma" },
  76 };
  77 
  78 #define NR_ROOT_DIRENTRY ((sizeof (root_dir))/(sizeof (root_dir[0])))
  79 
  80 static int proc_lookuproot(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  81         struct inode ** result)
  82 {
  83         unsigned int pid, c;
  84         int i, ino;
  85 
  86         *result = NULL;
  87         if (!dir)
  88                 return -ENOENT;
  89         if (!S_ISDIR(dir->i_mode)) {
  90                 iput(dir);
  91                 return -ENOENT;
  92         }
  93         i = NR_ROOT_DIRENTRY;
  94         while (i-- > 0 && !proc_match(len,name,root_dir+i))
  95                 /* nothing */;
  96         if (i >= 0) {
  97                 ino = root_dir[i].low_ino;
  98                 if (ino == PROC_ROOT_INO) {
  99                         *result = dir;
 100                         return 0;
 101                 }
 102                 if (ino == PROC_SELF) /* self modifying inode ... */
 103                         ino = (current->pid << 16) + 2;
 104         } else {
 105                 pid = 0;
 106                 while (len-- > 0) {
 107                         c = *name - '0';
 108                         name++;
 109                         if (c > 9) {
 110                                 pid = 0;
 111                                 break;
 112                         }
 113                         pid *= 10;
 114                         pid += c;
 115                         if (pid & 0xffff0000) {
 116                                 pid = 0;
 117                                 break;
 118                         }
 119                 }
 120                 for (i = 0 ; i < NR_TASKS ; i++)
 121                         if (task[i] && task[i]->pid == pid)
 122                                 break;
 123                 if (!pid || i >= NR_TASKS) {
 124                         iput(dir);
 125                         return -ENOENT;
 126                 }
 127                 ino = (pid << 16) + 2;
 128         }
 129         if (!(*result = iget(dir->i_sb,ino))) {
 130                 iput(dir);
 131                 return -ENOENT;
 132         }
 133         iput(dir);
 134         return 0;
 135 }
 136 
 137 static int proc_readroot(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 138         struct dirent * dirent, int count)
 139 {
 140         struct task_struct * p;
 141         unsigned int nr,pid;
 142         int i,j;
 143 
 144         if (!inode || !S_ISDIR(inode->i_mode))
 145                 return -EBADF;
 146 repeat:
 147         nr = filp->f_pos;
 148         if (nr < NR_ROOT_DIRENTRY) {
 149                 struct proc_dir_entry * de = root_dir + nr;
 150 
 151                 filp->f_pos++;
 152                 i = de->namelen;
 153                 put_fs_long(de->low_ino, &dirent->d_ino);
 154                 put_fs_word(i,&dirent->d_reclen);
 155                 put_fs_byte(0,i+dirent->d_name);
 156                 j = i;
 157                 while (i--)
 158                         put_fs_byte(de->name[i], i+dirent->d_name);
 159                 return j;
 160         }
 161         nr -= NR_ROOT_DIRENTRY;
 162         if (nr >= NR_TASKS)
 163                 return 0;
 164         filp->f_pos++;
 165         p = task[nr];
 166         if (!p || !(pid = p->pid))
 167                 goto repeat;
 168         if (pid & 0xffff0000)
 169                 goto repeat;
 170         j = 10;
 171         i = 1;
 172         while (pid >= j) {
 173                 j *= 10;
 174                 i++;
 175         }
 176         j = i;
 177         put_fs_long((pid << 16)+2, &dirent->d_ino);
 178         put_fs_word(i, &dirent->d_reclen);
 179         put_fs_byte(0, i+dirent->d_name);
 180         while (i--) {
 181                 put_fs_byte('0'+(pid % 10), i+dirent->d_name);
 182                 pid /= 10;
 183         }
 184         return j;
 185 }

/* [previous][next][first][last][top][bottom][index][help] */