17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*f928ce67Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 30*f928ce67Sceastha #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* LINTLIBRARY */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate # include <errno.h> 357c478bd9Sstevel@tonic-gate # include <string.h> 367c478bd9Sstevel@tonic-gate #include <syslog.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate # include "lp.h" 397c478bd9Sstevel@tonic-gate # include "msgs.h" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate extern char Resync[]; 427c478bd9Sstevel@tonic-gate extern char Endsync[]; 437c478bd9Sstevel@tonic-gate static int Had_Full_Buffer = 1; 447c478bd9Sstevel@tonic-gate int Garbage_Bytes = 0; 457c478bd9Sstevel@tonic-gate int Garbage_Messages= 0; 467c478bd9Sstevel@tonic-gate 47*f928ce67Sceastha static int _buffer(int); 48*f928ce67Sceastha 497c478bd9Sstevel@tonic-gate /* 507c478bd9Sstevel@tonic-gate ** A real message is written in one piece, and the write 517c478bd9Sstevel@tonic-gate ** is atomic. Thus, even if the O_NDELAY flag is set, 527c478bd9Sstevel@tonic-gate ** if we read part of the real message, we can continue 537c478bd9Sstevel@tonic-gate ** to read the rest of it in as many steps as we want 547c478bd9Sstevel@tonic-gate ** (up to the size of the message, of course!) without 557c478bd9Sstevel@tonic-gate ** UNIX returning 0 because no data is available. 567c478bd9Sstevel@tonic-gate ** So, a real message doesn't have to be read in one piece, 577c478bd9Sstevel@tonic-gate ** which is good since we don't know how much to read! 587c478bd9Sstevel@tonic-gate ** 597c478bd9Sstevel@tonic-gate ** Fake messages, or improperly written messages, don't 607c478bd9Sstevel@tonic-gate ** have this nice property. 617c478bd9Sstevel@tonic-gate ** 627c478bd9Sstevel@tonic-gate ** INTERRUPTED READS: 637c478bd9Sstevel@tonic-gate ** 647c478bd9Sstevel@tonic-gate ** If a signal occurs during an attempted read, we can exit. 657c478bd9Sstevel@tonic-gate ** The caller can retry the read and we will correctly restart 667c478bd9Sstevel@tonic-gate ** it. The correctness of this assertion can be seen by noticing 677c478bd9Sstevel@tonic-gate ** that at the beginning of each READ below, we can go back 687c478bd9Sstevel@tonic-gate ** to the first statement executed (the first READ below) 697c478bd9Sstevel@tonic-gate ** and correctly reexecute the code. 707c478bd9Sstevel@tonic-gate ** 717c478bd9Sstevel@tonic-gate ** If the last writer closed the fifo, we'll read 0 bytes 727c478bd9Sstevel@tonic-gate ** (at least on the subsequent read). If we were in the 737c478bd9Sstevel@tonic-gate ** middle of reading a message, we were reading a bogus 747c478bd9Sstevel@tonic-gate ** message (but see below). 757c478bd9Sstevel@tonic-gate ** 767c478bd9Sstevel@tonic-gate ** If we read less than we expect, it's because we were 777c478bd9Sstevel@tonic-gate ** reading a fake message (but see below). 787c478bd9Sstevel@tonic-gate ** 797c478bd9Sstevel@tonic-gate ** HOWEVER: In the last two cases, we may have ONE OR MORE 807c478bd9Sstevel@tonic-gate ** REAL MESSAGES snuggled in amongst the trash! 817c478bd9Sstevel@tonic-gate ** 827c478bd9Sstevel@tonic-gate ** All this verbal rambling is preface to let you understand why we 837c478bd9Sstevel@tonic-gate ** buffer the data (which is a shame, but necessary). 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate ** As long as we get real messages, we can avoid needless function calls. 887c478bd9Sstevel@tonic-gate ** The SYNC argument in this macro should be set if the resynch. bytes 897c478bd9Sstevel@tonic-gate ** have been read--i.e. if the rest of the message is trying to be read. 907c478bd9Sstevel@tonic-gate ** In this case, if we had not read a full buffer last time, then we 917c478bd9Sstevel@tonic-gate ** must be in the middle of a bogus message. 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate #define UNSYNCHED_READ(N) \ 957c478bd9Sstevel@tonic-gate if (fbp->psave_end - fbp->psave < N || fbp->psave >= fbp->psave_end) \ 967c478bd9Sstevel@tonic-gate { \ 977c478bd9Sstevel@tonic-gate switch (_buffer(fifo)) \ 987c478bd9Sstevel@tonic-gate { \ 997c478bd9Sstevel@tonic-gate case -1: \ 1007c478bd9Sstevel@tonic-gate return (-1); \ 1017c478bd9Sstevel@tonic-gate case 0: \ 1027c478bd9Sstevel@tonic-gate if (fbp->psave_end > fbp->psave) \ 1037c478bd9Sstevel@tonic-gate goto SyncUp; \ 1047c478bd9Sstevel@tonic-gate return (0); \ 1057c478bd9Sstevel@tonic-gate } \ 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #define SYNCHED_READ(N) \ 1097c478bd9Sstevel@tonic-gate if (fbp->psave_end - fbp->psave < N || fbp->psave >= fbp->psave_end) \ 1107c478bd9Sstevel@tonic-gate { \ 1117c478bd9Sstevel@tonic-gate switch (_buffer(fifo)) \ 1127c478bd9Sstevel@tonic-gate { \ 1137c478bd9Sstevel@tonic-gate case -1: \ 1147c478bd9Sstevel@tonic-gate return (-1); \ 1157c478bd9Sstevel@tonic-gate case 0: \ 1167c478bd9Sstevel@tonic-gate if (fbp->psave_end > fbp->psave) \ 1177c478bd9Sstevel@tonic-gate goto SyncUp; \ 1187c478bd9Sstevel@tonic-gate return (0); \ 1197c478bd9Sstevel@tonic-gate } \ 1207c478bd9Sstevel@tonic-gate if (!Had_Full_Buffer) \ 1217c478bd9Sstevel@tonic-gate goto SyncUp; \ 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate ** read_fifo() - READ A BUFFER WITH HEADER AND CHECKSUM 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate int 1287c478bd9Sstevel@tonic-gate read_fifo (fifo, buf, size) 1297c478bd9Sstevel@tonic-gate int fifo; 1307c478bd9Sstevel@tonic-gate char *buf; 1317c478bd9Sstevel@tonic-gate unsigned int size; 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate register fifobuffer_t *fbp; 1347c478bd9Sstevel@tonic-gate register unsigned int real_chksum, 1357c478bd9Sstevel@tonic-gate chksum, 1367c478bd9Sstevel@tonic-gate real_size; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* 1397c478bd9Sstevel@tonic-gate ** Make sure we start on a message boundary. The first 1407c478bd9Sstevel@tonic-gate ** line of defense is to look for the resync. bytes. 1417c478bd9Sstevel@tonic-gate ** 1427c478bd9Sstevel@tonic-gate ** The "SyncUp" label is global to this routine (below this point) 1437c478bd9Sstevel@tonic-gate ** and is called whenever we determine that we're out 1447c478bd9Sstevel@tonic-gate ** of sync. with the incoming bytes. 1457c478bd9Sstevel@tonic-gate */ 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (!(fbp=GetFifoBuffer (fifo))) 1487c478bd9Sstevel@tonic-gate return -1; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate UNSYNCHED_READ (HEAD_RESYNC_LEN); 1517c478bd9Sstevel@tonic-gate while (*fbp->psave != Resync[0] || *(fbp->psave + 1) != Resync[1]) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate SyncUp: 1547c478bd9Sstevel@tonic-gate #if defined(TRACE_MESSAGES) 1557c478bd9Sstevel@tonic-gate if (trace_messages) 1567c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "DISCARD %c\n", *fbp->psave); 1577c478bd9Sstevel@tonic-gate #endif 1587c478bd9Sstevel@tonic-gate fbp->psave++; 1597c478bd9Sstevel@tonic-gate Garbage_Bytes++; 1607c478bd9Sstevel@tonic-gate UNSYNCHED_READ (HEAD_RESYNC_LEN); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate ** We're sync'd, so read the full header. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate SYNCHED_READ (HEAD_LEN); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate ** If the header size is smaller than the minimum size for a header, 1737c478bd9Sstevel@tonic-gate ** or larger than allowed, we must assume that we really aren't 1747c478bd9Sstevel@tonic-gate ** synchronized. 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate real_size = stoh(fbp->psave + HEAD_SIZE); 1787c478bd9Sstevel@tonic-gate if (real_size < CONTROL_LEN || MSGMAX < real_size) 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate #if defined(TRACE_MESSAGES) 1817c478bd9Sstevel@tonic-gate if (trace_messages) 1827c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "BAD SIZE\n"); 1837c478bd9Sstevel@tonic-gate #endif 1847c478bd9Sstevel@tonic-gate goto SyncUp; 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* 1887c478bd9Sstevel@tonic-gate ** We have the header. Now we can finally read the rest of the 1897c478bd9Sstevel@tonic-gate ** message... 1907c478bd9Sstevel@tonic-gate */ 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate SYNCHED_READ (real_size); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* 1967c478bd9Sstevel@tonic-gate ** ...but did we read a real message?... 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if 2007c478bd9Sstevel@tonic-gate ( 2017c478bd9Sstevel@tonic-gate *(fbp->psave + TAIL_ENDSYNC(real_size)) != Endsync[0] 2027c478bd9Sstevel@tonic-gate || *(fbp->psave + TAIL_ENDSYNC(real_size) + 1) != Endsync[1] 2037c478bd9Sstevel@tonic-gate ) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate #if defined(TRACE_MESSAGES) 2067c478bd9Sstevel@tonic-gate if (trace_messages) 2077c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "BAD ENDSYNC\n"); 2087c478bd9Sstevel@tonic-gate #endif 2097c478bd9Sstevel@tonic-gate Garbage_Messages++; 2107c478bd9Sstevel@tonic-gate goto SyncUp; 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate chksum = stoh(fbp->psave + TAIL_CHKSUM(real_size)); 2147c478bd9Sstevel@tonic-gate CALC_CHKSUM (fbp->psave, real_size, real_chksum); 2157c478bd9Sstevel@tonic-gate if (real_chksum != chksum) 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate #if defined(TRACE_MESSAGES) 2187c478bd9Sstevel@tonic-gate if (trace_messages) 2197c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "BAD CHKSUM\n"); 2207c478bd9Sstevel@tonic-gate #endif 2217c478bd9Sstevel@tonic-gate Garbage_Messages++; 2227c478bd9Sstevel@tonic-gate goto SyncUp; 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate ** ...yes!...but can the caller handle the message? 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate if (size < real_size) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate errno = E2BIG; 2327c478bd9Sstevel@tonic-gate return (-1); 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate ** Yes!! We can finally copy the message into the caller's buffer 2387c478bd9Sstevel@tonic-gate ** and remove it from our buffer. That wasn't so bad, was it? 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate #if defined(TRACE_MESSAGES) 2427c478bd9Sstevel@tonic-gate if (trace_messages) 2437c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG, "MESSAGE: %-.*s", real_size, fbp->psave); 2447c478bd9Sstevel@tonic-gate #endif 2457c478bd9Sstevel@tonic-gate (void)memcpy (buf, fbp->psave, real_size); 2467c478bd9Sstevel@tonic-gate fbp->psave += real_size; 2477c478bd9Sstevel@tonic-gate return (real_size); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate int 2517c478bd9Sstevel@tonic-gate peek3_2 (fifo) 2527c478bd9Sstevel@tonic-gate int fifo; 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate register fifobuffer_t *fbp; 2557c478bd9Sstevel@tonic-gate register unsigned int real_size; 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* 2587c478bd9Sstevel@tonic-gate ** Make sure we start on a message boundary. The first 2597c478bd9Sstevel@tonic-gate ** line of defense is to look for the resync. bytes. 2607c478bd9Sstevel@tonic-gate ** 2617c478bd9Sstevel@tonic-gate ** The "SyncUp" label is global to this routine (below this point) 2627c478bd9Sstevel@tonic-gate ** and is called whenever we determine that we're out 2637c478bd9Sstevel@tonic-gate ** of sync. with the incoming bytes. 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if (!(fbp=GetFifoBuffer (fifo))) 2677c478bd9Sstevel@tonic-gate return -1; 2687c478bd9Sstevel@tonic-gate UNSYNCHED_READ (HEAD_RESYNC_LEN); 2697c478bd9Sstevel@tonic-gate while (*fbp->psave != Resync[0] || *(fbp->psave + 1) != Resync[1]) 2707c478bd9Sstevel@tonic-gate { 2717c478bd9Sstevel@tonic-gate SyncUp: 2727c478bd9Sstevel@tonic-gate fbp->psave++; 2737c478bd9Sstevel@tonic-gate Garbage_Bytes++; 2747c478bd9Sstevel@tonic-gate UNSYNCHED_READ (HEAD_RESYNC_LEN); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate /* 2797c478bd9Sstevel@tonic-gate ** We're sync'd, so read the full header. 2807c478bd9Sstevel@tonic-gate */ 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate SYNCHED_READ (HEAD_LEN); 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate ** If the header size is smaller than the minimum size for a header, 2877c478bd9Sstevel@tonic-gate ** or larger than allowed, we must assume that we really aren't 2887c478bd9Sstevel@tonic-gate ** synchronized. 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate real_size = stoh(fbp->psave + HEAD_SIZE); 2927c478bd9Sstevel@tonic-gate if (real_size < CONTROL_LEN || MSGMAX < real_size) 2937c478bd9Sstevel@tonic-gate { 2947c478bd9Sstevel@tonic-gate goto SyncUp; 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate return(real_size); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate static int 301*f928ce67Sceastha _buffer(int fifo) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate int n, nbytes, count = 0; 3047c478bd9Sstevel@tonic-gate register fifobuffer_t *fbp; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate ** As long as we get real messages, and if we chose 3087c478bd9Sstevel@tonic-gate ** SAVE_SIZE well, we shouldn't have to move the data 3097c478bd9Sstevel@tonic-gate ** in the "else" branch below: Each time we call "read" 3107c478bd9Sstevel@tonic-gate ** we aren't likely to get as many bytes as we ask for, 3117c478bd9Sstevel@tonic-gate ** just as many as are in the fifo, AND THIS SHOULD 3127c478bd9Sstevel@tonic-gate ** REPRESENT AN INTEGRAL NUMBER OF MESSAGES. Since 3137c478bd9Sstevel@tonic-gate ** the "read_fifo" routine reads complete messages, 3147c478bd9Sstevel@tonic-gate ** it will end its read at the end of the message, 3157c478bd9Sstevel@tonic-gate ** which (eventually) will make "psave_end" == "psave". 3167c478bd9Sstevel@tonic-gate */ 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate ** If the buffer is empty, there's nothing to move. 3207c478bd9Sstevel@tonic-gate */ 3217c478bd9Sstevel@tonic-gate if (!(fbp = GetFifoBuffer (fifo))) 3227c478bd9Sstevel@tonic-gate return -1; 3237c478bd9Sstevel@tonic-gate if (fbp->psave_end == fbp->psave) 3247c478bd9Sstevel@tonic-gate fbp->psave = fbp->psave_end = fbp->save; /* sane pointers! */ 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate /* 3277c478bd9Sstevel@tonic-gate ** If the buffer has data at the high end, move it down. 3287c478bd9Sstevel@tonic-gate */ 3297c478bd9Sstevel@tonic-gate else 3307c478bd9Sstevel@tonic-gate if (fbp->psave != fbp->save) /* sane pointers! */ 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate /* 3337c478bd9Sstevel@tonic-gate ** Move the data still left in the buffer to the 3347c478bd9Sstevel@tonic-gate ** front, so we can read as much as possible into 3357c478bd9Sstevel@tonic-gate ** buffer after it. 3367c478bd9Sstevel@tonic-gate */ 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate memmove(fbp->save, fbp->psave, fbp->psave_end - fbp->psave); 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate fbp->psave_end = fbp->save + (fbp->psave_end - fbp->psave); 3417c478bd9Sstevel@tonic-gate fbp->psave = fbp->save; /* sane pointers! */ 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate ** The "fbp->psave" and "fbp->psave_end" pointers must be in a sane 3467c478bd9Sstevel@tonic-gate ** state when we get here, in case the "read()" gets interrupted. 3477c478bd9Sstevel@tonic-gate ** When that happens, we return to the caller who may try 3487c478bd9Sstevel@tonic-gate ** to restart us! Sane: fbp->psave == fbp->save (HERE!) 3497c478bd9Sstevel@tonic-gate */ 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate nbytes = MSGMAX - (fbp->psave_end - fbp->save); 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate while ((n = read(fifo, fbp->psave_end, nbytes)) == 0 && count < 60) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate (void) sleep ((unsigned) 1); 3567c478bd9Sstevel@tonic-gate count++; 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate if (n > 0) 3607c478bd9Sstevel@tonic-gate fbp->psave_end += n; 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate Had_Full_Buffer = fbp->full; 3637c478bd9Sstevel@tonic-gate fbp->full = (nbytes == n); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate return (n); 3667c478bd9Sstevel@tonic-gate } 367