9 /* #include <trigger.h>
11 /* int fifo_trigger(service, buf, len, timeout)
12 /* const char *service;
17 /* fifo_trigger() wakes up the named fifo server by writing
18 /* the contents of the specified buffer to the fifo. There is
19 /* no guarantee that the written data will actually be received.
23 /* Name of the communication endpoint.
25 /* Address of data to be written.
27 /* Amount of data to be written.
29 /* Deadline in seconds. Specify a value <= 0 to disable
32 /* The result is zero when the fifo could be opened, -1 otherwise.
37 /* The Secure Mailer license must be distributed with this software.
40 /* IBM T.J. Watson Research
42 /* Yorktown Heights, NY 10598, USA
51 /* Utility library. */
55 #include <safe_open.h>
58 /* fifo_trigger - wakeup fifo server */
60 int fifo_trigger(const char *service
, const char *buf
, ssize_t len
, int timeout
)
63 const char *myname
= "fifo_trigger";
68 why
= vstring_alloc(1);
71 * Write the request to the service fifo. According to POSIX, the open
72 * shall always return immediately, and shall return an error when no
73 * process is reading from the FIFO.
75 * Use safe_open() so that we don't follow symlinks, and so that we don't
76 * open files with multiple hard links. We're not (yet) going to bother
77 * the caller with safe_open() specific quirks such as the why argument.
79 if ((fp
= safe_open(service
, O_WRONLY
| O_NONBLOCK
, 0,
80 (struct stat
*) 0, -1, -1, why
)) == 0) {
82 msg_info("%s: open %s: %s", myname
, service
, vstring_str(why
));
85 fd
= vstream_fileno(fp
);
88 * Write the request...
90 non_blocking(fd
, timeout
> 0 ? NON_BLOCKING
: BLOCKING
);
91 if (write_buf(fd
, buf
, len
, timeout
) < 0)
93 msg_warn("%s: write %s: %m", myname
, service
);
98 if (vstream_fclose(fp
))
100 msg_warn("%s: close %s: %m", myname
, service
);
107 * Set up a FIFO listener, and keep triggering until the listener becomes
108 * idle, which should never happen.
116 #define TEST_FIFO "test-fifo"
121 static void cleanup(void)
127 static void handler(int sig
)
129 msg_fatal("got signal %d after %d triggers %d wakeups",
130 sig
, trig_count
, wakeup_count
);
133 static void read_event(int unused_event
, char *context
)
135 int fd
= CAST_CHAR_PTR_TO_INT(context
);
140 if (read(fd
, &ch
, 1) != 1)
141 msg_fatal("read %s: %m", TEST_FIFO
);
144 int main(int unused_argc
, char **unused_argv
)
148 listen_fd
= fifo_listen(TEST_FIFO
, 0600, NON_BLOCKING
);
149 msg_cleanup(cleanup
);
150 event_enable_read(listen_fd
, read_event
, CAST_INT_TO_CHAR_PTR(listen_fd
));
151 signal(SIGINT
, handler
);
152 signal(SIGALRM
, handler
);
155 if (fifo_trigger(TEST_FIFO
, "", 1, 0) < 0)
156 msg_fatal("trigger %s: %m", TEST_FIFO
);
158 if (fifo_trigger(TEST_FIFO
, "", 1, 0) < 0)
159 msg_fatal("trigger %s: %m", TEST_FIFO
);
161 if (fifo_trigger(TEST_FIFO
, "", 1, 0) < 0)
162 msg_fatal("trigger %s: %m", TEST_FIFO
);