1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/* LINTLIBRARY */
# include <errno.h>
# include <string.h>
#include <syslog.h>
# include "lp.h"
# include "msgs.h"
extern char Resync[];
extern char Endsync[];
static int Had_Full_Buffer = 1;
int Garbage_Bytes = 0;
int Garbage_Messages= 0;
static int _buffer(int);
/*
** A real message is written in one piece, and the write
** is atomic. Thus, even if the O_NDELAY flag is set,
** if we read part of the real message, we can continue
** to read the rest of it in as many steps as we want
** (up to the size of the message, of course!) without
** UNIX returning 0 because no data is available.
** So, a real message doesn't have to be read in one piece,
** which is good since we don't know how much to read!
**
** Fake messages, or improperly written messages, don't
** have this nice property.
**
** INTERRUPTED READS:
**
** If a signal occurs during an attempted read, we can exit.
** The caller can retry the read and we will correctly restart
** it. The correctness of this assertion can be seen by noticing
** that at the beginning of each READ below, we can go back
** to the first statement executed (the first READ below)
** and correctly reexecute the code.
**
** If the last writer closed the fifo, we'll read 0 bytes
** (at least on the subsequent read). If we were in the
** middle of reading a message, we were reading a bogus
** message (but see below).
**
** If we read less than we expect, it's because we were
** reading a fake message (but see below).
**
** HOWEVER: In the last two cases, we may have ONE OR MORE
** REAL MESSAGES snuggled in amongst the trash!
**
** All this verbal rambling is preface to let you understand why we
** buffer the data (which is a shame, but necessary).
*/
/*
** As long as we get real messages, we can avoid needless function calls.
** The SYNC argument in this macro should be set if the resynch. bytes
** have been read--i.e. if the rest of the message is trying to be read.
** In this case, if we had not read a full buffer last time, then we
** must be in the middle of a bogus message.
*/
#define UNSYNCHED_READ(N) \
if (fbp->psave_end - fbp->psave < N || fbp->psave >= fbp->psave_end) \
{ \
switch (_buffer(fifo)) \
{ \
case -1: \
return (-1); \
case 0: \
if (fbp->psave_end > fbp->psave) \
goto SyncUp; \
return (0); \
} \
}
#define SYNCHED_READ(N) \
if (fbp->psave_end - fbp->psave < N || fbp->psave >= fbp->psave_end) \
{ \
switch (_buffer(fifo)) \
{ \
case -1: \
return (-1); \
case 0: \
if (fbp->psave_end > fbp->psave) \
goto SyncUp; \
return (0); \
} \
if (!Had_Full_Buffer) \
goto SyncUp; \
}
/*
** read_fifo() - READ A BUFFER WITH HEADER AND CHECKSUM
*/
int
read_fifo (fifo, buf, size)
int fifo;
char *buf;
unsigned int size;
{
register fifobuffer_t *fbp;
register unsigned int real_chksum,
chksum,
real_size;
/*
** Make sure we start on a message boundary. The first
** line of defense is to look for the resync. bytes.
**
** The "SyncUp" label is global to this routine (below this point)
** and is called whenever we determine that we're out
** of sync. with the incoming bytes.
*/
if (!(fbp=GetFifoBuffer (fifo)))
return -1;
UNSYNCHED_READ (HEAD_RESYNC_LEN);
while (*fbp->psave != Resync[0] || *(fbp->psave + 1) != Resync[1])
{
SyncUp:
#if defined(TRACE_MESSAGES)
if (trace_messages)
syslog(LOG_DEBUG, "DISCARD %c\n", *fbp->psave);
#endif
fbp->psave++;
Garbage_Bytes++;
UNSYNCHED_READ (HEAD_RESYNC_LEN);
}
/*
** We're sync'd, so read the full header.
*/
SYNCHED_READ (HEAD_LEN);
/*
** If the header size is smaller than the minimum size for a header,
** or larger than allowed, we must assume that we really aren't
** synchronized.
*/
real_size = stoh(fbp->psave + HEAD_SIZE);
if (real_size < CONTROL_LEN || MSGMAX < real_size)
{
#if defined(TRACE_MESSAGES)
if (trace_messages)
syslog(LOG_DEBUG, "BAD SIZE\n");
#endif
goto SyncUp;
}
/*
** We have the header. Now we can finally read the rest of the
** message...
*/
SYNCHED_READ (real_size);
/*
** ...but did we read a real message?...
*/
if
(
*(fbp->psave + TAIL_ENDSYNC(real_size)) != Endsync[0]
|| *(fbp->psave + TAIL_ENDSYNC(real_size) + 1) != Endsync[1]
)
{
#if defined(TRACE_MESSAGES)
if (trace_messages)
syslog(LOG_DEBUG, "BAD ENDSYNC\n");
#endif
Garbage_Messages++;
goto SyncUp;
}
chksum = stoh(fbp->psave + TAIL_CHKSUM(real_size));
CALC_CHKSUM (fbp->psave, real_size, real_chksum);
if (real_chksum != chksum)
{
#if defined(TRACE_MESSAGES)
if (trace_messages)
syslog(LOG_DEBUG, "BAD CHKSUM\n");
#endif
Garbage_Messages++;
goto SyncUp;
}
/*
** ...yes!...but can the caller handle the message?
*/
if (size < real_size)
{
errno = E2BIG;
return (-1);
}
/*
** Yes!! We can finally copy the message into the caller's buffer
** and remove it from our buffer. That wasn't so bad, was it?
*/
#if defined(TRACE_MESSAGES)
if (trace_messages)
syslog(LOG_DEBUG, "MESSAGE: %-.*s", real_size, fbp->psave);
#endif
(void)memcpy (buf, fbp->psave, real_size);
fbp->psave += real_size;
return (real_size);
}
int
peek3_2 (fifo)
int fifo;
{
register fifobuffer_t *fbp;
register unsigned int real_size;
/*
** Make sure we start on a message boundary. The first
** line of defense is to look for the resync. bytes.
**
** The "SyncUp" label is global to this routine (below this point)
** and is called whenever we determine that we're out
** of sync. with the incoming bytes.
*/
if (!(fbp=GetFifoBuffer (fifo)))
return -1;
UNSYNCHED_READ (HEAD_RESYNC_LEN);
while (*fbp->psave != Resync[0] || *(fbp->psave + 1) != Resync[1])
{
SyncUp:
fbp->psave++;
Garbage_Bytes++;
UNSYNCHED_READ (HEAD_RESYNC_LEN);
}
/*
** We're sync'd, so read the full header.
*/
SYNCHED_READ (HEAD_LEN);
/*
** If the header size is smaller than the minimum size for a header,
** or larger than allowed, we must assume that we really aren't
** synchronized.
*/
real_size = stoh(fbp->psave + HEAD_SIZE);
if (real_size < CONTROL_LEN || MSGMAX < real_size)
{
goto SyncUp;
}
return(real_size);
}
static int
_buffer(int fifo)
{
int n, nbytes, count = 0;
register fifobuffer_t *fbp;
/*
** As long as we get real messages, and if we chose
** SAVE_SIZE well, we shouldn't have to move the data
** in the "else" branch below: Each time we call "read"
** we aren't likely to get as many bytes as we ask for,
** just as many as are in the fifo, AND THIS SHOULD
** REPRESENT AN INTEGRAL NUMBER OF MESSAGES. Since
** the "read_fifo" routine reads complete messages,
** it will end its read at the end of the message,
** which (eventually) will make "psave_end" == "psave".
*/
/*
** If the buffer is empty, there's nothing to move.
*/
if (!(fbp = GetFifoBuffer (fifo)))
return -1;
if (fbp->psave_end == fbp->psave)
fbp->psave = fbp->psave_end = fbp->save; /* sane pointers! */
/*
** If the buffer has data at the high end, move it down.
*/
else
if (fbp->psave != fbp->save) /* sane pointers! */
{
/*
** Move the data still left in the buffer to the
** front, so we can read as much as possible into
** buffer after it.
*/
memmove(fbp->save, fbp->psave, fbp->psave_end - fbp->psave);
fbp->psave_end = fbp->save + (fbp->psave_end - fbp->psave);
fbp->psave = fbp->save; /* sane pointers! */
}
/*
** The "fbp->psave" and "fbp->psave_end" pointers must be in a sane
** state when we get here, in case the "read()" gets interrupted.
** When that happens, we return to the caller who may try
** to restart us! Sane: fbp->psave == fbp->save (HERE!)
*/
nbytes = MSGMAX - (fbp->psave_end - fbp->save);
while ((n = read(fifo, fbp->psave_end, nbytes)) == 0 && count < 60)
{
(void) sleep ((unsigned) 1);
count++;
}
if (n > 0)
fbp->psave_end += n;
Had_Full_Buffer = fbp->full;
fbp->full = (nbytes == n);
return (n);
}
|