From owner-svn-src-all@freebsd.org Sat Dec 7 18:07:50 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C67B91C8C3D; Sat, 7 Dec 2019 18:07:50 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47Vcr64rNfz43r8; Sat, 7 Dec 2019 18:07:50 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8A1E31D9AD; Sat, 7 Dec 2019 18:07:50 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xB7I7ol1047699; Sat, 7 Dec 2019 18:07:50 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xB7I7oVB047697; Sat, 7 Dec 2019 18:07:50 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201912071807.xB7I7oVB047697@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Sat, 7 Dec 2019 18:07:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r355500 - in head: lib/libc/sys sys/kern X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: in head: lib/libc/sys sys/kern X-SVN-Commit-Revision: 355500 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 07 Dec 2019 18:07:50 -0000 Author: kib Date: Sat Dec 7 18:07:49 2019 New Revision: 355500 URL: https://svnweb.freebsd.org/changeset/base/355500 Log: Only return EPERM from kill(-pid) when no process was signalled. As mandated by POSIX. Also clarify the kill(2) manpage. While there, restructure the code in killpg1() to use helper which keeps overall state of the process list iteration in the killpg1_ctx structued, later used to infer the error returned. Reported by: amdmi3 Reviewed by: jilles Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D22621 Modified: head/lib/libc/sys/kill.2 head/sys/kern/kern_sig.c Modified: head/lib/libc/sys/kill.2 ============================================================================== --- head/lib/libc/sys/kill.2 Sat Dec 7 17:54:40 2019 (r355499) +++ head/lib/libc/sys/kill.2 Sat Dec 7 18:07:49 2019 (r355500) @@ -28,7 +28,7 @@ .\" @(#)kill.2 8.3 (Berkeley) 4/19/94 .\" $FreeBSD$ .\" -.Dd December 1, 2017 +.Dd December 1, 2019 .Dt KILL 2 .Os .Sh NAME @@ -105,12 +105,11 @@ process with ID 1 .Xr init 8 ) , and the process sending the signal. If the user is not the super user, the signal is sent to all processes -with the same uid as the user excluding the process sending the signal. +which the caller has permissions to, excluding the process sending the signal. No error is returned if any process could be signaled. .El .Pp -For compatibility with System V, -if the process number is negative but not -1, +If the process number is negative but not -1, the signal is sent to all processes whose process group ID is equal to the absolute value of the process number. This is a variant of @@ -134,7 +133,7 @@ No process or process group can be found corresponding .It Bq Er EPERM The sending process does not have permission to send .Va sig -to the receiving process. +to any receiving process. .El .Sh SEE ALSO .Xr getpgrp 2 , Modified: head/sys/kern/kern_sig.c ============================================================================== --- head/sys/kern/kern_sig.c Sat Dec 7 17:54:40 2019 (r355499) +++ head/sys/kern/kern_sig.c Sat Dec 7 18:07:49 2019 (r355500) @@ -1679,6 +1679,36 @@ kern_sigaltstack(struct thread *td, stack_t *ss, stack return (0); } +struct killpg1_ctx { + struct thread *td; + ksiginfo_t *ksi; + int sig; + bool sent; + bool found; + int ret; +}; + +static void +killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg) +{ + int err; + + if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 || + (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW) + return; + PROC_LOCK(p); + err = p_cansignal(arg->td, p, arg->sig); + if (err == 0 && arg->sig != 0) + pksignal(p, arg->sig, arg->ksi); + PROC_UNLOCK(p); + if (err != ESRCH) + arg->found = true; + if (err == 0) + arg->sent = true; + else if (arg->ret == 0 && err != ESRCH && err != EPERM) + arg->ret = err; +} + /* * Common code for kill process group/broadcast kill. * cp is calling process. @@ -1688,30 +1718,21 @@ killpg1(struct thread *td, int sig, int pgid, int all, { struct proc *p; struct pgrp *pgrp; - int err; - int ret; + struct killpg1_ctx arg; - ret = ESRCH; + arg.td = td; + arg.ksi = ksi; + arg.sig = sig; + arg.sent = false; + arg.found = false; + arg.ret = 0; if (all) { /* * broadcast */ sx_slock(&allproc_lock); FOREACH_PROC_IN_SYSTEM(p) { - if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || - p == td->td_proc || p->p_state == PRS_NEW) { - continue; - } - PROC_LOCK(p); - err = p_cansignal(td, p, sig); - if (err == 0) { - if (sig) - pksignal(p, sig, ksi); - ret = err; - } - else if (ret == ESRCH) - ret = err; - PROC_UNLOCK(p); + killpg1_sendsig(p, true, &arg); } sx_sunlock(&allproc_lock); } else { @@ -1731,25 +1752,14 @@ killpg1(struct thread *td, int sig, int pgid, int all, } sx_sunlock(&proctree_lock); LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { - PROC_LOCK(p); - if (p->p_pid <= 1 || p->p_flag & P_SYSTEM || - p->p_state == PRS_NEW) { - PROC_UNLOCK(p); - continue; - } - err = p_cansignal(td, p, sig); - if (err == 0) { - if (sig) - pksignal(p, sig, ksi); - ret = err; - } - else if (ret == ESRCH) - ret = err; - PROC_UNLOCK(p); + killpg1_sendsig(p, false, &arg); } PGRP_UNLOCK(pgrp); } - return (ret); + MPASS(arg.ret != 0 || arg.found || !arg.sent); + if (arg.ret == 0 && !arg.sent) + arg.ret = arg.found ? EPERM : ESRCH; + return (arg.ret); } #ifndef _SYS_SYSPROTO_H_