📄 exec.c.bak
字号:
/* * linux/fs/exec.c * * (C) 1991 Linus Torvalds *//* * #!-checking implemented by tytso. *//* * Demand-loading implemented 01.12.91 - no need to read anything but * the header into memory. The inode of the executable is put into * "current->executable", and page faults do the actual loading. Clean. * * Once more I can proudly say that linux stood up to being changed: it * was less than 2 hours work to get demand-loading completely implemented. */#include <errno.h>#include <string.h>#include <sys/stat.h>#include <a.out.h>#include <linux/fs.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/mm.h>#include <asm/segment.h>extern int sys_exit(int exit_code);extern int sys_close(int fd);/* * MAX_ARG_PAGES defines the number of pages allocated for arguments * and envelope for the new program. 32 should suffice, this gives * a maximum env+arg of 128kB ! */#define MAX_ARG_PAGES 32/*进程的命令行参数和环境变量所占的最大页面数*//* * create_tables() parses the env- and arg-strings in new user * memory and creates the pointer tables from them, and puts their * addresses on the "stack", returning the new stack pointer value. */static unsigned long * create_tables(char * p,int argc,int envc) /*创建命令行参数和环境变量表,p526*/{ /* 这里会引起缺页中断 */ unsigned long *argv,*envp; unsigned long * sp; sp = (unsigned long *) (0xfffffffc & (unsigned long) p); sp -= envc+1; envp = sp; sp -= argc+1; argv = sp; put_fs_long((unsigned long)envp,--sp); put_fs_long((unsigned long)argv,--sp); put_fs_long((unsigned long)argc,--sp); while (argc-->0) { put_fs_long((unsigned long) p,argv++); while (get_fs_byte(p++)) /* nothing */ ; } put_fs_long(0,argv); while (envc-->0) { put_fs_long((unsigned long) p,envp++); while (get_fs_byte(p++)) /* nothing */ ; } put_fs_long(0,envp); return sp;}/* * count() counts the number of arguments/envelopes */static int count(char ** argv)/*计算命令行/环境变量的个数*/{ int i=0; char ** tmp; if (tmp = argv) while (get_fs_long((unsigned long *) (tmp++))) i++; return i;}/* * 'copy_string()' copies argument/envelope strings from user * memory to free pages in kernel mem. These are in a format ready * to be put directly into the top of new user memory. * * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies * whether the string and the string array are from user or kernel segments: * * from_kmem argv * argv ** * 0 user space user space * 1 kernel space user space * 2 kernel space kernel space * * We do this by playing games with the fs segment register. Since it * it is expensive to load a segment register, we try to avoid calling * set_fs() unless we absolutely have to. */static unsigned long copy_strings(int argc,char ** argv,unsigned long *page,/*自argv字符指针数组中复制命令行参数和环境变量至page页结构数组所指的页中(自末端开始)*/ unsigned long p, int from_kmem){ char *tmp, *pag; int len, offset = 0; unsigned long old_fs, new_fs; if (!p) return 0; /* bullet-proofing */ new_fs = get_ds();/*segment.h*/ old_fs = get_fs(); if (from_kmem==2) set_fs(new_fs); while (argc-- > 0) { if (from_kmem == 1) set_fs(new_fs); if (!(tmp = (char *)get_fs_long(((unsigned long *)argv)+argc))) panic("argc is wrong"); if (from_kmem == 1) set_fs(old_fs); len=0; /* remember zero-padding */ do { len++; } while (get_fs_byte(tmp++)); if (p-len < 0) { /* this shouldn't happen - 128kB */ set_fs(old_fs); return 0; } while (len) { --p; --tmp; --len; if (--offset < 0) { offset = p % PAGE_SIZE; if (from_kmem==2) set_fs(old_fs); if (!(pag = (char *) page[p/PAGE_SIZE]) && !(pag = (char *) page[p/PAGE_SIZE] = (unsigned long *) get_free_page())) return 0; if (from_kmem==2) set_fs(new_fs); } *(pag + offset) = get_fs_byte(tmp); } } if (from_kmem==2) set_fs(old_fs); return p;}static unsigned long change_ldt(unsigned long text_size,unsigned long * page)/*修改进程ldt中的项(根据加载的文件适当修改代码段限长)*/{ /*并将page中的环境变量和命令行参数放到进程末端*/ unsigned long code_limit,data_limit,code_base,data_base; int i; code_limit = text_size+PAGE_SIZE -1; code_limit &= 0xFFFFF000; data_limit = 0x4000000; code_base = get_base(current->ldt[1]); data_base = code_base; set_base(current->ldt[1],code_base); set_limit(current->ldt[1],code_limit); set_base(current->ldt[2],data_base); set_limit(current->ldt[2],data_limit);/* make sure fs points to the NEW data segment */ __asm__("pushl $0x17\n\tpop %%fs"::);/*刷新了fs中的不可见部分*/ data_base += data_limit; for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) { data_base -= PAGE_SIZE; if (page[i]) put_page(page[i],data_base);/*memery.c ,197,用于将页面映射进进程的逻辑空间*/ } return data_limit;}/* * 'do_execve()' executes a new program. */int do_execve(unsigned long * eip,long tmp,char * filename,/*p523*/ char ** argv, char ** envp) /*filename,argv,envp为调用系统调用时压入堆栈;tmp为执行call _sys_call_table时压入堆栈(无用)*/{ /*eip为call _do_execve之前压入堆栈,最终call _do_execve时压入堆栈的函数返回地址不作为参数处理*/ struct m_inode * inode; struct buffer_head * bh; struct exec ex; unsigned long page[MAX_ARG_PAGES]; int i,argc,envc; int e_uid, e_gid; int retval; int sh_bang = 0; unsigned long p=PAGE_SIZE*MAX_ARG_PAGES-4;/* p指向128k空间的末端 */ if ((0xffff & eip[1]) != 0x000f) /* 此时eip[0]为用户态进程调用系统调用时的ip地址,eip[1]为cs段选择符*/ panic("execve called from supervisor mode"); for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */ page[i]=0; if (!(inode=namei(filename))) /* get executables inode *//* 此处的filename为用户数据段中的一个偏移地址 */ return -ENOENT; argc = count(argv); envc = count(envp); restart_interp: if (!S_ISREG(inode->i_mode)) { /* must be regular file */ retval = -EACCES; goto exec_error2; /* 释放已占用资源并退出 */ } i = inode->i_mode; /*判断当前进程的执行权限,p533 */ e_uid = (i & S_ISUID) ? inode->i_uid : current->euid; e_gid = (i & S_ISGID) ? inode->i_gid : current->egid; if (current->euid == inode->i_uid) i >>= 6; else if (current->egid == inode->i_gid) i >>= 3; if (!(i & 1) && !((inode->i_mode & 0111) && suser())) { retval = -ENOEXEC; goto exec_error2; } if (!(bh = bread(inode->i_dev,inode->i_zone[0]))) {/*度入文件的第一个数据块*/ retval = -EACCES; goto exec_error2; } ex = *((struct exec *) bh->b_data); /* read exec-header */ if ((bh->b_data[0] == '#') && (bh->b_data[1] == '!') && (!sh_bang)) {/* 若该文件为脚本文件(以#!开头,需解释程序才能运行) */ /* * This section does the #! interpretation. * Sorta complicated, but hopefully it will work. -TYT */ char buf[1023], *cp, *interp, *i_name, *i_arg; unsigned long old_fs; strncpy(buf, bh->b_data+2, 1022);/*读入#!后的字符(其中包括解释程序名及参数) */ brelse(bh); iput(inode); buf[1022] = '\0'; if (cp = strchr(buf, '\n')) {/* strchr在指定串中查指定的字符,返回该字符指针 */ *cp = '\0'; /* 第一个回车换为null */ for (cp = buf; (*cp == ' ') || (*cp == '\t'); cp++);/* 去开头空格空格 */ } if (!cp || *cp == '\0') {/* 若无回车,则说明没内容 */ retval = -ENOEXEC; /* No interpreter name found */ goto exec_error1; } interp = i_name = cp;/* 解释程序名指针 */ i_arg = 0; for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) { if (*cp == '/') i_name = cp+1; } if (*cp) { *cp++ = '\0'; i_arg = cp;/* 解释程序参数指针 */ } /* * OK, we've parsed out the interpreter name and * (optional) argument. */ if (sh_bang++ == 0) { p = copy_strings(envc, envp, page, p, 0);/* 复制脚本文件环境变量 */ p = copy_strings(--argc, argv+1, page, p, 0);/*复制脚本文件参数(不带脚本文件名) */ } /* * Splice in (1) the interpreter's name for argv[0] * (2) (optional) argument to interpreter * (3) filename of shell script * * This is done in reverse order, because of how the * user environment and arguments are stored. */ p = copy_strings(1, &filename, page, p, 1);/* 复制脚本文件名 */ argc++; if (i_arg) { p = copy_strings(1, &i_arg, page, p, 2);/*复制解释程序参数 */ argc++; } p = copy_strings(1, &i_name, page, p, 2);/*复制解释程序名 */ argc++; /* 此时page所指处中的数据型如:解释程序名 解释程序参数 脚本名 脚本参数 脚本环境变量*/ if (!p) { retval = -ENOMEM; goto exec_error1; } /* * OK, now restart the process with the interpreter's inode. */ old_fs = get_fs(); set_fs(get_ds()); /* 由于namei的参数为用户数据段中的字符串,而此处interp为内核数据段中的数据,故需将fs零时设为ds(内核数据段) */ if (!(inode=namei(interp))) { /* get executables inode *//* 取得解释程序i节点 */ set_fs(old_fs); retval = -ENOENT; goto exec_error1; } set_fs(old_fs); goto restart_interp; } brelse(bh); if (N_MAGIC(ex) != ZMAGIC || ex.a_trsize || ex.a_drsize ||/* linux0.11仅能执行可执行文件(无重定位信息)同时文件不能太长,也不能与执行头部中的信息不符 */ ex.a_text+ex.a_data+ex.a_bss>0x3000000 || inode->i_size < ex.a_text+ex.a_data+ex.a_syms+N_TXTOFF(ex)) { retval = -ENOEXEC; goto exec_error2; } if (N_TXTOFF(ex) != BLOCK_SIZE) {/* 执行文件的起始点必须为一页的边界*/ printk("%s: N_TXTOFF != BLOCK_SIZE. See a.out.h.", filename); retval = -ENOEXEC; goto exec_error2; } if (!sh_bang) {/* sh_bang置位则说明将运行解释程序(复制操作已完成),否则说明复制操作未完成 */ p = copy_strings(envc,envp,page,p,0); p = copy_strings(argc,argv,page,p,0); if (!p) { retval = -ENOMEM; goto exec_error2; } }/* OK, This is the point of no return */ if (current->executable)/* 对进程加载执行文件的实质操作从这里开始(p,537) */ iput(current->executable);/* executable为进程执行文件的i节点 */ current->executable = inode; for (i=0 ; i<32 ; i++) current->sigaction[i].sa_handler = NULL;/*复位所有信号处理句柄*/ for (i=0 ; i<NR_OPEN ; i++) if ((current->close_on_exec>>i)&1) sys_close(i);/* 根据close_on_exec位图关闭指定文件*/ current->close_on_exec = 0; free_page_tables(get_base(current->ldt[1]),get_limit(0x0f));/* 释放原来的页表及页*/ free_page_tables(get_base(current->ldt[2]),get_limit(0x17)); if (last_task_used_math == current)/*协处理器 */ last_task_used_math = NULL; current->used_math = 0; p += change_ldt(ex.a_text,page)-MAX_ARG_PAGES*PAGE_SIZE;/* 根据执行文件的执行头结构重设ldt */ p = (unsigned long) create_tables((char *)p,argc,envc);/* 将128k的环境变量和命令行参数放入进程逻辑空间末端*/ current->brk = ex.a_bss + /* 根据执行文件的执行头结构修改pcb*/ (current->end_data = ex.a_data + (current->end_code = ex.a_text)); current->start_stack = p & 0xfffff000;/* 进程用户态堆栈顶(64m-环境变量和命令行参数的空间) */ current->euid = e_uid; current->egid = e_gid; i = ex.a_text+ex.a_data; while (i&0xfff) /*无用 */ put_fs_byte(0,(char *) (i++)); eip[0] = ex.a_entry; /* eip, magic happens :-) *//* 使进程返回用户态时返回到新的执行文件开始处 */ eip[3] = p; /* stack pointer *//* 重设进程堆栈 */ return 0;exec_error2: iput(inode);exec_error1: for (i=0 ; i<MAX_ARG_PAGES ; i++) free_page(page[i]); return(retval);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -