| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
|
| 22 |
#include <sys/types.h> |
| 23 |
#include <sys/stat.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <dlfcn.h> |
| 30 |
#include <stdarg.h> |
| 31 |
#include <errno.h> |
| 32 |
|
| 33 |
#include <hugetlbfs.h> |
| 34 |
|
| 35 |
#include "hugetests.h" |
| 36 |
|
| 37 |
#define BUF_SIZE 4096 |
| 38 |
#define FILLER "tmpfs /var/run tmpfs rw,nosuid,nodev,noexec,mode=755 0 0\n" |
| 39 |
|
| 40 |
int in_test; |
| 41 |
int tmp_mounts_fd; |
| 42 |
FILE *tmp_stream; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
int open(const char *path, int flags, ...) |
| 49 |
{ |
| 50 |
int (*old_open)(const char *, int, ...); |
| 51 |
int fd; |
| 52 |
va_list ap; |
| 53 |
|
| 54 |
old_open = dlsym(RTLD_NEXT, "open"); |
| 55 |
if (in_test && strcmp(path, "/proc/mounts") == 0) |
| 56 |
return tmp_mounts_fd; |
| 57 |
va_start(ap, flags); |
| 58 |
fd = (old_open)(path, flags, va_arg(ap, mode_t)); |
| 59 |
va_end(ap); |
| 60 |
return fd; |
| 61 |
} |
| 62 |
|
| 63 |
void make_test_mounts() |
| 64 |
{ |
| 65 |
char buf[BUF_SIZE]; |
| 66 |
int mounts_fd; |
| 67 |
unsigned int written = 0; |
| 68 |
int ret; |
| 69 |
int filler_sz; |
| 70 |
|
| 71 |
mounts_fd = open("/proc/mounts", O_RDONLY); |
| 72 |
if (mounts_fd < 0) |
| 73 |
FAIL("Unable to open /proc/mounts: %s", strerror(errno)); |
| 74 |
tmp_stream = tmpfile(); |
| 75 |
if (!tmp_stream) |
| 76 |
FAIL("Unable to open temporary mounts file: %s", strerror(errno)); |
| 77 |
|
| 78 |
tmp_mounts_fd = fileno(tmp_stream); |
| 79 |
if (tmp_mounts_fd < 0) |
| 80 |
FAIL("Unable to get file descriptor from stream."); |
| 81 |
|
| 82 |
filler_sz = strlen(FILLER); |
| 83 |
|
| 84 |
while (written < BUF_SIZE) { |
| 85 |
if (write(tmp_mounts_fd, FILLER, filler_sz) < 0) |
| 86 |
FAIL("Unable to write to temp mounts file: %s", |
| 87 |
strerror(errno)); |
| 88 |
written += filler_sz; |
| 89 |
} |
| 90 |
|
| 91 |
while ((ret = read(mounts_fd, buf, BUF_SIZE)) > 0) |
| 92 |
if (write(tmp_mounts_fd, buf, ret) < 0) |
| 93 |
FAIL("Unable to write to temp mounts file: %s", |
| 94 |
strerror(errno)); |
| 95 |
|
| 96 |
close(mounts_fd); |
| 97 |
if (lseek(tmp_mounts_fd, 0, SEEK_SET) < 0) |
| 98 |
FAIL("Unable to move temp mounts stream to beginning of file: %s", |
| 99 |
strerror(errno)); |
| 100 |
} |
| 101 |
|
| 102 |
int main(int argc, char *argv[]) |
| 103 |
{ |
| 104 |
int fd; |
| 105 |
|
| 106 |
make_test_mounts(); |
| 107 |
test_init(argc, argv); |
| 108 |
in_test = 1; |
| 109 |
|
| 110 |
fd = hugetlbfs_unlinked_fd(); |
| 111 |
|
| 112 |
fclose(tmp_stream); |
| 113 |
if (fd < 0) |
| 114 |
FAIL("Unable to find mount point\n"); |
| 115 |
|
| 116 |
PASS(); |
| 117 |
} |