root/fs/proc/base.c

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

DEFINITIONS

This source file includes following definitions.
  1. proc_match
  2. proc_lookupbase
  3. proc_readbase

   1 /*
   2  *  linux/fs/proc/base.c
   3  *
   4  *  Copyright (C) 1991, 1992 Linus Torvalds
   5  *
   6  *  proc base 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 
  16 static int proc_readbase(struct inode *, struct file *, struct dirent *, int);
  17 static int proc_lookupbase(struct inode *,const char *,int,struct inode **);
  18 
  19 static struct file_operations proc_base_operations = {
  20         NULL,                   /* lseek - default */
  21         NULL,                   /* read - bad */
  22         NULL,                   /* write - bad */
  23         proc_readbase,          /* readdir */
  24         NULL,                   /* select - default */
  25         NULL,                   /* ioctl - default */
  26         NULL,                   /* mmap */
  27         NULL,                   /* no special open code */
  28         NULL,                   /* no special release code */
  29         NULL                    /* can't fsync */
  30 };
  31 
  32 /*
  33  * proc directories can do almost nothing..
  34  */
  35 struct inode_operations proc_base_inode_operations = {
  36         &proc_base_operations,  /* default base directory file-ops */
  37         NULL,                   /* create */
  38         proc_lookupbase,        /* lookup */
  39         NULL,                   /* link */
  40         NULL,                   /* unlink */
  41         NULL,                   /* symlink */
  42         NULL,                   /* mkdir */
  43         NULL,                   /* rmdir */
  44         NULL,                   /* mknod */
  45         NULL,                   /* rename */
  46         NULL,                   /* readlink */
  47         NULL,                   /* follow_link */
  48         NULL,                   /* bmap */
  49         NULL,                   /* truncate */
  50         NULL                    /* permission */
  51 };
  52 
  53 static struct proc_dir_entry base_dir[] = {
  54         { 1,2,".." },
  55         { 2,1,"." },
  56         { 3,3,"mem" },
  57         { 4,3,"cwd" },
  58         { 5,4,"root" },
  59         { 6,3,"exe" },
  60         { 7,2,"fd" },
  61         { 8,4,"mmap" },
  62         { 9,7,"environ" },
  63         { 10,7,"cmdline" },
  64         { 11,4,"stat" },
  65         { 12,5,"statm" },
  66         { 15,4,"maps" }
  67 };
  68 
  69 #define NR_BASE_DIRENTRY ((sizeof (base_dir))/(sizeof (base_dir[0])))
  70 
  71 int proc_match(int len,const char * name,struct proc_dir_entry * de)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73         register int same;
  74 
  75         if (!de || !de->low_ino)
  76                 return 0;
  77         /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
  78         if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
  79                 return 1;
  80         if (de->namelen != len)
  81                 return 0;
  82         __asm__ __volatile__(
  83                 "cld\n\t"
  84                 "repe ; cmpsb\n\t"
  85                 "setz %%al"
  86                 :"=a" (same)
  87                 :"0" (0),"S" ((long) name),"D" ((long) de->name),"c" (len)
  88                 :"cx","di","si");
  89         return same;
  90 }
  91 
  92 static int proc_lookupbase(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  93         struct inode ** result)
  94 {
  95         unsigned int pid, ino;
  96         int i;
  97 
  98         *result = NULL;
  99         if (!dir)
 100                 return -ENOENT;
 101         if (!S_ISDIR(dir->i_mode)) {
 102                 iput(dir);
 103                 return -ENOENT;
 104         }
 105         ino = dir->i_ino;
 106         pid = ino >> 16;
 107         i = NR_BASE_DIRENTRY;
 108         while (i-- > 0 && !proc_match(len,name,base_dir+i))
 109                 /* nothing */;
 110         if (i < 0) {
 111                 iput(dir);
 112                 return -ENOENT;
 113         }
 114         if (base_dir[i].low_ino == 1)
 115                 ino = 1;
 116         else
 117                 ino = (pid << 16) + base_dir[i].low_ino;
 118         for (i = 0 ; i < NR_TASKS ; i++)
 119                 if (task[i] && task[i]->pid == pid)
 120                         break;
 121         if (!pid || i >= NR_TASKS) {
 122                 iput(dir);
 123                 return -ENOENT;
 124         }
 125         if (!(*result = iget(dir->i_sb,ino))) {
 126                 iput(dir);
 127                 return -ENOENT;
 128         }
 129         iput(dir);
 130         return 0;
 131 }
 132 
 133 static int proc_readbase(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 134         struct dirent * dirent, int count)
 135 {
 136         struct proc_dir_entry * de;
 137         unsigned int pid, ino;
 138         int i,j;
 139 
 140         if (!inode || !S_ISDIR(inode->i_mode))
 141                 return -EBADF;
 142         ino = inode->i_ino;
 143         pid = ino >> 16;
 144         for (i = 0 ; i < NR_TASKS ; i++)
 145                 if (task[i] && task[i]->pid == pid)
 146                         break;
 147         if (!pid || i >= NR_TASKS)
 148                 return 0;
 149         if (((unsigned) filp->f_pos) < NR_BASE_DIRENTRY) {
 150                 de = base_dir + filp->f_pos;
 151                 filp->f_pos++;
 152                 i = de->namelen;
 153                 ino = de->low_ino;
 154                 if (ino != 1)
 155                         ino |= (pid << 16);
 156                 put_fs_long(ino, &dirent->d_ino);
 157                 put_fs_word(i,&dirent->d_reclen);
 158                 put_fs_byte(0,i+dirent->d_name);
 159                 j = i;
 160                 while (i--)
 161                         put_fs_byte(de->name[i], i+dirent->d_name);
 162                 return j;
 163         }
 164         return 0;
 165 }

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