bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux and CPU test |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 18 | */ |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 19 | #define _GNU_SOURCE |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <errno.h> |
| 31 | #include <utime.h> |
| 32 | #include <time.h> |
| 33 | #include <sys/time.h> |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 34 | #include <sys/resource.h> |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 35 | #include <sys/uio.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <netinet/in.h> |
| 38 | #include <arpa/inet.h> |
| 39 | #include <sched.h> |
| 40 | #include <dirent.h> |
| 41 | #include <setjmp.h> |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 42 | #include <sys/shm.h> |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 43 | #include <assert.h> |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 44 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 45 | #define STACK_SIZE 16384 |
| 46 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 47 | static void error1(const char *filename, int line, const char *fmt, ...) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 48 | { |
| 49 | va_list ap; |
| 50 | va_start(ap, fmt); |
| 51 | fprintf(stderr, "%s:%d: ", filename, line); |
| 52 | vfprintf(stderr, fmt, ap); |
| 53 | fprintf(stderr, "\n"); |
| 54 | va_end(ap); |
| 55 | exit(1); |
| 56 | } |
| 57 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 58 | static int __chk_error(const char *filename, int line, int ret) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 59 | { |
| 60 | if (ret < 0) { |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 61 | error1(filename, line, "%m (ret=%d, errno=%d/%s)", |
| 62 | ret, errno, strerror(errno)); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 63 | } |
| 64 | return ret; |
| 65 | } |
| 66 | |
Blue Swirl | 001faf3 | 2009-05-13 17:53:17 +0000 | [diff] [blame] | 67 | #define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 68 | |
| 69 | #define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret)) |
| 70 | |
| 71 | /*******************************************************/ |
| 72 | |
| 73 | #define FILE_BUF_SIZE 300 |
| 74 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 75 | static void test_file(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 76 | { |
| 77 | int fd, i, len, ret; |
| 78 | uint8_t buf[FILE_BUF_SIZE]; |
| 79 | uint8_t buf2[FILE_BUF_SIZE]; |
| 80 | uint8_t buf3[FILE_BUF_SIZE]; |
| 81 | char cur_dir[1024]; |
| 82 | struct stat st; |
| 83 | struct utimbuf tbuf; |
| 84 | struct iovec vecs[2]; |
| 85 | DIR *dir; |
Laurent Vivier | 1f442da | 2018-11-13 18:54:37 +0100 | [diff] [blame] | 86 | struct dirent64 *de; |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 87 | /* TODO: make common tempdir creation for tcg tests */ |
| 88 | char template[] = "/tmp/linux-test-XXXXXX"; |
| 89 | char *tmpdir = mkdtemp(template); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 90 | |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 91 | assert(tmpdir); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 92 | |
| 93 | if (getcwd(cur_dir, sizeof(cur_dir)) == NULL) |
| 94 | error("getcwd"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 95 | |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 96 | chk_error(chdir(tmpdir)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 97 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 98 | /* open/read/write/close/readv/writev/lseek */ |
| 99 | |
| 100 | fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644)); |
| 101 | for(i=0;i < FILE_BUF_SIZE; i++) |
| 102 | buf[i] = i; |
| 103 | len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2)); |
| 104 | if (len != (FILE_BUF_SIZE / 2)) |
| 105 | error("write"); |
| 106 | vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2); |
| 107 | vecs[0].iov_len = 16; |
| 108 | vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16; |
| 109 | vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16; |
| 110 | len = chk_error(writev(fd, vecs, 2)); |
| 111 | if (len != (FILE_BUF_SIZE / 2)) |
| 112 | error("writev"); |
| 113 | chk_error(close(fd)); |
| 114 | |
| 115 | chk_error(rename("file1", "file2")); |
| 116 | |
| 117 | fd = chk_error(open("file2", O_RDONLY)); |
| 118 | |
| 119 | len = chk_error(read(fd, buf2, FILE_BUF_SIZE)); |
| 120 | if (len != FILE_BUF_SIZE) |
| 121 | error("read"); |
| 122 | if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0) |
| 123 | error("memcmp"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 124 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 125 | #define FOFFSET 16 |
| 126 | ret = chk_error(lseek(fd, FOFFSET, SEEK_SET)); |
| 127 | if (ret != 16) |
| 128 | error("lseek"); |
| 129 | vecs[0].iov_base = buf3; |
| 130 | vecs[0].iov_len = 32; |
| 131 | vecs[1].iov_base = buf3 + 32; |
| 132 | vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32; |
| 133 | len = chk_error(readv(fd, vecs, 2)); |
| 134 | if (len != FILE_BUF_SIZE - FOFFSET) |
| 135 | error("readv"); |
| 136 | if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0) |
| 137 | error("memcmp"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 138 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 139 | chk_error(close(fd)); |
| 140 | |
| 141 | /* access */ |
| 142 | chk_error(access("file2", R_OK)); |
| 143 | |
| 144 | /* stat/chmod/utime/truncate */ |
| 145 | |
| 146 | chk_error(chmod("file2", 0600)); |
| 147 | tbuf.actime = 1001; |
| 148 | tbuf.modtime = 1000; |
| 149 | chk_error(truncate("file2", 100)); |
| 150 | chk_error(utime("file2", &tbuf)); |
| 151 | chk_error(stat("file2", &st)); |
| 152 | if (st.st_size != 100) |
| 153 | error("stat size"); |
| 154 | if (!S_ISREG(st.st_mode)) |
| 155 | error("stat mode"); |
| 156 | if ((st.st_mode & 0777) != 0600) |
| 157 | error("stat mode2"); |
| 158 | if (st.st_atime != 1001 || |
| 159 | st.st_mtime != 1000) |
| 160 | error("stat time"); |
| 161 | |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 162 | chk_error(stat(tmpdir, &st)); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 163 | if (!S_ISDIR(st.st_mode)) |
| 164 | error("stat mode"); |
| 165 | |
| 166 | /* fstat */ |
| 167 | fd = chk_error(open("file2", O_RDWR)); |
| 168 | chk_error(ftruncate(fd, 50)); |
| 169 | chk_error(fstat(fd, &st)); |
| 170 | chk_error(close(fd)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 171 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 172 | if (st.st_size != 50) |
| 173 | error("stat size"); |
| 174 | if (!S_ISREG(st.st_mode)) |
| 175 | error("stat mode"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 176 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 177 | /* symlink/lstat */ |
| 178 | chk_error(symlink("file2", "file3")); |
| 179 | chk_error(lstat("file3", &st)); |
| 180 | if (!S_ISLNK(st.st_mode)) |
| 181 | error("stat mode"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 182 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 183 | /* getdents */ |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 184 | dir = opendir(tmpdir); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 185 | if (!dir) |
| 186 | error("opendir"); |
| 187 | len = 0; |
| 188 | for(;;) { |
Laurent Vivier | 1f442da | 2018-11-13 18:54:37 +0100 | [diff] [blame] | 189 | de = readdir64(dir); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 190 | if (!de) |
| 191 | break; |
| 192 | if (strcmp(de->d_name, ".") != 0 && |
| 193 | strcmp(de->d_name, "..") != 0 && |
| 194 | strcmp(de->d_name, "file2") != 0 && |
| 195 | strcmp(de->d_name, "file3") != 0) |
| 196 | error("readdir"); |
| 197 | len++; |
| 198 | } |
| 199 | closedir(dir); |
| 200 | if (len != 4) |
| 201 | error("readdir"); |
| 202 | |
| 203 | chk_error(unlink("file3")); |
| 204 | chk_error(unlink("file2")); |
| 205 | chk_error(chdir(cur_dir)); |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 206 | chk_error(rmdir(tmpdir)); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 209 | static void test_fork(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 210 | { |
| 211 | int pid, status; |
| 212 | |
| 213 | pid = chk_error(fork()); |
| 214 | if (pid == 0) { |
| 215 | /* child */ |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 216 | sleep(2); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 217 | exit(2); |
| 218 | } |
| 219 | chk_error(waitpid(pid, &status, 0)); |
| 220 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 2) |
| 221 | error("waitpid status=0x%x", status); |
| 222 | } |
| 223 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 224 | static void test_time(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 225 | { |
| 226 | struct timeval tv, tv2; |
| 227 | struct timespec ts, rem; |
| 228 | struct rusage rusg1, rusg2; |
| 229 | int ti, i; |
| 230 | |
| 231 | chk_error(gettimeofday(&tv, NULL)); |
| 232 | rem.tv_sec = 1; |
| 233 | ts.tv_sec = 0; |
| 234 | ts.tv_nsec = 20 * 1000000; |
| 235 | chk_error(nanosleep(&ts, &rem)); |
| 236 | if (rem.tv_sec != 1) |
| 237 | error("nanosleep"); |
| 238 | chk_error(gettimeofday(&tv2, NULL)); |
| 239 | ti = tv2.tv_sec - tv.tv_sec; |
| 240 | if (ti >= 2) |
| 241 | error("gettimeofday"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 242 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 243 | chk_error(getrusage(RUSAGE_SELF, &rusg1)); |
| 244 | for(i = 0;i < 10000; i++); |
| 245 | chk_error(getrusage(RUSAGE_SELF, &rusg2)); |
| 246 | if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 || |
| 247 | (rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0) |
| 248 | error("getrusage"); |
| 249 | } |
| 250 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 251 | static int server_socket(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 252 | { |
| 253 | int val, fd; |
Peter Maydell | baa873f | 2021-08-13 16:05:06 +0100 | [diff] [blame] | 254 | struct sockaddr_in sockaddr = {}; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 255 | |
| 256 | /* server socket */ |
| 257 | fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); |
| 258 | |
| 259 | val = 1; |
| 260 | chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))); |
| 261 | |
| 262 | sockaddr.sin_family = AF_INET; |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 263 | sockaddr.sin_port = htons(0); /* choose random ephemeral port) */ |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 264 | sockaddr.sin_addr.s_addr = 0; |
| 265 | chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); |
| 266 | chk_error(listen(fd, 0)); |
| 267 | return fd; |
| 268 | |
| 269 | } |
| 270 | |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 271 | static int client_socket(uint16_t port) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 272 | { |
| 273 | int fd; |
Peter Maydell | baa873f | 2021-08-13 16:05:06 +0100 | [diff] [blame] | 274 | struct sockaddr_in sockaddr = {}; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 275 | |
| 276 | /* server socket */ |
| 277 | fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); |
| 278 | sockaddr.sin_family = AF_INET; |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 279 | sockaddr.sin_port = htons(port); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 280 | inet_aton("127.0.0.1", &sockaddr.sin_addr); |
| 281 | chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))); |
| 282 | return fd; |
| 283 | } |
| 284 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 285 | static const char socket_msg[] = "hello socket\n"; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 286 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 287 | static void test_socket(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 288 | { |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 289 | int server_fd, client_fd, fd, pid, ret, val; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 290 | struct sockaddr_in sockaddr; |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 291 | struct sockaddr_in server_addr; |
| 292 | socklen_t len, socklen; |
| 293 | uint16_t server_port; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 294 | char buf[512]; |
| 295 | |
| 296 | server_fd = server_socket(); |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 297 | /* find out what port we got */ |
| 298 | socklen = sizeof(server_addr); |
Richard Henderson | 51f22c1 | 2020-10-05 11:41:26 -0500 | [diff] [blame] | 299 | ret = getsockname(server_fd, (struct sockaddr *)&server_addr, &socklen); |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 300 | chk_error(ret); |
| 301 | server_port = ntohs(server_addr.sin_port); |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 302 | |
| 303 | /* test a few socket options */ |
| 304 | len = sizeof(val); |
| 305 | chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len)); |
| 306 | if (val != SOCK_STREAM) |
| 307 | error("getsockopt"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 308 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 309 | pid = chk_error(fork()); |
| 310 | if (pid == 0) { |
Alex Bennée | 5aa632d | 2018-06-08 20:15:03 +0100 | [diff] [blame] | 311 | client_fd = client_socket(server_port); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 312 | send(client_fd, socket_msg, sizeof(socket_msg), 0); |
| 313 | close(client_fd); |
| 314 | exit(0); |
| 315 | } |
| 316 | len = sizeof(sockaddr); |
| 317 | fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len)); |
| 318 | |
| 319 | ret = chk_error(recv(fd, buf, sizeof(buf), 0)); |
| 320 | if (ret != sizeof(socket_msg)) |
| 321 | error("recv"); |
| 322 | if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0) |
| 323 | error("socket_msg"); |
| 324 | chk_error(close(fd)); |
| 325 | chk_error(close(server_fd)); |
| 326 | } |
| 327 | |
| 328 | #define WCOUNT_MAX 512 |
| 329 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 330 | static void test_pipe(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 331 | { |
| 332 | fd_set rfds, wfds; |
| 333 | int fds[2], fd_max, ret; |
| 334 | uint8_t ch; |
| 335 | int wcount, rcount; |
| 336 | |
| 337 | chk_error(pipe(fds)); |
| 338 | chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK)); |
| 339 | chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK)); |
| 340 | wcount = 0; |
| 341 | rcount = 0; |
| 342 | for(;;) { |
| 343 | FD_ZERO(&rfds); |
| 344 | fd_max = fds[0]; |
| 345 | FD_SET(fds[0], &rfds); |
| 346 | |
| 347 | FD_ZERO(&wfds); |
| 348 | FD_SET(fds[1], &wfds); |
| 349 | if (fds[1] > fd_max) |
| 350 | fd_max = fds[1]; |
| 351 | |
| 352 | ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL)); |
| 353 | if (ret > 0) { |
| 354 | if (FD_ISSET(fds[0], &rfds)) { |
| 355 | chk_error(read(fds[0], &ch, 1)); |
| 356 | rcount++; |
| 357 | if (rcount >= WCOUNT_MAX) |
| 358 | break; |
| 359 | } |
| 360 | if (FD_ISSET(fds[1], &wfds)) { |
| 361 | ch = 'a'; |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 362 | chk_error(write(fds[1], &ch, 1)); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 363 | wcount++; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | chk_error(close(fds[0])); |
| 368 | chk_error(close(fds[1])); |
| 369 | } |
| 370 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 371 | static int thread1_res; |
| 372 | static int thread2_res; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 373 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 374 | static int thread1_func(void *arg) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 375 | { |
| 376 | int i; |
| 377 | for(i=0;i<5;i++) { |
| 378 | thread1_res++; |
| 379 | usleep(10 * 1000); |
| 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 384 | static int thread2_func(void *arg) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 385 | { |
| 386 | int i; |
| 387 | for(i=0;i<6;i++) { |
| 388 | thread2_res++; |
| 389 | usleep(10 * 1000); |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 394 | static void wait_for_child(pid_t pid) |
| 395 | { |
| 396 | int status; |
| 397 | chk_error(waitpid(pid, &status, 0)); |
| 398 | } |
| 399 | |
| 400 | /* For test_clone we must match the clone flags used by glibc, see |
| 401 | * CLONE_THREAD_FLAGS in the QEMU source code. |
| 402 | */ |
| 403 | static void test_clone(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 404 | { |
| 405 | uint8_t *stack1, *stack2; |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 406 | pid_t pid1, pid2; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 407 | |
| 408 | stack1 = malloc(STACK_SIZE); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 409 | pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE, |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 410 | CLONE_VM | CLONE_FS | CLONE_FILES | |
| 411 | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, |
| 412 | "hello1")); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 413 | |
| 414 | stack2 = malloc(STACK_SIZE); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 415 | pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE, |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 416 | CLONE_VM | CLONE_FS | CLONE_FILES | |
| 417 | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM, |
| 418 | "hello2")); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 419 | |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 420 | wait_for_child(pid1); |
Stefan Weil | 8da91ff | 2011-01-21 22:49:29 +0100 | [diff] [blame] | 421 | free(stack1); |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 422 | wait_for_child(pid2); |
Stefan Weil | 8da91ff | 2011-01-21 22:49:29 +0100 | [diff] [blame] | 423 | free(stack2); |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 424 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 425 | if (thread1_res != 5 || |
| 426 | thread2_res != 6) |
| 427 | error("clone"); |
| 428 | } |
| 429 | |
| 430 | /***********************************/ |
| 431 | |
| 432 | volatile int alarm_count; |
| 433 | jmp_buf jmp_env; |
| 434 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 435 | static void sig_alarm(int sig) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 436 | { |
| 437 | if (sig != SIGALRM) |
| 438 | error("signal"); |
| 439 | alarm_count++; |
| 440 | } |
| 441 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 442 | static void sig_segv(int sig, siginfo_t *info, void *puc) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 443 | { |
| 444 | if (sig != SIGSEGV) |
| 445 | error("signal"); |
| 446 | longjmp(jmp_env, 1); |
| 447 | } |
| 448 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 449 | static void test_signal(void) |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 450 | { |
| 451 | struct sigaction act; |
| 452 | struct itimerval it, oit; |
| 453 | |
| 454 | /* timer test */ |
| 455 | |
| 456 | alarm_count = 0; |
| 457 | |
| 458 | act.sa_handler = sig_alarm; |
| 459 | sigemptyset(&act.sa_mask); |
| 460 | act.sa_flags = 0; |
| 461 | chk_error(sigaction(SIGALRM, &act, NULL)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 462 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 463 | it.it_interval.tv_sec = 0; |
| 464 | it.it_interval.tv_usec = 10 * 1000; |
| 465 | it.it_value.tv_sec = 0; |
| 466 | it.it_value.tv_usec = 10 * 1000; |
| 467 | chk_error(setitimer(ITIMER_REAL, &it, NULL)); |
| 468 | chk_error(getitimer(ITIMER_REAL, &oit)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 469 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 470 | while (alarm_count < 5) { |
| 471 | usleep(10 * 1000); |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 472 | getitimer(ITIMER_REAL, &oit); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | it.it_interval.tv_sec = 0; |
| 476 | it.it_interval.tv_usec = 0; |
| 477 | it.it_value.tv_sec = 0; |
| 478 | it.it_value.tv_usec = 0; |
| 479 | memset(&oit, 0xff, sizeof(oit)); |
| 480 | chk_error(setitimer(ITIMER_REAL, &it, &oit)); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 481 | |
| 482 | /* SIGSEGV test */ |
| 483 | act.sa_sigaction = sig_segv; |
| 484 | sigemptyset(&act.sa_mask); |
| 485 | act.sa_flags = SA_SIGINFO; |
| 486 | chk_error(sigaction(SIGSEGV, &act, NULL)); |
| 487 | if (setjmp(jmp_env) == 0) { |
Taylor Simpson | 22c30b2 | 2019-11-17 17:14:25 -0600 | [diff] [blame] | 488 | /* |
| 489 | * clang requires volatile or it will turn this into a |
| 490 | * call to abort() instead of forcing a SIGSEGV. |
| 491 | */ |
| 492 | *(volatile uint8_t *)0 = 0; |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 493 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 494 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 495 | act.sa_handler = SIG_DFL; |
| 496 | sigemptyset(&act.sa_mask); |
| 497 | act.sa_flags = 0; |
| 498 | chk_error(sigaction(SIGSEGV, &act, NULL)); |
Ilya Leoshkevich | 1df0f5c | 2021-06-01 16:56:00 +0200 | [diff] [blame] | 499 | |
| 500 | if (sigaction(SIGKILL, &act, NULL) == 0) { |
| 501 | error("sigaction(SIGKILL, &act, NULL) must not succeed"); |
| 502 | } |
| 503 | if (sigaction(SIGSTOP, &act, NULL) == 0) { |
| 504 | error("sigaction(SIGSTOP, &act, NULL) must not succeed"); |
| 505 | } |
| 506 | chk_error(sigaction(SIGKILL, NULL, &act)); |
| 507 | chk_error(sigaction(SIGSTOP, NULL, &act)); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 508 | } |
| 509 | |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 510 | #define SHM_SIZE 32768 |
| 511 | |
Fam Zheng | 5e13cbd | 2017-09-08 17:16:58 +0800 | [diff] [blame] | 512 | static void test_shm(void) |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 513 | { |
| 514 | void *ptr; |
| 515 | int shmid; |
| 516 | |
| 517 | shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777)); |
| 518 | ptr = shmat(shmid, NULL, 0); |
Richard Henderson | 3d72543 | 2019-11-06 12:33:08 +0100 | [diff] [blame] | 519 | if (ptr == (void *)-1) { |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 520 | error("shmat"); |
Richard Henderson | 3d72543 | 2019-11-06 12:33:08 +0100 | [diff] [blame] | 521 | } |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 522 | |
| 523 | memset(ptr, 0, SHM_SIZE); |
| 524 | |
| 525 | chk_error(shmctl(shmid, IPC_RMID, 0)); |
| 526 | chk_error(shmdt(ptr)); |
| 527 | } |
| 528 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 529 | int main(int argc, char **argv) |
| 530 | { |
bellard | 285dc33 | 2003-10-27 23:58:04 +0000 | [diff] [blame] | 531 | test_file(); |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 532 | test_pipe(); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 533 | test_fork(); |
| 534 | test_time(); |
| 535 | test_socket(); |
Alex Bennée | 875fcce | 2018-04-19 14:02:04 +0100 | [diff] [blame] | 536 | |
| 537 | if (argc > 1) { |
| 538 | printf("test_clone still considered buggy\n"); |
| 539 | test_clone(); |
| 540 | } |
| 541 | |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 542 | test_signal(); |
bellard | e374bfa | 2004-02-22 14:54:18 +0000 | [diff] [blame] | 543 | test_shm(); |
bellard | 196ad10 | 2003-09-28 18:59:32 +0000 | [diff] [blame] | 544 | return 0; |
| 545 | } |