From nobody Sat Apr 9 16:02:14 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id A0E0B1A88BCA; Sat, 9 Apr 2022 16:02:14 +0000 (UTC) (envelope-from git@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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KbKd24CQhz54fD; Sat, 9 Apr 2022 16:02:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649520134; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=uX1S2CNXWLa6eQFJABStL5PNXyLVf/qUlzZJhqoYJks=; b=kHrYWHAzHCg8Zbz/m0uLMhgmUD8jUXudlrTivElQRxFiP+Rq1sFPOLj47kXAxzHhXsT2Mc RAhTvvbKgwgDBtSJJV7U45Aj4qxhmucWLMcdKvIvjEtBWbU7cTBxGT8SZpeU0kf/ys+Rv4 TDpJNKOouyZjs8wpZTYjGpnaIZdDL+/KbldEe9XPUMzd2BDhBcKjY+2zfk6461eoJ2uUYe vCqL4SGp1dFVjMYBVzzZerv55LDjVzbOkNp0XjNQ9+tS3j5UjpWSzgp9cLnQg87t12CEwY omXBDIvZUakLIeiNXOEP+yNpfp29yWpPTwXsImUa5c0SFvYDaJmVn7s6umKfHw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 70CE11D169; Sat, 9 Apr 2022 16:02:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 239G2ECF032938; Sat, 9 Apr 2022 16:02:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 239G2ECO032937; Sat, 9 Apr 2022 16:02:14 GMT (envelope-from git) Date: Sat, 9 Apr 2022 16:02:14 GMT Message-Id: <202204091602.239G2ECO032937@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: 287451fd0192 - main - pidfile: add pidfile_signal List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 287451fd019299b138bab2ee99cdf0f3be495439 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649520134; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=uX1S2CNXWLa6eQFJABStL5PNXyLVf/qUlzZJhqoYJks=; b=rdDvfGH+XwwyKHJ8F/3Kr35BD4h0qh5kpSxJA26xcLaS6U3DvkP/y5+z0cJQK/y10KFtEw DOQsdeh4CYksudI24cobuLf0FHbSEJhdqtPmd54pVeJa9Ft8QrXOZOJI49MLDfGfuIbOM+ YkBNgfPujkAjNWSYgeuBLrg1H5vOaqpCfWv8pQNF6e4UvLe9DQuTlmsHJ0oKDdIAdQwMF2 1EHj8oc24HvNuITUepkBL6bup5EvLVdRVtyR8WlfF6a5xbsIQ06rs/3y+YK/LxPpWoOO1h 0gDcDxand4yZpq+AU3g/mNGCNt7qchQuJy0hMH1os9I1ThxU4FMEJe/FDeHXiw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1649520134; a=rsa-sha256; cv=none; b=PBKQb0XNIk5VZHYGS+w8t8KKEcDEaMmXNmVpfIrZKWc8p1tkb908F7FDrvr1M3HnzvBeql AmEISb0RhLZed2EcJtvbPNGb2B0eaGsILWL7hpmyGLdbN1JSv6xIcHnw0iYT5mDwjDdrNZ c4BQhF7kb/uWSNWzGH6URlKvD7cxrcvrtxVr7BuG98TwDtKJxeYb5xuD0VvbTzvmEiPLI2 1RVo47+taQykYUmpT5xtA0a9Hcck0RlL6jMhaN+VtOopjjCQsn8wXooomPXCLcyDOluvmw iBCYHneU+y66eqcrCNwo7V9SPcAZtBdbc1H2k9RzeOLLjeHDSwiyAFA1T+4TGw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=287451fd019299b138bab2ee99cdf0f3be495439 commit 287451fd019299b138bab2ee99cdf0f3be495439 Author: Mateusz Guzik AuthorDate: 2022-03-11 11:01:50 +0000 Commit: Mateusz Guzik CommitDate: 2022-04-09 15:59:43 +0000 pidfile: add pidfile_signal Differential Revision: https://reviews.freebsd.org/D34681 --- lib/libutil/libutil.h | 1 + lib/libutil/pidfile.c | 76 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h index 17c44de0fce7..1ca22ce40e95 100644 --- a/lib/libutil/libutil.h +++ b/lib/libutil/libutil.h @@ -122,6 +122,7 @@ int openpty(int *_amaster, int *_aslave, char *_name, struct termios *_termp, struct winsize *_winp); int pidfile_close(struct pidfh *_pfh); int pidfile_fileno(const struct pidfh *_pfh); +int pidfile_signal(const char *pathp, int sig, pid_t *pidptr); struct pidfh * pidfile_open(const char *_path, mode_t _mode, pid_t *_pidptr); int pidfile_remove(struct pidfh *_pfh); diff --git a/lib/libutil/pidfile.c b/lib/libutil/pidfile.c index eaa9379267db..33274849f06f 100644 --- a/lib/libutil/pidfile.c +++ b/lib/libutil/pidfile.c @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include @@ -39,6 +40,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -74,7 +76,7 @@ pidfile_verify(const struct pidfh *pfh) } static int -pidfile_read(int dirfd, const char *filename, pid_t *pidptr) +pidfile_read_impl(int dirfd, const char *filename, pid_t *pidptr) { char buf[16], *endptr; int error, fd, i; @@ -99,14 +101,33 @@ pidfile_read(int dirfd, const char *filename, pid_t *pidptr) return (0); } +static int +pidfile_read(int dirfd, const char *filename, pid_t *pidptr) +{ + struct timespec rqtp; + int count; + + count = 20; + rqtp.tv_sec = 0; + rqtp.tv_nsec = 5000000; + for (;;) { + errno = pidfile_read_impl(dirfd, filename, pidptr); + if (errno != EAGAIN || --count == 0) + break; + nanosleep(&rqtp, 0); + } + if (errno == EAGAIN) + *pidptr = -1; + return (errno); +} + struct pidfh * pidfile_open(const char *pathp, mode_t mode, pid_t *pidptr) { char path[MAXPATHLEN]; struct pidfh *pfh; struct stat sb; - int error, fd, dirfd, dirlen, filenamelen, count; - struct timespec rqtp; + int error, fd, dirfd, dirlen, filenamelen; cap_rights_t caprights; pfh = malloc(sizeof(*pfh)); @@ -159,18 +180,8 @@ pidfile_open(const char *pathp, mode_t mode, pid_t *pidptr) if (pidptr == NULL) { errno = EEXIST; } else { - count = 20; - rqtp.tv_sec = 0; - rqtp.tv_nsec = 5000000; - for (;;) { - errno = pidfile_read(dirfd, - pfh->pf_filename, pidptr); - if (errno != EAGAIN || --count == 0) - break; - nanosleep(&rqtp, 0); - } - if (errno == EAGAIN) - *pidptr = -1; + errno = pidfile_read(dirfd, + pfh->pf_filename, pidptr); if (errno == 0 || errno == EAGAIN) errno = EEXIST; } @@ -330,3 +341,38 @@ pidfile_fileno(const struct pidfh *pfh) } return (pfh->pf_fd); } + +int +pidfile_signal(const char *pathp, int sig, pid_t *pidptr) +{ + pid_t pid; + int fd; + + fd = flopenat(AT_FDCWD, pathp, + O_RDONLY | O_CLOEXEC | O_NONBLOCK); + if (fd >= 0) { + /* + * The file exists but is not locked, + * so the daemon is dead. Nothing to do. + */ + close(fd); + errno = ENOENT; + return (errno); + } + if (errno != EWOULDBLOCK) { + return (errno); + } + errno = pidfile_read(AT_FDCWD, pathp, &pid); + if (errno != 0) + return (errno); + /* + * Refuse to send broadcast or group signals, this has + * happened due to the bugs in pidfile(3). + */ + if (pid <= 0) + return (EDOM); + kill(pid, sig); + if (pidptr != NULL) + *pidptr = pid; + return (errno); +}