Annotation of linux/fs/open.c, revision 1.1.1.2

1.1       root        1: #include <string.h>
                      2: #include <errno.h>
                      3: #include <fcntl.h>
                      4: #include <sys/types.h>
                      5: #include <utime.h>
                      6: #include <sys/stat.h>
                      7: 
                      8: #include <linux/sched.h>
                      9: #include <linux/tty.h>
                     10: #include <linux/kernel.h>
                     11: #include <asm/segment.h>
                     12: 
                     13: int sys_utime(char * filename, struct utimbuf * times)
                     14: {
                     15:        struct m_inode * inode;
                     16:        long actime,modtime;
                     17: 
                     18:        if (!(inode=namei(filename)))
                     19:                return -ENOENT;
                     20:        if (times) {
                     21:                actime = get_fs_long((unsigned long *) &times->actime);
                     22:                modtime = get_fs_long((unsigned long *) &times->modtime);
                     23:        } else
                     24:                actime = modtime = CURRENT_TIME;
                     25:        inode->i_atime = actime;
                     26:        inode->i_mtime = modtime;
                     27:        inode->i_dirt = 1;
                     28:        iput(inode);
                     29:        return 0;
                     30: }
                     31: 
                     32: int sys_access(const char * filename,int mode)
                     33: {
                     34:        struct m_inode * inode;
                     35:        int res;
                     36: 
                     37:        mode &= 0007;
                     38:        if (!(inode=namei(filename)))
                     39:                return -EACCES;
                     40:        res = inode->i_mode & 0777;
                     41:        iput(inode);
1.1.1.2 ! root       42:        if (!(current->euid && current->uid)) {
1.1       root       43:                if (res & 0111)
                     44:                        res = 0777;
                     45:                else
                     46:                        res = 0666;
1.1.1.2 ! root       47:        }
1.1       root       48:        if (current->euid == inode->i_uid)
                     49:                res >>= 6;
                     50:        else if (current->egid == inode->i_gid)
                     51:                res >>= 6;
                     52:        if ((res & 0007 & mode) == mode)
                     53:                return 0;
                     54:        return -EACCES;
                     55: }
                     56: 
                     57: int sys_chdir(const char * filename)
                     58: {
                     59:        struct m_inode * inode;
                     60: 
                     61:        if (!(inode = namei(filename)))
                     62:                return -ENOENT;
                     63:        if (!S_ISDIR(inode->i_mode)) {
                     64:                iput(inode);
                     65:                return -ENOTDIR;
                     66:        }
                     67:        iput(current->pwd);
                     68:        current->pwd = inode;
                     69:        return (0);
                     70: }
                     71: 
                     72: int sys_chroot(const char * filename)
                     73: {
                     74:        struct m_inode * inode;
                     75: 
                     76:        if (!(inode=namei(filename)))
                     77:                return -ENOENT;
                     78:        if (!S_ISDIR(inode->i_mode)) {
                     79:                iput(inode);
                     80:                return -ENOTDIR;
                     81:        }
                     82:        iput(current->root);
                     83:        current->root = inode;
                     84:        return (0);
                     85: }
                     86: 
                     87: int sys_chmod(const char * filename,int mode)
                     88: {
                     89:        struct m_inode * inode;
                     90: 
                     91:        if (!(inode=namei(filename)))
                     92:                return -ENOENT;
1.1.1.2 ! root       93:        if (current->uid && current->euid) {
1.1       root       94:                if (current->uid!=inode->i_uid && current->euid!=inode->i_uid) {
                     95:                        iput(inode);
                     96:                        return -EACCES;
                     97:                } else 
                     98:                        mode = (mode & 0777) | (inode->i_mode & 07000);
1.1.1.2 ! root       99:        }
1.1       root      100:        inode->i_mode = (mode & 07777) | (inode->i_mode & ~07777);
                    101:        inode->i_dirt = 1;
                    102:        iput(inode);
                    103:        return 0;
                    104: }
                    105: 
                    106: int sys_chown(const char * filename,int uid,int gid)
                    107: {
                    108:        struct m_inode * inode;
                    109: 
                    110:        if (!(inode=namei(filename)))
                    111:                return -ENOENT;
                    112:        if (current->uid && current->euid) {
                    113:                iput(inode);
                    114:                return -EACCES;
                    115:        }
                    116:        inode->i_uid=uid;
                    117:        inode->i_gid=gid;
                    118:        inode->i_dirt=1;
                    119:        iput(inode);
                    120:        return 0;
                    121: }
                    122: 
                    123: int sys_open(const char * filename,int flag,int mode)
                    124: {
                    125:        struct m_inode * inode;
                    126:        struct file * f;
                    127:        int i,fd;
                    128: 
                    129:        mode &= 0777 & ~current->umask;
                    130:        for(fd=0 ; fd<NR_OPEN ; fd++)
                    131:                if (!current->filp[fd])
                    132:                        break;
                    133:        if (fd>=NR_OPEN)
                    134:                return -EINVAL;
                    135:        current->close_on_exec &= ~(1<<fd);
                    136:        f=0+file_table;
                    137:        for (i=0 ; i<NR_FILE ; i++,f++)
                    138:                if (!f->f_count) break;
                    139:        if (i>=NR_FILE)
                    140:                return -EINVAL;
                    141:        (current->filp[fd]=f)->f_count++;
                    142:        if ((i=open_namei(filename,flag,mode,&inode))<0) {
                    143:                current->filp[fd]=NULL;
                    144:                f->f_count=0;
                    145:                return i;
                    146:        }
                    147: /* ttys are somewhat special (ttyxx major==4, tty major==5) */
1.1.1.2 ! root      148:        if (S_ISCHR(inode->i_mode)) {
1.1       root      149:                if (MAJOR(inode->i_zone[0])==4) {
                    150:                        if (current->leader && current->tty<0) {
                    151:                                current->tty = MINOR(inode->i_zone[0]);
                    152:                                tty_table[current->tty].pgrp = current->pgrp;
                    153:                        }
                    154:                } else if (MAJOR(inode->i_zone[0])==5)
                    155:                        if (current->tty<0) {
                    156:                                iput(inode);
                    157:                                current->filp[fd]=NULL;
                    158:                                f->f_count=0;
                    159:                                return -EPERM;
                    160:                        }
1.1.1.2 ! root      161:        }
1.1       root      162:        f->f_mode = inode->i_mode;
                    163:        f->f_flags = flag;
                    164:        f->f_count = 1;
                    165:        f->f_inode = inode;
                    166:        f->f_pos = 0;
                    167:        return (fd);
                    168: }
                    169: 
                    170: int sys_creat(const char * pathname, int mode)
                    171: {
                    172:        return sys_open(pathname, O_CREAT | O_TRUNC, mode);
                    173: }
                    174: 
                    175: int sys_close(unsigned int fd)
                    176: {      
                    177:        struct file * filp;
                    178: 
                    179:        if (fd >= NR_OPEN)
                    180:                return -EINVAL;
                    181:        current->close_on_exec &= ~(1<<fd);
                    182:        if (!(filp = current->filp[fd]))
                    183:                return -EINVAL;
                    184:        current->filp[fd] = NULL;
                    185:        if (filp->f_count == 0)
                    186:                panic("Close: file count is 0");
                    187:        if (--filp->f_count)
                    188:                return (0);
                    189:        iput(filp->f_inode);
                    190:        return (0);
                    191: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.