5
/* connect to UNIX-domain file descriptor listener
7
/* #include <connect.h>
9
/* int upass_connect(addr, block_mode, timeout)
14
/* upass_connect() connects to a file descriptor listener in
15
/* the UNIX domain at the specified address, sends one half
16
/* of a socketpair to the listener, and returns the other half
19
/* The file descriptor transporting connection is closed by
20
/* a background thread. Some kernels might otherwise discard
21
/* the descriptor before the server has received it.
25
/* Null-terminated string with connection destination.
27
/* Either NON_BLOCKING for a non-blocking socket, or BLOCKING for
28
/* blocking mode. This setting has no effect on the connection
29
/* establishment process.
31
/* Bounds the number of seconds that the operation may take. Specify
32
/* a value <= 0 to disable the time limit.
34
/* The result is -1 in case the connection could not be made.
35
/* Fatal errors: other system call failures.
39
/* The Secure Mailer license must be distributed with this software.
42
/* IBM T.J. Watson Research
44
/* Yorktown Heights, NY 10598, USA
47
/* System interfaces. */
50
#include <sys/socket.h>
56
/* Utility library. */
60
#include <sane_connect.h>
62
#include <timed_connect.h>
65
#include <sane_socketpair.h>
68
* Workaround for hostile kernels that don't support graceful shutdown.
70
struct upass_connect {
75
/* upass_connect_event - disconnect from peer */
77
static void upass_connect_event(int event, char *context)
79
struct upass_connect *up = (struct upass_connect *) context;
80
static const char *myname = "upass_connect_event";
85
if (event == EVENT_TIME)
86
msg_warn("%s: read timeout for service %s", myname, up->service);
87
event_disable_readwrite(up->fd);
88
event_cancel_timer(upass_connect_event, context);
89
if (close(up->fd) < 0)
90
msg_warn("%s: close %s: %m", myname, up->service);
95
/* upass_connect - connect to UNIX-domain file descriptor listener */
97
int upass_connect(const char *addr, int block_mode, int timeout)
99
struct upass_connect *up;
106
if ((sock = unix_connect(addr, BLOCKING, timeout)) < 0)
110
* Send one socket pair half to the server.
115
if (sane_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
119
if (unix_send_fd(sock, pair[THEIR_HALF]) < 0) {
120
close(pair[THEIR_HALF]);
121
close(pair[OUR_HALF]);
125
close(pair[THEIR_HALF]);
128
* Return the other socket pair half to the caller. Don't close the
129
* control socket just yet, but wait until the receiver closes it first.
130
* Otherwise, some hostile kernel might discard the socket that we just
133
up = (struct upass_connect *) mymalloc(sizeof(*up));
135
up->service = mystrdup(addr);
137
event_request_timer(upass_connect_event, (char *) up, timeout + 100);
138
event_enable_read(sock, upass_connect_event, (char *) up);
139
non_blocking(pair[OUR_HALF], block_mode);
140
return (pair[OUR_HALF]);