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