? res_kqueue.diff Index: res_init.c =================================================================== RCS file: /cvsroot/src/lib/libc/resolv/res_init.c,v retrieving revision 1.31 diff -u -p -u -r1.31 res_init.c --- res_init.c 19 Apr 2017 22:21:07 -0000 1.31 +++ res_init.c 29 Sep 2021 22:36:29 -0000 @@ -190,6 +190,21 @@ res_ninit(res_state statp) { return (__res_vinit(statp, 0)); } +static int +__res_kqinit(res_state statp) +{ + struct kevent kc; + struct __res_state_ext *ext = statp->_u._ext.ext; + + ext->kq = kqueue1(O_CLOEXEC); + ext->kqpid = getpid(); + EV_SET(&kc, ext->resfd, EVFILT_VNODE, + EV_ADD|EV_ENABLE|EV_CLEAR, NOTE_DELETE|NOTE_WRITE| NOTE_EXTEND| + NOTE_ATTRIB|NOTE_LINK|NOTE_RENAME|NOTE_REVOKE, 0, 0); + (void)kevent(ext->kq, &kc, 1, NULL, 0, &ts); + return ext->kq; +} + /*% This function has to be reachable by res_data.c but not publically. */ int __res_vinit(res_state statp, int preinit) { @@ -346,7 +361,6 @@ __res_vinit(res_state statp, int preinit nserv = 0; if ((fp = fopen(__res_conf_name, "re")) != NULL) { struct stat st; - struct kevent kc; /* read the config file */ while (fgets(buf, (int)sizeof(buf), fp) != NULL) { @@ -500,11 +514,7 @@ __res_vinit(res_state statp, int preinit if (fstat(statp->_u._ext.ext->resfd, &st) != -1) __res_conf_time = statp->_u._ext.ext->res_conf_time = st.st_mtimespec; - statp->_u._ext.ext->kq = kqueue1(O_CLOEXEC); - EV_SET(&kc, statp->_u._ext.ext->resfd, EVFILT_VNODE, - EV_ADD|EV_ENABLE|EV_CLEAR, NOTE_DELETE|NOTE_WRITE| NOTE_EXTEND| - NOTE_ATTRIB|NOTE_LINK|NOTE_RENAME|NOTE_REVOKE, 0, 0); - (void)kevent(statp->_u._ext.ext->kq, &kc, 1, NULL, 0, &ts); + __res_kqinit(statp); } else { statp->_u._ext.ext->kq = -1; statp->_u._ext.ext->resfd = -1; @@ -573,6 +583,9 @@ res_check(res_state statp, struct timesp struct kevent ke; if (statp->_u._ext.ext->kq == -1) goto out; + if (statp->_u._ext.ext->kqpid != getpid() && + __res_kqinit(statp) == -1) + goto out; switch (kevent(statp->_u._ext.ext->kq, NULL, 0, &ke, 1, &ts)) { case 0: @@ -812,13 +825,14 @@ res_nclose(res_state statp) void res_ndestroy(res_state statp) { + struct __res_state_ext *ext = statp->_u._ext.ext; res_nclose(statp); - if (statp->_u._ext.ext != NULL) { - if (statp->_u._ext.ext->kq != -1) - (void)close(statp->_u._ext.ext->kq); - if (statp->_u._ext.ext->resfd != -1) - (void)close(statp->_u._ext.ext->resfd); - free(statp->_u._ext.ext); + if (ext != NULL) { + if (ext->kq != -1 && ext->kqpid == getpid()) + (void)close(ext->kq); + if (ext->resfd != -1) + (void)close(ext->resfd); + free(ext); statp->_u._ext.ext = NULL; } if (statp->_rnd != NULL) { Index: res_private.h =================================================================== RCS file: /cvsroot/src/lib/libc/resolv/res_private.h,v retrieving revision 1.3 diff -u -p -u -r1.3 res_private.h --- res_private.h 24 Oct 2009 17:24:01 -0000 1.3 +++ res_private.h 29 Sep 2021 22:36:29 -0000 @@ -16,6 +16,7 @@ struct __res_state_ext { char nsuffix2[64]; struct timespec res_conf_time; int kq, resfd; + pid_t kqpid; }; extern int res_ourserver_p(const res_state, const struct sockaddr *);