1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30/* Simple minded read-ahead/write-behind subroutines for tftp user and 31 server. Written originally with multiple buffers in mind, but current 32 implementation has two buffer logic wired in. 33 34 Todo: add some sort of final error check so when the write-buffer 35 is finally flushed, the caller can detect if the disk filled up 36 (or had an i/o error) and return a nak to the other side. 37 38 Jim Guyton 10/85 39 */ 40 41#ifdef HAVE_CONFIG_H 42#include <config.h> 43#endif 44 45#include <sys/types.h> 46#include <sys/socket.h> 47#include <sys/ioctl.h> 48#ifdef HAVE_SYS_FILIO_H 49#include <sys/filio.h> 50#endif 51#include <netinet/in.h> 52#include "kdump.h" 53 54#include <stdio.h> 55#include <unistd.h> 56#include <syslog.h> 57 58#include "kdumpsubs.h" 59 60#define PKTSIZE SEGSIZE+6 /* should be moved to kdump.h */ 61 62struct bf { 63 int counter; /* size of data in buffer, or flag */ 64 char buf[MAXIMUM_KDP_PKTSIZE]; /* room for data packet */ 65} bfs[2]; 66 67 /* Values for bf.counter */ 68#define BF_ALLOC -3 /* alloc'd but not yet filled */ 69#define BF_FREE -2 /* free */ 70/* [-1 .. SEGSIZE] = size of data in the data buffer */ 71 72static int nextone; /* index of next buffer to use */ 73static int current; /* index of buffer in use */ 74 75 /* control flags for crlf conversions */ 76int newline = 0; /* fillbuf: in middle of newline expansion */ 77int prevchar = -1; /* putbuf: previous char (cr check) */ 78 79static struct kdumphdr *rw_init __P ((int)); 80 81struct kdumphdr *w_init() { return rw_init(0); } /* write-behind */ 82struct kdumphdr *r_init() { return rw_init(1); } /* read-ahead */ 83 84extern uint32_t kdp_crashdump_pkt_size; 85extern uint32_t kdp_crashdump_seg_size; 86 87/* init for either read-ahead or write-behind */ 88/* zero for write-behind, one for read-head */ 89static struct kdumphdr * 90rw_init(int x) 91{ 92 newline = 0; /* init crlf flag */ 93 prevchar = -1; 94 bfs[0].counter = BF_ALLOC; /* pass out the first buffer */ 95 current = 0; 96 bfs[1].counter = BF_FREE; 97 nextone = x; /* ahead or behind? */ 98 return (struct kdumphdr *)bfs[0].buf; 99} 100 101 102/* Have emptied current buffer by sending to net and getting ack. 103 Free it and return next buffer filled with data. 104 */ 105/* if true, convert to ascii */ 106/* file opened for read */ 107 108/* int */ 109/* readit(FILE *file, struct kdumphdr **dpp, int convert) */ 110/* { */ 111/* struct bf *b; */ 112 113/* bfs[current].counter = BF_FREE; /\* free old one *\/ */ 114/* current = !current; /\* "incr" current *\/ */ 115 116/* b = &bfs[current]; /\* look at new buffer *\/ */ 117/* if (b->counter == BF_FREE) /\* if it's empty *\/ */ 118/* read_ahead(file, convert); /\* fill it *\/ */ 119/* /\* assert(b->counter != BF_FREE);*\//\* check *\/ */ 120/* *dpp = (struct kdumphdr *)b->buf; /\* set caller's ptr *\/ */ 121/* return b->counter; */ 122/* } */ 123 124/* 125 * fill the input buffer, doing ascii conversions if requested 126 * conversions are lf -> cr,lf and cr -> cr, nul 127 */ 128/* FILE *file; file opened for read */ 129/* int convert; if true, convert to ascii */ 130void 131read_ahead(FILE *file, int convert) 132{ 133 register int i; 134 register char *p; 135 register int c; 136 struct bf *b; 137 struct kdumphdr *dp; 138 139 b = &bfs[nextone]; /* look at "next" buffer */ 140 if (b->counter != BF_FREE) /* nop if not free */ 141 return; 142 nextone = !nextone; /* "incr" next buffer ptr */ 143 144 dp = (struct kdumphdr *)b->buf; 145 146 if (convert == 0) { 147 b->counter = read(fileno(file), dp->th_data, kdp_crashdump_seg_size); 148 return; 149 } 150 151 p = dp->th_data; 152 for (i = 0 ; i < kdp_crashdump_seg_size; i++) { 153 if (newline) { 154 if (prevchar == '\n') 155 c = '\n'; /* lf to cr,lf */ 156 else c = '\0'; /* cr to cr,nul */ 157 newline = 0; 158 } 159 else { 160 c = getc(file); 161 if (c == EOF) break; 162 if (c == '\n' || c == '\r') { 163 prevchar = c; 164 c = '\r'; 165 newline = 1; 166 } 167 } 168 *p++ = c; 169 } 170 b->counter = (int)(p - dp->th_data); 171} 172 173/* Update count associated with the buffer, get new buffer 174 from the queue. Calls write_behind only if next buffer not 175 available. 176 */ 177int 178writeit(FILE *file, struct kdumphdr **dpp, int ct, int convert) 179{ 180 bfs[current].counter = ct; /* set size of data to write */ 181 current = !current; /* switch to other buffer */ 182 if (bfs[current].counter != BF_FREE) /* if not free */ 183 (void)write_behind(file, convert); /* flush it */ 184 bfs[current].counter = BF_ALLOC; /* mark as alloc'd */ 185 *dpp = (struct kdumphdr *)bfs[current].buf; 186 return ct; /* this is a lie of course */ 187} 188 189 190/* 191 * Output a buffer to a file, converting from netascii if requested. 192 * CR,NUL -> CR and CR,LF => LF. 193 * Note spec is undefined if we get CR as last byte of file or a 194 * CR followed by anything else. In this case we leave it alone. 195 */ 196int 197write_behind(FILE *file, int convert) 198{ 199 char *buf; 200 int count; 201 register int ct; 202 register char *p; 203 register int c; /* current character */ 204 struct bf *b; 205 struct kdumphdr *dp; 206 207 b = &bfs[nextone]; 208 if (b->counter < -1) /* anything to flush? */ 209 return 0; /* just nop if nothing to do */ 210 211 count = b->counter; /* remember byte count */ 212 b->counter = BF_FREE; /* reset flag */ 213 dp = (struct kdumphdr *)b->buf; 214 nextone = !nextone; /* incr for next time */ 215 buf = dp->th_data; 216 217 if (count <= 0) return -1; /* nak logic? */ 218 219 if (convert == 0) 220 return write(fileno(file), buf, count); 221 222 p = buf; 223 ct = count; 224 while (ct--) { /* loop over the buffer */ 225 c = *p++; /* pick up a character */ 226 if (prevchar == '\r') { /* if prev char was cr */ 227 if (c == '\n') /* if have cr,lf then just */ 228 fseek(file, -1, 1); /* smash lf on top of the cr */ 229 else 230 if (c == '\0') /* if have cr,nul then */ 231 goto skipit; /* just skip over the putc */ 232 /* else just fall through and allow it */ 233 } 234 putc(c, file); 235skipit: 236 prevchar = c; 237 } 238 return count; 239} 240 241 242/* When an error has occurred, it is possible that the two sides 243 * are out of synch. Ie: that what I think is the other side's 244 * response to packet N is really their response to packet N-1. 245 * 246 * So, to try to prevent that, we flush all the input queued up 247 * for us on the network connection on our host. 248 * 249 * We return the number of packets we flushed (mostly for reporting 250 * when trace is active). 251 */ 252 253/*int f;socket to flush */ 254int 255synchnet(int f) 256{ 257 int i, j = 0; 258 char rbuf[kdp_crashdump_pkt_size]; 259 struct sockaddr_in from; 260 socklen_t fromlen; 261 262 while (1) { 263 (void) ioctl(f, FIONREAD, &i); 264 if (i) { 265 j++; 266 fromlen = sizeof from; 267 (void) recvfrom(f, rbuf, sizeof (rbuf), 0, 268 (struct sockaddr *)&from, &fromlen); 269 } else { 270 return(j); 271 } 272 } 273} 274