Debian Codegraph @

tests/large_mounts.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2008 Eric Munson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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; /* = 0; */
41 int tmp_mounts_fd; /* = 0; */
42 FILE *tmp_stream; /* = NULL; */
43
44 /*
45 * We override the normal open, so we can remember the fd for the
46 * mounts file
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 }