chiark / gitweb /
@@ -5,6 +5,7 @@
[chiark-utils.git] / backup / 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 2,
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, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  */
29
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <assert.h>
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <sys/mman.h>
40
41 #include "rwbuffer.h"
42
43 #ifndef RWBUFFER_SIZE_MB_DEF
44 #define RWBUFFER_SIZE_MB_DEF 16
45 #endif
46
47 #ifndef RWBUFFER_SIZE_MB_MAX
48 #define RWBUFFER_SIZE_MB_MAX 512
49 #endif
50
51 unsigned char *buf, *wp, *rp;
52 int used, seeneof;
53 size_t buffersize;
54 fd_set readfds;
55 fd_set writefds;
56
57 int min(int a, int b) { return a<=b ? a : b; }
58
59 static void usage(FILE *f) {
60   if (fprintf(f,"usage: %s [--mlock] [<megabytes>]\n",progname) < 0)
61     { perror("print usage"); exit(16); }
62 }
63
64 static void usageerr(const char *what) {
65   fprintf(stderr,"%s: bad usage: %s\n",progname,what);
66   usage(stderr);
67   exit(12);
68 }
69
70 static void nonblock(int fd, int yesno) {
71   int r;
72   r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(8); }
73   if (yesno) r |= O_NDELAY;
74   else r &= ~O_NDELAY;
75   if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(8); }
76 }
77
78 static void unnonblock(void) {
79   nonblock(0,0); nonblock(1,0);
80 }
81
82 void startup(const char *const *argv) {
83   const char *arg;
84   char *ep;
85   unsigned long opt_buffersize=RWBUFFER_SIZE_MB_DEF;
86   int opt_mlock=0;
87   
88   assert(argv[0]);
89   
90   while ((arg= *++argv)) {
91     if (!strcmp(arg,"--mlock")) {
92       opt_mlock= 1;
93     } else if (isdigit((unsigned char)arg[0])) {
94       opt_buffersize= strtoul(arg,&ep,0);
95       if (opt_buffersize > RWBUFFER_SIZE_MB_MAX)
96         usageerr("buffer size too big");
97     } else {
98       usageerr("invalid option");
99     }
100   }
101
102   buffersize= opt_buffersize*1024*1024;
103   buf= malloc(buffersize);
104   if (!buf) { perror("malloc buffer"); exit(6); }
105
106   if (opt_mlock) {
107     if (mlock(buf,buffersize)) { perror("mlock"); exit(2); }
108   }
109
110   used=0; wp=rp=buf; seeneof=0;
111   if (atexit(unnonblock)) { perror("atexit"); exit(16); }
112   nonblock(0,1); nonblock(1,1);
113 }
114
115 void callselect(void) {
116   int r;
117   
118   for (;;) {
119     r= select(2,&readfds,&writefds,0,0);
120     if (r != -1) return;
121     if (errno != EINTR) {
122       perror("select"); exit(4);
123     }
124   }
125 }