tpoll() clients in agent to prevent blocking - safe - password protected secret keeper
 (HTM) git clone git://git.z3bra.org/safe.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 23350b3136fc4edcf2fa40975e466a8e70d27a7c
 (DIR) parent b56e39e8835092c7b557297abf47860b3737480a
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Mon, 24 Jun 2019 12:09:13 +0200
       
       poll() clients in agent to prevent blocking
       
       Diffstat:
         M safe-agent.c                        |      27 ++++++++++++++++++---------
       
       1 file changed, 18 insertions(+), 9 deletions(-)
       ---
 (DIR) diff --git a/safe-agent.c b/safe-agent.c
       t@@ -6,6 +6,7 @@
        #include <err.h>
        #include <fcntl.h>
        #include <limits.h>
       +#include <poll.h>
        #include <signal.h>
        #include <stdint.h>
        #include <stdio.h>
       t@@ -146,8 +147,9 @@ sighandler(int signal)
        int
        servekey(int timeout)
        {
       -        int cfd, sfd;
       +        int r, sfd;
                ssize_t n;
       +        struct pollfd pfd;
        
                sfd = creatsock(sockp);
                if (sfd < 0)
       t@@ -156,21 +158,28 @@ servekey(int timeout)
                s.loaded = 0;
        
                for (;;) {
       -                cfd = accept(sfd, NULL, NULL);
       -                if (cfd < 0)
       +                pfd.fd = accept(sfd, NULL, NULL);
       +                pfd.events = POLLIN|POLLOUT|POLLERR;
       +
       +                if (pfd.fd < 0)
                                err(1, "%s", sockp);
        
       -                n = 0;
       -                if (s.loaded) {
       -                        xwrite(cfd, s.saltkey, sizeof(s.saltkey));
       -                } else {
       -                        n = xread(cfd, s.saltkey, sizeof(s.saltkey));
       +                if ((r = poll(&pfd, 1, 0)) < 1)
       +                        return r;
       +
       +                if (pfd.revents & POLLOUT) {
       +                        n = s.loaded ? sizeof(s.saltkey) : 0;
       +                        xwrite(pfd.fd, s.saltkey, n);
       +                }
       +
       +                if (pfd.revents & POLLIN) {
       +                        n = xread(pfd.fd, s.saltkey, sizeof(s.saltkey));
                                if (n == sizeof(s.saltkey)) {
                                        s.loaded = 1;
                                        alarm(timeout);
                                }
                        }
       -                close(cfd);
       +                close(pfd.fd);
                }
        
                /* NOTREACHED */