Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 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 tarball for details. |
| 8 | */ |
| 9 | |
| 10 | #include "libbb.h" |
| 11 | |
| 12 | ssize_t safe_read(int fd, void *buf, size_t count) |
| 13 | { |
| 14 | ssize_t n; |
| 15 | |
| 16 | do { |
| 17 | n = read(fd, buf, count); |
| 18 | } while (n < 0 && errno == EINTR); |
| 19 | |
| 20 | return n; |
| 21 | } |
| 22 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 23 | /* Suppose that you are a shell. You start child processes. |
| 24 | * They work and eventually exit. You want to get user input. |
| 25 | * You read stdin. But what happens if last child switched |
| 26 | * its stdin into O_NONBLOCK mode? |
| 27 | * |
| 28 | * *** SURPRISE! It will affect the parent too! *** |
| 29 | * *** BIG SURPRISE! It stays even after child exits! *** |
| 30 | * |
| 31 | * This is a design bug in UNIX API. |
| 32 | * fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK); |
| 33 | * will set nonblocking mode not only on _your_ stdin, but |
| 34 | * also on stdin of your parent, etc. |
| 35 | * |
| 36 | * In general, |
| 37 | * fd2 = dup(fd1); |
| 38 | * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL, 0) | O_NONBLOCK); |
| 39 | * sets both fd1 and fd2 to O_NONBLOCK. This includes cases |
| 40 | * where duping is done implicitly by fork() etc. |
| 41 | * |
| 42 | * We need |
| 43 | * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD, 0) | O_NONBLOCK); |
| 44 | * (note SETFD, not SETFL!) but such thing doesn't exist. |
| 45 | * |
| 46 | * Alternatively, we need nonblocking_read(fd, ...) which doesn't |
| 47 | * require O_NONBLOCK dance at all. Actually, it exists: |
| 48 | * n = recv(fd, buf, len, MSG_DONTWAIT); |
| 49 | * "MSG_DONTWAIT: |
| 50 | * Enables non-blocking operation; if the operation |
| 51 | * would block, EAGAIN is returned." |
| 52 | * but recv() works only for sockets! |
| 53 | * |
| 54 | * So far I don't see any good solution, I can only propose |
| 55 | * that affected readers should be careful and use this routine, |
| 56 | * which detects EAGAIN and uses poll() to wait on the fd. |
Denis Vlasenko | 081efd1 | 2008-02-20 22:57:24 +0000 | [diff] [blame] | 57 | * Thankfully, poll() doesn't care about O_NONBLOCK flag. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 58 | */ |
| 59 | ssize_t nonblock_safe_read(int fd, void *buf, size_t count) |
| 60 | { |
| 61 | struct pollfd pfd[1]; |
| 62 | ssize_t n; |
| 63 | |
| 64 | while (1) { |
| 65 | n = safe_read(fd, buf, count); |
| 66 | if (n >= 0 || errno != EAGAIN) |
| 67 | return n; |
| 68 | /* fd is in O_NONBLOCK mode. Wait using poll and repeat */ |
| 69 | pfd[0].fd = fd; |
| 70 | pfd[0].events = POLLIN; |
| 71 | safe_poll(pfd, 1, -1); |
| 72 | } |
| 73 | } |
| 74 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 75 | /* |
| 76 | * Read all of the supplied buffer from a file. |
| 77 | * This does multiple reads as necessary. |
| 78 | * Returns the amount read, or -1 on an error. |
| 79 | * A short read is returned on an end of file. |
| 80 | */ |
| 81 | ssize_t full_read(int fd, void *buf, size_t len) |
| 82 | { |
| 83 | ssize_t cc; |
| 84 | ssize_t total; |
| 85 | |
| 86 | total = 0; |
| 87 | |
| 88 | while (len) { |
| 89 | cc = safe_read(fd, buf, len); |
| 90 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 91 | if (cc < 0) { |
| 92 | if (total) { |
| 93 | /* we already have some! */ |
| 94 | /* user can do another read to know the error code */ |
| 95 | return total; |
| 96 | } |
| 97 | return cc; /* read() returns -1 on failure. */ |
| 98 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 99 | if (cc == 0) |
| 100 | break; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 101 | buf = ((char *)buf) + cc; |
| 102 | total += cc; |
| 103 | len -= cc; |
| 104 | } |
| 105 | |
| 106 | return total; |
| 107 | } |
| 108 | |
| 109 | // Die with an error message if we can't read the entire buffer. |
| 110 | void xread(int fd, void *buf, size_t count) |
| 111 | { |
| 112 | if (count) { |
| 113 | ssize_t size = full_read(fd, buf, count); |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 114 | if ((size_t)size != count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 115 | bb_error_msg_and_die("short read"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Die with an error message if we can't read one character. |
| 120 | unsigned char xread_char(int fd) |
| 121 | { |
| 122 | char tmp; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 123 | xread(fd, &tmp, 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 124 | return tmp; |
| 125 | } |
| 126 | |
| 127 | // Read one line a-la fgets. Works only on seekable streams |
| 128 | char *reads(int fd, char *buffer, size_t size) |
| 129 | { |
| 130 | char *p; |
| 131 | |
| 132 | if (size < 2) |
| 133 | return NULL; |
| 134 | size = full_read(fd, buffer, size-1); |
| 135 | if ((ssize_t)size <= 0) |
| 136 | return NULL; |
| 137 | |
| 138 | buffer[size] = '\0'; |
| 139 | p = strchr(buffer, '\n'); |
| 140 | if (p) { |
| 141 | off_t offset; |
| 142 | *p++ = '\0'; |
Denis Vlasenko | b141b9b | 2006-10-31 22:46:08 +0000 | [diff] [blame] | 143 | // avoid incorrect (unsigned) widening |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 144 | offset = (off_t)(p - buffer) - (off_t)size; |
Denis Vlasenko | 3a34d0c | 2007-01-12 22:10:34 +0000 | [diff] [blame] | 145 | // set fd position right after '\n' |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 146 | if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) |
| 147 | return NULL; |
| 148 | } |
| 149 | return buffer; |
| 150 | } |
| 151 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 152 | // Reads one line a-la fgets (but doesn't save terminating '\n'). |
| 153 | // Reads byte-by-byte. Useful when it is important to not read ahead. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 154 | // Bytes are appended to pfx (which must be malloced, or NULL). |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 155 | char *xmalloc_reads(int fd, char *buf, size_t *maxsz_p) |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 156 | { |
| 157 | char *p; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 158 | size_t sz = buf ? strlen(buf) : 0; |
| 159 | size_t maxsz = maxsz_p ? *maxsz_p : MAXINT(size_t); |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 160 | |
| 161 | goto jump_in; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 162 | while (sz < maxsz) { |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 163 | if ((size_t)(p - buf) == sz) { |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 164 | jump_in: |
| 165 | buf = xrealloc(buf, sz + 128); |
| 166 | p = buf + sz; |
| 167 | sz += 128; |
| 168 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 169 | /* nonblock_safe_read() because we are used by e.g. shells */ |
| 170 | if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */ |
Denis Vlasenko | f99afb5 | 2008-02-24 23:32:36 +0000 | [diff] [blame] | 171 | if (p == buf) { /* we read nothing */ |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 172 | free(buf); |
| 173 | return NULL; |
| 174 | } |
| 175 | break; |
| 176 | } |
| 177 | if (*p == '\n') |
| 178 | break; |
| 179 | p++; |
| 180 | } |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 181 | *p = '\0'; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 182 | if (maxsz_p) |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 183 | *maxsz_p = p - buf; |
| 184 | p++; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 185 | return xrealloc(buf, p - buf); |
| 186 | } |
| 187 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 188 | ssize_t read_close(int fd, void *buf, size_t size) |
| 189 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 190 | /*int e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 191 | size = full_read(fd, buf, size); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 192 | /*e = errno;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 193 | close(fd); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 194 | /*errno = e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 195 | return size; |
| 196 | } |
| 197 | |
| 198 | ssize_t open_read_close(const char *filename, void *buf, size_t size) |
| 199 | { |
| 200 | int fd = open(filename, O_RDONLY); |
| 201 | if (fd < 0) |
| 202 | return fd; |
| 203 | return read_close(fd, buf, size); |
| 204 | } |
| 205 | |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 206 | // Read (potentially big) files in one go. File size is estimated by |
| 207 | // lseek to end. |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 208 | void *xmalloc_open_read_close(const char *filename, size_t *sizep) |
| 209 | { |
| 210 | char *buf; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 211 | size_t size; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 212 | int fd; |
| 213 | off_t len; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 214 | struct stat st; |
| 215 | |
| 216 | fd = open(filename, O_RDONLY); |
| 217 | if (fd < 0) |
| 218 | return NULL; |
| 219 | |
| 220 | st.st_size = 0; /* in case fstat fail, define to 0 */ |
| 221 | fstat(fd, &st); |
| 222 | /* /proc/N/stat files report len 0 here */ |
| 223 | /* In order to make such files readable, we add small const */ |
| 224 | len = st.st_size | 0x3ff; /* read only 1k on unseekable files */ |
| 225 | size = sizep ? *sizep : INT_MAX; |
| 226 | if (len < size) |
| 227 | size = len; |
| 228 | buf = xmalloc(size + 1); |
| 229 | size = read_close(fd, buf, size); |
| 230 | if ((ssize_t)size < 0) { |
| 231 | free(buf); |
| 232 | return NULL; |
| 233 | } |
| 234 | xrealloc(buf, size + 1); |
| 235 | buf[size] = '\0'; |
| 236 | |
| 237 | if (sizep) |
| 238 | *sizep = size; |
| 239 | return buf; |
| 240 | } |
| 241 | |
| 242 | #ifdef USING_LSEEK_TO_GET_SIZE |
| 243 | /* Alternatively, file size can be obtained by lseek to the end. |
| 244 | * The code is slightly bigger. Retained in case fstat approach |
| 245 | * will not work for some weird cases (/proc, block devices, etc). |
| 246 | * (NB: lseek also can fail to work for some weird files) */ |
| 247 | |
| 248 | // Read (potentially big) files in one go. File size is estimated by |
| 249 | // lseek to end. |
| 250 | void *xmalloc_open_read_close(const char *filename, size_t *sizep) |
| 251 | { |
| 252 | char *buf; |
| 253 | size_t size; |
| 254 | int fd; |
| 255 | off_t len; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 257 | fd = open(filename, O_RDONLY); |
| 258 | if (fd < 0) |
| 259 | return NULL; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 260 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 261 | /* /proc/N/stat files report len 0 here */ |
| 262 | /* In order to make such files readable, we add small const */ |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 263 | size = 0x3ff; /* read only 1k on unseekable files */ |
| 264 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
| 265 | if (len != (off_t)-1) { |
| 266 | xlseek(fd, 0, SEEK_SET); |
| 267 | size = sizep ? *sizep : INT_MAX; |
| 268 | if (len < size) |
| 269 | size = len; |
| 270 | } |
| 271 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 272 | buf = xmalloc(size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 273 | size = read_close(fd, buf, size); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 274 | if ((ssize_t)size < 0) { |
| 275 | free(buf); |
| 276 | return NULL; |
| 277 | } |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 278 | xrealloc(buf, size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 279 | buf[size] = '\0'; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 280 | |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 281 | if (sizep) |
| 282 | *sizep = size; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 283 | return buf; |
| 284 | } |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 285 | #endif |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 286 | |
| 287 | void *xmalloc_xopen_read_close(const char *filename, size_t *sizep) |
| 288 | { |
| 289 | void *buf = xmalloc_open_read_close(filename, sizep); |
| 290 | if (!buf) |
| 291 | bb_perror_msg_and_die("can't read '%s'", filename); |
| 292 | return buf; |
| 293 | } |