← Commit list

fd3c512f8

Denys Vlasenko
Denys Vlasenko
2020-12-14
SUCCESS Duration: 148s

Subject

libbb: create and use mmap() helpers

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

archival/rpm.c+1 -1
@@ -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);
include/libbb.h+3 -0
@@ -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)?
libbb/copyfd.c+1 -4
@@ -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:
libbb/xfuncs_printf.c+21 -0
@@ -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)
modutils/modutils.c+1 -1
@@ -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)) {
util-linux/uevent.c+1 -6
@@ -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);