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 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.8 */
32*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate # include <errno.h>
35*7c478bd9Sstevel@tonic-gate # include <string.h>
36*7c478bd9Sstevel@tonic-gate # include <stropts.h>
37*7c478bd9Sstevel@tonic-gate
38*7c478bd9Sstevel@tonic-gate # include "lp.h"
39*7c478bd9Sstevel@tonic-gate # include "msgs.h"
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate int Lp_prio_msg = 0;
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate static int _mwrite ( MESG * md , char * msgbuf , int );
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate * mflush()
47*7c478bd9Sstevel@tonic-gate * return 0
48*7c478bd9Sstevel@tonic-gate * if it successfully writes all the queued message(s), or
49*7c478bd9Sstevel@tonic-gate * if some of them (errno is EAGAIN in this case).
50*7c478bd9Sstevel@tonic-gate * return -1 (with errno) when it failed.
51*7c478bd9Sstevel@tonic-gate */
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate int
mflush(MESG * md)54*7c478bd9Sstevel@tonic-gate mflush(MESG *md)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate MQUE *p;
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate errno = 0;
59*7c478bd9Sstevel@tonic-gate if (md == NULL || md->mque == NULL) {
60*7c478bd9Sstevel@tonic-gate errno = ENXIO;
61*7c478bd9Sstevel@tonic-gate return (-1);
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate while ((p = md->mque) != NULL) {
65*7c478bd9Sstevel@tonic-gate if (_mwrite(md, p->dat->buf, p->dat->len) != 0)
66*7c478bd9Sstevel@tonic-gate return (errno == EAGAIN ? 0 : -1);
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate /* mwrite successful, get the next and free this entry */
69*7c478bd9Sstevel@tonic-gate md->mque = p->next;
70*7c478bd9Sstevel@tonic-gate Free(p->dat->buf);
71*7c478bd9Sstevel@tonic-gate Free(p->dat);
72*7c478bd9Sstevel@tonic-gate Free(p);
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate return (0);
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate * mwrite()
80*7c478bd9Sstevel@tonic-gate * return 0
81*7c478bd9Sstevel@tonic-gate * if it successfully writes the messages, or
82*7c478bd9Sstevel@tonic-gate * if it has been queued (errno is EAGAIN in this case)
83*7c478bd9Sstevel@tonic-gate * and md->mque is updated.
84*7c478bd9Sstevel@tonic-gate * return -1 (with errno) when it failed.
85*7c478bd9Sstevel@tonic-gate */
86*7c478bd9Sstevel@tonic-gate
mwrite(MESG * md,char * msgbuf)87*7c478bd9Sstevel@tonic-gate int mwrite ( MESG * md, char * msgbuf )
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate short size;
90*7c478bd9Sstevel@tonic-gate MQUE * p;
91*7c478bd9Sstevel@tonic-gate MQUE * q;
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate errno = 0;
94*7c478bd9Sstevel@tonic-gate if (md == NULL)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate errno = ENXIO;
97*7c478bd9Sstevel@tonic-gate return(-1);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate if (msgbuf == NULL)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate errno = EINVAL;
102*7c478bd9Sstevel@tonic-gate return(-1);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate size = stoh(msgbuf);
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate if (LAST_MESSAGE < stoh(msgbuf + MESG_TYPE))
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate errno = EINVAL;
110*7c478bd9Sstevel@tonic-gate return (-1);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate if (md->mque)
113*7c478bd9Sstevel@tonic-gate goto queue; /* if there is a queue already, try to write all */
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate if (_mwrite(md, msgbuf, size) == 0)
116*7c478bd9Sstevel@tonic-gate return(0);
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate if (errno != EAGAIN)
119*7c478bd9Sstevel@tonic-gate return(-1);
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate * fall through to queue the messages that cannot be sent now.
123*7c478bd9Sstevel@tonic-gate */
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate queue:
126*7c478bd9Sstevel@tonic-gate if ((p = (MQUE *)Malloc(sizeof(MQUE))) == NULL
127*7c478bd9Sstevel@tonic-gate || (p->dat = (struct strbuf *)Malloc(sizeof(struct strbuf))) == NULL
128*7c478bd9Sstevel@tonic-gate || (p->dat->buf = (char *)Malloc(size)) == NULL)
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate errno = ENOMEM;
131*7c478bd9Sstevel@tonic-gate return(-1);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate (void) memcpy(p->dat->buf, msgbuf, size);
134*7c478bd9Sstevel@tonic-gate p->dat->len = size;
135*7c478bd9Sstevel@tonic-gate p->next = 0;
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate if ((q = md->mque) != NULL)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate /* insert the new one to tail */
140*7c478bd9Sstevel@tonic-gate while (q->next)
141*7c478bd9Sstevel@tonic-gate q = q->next;
142*7c478bd9Sstevel@tonic-gate q->next = p;
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate while ((p = md->mque) != NULL)
145*7c478bd9Sstevel@tonic-gate {
146*7c478bd9Sstevel@tonic-gate if (_mwrite(md, p->dat->buf, p->dat->len) != 0) {
147*7c478bd9Sstevel@tonic-gate return (errno == EAGAIN ? 0 : -1);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate /* mwrite successful, get the next and free this entry */
151*7c478bd9Sstevel@tonic-gate md->mque = p->next;
152*7c478bd9Sstevel@tonic-gate Free(p->dat->buf);
153*7c478bd9Sstevel@tonic-gate Free(p->dat);
154*7c478bd9Sstevel@tonic-gate Free(p);
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate else
158*7c478bd9Sstevel@tonic-gate md->mque = p;
159*7c478bd9Sstevel@tonic-gate
160*7c478bd9Sstevel@tonic-gate return(0);
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate
_mwrite(MESG * md,char * msgbuf,int size)163*7c478bd9Sstevel@tonic-gate int _mwrite ( MESG * md, char * msgbuf , int size )
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate int flag = 0;
166*7c478bd9Sstevel@tonic-gate struct strbuf ctl;
167*7c478bd9Sstevel@tonic-gate struct strbuf dat;
168*7c478bd9Sstevel@tonic-gate
169*7c478bd9Sstevel@tonic-gate switch (md->type)
170*7c478bd9Sstevel@tonic-gate {
171*7c478bd9Sstevel@tonic-gate case MD_CHILD:
172*7c478bd9Sstevel@tonic-gate case MD_STREAM:
173*7c478bd9Sstevel@tonic-gate case MD_BOUND:
174*7c478bd9Sstevel@tonic-gate if (size <= 0 || size > MSGMAX)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate errno = EINVAL;
177*7c478bd9Sstevel@tonic-gate return(-1);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate ctl.buf = "xyzzy";
181*7c478bd9Sstevel@tonic-gate ctl.maxlen = ctl.len = strlen(ctl.buf)+1;
182*7c478bd9Sstevel@tonic-gate dat.buf = msgbuf;
183*7c478bd9Sstevel@tonic-gate dat.maxlen = dat.len = size;
184*7c478bd9Sstevel@tonic-gate flag = Lp_prio_msg;
185*7c478bd9Sstevel@tonic-gate Lp_prio_msg = 0; /* clean this up so there are no surprises */
186*7c478bd9Sstevel@tonic-gate
187*7c478bd9Sstevel@tonic-gate if (Putmsg(md, &ctl, &dat, flag) == 0)
188*7c478bd9Sstevel@tonic-gate return(0);
189*7c478bd9Sstevel@tonic-gate return(-1);
190*7c478bd9Sstevel@tonic-gate
191*7c478bd9Sstevel@tonic-gate case MD_SYS_FIFO:
192*7c478bd9Sstevel@tonic-gate case MD_USR_FIFO:
193*7c478bd9Sstevel@tonic-gate switch (write3_2(md, msgbuf, size))
194*7c478bd9Sstevel@tonic-gate {
195*7c478bd9Sstevel@tonic-gate case -1:
196*7c478bd9Sstevel@tonic-gate return(-1);
197*7c478bd9Sstevel@tonic-gate case 0:
198*7c478bd9Sstevel@tonic-gate break;
199*7c478bd9Sstevel@tonic-gate default:
200*7c478bd9Sstevel@tonic-gate return(0);
201*7c478bd9Sstevel@tonic-gate }
202*7c478bd9Sstevel@tonic-gate break;
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate default:
205*7c478bd9Sstevel@tonic-gate errno = EINVAL;
206*7c478bd9Sstevel@tonic-gate return(-1);
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate return 0;
209*7c478bd9Sstevel@tonic-gate }
210