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

📄 exec.c

📁 linux1.1源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/fs/exec.c * *  Copyright (C) 1991, 1992  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. * * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead, * current->executable is only used by the procfs.  This allows a dispatch * table to check for several different types  of binary formats.  We keep * trying until we recognize the file or we run out of supported binary * formats.  */#include <linux/fs.h>#include <linux/sched.h>#include <linux/kernel.h>#include <linux/mm.h>#include <linux/mman.h>#include <linux/a.out.h>#include <linux/errno.h>#include <linux/signal.h>#include <linux/string.h>#include <linux/stat.h>#include <linux/fcntl.h>#include <linux/ptrace.h>#include <linux/user.h>#include <linux/segment.h>#include <linux/malloc.h>#include <asm/system.h>#include <linux/binfmts.h>#include <asm/segment.h>#include <asm/system.h>asmlinkage int sys_exit(int exit_code);asmlinkage int sys_close(unsigned fd);asmlinkage int sys_open(const char *, int, int);asmlinkage int sys_brk(unsigned long);extern void shm_exit (void);int open_inode(struct inode * inode, int mode){	int error, fd;	struct file *f, **fpp;	if (!inode->i_op || !inode->i_op->default_file_ops)		return -EINVAL;	f = get_empty_filp();	if (!f)		return -EMFILE;	fd = 0;	fpp = current->filp;	for (;;) {		if (!*fpp)			break;		if (++fd > NR_OPEN)			return -ENFILE;		fpp++;	}	*fpp = f;	f->f_flags = mode;	f->f_mode = (mode+1) & O_ACCMODE;	f->f_inode = inode;	f->f_pos = 0;	f->f_reada = 0;	f->f_op = inode->i_op->default_file_ops;	if (f->f_op->open) {		error = f->f_op->open(inode,f);		if (error) {			*fpp = NULL;			f->f_count--;			return error;		}	}	inode->i_count++;	return fd;}/* * These are the only things you should do on a core-file: use only these * macros to write out all the necessary info. */#define DUMP_WRITE(addr,nr) \while (file.f_op->write(inode,&file,(char *)(addr),(nr)) != (nr)) goto close_coredump#define DUMP_SEEK(offset) \if (file.f_op->lseek) { \	if (file.f_op->lseek(inode,&file,(offset),0) != (offset)) \ 		goto close_coredump; \} else file.f_pos = (offset)		/* * Routine writes a core dump image in the current directory. * Currently only a stub-function. * * Note that setuid/setgid files won't make a core-dump if the uid/gid * changed due to the set[u|g]id. It's enforced by the "current->dumpable" * field, which also makes sure the core-dumps won't be recursive if the * dumping of the process results in another error.. */int core_dump(long signr, struct pt_regs * regs){	struct inode * inode = NULL;	struct file file;	unsigned short fs;	int has_dumped = 0;	char corefile[6+sizeof(current->comm)];	int i;	register int dump_start, dump_size;	struct user dump;	if (!current->dumpable)		return 0;	current->dumpable = 0;/* See if we have enough room to write the upage.  */	if (current->rlim[RLIMIT_CORE].rlim_cur < PAGE_SIZE)		return 0;	fs = get_fs();	set_fs(KERNEL_DS);	memcpy(corefile,"core.",5);#if 0	memcpy(corefile+5,current->comm,sizeof(current->comm));#else	corefile[4] = '\0';#endif	if (open_namei(corefile,O_CREAT | 2 | O_TRUNC,0600,&inode,NULL)) {		inode = NULL;		goto end_coredump;	}	if (!S_ISREG(inode->i_mode))		goto end_coredump;	if (!inode->i_op || !inode->i_op->default_file_ops)		goto end_coredump;	file.f_mode = 3;	file.f_flags = 0;	file.f_count = 1;	file.f_inode = inode;	file.f_pos = 0;	file.f_reada = 0;	file.f_op = inode->i_op->default_file_ops;	if (file.f_op->open)		if (file.f_op->open(inode,&file))			goto end_coredump;	if (!file.f_op->write)		goto close_coredump;	has_dumped = 1;/* changed the size calculations - should hopefully work better. lbt */	dump.magic = CMAGIC;	dump.start_code = 0;	dump.start_stack = regs->esp & ~(PAGE_SIZE - 1);	dump.u_tsize = ((unsigned long) current->end_code) >> 12;	dump.u_dsize = ((unsigned long) (current->brk + (PAGE_SIZE-1))) >> 12;	dump.u_dsize -= dump.u_tsize;	dump.u_ssize = 0;	for(i=0; i<8; i++) dump.u_debugreg[i] = current->debugreg[i];  	if (dump.start_stack < TASK_SIZE)		dump.u_ssize = ((unsigned long) (TASK_SIZE - dump.start_stack)) >> 12;/* If the size of the dump file exceeds the rlimit, then see what would happen   if we wrote the stack, but not the data area.  */	if ((dump.u_dsize+dump.u_ssize+1) * PAGE_SIZE >	    current->rlim[RLIMIT_CORE].rlim_cur)		dump.u_dsize = 0;/* Make sure we have enough room to write the stack and data areas. */	if ((dump.u_ssize+1) * PAGE_SIZE >	    current->rlim[RLIMIT_CORE].rlim_cur)		dump.u_ssize = 0;       	strncpy(dump.u_comm, current->comm, sizeof(current->comm));	dump.u_ar0 = (struct pt_regs *)(((int)(&dump.regs)) -((int)(&dump)));	dump.signal = signr;	dump.regs = *regs;/* Flag indicating the math stuff is valid. We don't support this for the   soft-float routines yet */	if (hard_math) {		if ((dump.u_fpvalid = current->used_math) != 0) {			if (last_task_used_math == current)				__asm__("clts ; fnsave %0": :"m" (dump.i387));			else				memcpy(&dump.i387,&current->tss.i387.hard,sizeof(dump.i387));		}	} else {		/* we should dump the emulator state here, but we need to		   convert it into standard 387 format first.. */		dump.u_fpvalid = 0;	}	set_fs(KERNEL_DS);/* struct user */	DUMP_WRITE(&dump,sizeof(dump));/* Now dump all of the user data.  Include malloced stuff as well */	DUMP_SEEK(PAGE_SIZE);/* now we start writing out the user space info */	set_fs(USER_DS);/* Dump the data area */	if (dump.u_dsize != 0) {		dump_start = dump.u_tsize << 12;		dump_size = dump.u_dsize << 12;		DUMP_WRITE(dump_start,dump_size);	};/* Now prepare to dump the stack area */	if (dump.u_ssize != 0) {		dump_start = dump.start_stack;		dump_size = dump.u_ssize << 12;		DUMP_WRITE(dump_start,dump_size);	};/* Finally dump the task struct.  Not be used by gdb, but could be useful */	set_fs(KERNEL_DS);	DUMP_WRITE(current,sizeof(*current));close_coredump:	if (file.f_op->release)		file.f_op->release(inode,&file);end_coredump:	set_fs(fs);	iput(inode);	return has_dumped;}/* * Note that a shared library must be both readable and executable due to * security reasons. * * Also note that we take the address to load from from the file itself. */asmlinkage int sys_uselib(const char * library){	int fd, retval;	struct file * file;	struct linux_binfmt * fmt;	fd = sys_open(library, 0, 0);	if (fd < 0)		return fd;	file = current->filp[fd];	retval = -ENOEXEC;	if (file && file->f_inode && file->f_op && file->f_op->read) {		fmt = formats;		do {			int (*fn)(int) = fmt->load_shlib;			if (!fn)				break;			retval = fn(fd);			fmt++;		} while (retval == -ENOEXEC);	}	sys_close(fd);  	return retval;}/* * 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. */unsigned long * create_tables(char * p,int argc,int envc,int ibcs){	unsigned long *argv,*envp;	unsigned long * sp;	struct vm_area_struct *mpnt;	mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);	if (mpnt) {		mpnt->vm_task = current;		mpnt->vm_start = PAGE_MASK & (unsigned long) p;		mpnt->vm_end = TASK_SIZE;		mpnt->vm_page_prot = PAGE_PRIVATE|PAGE_DIRTY;		mpnt->vm_share = NULL;		mpnt->vm_inode = NULL;		mpnt->vm_offset = 0;		mpnt->vm_ops = NULL;		insert_vm_struct(current, mpnt);		current->stk_vma = mpnt;	}	sp = (unsigned long *) (0xfffffffc & (unsigned long) p);	sp -= envc+1;	envp = sp;	sp -= argc+1;	argv = sp;	if (!ibcs) {		put_fs_long((unsigned long)envp,--sp);		put_fs_long((unsigned long)argv,--sp);	}	put_fs_long((unsigned long)argc,--sp);	current->arg_start = (unsigned long) p;	while (argc-->0) {		put_fs_long((unsigned long) p,argv++);		while (get_fs_byte(p++)) /* nothing */ ;	}	put_fs_long(0,argv);	current->arg_end = current->env_start = (unsigned long) p;	while (envc-->0) {		put_fs_long((unsigned long) p,envp++);		while (get_fs_byte(p++)) /* nothing */ ;	}	put_fs_long(0,envp);	current->env_end = (unsigned long) p;	return sp;}/* * count() counts the number of arguments/envelopes */static int count(char ** argv){	int i=0;	char ** tmp;	if ((tmp = argv) != 0)		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. */unsigned long copy_strings(int argc,char ** argv,unsigned long *page,		unsigned long p, int from_kmem){	char *tmp, *pag = NULL;	int len, offset = 0;	unsigned long old_fs, new_fs;	if (!p)		return 0;	/* bullet-proofing */	new_fs = get_ds();	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("VFS: 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) {	/* 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(GFP_USER))) 					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;}unsigned long change_ldt(unsigned long text_size,unsigned long * page){	unsigned long code_limit,data_limit,code_base,data_base;	int i;	code_limit = TASK_SIZE;	data_limit = TASK_SIZE;	code_base = data_base = 0;	current->start_code = code_base;	data_base += data_limit;	for (i=MAX_ARG_PAGES-1 ; i>=0 ; i--) {		data_base -= PAGE_SIZE;		if (page[i]) {			current->rss++;			put_dirty_page(current,page[i],data_base);		}	}	return data_limit;}/* * Read in the complete executable. This is used for "-N" files * that aren't on a block boundary, and for files on filesystems * without bmap support. */int read_exec(struct inode *inode, unsigned long offset,	char * addr, unsigned long count){	struct file file;	int result = -ENOEXEC;	if (!inode->i_op || !inode->i_op->default_file_ops)		goto end_readexec;	file.f_mode = 1;	file.f_flags = 0;	file.f_count = 1;	file.f_inode = inode;	file.f_pos = 0;	file.f_reada = 0;	file.f_op = inode->i_op->default_file_ops;	if (file.f_op->open)		if (file.f_op->open(inode,&file))			goto end_readexec;	if (!file.f_op || !file.f_op->read)		goto close_readexec;	if (file.f_op->lseek) {		if (file.f_op->lseek(inode,&file,offset,0) != offset) 			goto close_readexec;	} else		file.f_pos = offset;	if (get_fs() == USER_DS) {		result = verify_area(VERIFY_WRITE, addr, count);		if (result)			goto close_readexec;	}	result = file.f_op->read(inode, &file, addr, count);close_readexec:	if (file.f_op->release)		file.f_op->release(inode,&file);end_readexec:	return result;}

⌨️ 快捷键说明

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