1 /*++
2 /* NAME
3 /* mail_connect 3
4 /* SUMMARY
5 /* intra-mail system connection management
6 /* SYNOPSIS
7 /* #include <mail_proto.h>
8 /*
9 /* VSTREAM *mail_connect(class, name, block_mode)
10 /* const char *class;
11 /* const char *name;
12 /* int block_mode;
13 /*
14 /* VSTREAM *mail_connect_wait(class, name)
15 /* const char *class;
16 /* const char *name;
17 /* DESCRIPTION
18 /* This module does low-level connection management for intra-mail
19 /* communication. All reads and writes are subject to a time limit
20 /* (controlled by the global variable \fIvar_ipc_timeout\fR). This
21 /* protects against deadlock conditions that should never happen.
22 /*
23 /* mail_connect() attempts to connect to the UNIX-domain socket of
24 /* the named subsystem. The result is a null pointer in case of failure.
25 /* By default this function provides no errno logging.
26 /*
27 /* mail_connect_wait() is like mail_connect(), but keeps trying until
28 /* the connection succeeds. However, mail_connect_wait() terminates
29 /* with a fatal error when the service is down. This is to ensure that
30 /* processes terminate when the mail system shuts down.
31 /*
32 /* Arguments:
33 /* .IP class
34 /* Name of a class of local transport channel endpoints,
35 /* either \fIpublic\fR (accessible by any local user) or
36 /* \fIprivate\fR (administrative access only).
37 /* .IP service
38 /* The name of a local transport endpoint within the named class.
39 /* .IP block_mode
40 /* NON_BLOCKING for a non-blocking connection, or BLOCKING.
41 /* SEE ALSO
42 /* timed_ipc(3), enforce IPC timeouts.
43 /* LICENSE
44 /* .ad
45 /* .fi
46 /* The Secure Mailer license must be distributed with this software.
47 /* AUTHOR(S)
48 /* Wietse Venema
49 /* IBM T.J. Watson Research
50 /* P.O. Box 704
51 /* Yorktown Heights, NY 10598, USA
52 /*--*/
53
54 /* System library. */
55
56 #include <sys_defs.h>
57 #include <fcntl.h>
58 #include <unistd.h>
59 #include <stdlib.h>
60 #include <stdarg.h>
61 #include <errno.h>
62
63 /* Utility library. */
64
65 #include <msg.h>
66 #include <vstream.h>
67 #include <connect.h>
68 #include <mymalloc.h>
69 #include <iostuff.h>
70 #include <stringops.h>
71
72 /* Global library. */
73
74 #include "timed_ipc.h"
75 #include "mail_proto.h"
76
77 /* mail_connect - connect to mail subsystem */
78
mail_connect(const char * class,const char * name,int block_mode)79 VSTREAM *mail_connect(const char *class, const char *name, int block_mode)
80 {
81 char *path;
82 VSTREAM *stream;
83 int fd;
84 char *sock_name;
85
86 path = mail_pathname(class, name);
87 if ((fd = LOCAL_CONNECT(path, block_mode, 0)) < 0) {
88 if (msg_verbose)
89 msg_info("connect to subsystem %s: %m", path);
90 stream = 0;
91 } else {
92 if (msg_verbose)
93 msg_info("connect to subsystem %s", path);
94 stream = vstream_fdopen(fd, O_RDWR);
95 timed_ipc_setup(stream);
96 sock_name = concatenate(path, " socket", (char *) 0);
97 vstream_control(stream,
98 CA_VSTREAM_CTL_PATH(sock_name),
99 CA_VSTREAM_CTL_END);
100 myfree(sock_name);
101 }
102 myfree(path);
103 return (stream);
104 }
105
106 /* mail_connect_wait - connect to mail service until it succeeds */
107
mail_connect_wait(const char * class,const char * name)108 VSTREAM *mail_connect_wait(const char *class, const char *name)
109 {
110 VSTREAM *stream;
111 int count = 0;
112
113 /*
114 * XXX Solaris workaround for ECONNREFUSED on a busy socket.
115 */
116 while ((stream = mail_connect(class, name, BLOCKING)) == 0) {
117 if (count++ >= 10) {
118 msg_fatal("connect #%d to subsystem %s/%s: %m",
119 count, class, name);
120 } else {
121 msg_warn("connect #%d to subsystem %s/%s: %m",
122 count, class, name);
123 }
124 sleep(10); /* XXX make configurable */
125 }
126 return (stream);
127 }
128