Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 11 | #if ENABLE_FEATURE_USE_SENDFILE |
| 12 | # include <sys/sendfile.h> |
| 13 | #else |
| 14 | # define sendfile(a,b,c,d) (-1) |
| 15 | #endif |
| 16 | |
| 17 | /* |
| 18 | * We were using 0x7fff0000 as sendfile chunk size, but it |
| 19 | * was seen to cause largish delays when user tries to ^C a file copy. |
| 20 | * Let's use a saner size. |
| 21 | * Note: needs to be >= max(CONFIG_FEATURE_COPYBUF_KB), |
| 22 | * or else "copy to eof" code will use neddlesly short reads. |
| 23 | */ |
| 24 | #define SENDFILE_BIGBUF (16*1024*1024) |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 25 | |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 26 | /* Used by NOFORK applets (e.g. cat) - must not use xmalloc. |
| 27 | * size < 0 means "ignore write errors", used by tar --to-command |
| 28 | * size = 0 means "copy till EOF" |
| 29 | */ |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 30 | static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 31 | { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 32 | int status = -1; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 33 | off_t total = 0; |
Denys Vlasenko | 1eaf7e7 | 2010-06-26 23:08:59 +0200 | [diff] [blame] | 34 | bool continue_on_write_error = 0; |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 35 | ssize_t sendfile_sz; |
| 36 | #if CONFIG_FEATURE_COPYBUF_KB > 4 |
| 37 | char *buffer = buffer; /* for compiler */ |
| 38 | int buffer_size = 0; |
| 39 | #else |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 40 | char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024]; |
| 41 | enum { buffer_size = sizeof(buffer) }; |
Denys Vlasenko | 1eaf7e7 | 2010-06-26 23:08:59 +0200 | [diff] [blame] | 42 | #endif |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 43 | |
| 44 | if (size < 0) { |
| 45 | size = -size; |
| 46 | continue_on_write_error = 1; |
| 47 | } |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 49 | if (src_fd < 0) |
| 50 | goto out; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 51 | |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 52 | sendfile_sz = !ENABLE_FEATURE_USE_SENDFILE |
| 53 | ? 0 |
| 54 | : SENDFILE_BIGBUF; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 55 | if (!size) { |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 56 | size = SENDFILE_BIGBUF; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 57 | status = 1; /* copy until eof */ |
| 58 | } |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 60 | while (1) { |
| 61 | ssize_t rd; |
| 62 | |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 63 | if (sendfile_sz) { |
| 64 | rd = sendfile(dst_fd, src_fd, NULL, |
| 65 | size > sendfile_sz ? sendfile_sz : size); |
| 66 | if (rd >= 0) |
| 67 | goto read_ok; |
| 68 | sendfile_sz = 0; /* do not try sendfile anymore */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 69 | } |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 70 | #if CONFIG_FEATURE_COPYBUF_KB > 4 |
| 71 | if (buffer_size == 0) { |
| 72 | if (size > 0 && size <= 4 * 1024) |
| 73 | goto use_small_buf; |
| 74 | /* We want page-aligned buffer, just in case kernel is clever |
| 75 | * and can do page-aligned io more efficiently */ |
| 76 | buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024, |
| 77 | PROT_READ | PROT_WRITE, |
| 78 | MAP_PRIVATE | MAP_ANON, |
| 79 | /* ignored: */ -1, 0); |
| 80 | buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024; |
| 81 | if (buffer == MAP_FAILED) { |
| 82 | use_small_buf: |
| 83 | buffer = alloca(4 * 1024); |
| 84 | buffer_size = 4 * 1024; |
| 85 | } |
| 86 | } |
| 87 | #endif |
| 88 | rd = safe_read(src_fd, buffer, |
| 89 | size > buffer_size ? buffer_size : size); |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 90 | if (rd < 0) { |
| 91 | bb_perror_msg(bb_msg_read_error); |
| 92 | break; |
| 93 | } |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 94 | read_ok: |
| 95 | if (!rd) { /* eof - all done */ |
| 96 | status = 0; |
| 97 | break; |
| 98 | } |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 99 | /* dst_fd == -1 is a fake, else... */ |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 100 | if (dst_fd >= 0 && !sendfile_sz) { |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 101 | ssize_t wr = full_write(dst_fd, buffer, rd); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 102 | if (wr < rd) { |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 103 | if (!continue_on_write_error) { |
| 104 | bb_perror_msg(bb_msg_write_error); |
| 105 | break; |
| 106 | } |
| 107 | dst_fd = -1; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 108 | } |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 109 | } |
| 110 | total += rd; |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 111 | if (status < 0) { /* if we aren't copying till EOF... */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 112 | size -= rd; |
| 113 | if (!size) { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 114 | /* 'size' bytes copied - all done */ |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 115 | status = 0; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 116 | break; |
| 117 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 118 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 119 | } |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 120 | out: |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 121 | |
Johannes Schindelin | e5b1f5a | 2017-07-14 16:11:43 +0200 | [diff] [blame] | 122 | /* some environments don't have munmap(), hide it in #if */ |
| 123 | #if CONFIG_FEATURE_COPYBUF_KB > 4 |
Bartosz Golaszewski | 8d75d79 | 2014-11-27 13:20:24 +0100 | [diff] [blame] | 124 | if (buffer_size > 4 * 1024) |
Denis Vlasenko | 4c13922 | 2007-12-02 03:27:42 +0000 | [diff] [blame] | 125 | munmap(buffer, buffer_size); |
Johannes Schindelin | e5b1f5a | 2017-07-14 16:11:43 +0200 | [diff] [blame] | 126 | #endif |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 127 | return status ? -1 : total; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 131 | #if 0 |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 132 | void FAST_FUNC complain_copyfd_and_die(off_t sz) |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 133 | { |
| 134 | if (sz != -1) |
| 135 | bb_error_msg_and_die("short read"); |
| 136 | /* if sz == -1, bb_copyfd_XX already complained */ |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 137 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 138 | } |
| 139 | #endif |
| 140 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 141 | off_t FAST_FUNC bb_copyfd_size(int fd1, int fd2, off_t size) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 142 | { |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 143 | if (size) { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 144 | return bb_full_fd_action(fd1, fd2, size); |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 145 | } |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 146 | return 0; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 149 | void FAST_FUNC bb_copyfd_exact_size(int fd1, int fd2, off_t size) |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 150 | { |
| 151 | off_t sz = bb_copyfd_size(fd1, fd2, size); |
Denys Vlasenko | d0a8a0d | 2010-06-26 18:11:44 +0200 | [diff] [blame] | 152 | if (sz == (size >= 0 ? size : -size)) |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 153 | return; |
| 154 | if (sz != -1) |
| 155 | bb_error_msg_and_die("short read"); |
| 156 | /* if sz == -1, bb_copyfd_XX already complained */ |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 157 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 160 | off_t FAST_FUNC bb_copyfd_eof(int fd1, int fd2) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 161 | { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 162 | return bb_full_fd_action(fd1, fd2, 0); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 163 | } |