7 /* wakeup stream server
9 /* #include <trigger.h>
11 /* int stream_trigger(service, buf, len, timeout)
12 /* const char *service;
17 /* stream_trigger() wakes up the named stream server by making
18 /* a brief connection to it and writing the named buffer.
20 /* The connection is closed by a background thread. Some kernels
21 /* cannot handle client-side disconnect before the server has
22 /* received the message.
26 /* Name of the communication endpoint.
28 /* Address of data to be written.
30 /* Amount of data to be written.
32 /* Deadline in seconds. Specify a value <= 0 to disable
35 /* The result is zero in case of success, -1 in case of problems.
37 /* stream_connect(3), stream client
41 /* The Secure Mailer license must be distributed with this software.
44 /* IBM T.J. Watson Research
46 /* Yorktown Heights, NY 10598, USA
55 /* Utility library. */
64 struct stream_trigger
{
69 /* stream_trigger_event - disconnect from peer */
71 static void stream_trigger_event(int event
, char *context
)
73 struct stream_trigger
*sp
= (struct stream_trigger
*) context
;
74 static const char *myname
= "stream_trigger_event";
79 if (event
== EVENT_TIME
)
80 msg_warn("%s: read timeout for service %s", myname
, sp
->service
);
81 event_disable_readwrite(sp
->fd
);
82 event_cancel_timer(stream_trigger_event
, context
);
83 if (close(sp
->fd
) < 0)
84 msg_warn("%s: close %s: %m", myname
, sp
->service
);
89 /* stream_trigger - wakeup stream server */
91 int stream_trigger(const char *service
, const char *buf
, ssize_t len
, int timeout
)
93 const char *myname
= "stream_trigger";
94 struct stream_trigger
*sp
;
98 msg_info("%s: service %s", myname
, service
);
103 if ((fd
= stream_connect(service
, BLOCKING
, timeout
)) < 0) {
105 msg_warn("%s: connect to %s: %m", myname
, service
);
108 close_on_exec(fd
, CLOSE_ON_EXEC
);
111 * Stash away context.
113 sp
= (struct stream_trigger
*) mymalloc(sizeof(*sp
));
115 sp
->service
= mystrdup(service
);
118 * Write the request...
120 if (write_buf(fd
, buf
, len
, timeout
) < 0
121 || write_buf(fd
, "", 1, timeout
) < 0)
123 msg_warn("%s: write to %s: %m", myname
, service
);
126 * Wakeup when the peer disconnects, or when we lose patience.
129 event_request_timer(stream_trigger_event
, (char *) sp
, timeout
+ 100);
130 event_enable_read(fd
, stream_trigger_event
, (char *) sp
);