⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 open.c.bak

📁 一份精简的linux内核源代码
💻 BAK
字号:
/* *  linux/fs/open.c * *  (C) 1991  Linus Torvalds */#include <string.h>#include <errno.h>#include <fcntl.h>#include <sys/types.h>#include <utime.h>#include <sys/stat.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/kernel.h>#include <asm/segment.h>int sys_ustat(int dev, struct ustat * ubuf)/*取文件系统信息 */{	return -ENOSYS;}int sys_utime(char * filename, struct utimbuf * times)/*设置文件修改时间 */{	struct m_inode * inode;	long actime,modtime;	if (!(inode=namei(filename)))		return -ENOENT;	if (times) {		actime = get_fs_long((unsigned long *) &times->actime);		modtime = get_fs_long((unsigned long *) &times->modtime);	} else		actime = modtime = CURRENT_TIME;	inode->i_atime = actime;	inode->i_mtime = modtime;	inode->i_dirt = 1;	iput(inode);	return 0;}/* * XXX should we use the real or effective uid?  BSD uses the real uid, * so as to make this call useful to setuid programs. */int sys_access(const char * filename,int mode)/*检查系统访问权限 ???(p517)*/{	struct m_inode * inode;	int res, i_mode;	mode &= 0007;/*检测的访问属性在底3位 */	if (!(inode=namei(filename)))		return -EACCES;	i_mode = res = inode->i_mode & 0777;	iput(inode);	if (current->uid == inode->i_uid)		res >>= 6;	else if (current->gid == inode->i_gid)		res >>= 6;	if ((res & 0007 & mode) == mode)		return 0;	/*	 * XXX we are doing this test last because we really should be	 * swapping the effective with the real user id (temporarily),	 * and then calling suser() routine.  If we do call the	 * suser() routine, it needs to be called last. 	 */	if ((!current->uid) &&	    (!(mode & 1) || (i_mode & 0111)))		return 0;	return -EACCES;}int sys_chdir(const char * filename)/*改变当前工作目录 */{	struct m_inode * inode;	if (!(inode = namei(filename)))		return -ENOENT;	if (!S_ISDIR(inode->i_mode)) {		iput(inode);		return -ENOTDIR;	}	iput(current->pwd);	current->pwd = inode;	return (0);}int sys_chroot(const char * filename)/*改变当前进程根目录 */{	struct m_inode * inode;	if (!(inode=namei(filename)))		return -ENOENT;	if (!S_ISDIR(inode->i_mode)) {		iput(inode);		return -ENOTDIR;	}	iput(current->root);	current->root = inode;	return (0);}int sys_chmod(const char * filename,int mode)/*修改文件属性 */{	struct m_inode * inode;	if (!(inode=namei(filename)))		return -ENOENT;	if ((current->euid != inode->i_uid) && !suser()) {		iput(inode);		return -EACCES;	}	inode->i_mode = (mode & 07777) | (inode->i_mode & ~07777);	inode->i_dirt = 1;	iput(inode);	return 0;}int sys_chown(const char * filename,int uid,int gid)/*修改文件宿主用户 */{	struct m_inode * inode;	if (!(inode=namei(filename)))		return -ENOENT;	if (!suser()) {		iput(inode);		return -EACCES;	}	inode->i_uid=uid;	inode->i_gid=gid;	inode->i_dirt=1;	iput(inode);	return 0;}int sys_open(const char * filename,int flag,int mode)/*打开文件:获得指定路径名的i节点,在内存文件表中申请一项使该项指向该i节点*/{                                                    /*使当前进程的文件表中的一项指向刚才申请到的文件表项,并返回该项的索引(句柄)*/	struct m_inode * inode;	struct file * f;	int i,fd;	mode &= 0777 & ~current->umask;/*??将用户设置的文件模式和进程模式屏蔽码相与产生许可的文件模式 */	for(fd=0 ; fd<NR_OPEN ; fd++)		if (!current->filp[fd])			break;	if (fd>=NR_OPEN)		return -EINVAL;	current->close_on_exec &= ~(1<<fd);/*??设置当前进程的执行时关闭文件句柄位图,复位对应比特位 */	f=0+file_table;                     /*即设置当子进程调用execve时关闭的文件(默认情况下新建的子进程将打开所有父进程打开的文件) */	for (i=0 ; i<NR_FILE ; i++,f++)		if (!f->f_count) break;	if (i>=NR_FILE)		return -EINVAL;	(current->filp[fd]=f)->f_count++;	if ((i=open_namei(filename,flag,mode,&inode))<0) {		current->filp[fd]=NULL;		f->f_count=0;		return i;	}/* ttys are somewhat special (ttyxx major==4, tty major==5) */	if (S_ISCHR(inode->i_mode))      /*为进程分配终端???*/		if (MAJOR(inode->i_zone[0])==4) {			if (current->leader && current->tty<0) {				current->tty = MINOR(inode->i_zone[0]);				tty_table[current->tty].pgrp = current->pgrp;			}		} else if (MAJOR(inode->i_zone[0])==5)			if (current->tty<0) {				iput(inode);				current->filp[fd]=NULL;				f->f_count=0;				return -EPERM;			}/* Likewise with block-devices: check for floppy_change */	if (S_ISBLK(inode->i_mode))		check_disk_change(inode->i_zone[0]);	f->f_mode = inode->i_mode;  /*内存文件表项初始化*/	f->f_flags = flag;	f->f_count = 1;	f->f_inode = inode;	f->f_pos = 0;	return (fd);}int sys_creat(const char * pathname, int mode)  /*创建文件*/{	return sys_open(pathname, O_CREAT | O_TRUNC, mode);}int sys_close(unsigned int fd)  /*关闭文件(参数为文件句柄)。当前进程的文件数组中相应项置null;*/{	                            /*内存文件表结构中相应项引用计数减1;iput该i节点*/	struct file * filp;	if (fd >= NR_OPEN)		return -EINVAL;	current->close_on_exec &= ~(1<<fd);	if (!(filp = current->filp[fd]))		return -EINVAL;	current->filp[fd] = NULL;	if (filp->f_count == 0)		panic("Close: file count is 0");	if (--filp->f_count)  /*申请内存文件表项时的依据是引用计数为0,这里出现引用计数>1是由于创建子进程时增加了文件引用计数*/		return (0);	iput(filp->f_inode);	return (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -