root/fs/proc/root.c

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

DEFINITIONS

This source file includes following definitions.
  1. proc_register
  2. proc_unregister
  3. proc_self_followlink
  4. proc_self_readlink
  5. proc_root_init
  6. proc_match
  7. proc_lookup
  8. proc_root_lookup
  9. proc_readdir
  10. proc_root_readdir

   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 #ifdef CONFIG_APM
  17 #include <linux/apm_bios.h>
  18 #endif
  19 
  20 /*
  21  * Offset of the first process in the /proc root directory..
  22  */
  23 #define FIRST_PROCESS_ENTRY 256
  24 
  25 static int proc_root_readdir(struct inode *, struct file *, void *, filldir_t);
  26 static int proc_root_lookup(struct inode *,const char *,int,struct inode **);
  27 
  28 /*
  29  * These are the generic /proc directory operations. They
  30  * use the in-memory "struct proc_dir_entry" tree to parse
  31  * the /proc directory.
  32  *
  33  * NOTE! The /proc/scsi directory currently does not correctly
  34  * build up the proc_dir_entry tree, and will show up empty.
  35  */
  36 static struct file_operations proc_dir_operations = {
  37         NULL,                   /* lseek - default */
  38         NULL,                   /* read - bad */
  39         NULL,                   /* write - bad */
  40         proc_readdir,           /* readdir */
  41         NULL,                   /* select - default */
  42         NULL,                   /* ioctl - default */
  43         NULL,                   /* mmap */
  44         NULL,                   /* no special open code */
  45         NULL,                   /* no special release code */
  46         NULL                    /* can't fsync */
  47 };
  48 
  49 /*
  50  * proc directories can do almost nothing..
  51  */
  52 static struct inode_operations proc_dir_inode_operations = {
  53         &proc_dir_operations,   /* default net directory file-ops */
  54         NULL,                   /* create */
  55         proc_lookup,            /* lookup */
  56         NULL,                   /* link */
  57         NULL,                   /* unlink */
  58         NULL,                   /* symlink */
  59         NULL,                   /* mkdir */
  60         NULL,                   /* rmdir */
  61         NULL,                   /* mknod */
  62         NULL,                   /* rename */
  63         NULL,                   /* readlink */
  64         NULL,                   /* follow_link */
  65         NULL,                   /* readpage */
  66         NULL,                   /* writepage */
  67         NULL,                   /* bmap */
  68         NULL,                   /* truncate */
  69         NULL                    /* permission */
  70 };
  71 
  72 /*
  73  * The root /proc directory is special, as it has the
  74  * <pid> directories. Thus we don't use the generic
  75  * directory handling functions for that..
  76  */
  77 static struct file_operations proc_root_operations = {
  78         NULL,                   /* lseek - default */
  79         NULL,                   /* read - bad */
  80         NULL,                   /* write - bad */
  81         proc_root_readdir,      /* readdir */
  82         NULL,                   /* select - default */
  83         NULL,                   /* ioctl - default */
  84         NULL,                   /* mmap */
  85         NULL,                   /* no special open code */
  86         NULL,                   /* no special release code */
  87         NULL                    /* no fsync */
  88 };
  89 
  90 /*
  91  * proc root can do almost nothing..
  92  */
  93 static struct inode_operations proc_root_inode_operations = {
  94         &proc_root_operations,  /* default base directory file-ops */
  95         NULL,                   /* create */
  96         proc_root_lookup,       /* lookup */
  97         NULL,                   /* link */
  98         NULL,                   /* unlink */
  99         NULL,                   /* symlink */
 100         NULL,                   /* mkdir */
 101         NULL,                   /* rmdir */
 102         NULL,                   /* mknod */
 103         NULL,                   /* rename */
 104         NULL,                   /* readlink */
 105         NULL,                   /* follow_link */
 106         NULL,                   /* readpage */
 107         NULL,                   /* writepage */
 108         NULL,                   /* bmap */
 109         NULL,                   /* truncate */
 110         NULL                    /* permission */
 111 };
 112 
 113 /*
 114  * This is the root "inode" in the /proc tree..
 115  */
 116 struct proc_dir_entry proc_root = {
 117         PROC_ROOT_INO, 5, "/proc",
 118         S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
 119         0, &proc_root_inode_operations,
 120         NULL, NULL,
 121         NULL,
 122         &proc_root, NULL
 123 };
 124 
 125 struct proc_dir_entry proc_net = {
 126         PROC_NET, 3, "net",
 127         S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
 128         0, &proc_dir_inode_operations,
 129         NULL, NULL,
 130         NULL,
 131         NULL, NULL      
 132 };
 133 
 134 struct proc_dir_entry proc_scsi = {
 135         PROC_SCSI, 4, "scsi",
 136         S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
 137         0, &proc_dir_inode_operations,
 138         NULL, NULL,
 139         NULL, &proc_root, NULL
 140 };
 141 
 142 int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
     /* [previous][next][first][last][top][bottom][index][help] */
 143 {
 144         dp->next = dir->subdir;
 145         dp->parent = dir;
 146         dir->subdir = dp;
 147         if (S_ISDIR(dp->mode))
 148                 dir->nlink++;
 149         return 0;
 150 }
 151 
 152 int proc_unregister(struct proc_dir_entry * dir, int ino)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154         struct proc_dir_entry **p = &dir->subdir, *dp;
 155 
 156         while ((dp = *p) != NULL) {
 157                 if (dp->low_ino == ino) {
 158                         *p = dp->next;
 159                         dp->next = NULL;
 160                         if (S_ISDIR(dp->mode))
 161                                 dir->nlink--;
 162                         return 0;
 163                 }
 164                 p = &dp->next;
 165         }
 166         return -EINVAL;
 167 }       
 168 
 169 /*
 170  * /proc/self:
 171  */
 172 static int proc_self_followlink(struct inode * dir, struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
 173                         int flag, int mode, struct inode ** res_inode)
 174 {
 175         iput(dir);
 176         *res_inode = proc_get_inode(inode->i_sb, (current->pid << 16) + PROC_PID_INO, &proc_pid);
 177         iput(inode);
 178         if (!*res_inode)
 179                 return -ENOENT;
 180         return 0;
 181 }
 182 
 183 static int proc_self_readlink(struct inode * inode, char * buffer, int buflen)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185         int len;
 186         char tmp[30];
 187 
 188         iput(inode);
 189         len = 1 + sprintf(tmp, "%d", current->pid);
 190         if (buflen < len)
 191                 len = buflen;
 192         memcpy_tofs(buffer, tmp, len);
 193         return len;
 194 }
 195 
 196 static struct inode_operations proc_self_inode_operations = {
 197         NULL,                   /* no file-ops */
 198         NULL,                   /* create */
 199         NULL,                   /* lookup */
 200         NULL,                   /* link */
 201         NULL,                   /* unlink */
 202         NULL,                   /* symlink */
 203         NULL,                   /* mkdir */
 204         NULL,                   /* rmdir */
 205         NULL,                   /* mknod */
 206         NULL,                   /* rename */
 207         proc_self_readlink,     /* readlink */
 208         proc_self_followlink,   /* follow_link */
 209         NULL,                   /* readpage */
 210         NULL,                   /* writepage */
 211         NULL,                   /* bmap */
 212         NULL,                   /* truncate */
 213         NULL                    /* permission */
 214 };
 215 
 216 void proc_root_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218         static int done = 0;
 219 
 220         if (done)
 221                 return;
 222         done = 1;
 223         proc_base_init();
 224         proc_register(&proc_root, &(struct proc_dir_entry) {
 225                 PROC_LOADAVG, 7, "loadavg",
 226                 S_IFREG | S_IRUGO, 1, 0, 0,
 227         });
 228         proc_register(&proc_root, &(struct proc_dir_entry) {
 229                 PROC_UPTIME, 6, "uptime",
 230                 S_IFREG | S_IRUGO, 1, 0, 0,
 231         });
 232         proc_register(&proc_root, &(struct proc_dir_entry) {
 233                 PROC_MEMINFO, 7, "meminfo",
 234                 S_IFREG | S_IRUGO, 1, 0, 0,
 235         });
 236         proc_register(&proc_root, &(struct proc_dir_entry) {
 237                 PROC_KMSG, 4, "kmsg",
 238                 S_IFREG | S_IRUSR, 1, 0, 0,
 239         });
 240         proc_register(&proc_root, &(struct proc_dir_entry) {
 241                 PROC_VERSION, 7, "version",
 242                 S_IFREG | S_IRUGO, 1, 0, 0,
 243         });
 244 #ifdef CONFIG_PCI
 245         proc_register(&proc_root, &(struct proc_dir_entry) {
 246                 PROC_PCI, 3, "pci",
 247                 S_IFREG | S_IRUGO, 1, 0, 0,
 248         });
 249 #endif
 250         proc_register(&proc_root, &(struct proc_dir_entry) {
 251                 PROC_CPUINFO, 7, "cpuinfo",
 252                 S_IFREG | S_IRUGO, 1, 0, 0,
 253         });
 254         proc_register(&proc_root, &(struct proc_dir_entry) {
 255                 PROC_SELF, 4, "self",
 256                 S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO, 1, 0, 0,
 257                 64, &proc_self_inode_operations,
 258         });
 259         proc_register(&proc_root, &proc_net);
 260 
 261         proc_register(&proc_root, &proc_scsi);
 262 
 263 #ifdef CONFIG_DEBUG_MALLOC
 264         proc_register(&proc_root, &(struct proc_dir_entry) {
 265                 PROC_MALLOC, 6, "malloc",
 266                 S_IFREG | S_IRUGO, 1, 0, 0,
 267         });
 268 #endif
 269         proc_register(&proc_root, &(struct proc_dir_entry) {
 270                 PROC_KCORE, 5, "kcore",
 271                 S_IFREG | S_IRUSR, 1, 0, 0,
 272         });
 273         proc_register(&proc_root, &(struct proc_dir_entry) {
 274                 PROC_MODULES, 7, "modules",
 275                 S_IFREG | S_IRUGO, 1, 0, 0,
 276         });
 277         proc_register(&proc_root, &(struct proc_dir_entry) {
 278                 PROC_STAT, 4, "stat",
 279                 S_IFREG | S_IRUGO, 1, 0, 0,
 280         });
 281         proc_register(&proc_root, &(struct proc_dir_entry) {
 282                 PROC_DEVICES, 7, "devices",
 283                 S_IFREG | S_IRUGO, 1, 0, 0,
 284         });
 285         proc_register(&proc_root, &(struct proc_dir_entry) {
 286                 PROC_INTERRUPTS, 10,"interrupts",
 287                 S_IFREG | S_IRUGO, 1, 0, 0,
 288         });
 289         proc_register(&proc_root, &(struct proc_dir_entry) {
 290                 PROC_FILESYSTEMS, 11,"filesystems",
 291                 S_IFREG | S_IRUGO, 1, 0, 0,
 292         });
 293         proc_register(&proc_root, &(struct proc_dir_entry) {
 294                 PROC_KSYMS, 5, "ksyms",
 295                 S_IFREG | S_IRUGO, 1, 0, 0,
 296         });
 297         proc_register(&proc_root, &(struct proc_dir_entry) {
 298                 PROC_DMA, 3, "dma",
 299                 S_IFREG | S_IRUGO, 1, 0, 0,
 300         });
 301         proc_register(&proc_root, &(struct proc_dir_entry) {
 302                 PROC_IOPORTS, 7, "ioports",
 303                 S_IFREG | S_IRUGO, 1, 0, 0,
 304         });
 305 #ifdef CONFIG_APM
 306         proc_register(&proc_root, &(struct proc_dir_entry) {
 307                 PROC_APM, 3, "apm",
 308                 S_IFREG | S_IRUGO, 1, 0, 0,
 309         });
 310 #endif
 311         proc_register(&proc_root, &(struct proc_dir_entry) {
 312                 PROC_CMDLINE, 7, "cmdline",
 313                 S_IFREG | S_IRUGO, 1, 0, 0,
 314         });
 315                    
 316         if (prof_shift) {
 317                 proc_register(&proc_root, &(struct proc_dir_entry) {
 318                         PROC_PROFILE, 7, "profile",
 319                         S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0,
 320                 });
 321         }
 322 }
 323 
 324 
 325 int proc_match(int len,const char * name,struct proc_dir_entry * de)
     /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327         if (!de || !de->low_ino)
 328                 return 0;
 329         /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
 330         if (!len && (de->name[0]=='.') && (de->name[1]=='\0'))
 331                 return 1;
 332         if (de->namelen != len)
 333                 return 0;
 334         return !memcmp(name, de->name, len);
 335 }
 336 
 337 int proc_lookup(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 338         struct inode ** result)
 339 {
 340         struct proc_dir_entry * de;
 341         int ino;
 342 
 343         *result = NULL;
 344         if (!dir || !S_ISDIR(dir->i_mode)) {
 345                 iput(dir);
 346                 return -ENOTDIR;
 347         }
 348 
 349         de = (struct proc_dir_entry *) dir->u.generic_ip;
 350         if (!de) {
 351                 iput(dir);
 352                 return -EINVAL;
 353         }
 354 
 355         /* Special case "." and "..": they aren't on the directory list */
 356         *result = dir;
 357         if (!len)
 358                 return 0;
 359         if (name[0] == '.') {
 360                 if (len == 1)
 361                         return 0;
 362                 if (name[1] == '.' && len == 2) {
 363                         struct inode * inode;
 364                         inode = proc_get_inode(dir->i_sb, de->parent->low_ino, de->parent);
 365                         iput(dir);
 366                         if (!inode)
 367                                 return -EINVAL;
 368                         *result = inode;
 369                         return 0;
 370                 }
 371         }
 372 
 373         *result = NULL;
 374         for (de = de->subdir; de ; de = de->next) {
 375                 if (proc_match(len, name, de))
 376                         break;
 377         }
 378         if (!de) {
 379                 iput(dir);
 380                 return -ENOENT;
 381         }
 382 
 383         ino = de->low_ino | (dir->i_ino & ~(0xffff));
 384 
 385         if (!(*result = proc_get_inode(dir->i_sb, ino, de))) {
 386                 iput(dir);
 387                 return -EINVAL;
 388         }
 389         iput(dir);
 390         return 0;
 391 }
 392 
 393 static int proc_root_lookup(struct inode * dir,const char * name, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 394         struct inode ** result)
 395 {
 396         unsigned int pid, c;
 397         int i, ino, retval;
 398 
 399         dir->i_count++;
 400         retval = proc_lookup(dir, name, len, result);
 401         if (retval != -ENOENT) {
 402                 iput(dir);
 403                 return retval;
 404         }
 405         
 406         pid = 0;
 407         while (len-- > 0) {
 408                 c = *name - '0';
 409                 name++;
 410                 if (c > 9) {
 411                         pid = 0;
 412                         break;
 413                 }
 414                 pid *= 10;
 415                 pid += c;
 416                 if (pid & 0xffff0000) {
 417                         pid = 0;
 418                         break;
 419                 }
 420         }
 421         for (i = 0 ; i < NR_TASKS ; i++)
 422                 if (task[i] && task[i]->pid == pid)
 423                         break;
 424         if (!pid || i >= NR_TASKS) {
 425                 iput(dir);
 426                 return -ENOENT;
 427         }
 428         ino = (pid << 16) + PROC_PID_INO;
 429         if (!(*result = proc_get_inode(dir->i_sb, ino, &proc_pid))) {
 430                 iput(dir);
 431                 return -EINVAL;
 432         }
 433         iput(dir);
 434         return 0;
 435 }
 436 
 437 /*
 438  * This returns non-zero if at EOF, so that the /proc
 439  * root directory can use this and check if it should
 440  * continue with the <pid> entries..
 441  *
 442  * Note that the VFS-layer doesn't care about the return
 443  * value of the readdir() call, as long as it's non-negative
 444  * for success..
 445  */
 446 int proc_readdir(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 447         void * dirent, filldir_t filldir)
 448 {
 449         struct proc_dir_entry * de;
 450         unsigned int ino;
 451         int i;
 452 
 453         if (!inode || !S_ISDIR(inode->i_mode))
 454                 return -ENOTDIR;
 455         ino = inode->i_ino;
 456         de = (struct proc_dir_entry *) inode->u.generic_ip;
 457         if (!de)
 458                 return -EINVAL;
 459         i = filp->f_pos;
 460         switch (i) {
 461                 case 0:
 462                         if (filldir(dirent, ".", 1, i, ino) < 0)
 463                                 return 0;
 464                         i++;
 465                         filp->f_pos++;
 466                         /* fall through */
 467                 case 1:
 468                         if (filldir(dirent, "..", 2, i, de->parent->low_ino) < 0)
 469                                 return 0;
 470                         i++;
 471                         filp->f_pos++;
 472                         /* fall through */
 473                 default:
 474                         ino &= ~0xffff;
 475                         de = de->subdir;
 476                         i -= 2;
 477                         for (;;) {
 478                                 if (!de)
 479                                         return 1;
 480                                 if (!i)
 481                                         break;
 482                                 de = de->next;
 483                                 i--;
 484                         }
 485 
 486                         do {
 487                                 if (filldir(dirent, de->name, de->namelen, filp->f_pos, ino | de->low_ino) < 0)
 488                                         return 0;
 489                                 filp->f_pos++;
 490                                 de = de->next;
 491                         } while (de);
 492         }
 493         return 1;
 494 }
 495 
 496 #define NUMBUF 10
 497 
 498 static int proc_root_readdir(struct inode * inode, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
 499         void * dirent, filldir_t filldir)
 500 {
 501         char buf[NUMBUF];
 502         unsigned int nr,pid;
 503         unsigned long i,j;
 504 
 505         nr = filp->f_pos;
 506         if (nr < FIRST_PROCESS_ENTRY) {
 507                 int error = proc_readdir(inode, filp, dirent, filldir);
 508                 if (error <= 0)
 509                         return error;
 510                 filp->f_pos = nr = FIRST_PROCESS_ENTRY;
 511         }
 512 
 513         for (nr -= FIRST_PROCESS_ENTRY; nr < NR_TASKS; nr++, filp->f_pos++) {
 514                 struct task_struct * p = task[nr];
 515 
 516                 if (!p || !(pid = p->pid))
 517                         continue;
 518 
 519                 j = NUMBUF;
 520                 i = pid;
 521                 do {
 522                         j--;
 523                         buf[j] = '0' + (i % 10);
 524                         i /= 10;
 525                 } while (i);
 526 
 527                 if (filldir(dirent, buf+j, NUMBUF-j, filp->f_pos, (pid << 16) + PROC_PID_INO) < 0)
 528                         break;
 529         }
 530         return 0;
 531 }

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