作者:你的拥吻像情歌一样凄美_207 | 来源:互联网 | 2024-10-12 13:36
1、打开终端设备文件及复制文件句柄 代码路径:initmain.c 目前处于进程1的3特权级voidinit(void){intpid,i;setup((voi
1、打开标准输入设备
代码路径:init/main.c 目前处于进程1的3特权级
void init(void)
{
int pid,i;
setup((void *) &drive_info);
(void) open("/dev/tty0",O_RDWR,0);
(void) dup(0);
(void) dup(0);
...
} open同样调用int 0x80进入进程1的0特权级,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 if (!current->filp[fd])
break;
if (fd>=NR_OPEN)
return -EINVAL;
current->close_on_exec &= ~(1< f=0+file_table;
for (i=0 ; i if (!f->f_count) break;
if (i>=NR_FILE)
return -EINVAL;
(current->filp[fd]=f)->f_count++;//将进程1的filp[20]与file_table[64]挂接,并增加引用计数,f_count为1
if ((i=open_namei(filename,flag,mode,&inode))<0) {
current->filp[fd]=NULL;
f->f_count=0;
return i;
}
...
} 代码路径:include/linux/fs.h
#define NR_OPEN 20
#define NR_FILE 64 代码路径:fs/namei.c
int open_namei(const char * pathname, int flag, int mode,
struct m_inode ** res_inode)
{
const char * basename;
int inr,dev,namelen;
struct m_inode * dir, *inode;
struct buffer_head * bh;
struct dir_entry * de;
if ((flag & O_TRUNC) && !(flag & O_ACCMODE))
flag |= O_WRONLY;
mode &= 0777 & ~current->umask;
mode |= I_REGULAR;//暂时不考虑
if (!(dir = dir_namei(pathname,&namelen,&basename)))//获取枝梢i节点,namelen为tty0的长度,basename指向tty0的第一个字母‘t‘
return -ENOENT;
if (!namelen) { /* special case: ‘/usr/‘ etc */
if (!(flag & (O_ACCMODE|O_CREAT|O_TRUNC))) {
*res_inode=dir;
return 0;
}
iput(dir);
return -EISDIR;
}
bh = find_entry(&dir,basename,namelen,&de);//此时根据dev的i节点和tty0来查找tty0的目录项
...
} 代码路径:fs/namei.c
static struct m_inode * dir_namei(const char * pathname,
int * namelen, const char ** name)
{
char c;
const char * basename;
struct m_inode * dir;
if (!(dir = get_dir(pathname)))
return NULL;
basename = pathname;
while ((c=get_fs_byte(pathname++)))
if (c==‘/‘)
basename=pathname;
*namelen = pathname-basename-1;//得到tty0名字的长度
*name = basename;//得到tty0中第一个‘t’字符的地址
return dir;
} 代码路径:fs/namei.c
static struct m_inode * get_dir(const char * pathname)
{
char c;
const char * thisname;
struct m_inode * inode;
struct buffer_head * bh;
int namelen,inr,idev;
struct dir_entry * de;
if (!current->root || !current->root->i_count)
panic("No root inode");
if (!current->pwd || !current->pwd->i_count)
panic("No cwd inode");
if ((c=get_fs_byte(pathname))==‘/‘) {
inode = current->root;//根i节点
pathname++;//指向d
} else if (c)
inode = current->pwd;
else
return NULL; /* empty name is bad */
inode->i_count++;//根i节点i_count为5
while (1) {
thisname = pathname;
if (!S_ISDIR(inode->i_mode) || !permission(inode,MAY_EXEC)) {
iput(inode);//不执行
return NULL;
}
for(namelen=0;(c=get_fs_byte(pathname++))&&(c!=‘/‘);namelen++)//如果遇到/或者字符串结尾就退出
/* nothing */ ;
if (!c)
return inode;//第二次循环返回空了,执行到此,返回dev的i节点
if (!(bh = find_entry(&inode,thisname,namelen,&de))) {//此时根据根i节点和dev来查找dev的目录项,此时thisname为dev,namelen为3
iput(inode);
return NULL;
}
inr = de->inode;//dev的i节点号
idev = inode->i_dev;//虚拟盘,0x101
brelse(bh);
iput(inode);//根i节点i_count为4
if (!(inode = iget(idev,inr)))//获取了dev的i节点(inode_table第二个结构体),i_count为1
return NULL;
}
} 代码路径:include/linux/fs.h
...
#define NAME_LEN 14
...
struct dir_entry {
unsigned short inode;
char name[NAME_LEN];
}; 代码路径:include/linux/fs.h
...
struct m_inode {
unsigned short i_mode;
unsigned short i_uid;
unsigned long i_size;
unsigned long i_mtime;
unsigned char i_gid;
unsigned char i_nlinks;
unsigned short i_zone[9];
/* these are in memory also */
struct task_struct * i_wait;
unsigned long i_atime;
unsigned long i_ctime;
unsigned short i_dev;
unsigned short i_num;
unsigned short i_count;
unsigned char i_lock;
unsigned char i_dirt;
unsigned char i_pipe;
unsigned char i_mount;
unsigned char i_seek;
unsigned char i_update;
};
...
程序执行到了open_namei,找到了tty0的目录项,接下来继续执行:
int open_namei(const char * pathname, int flag, int mode,
struct m_inode ** res_inode)
{
...
bh = find_entry(&dir,basename,namelen,&de);
...
inr = de->inode;//tty0的i节点号
dev = dir->i_dev;//0x101
brelse(bh);
iput(dir);//第二个inode_table的i_count为0
if (flag & O_EXCL)
return -EEXIST;//不执行
if (!(inode=iget(dev,inr)))//得到了tty0的i节点,(inode_table第二个结构体),i_count为1
return -EACCES;
if ((S_ISDIR(inode->i_mode) && (flag & O_ACCMODE)) ||
!permission(inode,ACC_MODE(flag))) {
iput(inode);//不执行
return -EPERM;
}
inode->i_atime = CURRENT_TIME;
if (flag & O_TRUNC)
truncate(inode);//不执行
*res_inode = inode;
return 0;
}
返回sys_open继续执行:
...
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 if (!current->filp[fd])//找到进程第一个空闲的文件指针
break;
if (fd>=NR_OPEN)
return -EINVAL;
current->close_on_exec &= ~(1< f=0+file_table;
for (i=0 ; i if (!f->f_count) break;
if (i>=NR_FILE)
return -EINVAL;
(current->filp[fd]=f)->f_count++;//将进程1的filp[20]与file_table[64]挂接,并增加引用计数,f_count为1
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)) {//检查tty0文件的i节点属性,确定它是一个设备文件
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);//fd为0
}
... 至此进程1的current->filp[0]存放的file_table第一个元素地址,file_table第一个元素,又存放着inode的地址,f_count为1
2、打开标准输出、标准错误输出设备
又返回了进程1的3特权级,接着执行init()
代码路径:init/main.c
void init(void)
{
int pid,i;
setup((void *) &drive_info);
(void) open("/dev/tty0",O_RDWR,0);
(void) dup(0);
(void) dup(0);
...
} 执行dup(0),又陷入了进程1的0特权级,开始执行sys_dup
代码路径:fs/fcntl.c
static int dupfd(unsigned int fd, unsigned int arg)//fd为0,arg为0
{
if (fd >= NR_OPEN || !current->filp[fd])
return -EBADF;
if (arg >= NR_OPEN)
return -EINVAL;
while (arg if (current->filp[arg])
arg++;
else
break;//arg为1
if (arg >= NR_OPEN)
return -EMFILE;
current->close_on_exec &= ~(1< (current->filp[arg] = current->filp[fd])->f_count++;//0和1共同指向一个文件地址,并且f_count为2
return arg;
} 然后又返回进程1的3特权级,又一次执行dup(0),结果是current->filp[0],current->filp[1],current->filp[2]共同指向一个文件地址,并且f_count为3。
Linux内核设计的艺术-进程2的创建及执行,布布扣,bubuko.com