summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archival/rpm.c2
-rw-r--r--include/libbb.h3
-rw-r--r--libbb/copyfd.c5
-rw-r--r--libbb/xfuncs_printf.c21
-rw-r--r--modutils/modutils.c2
-rw-r--r--util-linux/uevent.c7
6 files changed, 28 insertions, 12 deletions
diff --git a/archival/rpm.c b/archival/rpm.c
index 68afba9..a4d850b 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -145,7 +145,7 @@ static int rpm_gettags(const char *filename)
/* remember size for munmap */
G.mapsize = storepos;
/* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */
- G.map = mmap(0, storepos, PROT_READ, MAP_PRIVATE, fd, 0);
+ G.map = mmap_read(fd, storepos);
if (G.map == MAP_FAILED)
bb_perror_msg_and_die("mmap '%s'", filename);
diff --git a/include/libbb.h b/include/libbb.h
index 8f1ee7e..a74b311 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -387,6 +387,9 @@ void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) F
char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC;
char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC;
void *xmemdup(const void *s, int n) FAST_FUNC RETURNS_MALLOC;
+void *mmap_read(int fd, size_t size) FAST_FUNC;
+void *mmap_anon(size_t size) FAST_FUNC;
+void *xmmap_anon(size_t size) FAST_FUNC;
//TODO: supply a pointer to char[11] buffer (avoid statics)?
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index d41fd10..7f9d92e 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -75,10 +75,7 @@ static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
goto use_small_buf;
/* We want page-aligned buffer, just in case kernel is clever
* and can do page-aligned io more efficiently */
- buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
- PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON,
- /* ignored: */ -1, 0);
+ buffer = mmap_anon(CONFIG_FEATURE_COPYBUF_KB * 1024);
buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
if (buffer == MAP_FAILED) {
use_small_buf:
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index fcc7986..db40e99 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -111,6 +111,27 @@ void* FAST_FUNC xmemdup(const void *s, int n)
return memcpy(xmalloc(n), s, n);
}
+void* FAST_FUNC mmap_read(int fd, size_t size)
+{
+ return mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
+}
+
+void* FAST_FUNC mmap_anon(size_t size)
+{
+ return mmap(NULL, size,
+ PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS,
+ /* ignored: */ -1, 0);
+}
+
+void* FAST_FUNC xmmap_anon(size_t size)
+{
+ void *p = mmap_anon(size);
+ if (p == MAP_FAILED)
+ bb_die_memory_exhausted();
+ return p;
+}
+
// Die if we can't open a file and return a FILE* to it.
// Notice we haven't got xfread(), This is for use with fscanf() and friends.
FILE* FAST_FUNC xfopen(const char *path, const char *mode)
diff --git a/modutils/modutils.c b/modutils/modutils.c
index 6f7cd97..f7ad5e8 100644
--- a/modutils/modutils.c
+++ b/modutils/modutils.c
@@ -169,7 +169,7 @@ void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
/* st.st_size is off_t, we can't just pass it to mmap */
if (st.st_size <= *image_size_p) {
size_t image_size = st.st_size;
- image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ image = mmap_read(fd, image_size);
if (image == MAP_FAILED) {
image = NULL;
} else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
diff --git a/util-linux/uevent.c b/util-linux/uevent.c
index 045b354..015f1ee 100644
--- a/util-linux/uevent.c
+++ b/util-linux/uevent.c
@@ -74,12 +74,7 @@ int uevent_main(int argc UNUSED_PARAM, char **argv)
// for a new uevent notification to come in.
// We use a fresh mmap so that buffer is not allocated
// until kernel actually starts filling it.
- netbuf = mmap(NULL, USER_RCVBUF,
- PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON,
- /* ignored: */ -1, 0);
- if (netbuf == MAP_FAILED)
- bb_simple_perror_msg_and_die("mmap");
+ netbuf = xmmap_anon(USER_RCVBUF);
// Here we block, possibly for a very long time
len = safe_read(fd, netbuf, USER_RCVBUF - 1);