root/fs/proc/fd.c

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

DEFINITIONS

This source file includes following definitions.
  1. proc_lookupfd
  2. proc_readfd

   1 /*
   2  *  linux/fs/proc/fd.c
   3  *
   4  *  Copyright (C) 1991, 1992 Linus Torvalds
   5  *
   6  *  proc fd 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_readfd(struct inode *, struct file *, struct dirent *, int);
  17 static int proc_lookupfd(struct inode *,const char *,int,struct inode **);
  18 
  19 static struct file_operations proc_fd_operations = {
  20         NULL,                   /* lseek - default */
  21         NULL,                   /* read - bad */
  22         NULL,                   /* write - bad */
  23         proc_readfd,            /* 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_fd_inode_operations = {
  36         &proc_fd_operations,    /* default base directory file-ops */
  37         NULL,                   /* create */
  38         proc_lookupfd,          /* 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 int proc_lookupfd(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  54         struct inode ** result)
  55 {
  56         unsigned int ino, pid, fd, c;
  57         struct task_struct * p;
  58         struct super_block * sb;
  59         int i;
  60 
  61         *result = NULL;
  62         ino = dir->i_ino;
  63         pid = ino >> 16;
  64         ino &= 0x0000ffff;
  65         ino -= 7;
  66         if (!dir)
  67                 return -ENOENT;
  68         sb = dir->i_sb;
  69         if (!pid || ino > 1 || !S_ISDIR(dir->i_mode)) {
  70                 iput(dir);
  71                 return -ENOENT;
  72         }
  73         if (!len || (name[0] == '.' && (len == 1 ||
  74             (name[1] == '.' && len == 2)))) {
  75                 if (len < 2) {
  76                         *result = dir;
  77                         return 0;
  78                 }
  79                 if (!(*result = iget(sb,(pid << 16)+2))) {
  80                         iput(dir);
  81                         return -ENOENT;
  82                 }
  83                 iput(dir);
  84                 return 0;
  85         }
  86         iput(dir);
  87         fd = 0;
  88         while (len-- > 0) {
  89                 c = *name - '0';
  90                 name++;
  91                 if (c > 9) {
  92                         fd = 0xfffff;
  93                         break;
  94                 }
  95                 fd *= 10;
  96                 fd += c;
  97                 if (fd & 0xffff0000) {
  98                         fd = 0xfffff;
  99                         break;
 100                 }
 101         }
 102         for (i = 0 ; i < NR_TASKS ; i++)
 103                 if ((p = task[i]) && p->pid == pid)
 104                         break;
 105         if (!pid || i >= NR_TASKS)
 106                 return -ENOENT;
 107         if (!ino) {
 108                 if (fd >= NR_OPEN || !p->filp[fd] || !p->filp[fd]->f_inode)
 109                         return -ENOENT;
 110                 ino = (pid << 16) + 0x100 + fd;
 111         } else {
 112                 if (fd >= p->numlibraries)
 113                         return -ENOENT;
 114                 ino = (pid << 16) + 0x200 + fd;
 115         }
 116         if (!(*result = iget(sb,ino)))
 117                 return -ENOENT;
 118         return 0;
 119 }
 120 
 121 static int proc_readfd(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 122         struct dirent * dirent, int count)
 123 {
 124         struct task_struct * p;
 125         unsigned int fd, pid, ino;
 126         int i,j;
 127 
 128         if (!inode || !S_ISDIR(inode->i_mode))
 129                 return -EBADF;
 130         ino = inode->i_ino;
 131         pid = ino >> 16;
 132         ino &= 0x0000ffff;
 133         ino -= 7;
 134         if (ino > 1)
 135                 return 0;
 136         while (1) {
 137                 fd = filp->f_pos;
 138                 filp->f_pos++;
 139                 if (fd < 2) {
 140                         i = j = fd+1;
 141                         if (!fd)
 142                                 fd = inode->i_ino;
 143                         else
 144                                 fd = (inode->i_ino & 0xffff0000) | 2;
 145                         put_fs_long(fd, &dirent->d_ino);
 146                         put_fs_word(i, &dirent->d_reclen);
 147                         put_fs_byte(0, i+dirent->d_name);
 148                         while (i--)
 149                                 put_fs_byte('.', i+dirent->d_name);
 150                         return j;
 151                 }
 152                 fd -= 2;
 153                 for (i = 1 ; i < NR_TASKS ; i++)
 154                         if ((p = task[i]) && p->pid == pid)
 155                                 break;
 156                 if (i >= NR_TASKS)
 157                         return 0;
 158                 if (!ino) {
 159                         if (fd >= NR_OPEN)
 160                                 break;
 161                         if (!p->filp[fd] || !p->filp[fd]->f_inode)
 162                                 continue;
 163                 } else
 164                         if (fd >= p->numlibraries)
 165                                 break;
 166                 j = 10;
 167                 i = 1;
 168                 while (fd >= j) {
 169                         j *= 10;
 170                         i++;
 171                 }
 172                 j = i;
 173                 if (!ino)
 174                         ino = (pid << 16) + 0x100 + fd;
 175                 else
 176                         ino = (pid << 16) + 0x200 + fd;
 177                 put_fs_long(ino, &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'+(fd % 10), i+dirent->d_name);
 182                         fd /= 10;
 183                 }
 184                 return j;
 185         }
 186         return 0;
 187 }

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