3 * common definitions for readbuffer/writebuffer
5 * readbuffer and writebuffer are:
6 * Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
8 * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
9 * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
11 * Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
12 * Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
14 * This is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 3,
17 * or (at your option) any later version.
19 * This is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this file; if not, consult the Free Software
26 * Foundation's website at www.fsf.org, or the GNU Project website at
33 #ifndef RWBUFFER_SIZE_MB_DEF
34 #define RWBUFFER_SIZE_MB_DEF 16
37 #ifndef RWBUFFER_SIZE_MB_MAX
38 #define RWBUFFER_SIZE_MB_MAX 512
41 unsigned char *buf, *wp, *rp;
42 int used, seeneof, maxselfd;
43 size_t buffersize= RWBUFFER_SIZE_MB_DEF*1024*1024;
47 static int opt_mlock=0;
49 int min(int a, int b) { return a<=b ? a : b; }
51 static void usage(FILE *f) {
52 if (fprintf(f,"usage: %s [--mlock] [<megabytes>]\n",progname) < 0)
53 { perror("print usage"); exit(16); }
56 static void usageerr(const char *what) {
57 fprintf(stderr,"%s: bad usage: %s\n",progname,what);
62 void nonblock(int fd, int yesno) {
64 r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(8); }
65 if (yesno) r |= O_NDELAY;
67 if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(8); }
70 static void unnonblock(void) {
71 nonblock(0,0); nonblock(1,0);
74 void startupcore(void) {
75 buf= xmalloc(buffersize);
78 if (mlock(buf,buffersize)) { perror("mlock"); exit(2); }
81 used=0; wp=rp=buf; seeneof=0;
82 if (atexit(unnonblock)) { perror("atexit"); exit(16); }
85 void startup(const char *const *argv) {
92 while ((arg= *++argv)) {
93 if (!strcmp(arg,"--mlock")) {
95 } else if (isdigit((unsigned char)arg[0])) {
96 buffersize= strtoul(arg,&ep,0);
97 if (ep[0] && ep[1]) usageerr("buffer size spec. invalid");
99 case 0: case 'm': shift= 20; break;
100 case 'k': shift= 10; break;
101 case 'b': shift= 0; break;
102 default: usageerr("buffer size unit unknown");
104 if (buffersize > ((RWBUFFER_SIZE_MB_MAX << 20) >> shift))
105 usageerr("buffer size too big");
106 buffersize <<= shift;
108 usageerr("invalid option");
113 nonblock(0,1); nonblock(1,1);
116 void *xmalloc(size_t sz) {
117 void *r= malloc(sz); if (!r) { perror("malloc"); exit(6); }; return r;
120 void callselect(void) {
124 r= select(maxselfd,&readfds,&writefds,0,0);
126 if (errno != EINTR) {
127 perror("select"); exit(4);