系统相关
首页 > 系统相关> > linux-0.11分析:进程初始化函数init(),第二部分(void) open("/dev/tty0",O_RDWR,0),第十二篇随笔

linux-0.11分析:进程初始化函数init(),第二部分(void) open("/dev/tty0",O_RDWR,0),第十二篇随笔

作者:互联网

第二部分,(void) open("/dev/tty0",O_RDWR,0);

参考 [github这个博主的 厉害][ https://github.com/sunym1993/flash-linux0.11-talk ]

它会触发一个int 0x80中断,会找的sys_open的函数执行

看看这个函数吧

fs文件 -> open.c

int sys_open(const char * filename,int flag,int mode)
{
	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;
	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);
}

一部分一部分看吧

标签:count,struct,open,dev,current,fd,mode,tty0,inode
来源: https://www.cnblogs.com/shuisanya/p/16593933.html