Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | /* -*- linux-c -*- --------------------------------------------------------- * * * linux/fs/devpts/root.c * * Copyright 1998 H. Peter Anvin -- All Rights Reserved * * This file is part of the Linux kernel and is made available under * the terms of the GNU General Public License, version 2, or at your * option, any later version, incorporated herein by reference. * * ------------------------------------------------------------------------- */ #include <linux/errno.h> #include <linux/stat.h> #include <linux/param.h> #include <linux/string.h> #include "devpts_i.h" static int devpts_root_readdir(struct file *,void *,filldir_t); static struct dentry *devpts_root_lookup(struct inode *,struct dentry *); static int devpts_revalidate(struct dentry *, int); static struct file_operations devpts_root_operations = { NULL, /* llseek */ NULL, /* read */ NULL, /* write */ devpts_root_readdir, /* readdir */ NULL, /* poll */ NULL, /* ioctl */ NULL, /* mmap */ NULL, /* open */ NULL, /* flush */ NULL, /* release */ NULL, /* fsync */ NULL, /* fasync */ NULL, /* check_media_change */ NULL, /* revalidate */ NULL /* lock */ }; struct inode_operations devpts_root_inode_operations = { &devpts_root_operations, /* file operations */ NULL, /* create */ devpts_root_lookup, /* lookup */ NULL, /* link */ NULL, /* unlink */ NULL, /* symlink */ NULL, /* mkdir */ NULL, /* rmdir */ NULL, /* mknod */ NULL, /* rename */ NULL, /* readlink */ NULL, /* follow_link */ NULL, /* get_block */ NULL, /* readpage */ NULL, /* writepage */ NULL, /* flushpage */ NULL, /* truncate */ NULL, /* permission */ NULL, /* smap */ NULL /* revalidate */ }; static struct dentry_operations devpts_dentry_operations = { devpts_revalidate, /* d_revalidate */ NULL, /* d_hash */ NULL, /* d_compare */ }; /* * The normal naming convention is simply /dev/pts/<number>; this conforms * to the System V naming convention */ #define genptsname(buf,num) sprintf(buf, "%d", num) static int devpts_root_readdir(struct file *filp, void *dirent, filldir_t filldir) { struct inode * inode = filp->f_dentry->d_inode; struct devpts_sb_info * sbi = SBI(filp->f_dentry->d_inode->i_sb); off_t nr; char numbuf[16]; nr = filp->f_pos; switch(nr) { case 0: if (filldir(dirent, ".", 1, nr, inode->i_ino) < 0) return 0; filp->f_pos = ++nr; /* fall through */ case 1: if (filldir(dirent, "..", 2, nr, inode->i_ino) < 0) return 0; filp->f_pos = ++nr; /* fall through */ default: while ( nr < sbi->max_ptys ) { int ptynr = nr - 2; if ( sbi->inodes[ptynr] ) { genptsname(numbuf, ptynr); if ( filldir(dirent, numbuf, strlen(numbuf), nr, nr) < 0 ) return 0; } filp->f_pos = ++nr; } break; } return 0; } /* * Revalidate is called on every cache lookup. We use it to check that * the pty really does still exist. Never revalidate negative dentries; * for simplicity (fix later?) */ static int devpts_revalidate(struct dentry * dentry, int flags) { struct devpts_sb_info *sbi; if ( !dentry->d_inode ) return 0; sbi = SBI(dentry->d_inode->i_sb); return ( sbi->inodes[dentry->d_inode->i_ino - 2] == dentry->d_inode ); } static struct dentry *devpts_root_lookup(struct inode * dir, struct dentry * dentry) { struct devpts_sb_info *sbi = SBI(dir->i_sb); unsigned int entry; int i; const char *p; dentry->d_inode = NULL; /* Assume failure */ dentry->d_op = &devpts_dentry_operations; if ( dentry->d_name.len == 1 && dentry->d_name.name[0] == '0' ) { entry = 0; } else if ( dentry->d_name.len < 1 ) { return NULL; } else { p = dentry->d_name.name; if ( *p < '1' || *p > '9' ) return NULL; entry = *p++ - '0'; for ( i = dentry->d_name.len-1 ; i ; i-- ) { unsigned int nentry = *p++ - '0'; if ( nentry > 9 ) return NULL; if ( entry >= ~0U/10 ) return NULL; entry = nentry + entry * 10; } } if ( entry >= sbi->max_ptys ) return NULL; dentry->d_inode = sbi->inodes[entry]; if ( dentry->d_inode ) dentry->d_inode->i_count++; d_add(dentry, dentry->d_inode); return NULL; } |