summaryrefslogtreecommitdiff
path: root/csvncgi/wrapper.c
blob: 60c184a35af29296834f49814e9641afbbb71c66 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <stdint.h>
#include <dirent.h>
#include <sys/stat.h> /* chmod(2)    */
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>   /* strdup(3)   */
#include <libgen.h>   /* basename(3) */
#include <ctype.h>    /* tolower(3)  */
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <poll.h>
#include <unistd.h>

#include <defs.h>
#include <wrapper.h>


#define WRAPPER_ERRMSG_SIZE 4096

void wrapper_error( const char *fmt, ... )
{
  va_list arg_ptr;
  char  buf[WRAPPER_ERRMSG_SIZE];
  char  msg[WRAPPER_ERRMSG_SIZE];
  char *format = "%s: %s\n";

  va_start( arg_ptr, fmt );

  vsnprintf( msg, WRAPPER_ERRMSG_SIZE, (const void *)fmt, arg_ptr );

  va_end( arg_ptr ); /* Reset variable arguments. */

  snprintf( buf, WRAPPER_ERRMSG_SIZE, format, "wrapper", msg );

  (void)write( STDERR_FILENO, buf, strlen( buf ) );

  exit( 1 );
}

wrapper_errfunc wrapper_fatal = wrapper_error;


char *xstrdup( const char *str )
{
  char *ret;

  ret = strdup( str );
  if( !ret )
    wrapper_fatal( "Out of memory, strdup failed" );
  return ret;
}

void *xmalloc( size_t size )
{
  void *ret;

  ret = malloc( size );
  if( !ret )
  {
    wrapper_fatal( "Out of memory, malloc failed (tried to allocate %lu bytes)", (unsigned long)size );
    return NULL;
  }
  memset( ret, 0, size );
  return ret;
}

void *xrealloc( void *ptr, size_t size )
{
  void *ret = NULL;

  ret = realloc( ptr, size );
  if( !ret && !size )
    ret = realloc( ptr, 1 );
  if( !ret )
    wrapper_fatal( "Out of memory, realloc failed" );
  return ret;
}

/************************************************************************
  Limit size of IO chunks, because huge chunks only cause pain.
  OS X 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
  the absence of bugs, large chunks can result in bad latencies when
  you decide to kill the process.

  We pick 8 MiB as our default, but if the platform defines SSIZE_MAX
  that is smaller than that, clip it to SSIZE_MAX, as a call to read(2)
  or write(2) larger than that is allowed to fail. As the last resort,
  we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value" to
  override this, if the definition of SSIZE_MAX given by the platform
  is broken.
 ************************************************************************/
#ifndef MAX_IO_SIZE
# define MAX_IO_SIZE_DEFAULT (8*1024*1024)
# if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
#  define MAX_IO_SIZE SSIZE_MAX
# else
#  define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
# endif
#endif

/*
  xopen() is the same as open(), but it die()s if the open() fails.
 */
int xopen( const char *path, int oflag, ... )
{
  mode_t mode = 0;
  va_list ap;

  /*
    va_arg() will have undefined behavior if the specified type is not
    compatible with the argument type. Since integers are promoted to
    ints, we fetch the next argument as an int, and then cast it to a
    mode_t to avoid undefined behavior.
   */
  va_start( ap, oflag );
  if( oflag & O_CREAT )
    mode = va_arg( ap, int );
  va_end( ap );

  for( ;; )
  {
    int fd = open( path, oflag, mode );
    if( fd >= 0 )
      return fd;
    if( errno == EINTR )
      continue;

    if( (oflag & O_RDWR) == O_RDWR )
      wrapper_fatal( "could not open '%s' for reading and writing", path );
    else if( (oflag & O_WRONLY) == O_WRONLY )
      wrapper_fatal( "could not open '%s' for writing", path );
    else
      wrapper_fatal( "could not open '%s' for reading", path );
  }
}

static int handle_nonblock( int fd, short poll_events, int err )
{
  struct pollfd pfd;

  if( err != EAGAIN && err != EWOULDBLOCK )
    return 0;

  pfd.fd = fd;
  pfd.events = poll_events;

  /*
    no need to check for errors, here;
    a subsequent read/write will detect unrecoverable errors
   */
  poll( &pfd, 1, -1 );
  return 1;
}

/*
  xread() is the same a read(), but it automatically restarts read()
  operations with a recoverable error (EAGAIN and EINTR). xread()
  DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
 */
ssize_t xread(int fd, void *buf, size_t len)
{
  ssize_t nr;

  if( len > MAX_IO_SIZE )
    len = MAX_IO_SIZE;

  while( 1 )
  {
    nr = read( fd, buf, len );
    if( nr < 0 )
    {
      if( errno == EINTR )
        continue;
      if( handle_nonblock(fd, POLLIN, errno) )
        continue;
    }
    return nr;
  }
}

/*
  xwrite() is the same a write(), but it automatically restarts write()
  operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
  GUARANTEE that "len" bytes is written even if the operation is successful.
 */
ssize_t xwrite( int fd, const void *buf, size_t len )
{
  ssize_t nr;

  if( len > MAX_IO_SIZE )
    len = MAX_IO_SIZE;

  while( 1 )
  {
    nr = write( fd, buf, len );
    if( nr < 0 )
    {
      if( errno == EINTR )
        continue;
      if( handle_nonblock(fd, POLLOUT, errno) )
        continue;
    }
    return nr;
  }
}

ssize_t read_in_full( int fd, void *buf, size_t count )
{
  char *p = buf;
  ssize_t total = 0;

  while( count > 0 )
  {
    ssize_t loaded = xread( fd, p, count );

    if (loaded < 0)
      return -1;

    if (loaded == 0)
      return total;

    count -= loaded;
        p += loaded;
    total += loaded;
  }

  return total;
}

ssize_t write_in_full( int fd, const void *buf, size_t count )
{
  const char *p = buf;
  ssize_t total = 0;

  while( count > 0 )
  {
    ssize_t written = xwrite( fd, p, count );
    if( written < 0 )
       return -1;

    if( !written )
    {
      errno = ENOSPC;
      return -1;
    }
    count -= written;
        p += written;
    total += written;
  }

  return total;
}

int xdup( int fd )
{
  int ret = dup( fd );
  if( ret < 0 )
    wrapper_fatal( "dup failed" );
  return ret;
}

/*
  xfopen() is the same as fopen(), but it die()s if the fopen() fails.
 */
FILE *xfopen( const char *path, const char *mode )
{
  for( ;;  )
  {
    FILE *fp = fopen( path, mode );
    if (fp)
      return fp;
    if (errno == EINTR)
      continue;

    if( *mode && mode[1] == '+' )
      wrapper_fatal( "could not open '%s' for reading and writing", path );
    else if( *mode == 'w' || *mode == 'a' )
      wrapper_fatal( "could not open '%s' for writing", path );
    else
      wrapper_fatal( "could not open '%s' for reading", path );
  }
}

FILE *xfdopen( int fd, const char *mode )
{
  FILE *stream = fdopen( fd, mode );
  if( stream == NULL )
    wrapper_fatal( "Out of memory? fdopen failed" );
  return stream;
}