busybox/libbb/read_printf.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Utility routines.
   4 *
   5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
   6 *
   7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   8 */
   9#include "libbb.h"
  10
  11
  12/* Suppose that you are a shell. You start child processes.
  13 * They work and eventually exit. You want to get user input.
  14 * You read stdin. But what happens if last child switched
  15 * its stdin into O_NONBLOCK mode?
  16 *
  17 * *** SURPRISE! It will affect the parent too! ***
  18 * *** BIG SURPRISE! It stays even after child exits! ***
  19 *
  20 * This is a design bug in UNIX API.
  21 *      fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
  22 * will set nonblocking mode not only on _your_ stdin, but
  23 * also on stdin of your parent, etc.
  24 *
  25 * In general,
  26 *      fd2 = dup(fd1);
  27 *      fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
  28 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
  29 * where duping is done implicitly by fork() etc.
  30 *
  31 * We need
  32 *      fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
  33 * (note SETFD, not SETFL!) but such thing doesn't exist.
  34 *
  35 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
  36 * require O_NONBLOCK dance at all. Actually, it exists:
  37 *      n = recv(fd, buf, len, MSG_DONTWAIT);
  38 *      "MSG_DONTWAIT:
  39 *      Enables non-blocking operation; if the operation
  40 *      would block, EAGAIN is returned."
  41 * but recv() works only for sockets!
  42 *
  43 * So far I don't see any good solution, I can only propose
  44 * that affected readers should be careful and use this routine,
  45 * which detects EAGAIN and uses poll() to wait on the fd.
  46 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
  47 */
  48ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count)
  49{
  50        struct pollfd pfd[1];
  51        ssize_t n;
  52
  53        while (1) {
  54                n = safe_read(fd, buf, count);
  55                if (n >= 0 || errno != EAGAIN)
  56                        return n;
  57                /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
  58                pfd[0].fd = fd;
  59                pfd[0].events = POLLIN;
  60                /* note: safe_poll pulls in printf */
  61                safe_poll(pfd, 1, -1);
  62        }
  63}
  64
  65// Reads one line a-la fgets (but doesn't save terminating '\n').
  66// Reads byte-by-byte. Useful when it is important to not read ahead.
  67// Bytes are appended to pfx (which must be malloced, or NULL).
  68char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
  69{
  70        char *p;
  71        char *buf = NULL;
  72        size_t sz = 0;
  73        size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  74
  75        goto jump_in;
  76
  77        while (sz < maxsz) {
  78                if ((size_t)(p - buf) == sz) {
  79 jump_in:
  80                        buf = xrealloc(buf, sz + 128);
  81                        p = buf + sz;
  82                        sz += 128;
  83                }
  84                if (nonblock_immune_read(fd, p, 1) != 1) {
  85                        /* EOF/error */
  86                        if (p == buf) { /* we read nothing */
  87                                free(buf);
  88                                return NULL;
  89                        }
  90                        break;
  91                }
  92                if (*p == '\n')
  93                        break;
  94                p++;
  95        }
  96        *p = '\0';
  97        if (maxsz_p)
  98                *maxsz_p  = p - buf;
  99        p++;
 100        return xrealloc(buf, p - buf);
 101}
 102
 103// Read (potentially big) files in one go. File size is estimated
 104// by stat. Extra '\0' byte is appended.
 105void* FAST_FUNC xmalloc_read_with_initial_buf(int fd, size_t *maxsz_p, char *buf, size_t total)
 106{
 107        size_t size, rd_size;
 108        size_t to_read;
 109        struct stat st;
 110
 111        to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
 112
 113        /* Estimate file size */
 114        st.st_size = 0; /* in case fstat fails, assume 0 */
 115        fstat(fd, &st);
 116        /* /proc/N/stat files report st_size 0 */
 117        /* In order to make such files readable, we add small const */
 118        size = (st.st_size | 0x3ff) + 1;
 119
 120        while (1) {
 121                if (to_read < size)
 122                        size = to_read;
 123                buf = xrealloc(buf, total + size + 1);
 124                rd_size = full_read(fd, buf + total, size);
 125                if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
 126                        free(buf);
 127                        return NULL;
 128                }
 129                total += rd_size;
 130                if (rd_size < size) /* EOF */
 131                        break;
 132                if (to_read <= rd_size)
 133                        break;
 134                to_read -= rd_size;
 135                /* grow by 1/8, but in [1k..64k] bounds */
 136                size = ((total / 8) | 0x3ff) + 1;
 137                if (size > 64*1024)
 138                        size = 64*1024;
 139        }
 140        buf = xrealloc(buf, total + 1);
 141        buf[total] = '\0';
 142
 143        if (maxsz_p)
 144                *maxsz_p = total;
 145        return buf;
 146}
 147
 148void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
 149{
 150        return xmalloc_read_with_initial_buf(fd, maxsz_p, NULL, 0);
 151}
 152
 153#ifdef USING_LSEEK_TO_GET_SIZE
 154/* Alternatively, file size can be obtained by lseek to the end.
 155 * The code is slightly bigger. Retained in case fstat approach
 156 * will not work for some weird cases (/proc, block devices, etc).
 157 * (NB: lseek also can fail to work for some weird files) */
 158
 159// Read (potentially big) files in one go. File size is estimated by
 160// lseek to end.
 161void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
 162{
 163        char *buf;
 164        size_t size;
 165        int fd;
 166        off_t len;
 167
 168        fd = open(filename, O_RDONLY);
 169        if (fd < 0)
 170                return NULL;
 171
 172        /* /proc/N/stat files report len 0 here */
 173        /* In order to make such files readable, we add small const */
 174        size = 0x3ff; /* read only 1k on unseekable files */
 175        len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
 176        if (len != (off_t)-1) {
 177                xlseek(fd, 0, SEEK_SET);
 178                size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
 179                if (len < size)
 180                        size = len;
 181        }
 182
 183        buf = xmalloc(size + 1);
 184        size = read_close(fd, buf, size);
 185        if ((ssize_t)size < 0) {
 186                free(buf);
 187                return NULL;
 188        }
 189        buf = xrealloc(buf, size + 1);
 190        buf[size] = '\0';
 191
 192        if (maxsz_p)
 193                *maxsz_p = size;
 194        return buf;
 195}
 196#endif
 197
 198// Read (potentially big) files in one go. File size is estimated
 199// by stat.
 200void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
 201{
 202        char *buf;
 203        int fd;
 204
 205        fd = open(filename, O_RDONLY);
 206        if (fd < 0)
 207                return NULL;
 208
 209        buf = xmalloc_read(fd, maxsz_p);
 210        close(fd);
 211        return buf;
 212}
 213
 214/* Die with an error message if we can't read the entire buffer. */
 215void FAST_FUNC xread(int fd, void *buf, size_t count)
 216{
 217        if (count) {
 218                ssize_t size = full_read(fd, buf, count);
 219                if ((size_t)size != count)
 220                        bb_error_msg_and_die("short read");
 221        }
 222}
 223
 224/* Die with an error message if we can't read one character. */
 225unsigned char FAST_FUNC xread_char(int fd)
 226{
 227        char tmp;
 228        xread(fd, &tmp, 1);
 229        return tmp;
 230}
 231
 232void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
 233{
 234        void *buf = xmalloc_open_read_close(filename, maxsz_p);
 235        if (!buf)
 236                bb_perror_msg_and_die("can't read '%s'", filename);
 237        return buf;
 238}
 239