2 * Copyright (c) 2016 Proofpoint, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
13 #include <sys/types.h>
21 #include <string.h> /* for memset() */
23 #include <sm/conf.h> /* FDSET_CAST */
25 #include <sm/assert.h>
26 #include <sm/notify.h>
29 #define SM_DBG(p) fprintf p
34 static int Notifypipe[2];
35 #define NotifyRDpipe Notifypipe[0]
36 #define NotifyWRpipe Notifypipe[1]
38 #define CLOSEFD(fd) do { \
47 ** SM_NOTIFY_INIT -- initialize notify system
61 if (pipe(Notifypipe) < 0)
67 ** SM_NOTIFY_START -- start notify system
71 ** flags -- currently ignored.
79 sm_notify_start(owner, flags)
87 CLOSEFD(NotifyWRpipe);
89 CLOSEFD(NotifyRDpipe);
94 ** SM_NOTIFY_STOP -- stop notify system
98 ** flags -- currently ignored.
106 sm_notify_stop(owner, flags)
111 CLOSEFD(NotifyRDpipe);
113 CLOSEFD(NotifyWRpipe);
118 ** SM_NOTIFY_SND -- send notification
121 ** buf -- where to write data
122 ** buflen -- len of buffer
130 sm_notify_snd(buf, buflen)
137 SM_REQUIRE(buf != NULL);
138 SM_REQUIRE(buflen > 0);
139 if (NotifyWRpipe < 0)
142 r = write(NotifyWRpipe, buf, buflen);
144 SM_DBG((stderr, "write=%d, fd=%d, e=%d\n", r, NotifyWRpipe, save_errno));
145 return r >= 0 ? 0 : -save_errno;
149 ** SM_NOTIFY_RCV -- receive notification
152 ** buf -- where to write data
153 ** buflen -- len of buffer
162 sm_notify_rcv(buf, buflen, tmo)
170 struct timeval timeout;
172 SM_REQUIRE(buf != NULL);
173 SM_REQUIRE(buflen > 0);
174 if (NotifyRDpipe < 0)
177 SM_FD_SET(NotifyRDpipe, &readfds);
178 timeout.tv_sec = tmo;
182 r = select(NotifyRDpipe + 1, FDSET_CAST &readfds, NULL, NULL, &timeout);
184 SM_DBG((stderr, "select=%d, fd=%d, e=%d\n", r, NotifyRDpipe, save_errno));
185 } while (r < 0 && save_errno == EINTR);
189 SM_DBG((stderr, "select=%d, e=%d\n", r, save_errno));
193 /* bogus... need to check again? */
194 if (!FD_ISSET(NotifyRDpipe, &readfds))
197 r = read(NotifyRDpipe, buf, buflen);
199 SM_DBG((stderr, "read=%d, e=%d\n", r, save_errno));