xref: /titanic_50/usr/src/cmd/sendmail/libmilter/comm.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1999-2004 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: comm.c,v 8.66 2004/08/20 20:38:35 ca Exp $")
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #include "libmilter.h"
17*7c478bd9Sstevel@tonic-gate #include <sm/errstring.h>
18*7c478bd9Sstevel@tonic-gate #include <sys/uio.h>
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate static ssize_t	retry_writev __P((socket_t, struct iovec *, int, struct timeval *));
21*7c478bd9Sstevel@tonic-gate static size_t Maxdatasize = MILTER_MAX_DATA_SIZE;
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate #if _FFR_MAXDATASIZE
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate **  SMFI_SETMAXDATASIZE -- set limit for milter data read/write.
26*7c478bd9Sstevel@tonic-gate **
27*7c478bd9Sstevel@tonic-gate **	Parameters:
28*7c478bd9Sstevel@tonic-gate **		sz -- new limit.
29*7c478bd9Sstevel@tonic-gate **
30*7c478bd9Sstevel@tonic-gate **	Returns:
31*7c478bd9Sstevel@tonic-gate **		old limit
32*7c478bd9Sstevel@tonic-gate */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate size_t
35*7c478bd9Sstevel@tonic-gate smfi_setmaxdatasize(sz)
36*7c478bd9Sstevel@tonic-gate 	size_t sz;
37*7c478bd9Sstevel@tonic-gate {
38*7c478bd9Sstevel@tonic-gate 	size_t old;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate 	old = Maxdatasize;
41*7c478bd9Sstevel@tonic-gate 	Maxdatasize = sz;
42*7c478bd9Sstevel@tonic-gate 	return old;
43*7c478bd9Sstevel@tonic-gate }
44*7c478bd9Sstevel@tonic-gate #endif /* _FFR_MAXDATASIZE */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate **  MI_RD_CMD -- read a command
48*7c478bd9Sstevel@tonic-gate **
49*7c478bd9Sstevel@tonic-gate **	Parameters:
50*7c478bd9Sstevel@tonic-gate **		sd -- socket descriptor
51*7c478bd9Sstevel@tonic-gate **		timeout -- maximum time to wait
52*7c478bd9Sstevel@tonic-gate **		cmd -- single character command read from sd
53*7c478bd9Sstevel@tonic-gate **		rlen -- pointer to length of result
54*7c478bd9Sstevel@tonic-gate **		name -- name of milter
55*7c478bd9Sstevel@tonic-gate **
56*7c478bd9Sstevel@tonic-gate **	Returns:
57*7c478bd9Sstevel@tonic-gate **		buffer with rest of command
58*7c478bd9Sstevel@tonic-gate **		(malloc()ed here, should be free()d)
59*7c478bd9Sstevel@tonic-gate **		hack: encode error in cmd
60*7c478bd9Sstevel@tonic-gate */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate char *
63*7c478bd9Sstevel@tonic-gate mi_rd_cmd(sd, timeout, cmd, rlen, name)
64*7c478bd9Sstevel@tonic-gate 	socket_t sd;
65*7c478bd9Sstevel@tonic-gate 	struct timeval *timeout;
66*7c478bd9Sstevel@tonic-gate 	char *cmd;
67*7c478bd9Sstevel@tonic-gate 	size_t *rlen;
68*7c478bd9Sstevel@tonic-gate 	char *name;
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	ssize_t len;
71*7c478bd9Sstevel@tonic-gate 	mi_int32 expl;
72*7c478bd9Sstevel@tonic-gate 	ssize_t i;
73*7c478bd9Sstevel@tonic-gate 	FD_RD_VAR(rds, excs);
74*7c478bd9Sstevel@tonic-gate 	int ret;
75*7c478bd9Sstevel@tonic-gate 	int save_errno;
76*7c478bd9Sstevel@tonic-gate 	char *buf;
77*7c478bd9Sstevel@tonic-gate 	char data[MILTER_LEN_BYTES + 1];
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	*cmd = '\0';
80*7c478bd9Sstevel@tonic-gate 	*rlen = 0;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	i = 0;
83*7c478bd9Sstevel@tonic-gate 	for (;;)
84*7c478bd9Sstevel@tonic-gate 	{
85*7c478bd9Sstevel@tonic-gate 		FD_RD_INIT(sd, rds, excs);
86*7c478bd9Sstevel@tonic-gate 		ret = FD_RD_READY(sd, rds, excs, timeout);
87*7c478bd9Sstevel@tonic-gate 		if (ret == 0)
88*7c478bd9Sstevel@tonic-gate 			break;
89*7c478bd9Sstevel@tonic-gate 		else if (ret < 0)
90*7c478bd9Sstevel@tonic-gate 		{
91*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
92*7c478bd9Sstevel@tonic-gate 				continue;
93*7c478bd9Sstevel@tonic-gate 			break;
94*7c478bd9Sstevel@tonic-gate 		}
95*7c478bd9Sstevel@tonic-gate 		if (FD_IS_RD_EXC(sd, rds, excs))
96*7c478bd9Sstevel@tonic-gate 		{
97*7c478bd9Sstevel@tonic-gate 			*cmd = SMFIC_SELECT;
98*7c478bd9Sstevel@tonic-gate 			return NULL;
99*7c478bd9Sstevel@tonic-gate 		}
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		len = MI_SOCK_READ(sd, data + i, sizeof data - i);
102*7c478bd9Sstevel@tonic-gate 		if (MI_SOCK_READ_FAIL(len))
103*7c478bd9Sstevel@tonic-gate 		{
104*7c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
105*7c478bd9Sstevel@tonic-gate 				"%s, mi_rd_cmd: read returned %d: %s",
106*7c478bd9Sstevel@tonic-gate 				name, (int) len, sm_errstring(errno));
107*7c478bd9Sstevel@tonic-gate 			*cmd = SMFIC_RECVERR;
108*7c478bd9Sstevel@tonic-gate 			return NULL;
109*7c478bd9Sstevel@tonic-gate 		}
110*7c478bd9Sstevel@tonic-gate 		if (len == 0)
111*7c478bd9Sstevel@tonic-gate 		{
112*7c478bd9Sstevel@tonic-gate 			*cmd = SMFIC_EOF;
113*7c478bd9Sstevel@tonic-gate 			return NULL;
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 		if (len >= (ssize_t) sizeof data - i)
116*7c478bd9Sstevel@tonic-gate 			break;
117*7c478bd9Sstevel@tonic-gate 		i += len;
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	if (ret == 0)
120*7c478bd9Sstevel@tonic-gate 	{
121*7c478bd9Sstevel@tonic-gate 		*cmd = SMFIC_TIMEOUT;
122*7c478bd9Sstevel@tonic-gate 		return NULL;
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 	else if (ret < 0)
125*7c478bd9Sstevel@tonic-gate 	{
126*7c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
127*7c478bd9Sstevel@tonic-gate 			"%s: mi_rd_cmd: select returned %d: %s",
128*7c478bd9Sstevel@tonic-gate 			name, ret, sm_errstring(errno));
129*7c478bd9Sstevel@tonic-gate 		*cmd = SMFIC_RECVERR;
130*7c478bd9Sstevel@tonic-gate 		return NULL;
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	*cmd = data[MILTER_LEN_BYTES];
134*7c478bd9Sstevel@tonic-gate 	data[MILTER_LEN_BYTES] = '\0';
135*7c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) &expl, (void *) &(data[0]), MILTER_LEN_BYTES);
136*7c478bd9Sstevel@tonic-gate 	expl = ntohl(expl) - 1;
137*7c478bd9Sstevel@tonic-gate 	if (expl <= 0)
138*7c478bd9Sstevel@tonic-gate 		return NULL;
139*7c478bd9Sstevel@tonic-gate 	if (expl > Maxdatasize)
140*7c478bd9Sstevel@tonic-gate 	{
141*7c478bd9Sstevel@tonic-gate 		*cmd = SMFIC_TOOBIG;
142*7c478bd9Sstevel@tonic-gate 		return NULL;
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate #if _FFR_ADD_NULL
145*7c478bd9Sstevel@tonic-gate 	buf = malloc(expl + 1);
146*7c478bd9Sstevel@tonic-gate #else /* _FFR_ADD_NULL */
147*7c478bd9Sstevel@tonic-gate 	buf = malloc(expl);
148*7c478bd9Sstevel@tonic-gate #endif /* _FFR_ADD_NULL */
149*7c478bd9Sstevel@tonic-gate 	if (buf == NULL)
150*7c478bd9Sstevel@tonic-gate 	{
151*7c478bd9Sstevel@tonic-gate 		*cmd = SMFIC_MALLOC;
152*7c478bd9Sstevel@tonic-gate 		return NULL;
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	i = 0;
156*7c478bd9Sstevel@tonic-gate 	for (;;)
157*7c478bd9Sstevel@tonic-gate 	{
158*7c478bd9Sstevel@tonic-gate 		FD_RD_INIT(sd, rds, excs);
159*7c478bd9Sstevel@tonic-gate 		ret = FD_RD_READY(sd, rds, excs, timeout);
160*7c478bd9Sstevel@tonic-gate 		if (ret == 0)
161*7c478bd9Sstevel@tonic-gate 			break;
162*7c478bd9Sstevel@tonic-gate 		else if (ret < 0)
163*7c478bd9Sstevel@tonic-gate 		{
164*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
165*7c478bd9Sstevel@tonic-gate 				continue;
166*7c478bd9Sstevel@tonic-gate 			break;
167*7c478bd9Sstevel@tonic-gate 		}
168*7c478bd9Sstevel@tonic-gate 		if (FD_IS_RD_EXC(sd, rds, excs))
169*7c478bd9Sstevel@tonic-gate 		{
170*7c478bd9Sstevel@tonic-gate 			*cmd = SMFIC_SELECT;
171*7c478bd9Sstevel@tonic-gate 			free(buf);
172*7c478bd9Sstevel@tonic-gate 			return NULL;
173*7c478bd9Sstevel@tonic-gate 		}
174*7c478bd9Sstevel@tonic-gate 		len = MI_SOCK_READ(sd, buf + i, expl - i);
175*7c478bd9Sstevel@tonic-gate 		if (MI_SOCK_READ_FAIL(len))
176*7c478bd9Sstevel@tonic-gate 		{
177*7c478bd9Sstevel@tonic-gate 			smi_log(SMI_LOG_ERR,
178*7c478bd9Sstevel@tonic-gate 				"%s: mi_rd_cmd: read returned %d: %s",
179*7c478bd9Sstevel@tonic-gate 				name, (int) len, sm_errstring(errno));
180*7c478bd9Sstevel@tonic-gate 			ret = -1;
181*7c478bd9Sstevel@tonic-gate 			break;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 		if (len == 0)
184*7c478bd9Sstevel@tonic-gate 		{
185*7c478bd9Sstevel@tonic-gate 			*cmd = SMFIC_EOF;
186*7c478bd9Sstevel@tonic-gate 			free(buf);
187*7c478bd9Sstevel@tonic-gate 			return NULL;
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 		if (len > expl - i)
190*7c478bd9Sstevel@tonic-gate 		{
191*7c478bd9Sstevel@tonic-gate 			*cmd = SMFIC_RECVERR;
192*7c478bd9Sstevel@tonic-gate 			free(buf);
193*7c478bd9Sstevel@tonic-gate 			return NULL;
194*7c478bd9Sstevel@tonic-gate 		}
195*7c478bd9Sstevel@tonic-gate 		if (len >= expl - i)
196*7c478bd9Sstevel@tonic-gate 		{
197*7c478bd9Sstevel@tonic-gate 			*rlen = expl;
198*7c478bd9Sstevel@tonic-gate #if _FFR_ADD_NULL
199*7c478bd9Sstevel@tonic-gate 			/* makes life simpler for common string routines */
200*7c478bd9Sstevel@tonic-gate 			buf[expl] = '\0';
201*7c478bd9Sstevel@tonic-gate #endif /* _FFR_ADD_NULL */
202*7c478bd9Sstevel@tonic-gate 			return buf;
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 		i += len;
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	save_errno = errno;
208*7c478bd9Sstevel@tonic-gate 	free(buf);
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	/* select returned 0 (timeout) or < 0 (error) */
211*7c478bd9Sstevel@tonic-gate 	if (ret == 0)
212*7c478bd9Sstevel@tonic-gate 	{
213*7c478bd9Sstevel@tonic-gate 		*cmd = SMFIC_TIMEOUT;
214*7c478bd9Sstevel@tonic-gate 		return NULL;
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	if (ret < 0)
217*7c478bd9Sstevel@tonic-gate 	{
218*7c478bd9Sstevel@tonic-gate 		smi_log(SMI_LOG_ERR,
219*7c478bd9Sstevel@tonic-gate 			"%s: mi_rd_cmd: select returned %d: %s",
220*7c478bd9Sstevel@tonic-gate 			name, ret, sm_errstring(save_errno));
221*7c478bd9Sstevel@tonic-gate 		*cmd = SMFIC_RECVERR;
222*7c478bd9Sstevel@tonic-gate 		return NULL;
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate 	*cmd = SMFIC_UNKNERR;
225*7c478bd9Sstevel@tonic-gate 	return NULL;
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate /*
229*7c478bd9Sstevel@tonic-gate **  RETRY_WRITEV -- Keep calling the writev() system call
230*7c478bd9Sstevel@tonic-gate **	until all the data is written out or an error occurs.
231*7c478bd9Sstevel@tonic-gate **
232*7c478bd9Sstevel@tonic-gate **	Parameters:
233*7c478bd9Sstevel@tonic-gate **		fd -- socket descriptor
234*7c478bd9Sstevel@tonic-gate **		iov -- io vector
235*7c478bd9Sstevel@tonic-gate **		iovcnt -- number of elements in io vector
236*7c478bd9Sstevel@tonic-gate **			must NOT exceed UIO_MAXIOV.
237*7c478bd9Sstevel@tonic-gate **		timeout -- maximum time to wait
238*7c478bd9Sstevel@tonic-gate **
239*7c478bd9Sstevel@tonic-gate **	Returns:
240*7c478bd9Sstevel@tonic-gate **		success: number of bytes written
241*7c478bd9Sstevel@tonic-gate **		otherwise: MI_FAILURE
242*7c478bd9Sstevel@tonic-gate */
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate static ssize_t
245*7c478bd9Sstevel@tonic-gate retry_writev(fd, iov, iovcnt, timeout)
246*7c478bd9Sstevel@tonic-gate 	socket_t fd;
247*7c478bd9Sstevel@tonic-gate 	struct iovec *iov;
248*7c478bd9Sstevel@tonic-gate 	int iovcnt;
249*7c478bd9Sstevel@tonic-gate 	struct timeval *timeout;
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	int i;
252*7c478bd9Sstevel@tonic-gate 	ssize_t n, written;
253*7c478bd9Sstevel@tonic-gate 	FD_WR_VAR(wrs);
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	written = 0;
256*7c478bd9Sstevel@tonic-gate 	for (;;)
257*7c478bd9Sstevel@tonic-gate 	{
258*7c478bd9Sstevel@tonic-gate 		while (iovcnt > 0 && iov[0].iov_len == 0)
259*7c478bd9Sstevel@tonic-gate 		{
260*7c478bd9Sstevel@tonic-gate 			iov++;
261*7c478bd9Sstevel@tonic-gate 			iovcnt--;
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 		if (iovcnt <= 0)
264*7c478bd9Sstevel@tonic-gate 			return written;
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 		/*
267*7c478bd9Sstevel@tonic-gate 		**  We don't care much about the timeout here,
268*7c478bd9Sstevel@tonic-gate 		**  it's very long anyway; correct solution would be
269*7c478bd9Sstevel@tonic-gate 		**  to take the time before the loop and reduce the
270*7c478bd9Sstevel@tonic-gate 		**  timeout after each invocation.
271*7c478bd9Sstevel@tonic-gate 		**  FD_SETSIZE is checked when socket is created.
272*7c478bd9Sstevel@tonic-gate 		*/
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 		FD_WR_INIT(fd, wrs);
275*7c478bd9Sstevel@tonic-gate 		i = FD_WR_READY(fd, wrs, timeout);
276*7c478bd9Sstevel@tonic-gate 		if (i == 0)
277*7c478bd9Sstevel@tonic-gate 			return MI_FAILURE;
278*7c478bd9Sstevel@tonic-gate 		if (i < 0)
279*7c478bd9Sstevel@tonic-gate 		{
280*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR || errno == EAGAIN)
281*7c478bd9Sstevel@tonic-gate 				continue;
282*7c478bd9Sstevel@tonic-gate 			return MI_FAILURE;
283*7c478bd9Sstevel@tonic-gate 		}
284*7c478bd9Sstevel@tonic-gate 		n = writev(fd, iov, iovcnt);
285*7c478bd9Sstevel@tonic-gate 		if (n == -1)
286*7c478bd9Sstevel@tonic-gate 		{
287*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR || errno == EAGAIN)
288*7c478bd9Sstevel@tonic-gate 				continue;
289*7c478bd9Sstevel@tonic-gate 			return MI_FAILURE;
290*7c478bd9Sstevel@tonic-gate 		}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 		written += n;
293*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < iovcnt; i++)
294*7c478bd9Sstevel@tonic-gate 		{
295*7c478bd9Sstevel@tonic-gate 			if (iov[i].iov_len > (unsigned int) n)
296*7c478bd9Sstevel@tonic-gate 			{
297*7c478bd9Sstevel@tonic-gate 				iov[i].iov_base = (char *)iov[i].iov_base + n;
298*7c478bd9Sstevel@tonic-gate 				iov[i].iov_len -= (unsigned int) n;
299*7c478bd9Sstevel@tonic-gate 				break;
300*7c478bd9Sstevel@tonic-gate 			}
301*7c478bd9Sstevel@tonic-gate 			n -= (int) iov[i].iov_len;
302*7c478bd9Sstevel@tonic-gate 			iov[i].iov_len = 0;
303*7c478bd9Sstevel@tonic-gate 		}
304*7c478bd9Sstevel@tonic-gate 		if (i == iovcnt)
305*7c478bd9Sstevel@tonic-gate 			return written;
306*7c478bd9Sstevel@tonic-gate 	}
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate /*
310*7c478bd9Sstevel@tonic-gate **  MI_WR_CMD -- write a cmd to sd
311*7c478bd9Sstevel@tonic-gate **
312*7c478bd9Sstevel@tonic-gate **	Parameters:
313*7c478bd9Sstevel@tonic-gate **		sd -- socket descriptor
314*7c478bd9Sstevel@tonic-gate **		timeout -- maximum time to wait
315*7c478bd9Sstevel@tonic-gate **		cmd -- single character command to write
316*7c478bd9Sstevel@tonic-gate **		buf -- buffer with further data
317*7c478bd9Sstevel@tonic-gate **		len -- length of buffer (without cmd!)
318*7c478bd9Sstevel@tonic-gate **
319*7c478bd9Sstevel@tonic-gate **	Returns:
320*7c478bd9Sstevel@tonic-gate **		MI_SUCCESS/MI_FAILURE
321*7c478bd9Sstevel@tonic-gate */
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate int
324*7c478bd9Sstevel@tonic-gate mi_wr_cmd(sd, timeout, cmd, buf, len)
325*7c478bd9Sstevel@tonic-gate 	socket_t sd;
326*7c478bd9Sstevel@tonic-gate 	struct timeval *timeout;
327*7c478bd9Sstevel@tonic-gate 	int cmd;
328*7c478bd9Sstevel@tonic-gate 	char *buf;
329*7c478bd9Sstevel@tonic-gate 	size_t len;
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate 	size_t sl, i;
332*7c478bd9Sstevel@tonic-gate 	ssize_t l;
333*7c478bd9Sstevel@tonic-gate 	mi_int32 nl;
334*7c478bd9Sstevel@tonic-gate 	int iovcnt;
335*7c478bd9Sstevel@tonic-gate 	struct iovec iov[2];
336*7c478bd9Sstevel@tonic-gate 	char data[MILTER_LEN_BYTES + 1];
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	if (len > Maxdatasize || (len > 0 && buf == NULL))
339*7c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	nl = htonl(len + 1);	/* add 1 for the cmd char */
342*7c478bd9Sstevel@tonic-gate 	(void) memcpy(data, (void *) &nl, MILTER_LEN_BYTES);
343*7c478bd9Sstevel@tonic-gate 	data[MILTER_LEN_BYTES] = (char) cmd;
344*7c478bd9Sstevel@tonic-gate 	i = 0;
345*7c478bd9Sstevel@tonic-gate 	sl = MILTER_LEN_BYTES + 1;
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	/* set up the vector for the size / command */
348*7c478bd9Sstevel@tonic-gate 	iov[0].iov_base = (void *) data;
349*7c478bd9Sstevel@tonic-gate 	iov[0].iov_len  = sl;
350*7c478bd9Sstevel@tonic-gate 	iovcnt = 1;
351*7c478bd9Sstevel@tonic-gate 	if (len >= 0 && buf != NULL)
352*7c478bd9Sstevel@tonic-gate 	{
353*7c478bd9Sstevel@tonic-gate 		iov[1].iov_base = (void *) buf;
354*7c478bd9Sstevel@tonic-gate 		iov[1].iov_len  = len;
355*7c478bd9Sstevel@tonic-gate 		iovcnt = 2;
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	l = retry_writev(sd, iov, iovcnt, timeout);
359*7c478bd9Sstevel@tonic-gate 	if (l == MI_FAILURE)
360*7c478bd9Sstevel@tonic-gate 		return MI_FAILURE;
361*7c478bd9Sstevel@tonic-gate 	return MI_SUCCESS;
362*7c478bd9Sstevel@tonic-gate }
363