From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 5013 invoked from network); 24 Nov 2023 23:46:45 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 24 Nov 2023 23:46:45 -0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Message-ID:Date:Content-ID: Content-Type:MIME-Version:Subject:To:From:Reply-To:Cc: Content-Transfer-Encoding:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References; bh=M52rIbA8IttcSQOiDXFWJM+yVvCY3RHvMyCmDkK4PrA=; b=bF60plVOyAhmyITMiHq01kNXoN N1yuYmWvHerRz2vPnmyAt5DSiU4xocsEVhC50w1N/z2zU8SEBIzdbK62CSQ6+lqEklbXZHZkVHUhs 9AWrUP39Igw1XhFNnc+Py3MIOW0EcDyDNkFtrMGGoVRkT17UzH2Rx3DocBfpSZ2TMAjqPws//7sxi v0EwBX2m1E8rCzODZ21/zELQr7CaeCo9bOAnJGeR59ejNsNVbXmzQ48/y8l65lHnLaPDTncumw6aI 5jqTWpi0GjDfF/y0CvnnRggVRsh1FH0XLLRE7U4Oq4DPltjBHY0MTxjyJB9gguvdy5n/PI5MGtiPU /PvoH64g==; Received: by zero.zsh.org with local id 1r6fsi-000KEJ-JT; Fri, 24 Nov 2023 23:46:44 +0000 Received: by zero.zsh.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1r6fs8-000Jwm-Lj; Fri, 24 Nov 2023 23:46:08 +0000 Received: from [192.168.178.21] (helo=hydra) by mail.kiddle.eu with esmtp(Exim 4.95) (envelope-from ) id 1r6fs7-000JNx-Cg for zsh-workers@zsh.org; Sat, 25 Nov 2023 00:46:07 +0100 From: Oliver Kiddle To: Zsh workers Subject: PATCH: add -q option to kill for sigqueue MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <74519.1700869567.1@hydra> Date: Sat, 25 Nov 2023 00:46:07 +0100 Message-ID: <74520-1700869567.390063@twie.42H7.9hr2> X-Seq: 52326 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: The following adds support for a -q option to kill which takes a numeric argument. It then uses sigqueue(2) to send the signal with the provided number which a signal handler can pick up as si_value.sival_int in the siginfo_t parameter. The util-linux kill also supports a similar option. sigqueue and a range of "realtime" signals are specified in POSIX and well supported. As far as I can tell, the RT signals are intended for user-defined purposes like SIGUSR1 and USR2. The value can be an int or a pointer but in practice, sending a pointer is fairly useless given that signals are usually sent to a different process. We could potentially make the signal value and other details such as si_pid available to trap handlers. Perhaps using namespaced variables - e.g. .signal.pid and .signal.value? However, I don't really understand enough about how trap is implemented to know how (or whether) that can be done safely. It also ought to be possible to make SIGRTMIN through to SIGRTMAX available. util-linux's kill allows those to be specified as RT, RTMIN+ or RTMAX-. Would that need us to muck around with signames2.awk or is leaving them out of $signals / kill -l and using #ifdef RTMIN sufficient? Oliver diff --git a/Src/jobs.c b/Src/jobs.c index a3b9f667a..4e9767ee4 100644 --- a/Src/jobs.c +++ b/Src/jobs.c @@ -2677,9 +2677,35 @@ bin_kill(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func)) { int sig = SIGTERM; int returnval = 0; +#ifdef HAVE_SIGQUEUE + union sigval sigqueue_info; +#endif + int use_sigqueue = 0, got_sig = 0; + + while (*argv && **argv == '-') { + if (!use_sigqueue && (*argv)[1] == 'q' && (*argv)[2] == '\0') { + char *endp; + + if (!*++argv) { + zwarnnam(nam, "-q: argument expected"); + return 1; + } +#ifdef HAVE_SIGQUEUE + sigqueue_info.sival_int = +#endif + zstrtol(*argv, &endp, 10); + if (*endp) { + zwarnnam(nam, "invalid number: %s", *argv); + return 1; + } + use_sigqueue = 1; + argv++; + continue; + } + if (got_sig) + break; /* check for, and interpret, a signal specifier */ - if (*argv && **argv == '-') { if (idigit((*argv)[1])) { char *endp; /* signal specified by number */ @@ -2796,6 +2822,7 @@ bin_kill(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func)) } } argv++; + got_sig = 1; } /* Discard the standard "-" and "--" option breaks */ @@ -2844,7 +2871,12 @@ bin_kill(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func)) returnval++; } else { int pid = atoi(*argv); - if (kill(pid, sig) == -1) { + if ( +#ifdef HAVE_SIGQUEUE + use_sigqueue ? sigqueue(pid, sig, sigqueue_info) : +#endif + kill(pid, sig) == -1) + { zwarnnam("kill", "kill %s failed: %e", *argv, errno); returnval++; } diff --git a/configure.ac b/configure.ac index c5263035e..9cb6e032b 100644 --- a/configure.ac +++ b/configure.ac @@ -1299,6 +1299,7 @@ AC_CHECK_FUNCS(strftime strptime mktime timelocal \ mkfifo _mktemp mkstemp \ waitpid wait3 \ sigaction sigblock sighold sigrelse sigsetmask sigprocmask \ + sigqueue \ killpg setpgid setpgrp tcsetpgrp tcgetattr nice \ gethostname gethostbyname2 getipnodebyname \ inet_aton inet_pton inet_ntop \