xref: /onnv-gate/usr/src/cmd/lp/lib/msgs/read_fifo.c (revision 320:7e1ecf3ab9be)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*320Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*320Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /* LINTLIBRARY */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate # include	<errno.h>
350Sstevel@tonic-gate # include	<string.h>
360Sstevel@tonic-gate #include <syslog.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate # include	"lp.h"
390Sstevel@tonic-gate # include	"msgs.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate extern char	Resync[];
420Sstevel@tonic-gate extern char	Endsync[];
430Sstevel@tonic-gate static int	Had_Full_Buffer = 1;
440Sstevel@tonic-gate int		Garbage_Bytes	= 0;
450Sstevel@tonic-gate int		Garbage_Messages= 0;
460Sstevel@tonic-gate 
47*320Sceastha static int _buffer(int);
48*320Sceastha 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate ** A real message is written in one piece, and the write
510Sstevel@tonic-gate ** is atomic. Thus, even if the O_NDELAY flag is set,
520Sstevel@tonic-gate ** if we read part of the real message, we can continue
530Sstevel@tonic-gate ** to read the rest of it in as many steps as we want
540Sstevel@tonic-gate ** (up to the size of the message, of course!) without
550Sstevel@tonic-gate ** UNIX returning 0 because no data is available.
560Sstevel@tonic-gate ** So, a real message doesn't have to be read in one piece,
570Sstevel@tonic-gate ** which is good since we don't know how much to read!
580Sstevel@tonic-gate **
590Sstevel@tonic-gate ** Fake messages, or improperly written messages, don't
600Sstevel@tonic-gate ** have this nice property.
610Sstevel@tonic-gate **
620Sstevel@tonic-gate ** INTERRUPTED READS:
630Sstevel@tonic-gate **
640Sstevel@tonic-gate ** If a signal occurs during an attempted read, we can exit.
650Sstevel@tonic-gate ** The caller can retry the read and we will correctly restart
660Sstevel@tonic-gate ** it. The correctness of this assertion can be seen by noticing
670Sstevel@tonic-gate ** that at the beginning of each READ below, we can go back
680Sstevel@tonic-gate ** to the first statement executed (the first READ below)
690Sstevel@tonic-gate ** and correctly reexecute the code.
700Sstevel@tonic-gate **
710Sstevel@tonic-gate ** If the last writer closed the fifo, we'll read 0 bytes
720Sstevel@tonic-gate ** (at least on the subsequent read). If we were in the
730Sstevel@tonic-gate ** middle of reading a message, we were reading a bogus
740Sstevel@tonic-gate ** message (but see below).
750Sstevel@tonic-gate **
760Sstevel@tonic-gate ** If we read less than we expect, it's because we were
770Sstevel@tonic-gate ** reading a fake message (but see below).
780Sstevel@tonic-gate **
790Sstevel@tonic-gate ** HOWEVER: In the last two cases, we may have ONE OR MORE
800Sstevel@tonic-gate ** REAL MESSAGES snuggled in amongst the trash!
810Sstevel@tonic-gate **
820Sstevel@tonic-gate ** All this verbal rambling is preface to let you understand why we
830Sstevel@tonic-gate ** buffer the data (which is a shame, but necessary).
840Sstevel@tonic-gate */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate ** As long as we get real messages, we can avoid needless function calls.
880Sstevel@tonic-gate ** The SYNC argument in this macro should be set if the resynch. bytes
890Sstevel@tonic-gate ** have been read--i.e. if the rest of the message is trying to be read.
900Sstevel@tonic-gate ** In this case, if we had not read a full buffer last time, then we
910Sstevel@tonic-gate ** must be in the middle of a bogus message.
920Sstevel@tonic-gate */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #define UNSYNCHED_READ(N) \
950Sstevel@tonic-gate     if (fbp->psave_end - fbp->psave < N || fbp->psave >= fbp->psave_end) \
960Sstevel@tonic-gate     { \
970Sstevel@tonic-gate 	switch (_buffer(fifo)) \
980Sstevel@tonic-gate 	{ \
990Sstevel@tonic-gate 	    case -1: \
1000Sstevel@tonic-gate 		return (-1); \
1010Sstevel@tonic-gate 	    case 0: \
1020Sstevel@tonic-gate 		if (fbp->psave_end > fbp->psave) \
1030Sstevel@tonic-gate 		    goto SyncUp; \
1040Sstevel@tonic-gate 		return (0); \
1050Sstevel@tonic-gate 	} \
1060Sstevel@tonic-gate     }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #define SYNCHED_READ(N) \
1090Sstevel@tonic-gate     if (fbp->psave_end - fbp->psave < N || fbp->psave >= fbp->psave_end) \
1100Sstevel@tonic-gate     { \
1110Sstevel@tonic-gate 	switch (_buffer(fifo)) \
1120Sstevel@tonic-gate 	{ \
1130Sstevel@tonic-gate 	    case -1: \
1140Sstevel@tonic-gate 		return (-1); \
1150Sstevel@tonic-gate 	    case 0: \
1160Sstevel@tonic-gate 		if (fbp->psave_end > fbp->psave) \
1170Sstevel@tonic-gate 		    goto SyncUp; \
1180Sstevel@tonic-gate 		return (0); \
1190Sstevel@tonic-gate 	} \
1200Sstevel@tonic-gate 	if (!Had_Full_Buffer) \
1210Sstevel@tonic-gate 	    goto SyncUp; \
1220Sstevel@tonic-gate     }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate ** read_fifo() - READ A BUFFER WITH HEADER AND CHECKSUM
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate int
read_fifo(fifo,buf,size)1280Sstevel@tonic-gate read_fifo (fifo, buf, size)
1290Sstevel@tonic-gate int		fifo;
1300Sstevel@tonic-gate char		*buf;
1310Sstevel@tonic-gate unsigned int	size;
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate     register fifobuffer_t *fbp;
1340Sstevel@tonic-gate     register unsigned int real_chksum,
1350Sstevel@tonic-gate 			  chksum,
1360Sstevel@tonic-gate 			  real_size;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate     /*
1390Sstevel@tonic-gate     ** Make sure we start on a message boundary. The first
1400Sstevel@tonic-gate     ** line of defense is to look for the resync. bytes.
1410Sstevel@tonic-gate     **
1420Sstevel@tonic-gate     ** The "SyncUp" label is global to this routine (below this point)
1430Sstevel@tonic-gate     ** and is called whenever we determine that we're out
1440Sstevel@tonic-gate     ** of sync. with the incoming bytes.
1450Sstevel@tonic-gate     */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate     if (!(fbp=GetFifoBuffer (fifo)))
1480Sstevel@tonic-gate 	return	-1;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate     UNSYNCHED_READ (HEAD_RESYNC_LEN);
1510Sstevel@tonic-gate     while (*fbp->psave != Resync[0] || *(fbp->psave + 1) != Resync[1])
1520Sstevel@tonic-gate     {
1530Sstevel@tonic-gate SyncUp:
1540Sstevel@tonic-gate #if	defined(TRACE_MESSAGES)
1550Sstevel@tonic-gate 	if (trace_messages)
1560Sstevel@tonic-gate 		syslog(LOG_DEBUG, "DISCARD %c\n", *fbp->psave);
1570Sstevel@tonic-gate #endif
1580Sstevel@tonic-gate 	fbp->psave++;
1590Sstevel@tonic-gate 	Garbage_Bytes++;
1600Sstevel@tonic-gate 	UNSYNCHED_READ (HEAD_RESYNC_LEN);
1610Sstevel@tonic-gate     }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate     /*
1650Sstevel@tonic-gate     ** We're sync'd, so read the full header.
1660Sstevel@tonic-gate     */
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate     SYNCHED_READ (HEAD_LEN);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate     /*
1720Sstevel@tonic-gate     ** If the header size is smaller than the minimum size for a header,
1730Sstevel@tonic-gate     ** or larger than allowed, we must assume that we really aren't
1740Sstevel@tonic-gate     ** synchronized.
1750Sstevel@tonic-gate     */
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate     real_size = stoh(fbp->psave + HEAD_SIZE);
1780Sstevel@tonic-gate     if (real_size < CONTROL_LEN || MSGMAX < real_size)
1790Sstevel@tonic-gate     {
1800Sstevel@tonic-gate #if	defined(TRACE_MESSAGES)
1810Sstevel@tonic-gate 	if (trace_messages)
1820Sstevel@tonic-gate 		syslog(LOG_DEBUG, "BAD SIZE\n");
1830Sstevel@tonic-gate #endif
1840Sstevel@tonic-gate 	goto SyncUp;
1850Sstevel@tonic-gate     }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate     /*
1880Sstevel@tonic-gate     ** We have the header. Now we can finally read the rest of the
1890Sstevel@tonic-gate     ** message...
1900Sstevel@tonic-gate     */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate     SYNCHED_READ (real_size);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate     /*
1960Sstevel@tonic-gate     ** ...but did we read a real message?...
1970Sstevel@tonic-gate     */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate     if
2000Sstevel@tonic-gate     (
2010Sstevel@tonic-gate 	   *(fbp->psave + TAIL_ENDSYNC(real_size)) != Endsync[0]
2020Sstevel@tonic-gate 	|| *(fbp->psave + TAIL_ENDSYNC(real_size) + 1) != Endsync[1]
2030Sstevel@tonic-gate     )
2040Sstevel@tonic-gate     {
2050Sstevel@tonic-gate #if	defined(TRACE_MESSAGES)
2060Sstevel@tonic-gate 	if (trace_messages)
2070Sstevel@tonic-gate 		syslog(LOG_DEBUG, "BAD ENDSYNC\n");
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate 	Garbage_Messages++;
2100Sstevel@tonic-gate 	goto SyncUp;
2110Sstevel@tonic-gate     }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate     chksum = stoh(fbp->psave + TAIL_CHKSUM(real_size));
2140Sstevel@tonic-gate     CALC_CHKSUM (fbp->psave, real_size, real_chksum);
2150Sstevel@tonic-gate     if (real_chksum != chksum)
2160Sstevel@tonic-gate     {
2170Sstevel@tonic-gate #if	defined(TRACE_MESSAGES)
2180Sstevel@tonic-gate 	if (trace_messages)
2190Sstevel@tonic-gate 		syslog(LOG_DEBUG, "BAD CHKSUM\n");
2200Sstevel@tonic-gate #endif
2210Sstevel@tonic-gate 	Garbage_Messages++;
2220Sstevel@tonic-gate 	goto SyncUp;
2230Sstevel@tonic-gate     }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate     /*
2260Sstevel@tonic-gate     ** ...yes!...but can the caller handle the message?
2270Sstevel@tonic-gate     */
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate     if (size < real_size)
2300Sstevel@tonic-gate     {
2310Sstevel@tonic-gate 	errno = E2BIG;
2320Sstevel@tonic-gate 	return (-1);
2330Sstevel@tonic-gate     }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate     /*
2370Sstevel@tonic-gate     ** Yes!! We can finally copy the message into the caller's buffer
2380Sstevel@tonic-gate     ** and remove it from our buffer. That wasn't so bad, was it?
2390Sstevel@tonic-gate     */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate #if	defined(TRACE_MESSAGES)
2420Sstevel@tonic-gate     if (trace_messages)
2430Sstevel@tonic-gate 	syslog(LOG_DEBUG, "MESSAGE: %-.*s", real_size, fbp->psave);
2440Sstevel@tonic-gate #endif
2450Sstevel@tonic-gate     (void)memcpy (buf, fbp->psave, real_size);
2460Sstevel@tonic-gate     fbp->psave += real_size;
2470Sstevel@tonic-gate     return (real_size);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate int
peek3_2(fifo)2510Sstevel@tonic-gate peek3_2 (fifo)
2520Sstevel@tonic-gate int		fifo;
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate     register fifobuffer_t	*fbp;
2550Sstevel@tonic-gate     register unsigned int	real_size;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate     /*
2580Sstevel@tonic-gate     ** Make sure we start on a message boundary. The first
2590Sstevel@tonic-gate     ** line of defense is to look for the resync. bytes.
2600Sstevel@tonic-gate     **
2610Sstevel@tonic-gate     ** The "SyncUp" label is global to this routine (below this point)
2620Sstevel@tonic-gate     ** and is called whenever we determine that we're out
2630Sstevel@tonic-gate     ** of sync. with the incoming bytes.
2640Sstevel@tonic-gate     */
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate     if (!(fbp=GetFifoBuffer (fifo)))
2670Sstevel@tonic-gate 	return	-1;
2680Sstevel@tonic-gate     UNSYNCHED_READ (HEAD_RESYNC_LEN);
2690Sstevel@tonic-gate     while (*fbp->psave != Resync[0] || *(fbp->psave + 1) != Resync[1])
2700Sstevel@tonic-gate     {
2710Sstevel@tonic-gate SyncUp:
2720Sstevel@tonic-gate 	fbp->psave++;
2730Sstevel@tonic-gate 	Garbage_Bytes++;
2740Sstevel@tonic-gate 	UNSYNCHED_READ (HEAD_RESYNC_LEN);
2750Sstevel@tonic-gate     }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate     /*
2790Sstevel@tonic-gate     ** We're sync'd, so read the full header.
2800Sstevel@tonic-gate     */
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate     SYNCHED_READ (HEAD_LEN);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate     /*
2860Sstevel@tonic-gate     ** If the header size is smaller than the minimum size for a header,
2870Sstevel@tonic-gate     ** or larger than allowed, we must assume that we really aren't
2880Sstevel@tonic-gate     ** synchronized.
2890Sstevel@tonic-gate     */
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate     real_size = stoh(fbp->psave + HEAD_SIZE);
2920Sstevel@tonic-gate     if (real_size < CONTROL_LEN || MSGMAX < real_size)
2930Sstevel@tonic-gate     {
2940Sstevel@tonic-gate 	goto SyncUp;
2950Sstevel@tonic-gate     }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate     return(real_size);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate static int
_buffer(int fifo)301*320Sceastha _buffer(int fifo)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	     int	   n, nbytes, count = 0;
3040Sstevel@tonic-gate     register fifobuffer_t  *fbp;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate     /*
3070Sstevel@tonic-gate     ** As long as we get real messages, and if we chose
3080Sstevel@tonic-gate     ** SAVE_SIZE well, we shouldn't have to move the data
3090Sstevel@tonic-gate     ** in the "else" branch below: Each time we call "read"
3100Sstevel@tonic-gate     ** we aren't likely to get as many bytes as we ask for,
3110Sstevel@tonic-gate     ** just as many as are in the fifo, AND THIS SHOULD
3120Sstevel@tonic-gate     ** REPRESENT AN INTEGRAL NUMBER OF MESSAGES. Since
3130Sstevel@tonic-gate     ** the "read_fifo" routine reads complete messages,
3140Sstevel@tonic-gate     ** it will end its read at the end of the message,
3150Sstevel@tonic-gate     ** which (eventually) will make "psave_end" == "psave".
3160Sstevel@tonic-gate     */
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate     /*
3190Sstevel@tonic-gate     ** If the buffer is empty, there's nothing to move.
3200Sstevel@tonic-gate     */
3210Sstevel@tonic-gate     if (!(fbp = GetFifoBuffer (fifo)))
3220Sstevel@tonic-gate 	return	-1;
3230Sstevel@tonic-gate     if (fbp->psave_end == fbp->psave)
3240Sstevel@tonic-gate 	fbp->psave = fbp->psave_end = fbp->save;	/* sane pointers! */
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate     /*
3270Sstevel@tonic-gate     ** If the buffer has data at the high end, move it down.
3280Sstevel@tonic-gate     */
3290Sstevel@tonic-gate     else
3300Sstevel@tonic-gate     if (fbp->psave != fbp->save)		/* sane pointers! */
3310Sstevel@tonic-gate     {
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	** Move the data still left in the buffer to the
3340Sstevel@tonic-gate 	** front, so we can read as much as possible into
3350Sstevel@tonic-gate 	** buffer after it.
3360Sstevel@tonic-gate 	*/
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	memmove(fbp->save, fbp->psave, fbp->psave_end - fbp->psave);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	fbp->psave_end = fbp->save + (fbp->psave_end - fbp->psave);
3410Sstevel@tonic-gate 	fbp->psave = fbp->save;	/* sane	pointers! */
3420Sstevel@tonic-gate     }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate     /*
3450Sstevel@tonic-gate     ** The "fbp->psave" and "fbp->psave_end" pointers must be in a sane
3460Sstevel@tonic-gate     ** state when we get here, in case the "read()" gets interrupted.
3470Sstevel@tonic-gate     ** When that happens, we return to the caller who may try
3480Sstevel@tonic-gate     ** to restart us! Sane: fbp->psave == fbp->save (HERE!)
3490Sstevel@tonic-gate     */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate     nbytes = MSGMAX - (fbp->psave_end - fbp->save);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate     while ((n = read(fifo, fbp->psave_end, nbytes)) == 0 && count < 60)
3540Sstevel@tonic-gate     {
3550Sstevel@tonic-gate 	(void)	sleep ((unsigned) 1);
3560Sstevel@tonic-gate 	count++;
3570Sstevel@tonic-gate     }
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate     if (n > 0)
3600Sstevel@tonic-gate 	fbp->psave_end += n;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate     Had_Full_Buffer = fbp->full;
3630Sstevel@tonic-gate     fbp->full = (nbytes == n);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate     return (n);
3660Sstevel@tonic-gate }
367