2003-09-28 22:59:32 +04:00
|
|
|
/*
|
|
|
|
* linux and CPU test
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2003-09-28 22:59:32 +04:00
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-07-17 00:47:01 +04:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-09-28 22:59:32 +04:00
|
|
|
*/
|
2018-04-19 16:02:04 +03:00
|
|
|
#define _GNU_SOURCE
|
2003-09-28 22:59:32 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2017-09-08 12:16:58 +03:00
|
|
|
#include <sys/resource.h>
|
2003-09-28 22:59:32 +04:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <setjmp.h>
|
2004-02-22 17:54:18 +03:00
|
|
|
#include <sys/shm.h>
|
2018-06-08 22:15:03 +03:00
|
|
|
#include <assert.h>
|
2003-09-28 22:59:32 +04:00
|
|
|
|
|
|
|
#define STACK_SIZE 16384
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void error1(const char *filename, int line, const char *fmt, ...)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "%s:%d: ", filename, line);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static int __chk_error(const char *filename, int line, int ret)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
if (ret < 0) {
|
2018-04-19 16:02:04 +03:00
|
|
|
error1(filename, line, "%m (ret=%d, errno=%d/%s)",
|
|
|
|
ret, errno, strerror(errno));
|
2003-09-28 22:59:32 +04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-05-13 21:53:17 +04:00
|
|
|
#define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
|
2003-09-28 22:59:32 +04:00
|
|
|
|
|
|
|
#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
|
|
|
|
|
|
|
|
/*******************************************************/
|
|
|
|
|
|
|
|
#define FILE_BUF_SIZE 300
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void test_file(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
int fd, i, len, ret;
|
|
|
|
uint8_t buf[FILE_BUF_SIZE];
|
|
|
|
uint8_t buf2[FILE_BUF_SIZE];
|
|
|
|
uint8_t buf3[FILE_BUF_SIZE];
|
|
|
|
char cur_dir[1024];
|
|
|
|
struct stat st;
|
|
|
|
struct utimbuf tbuf;
|
|
|
|
struct iovec vecs[2];
|
|
|
|
DIR *dir;
|
2018-11-13 20:54:37 +03:00
|
|
|
struct dirent64 *de;
|
2018-06-08 22:15:03 +03:00
|
|
|
/* TODO: make common tempdir creation for tcg tests */
|
|
|
|
char template[] = "/tmp/linux-test-XXXXXX";
|
|
|
|
char *tmpdir = mkdtemp(template);
|
2003-09-28 22:59:32 +04:00
|
|
|
|
2018-06-08 22:15:03 +03:00
|
|
|
assert(tmpdir);
|
2003-09-28 22:59:32 +04:00
|
|
|
|
|
|
|
if (getcwd(cur_dir, sizeof(cur_dir)) == NULL)
|
|
|
|
error("getcwd");
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2018-06-08 22:15:03 +03:00
|
|
|
chk_error(chdir(tmpdir));
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
/* open/read/write/close/readv/writev/lseek */
|
|
|
|
|
|
|
|
fd = chk_error(open("file1", O_WRONLY | O_TRUNC | O_CREAT, 0644));
|
|
|
|
for(i=0;i < FILE_BUF_SIZE; i++)
|
|
|
|
buf[i] = i;
|
|
|
|
len = chk_error(write(fd, buf, FILE_BUF_SIZE / 2));
|
|
|
|
if (len != (FILE_BUF_SIZE / 2))
|
|
|
|
error("write");
|
|
|
|
vecs[0].iov_base = buf + (FILE_BUF_SIZE / 2);
|
|
|
|
vecs[0].iov_len = 16;
|
|
|
|
vecs[1].iov_base = buf + (FILE_BUF_SIZE / 2) + 16;
|
|
|
|
vecs[1].iov_len = (FILE_BUF_SIZE / 2) - 16;
|
|
|
|
len = chk_error(writev(fd, vecs, 2));
|
|
|
|
if (len != (FILE_BUF_SIZE / 2))
|
|
|
|
error("writev");
|
|
|
|
chk_error(close(fd));
|
|
|
|
|
|
|
|
chk_error(rename("file1", "file2"));
|
|
|
|
|
|
|
|
fd = chk_error(open("file2", O_RDONLY));
|
|
|
|
|
|
|
|
len = chk_error(read(fd, buf2, FILE_BUF_SIZE));
|
|
|
|
if (len != FILE_BUF_SIZE)
|
|
|
|
error("read");
|
|
|
|
if (memcmp(buf, buf2, FILE_BUF_SIZE) != 0)
|
|
|
|
error("memcmp");
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
#define FOFFSET 16
|
|
|
|
ret = chk_error(lseek(fd, FOFFSET, SEEK_SET));
|
|
|
|
if (ret != 16)
|
|
|
|
error("lseek");
|
|
|
|
vecs[0].iov_base = buf3;
|
|
|
|
vecs[0].iov_len = 32;
|
|
|
|
vecs[1].iov_base = buf3 + 32;
|
|
|
|
vecs[1].iov_len = FILE_BUF_SIZE - FOFFSET - 32;
|
|
|
|
len = chk_error(readv(fd, vecs, 2));
|
|
|
|
if (len != FILE_BUF_SIZE - FOFFSET)
|
|
|
|
error("readv");
|
|
|
|
if (memcmp(buf + FOFFSET, buf3, FILE_BUF_SIZE - FOFFSET) != 0)
|
|
|
|
error("memcmp");
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
chk_error(close(fd));
|
|
|
|
|
|
|
|
/* access */
|
|
|
|
chk_error(access("file2", R_OK));
|
|
|
|
|
|
|
|
/* stat/chmod/utime/truncate */
|
|
|
|
|
|
|
|
chk_error(chmod("file2", 0600));
|
|
|
|
tbuf.actime = 1001;
|
|
|
|
tbuf.modtime = 1000;
|
|
|
|
chk_error(truncate("file2", 100));
|
|
|
|
chk_error(utime("file2", &tbuf));
|
|
|
|
chk_error(stat("file2", &st));
|
|
|
|
if (st.st_size != 100)
|
|
|
|
error("stat size");
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
error("stat mode");
|
|
|
|
if ((st.st_mode & 0777) != 0600)
|
|
|
|
error("stat mode2");
|
|
|
|
if (st.st_atime != 1001 ||
|
|
|
|
st.st_mtime != 1000)
|
|
|
|
error("stat time");
|
|
|
|
|
2018-06-08 22:15:03 +03:00
|
|
|
chk_error(stat(tmpdir, &st));
|
2003-09-28 22:59:32 +04:00
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
error("stat mode");
|
|
|
|
|
|
|
|
/* fstat */
|
|
|
|
fd = chk_error(open("file2", O_RDWR));
|
|
|
|
chk_error(ftruncate(fd, 50));
|
|
|
|
chk_error(fstat(fd, &st));
|
|
|
|
chk_error(close(fd));
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
if (st.st_size != 50)
|
|
|
|
error("stat size");
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
error("stat mode");
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
/* symlink/lstat */
|
|
|
|
chk_error(symlink("file2", "file3"));
|
|
|
|
chk_error(lstat("file3", &st));
|
|
|
|
if (!S_ISLNK(st.st_mode))
|
|
|
|
error("stat mode");
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
/* getdents */
|
2018-06-08 22:15:03 +03:00
|
|
|
dir = opendir(tmpdir);
|
2003-09-28 22:59:32 +04:00
|
|
|
if (!dir)
|
|
|
|
error("opendir");
|
|
|
|
len = 0;
|
|
|
|
for(;;) {
|
2018-11-13 20:54:37 +03:00
|
|
|
de = readdir64(dir);
|
2003-09-28 22:59:32 +04:00
|
|
|
if (!de)
|
|
|
|
break;
|
|
|
|
if (strcmp(de->d_name, ".") != 0 &&
|
|
|
|
strcmp(de->d_name, "..") != 0 &&
|
|
|
|
strcmp(de->d_name, "file2") != 0 &&
|
|
|
|
strcmp(de->d_name, "file3") != 0)
|
|
|
|
error("readdir");
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
if (len != 4)
|
|
|
|
error("readdir");
|
|
|
|
|
|
|
|
chk_error(unlink("file3"));
|
|
|
|
chk_error(unlink("file2"));
|
|
|
|
chk_error(chdir(cur_dir));
|
2018-06-08 22:15:03 +03:00
|
|
|
chk_error(rmdir(tmpdir));
|
2003-09-28 22:59:32 +04:00
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void test_fork(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
int pid, status;
|
|
|
|
|
|
|
|
pid = chk_error(fork());
|
|
|
|
if (pid == 0) {
|
|
|
|
/* child */
|
2018-04-19 16:02:04 +03:00
|
|
|
sleep(2);
|
2003-09-28 22:59:32 +04:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
chk_error(waitpid(pid, &status, 0));
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 2)
|
|
|
|
error("waitpid status=0x%x", status);
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void test_time(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
struct timeval tv, tv2;
|
|
|
|
struct timespec ts, rem;
|
|
|
|
struct rusage rusg1, rusg2;
|
|
|
|
int ti, i;
|
|
|
|
|
|
|
|
chk_error(gettimeofday(&tv, NULL));
|
|
|
|
rem.tv_sec = 1;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 20 * 1000000;
|
|
|
|
chk_error(nanosleep(&ts, &rem));
|
|
|
|
if (rem.tv_sec != 1)
|
|
|
|
error("nanosleep");
|
|
|
|
chk_error(gettimeofday(&tv2, NULL));
|
|
|
|
ti = tv2.tv_sec - tv.tv_sec;
|
|
|
|
if (ti >= 2)
|
|
|
|
error("gettimeofday");
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
chk_error(getrusage(RUSAGE_SELF, &rusg1));
|
|
|
|
for(i = 0;i < 10000; i++);
|
|
|
|
chk_error(getrusage(RUSAGE_SELF, &rusg2));
|
|
|
|
if ((rusg2.ru_utime.tv_sec - rusg1.ru_utime.tv_sec) < 0 ||
|
|
|
|
(rusg2.ru_stime.tv_sec - rusg1.ru_stime.tv_sec) < 0)
|
|
|
|
error("getrusage");
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static int server_socket(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
int val, fd;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
|
|
|
|
/* server socket */
|
|
|
|
fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
|
|
|
|
|
|
|
|
val = 1;
|
|
|
|
chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)));
|
|
|
|
|
|
|
|
sockaddr.sin_family = AF_INET;
|
2018-06-08 22:15:03 +03:00
|
|
|
sockaddr.sin_port = htons(0); /* choose random ephemeral port) */
|
2003-09-28 22:59:32 +04:00
|
|
|
sockaddr.sin_addr.s_addr = 0;
|
|
|
|
chk_error(bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
|
|
|
|
chk_error(listen(fd, 0));
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-08 22:15:03 +03:00
|
|
|
static int client_socket(uint16_t port)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
|
|
|
|
/* server socket */
|
|
|
|
fd = chk_error(socket(PF_INET, SOCK_STREAM, 0));
|
|
|
|
sockaddr.sin_family = AF_INET;
|
2018-06-08 22:15:03 +03:00
|
|
|
sockaddr.sin_port = htons(port);
|
2003-09-28 22:59:32 +04:00
|
|
|
inet_aton("127.0.0.1", &sockaddr.sin_addr);
|
|
|
|
chk_error(connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)));
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static const char socket_msg[] = "hello socket\n";
|
2003-09-28 22:59:32 +04:00
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void test_socket(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
2004-02-22 17:54:18 +03:00
|
|
|
int server_fd, client_fd, fd, pid, ret, val;
|
2003-09-28 22:59:32 +04:00
|
|
|
struct sockaddr_in sockaddr;
|
2018-06-08 22:15:03 +03:00
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
socklen_t len, socklen;
|
|
|
|
uint16_t server_port;
|
2003-09-28 22:59:32 +04:00
|
|
|
char buf[512];
|
|
|
|
|
|
|
|
server_fd = server_socket();
|
2018-06-08 22:15:03 +03:00
|
|
|
/* find out what port we got */
|
|
|
|
socklen = sizeof(server_addr);
|
2020-10-05 19:41:26 +03:00
|
|
|
ret = getsockname(server_fd, (struct sockaddr *)&server_addr, &socklen);
|
2018-06-08 22:15:03 +03:00
|
|
|
chk_error(ret);
|
|
|
|
server_port = ntohs(server_addr.sin_port);
|
2004-02-22 17:54:18 +03:00
|
|
|
|
|
|
|
/* test a few socket options */
|
|
|
|
len = sizeof(val);
|
|
|
|
chk_error(getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &val, &len));
|
|
|
|
if (val != SOCK_STREAM)
|
|
|
|
error("getsockopt");
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
pid = chk_error(fork());
|
|
|
|
if (pid == 0) {
|
2018-06-08 22:15:03 +03:00
|
|
|
client_fd = client_socket(server_port);
|
2003-09-28 22:59:32 +04:00
|
|
|
send(client_fd, socket_msg, sizeof(socket_msg), 0);
|
|
|
|
close(client_fd);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
len = sizeof(sockaddr);
|
|
|
|
fd = chk_error(accept(server_fd, (struct sockaddr *)&sockaddr, &len));
|
|
|
|
|
|
|
|
ret = chk_error(recv(fd, buf, sizeof(buf), 0));
|
|
|
|
if (ret != sizeof(socket_msg))
|
|
|
|
error("recv");
|
|
|
|
if (memcmp(buf, socket_msg, sizeof(socket_msg)) != 0)
|
|
|
|
error("socket_msg");
|
|
|
|
chk_error(close(fd));
|
|
|
|
chk_error(close(server_fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define WCOUNT_MAX 512
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void test_pipe(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
fd_set rfds, wfds;
|
|
|
|
int fds[2], fd_max, ret;
|
|
|
|
uint8_t ch;
|
|
|
|
int wcount, rcount;
|
|
|
|
|
|
|
|
chk_error(pipe(fds));
|
|
|
|
chk_error(fcntl(fds[0], F_SETFL, O_NONBLOCK));
|
|
|
|
chk_error(fcntl(fds[1], F_SETFL, O_NONBLOCK));
|
|
|
|
wcount = 0;
|
|
|
|
rcount = 0;
|
|
|
|
for(;;) {
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
fd_max = fds[0];
|
|
|
|
FD_SET(fds[0], &rfds);
|
|
|
|
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(fds[1], &wfds);
|
|
|
|
if (fds[1] > fd_max)
|
|
|
|
fd_max = fds[1];
|
|
|
|
|
|
|
|
ret = chk_error(select(fd_max + 1, &rfds, &wfds, NULL, NULL));
|
|
|
|
if (ret > 0) {
|
|
|
|
if (FD_ISSET(fds[0], &rfds)) {
|
|
|
|
chk_error(read(fds[0], &ch, 1));
|
|
|
|
rcount++;
|
|
|
|
if (rcount >= WCOUNT_MAX)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (FD_ISSET(fds[1], &wfds)) {
|
|
|
|
ch = 'a';
|
2018-04-19 16:02:04 +03:00
|
|
|
chk_error(write(fds[1], &ch, 1));
|
2003-09-28 22:59:32 +04:00
|
|
|
wcount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chk_error(close(fds[0]));
|
|
|
|
chk_error(close(fds[1]));
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static int thread1_res;
|
|
|
|
static int thread2_res;
|
2003-09-28 22:59:32 +04:00
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static int thread1_func(void *arg)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<5;i++) {
|
|
|
|
thread1_res++;
|
|
|
|
usleep(10 * 1000);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static int thread2_func(void *arg)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<6;i++) {
|
|
|
|
thread2_res++;
|
|
|
|
usleep(10 * 1000);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:02:04 +03:00
|
|
|
static void wait_for_child(pid_t pid)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
chk_error(waitpid(pid, &status, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For test_clone we must match the clone flags used by glibc, see
|
|
|
|
* CLONE_THREAD_FLAGS in the QEMU source code.
|
|
|
|
*/
|
|
|
|
static void test_clone(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
uint8_t *stack1, *stack2;
|
2018-04-19 16:02:04 +03:00
|
|
|
pid_t pid1, pid2;
|
2003-09-28 22:59:32 +04:00
|
|
|
|
|
|
|
stack1 = malloc(STACK_SIZE);
|
2007-09-17 01:08:06 +04:00
|
|
|
pid1 = chk_error(clone(thread1_func, stack1 + STACK_SIZE,
|
2018-04-19 16:02:04 +03:00
|
|
|
CLONE_VM | CLONE_FS | CLONE_FILES |
|
|
|
|
CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM,
|
|
|
|
"hello1"));
|
2003-09-28 22:59:32 +04:00
|
|
|
|
|
|
|
stack2 = malloc(STACK_SIZE);
|
2007-09-17 01:08:06 +04:00
|
|
|
pid2 = chk_error(clone(thread2_func, stack2 + STACK_SIZE,
|
2018-04-19 16:02:04 +03:00
|
|
|
CLONE_VM | CLONE_FS | CLONE_FILES |
|
|
|
|
CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM,
|
|
|
|
"hello2"));
|
2003-09-28 22:59:32 +04:00
|
|
|
|
2018-04-19 16:02:04 +03:00
|
|
|
wait_for_child(pid1);
|
2011-01-22 00:49:29 +03:00
|
|
|
free(stack1);
|
2018-04-19 16:02:04 +03:00
|
|
|
wait_for_child(pid2);
|
2011-01-22 00:49:29 +03:00
|
|
|
free(stack2);
|
2018-04-19 16:02:04 +03:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
if (thread1_res != 5 ||
|
|
|
|
thread2_res != 6)
|
|
|
|
error("clone");
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************/
|
|
|
|
|
|
|
|
volatile int alarm_count;
|
|
|
|
jmp_buf jmp_env;
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void sig_alarm(int sig)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
if (sig != SIGALRM)
|
|
|
|
error("signal");
|
|
|
|
alarm_count++;
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void sig_segv(int sig, siginfo_t *info, void *puc)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
if (sig != SIGSEGV)
|
|
|
|
error("signal");
|
|
|
|
longjmp(jmp_env, 1);
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void test_signal(void)
|
2003-09-28 22:59:32 +04:00
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
struct itimerval it, oit;
|
|
|
|
|
|
|
|
/* timer test */
|
|
|
|
|
|
|
|
alarm_count = 0;
|
|
|
|
|
|
|
|
act.sa_handler = sig_alarm;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
chk_error(sigaction(SIGALRM, &act, NULL));
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
it.it_interval.tv_sec = 0;
|
|
|
|
it.it_interval.tv_usec = 10 * 1000;
|
|
|
|
it.it_value.tv_sec = 0;
|
|
|
|
it.it_value.tv_usec = 10 * 1000;
|
|
|
|
chk_error(setitimer(ITIMER_REAL, &it, NULL));
|
|
|
|
chk_error(getitimer(ITIMER_REAL, &oit));
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
while (alarm_count < 5) {
|
|
|
|
usleep(10 * 1000);
|
2018-04-19 16:02:04 +03:00
|
|
|
getitimer(ITIMER_REAL, &oit);
|
2003-09-28 22:59:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
it.it_interval.tv_sec = 0;
|
|
|
|
it.it_interval.tv_usec = 0;
|
|
|
|
it.it_value.tv_sec = 0;
|
|
|
|
it.it_value.tv_usec = 0;
|
|
|
|
memset(&oit, 0xff, sizeof(oit));
|
|
|
|
chk_error(setitimer(ITIMER_REAL, &it, &oit));
|
|
|
|
|
|
|
|
/* SIGSEGV test */
|
|
|
|
act.sa_sigaction = sig_segv;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
chk_error(sigaction(SIGSEGV, &act, NULL));
|
|
|
|
if (setjmp(jmp_env) == 0) {
|
2019-11-18 02:14:25 +03:00
|
|
|
/*
|
|
|
|
* clang requires volatile or it will turn this into a
|
|
|
|
* call to abort() instead of forcing a SIGSEGV.
|
|
|
|
*/
|
|
|
|
*(volatile uint8_t *)0 = 0;
|
2003-09-28 22:59:32 +04:00
|
|
|
}
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
chk_error(sigaction(SIGSEGV, &act, NULL));
|
|
|
|
}
|
|
|
|
|
2004-02-22 17:54:18 +03:00
|
|
|
#define SHM_SIZE 32768
|
|
|
|
|
2017-09-08 12:16:58 +03:00
|
|
|
static void test_shm(void)
|
2004-02-22 17:54:18 +03:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
int shmid;
|
|
|
|
|
|
|
|
shmid = chk_error(shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0777));
|
|
|
|
ptr = shmat(shmid, NULL, 0);
|
2019-11-06 14:33:08 +03:00
|
|
|
if (ptr == (void *)-1) {
|
2004-02-22 17:54:18 +03:00
|
|
|
error("shmat");
|
2019-11-06 14:33:08 +03:00
|
|
|
}
|
2004-02-22 17:54:18 +03:00
|
|
|
|
|
|
|
memset(ptr, 0, SHM_SIZE);
|
|
|
|
|
|
|
|
chk_error(shmctl(shmid, IPC_RMID, 0));
|
|
|
|
chk_error(shmdt(ptr));
|
|
|
|
}
|
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2003-10-28 02:58:04 +03:00
|
|
|
test_file();
|
2018-04-19 16:02:04 +03:00
|
|
|
test_pipe();
|
2003-09-28 22:59:32 +04:00
|
|
|
test_fork();
|
|
|
|
test_time();
|
|
|
|
test_socket();
|
2018-04-19 16:02:04 +03:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
printf("test_clone still considered buggy\n");
|
|
|
|
test_clone();
|
|
|
|
}
|
|
|
|
|
2003-09-28 22:59:32 +04:00
|
|
|
test_signal();
|
2004-02-22 17:54:18 +03:00
|
|
|
test_shm();
|
2003-09-28 22:59:32 +04:00
|
|
|
return 0;
|
|
|
|
}
|