提交 fcb21ffc 编写于 作者: O openharmony_ci 提交者: Gitee

!676 修复ppoll接口"[ERR]OsMemFree check error!"报错

Merge pull request !676 from pef/ppoll-1
...@@ -215,28 +215,22 @@ void OsSigMaskSwitch(LosTaskCB * const rtcb, sigset_t set) ...@@ -215,28 +215,22 @@ void OsSigMaskSwitch(LosTaskCB * const rtcb, sigset_t set)
} }
} }
int OsSigprocMask(int how, const sigset_t_l *setl, sigset_t_l *oldset) int OsSigprocMask(int how, const sigset_t_l *setl, sigset_t_l *oldsetl)
{ {
LosTaskCB *spcb = NULL; LosTaskCB *spcb = NULL;
sigset_t oldSigprocmask;
int ret = LOS_OK; int ret = LOS_OK;
unsigned int intSave; unsigned int intSave;
sigset_t set; sigset_t set;
int retVal;
if (setl != NULL) {
retVal = LOS_ArchCopyFromUser(&set, &(setl->sig[0]), sizeof(sigset_t));
if (retVal != 0) {
return -EFAULT;
}
}
SCHEDULER_LOCK(intSave); SCHEDULER_LOCK(intSave);
spcb = OsCurrTaskGet(); spcb = OsCurrTaskGet();
/* If requested, copy the old mask to user. */ /* If requested, copy the old mask to user. */
oldSigprocmask = spcb->sig.sigprocmask; if (oldsetl != NULL) {
*(sigset_t *)oldsetl = spcb->sig.sigprocmask;
}
/* If requested, modify the current signal mask. */ /* If requested, modify the current signal mask. */
if (setl != NULL) { if (setl != NULL) {
set = *(sigset_t *)setl;
/* Okay, determine what we are supposed to do */ /* Okay, determine what we are supposed to do */
switch (how) { switch (how) {
/* Set the union of the current set and the signal /* Set the union of the current set and the signal
...@@ -264,12 +258,6 @@ int OsSigprocMask(int how, const sigset_t_l *setl, sigset_t_l *oldset) ...@@ -264,12 +258,6 @@ int OsSigprocMask(int how, const sigset_t_l *setl, sigset_t_l *oldset)
} }
SCHEDULER_UNLOCK(intSave); SCHEDULER_UNLOCK(intSave);
if (oldset != NULL) {
retVal = LOS_ArchCopyToUser(&(oldset->sig[0]), &oldSigprocmask, sizeof(sigset_t));
if (retVal != 0) {
return -EFAULT;
}
}
return ret; return ret;
} }
......
...@@ -2516,37 +2516,36 @@ int SysFstatfs64(int fd, size_t sz, struct statfs *buf) ...@@ -2516,37 +2516,36 @@ int SysFstatfs64(int fd, size_t sz, struct statfs *buf)
int SysPpoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigMask, int nsig) int SysPpoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigMask, int nsig)
{ {
int timeout; int timeout, retVal;
int ret; sigset_t_l origMask = {0};
sigset_t_l origMask; sigset_t_l set = {0};
sigset_t_l setl;
if (sigMask == NULL) {
ret = -EINVAL;
return ret;
}
CHECK_ASPACE(tmo_p, sizeof(struct timespec)); CHECK_ASPACE(tmo_p, sizeof(struct timespec));
CHECK_ASPACE(sigMask, sizeof(sigset_t));
CPY_FROM_USER(tmo_p); CPY_FROM_USER(tmo_p);
CPY_FROM_USER(sigMask);
timeout = (tmo_p == NULL) ? -1 : (tmo_p->tv_sec * OS_SYS_US_PER_MS + tmo_p->tv_nsec / OS_SYS_NS_PER_MS); if (tmo_p != NULL) {
if (timeout & 0x80000000) { timeout = tmo_p->tv_sec * OS_SYS_US_PER_MS + tmo_p->tv_nsec / OS_SYS_NS_PER_MS;
ret = -EINVAL; if (timeout < 0) {
return ret; return -EINVAL;
}
} else {
timeout = -1;
} }
setl.sig[0] = *sigMask;
OsSigprocMask(SIG_SETMASK, &setl, &origMask); if (sigMask != NULL) {
ret = SysPoll(fds, nfds, timeout); retVal = LOS_ArchCopyFromUser(&set, sigMask, sizeof(sigset_t));
if (ret < 0) { if (retVal != 0) {
ret = -get_errno(); return -EFAULT;
}
(VOID)OsSigprocMask(SIG_SETMASK, &set, &origMask);
} else {
(VOID)OsSigprocMask(SIG_SETMASK, NULL, &origMask);
} }
OsSigprocMask(SIG_SETMASK, &origMask, NULL);
PointerFree(tmo_pbak); retVal = SysPoll(fds, nfds, timeout);
PointerFree(sigMaskbak); (VOID)OsSigprocMask(SIG_SETMASK, &origMask, NULL);
return (ret == -1) ? -get_errno() : ret;
return retVal;
} }
int SysPselect6(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, int SysPselect6(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "syscall_pub.h"
#include "mqueue.h" #include "mqueue.h"
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -36,6 +37,7 @@ ...@@ -36,6 +37,7 @@
#include "time_posix.h" #include "time_posix.h"
#include "user_copy.h" #include "user_copy.h"
#include "los_signal.h" #include "los_signal.h"
#include "los_process_pri.h"
#include "los_strncpy_from_user.h" #include "los_strncpy_from_user.h"
#include "fs/file.h" #include "fs/file.h"
...@@ -229,8 +231,14 @@ int SysSigAction(int sig, const sigaction_t *restrict sa, sigaction_t *restrict ...@@ -229,8 +231,14 @@ int SysSigAction(int sig, const sigaction_t *restrict sa, sigaction_t *restrict
int SysSigprocMask(int how, const sigset_t_l *restrict setl, sigset_t_l *restrict oldl, size_t sigsetsize) int SysSigprocMask(int how, const sigset_t_l *restrict setl, sigset_t_l *restrict oldl, size_t sigsetsize)
{ {
/* Let nxsig_procmask do all of the work */ CHECK_ASPACE(setl, sizeof(sigset_t_l));
return OsSigprocMask(how, setl, oldl); CHECK_ASPACE(oldl, sizeof(sigset_t_l));
CPY_FROM_USER(setl);
CPY_FROM_USER(oldl);
/* Let OsSigprocMask do all of the work */
int ret = OsSigprocMask(how, setl, oldl);
CPY_TO_USER(oldl);
return ret;
} }
int SysKill(pid_t pid, int sig) int SysKill(pid_t pid, int sig)
......
...@@ -110,7 +110,7 @@ Please deal with the "char *" by function:UserPathCopy. ...@@ -110,7 +110,7 @@ Please deal with the "char *" by function:UserPathCopy.
#define CPY_FROM_USER(ptr) \ #define CPY_FROM_USER(ptr) \
__typeof(*ptr) ptr##cpy = {0}, *ptr##bak = ptr; \ __typeof(*ptr) ptr##cpy = {0}, *ptr##bak = ptr; \
if (ptr != NULL) { \ if (ptr != NULL) { \
if (LOS_ArchCopyFromUser((void*)&ptr##cpy, ptr, sizeof(*ptr)) != 0) { \ if (LOS_ArchCopyFromUser((void*)&ptr##cpy, ptr##bak, sizeof(*ptr##bak)) != 0) { \
set_errno(EFAULT); \ set_errno(EFAULT); \
return -get_errno(); \ return -get_errno(); \
} \ } \
......
...@@ -85,6 +85,7 @@ sources_full = [ ...@@ -85,6 +85,7 @@ sources_full = [
"full/IO_test_strncasecmp_l_002.cpp", "full/IO_test_strncasecmp_l_002.cpp",
"full/IO_test_ppoll_001.cpp", "full/IO_test_ppoll_001.cpp",
"full/IO_test_ppoll_002.cpp", "full/IO_test_ppoll_002.cpp",
"full/IO_test_ppoll_003.cpp",
"full/IO_test_pselect_001.cpp", "full/IO_test_pselect_001.cpp",
"full/IO_test_pselect_002.cpp", "full/IO_test_pselect_002.cpp",
] ]
......
...@@ -127,13 +127,12 @@ extern VOID IO_TEST_DCNGETTEXT_002(VOID); ...@@ -127,13 +127,12 @@ extern VOID IO_TEST_DCNGETTEXT_002(VOID);
extern VOID IO_TEST_DCGETTEXT_001(VOID); extern VOID IO_TEST_DCGETTEXT_001(VOID);
extern VOID IO_TEST_DCGETTEXT_002(VOID); extern VOID IO_TEST_DCGETTEXT_002(VOID);
extern VOID IO_TEST_GETTEXT_001(VOID); extern VOID IO_TEST_GETTEXT_001(VOID);
extern VOID IO_TEST_PPOLL_001(void);
extern VOID IO_TEST_PPOLL_002(void);
extern VOID IO_TEST_PSELECT_001(void); extern VOID IO_TEST_PSELECT_001(void);
extern VOID IO_TEST_PSELECT_002(void); extern VOID IO_TEST_PSELECT_002(void);
extern VOID IO_TEST_STRFMON_L_001(VOID); extern VOID IO_TEST_STRFMON_L_001(VOID);
extern VOID IO_TEST_STRFMON_L_002(VOID); extern VOID IO_TEST_STRFMON_L_002(VOID);
extern VOID IO_TEST_PPOLL_001(VOID); extern VOID IO_TEST_PPOLL_001(VOID);
extern VOID IO_TEST_PPOLL_002(VOID); extern VOID IO_TEST_PPOLL_002(VOID);
extern VOID IO_TEST_PPOLL_003(VOID);
#endif #endif
...@@ -30,143 +30,91 @@ ...@@ -30,143 +30,91 @@
*/ */
#include "It_test_IO.h" #include "It_test_IO.h"
#include <poll.h>
#include "signal.h"
#include "pthread.h" #include "pthread.h"
#define LISTEN_FD_NUM 10 const int BUF_SIZE = 128;
#define POLL_EVENTS 1 const int DELAY_TIME = 200;
int pipeFdPpoll[LISTEN_FD_NUM][2]; static INT32 pipeFdPpoll[2];
static pthread_t g_tid = -1; static INT32 g_step = 1;
extern int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigmask); static CHAR strBuf[] = "hello world.";
static struct pollfd pfd;
static void *pthread_01(void) static void *pthread_01(void *arg)
{ {
static int count = 0; INT32 retVal;
int total_num = 0; CHAR buf[BUF_SIZE];
int times = 3;
int i, ret; /* 执行ppoll监视文件描述符 */
struct pollfd fds[LISTEN_FD_NUM] = { 0 }; while (g_step < 3) { /* 3, 3rd step */
char buffer[20]; usleep(DELAY_TIME);
struct timespec t = { 3,0 };
sigset_t sigset;
/* TEST_PRINT("[INFO]%s:%d,%s,Create thread %d\n", __FILE__, __LINE__, __func__, count); */
count++;
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM); /* 把SIGALRM 信号添加到sigset 信号集中 */
sigaddset(&sigset, SIGUSR1);
for (i = 0; i < LISTEN_FD_NUM; i++) {
fds[i].fd = pipeFdPpoll[i][0];
fds[i].events = POLL_EVENTS;
} }
g_step++;
while (times--) { retVal = ppoll(&pfd, 1, NULL, NULL);
ret = ppoll(fds, LISTEN_FD_NUM, &t, &sigset); ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
total_num += ((ret > 0) ? ret : 0);
while (g_step < 5) { /* 5, 5th step */
if (ret <= 0) { usleep(DELAY_TIME);
continue;
}
for (i = 0; i < LISTEN_FD_NUM; i++) {
if (fds[i].revents & POLL_EVENTS) {
ret = read(fds[i].fd, buffer, 12);
ICUNIT_GOTO_EQUAL(ret, 12, ret, EXIT);
ret = strcmp(buffer, "hello world");
TEST_PRINT("[EVENT]%s:%d,%s,buffer=%s\n", __FILE__, __LINE__, __func__, buffer);
}
}
if (total_num == LISTEN_FD_NUM) {
break;
}
} }
g_step++;
/* ICUNIT_GOTO_EQUAL(total_num, 10, -1, EXIT); */
/* TEST_PRINT("[INFO]%s:%d,%s,total_num=%d\n", __FILE__, __LINE__, __func__, total_num); */ /* 判断revents */
EXIT: if (pfd.revents & POLLIN) {
return nullptr; memset_s(buf, sizeof(buf), 0, sizeof(buf));
} retVal = read(pfd.fd, buf, BUF_SIZE);
ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
static UINT32 testcase1(VOID)
{ retVal = strcmp(strBuf, buf);
int i; ICUNIT_ASSERT_EQUAL_NULL(retVal, 0, retVal);
int ret;
int count = 0;
for (i = 0; i < LISTEN_FD_NUM; i++) {
ret = pipe(pipeFdPpoll[i]);
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,pipeFdPpoll[%d][0]=%d\n", __FILE__, __LINE__, __func__, ret, i, pipeFdPpoll[i][0]);
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,pipeFdPpoll[%d][0]=%d\n", __FILE__, __LINE__, __func__, ret, i, pipeFdPpoll[i][1]);
} }
errno = 0; while (g_step < 6) { /* 6, 6th step */
ret = pthread_create(&g_tid, nullptr, (void *(*)(void *))pthread_01, nullptr); usleep(DELAY_TIME);
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
errno = 0;
for (i = 0; i < LISTEN_FD_NUM; i++) {
if ((pipeFdPpoll[i][1] != 9) && (pipeFdPpoll[i][1] != 10) && (pipeFdPpoll[i][1] != 11)) {
ret = write(pipeFdPpoll[i][1], "hello world!", 12); /* 12, "hello world" length and '\0' */
}
ICUNIT_GOTO_EQUAL(ret, 12, ret, EXIT);
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
} }
pthread_exit(NULL);
}
#if 1 /* write looply */ STATIC UINT32 testcase(VOID)
while(1) { {
if (count++ > 3) { INT32 retVal;
break; pthread_t tid;
}
sleep(1); /* 建立管道 */
for (i = 0; i < LISTEN_FD_NUM; i++) { while (g_step < 1) { /* 1, 1st step */
errno = 0; usleep(DELAY_TIME);
ret = pthread_create(&g_tid, nullptr, (void *(*)(void *))pthread_01, nullptr);
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
if ((pipeFdPpoll[i][1] == 13) || (pipeFdPpoll[i][1] == 14) || (pipeFdPpoll[i][1] == 15)) {
errno = 0;
ret = write(pipeFdPpoll[i][1], "World HELLO!", 12); /* 12, "hello world" length and '\0' */
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,errno=%d,errstr=%s\n", __FILE__, __LINE__, __func__, ret, errno, strerror(errno));
TEST_PRINT("[INFO]%s:%d,%s,ret=%d,pipeFdPpoll[%d][1]=%d,count=%d\n", __FILE__, __LINE__, __func__, ret,i, pipeFdPpoll[i][1], count);
}
/* ICUNIT_GOTO_EQUAL(ret, 12, ret, EXIT); */
}
} }
#endif retVal = pipe(pipeFdPpoll);
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
pthread_join(g_tid, nullptr); g_step++;
for (i = 0; i < LISTEN_FD_NUM; i++) { /* 设置pfd */
close(pipeFdPpoll[i][0]); pfd.fd = pipeFdPpoll[0];
close(pipeFdPpoll[i][1]); pfd.events = POLLIN;
/* 开辟线程执行 ppoll */
while (g_step < 2) { /* 2, 2nd step */
usleep(DELAY_TIME);
} }
retVal = pthread_create(&tid, NULL, pthread_01, NULL);
return LOS_OK; ICUNIT_ASSERT_EQUAL(retVal, 0, retVal);
g_step++;
EXIT:
for (i = 0; i < LISTEN_FD_NUM; i++) { /* 向管道写入数据 */
close(pipeFdPpoll[i][0]); while (g_step < 4) { /* 4, 4th step */
close(pipeFdPpoll[i][1]); usleep(DELAY_TIME);
} }
return LOS_NOK; sleep(1);
}
retVal = write(pipeFdPpoll[1], "hello world.", sizeof(strBuf));
static UINT32 testcase(VOID) ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
{ g_step++;
testcase1();
pthread_join(tid, NULL);
return LOS_OK; return LOS_OK;
} }
VOID IO_TEST_PPOLL_001(VOID) VOID IO_TEST_PPOLL_001(VOID)
{ {
TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION); TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
} }
\ No newline at end of file
...@@ -30,83 +30,87 @@ ...@@ -30,83 +30,87 @@
*/ */
#include "It_test_IO.h" #include "It_test_IO.h"
#include "time.h"
#include "signal.h" #include "signal.h"
#include <poll.h> #include "pthread.h"
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#define BUF_LEN 20 const int BUF_SIZE = 128;
#define MAX_SCAN_FDSET 10 /* define poll's listened FD set's size */ const int DELAY_TIME = 200;
#define POLL_WAIT_TIMEOUT 10*1000 /* ms */
extern int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask); static int pipeFdPpoll[2];
void work_ppoll(int fd) static int g_step = 1;
{ static char strBuf[] = "hello world.";
int ret = 0; static struct pollfd pfd;
int i = 0;
char recv_buf[BUF_LEN];
sigset_t sigset;
struct timespec t;
struct pollfd scan_fdset[MAX_SCAN_FDSET];
int scan_fdset_num = 10;
int count = 5;
bzero(recv_buf, BUF_LEN);
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM); /* add SIGALRM to sigset */
sigaddset(&sigset, SIGUSR1); /* add SIGUSR1 to sigset */
bzero(&t, sizeof(struct timespec));
t.tv_sec = 10;
t.tv_nsec = 0;
bzero(scan_fdset, sizeof(struct pollfd) * MAX_SCAN_FDSET);
scan_fdset[0].fd = fd;
/* attention:in the book《UNIX网络编程第一卷》P162 metions:POLLERR,POLLHUP,POLLNVAL\
those error signals can not be set in events. */
/* they will return in revents,while the proper condition happens. */
scan_fdset[0].events = POLLOUT; /* set the signal needed to be listened:POLLOUT/POLLIN */
/* set other elements in the array as invalid. */ static void *pthread_01(void *arg)
for (i = 1; i < MAX_SCAN_FDSET; i++) { {
/* scan_fdset[i].fd = -1; */ int retVal;
scan_fdset[i].fd = fd; char buf[BUF_SIZE];
scan_fdset[i].events = POLLOUT; /* set the signal needed to be listened. */ const struct timespec timeout = {10000, 0};
/* 执行ppoll监视文件描述符 */
while (g_step < 4) { /* 4, 4th step */
usleep(DELAY_TIME);
} }
/* scan_fdset_num = 1; */ /* 表示当前的scan_fdset[] 数组中只使用前面1 个元素存放需要监听的扫描符 */ g_step++;
retVal = ppoll(&pfd, 1, &timeout, NULL);
while (count--) { ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
ret = ppoll(scan_fdset, scan_fdset_num, &t, &sigset);
/* 判断revents */
for (i = 0; i < MAX_SCAN_FDSET; i++) { if (pfd.revents & POLLIN) {
if (scan_fdset[i].revents & POLLOUT) { memset_s(buf, sizeof(buf), 0, sizeof(buf));
TEST_PRINT("[INFO]%s:%d,%s,fd have signal!\n", __FILE__, __LINE__, __func__); retVal = read(pfd.fd, buf, BUF_SIZE);
ret = read(fd, recv_buf, BUF_LEN); ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
if (-1 == ret) { retVal = strcmp(strBuf, buf);
TEST_PRINT("[INFO]%s:%d,%s,read error!\n", __FILE__, __LINE__, __func__); ICUNIT_ASSERT_EQUAL_NULL(retVal, 0, retVal);
continue;
}
TEST_PRINT("[INFO]%s:%d,%s,recv_buf=%s\n", __FILE__, __LINE__, __func__, recv_buf);
}
TEST_PRINT("[INFO]%s:%d,%s,scan_fdset[i].revents=%d\n", __FILE__, __LINE__, __func__, scan_fdset[i].revents);
}
} }
while (g_step < 5) { /* 5, 5th step */
usleep(DELAY_TIME);
}
pthread_exit(NULL);
} }
static UINT32 testcase(VOID) static UINT32 testcase(VOID)
{ {
int fd; int retVal;
char *filename = FILEPATH_775; pthread_t tid;
/* 建立管道 */
while (g_step < 1) {
usleep(DELAY_TIME);
}
retVal = pipe(pipeFdPpoll);
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
g_step++;
/* 设置pfd */
pfd.fd = pipeFdPpoll[0];
pfd.events = POLLIN;
/* 向管道写入数据 */
while (g_step < 2) { /* 2, 2nd step */
usleep(DELAY_TIME);
}
sleep(1);
retVal = write(pipeFdPpoll[1], "hello world.", sizeof(strBuf));
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
g_step++;
fd = open(filename, O_RDWR); /* 开辟线程执行 ppoll */
TEST_PRINT("[INFO]%s:%d,%s,fd=%d\n", __FILE__, __LINE__, __func__, fd); while (g_step < 3) { /* 3, 3rd step */
work_ppoll(fd); usleep(DELAY_TIME);
}
retVal = pthread_create(&tid, NULL, pthread_01, NULL);
ICUNIT_ASSERT_EQUAL(retVal, 0, retVal);
g_step++;
pthread_join(tid, NULL);
return LOS_OK; return LOS_OK;
} }
VOID IO_TEST_PPOLL_002(VOID) VOID IO_TEST_PPOLL_002(VOID)
{ {
TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION); TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
} }
\ No newline at end of file
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "It_test_IO.h"
#include "pthread.h"
#include "signal.h"
const int BUF_SIZE = 128;
const int DELAY_TIME = 200;
static INT32 pipeFdPpoll[2];
static INT32 g_step = 1;
static CHAR strBuf[] = "hello world.";
static struct pollfd pfd;
static sigset_t sigMask;
static UINT32 count = 0;
static void signalHandle(INT32 sigNum)
{
g_step++;
return;
}
static void *pthread_01(void *arg)
{
INT32 retVal;
CHAR buf[BUF_SIZE];
(void)signal(SIGUSR1, signalHandle);
while (1) {
/* 执行ppoll监视文件描述符 */
while (g_step < 2) { /* 2, 2nd step */
usleep(DELAY_TIME);
}
g_step++;
retVal = ppoll(&pfd, 1, NULL, &sigMask);
ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
/* 判断revents */
if (pfd.revents & POLLIN) {
memset_s(buf, sizeof(buf), 0, sizeof(buf));
retVal = read(pfd.fd, buf, BUF_SIZE);
ICUNIT_ASSERT_NOT_EQUAL_NULL(retVal, -1, retVal);
retVal = strcmp(strBuf, buf);
ICUNIT_ASSERT_EQUAL_NULL(retVal, 0, retVal);
count++;
} else {
ICUNIT_ASSERT_NOT_EQUAL_NULL(pfd.revents & POLLIN, 0, pfd.revents & POLLIN);
}
g_step++;
if (g_step >= 7) { /* 7, 7th step */
ICUNIT_ASSERT_EQUAL_NULL(count, 2, count); /* 2, 2nd step */
pthread_exit(NULL);
}
}
return LOS_OK;
}
static UINT32 testcase(VOID)
{
INT32 retVal;
pthread_t tid;
/* 建立管道 */
while (g_step < 1) {
usleep(DELAY_TIME);
}
retVal = pipe(pipeFdPpoll);
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
/* 设置pfd sigmask */
pfd.fd = pipeFdPpoll[0];
pfd.events = POLLIN;
pfd.revents = 0x0;
sigemptyset(&sigMask);
sigaddset(&sigMask, SIGUSR1);
/* 开辟线程执行 ppoll */
retVal = pthread_create(&tid, NULL, pthread_01, NULL);
ICUNIT_ASSERT_EQUAL(retVal, 0, retVal);
g_step++;
/* 向管道写入数据 */
while (g_step < 3) { /* 3, 3ed step */
usleep(DELAY_TIME);
}
sleep(1); /* 保证先挂起再写入数据 */
retVal = write(pipeFdPpoll[1], "hello world.", sizeof(strBuf));
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
/* 向线程发送信号 */
while (g_step < 5) { /* 5, 5th step */
usleep(DELAY_TIME);
}
sleep(1); /* 保证先挂起再发送信号 */
retVal = pthread_kill(tid, SIGUSR1);
ICUNIT_ASSERT_EQUAL(retVal, 0, retVal);
/* 继续向管道写入数据 */
ICUNIT_ASSERT_EQUAL(g_step, 5, g_step); /* 5, sth。判断挂起解除之前信号没有被处理 */
retVal = write(pipeFdPpoll[1], "hello world.", sizeof(strBuf));
ICUNIT_ASSERT_NOT_EQUAL(retVal, -1, retVal);
while (g_step < 7) { /* 7, 7th step */
usleep(DELAY_TIME);
}
ICUNIT_ASSERT_EQUAL(count, 2, count); /* 2, 2nd step */
/* 等待退出 */
pthread_join(tid, NULL);
return LOS_OK;
}
VOID IO_TEST_PPOLL_003(VOID)
{
TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
}
\ No newline at end of file
...@@ -137,6 +137,17 @@ HWTEST_F(IoTest, IO_TEST_PPOLL_002, TestSize.Level0) ...@@ -137,6 +137,17 @@ HWTEST_F(IoTest, IO_TEST_PPOLL_002, TestSize.Level0)
IO_TEST_PPOLL_002(); IO_TEST_PPOLL_002();
} }
/* *
* @tc.name: IO_TEST_PPOLL_003
* @tc.desc: function for IoTest
* @tc.type: FUNC
* @tc.require: AR000EEMQ9
*/
HWTEST_F(IoTest, IO_TEST_PPOLL_003, TestSize.Level0)
{
IO_TEST_PPOLL_003();
}
/* * /* *
* @tc.name: IT_STDLIB_POLL_002 * @tc.name: IT_STDLIB_POLL_002
* @tc.desc: function for IoTest * @tc.desc: function for IoTest
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册