The branch main has been updated by adrian: URL: https://cgit.FreeBSD.org/src/commit/?id=db80116da404c90e6f0d69350554291f866e8ebb
commit db80116da404c90e6f0d69350554291f866e8ebb Author: Adrian Chadd <adr...@freebsd.org> AuthorDate: 2025-02-13 18:33:12 +0000 Commit: Adrian Chadd <adr...@freebsd.org> CommitDate: 2025-02-28 02:49:49 +0000 wlanwds: add pidfile support Add pidfile support based on "man pidfile". Differential Revision: https://reviews.freebsd.org/D48997 --- tools/tools/net80211/wlanwds/Makefile | 2 ++ tools/tools/net80211/wlanwds/wlanwds.c | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tools/tools/net80211/wlanwds/Makefile b/tools/tools/net80211/wlanwds/Makefile index 1e993cc07c21..293ddb75f9b4 100644 --- a/tools/tools/net80211/wlanwds/Makefile +++ b/tools/tools/net80211/wlanwds/Makefile @@ -2,4 +2,6 @@ PROG= wlanwds BINDIR= /usr/local/bin MAN= +LIBADD= util + .include <bsd.prog.mk> diff --git a/tools/tools/net80211/wlanwds/wlanwds.c b/tools/tools/net80211/wlanwds/wlanwds.c index e64b05658740..f6364c3ce4da 100644 --- a/tools/tools/net80211/wlanwds/wlanwds.c +++ b/tools/tools/net80211/wlanwds/wlanwds.c @@ -68,6 +68,7 @@ #include <syslog.h> #include <unistd.h> #include <ifaddrs.h> +#include <libutil.h> #define IEEE80211_ADDR_EQ(a1,a2) (memcmp(a1,a2,IEEE80211_ADDR_LEN) == 0) #define IEEE80211_ADDR_COPY(dst,src) memcpy(dst,src,IEEE80211_ADDR_LEN) @@ -107,9 +108,11 @@ main(int argc, char *argv[]) { const char *progname = argv[0]; const char *pidfile = NULL; + struct pidfh *pfh = NULL; int s, c, logmask, bg = 1; char msg[2048]; int log_stderr = 0; + pid_t otherpid; logmask = LOG_UPTO(LOG_INFO); while ((c = getopt(argc, argv, "efjP:s:tv")) != -1) @@ -148,6 +151,17 @@ main(int argc, char *argv[]) ifnets = argv; nifnets = argc; + if (pidfile != NULL) { + pfh = pidfile_open(pidfile, 0600, &otherpid); + if (pfh == NULL) { + if (errno == EEXIST) + errx(EXIT_FAILURE, "Daemon already running; pid: %jd.", + (intmax_t)otherpid); + + warn("Cannot open or create pidfile"); + } + } + s = socket(PF_ROUTE, SOCK_RAW, 0); if (s < 0) err(EX_OSERR, "socket"); @@ -157,8 +171,12 @@ main(int argc, char *argv[]) scanforvaps(s); /* XXX what directory to work in? */ - if (bg && daemon(0, 0) < 0) + if (bg && daemon(0, 0) < 0) { + pidfile_remove(pfh); err(EX_OSERR, "daemon"); + } + + pidfile_write(pfh); openlog("wlanwds", log_stderr | LOG_PID | LOG_CONS, LOG_DAEMON); setlogmask(logmask); @@ -167,6 +185,8 @@ main(int argc, char *argv[]) ssize_t n = read(s, msg, sizeof(msg)); handle_rtmsg((struct rt_msghdr *)msg, n); } + + pidfile_remove(pfh); return 0; }