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

📄 exit.c

📁 Kernel code of linux kernel
💻 C
📖 第 1 页 / 共 4 页
字号:
/* *  linux/kernel/exit.c * *  Copyright (C) 1991, 1992  Linus Torvalds */#include <linux/mm.h>#include <linux/slab.h>#include <linux/interrupt.h>#include <linux/module.h>#include <linux/capability.h>#include <linux/completion.h>#include <linux/personality.h>#include <linux/tty.h>#include <linux/mnt_namespace.h>#include <linux/iocontext.h>#include <linux/key.h>#include <linux/security.h>#include <linux/cpu.h>#include <linux/acct.h>#include <linux/tsacct_kern.h>#include <linux/file.h>#include <linux/fdtable.h>#include <linux/binfmts.h>#include <linux/nsproxy.h>#include <linux/pid_namespace.h>#include <linux/ptrace.h>#include <linux/profile.h>#include <linux/mount.h>#include <linux/proc_fs.h>#include <linux/kthread.h>#include <linux/mempolicy.h>#include <linux/taskstats_kern.h>#include <linux/delayacct.h>#include <linux/freezer.h>#include <linux/cgroup.h>#include <linux/syscalls.h>#include <linux/signal.h>#include <linux/posix-timers.h>#include <linux/cn_proc.h>#include <linux/mutex.h>#include <linux/futex.h>#include <linux/compat.h>#include <linux/pipe_fs_i.h>#include <linux/audit.h> /* for audit_free() */#include <linux/resource.h>#include <linux/blkdev.h>#include <linux/task_io_accounting_ops.h>#include <linux/tracehook.h>#include <asm/uaccess.h>#include <asm/unistd.h>#include <asm/pgtable.h>#include <asm/mmu_context.h>static void exit_mm(struct task_struct * tsk);static inline int task_detached(struct task_struct *p){	return p->exit_signal == -1;}static void __unhash_process(struct task_struct *p){	nr_threads--;	detach_pid(p, PIDTYPE_PID);	if (thread_group_leader(p)) {		detach_pid(p, PIDTYPE_PGID);		detach_pid(p, PIDTYPE_SID);		list_del_rcu(&p->tasks);		__get_cpu_var(process_counts)--;	}	list_del_rcu(&p->thread_group);	list_del_init(&p->sibling);}/* * This function expects the tasklist_lock write-locked. */static void __exit_signal(struct task_struct *tsk){	struct signal_struct *sig = tsk->signal;	struct sighand_struct *sighand;	BUG_ON(!sig);	BUG_ON(!atomic_read(&sig->count));	sighand = rcu_dereference(tsk->sighand);	spin_lock(&sighand->siglock);	posix_cpu_timers_exit(tsk);	if (atomic_dec_and_test(&sig->count))		posix_cpu_timers_exit_group(tsk);	else {		/*		 * If there is any task waiting for the group exit		 * then notify it:		 */		if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count)			wake_up_process(sig->group_exit_task);		if (tsk == sig->curr_target)			sig->curr_target = next_thread(tsk);		/*		 * Accumulate here the counters for all threads but the		 * group leader as they die, so they can be added into		 * the process-wide totals when those are taken.		 * The group leader stays around as a zombie as long		 * as there are other threads.  When it gets reaped,		 * the exit.c code will add its counts into these totals.		 * We won't ever get here for the group leader, since it		 * will have been the last reference on the signal_struct.		 */		sig->utime = cputime_add(sig->utime, task_utime(tsk));		sig->stime = cputime_add(sig->stime, task_stime(tsk));		sig->gtime = cputime_add(sig->gtime, task_gtime(tsk));		sig->min_flt += tsk->min_flt;		sig->maj_flt += tsk->maj_flt;		sig->nvcsw += tsk->nvcsw;		sig->nivcsw += tsk->nivcsw;		sig->inblock += task_io_get_inblock(tsk);		sig->oublock += task_io_get_oublock(tsk);		task_io_accounting_add(&sig->ioac, &tsk->ioac);		sig->sum_sched_runtime += tsk->se.sum_exec_runtime;		sig = NULL; /* Marker for below. */	}	__unhash_process(tsk);	/*	 * Do this under ->siglock, we can race with another thread	 * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.	 */	flush_sigqueue(&tsk->pending);	tsk->signal = NULL;	tsk->sighand = NULL;	spin_unlock(&sighand->siglock);	__cleanup_sighand(sighand);	clear_tsk_thread_flag(tsk,TIF_SIGPENDING);	if (sig) {		flush_sigqueue(&sig->shared_pending);		taskstats_tgid_free(sig);		__cleanup_signal(sig);	}}static void delayed_put_task_struct(struct rcu_head *rhp){	put_task_struct(container_of(rhp, struct task_struct, rcu));}void release_task(struct task_struct * p){	struct task_struct *leader;	int zap_leader;repeat:	tracehook_prepare_release_task(p);	atomic_dec(&p->user->processes);	proc_flush_task(p);	write_lock_irq(&tasklist_lock);	tracehook_finish_release_task(p);	__exit_signal(p);	/*	 * If we are the last non-leader member of the thread	 * group, and the leader is zombie, then notify the	 * group leader's parent process. (if it wants notification.)	 */	zap_leader = 0;	leader = p->group_leader;	if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {		BUG_ON(task_detached(leader));		do_notify_parent(leader, leader->exit_signal);		/*		 * If we were the last child thread and the leader has		 * exited already, and the leader's parent ignores SIGCHLD,		 * then we are the one who should release the leader.		 *		 * do_notify_parent() will have marked it self-reaping in		 * that case.		 */		zap_leader = task_detached(leader);		/*		 * This maintains the invariant that release_task()		 * only runs on a task in EXIT_DEAD, just for sanity.		 */		if (zap_leader)			leader->exit_state = EXIT_DEAD;	}	write_unlock_irq(&tasklist_lock);	release_thread(p);	call_rcu(&p->rcu, delayed_put_task_struct);	p = leader;	if (unlikely(zap_leader))		goto repeat;}/* * This checks not only the pgrp, but falls back on the pid if no * satisfactory pgrp is found. I dunno - gdb doesn't work correctly * without this... * * The caller must hold rcu lock or the tasklist lock. */struct pid *session_of_pgrp(struct pid *pgrp){	struct task_struct *p;	struct pid *sid = NULL;	p = pid_task(pgrp, PIDTYPE_PGID);	if (p == NULL)		p = pid_task(pgrp, PIDTYPE_PID);	if (p != NULL)		sid = task_session(p);	return sid;}/* * Determine if a process group is "orphaned", according to the POSIX * definition in 2.2.2.52.  Orphaned process groups are not to be affected * by terminal-generated stop signals.  Newly orphaned process groups are * to receive a SIGHUP and a SIGCONT. * * "I ask you, have you ever known what it is to be an orphan?" */static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task){	struct task_struct *p;	do_each_pid_task(pgrp, PIDTYPE_PGID, p) {		if ((p == ignored_task) ||		    (p->exit_state && thread_group_empty(p)) ||		    is_global_init(p->real_parent))			continue;		if (task_pgrp(p->real_parent) != pgrp &&		    task_session(p->real_parent) == task_session(p))			return 0;	} while_each_pid_task(pgrp, PIDTYPE_PGID, p);	return 1;}int is_current_pgrp_orphaned(void){	int retval;	read_lock(&tasklist_lock);	retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);	read_unlock(&tasklist_lock);	return retval;}static int has_stopped_jobs(struct pid *pgrp){	int retval = 0;	struct task_struct *p;	do_each_pid_task(pgrp, PIDTYPE_PGID, p) {		if (!task_is_stopped(p))			continue;		retval = 1;		break;	} while_each_pid_task(pgrp, PIDTYPE_PGID, p);	return retval;}/* * Check to see if any process groups have become orphaned as * a result of our exiting, and if they have any stopped jobs, * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) */static voidkill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent){	struct pid *pgrp = task_pgrp(tsk);	struct task_struct *ignored_task = tsk;	if (!parent)		 /* exit: our father is in a different pgrp than		  * we are and we were the only connection outside.		  */		parent = tsk->real_parent;	else		/* reparent: our child is in a different pgrp than		 * we are, and it was the only connection outside.		 */		ignored_task = NULL;	if (task_pgrp(parent) != pgrp &&	    task_session(parent) == task_session(tsk) &&	    will_become_orphaned_pgrp(pgrp, ignored_task) &&	    has_stopped_jobs(pgrp)) {		__kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);		__kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);	}}/** * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd * * If a kernel thread is launched as a result of a system call, or if * it ever exits, it should generally reparent itself to kthreadd so it * isn't in the way of other processes and is correctly cleaned up on exit. * * The various task state such as scheduling policy and priority may have * been inherited from a user process, so we reset them to sane values here. * * NOTE that reparent_to_kthreadd() gives the caller full capabilities. */static void reparent_to_kthreadd(void){	write_lock_irq(&tasklist_lock);	ptrace_unlink(current);	/* Reparent to init */	current->real_parent = current->parent = kthreadd_task;	list_move_tail(&current->sibling, &current->real_parent->children);	/* Set the exit signal to SIGCHLD so we signal init on exit */	current->exit_signal = SIGCHLD;	if (task_nice(current) < 0)		set_user_nice(current, 0);	/* cpus_allowed? */	/* rt_priority? */	/* signals? */	security_task_reparent_to_init(current);	memcpy(current->signal->rlim, init_task.signal->rlim,	       sizeof(current->signal->rlim));	atomic_inc(&(INIT_USER->__count));	write_unlock_irq(&tasklist_lock);	switch_uid(INIT_USER);}void __set_special_pids(struct pid *pid){	struct task_struct *curr = current->group_leader;	pid_t nr = pid_nr(pid);	if (task_session(curr) != pid) {		change_pid(curr, PIDTYPE_SID, pid);		set_task_session(curr, nr);	}	if (task_pgrp(curr) != pid) {		change_pid(curr, PIDTYPE_PGID, pid);		set_task_pgrp(curr, nr);	}}static void set_special_pids(struct pid *pid){	write_lock_irq(&tasklist_lock);	__set_special_pids(pid);	write_unlock_irq(&tasklist_lock);}/* * Let kernel threads use this to say that they * allow a certain signal (since daemonize() will * have disabled all of them by default). */int allow_signal(int sig){	if (!valid_signal(sig) || sig < 1)		return -EINVAL;	spin_lock_irq(&current->sighand->siglock);	sigdelset(&current->blocked, sig);	if (!current->mm) {		/* Kernel threads handle their own signals.		   Let the signal code know it'll be handled, so		   that they don't get converted to SIGKILL or		   just silently dropped */		current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;	}	recalc_sigpending();	spin_unlock_irq(&current->sighand->siglock);	return 0;}EXPORT_SYMBOL(allow_signal);int disallow_signal(int sig){	if (!valid_signal(sig) || sig < 1)		return -EINVAL;	spin_lock_irq(&current->sighand->siglock);	current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;	recalc_sigpending();	spin_unlock_irq(&current->sighand->siglock);	return 0;}EXPORT_SYMBOL(disallow_signal);/* *	Put all the gunge required to become a kernel thread without *	attached user resources in one place where it belongs. */void daemonize(const char *name, ...){	va_list args;	struct fs_struct *fs;	sigset_t blocked;	va_start(args, name);	vsnprintf(current->comm, sizeof(current->comm), name, args);	va_end(args);	/*	 * If we were started as result of loading a module, close all of the	 * user space pages.  We don't need them, and if we didn't close them	 * they would be locked into memory.	 */	exit_mm(current);	/*	 * We don't want to have TIF_FREEZE set if the system-wide hibernation	 * or suspend transition begins right now.	 */	current->flags |= (PF_NOFREEZE | PF_KTHREAD);	if (current->nsproxy != &init_nsproxy) {		get_nsproxy(&init_nsproxy);		switch_task_namespaces(current, &init_nsproxy);	}	set_special_pids(&init_struct_pid);	proc_clear_tty(current);	/* Block and flush all signals */	sigfillset(&blocked);	sigprocmask(SIG_BLOCK, &blocked, NULL);	flush_signals(current);	/* Become as one with the init task */	exit_fs(current);	/* current->fs->count--; */	fs = init_task.fs;	current->fs = fs;	atomic_inc(&fs->count);	exit_files(current);	current->files = init_task.files;	atomic_inc(&current->files->count);	reparent_to_kthreadd();}EXPORT_SYMBOL(daemonize);

⌨️ 快捷键说明

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