blob: 4ad41d589fe79f79237cd7b5724cbc1be0d788bc [file] [log] [blame]
Denis Vlasenkoea620772006-10-14 02:23:43 +00001/* 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
12ssize_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 Vlasenkoe376d452008-02-20 22:23:24 +000023/* 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 Vlasenko081efd12008-02-20 22:57:24 +000057 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
Denis Vlasenkoe376d452008-02-20 22:23:24 +000058 */
59ssize_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 Vlasenkoea620772006-10-14 02:23:43 +000075/*
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 */
81ssize_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
91 if (cc < 0)
92 return cc; /* read() returns -1 on failure. */
Denis Vlasenkoea620772006-10-14 02:23:43 +000093 if (cc == 0)
94 break;
Denis Vlasenkoea620772006-10-14 02:23:43 +000095 buf = ((char *)buf) + cc;
96 total += cc;
97 len -= cc;
98 }
99
100 return total;
101}
102
103// Die with an error message if we can't read the entire buffer.
104void xread(int fd, void *buf, size_t count)
105{
106 if (count) {
107 ssize_t size = full_read(fd, buf, count);
108 if (size != count)
109 bb_error_msg_and_die("short read");
110 }
111}
112
113// Die with an error message if we can't read one character.
114unsigned char xread_char(int fd)
115{
116 char tmp;
Denis Vlasenkoea620772006-10-14 02:23:43 +0000117 xread(fd, &tmp, 1);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000118 return tmp;
119}
120
121// Read one line a-la fgets. Works only on seekable streams
122char *reads(int fd, char *buffer, size_t size)
123{
124 char *p;
125
126 if (size < 2)
127 return NULL;
128 size = full_read(fd, buffer, size-1);
129 if ((ssize_t)size <= 0)
130 return NULL;
131
132 buffer[size] = '\0';
133 p = strchr(buffer, '\n');
134 if (p) {
135 off_t offset;
136 *p++ = '\0';
Denis Vlasenkob141b9b2006-10-31 22:46:08 +0000137 // avoid incorrect (unsigned) widening
138 offset = (off_t)(p-buffer) - (off_t)size;
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +0000139 // set fd position right after '\n'
Denis Vlasenkoea620772006-10-14 02:23:43 +0000140 if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
141 return NULL;
142 }
143 return buffer;
144}
145
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000146// Read one line a-la fgets. Reads byte-by-byte.
147// Useful when it is important to not read ahead.
Denis Vlasenkoe376d452008-02-20 22:23:24 +0000148// Bytes are appended to pfx (which must be malloced, or NULL).
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000149char *xmalloc_reads(int fd, char *buf)
150{
151 char *p;
152 int sz = buf ? strlen(buf) : 0;
153
154 goto jump_in;
155 while (1) {
156 if (p - buf == sz) {
157 jump_in:
158 buf = xrealloc(buf, sz + 128);
159 p = buf + sz;
160 sz += 128;
161 }
Denis Vlasenkoe376d452008-02-20 22:23:24 +0000162 /* nonblock_safe_read() because we are used by e.g. shells */
163 if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000164 if (p == buf) { /* we read nothing */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000165 free(buf);
166 return NULL;
167 }
168 break;
169 }
170 if (*p == '\n')
171 break;
172 p++;
173 }
174 *p++ = '\0';
175 return xrealloc(buf, p - buf);
176}
177
Denis Vlasenkoea620772006-10-14 02:23:43 +0000178ssize_t read_close(int fd, void *buf, size_t size)
179{
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000180 /*int e;*/
Denis Vlasenkoea620772006-10-14 02:23:43 +0000181 size = full_read(fd, buf, size);
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000182 /*e = errno;*/
Denis Vlasenkoea620772006-10-14 02:23:43 +0000183 close(fd);
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000184 /*errno = e;*/
Denis Vlasenkoea620772006-10-14 02:23:43 +0000185 return size;
186}
187
188ssize_t open_read_close(const char *filename, void *buf, size_t size)
189{
190 int fd = open(filename, O_RDONLY);
191 if (fd < 0)
192 return fd;
193 return read_close(fd, buf, size);
194}
195
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000196// Read (potentially big) files in one go. File size is estimated by
197// lseek to end.
Denis Vlasenkoea620772006-10-14 02:23:43 +0000198void *xmalloc_open_read_close(const char *filename, size_t *sizep)
199{
200 char *buf;
201 size_t size = sizep ? *sizep : INT_MAX;
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000202 int fd;
203 off_t len;
204
205 fd = xopen(filename, O_RDONLY);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000206 /* /proc/N/stat files report len 0 here */
207 /* In order to make such files readable, we add small const */
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000208 len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
Denis Vlasenkoea620772006-10-14 02:23:43 +0000209 xlseek(fd, 0, SEEK_SET);
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000210 if (len < size)
211 size = len;
Denis Vlasenkob131b272006-12-17 17:30:01 +0000212 buf = xmalloc(size + 1);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000213 size = read_close(fd, buf, size);
214 if ((ssize_t)size < 0)
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000215 bb_perror_msg_and_die("'%s'", filename);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000216 xrealloc(buf, size + 1);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000217 buf[size] = '\0';
Denis Vlasenkod67cef22007-06-13 06:47:47 +0000218 if (sizep)
219 *sizep = size;
Denis Vlasenkoea620772006-10-14 02:23:43 +0000220 return buf;
221}