免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1125 | 回复: 0
打印 上一主题 下一主题

linux 1.0 内核注解 linux/fs/open.c [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-19 19:38 |只看该作者 |倒序浏览
/********************************************
*Created By: Prometheus
*Date        : 2009-5-19      
********************************************/
/*
*  linux/fs/open.c
*
*  Copyright (C) 1991, 1992  Linus Torvalds
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern void fcntl_remove_locks(struct task_struct *, struct file *, unsigned int fd);
/*返回一个已经挂载的文件系统的信息到ubuf中,不过此处没有实现*/
asmlinkage int sys_ustat(int dev, struct ustat * ubuf)
{
    return -ENOSYS;
}
/*返回一个已经被挂载的文件系统的信息,其中path是在这个被
*挂载的文件系统内部的任何的路径名*/
asmlinkage int sys_statfs(const char * path, struct statfs * buf)
{
    struct inode * inode;
    int error;
    error = verify_area(VERIFY_WRITE, buf, sizeof(struct statfs));
    if (error)
        return error;
    error = namei(path,&inode);    /*映射路径为节点信息*/
    if (error)
        return error;
    if (!inode->i_sb->s_op->statfs) {
        iput(inode);
        return -ENOSYS;
    }
    inode->i_sb->s_op->statfs(inode->i_sb, buf);
    iput(inode);
    return 0;
}
asmlinkage int sys_fstatfs(unsigned int fd, struct statfs * buf)
{
    struct inode * inode;
    struct file * file;
    int error;
    error = verify_area(VERIFY_WRITE, buf, sizeof(struct statfs));
    if (error)
        return error;
    if (fd >= NR_OPEN || !(file = current->filp[fd]))
        return -EBADF;
    if (!(inode = file->f_inode))    /*一样的,就是获得节点的方法不同*/
        return -ENOENT;
    if (!inode->i_sb->s_op->statfs)
        return -ENOSYS;
    inode->i_sb->s_op->statfs(inode->i_sb, buf);
    return 0;
}
asmlinkage int sys_truncate(const char * path, unsigned int length)
{
    struct inode * inode;
    int error;
    error = namei(path,&inode);
    if (error)
        return error;
        
    /*检查文件类型和访问权限以及下面的访问方法*/
    if (S_ISDIR(inode->i_mode) || !permission(inode,MAY_WRITE)) {
        iput(inode);
        return -EACCES;
    }
    if (IS_RDONLY(inode)) {
        iput(inode);
        return -EROFS;
    }
    inode->i_size = length;        /*更新节点信息*/
    if (inode->i_op && inode->i_op->truncate)
        inode->i_op->truncate(inode);
    inode->i_ctime = inode->i_mtime = CURRENT_TIME;
    inode->i_dirt = 1;
    error = notify_change(NOTIFY_SIZE, inode);    /*可能比调用write_inode要好*/
    iput(inode);
    return error;
}
asmlinkage int sys_ftruncate(unsigned int fd, unsigned int length)
{
    struct inode * inode;
    struct file * file;
    if (fd >= NR_OPEN || !(file = current->filp[fd]))
        return -EBADF;
    if (!(inode = file->f_inode))
        return -ENOENT;
    if (S_ISDIR(inode->i_mode) || !(file->f_mode & 2))
        return -EACCES;
    inode->i_size = length;
    if (inode->i_op && inode->i_op->truncate)
        inode->i_op->truncate(inode);
    inode->i_ctime = inode->i_mtime = CURRENT_TIME;
    inode->i_dirt = 1;
    return notify_change(NOTIFY_SIZE, inode);
}
/*改变节点的访问和修改时间到times,但是如果times==NULL
*就设置为当前时间。对于一般的用户如果要更改就必须有相应
*的节点的写权限,而节点的拥有者没有这个限制(超级用户很霸道)
*注意的是字段i_ctime表示i节点的自身的修改时间,必须设置
*为CURRENT_TIME*/
asmlinkage int sys_utime(char * filename, struct utimbuf * times)
{
    struct inode * inode;
    long actime,modtime;
    int error;
    error = namei(filename,&inode);
    if (error)
        return error;
    if (IS_RDONLY(inode)) {
        iput(inode);
        return -EROFS;
    }
    if (times) {
        if ((current->euid != inode->i_uid) && !suser()) {
            iput(inode);
            return -EPERM;
        }
        actime = get_fs_long((unsigned long *) &times->actime);
        modtime = get_fs_long((unsigned long *) &times->modtime);
        inode->i_ctime = CURRENT_TIME;
    } else {    /*times==NULL*/
        if ((current->euid != inode->i_uid) &&
            !permission(inode,MAY_WRITE)) {
            iput(inode);
            return -EACCES;
        }
        actime = modtime = inode->i_ctime = CURRENT_TIME;
    }
    inode->i_atime = actime;
    inode->i_mtime = modtime;
   
    inode->i_dirt = 1;
    error = notify_change(NOTIFY_TIME, inode);
    iput(inode);
    return error;
}
asmlinkage int sys_access(const char * filename,int mode)
{
    struct inode * inode;
    int res, i_mode;
   
   
    //这句代码让我很不解,为什么只是测试 other 用户的访问模式?
    //No!!这里只是访问的模式,不是实际的权限,呵呵
    if (mode != (mode & S_IRWXO))    /* where's F_OK, X_OK, W_OK, R_OK? */
        return -EINVAL;
        
    res = namei(filename,&inode);
    if (res)
        return res;
    i_mode = inode->i_mode;
    res = i_mode & S_IRWXUGO;    /*这里检查了所有可能用户的访问模式了*/
   
    //话说这里是个漏洞,就是用 uid 进行比较的,如果进程在此之前
      //使用了 s 粘滞位,那么 uid 就会改变,so ...... (不知道该不该这样理解,反正 manual不推荐使用的)
    if (current->uid == inode->i_uid)
        res >>= 6;        /* needs cleaning? */
    else if (in_group_p(inode->i_gid))
        res >>= 3;        /* needs cleaning? */
    iput(inode);
    if ((res & mode) == mode)    /*返回0表示能够访问的*/
        return 0;
        
    /*这句话我不懂,代码也很蹊跷
     * XXX nope.  suser() is inappropriate and swapping the ids while
     * decomposing the path would be racy.
     */
    if ((!current->uid) &&
        (S_ISDIR(i_mode) || !(mode & S_IXOTH) || (i_mode & S_IXUGO)))
        return 0;
    return -EACCES;
}
/*用于改变当前的工作目录'cd'*/
asmlinkage int sys_chdir(const char * filename)
{
    struct inode * inode;
    int error;
    error = namei(filename,&inode);
    if (error)
        return error;
    if (!S_ISDIR(inode->i_mode)) {        //必须是目录的节点
        iput(inode);
        return -ENOTDIR;
    }
    if (!permission(inode,MAY_EXEC)) {    /*返回1表示允许,对于工作目录要执行?*/
        iput(inode);
        return -EACCES;
    }
    iput(current->pwd);
    current->pwd = inode;
        
    /*这里没有使用inode->i_count++将节点的引用次数增加
     *可能的原因是这里在使用namei来得到节点的时候就已经
     *将节点的引用计数增加了,而下面的函数是直接从文件
     *描述符中得到的,所以要递增引用*/
    return (0);
}
asmlinkage int sys_fchdir(unsigned int fd)
{
    struct inode * inode;
    struct file * file;
    if (fd >= NR_OPEN || !(file = current->filp[fd]))
        return -EBADF;
    if (!(inode = file->f_inode))
        return -ENOENT;
    if (!S_ISDIR(inode->i_mode))
        return -ENOTDIR;
    if (!permission(inode,MAY_EXEC))
        return -EACCES;
    iput(current->pwd);
    current->pwd = inode;
    inode->i_count++;
    return (0);
}
/*改变当前进程的根节点,这个改变将被所有的子进程
*所共享的。这个函数只是影响了路径的解析作用,而
*且也不影响当前工作目录(就是'.'可以在'根目录的外
*边'),这个权限必须要有根用户,而且根用户可以跳过
*这个根目录的限制:
*mkdir foo;chroot foo;cd ..;
*vsftp就有chroot 的吧哈:-)*/
asmlinkage int sys_chroot(const char * filename)
{
    struct inode * inode;  //话说这里是个漏洞,就是用 uid 进行比较的,如果进程在此之前
  //使用了 s 粘滞位,那么 uid 就会改变,so ...... (不知道该不该这样理解,反正 manual
不推荐使用的)
    int error;
    error = namei(filename,&inode);
    if (error)
        return error;
    if (!S_ISDIR(inode->i_mode)) {
        iput(inode);
        return -ENOTDIR;
    }
    if (!suser()) {
        iput(inode);
        return -EPERM;
    }
    iput(current->root);
    current->root = inode;
    return (0);
}
asmlinkage int sys_fchmod(unsigned int fd, mode_t mode)
{
    struct inode * inode;
    struct file * file;
    if (fd >= NR_OPEN || !(file = current->filp[fd]))
        return -EBADF;
    if (!(inode = file->f_inode))
        return -ENOENT;
    if ((current->euid != inode->i_uid) && !suser())
        return -EPERM;
    if (IS_RDONLY(inode))    /*检查节点本身是否可以被改变*/
        return -EROFS;
    if (mode == (mode_t) -1)
        mode = inode->i_mode;
    inode->i_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
    if (!suser() && !in_group_p(inode->i_gid))
        inode->i_mode &= ~S_ISGID;
    inode->i_ctime = CURRENT_TIME;
    inode->i_dirt = 1;
    return notify_change(NOTIFY_MODE, inode);
}
/*改变文件的访问方式,这个只有文件的拥有者和
*超级用户才有这个权限*/
asmlinkage int sys_chmod(const char * filename, mode_t mode)    /*mode_t ->unsigned short*/
{
    struct inode * inode;
    int error;
    error = namei(filename,&inode);
    if (error)
        return error;
    if ((current->euid != inode->i_uid) && !suser()) {
        iput(inode);
        return -EPERM;
    }
    if (IS_RDONLY(inode)) {    /*只读节点?*/
        iput(inode);
        return -EROFS;
    }
    if (mode == (mode_t) -1)        
    /*试验发现,如果参数是-1,那么原先的访问权限就不改变*/
        mode = inode->i_mode;
   
    /*如果参数不是-1,那么就进行设置了,注意的是这里的权限使用的是
     *八进制的数据,所以各个访问权限是连续的,而不是十六进制的数据
     *所以权限中使用了5位(15bit),而S_IALLUGO可以设置后面的12bit了*/   
    inode->i_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
    if (!suser() && !in_group_p(inode->i_gid))
        inode->i_mode &= ~S_ISGID;
   
    inode->i_ctime = CURRENT_TIME;
    inode->i_dirt = 1;
    error = notify_change(NOTIFY_MODE, inode);
    iput(inode);
    return error;
}
/*改变文件的所有者,包含了用户和组号
*任何一个是-1就不进行设置了*/
asmlinkage int sys_fchown(unsigned int fd, uid_t user, gid_t group)
{
    struct inode * inode;
    struct file * file;
    if (fd >= NR_OPEN || !(file = current->filp[fd]))
        return -EBADF;
    if (!(inode = file->f_inode))
        return -ENOENT;
    if (IS_RDONLY(inode))
        return -EROFS;
        
    if (user == (uid_t) -1)
        user = inode->i_uid;
    if (group == (gid_t) -1)
        group = inode->i_gid;
   
    if ((current->euid == inode->i_uid && user == inode->i_uid &&
         (in_group_p(group) || group == inode->i_gid)) ||
         /*上面的一部分是表明:这个改变必须是节点的有效用户,而且如果
          *文件的所有者是一般的用户,只能改变这个文件的组(而且用户本身
          *也必须在这个组中)而不能改变所有者,这也是可以理解的:
          *己所不欲勿施于人
          *I do not want to say anything about super-user.*/
        suser()) {
        inode->i_uid = user;
        inode->i_gid = group;
        inode->i_ctime = CURRENT_TIME;
        inode->i_dirt = 1;
        return notify_change(NOTIFY_UIDGID, inode);
    }
    return -EPERM;
}
asmlinkage int sys_chown(const char * filename, uid_t user, gid_t group)
{
    struct inode * inode;
    int error;
    error = lnamei(filename,&inode);
    if (error)
        return error;
    if (IS_RDONLY(inode)) {
        iput(inode);
        return -EROFS;
    }
    if (user == (uid_t) -1)
        user = inode->i_uid;
    if (group == (gid_t) -1)
        group = inode->i_gid;
    if ((current->euid == inode->i_uid && user == inode->i_uid &&
         (in_group_p(group) || group == inode->i_gid)) ||
        suser()) {
        inode->i_uid = user;
        inode->i_gid = group;
        inode->i_ctime = CURRENT_TIME;
        inode->i_dirt = 1;
        error = notify_change(NOTIFY_UIDGID, inode);
        iput(inode);
        return error;
    }
    iput(inode);
    return -EPERM;
}
/*
* Note that while the flag value (low two bits) for sys_open means:
*    00 - read-only
*    01 - write-only
*    10 - read-write
*    11 - special
* it is changed into
*    00 - no permissions needed
*    01 - read-permission
*    10 - write-permission
*    11 - read-write
* for the internal routines (ie open_namei()/follow_link() etc). 00 is
* used by symlinks.
*/
int do_open(const char * filename,int flags,int mode)
{
    struct inode * inode;
    struct file * f;
    int flag,error,fd;
    for(fd=0 ; fdfilp[fd])
            break;
    if (fd>=NR_OPEN)
        return -EMFILE;
        
    FD_CLR(fd,&current->close_on_exec);
   
    f = get_empty_filp();
    if (!f)
        return -ENFILE;
    //这里占用资源了,以后有错误就需要进行清理工作了
    current->filp[fd] = f;        
    f->f_flags = flag = flags;
   
    /*这里的原因看上面的注释,刚好增加了1*/
    f->f_mode = (flag+1) & O_ACCMODE;    //00003
   
    if (f->f_mode)
        flag++;
    if (flag & (O_TRUNC | O_CREAT))
        flag |= 2;            /*需要写的权限*/
   
    //正真的底层调用函数啊   
    error = open_namei(filename,flag,mode,&inode,NULL);
    if (error) {
        current->filp[fd]=NULL;
        f->f_count--;
        return error;
    }
    f->f_inode = inode;
    f->f_pos = 0;
    f->f_reada = 0;
    f->f_op = NULL;
   
    if (inode->i_op)
        f->f_op = inode->i_op->default_file_ops;
    if (f->f_op && f->f_op->open) {
        error = f->f_op->open(inode,f);
        if (error) {
            iput(inode);
            f->f_count--;
            current->filp[fd]=NULL;
            return error;
        }
    }
    f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
    return (fd);
}
asmlinkage int sys_open(const char * filename,int flags,int mode)
{
    char * tmp;
    int error;
    error = getname(filename, &tmp);
    if (error)
        return error;
    error = do_open(tmp,flags,mode);
    putname(tmp);
    return error;
}
asmlinkage int sys_creat(const char * pathname, int mode)
{
    return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
}
int close_fp(struct file *filp, unsigned int fd)
{
    struct inode *inode;
    if (filp->f_count == 0) {
        printk("VFS: Close: file count is 0\n");
        return 0;
    }
    inode = filp->f_inode;
    if (inode && S_ISREG(inode->i_mode))
        fcntl_remove_locks(current, filp, fd);
    if (filp->f_count > 1) {        //被共享了
        filp->f_count--;
        return 0;
    }
    if (filp->f_op && filp->f_op->release)
        filp->f_op->release(inode,filp);
    filp->f_count--;
    filp->f_inode = NULL;
    iput(inode);
    return 0;
}
asmlinkage int sys_close(unsigned int fd)
{   
    struct file * filp;
    if (fd >= NR_OPEN)
        return -EBADF;
    FD_CLR(fd, &current->close_on_exec);
    if (!(filp = current->filp[fd]))
        return -EBADF;
    current->filp[fd] = NULL;    //直接清理了,善后工作就下面了
    return (close_fp (filp, fd));
}
/*
* This routine simulates a hangup on the tty, to arrange that users
* are given clean terminals at login time.
*/
//挂起当前终端
//simulates  a  hangup  on  the  current  terminal.  This call
//arranges for other users to have a “clean” tty at login time.
asmlinkage int sys_vhangup(void)
{
    struct tty_struct *tty;
    if (!suser())
        return -EPERM;
    /* See if there is a controlling tty. */
    if (current->tty tty));
    tty_vhangup(tty);
    return 0;
}
文档地址:http://blogimg.chinaunix.net/blog/upfile2/090519193335.pdf


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/90306/showart_1933058.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP