xref: /onnv-gate/usr/src/cmd/lp/lib/msgs/streamio.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2	*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include	<unistd.h>
35*0Sstevel@tonic-gate #include	<signal.h>
36*0Sstevel@tonic-gate #include	<stropts.h>
37*0Sstevel@tonic-gate #include	<errno.h>
38*0Sstevel@tonic-gate #include	"lp.h"
39*0Sstevel@tonic-gate #include	"msgs.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate extern	int	errno;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate  * Putmsg() function
45*0Sstevel@tonic-gate  * Return 0: success,
46*0Sstevel@tonic-gate  *        non-zero: return code of the failed putmsg() system call.
47*0Sstevel@tonic-gate  *		    plus errno for caller to check.
48*0Sstevel@tonic-gate  * NOTE: cannot do TRACE* calls if errno is expected to be returned!
49*0Sstevel@tonic-gate  *	 TRACE* uses fprintf and destroys the content of errno.
50*0Sstevel@tonic-gate  *	 Save errno before the TRACE* calls.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate int
Putmsg(MESG * mdp,strbuf_t * ctlp,strbuf_t * datap,int flags)54*0Sstevel@tonic-gate Putmsg (MESG *mdp, strbuf_t *ctlp, strbuf_t *datap, int flags)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate 	int	i;
57*0Sstevel@tonic-gate 	int	rtncode;
58*0Sstevel@tonic-gate 	int	count;
59*0Sstevel@tonic-gate 	struct pollfd fds;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	fds.fd = mdp->writefd;
62*0Sstevel@tonic-gate 	fds.events = POLLOUT;
63*0Sstevel@tonic-gate 	fds.revents = 0;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	(void) poll(&fds, 1, 1000);
66*0Sstevel@tonic-gate 	if (fds.revents & (POLLHUP | POLLERR | POLLNVAL)) {
67*0Sstevel@tonic-gate 		errno = EBADF;
68*0Sstevel@tonic-gate 		return (-1);
69*0Sstevel@tonic-gate 	}
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (!(fds.revents & POLLOUT)) {
72*0Sstevel@tonic-gate 		errno = EAGAIN;
73*0Sstevel@tonic-gate 		return (-1);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	rtncode = putmsg (mdp->writefd, ctlp, datap, flags);
77*0Sstevel@tonic-gate 	return (rtncode);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate int
Getmsg(MESG * mdp,strbuf_t * ctlp,strbuf_t * datap,int * flagsp)81*0Sstevel@tonic-gate Getmsg (MESG *mdp, strbuf_t *ctlp, strbuf_t *datap, int *flagsp)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	int	rtncode;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	rtncode = getmsg (mdp->readfd, ctlp, datap, flagsp);
86*0Sstevel@tonic-gate 	return (rtncode);
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate char		AuthCode[HEAD_AUTHCODE_LEN];
90*0Sstevel@tonic-gate static void	(*callers_sigpipe_trap)() = SIG_DFL;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate /*
94*0Sstevel@tonic-gate **	Function:	static int read3_2( MESG *, char *, int)
95*0Sstevel@tonic-gate **	Args:		message descriptor
96*0Sstevel@tonic-gate **			message buffer (var)
97*0Sstevel@tonic-gate **			buffer size
98*0Sstevel@tonic-gate **	Return:		0 for sucess, -1 for failure
99*0Sstevel@tonic-gate **
100*0Sstevel@tonic-gate **	This performs a 3.2 HPI style read_fifo on the pipe referanced
101*0Sstevel@tonic-gate **	in the message descriptor.  If a message is found, it is returned
102*0Sstevel@tonic-gate **	in message buffer.
103*0Sstevel@tonic-gate */
read3_2(MESG * md,char * msgbuf,int size)104*0Sstevel@tonic-gate int read3_2 ( MESG * md, char *msgbuf, int size )
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate     short	type;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate     if (md->type == MD_USR_FIFO)
109*0Sstevel@tonic-gate 	(void) Close (Open(md->file, O_RDONLY, 0));
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate     do
112*0Sstevel@tonic-gate     {
113*0Sstevel@tonic-gate 	switch (read_fifo(md->readfd, msgbuf, size))
114*0Sstevel@tonic-gate 	{
115*0Sstevel@tonic-gate 	  case -1:
116*0Sstevel@tonic-gate 	    return (-1);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	  case 0:
119*0Sstevel@tonic-gate 	    /*
120*0Sstevel@tonic-gate 	     ** The fifo was empty and we have O_NDELAY set,
121*0Sstevel@tonic-gate 	     ** or the Spooler closed our FIFO.
122*0Sstevel@tonic-gate 	     ** We don't set O_NDELAY in the user process,
123*0Sstevel@tonic-gate 	     ** so that should never happen. But be warned
124*0Sstevel@tonic-gate 	     ** that we can't tell the difference in some versions
125*0Sstevel@tonic-gate 	     ** of the UNIX op. sys.!!
126*0Sstevel@tonic-gate 	     **
127*0Sstevel@tonic-gate 	     */
128*0Sstevel@tonic-gate 	    errno = EPIPE;
129*0Sstevel@tonic-gate 	    return (-1);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if ((type = stoh(msgbuf + HEAD_TYPE)) < 0 || LAST_MESSAGE < type)
133*0Sstevel@tonic-gate 	{
134*0Sstevel@tonic-gate 	    errno = ENOMSG;
135*0Sstevel@tonic-gate 	    return (-1);
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate     }
138*0Sstevel@tonic-gate     while (type == I_QUEUE_CHK);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate     (void)memcpy (AuthCode, msgbuf + HEAD_AUTHCODE, HEAD_AUTHCODE_LEN);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate     /*
143*0Sstevel@tonic-gate     **	Get the size from the 3.2 HPI message
144*0Sstevel@tonic-gate     **	minus the size of the control data
145*0Sstevel@tonic-gate     **	Copy the actual message
146*0Sstevel@tonic-gate     **	Reset the message size.
147*0Sstevel@tonic-gate     */
148*0Sstevel@tonic-gate     size = stoh(msgbuf + HEAD_SIZE) - EXCESS_3_2_LEN;
149*0Sstevel@tonic-gate     memmove(msgbuf, msgbuf + HEAD_SIZE, size);
150*0Sstevel@tonic-gate     (void) htos(msgbuf + MESG_SIZE, size);
151*0Sstevel@tonic-gate     return(0);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
write3_2(MESG * md,char * msgbuf,int size)154*0Sstevel@tonic-gate int write3_2 ( MESG * md, char * msgbuf, int size )
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate     char	tmpbuf [MSGMAX + EXCESS_3_2_LEN];
157*0Sstevel@tonic-gate     int		rval;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate     (void) memmove(tmpbuf + HEAD_SIZE, msgbuf, size);
161*0Sstevel@tonic-gate     (void) htos(tmpbuf + HEAD_SIZE, size + EXCESS_3_2_LEN);
162*0Sstevel@tonic-gate     (void) memcpy (tmpbuf + HEAD_AUTHCODE, AuthCode, HEAD_AUTHCODE_LEN);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate     callers_sigpipe_trap = signal(SIGPIPE, SIG_IGN);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate     rval = write_fifo(md->writefd, tmpbuf, size + EXCESS_3_2_LEN);
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate     (void) signal(SIGPIPE, callers_sigpipe_trap);
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate     return (rval);
172*0Sstevel@tonic-gate }
173