diff options
author | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-07-16 08:14:35 +0000 |
---|---|---|
committer | landley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-07-16 08:14:35 +0000 |
commit | a15cdc358e5f9d1cb517e2b2e490ac209b0dfa17 (patch) | |
tree | fac906b4fa40a68c53cecf20215a7a25b3b1cab6 /libbb | |
parent | a4b7afd4143063613cf88fb5304803e424f5c72a (diff) | |
download | busybox-w32-a15cdc358e5f9d1cb517e2b2e490ac209b0dfa17.tar.gz busybox-w32-a15cdc358e5f9d1cb517e2b2e490ac209b0dfa17.tar.bz2 busybox-w32-a15cdc358e5f9d1cb517e2b2e490ac209b0dfa17.zip |
Cleaup read() and write() variants, plus a couple of new functions like
xlseek and fdlength() for the new mkswap.
git-svn-id: svn://busybox.net/trunk/busybox@15703 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/change_identity.c | 6 | ||||
-rw-r--r-- | libbb/copyfd.c | 16 | ||||
-rw-r--r-- | libbb/create_icmp6_socket.c | 2 | ||||
-rw-r--r-- | libbb/create_icmp_socket.c | 2 | ||||
-rw-r--r-- | libbb/full_read.c | 2 | ||||
-rw-r--r-- | libbb/full_write.c | 2 | ||||
-rw-r--r-- | libbb/loop.c | 1 | ||||
-rw-r--r-- | libbb/xfuncs.c | 85 |
8 files changed, 84 insertions, 32 deletions
diff --git a/libbb/change_identity.c b/libbb/change_identity.c index adebad8ed..74ffccbd3 100644 --- a/libbb/change_identity.c +++ b/libbb/change_identity.c | |||
@@ -46,10 +46,8 @@ const char *change_identity_e2str ( const struct passwd *pw ) | |||
46 | return "cannot set groups"; | 46 | return "cannot set groups"; |
47 | endgrent ( ); | 47 | endgrent ( ); |
48 | 48 | ||
49 | if ( setgid ( pw-> pw_gid )) | 49 | xsetgid(pw-> pw_gid); |
50 | return "cannot set group id"; | 50 | xsetuid(pw->pw_uid); |
51 | if ( setuid ( pw->pw_uid )) | ||
52 | return "cannot set user id"; | ||
53 | return NULL; | 51 | return NULL; |
54 | } | 52 | } |
55 | 53 | ||
diff --git a/libbb/copyfd.c b/libbb/copyfd.c index e2c542e32..0c4f7a054 100644 --- a/libbb/copyfd.c +++ b/libbb/copyfd.c | |||
@@ -30,24 +30,24 @@ static ssize_t bb_full_fd_action(int src_fd, int dst_fd, size_t size) | |||
30 | if (src_fd < 0) goto out; | 30 | if (src_fd < 0) goto out; |
31 | while (!size || total < size) | 31 | while (!size || total < size) |
32 | { | 32 | { |
33 | ssize_t wrote, xread; | 33 | ssize_t wr, rd; |
34 | 34 | ||
35 | xread = safe_read(src_fd, buffer, | 35 | rd = safe_read(src_fd, buffer, |
36 | (!size || size - total > BUFSIZ) ? BUFSIZ : size - total); | 36 | (!size || size - total > BUFSIZ) ? BUFSIZ : size - total); |
37 | 37 | ||
38 | if (xread > 0) { | 38 | if (rd > 0) { |
39 | /* A -1 dst_fd means we need to fake it... */ | 39 | /* A -1 dst_fd means we need to fake it... */ |
40 | wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread); | 40 | wr = (dst_fd < 0) ? rd : full_write(dst_fd, buffer, rd); |
41 | if (wrote < xread) { | 41 | if (wr < rd) { |
42 | bb_perror_msg(bb_msg_write_error); | 42 | bb_perror_msg(bb_msg_write_error); |
43 | break; | 43 | break; |
44 | } | 44 | } |
45 | total += wrote; | 45 | total += wr; |
46 | if (total == size) status = 0; | 46 | if (total == size) status = 0; |
47 | } else if (xread < 0) { | 47 | } else if (rd < 0) { |
48 | bb_perror_msg(bb_msg_read_error); | 48 | bb_perror_msg(bb_msg_read_error); |
49 | break; | 49 | break; |
50 | } else if (xread == 0) { | 50 | } else if (rd == 0) { |
51 | /* All done. */ | 51 | /* All done. */ |
52 | status = 0; | 52 | status = 0; |
53 | break; | 53 | break; |
diff --git a/libbb/create_icmp6_socket.c b/libbb/create_icmp6_socket.c index d8ff35a0a..c3d1b5578 100644 --- a/libbb/create_icmp6_socket.c +++ b/libbb/create_icmp6_socket.c | |||
@@ -32,7 +32,7 @@ int create_icmp6_socket(void) | |||
32 | } | 32 | } |
33 | 33 | ||
34 | /* drop root privs if running setuid */ | 34 | /* drop root privs if running setuid */ |
35 | setuid(getuid()); | 35 | xsetuid(getuid()); |
36 | 36 | ||
37 | return sock; | 37 | return sock; |
38 | } | 38 | } |
diff --git a/libbb/create_icmp_socket.c b/libbb/create_icmp_socket.c index 26120a66d..431c4d8a7 100644 --- a/libbb/create_icmp_socket.c +++ b/libbb/create_icmp_socket.c | |||
@@ -31,7 +31,7 @@ int create_icmp_socket(void) | |||
31 | } | 31 | } |
32 | 32 | ||
33 | /* drop root privs if running setuid */ | 33 | /* drop root privs if running setuid */ |
34 | setuid(getuid()); | 34 | xsetuid(getuid()); |
35 | 35 | ||
36 | return sock; | 36 | return sock; |
37 | } | 37 | } |
diff --git a/libbb/full_read.c b/libbb/full_read.c index 8d64bfb90..b5837d5bd 100644 --- a/libbb/full_read.c +++ b/libbb/full_read.c | |||
@@ -17,7 +17,7 @@ | |||
17 | * Returns the amount read, or -1 on an error. | 17 | * Returns the amount read, or -1 on an error. |
18 | * A short read is returned on an end of file. | 18 | * A short read is returned on an end of file. |
19 | */ | 19 | */ |
20 | ssize_t bb_full_read(int fd, void *buf, size_t len) | 20 | ssize_t full_read(int fd, void *buf, size_t len) |
21 | { | 21 | { |
22 | ssize_t cc; | 22 | ssize_t cc; |
23 | ssize_t total; | 23 | ssize_t total; |
diff --git a/libbb/full_write.c b/libbb/full_write.c index 3d6d40184..d812d04b4 100644 --- a/libbb/full_write.c +++ b/libbb/full_write.c | |||
@@ -16,7 +16,7 @@ | |||
16 | * This does multiple writes as necessary. | 16 | * This does multiple writes as necessary. |
17 | * Returns the amount written, or -1 on an error. | 17 | * Returns the amount written, or -1 on an error. |
18 | */ | 18 | */ |
19 | ssize_t bb_full_write(int fd, const void *buf, size_t len) | 19 | ssize_t full_write(int fd, const void *buf, size_t len) |
20 | { | 20 | { |
21 | ssize_t cc; | 21 | ssize_t cc; |
22 | ssize_t total; | 22 | ssize_t total; |
diff --git a/libbb/loop.c b/libbb/loop.c index b9caa973b..0b05cd75f 100644 --- a/libbb/loop.c +++ b/libbb/loop.c | |||
@@ -3,6 +3,7 @@ | |||
3 | * Utility routines. | 3 | * Utility routines. |
4 | * | 4 | * |
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 | * Copyright (C) 2005 by Rob Landley <rob@landley.net> | ||
6 | * | 7 | * |
7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
8 | */ | 9 | */ |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index d843414f9..8562a4fcb 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -120,40 +120,55 @@ int bb_xopen3(const char *pathname, int flags, int mode) | |||
120 | #endif | 120 | #endif |
121 | 121 | ||
122 | #ifdef L_xread | 122 | #ifdef L_xread |
123 | ssize_t bb_xread(int fd, void *buf, size_t count) | 123 | |
124 | // Die with an error message if we can't read the entire buffer. | ||
125 | |||
126 | void xread(int fd, void *buf, size_t count) | ||
124 | { | 127 | { |
125 | ssize_t size; | 128 | while (count) { |
129 | ssize_t size; | ||
126 | 130 | ||
127 | size = read(fd, buf, count); | 131 | if ((size = safe_read(fd, buf, count)) < 1) |
128 | if (size < 0) { | 132 | bb_error_msg_and_die("Short read"); |
129 | bb_perror_msg_and_die(bb_msg_read_error); | 133 | count -= size; |
134 | buf = ((char *) buf) + size; | ||
130 | } | 135 | } |
131 | return(size); | ||
132 | } | 136 | } |
133 | #endif | 137 | #endif |
134 | 138 | ||
135 | #ifdef L_xread_all | 139 | #ifdef L_xwrite |
136 | void bb_xread_all(int fd, void *buf, size_t count) | ||
137 | { | ||
138 | ssize_t size; | ||
139 | 140 | ||
141 | // Die with an error message if we can't write the entire buffer. | ||
142 | |||
143 | void xwrite(int fd, void *buf, size_t count) | ||
144 | { | ||
140 | while (count) { | 145 | while (count) { |
141 | if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */ | 146 | ssize_t size; |
142 | bb_error_msg_and_die("Short read"); | 147 | |
143 | } | 148 | if ((size = safe_write(fd, buf, count)) < 1) |
149 | bb_error_msg_and_die("Short write"); | ||
144 | count -= size; | 150 | count -= size; |
145 | buf = ((char *) buf) + size; | 151 | buf = ((char *) buf) + size; |
146 | } | 152 | } |
147 | return; | 153 | } |
154 | #endif | ||
155 | |||
156 | #ifdef L_xlseek | ||
157 | |||
158 | // Die if we can't lseek to the right spot. | ||
159 | |||
160 | void xlseek(int fd, off_t offset, int whence) | ||
161 | { | ||
162 | if (whence != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek"); | ||
148 | } | 163 | } |
149 | #endif | 164 | #endif |
150 | 165 | ||
151 | #ifdef L_xread_char | 166 | #ifdef L_xread_char |
152 | unsigned char bb_xread_char(int fd) | 167 | unsigned char xread_char(int fd) |
153 | { | 168 | { |
154 | char tmp; | 169 | char tmp; |
155 | 170 | ||
156 | bb_xread_all(fd, &tmp, 1); | 171 | xread(fd, &tmp, 1); |
157 | 172 | ||
158 | return(tmp); | 173 | return(tmp); |
159 | } | 174 | } |
@@ -294,3 +309,41 @@ void xsetuid(uid_t uid) | |||
294 | if (setuid(uid)) bb_error_msg_and_die("setuid"); | 309 | if (setuid(uid)) bb_error_msg_and_die("setuid"); |
295 | } | 310 | } |
296 | #endif | 311 | #endif |
312 | |||
313 | #ifdef L_fdlength | ||
314 | off_t fdlength(int fd) | ||
315 | { | ||
316 | off_t bottom = 0, top = 0, pos; | ||
317 | long size; | ||
318 | |||
319 | // If the ioctl works for this, return it. | ||
320 | |||
321 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; | ||
322 | |||
323 | // If not, do a binary search for the last location we can read. | ||
324 | |||
325 | do { | ||
326 | char temp; | ||
327 | |||
328 | pos = bottom + (top - bottom) / 2;; | ||
329 | |||
330 | // If we can read from the current location, it's bigger. | ||
331 | |||
332 | if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { | ||
333 | if (bottom == top) bottom = top = (top+1) * 2; | ||
334 | else bottom = pos; | ||
335 | |||
336 | // If we can't, it's smaller. | ||
337 | |||
338 | } else { | ||
339 | if (bottom == top) { | ||
340 | if (!top) return 0; | ||
341 | bottom = top/2; | ||
342 | } | ||
343 | else top = pos; | ||
344 | } | ||
345 | } while (bottom + 1 != top); | ||
346 | |||
347 | return pos + 1; | ||
348 | } | ||
349 | #endif | ||