File: rwbuffer.c

package info (click to toggle)
chiark-utils 4.0.0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 784 kB
  • ctags: 262
  • sloc: perl: 2,568; ansic: 864; makefile: 339; sh: 111
file content (122 lines) | stat: -rw-r--r-- 3,186 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * rwbuffer.c
 * common definitions for readbuffer/writebuffer
 *
 * readbuffer and writebuffer are:
 *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
 *
 * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
 * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
 * chiark backup is:
 *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
 *  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this file; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "rwbuffer.h"

#ifndef RWBUFFER_SIZE_MB_DEF
#define RWBUFFER_SIZE_MB_DEF 16
#endif

#ifndef RWBUFFER_SIZE_MB_MAX
#define RWBUFFER_SIZE_MB_MAX 512
#endif

unsigned char *buf, *wp, *rp;
int used, seeneof, maxselfd;
size_t buffersize;
fd_set readfds;
fd_set writefds;

static int opt_mlock=0;

int min(int a, int b) { return a<=b ? a : b; }

static void usage(FILE *f) {
  if (fprintf(f,"usage: %s [--mlock] [<megabytes>]\n",progname) < 0)
    { perror("print usage"); exit(16); }
}

static void usageerr(const char *what) {
  fprintf(stderr,"%s: bad usage: %s\n",progname,what);
  usage(stderr);
  exit(12);
}

void nonblock(int fd, int yesno) {
  int r;
  r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(8); }
  if (yesno) r |= O_NDELAY;
  else r &= ~O_NDELAY;
  if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(8); }
}

static void unnonblock(void) {
  nonblock(0,0); nonblock(1,0);
}

void startupcore(void) {
  buf= xmalloc(buffersize);

  if (opt_mlock) {
    if (mlock(buf,buffersize)) { perror("mlock"); exit(2); }
  }

  used=0; wp=rp=buf; seeneof=0;
  if (atexit(unnonblock)) { perror("atexit"); exit(16); }
}

void startup(const char *const *argv) {
  const char *arg;
  char *ep;
  unsigned long opt_buffersize=RWBUFFER_SIZE_MB_DEF;
  
  assert(argv[0]);
  
  while ((arg= *++argv)) {
    if (!strcmp(arg,"--mlock")) {
      opt_mlock= 1;
    } else if (isdigit((unsigned char)arg[0])) {
      opt_buffersize= strtoul(arg,&ep,0);
      if (opt_buffersize > RWBUFFER_SIZE_MB_MAX)
	usageerr("buffer size too big");
    } else {
      usageerr("invalid option");
    }
  }

  buffersize= opt_buffersize*1024*1024;
  startupcore();
  nonblock(0,1); nonblock(1,1);
}

void *xmalloc(size_t sz) {
  void *r= malloc(sz); if (!r) { perror("malloc"); exit(6); }; return r;
}

void callselect(void) {
  int r;
  
  for (;;) {
    r= select(maxselfd,&readfds,&writefds,0,0);
    if (r != -1) return;
    if (errno != EINTR) {
      perror("select"); exit(4);
    }
  }
}