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 *, void *, filldir_t);
  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 #ifdef CONFIG_PCI
  63         { PROC_PCI,             3, "pci"  },
  64 #endif
  65         { PROC_CPUINFO,         7, "cpuinfo" },
  66         { PROC_SELF,            4, "self" },    /* will change inode # */
  67         { PROC_NET,             3, "net" },
  68         { PROC_SCSI,            4, "scsi" },
  69 #ifdef CONFIG_DEBUG_MALLOC
  70         { PROC_MALLOC,          6, "malloc" },
  71 #endif
  72         { PROC_KCORE,           5, "kcore" },
  73         { PROC_MODULES,         7, "modules" },
  74         { PROC_STAT,            4, "stat" },
  75         { PROC_DEVICES,         7, "devices" },
  76         { PROC_INTERRUPTS,      10,"interrupts" },
  77         { PROC_FILESYSTEMS,     11,"filesystems" },
  78         { PROC_KSYMS,           5, "ksyms" },
  79         { PROC_DMA,             3, "dma" },
  80         { PROC_IOPORTS,         7, "ioports"},
  81 #ifdef CONFIG_PROFILE
  82         { PROC_PROFILE,         7, "profile"},
  83 #endif
  84 };
  85 
  86 #define NR_ROOT_DIRENTRY ((sizeof (root_dir))/(sizeof (root_dir[0])))
  87 
  88 static int proc_lookuproot(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  89         struct inode ** result)
  90 {
  91         unsigned int pid, c;
  92         int i, ino;
  93 
  94         *result = NULL;
  95         if (!dir)
  96                 return -ENOENT;
  97         if (!S_ISDIR(dir->i_mode)) {
  98                 iput(dir);
  99                 return -ENOENT;
 100         }
 101         i = NR_ROOT_DIRENTRY;
 102         while (i-- > 0 && !proc_match(len,name,root_dir+i))
 103                 /* nothing */;
 104         if (i >= 0) {
 105                 ino = root_dir[i].low_ino;
 106                 if (ino == PROC_ROOT_INO) {
 107                         *result = dir;
 108                         return 0;
 109                 }
 110                 if (ino == PROC_SELF) /* self modifying inode ... */
 111                         ino = (current->pid << 16) + 2;
 112         } else {
 113                 pid = 0;
 114                 while (len-- > 0) {
 115                         c = *name - '0';
 116                         name++;
 117                         if (c > 9) {
 118                                 pid = 0;
 119                                 break;
 120                         }
 121                         pid *= 10;
 122                         pid += c;
 123                         if (pid & 0xffff0000) {
 124                                 pid = 0;
 125                                 break;
 126                         }
 127                 }
 128                 for (i = 0 ; i < NR_TASKS ; i++)
 129                         if (task[i] && task[i]->pid == pid)
 130                                 break;
 131                 if (!pid || i >= NR_TASKS) {
 132                         iput(dir);
 133                         return -ENOENT;
 134                 }
 135                 ino = (pid << 16) + 2;
 136         }
 137         if (!(*result = iget(dir->i_sb,ino))) {
 138                 iput(dir);
 139                 return -ENOENT;
 140         }
 141         iput(dir);
 142         return 0;
 143 }
 144 
 145 #define NUMBUF 10
 146 
 147 static int proc_readroot(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 148         void * dirent, filldir_t filldir)
 149 {
 150         char buf[NUMBUF];
 151         unsigned int nr,pid;
 152         unsigned long i,j;
 153 
 154         if (!inode || !S_ISDIR(inode->i_mode))
 155                 return -EBADF;
 156 
 157         nr = filp->f_pos;
 158         while (nr < NR_ROOT_DIRENTRY) {
 159                 struct proc_dir_entry * de = root_dir + nr;
 160 
 161                 if (filldir(dirent, de->name, de->namelen, nr, de->low_ino) < 0)
 162                         return 0;
 163                 filp->f_pos++;
 164                 nr++;
 165         }
 166 
 167         for (nr -= NR_ROOT_DIRENTRY; nr < NR_TASKS; nr++, filp->f_pos++) {
 168                 struct task_struct * p = task[nr];
 169 
 170                 if (!p || !(pid = p->pid))
 171                         continue;
 172 
 173                 j = NUMBUF;
 174                 i = pid;
 175                 do {
 176                         j--;
 177                         buf[j] = '0' + (i % 10);
 178                         i /= 10;
 179                 } while (i);
 180 
 181                 if (filldir(dirent, buf+j, NUMBUF-j, nr+NR_ROOT_DIRENTRY, (pid << 16)+2) < 0)
 182                         break;
 183         }
 184         return 0;
 185 }

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