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