/** * sys_kill - send a signal to a process * @pid: the PID of the process * @sig: signal to be sent */ SYSCALL_DEFINE2(kill, pid_t, pid, int, sig) { structkernel_siginfoinfo;// info是signal的结构体
/* * si_code values * Digital reserves positive values for kernel-generated signals. */ #define SI_USER 0 /* sent by kill, sigsend, raise 由kill、raise等发送的*/ #define SI_KERNEL 0x80 /* sent by the kernel from somewhere 从内核某处发送 */ #define SI_QUEUE -1 /* sent by sigqueue */ #define SI_TIMER -2 /* sent by timer expiration */ #define SI_MESGQ -3 /* sent by real time mesq state change */ #define SI_ASYNCIO -4 /* sent by AIO completion */ #define SI_SIGIO -5 /* sent by queued SIGIO */ #define SI_TKILL -6 /* sent by tkill system call */ #define SI_DETHREAD -7 /* sent by execve() killing subsidiary threads */ #define SI_ASYNCNL -60 /* sent by glibc async name lookup completion */
/* * We don't use read_sysreg() as we want the compiler to cache the value where * possible. */ static __always_inline struct task_struct *get_current(void) { unsignedlong sp_el0;
asm ("mrs %0, sp_el0" : "=r" (sp_el0));
return (struct task_struct *)sp_el0; }
#define current get_current() // 获取当前运行的task_struct
for (;;) { rcu_read_lock(); p = pid_task(pid, PIDTYPE_PID); if (p) error = group_send_sig_info(sig, info, p, PIDTYPE_TGID); rcu_read_unlock(); if (likely(!p || error != -ESRCH)) return error;
/* * The task was unhashed in between, try again. If it * is dead, pid_task() will return NULL, if we race with * de_thread() it will find the new leader. */ } }
pid_task:
1 2 3 4 5 6 7 8 9 10 11 12
struct task_struct *pid_task(struct pid *pid, enum pid_type type) { structtask_struct *result =NULL; if (pid) { structhlist_node *first; first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), lockdep_tasklist_lock_is_held()); if (first) result = hlist_entry(first, struct task_struct, pid_links[(type)]); } return result; }
/* * Bad permissions for sending the signal * - the caller must hold the RCU read lock */ staticintcheck_kill_permission(int sig, struct kernel_siginfo *info, struct task_struct *t) { structpid *sid; int error;
/** * audit_signal_info - record signal info for shutting down audit subsystem * @sig: signal value * @t: task being signaled * * If the audit subsystem is being terminated, record the task (pid) * and uid that is doing that. */ intaudit_signal_info(int sig, struct task_struct *t) { kuid_t uid = current_uid(), auid;
if (auditd_test_task(t) && // auditd_test_task判断是否是注册的审计进程,是返回1,否返回0 (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2)) { // 如果sig是SIGTERM、SIGHUP或用户自定义的SIGUSR1和SIGUSR2 audit_sig_pid = task_tgid_nr(current); auid = audit_get_loginuid(current); if (uid_valid(auid)) audit_sig_uid = auid; else audit_sig_uid = uid; security_task_getsecid(current, &audit_sig_sid); }
/** * auditd_test_task - Check to see if a given task is an audit daemon * @task: the task to check * * Description: * Return 1 if the task is a registered audit daemon, 0 otherwise. */ intauditd_test_task(struct task_struct *task) { int rc; structauditd_connection *ac;
staticintsend_signal(int sig, struct kernel_siginfo *info, struct task_struct *t, enum pid_type type) { /* Should SIGKILL or SIGSTOP be received by a pid namespace init? */ // pid namespace init是否需要接收SIGKILL or SIGSTOP bool force = false;
if (info == SEND_SIG_NOINFO) { /* Force if sent from an ancestor pid namespace */ // 如果来自祖先用户空间,则强制发送 force = !task_pid_nr_ns(current, task_active_pid_ns(t)); } elseif (info == SEND_SIG_PRIV) { /* Don't ignore kernel generated signals */ // 来自内核的信号不会被忽略 force = true; } elseif (has_si_pid_and_uid(info)) { /* SIGKILL and SIGSTOP is special or has ids */ struct user_namespace *t_user_ns;
/* SIGKILL and SIGSTOP may not be sent to the global init */ // SIGKILL and SIGSTOP不会被发送到global init进程 // 如果是SIGKILL and SIGSTOP目标进程是init进程 if (unlikely(is_global_init(t) && sig_kernel_only(sig))) returntrue;