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