1*e89934bbSchristos /* $NetBSD: stream_trigger.c,v 1.2 2017/02/14 01:16:49 christos Exp $ */
241fbaed0Stron
341fbaed0Stron /*++
441fbaed0Stron /* NAME
541fbaed0Stron /* stream_trigger 3
641fbaed0Stron /* SUMMARY
741fbaed0Stron /* wakeup stream server
841fbaed0Stron /* SYNOPSIS
941fbaed0Stron /* #include <trigger.h>
1041fbaed0Stron /*
1141fbaed0Stron /* int stream_trigger(service, buf, len, timeout)
1241fbaed0Stron /* const char *service;
1341fbaed0Stron /* const char *buf;
1441fbaed0Stron /* ssize_t len;
1541fbaed0Stron /* int timeout;
1641fbaed0Stron /* DESCRIPTION
1741fbaed0Stron /* stream_trigger() wakes up the named stream server by making
1841fbaed0Stron /* a brief connection to it and writing the named buffer.
1941fbaed0Stron /*
2041fbaed0Stron /* The connection is closed by a background thread. Some kernels
2141fbaed0Stron /* cannot handle client-side disconnect before the server has
2241fbaed0Stron /* received the message.
2341fbaed0Stron /*
2441fbaed0Stron /* Arguments:
2541fbaed0Stron /* .IP service
2641fbaed0Stron /* Name of the communication endpoint.
2741fbaed0Stron /* .IP buf
2841fbaed0Stron /* Address of data to be written.
2941fbaed0Stron /* .IP len
3041fbaed0Stron /* Amount of data to be written.
3141fbaed0Stron /* .IP timeout
3241fbaed0Stron /* Deadline in seconds. Specify a value <= 0 to disable
3341fbaed0Stron /* the time limit.
3441fbaed0Stron /* DIAGNOSTICS
3541fbaed0Stron /* The result is zero in case of success, -1 in case of problems.
3641fbaed0Stron /* SEE ALSO
3741fbaed0Stron /* stream_connect(3), stream client
3841fbaed0Stron /* LICENSE
3941fbaed0Stron /* .ad
4041fbaed0Stron /* .fi
4141fbaed0Stron /* The Secure Mailer license must be distributed with this software.
4241fbaed0Stron /* AUTHOR(S)
4341fbaed0Stron /* Wietse Venema
4441fbaed0Stron /* IBM T.J. Watson Research
4541fbaed0Stron /* P.O. Box 704
4641fbaed0Stron /* Yorktown Heights, NY 10598, USA
4741fbaed0Stron /*--*/
4841fbaed0Stron
4941fbaed0Stron /* System library. */
5041fbaed0Stron
5141fbaed0Stron #include <sys_defs.h>
5241fbaed0Stron #include <unistd.h>
5341fbaed0Stron #include <string.h>
5441fbaed0Stron
5541fbaed0Stron /* Utility library. */
5641fbaed0Stron
5741fbaed0Stron #include <msg.h>
5841fbaed0Stron #include <connect.h>
5941fbaed0Stron #include <iostuff.h>
6041fbaed0Stron #include <mymalloc.h>
6141fbaed0Stron #include <events.h>
6241fbaed0Stron #include <trigger.h>
6341fbaed0Stron
6441fbaed0Stron struct stream_trigger {
6541fbaed0Stron int fd;
6641fbaed0Stron char *service;
6741fbaed0Stron };
6841fbaed0Stron
6941fbaed0Stron /* stream_trigger_event - disconnect from peer */
7041fbaed0Stron
stream_trigger_event(int event,void * context)71e262b48eSchristos static void stream_trigger_event(int event, void *context)
7241fbaed0Stron {
7341fbaed0Stron struct stream_trigger *sp = (struct stream_trigger *) context;
7441fbaed0Stron static const char *myname = "stream_trigger_event";
7541fbaed0Stron
7641fbaed0Stron /*
7741fbaed0Stron * Disconnect.
7841fbaed0Stron */
7941fbaed0Stron if (event == EVENT_TIME)
8041fbaed0Stron msg_warn("%s: read timeout for service %s", myname, sp->service);
8141fbaed0Stron event_disable_readwrite(sp->fd);
8241fbaed0Stron event_cancel_timer(stream_trigger_event, context);
8341fbaed0Stron if (close(sp->fd) < 0)
8441fbaed0Stron msg_warn("%s: close %s: %m", myname, sp->service);
8541fbaed0Stron myfree(sp->service);
86e262b48eSchristos myfree((void *) sp);
8741fbaed0Stron }
8841fbaed0Stron
8941fbaed0Stron /* stream_trigger - wakeup stream server */
9041fbaed0Stron
stream_trigger(const char * service,const char * buf,ssize_t len,int timeout)9141fbaed0Stron int stream_trigger(const char *service, const char *buf, ssize_t len, int timeout)
9241fbaed0Stron {
9341fbaed0Stron const char *myname = "stream_trigger";
9441fbaed0Stron struct stream_trigger *sp;
9541fbaed0Stron int fd;
9641fbaed0Stron
9741fbaed0Stron if (msg_verbose > 1)
9841fbaed0Stron msg_info("%s: service %s", myname, service);
9941fbaed0Stron
10041fbaed0Stron /*
10141fbaed0Stron * Connect...
10241fbaed0Stron */
10341fbaed0Stron if ((fd = stream_connect(service, BLOCKING, timeout)) < 0) {
10441fbaed0Stron if (msg_verbose)
10541fbaed0Stron msg_warn("%s: connect to %s: %m", myname, service);
10641fbaed0Stron return (-1);
10741fbaed0Stron }
10841fbaed0Stron close_on_exec(fd, CLOSE_ON_EXEC);
10941fbaed0Stron
11041fbaed0Stron /*
11141fbaed0Stron * Stash away context.
11241fbaed0Stron */
11341fbaed0Stron sp = (struct stream_trigger *) mymalloc(sizeof(*sp));
11441fbaed0Stron sp->fd = fd;
11541fbaed0Stron sp->service = mystrdup(service);
11641fbaed0Stron
11741fbaed0Stron /*
11841fbaed0Stron * Write the request...
11941fbaed0Stron */
12041fbaed0Stron if (write_buf(fd, buf, len, timeout) < 0
12141fbaed0Stron || write_buf(fd, "", 1, timeout) < 0)
12241fbaed0Stron if (msg_verbose)
12341fbaed0Stron msg_warn("%s: write to %s: %m", myname, service);
12441fbaed0Stron
12541fbaed0Stron /*
12641fbaed0Stron * Wakeup when the peer disconnects, or when we lose patience.
12741fbaed0Stron */
12841fbaed0Stron if (timeout > 0)
129e262b48eSchristos event_request_timer(stream_trigger_event, (void *) sp, timeout + 100);
130e262b48eSchristos event_enable_read(fd, stream_trigger_event, (void *) sp);
13141fbaed0Stron return (0);
13241fbaed0Stron }
133