chiark / gitweb /
finalise changelog
[chiark-utils.git] / cprogs / rwbuffer.c
1 /*
2  * rwbuffer.c
3  * common definitions for readbuffer/writebuffer
4  *
5  * readbuffer and writebuffer are:
6  *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
7  *
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.
10  * chiark backup is:
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>
13  *
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.
18  *
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.
23  *
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
27  * www.gnu.org.
28  *
29  */
30
31 #include "rwbuffer.h"
32
33 #ifndef RWBUFFER_SIZE_MB_DEF
34 #define RWBUFFER_SIZE_MB_DEF 16
35 #endif
36
37 #ifndef RWBUFFER_SIZE_MB_MAX
38 #define RWBUFFER_SIZE_MB_MAX 512
39 #endif
40
41 unsigned char *buf, *wp, *rp;
42 int used, seeneof, maxselfd;
43 size_t buffersize= RWBUFFER_SIZE_MB_DEF*1024*1024;
44 fd_set readfds;
45 fd_set writefds;
46
47 static int opt_mlock=0;
48
49 int min(int a, int b) { return a<=b ? a : b; }
50
51 static void usage(FILE *f) {
52   if (fprintf(f,"usage: %s [--mlock] [<megabytes>]\n",progname) < 0)
53     { perror("print usage"); exit(16); }
54 }
55
56 static void usageerr(const char *what) {
57   fprintf(stderr,"%s: bad usage: %s\n",progname,what);
58   usage(stderr);
59   exit(12);
60 }
61
62 void nonblock(int fd, int yesno) {
63   int r;
64   r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(8); }
65   if (yesno) r |= O_NDELAY;
66   else r &= ~O_NDELAY;
67   if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(8); }
68 }
69
70 static void unnonblock(void) {
71   nonblock(0,0); nonblock(1,0);
72 }
73
74 void startupcore(void) {
75   buf= xmalloc(buffersize);
76
77   if (opt_mlock) {
78     if (mlock(buf,buffersize)) { perror("mlock"); exit(2); }
79   }
80
81   used=0; wp=rp=buf; seeneof=0;
82   if (atexit(unnonblock)) { perror("atexit"); exit(16); }
83 }
84
85 void startup(const char *const *argv) {
86   const char *arg;
87   char *ep;
88   int shift=-1;
89   
90   assert(argv[0]);
91   
92   while ((arg= *++argv)) {
93     if (!strcmp(arg,"--mlock")) {
94       opt_mlock= 1;
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");
98       switch (ep[0]) {
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");
103       }
104       if (buffersize > ((RWBUFFER_SIZE_MB_MAX << 20) >> shift))
105         usageerr("buffer size too big");
106       buffersize <<= shift;
107     } else {
108       usageerr("invalid option");
109     }
110   }
111
112   startupcore();
113   nonblock(0,1); nonblock(1,1);
114 }
115
116 void *xmalloc(size_t sz) {
117   void *r= malloc(sz); if (!r) { perror("malloc"); exit(6); }; return r;
118 }
119
120 void callselect(void) {
121   int r;
122   
123   for (;;) {
124     r= select(maxselfd,&readfds,&writefds,0,0);
125     if (r != -1) return;
126     if (errno != EINTR) {
127       perror("select"); exit(4);
128     }
129   }
130 }