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