Skip to content

Commit a3e5f1b

Browse files
committed
Passing a 64 bit value with syscall() has problems in certain older
syscalls on some 32 bit architectures, and this one is used as the _example_ in the the "Architecture-specific requirements" section of "man 2 syscall". Some 32-bit processors can only use 64 bit values in even register pairs. Eight old syscalls (fadvise64_64, ftruncate64, posix_fadvise, pread64, pwrite64, readahead, sync_file_range, and truncate64) didn't arrange the argument order to naturally align, so required padding on 32-bit arm eabi, mips oabi, powerpc, parisc, and xtensa. The libc wrapper inserts this padding when necessary, and using that is less ugly than architecture specific whack-a-mole in toybox, even working around glibc's refusal to provide the prototype unless we declare Linux to be somehow owned by the Free Softare Foundation (which was why it was using the syscall directly before).
1 parent 7932d08 commit a3e5f1b

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

toys/other/readahead.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ config READAHEAD
1515
Preload files into disk cache.
1616
*/
1717

18+
#define _LARGEFILE64_SOURCE // musl's _ALL_SOURCE lies, no off64_t
1819
#include "toys.h"
1920

21+
// glibc won't provide this prototype unless we claim Linux belongs to the FSF
22+
ssize_t readahead(int fd, off64_t offset, size_t count);
23+
2024
static void do_readahead(int fd, char *name)
2125
{
22-
int rc;
23-
24-
// Since including fcntl.h doesn't give us the wrapper, use the syscall.
25-
// 32 bits takes LO/HI offset (we don't care about endianness of 0).
26-
if (sizeof(long) == 4) rc = syscall(__NR_readahead, fd, 0, 0, INT_MAX);
27-
else rc = syscall(__NR_readahead, fd, 0, INT_MAX);
28-
29-
if (rc) perror_msg("readahead: %s", name);
26+
if (readahead(fd, 0, INT_MAX)) perror_msg("readahead: %s", name);
3027
}
3128

3229
void readahead_main(void)

0 commit comments

Comments
 (0)