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 };
  30 
  31 /*
  32  * proc directories can do almost nothing..
  33  */
  34 struct inode_operations proc_fd_inode_operations = {
  35         &proc_fd_operations,    /* default base directory file-ops */
  36         NULL,                   /* create */
  37         proc_lookupfd,          /* lookup */
  38         NULL,                   /* link */
  39         NULL,                   /* unlink */
  40         NULL,                   /* symlink */
  41         NULL,                   /* mkdir */
  42         NULL,                   /* rmdir */
  43         NULL,                   /* mknod */
  44         NULL,                   /* rename */
  45         NULL,                   /* readlink */
  46         NULL,                   /* follow_link */
  47         NULL,                   /* bmap */
  48         NULL                    /* truncate */
  49 };
  50 
  51 static int proc_lookupfd(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
  52         struct inode ** result)
  53 {
  54         unsigned int ino, pid, fd, c;
  55         struct task_struct * p;
  56         struct super_block * sb;
  57         int i;
  58 
  59         *result = NULL;
  60         ino = dir->i_ino;
  61         pid = ino >> 16;
  62         ino &= 0x0000ffff;
  63         ino -= 7;
  64         if (!dir)
  65                 return -ENOENT;
  66         sb = dir->i_sb;
  67         if (!pid || ino > 1 || !S_ISDIR(dir->i_mode)) {
  68                 iput(dir);
  69                 return -ENOENT;
  70         }
  71         if (!len || (get_fs_byte(name) == '.' && (len == 1 ||
  72             (get_fs_byte(name+1) == '.' && len == 2)))) {
  73                 if (len < 2) {
  74                         *result = dir;
  75                         return 0;
  76                 }
  77                 if (!(*result = iget(sb,(pid << 16)+2))) {
  78                         iput(dir);
  79                         return -ENOENT;
  80                 }
  81                 iput(dir);
  82                 return 0;
  83         }
  84         iput(dir);
  85         fd = 0;
  86         while (len-- > 0) {
  87                 c = get_fs_byte(name) - '0';
  88                 name++;
  89                 if (c > 9) {
  90                         fd = 0xfffff;
  91                         break;
  92                 }
  93                 fd *= 10;
  94                 fd += c;
  95                 if (fd & 0xffff0000) {
  96                         fd = 0xfffff;
  97                         break;
  98                 }
  99         }
 100         for (i = 0 ; i < NR_TASKS ; i++)
 101                 if ((p = task[i]) && p->pid == pid)
 102                         break;
 103         if (!pid || i >= NR_TASKS)
 104                 return -ENOENT;
 105         if (!ino) {
 106                 if (fd >= NR_OPEN || !p->filp[fd] || !p->filp[fd]->f_inode)
 107                         return -ENOENT;
 108                 ino = (pid << 16) + 0x100 + fd;
 109         } else {
 110                 if (fd >= p->numlibraries)
 111                         return -ENOENT;
 112                 ino = (pid << 16) + 0x200 + fd;
 113         }
 114         if (!(*result = iget(sb,ino)))
 115                 return -ENOENT;
 116         return 0;
 117 }
 118 
 119 static int proc_readfd(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 120         struct dirent * dirent, int count)
 121 {
 122         struct task_struct * p;
 123         unsigned int fd, pid, ino;
 124         int i,j;
 125 
 126         if (!inode || !S_ISDIR(inode->i_mode))
 127                 return -EBADF;
 128         ino = inode->i_ino;
 129         pid = ino >> 16;
 130         ino &= 0x0000ffff;
 131         ino -= 7;
 132         if (ino > 1)
 133                 return 0;
 134         while (1) {
 135                 fd = filp->f_pos;
 136                 filp->f_pos++;
 137                 if (fd < 2) {
 138                         i = j = fd+1;
 139                         if (!fd)
 140                                 fd = inode->i_ino;
 141                         else
 142                                 fd = (inode->i_ino & 0xffff0000) | 2;
 143                         put_fs_long(fd, &dirent->d_ino);
 144                         put_fs_word(i, &dirent->d_reclen);
 145                         put_fs_byte(0, i+dirent->d_name);
 146                         while (i--)
 147                                 put_fs_byte('.', i+dirent->d_name);
 148                         return j;
 149                 }
 150                 fd -= 2;
 151                 for (i = 1 ; i < NR_TASKS ; i++)
 152                         if ((p = task[i]) && p->pid == pid)
 153                                 break;
 154                 if (i >= NR_TASKS)
 155                         return 0;
 156                 if (!ino) {
 157                         if (fd >= NR_OPEN)
 158                                 break;
 159                         if (!p->filp[fd] || !p->filp[fd]->f_inode)
 160                                 continue;
 161                 } else
 162                         if (fd >= p->numlibraries)
 163                                 break;
 164                 j = 10;
 165                 i = 1;
 166                 while (fd >= j) {
 167                         j *= 10;
 168                         i++;
 169                 }
 170                 j = i;
 171                 if (!ino)
 172                         ino = (pid << 16) + 0x100 + fd;
 173                 else
 174                         ino = (pid << 16) + 0x200 + fd;
 175                 put_fs_long(ino, &dirent->d_ino);
 176                 put_fs_word(i, &dirent->d_reclen);
 177                 put_fs_byte(0, i+dirent->d_name);
 178                 while (i--) {
 179                         put_fs_byte('0'+(fd % 10), i+dirent->d_name);
 180                         fd /= 10;
 181                 }
 182                 return j;
 183         }
 184         return 0;
 185 }

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