apps/examples/pipe: check POSIX-compliant behavior of NuttX's FIFOs

Adjust the test considering the POSIX-compliant behavior of the
NuttX's FIFO (mkfifo), which should block `open` for read-only
and write-only. This test's result is expected to be the same with
any other POSIX-compliant system.

This commit also:

 * Fix redirect test;
 * Use pthread instead `task_create` to be able to run this test
on POSIX-compliant systems;
 * General fixes regarding formatting and error messages;
This commit is contained in:
Tiago Medicci Serrano 2023-03-26 19:19:48 -03:00 committed by Xiang Xiao
parent f2819b71e5
commit 0a9b983e90
4 changed files with 361 additions and 215 deletions

View File

@ -50,6 +50,50 @@
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
/****************************************************************************
* Name: null_reader
****************************************************************************/
static void *null_reader(pthread_addr_t pvarg)
{
int fd;
/* Wait a bit */
printf("null_reader: started -- sleeping\n");
sleep(5);
/* Then open the FIFO for read access */
printf("null_reader: Opening FIFO for read access\n");
fd = open(FIFO_PATH2, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, \
"null_reader: Failed to open FIFO %s for reading, errno=%d\n",
FIFO_PATH2, errno);
return (void *)(uintptr_t)1;
}
/* Wait a bit more */
printf("null_reader: Opened %s for reading -- sleeping\n", FIFO_PATH2);
sleep(5);
/* Then close the FIFO */
printf("null_reader: Closing %s\n", FIFO_PATH2);
if (close(fd) != 0)
{
fprintf(stderr, "null_reader: close failed: %d\n", errno);
}
sleep(5);
printf("null_reader: Returning success\n");
return NULL;
}
/**************************************************************************** /****************************************************************************
* Name: null_writer * Name: null_writer
****************************************************************************/ ****************************************************************************/
@ -70,7 +114,7 @@ static void *null_writer(pthread_addr_t pvarg)
if (fd < 0) if (fd < 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"null_writer: Failed to open FIFO %s for writing, errno=%d\n", "null_writer: Failed to open FIFO %s for writing, errno=%d\n",
FIFO_PATH2, errno); FIFO_PATH2, errno);
return (void *)(uintptr_t)1; return (void *)(uintptr_t)1;
} }
@ -104,6 +148,7 @@ static void *null_writer(pthread_addr_t pvarg)
int interlock_test(void) int interlock_test(void)
{ {
pthread_t readerid;
pthread_t writerid; pthread_t writerid;
void *value; void *value;
char data[16]; char data[16];
@ -117,28 +162,25 @@ int interlock_test(void)
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"interlock_test: mkfifo failed with errno=%d\n", \ "interlock_test: mkfifo failed with errno=%d\n", errno);
errno);
return 1; return 1;
} }
/* Start the null_writer_thread */ /* Start the null_writer thread */
printf("interlock_test: Starting null_writer thread\n"); printf("interlock_test: Starting null_writer thread\n");
ret = pthread_create(&writerid, NULL, null_writer, NULL); ret = pthread_create(&writerid, NULL, null_writer, NULL);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"interlock_test: Failed to create null_writer thread, \ "interlock_test: Failed to create null_writer thread,"
error=%d\n", ret); "error=%d\n", ret);
ret = 2; ret = 2;
goto errout_with_fifo; goto errout_with_fifo;
} }
/* Open one end of the FIFO for reading. /* Open one end of the FIFO for reading. This open call should block until
* This open call should block until the * the null_writer thread opens the other end of the FIFO for writing.
* null_writer thread opens the other
* end of the FIFO for writing.
*/ */
printf("interlock_test: Opening FIFO for read access\n"); printf("interlock_test: Opening FIFO for read access\n");
@ -146,16 +188,15 @@ int interlock_test(void)
if (fd < 0) if (fd < 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"interlock_test: Failed to open FIFO %s for reading, \ "interlock_test: Failed to open FIFO %s for reading"
errno=%d\n", "errno=%d\n",
FIFO_PATH2, errno); FIFO_PATH2, errno);
ret = 3; ret = 3;
goto errout_with_thread; goto errout_with_null_writer_thread;
} }
/* Attempt to read one byte from the FIFO. /* Attempt to read one byte from the FIFO. This should return end-of-file
* This should return end-of-file because * because the null_writer closes the FIFO without writing anything.
* the null_writer closes the FIFO without writing anything.
*/ */
printf("interlock_test: Reading from %s\n", FIFO_PATH2); printf("interlock_test: Reading from %s\n", FIFO_PATH2);
@ -163,19 +204,21 @@ int interlock_test(void)
if (nbytes < 0) if (nbytes < 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"interlock_test: read failed, errno=%d\n", errno); "interlock_test: read failed, errno=%d\n", errno);
ret = 4; ret = 4;
goto errout_with_file; goto errout_with_file;
} }
else if (ret != 0) else if (ret != 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"interlock_test: Read %ld bytes of data -- aborting: %d\n", "interlock_test: Read %ld bytes of data -- aborting: %d\n",
(long)nbytes, errno); (long)nbytes, errno);
ret = 5; ret = 5;
goto errout_with_file; goto errout_with_file;
} }
printf("interlock_test: read returned\n");
/* Close the file */ /* Close the file */
printf("interlock_test: Closing %s\n", FIFO_PATH2); printf("interlock_test: Closing %s\n", FIFO_PATH2);
@ -191,7 +234,7 @@ int interlock_test(void)
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"interlock_test: pthread_join failed, error=%d\n", ret); "interlock_test: pthread_join failed, error=%d\n", ret);
ret = 6; ret = 6;
goto errout_with_fifo; goto errout_with_fifo;
} }
@ -205,10 +248,90 @@ int interlock_test(void)
} }
} }
/* unlink(FIFO_PATH2); */ /* Start the null_reader thread */
printf("interlock_test: Returning success\n"); printf("interlock_test: Starting null_reader thread\n");
return 0; ret = pthread_create(&readerid, NULL, null_reader, NULL);
if (ret != 0)
{
fprintf(stderr, \
"interlock_test: Failed to create null_reader thread,"
"error=%d\n", ret);
ret = 8;
goto errout_with_fifo;
}
/* Open one end of the FIFO for writing. This open call should block until
* the null_reader thread opens the other end of the FIFO for reading.
*/
printf("interlock_test: Opening FIFO for write access\n");
fd = open(FIFO_PATH2, O_WRONLY);
if (fd < 0)
{
fprintf(stderr, \
"interlock_test: Failed to open FIFO %s for writing,"
"errno=%d\n",
FIFO_PATH2, errno);
ret = 9;
goto errout_with_null_reader_thread;
}
/* Attempt to write one byte from the FIFO. This should return 0 bytes
* written because the null_reader closes the FIFO.
*/
printf("interlock_test: Writing to %s\n", FIFO_PATH2);
nbytes = write(fd, data, 16);
if (nbytes < 0)
{
fprintf(stderr, \
"interlock_test: write failed, errno=%d\n", errno);
ret = 10;
goto errout_with_file;
}
else if (ret != 0)
{
fprintf(stderr, \
"interlock_test: Wrote %ld bytes of data -- aborting: %d\n",
(long)nbytes, errno);
ret = 11;
goto errout_with_file;
}
printf("interlock_test: write returned\n");
/* Close the file */
printf("interlock_test: Closing %s\n", FIFO_PATH2);
if (close(fd) != 0)
{
fprintf(stderr, "interlock_test: close failed: %d\n", errno);
}
/* Wait for null_reader thread to complete */
printf("interlock_test: Waiting for null_reader thread\n");
ret = pthread_join(readerid, &value);
if (ret != 0)
{
fprintf(stderr, \
"interlock_test: pthread_join failed, error=%d\n", ret);
ret = 12;
goto errout_with_fifo;
}
else
{
printf("interlock_test: reader returned %p\n", value);
if (value != NULL)
{
ret = 13;
goto errout_with_fifo;
}
}
ret = 0;
goto errout_with_fifo;
errout_with_file: errout_with_file:
if (close(fd) != 0) if (close(fd) != 0)
@ -216,12 +339,22 @@ errout_with_file:
fprintf(stderr, "interlock_test: close failed: %d\n", errno); fprintf(stderr, "interlock_test: close failed: %d\n", errno);
} }
errout_with_thread: errout_with_null_reader_thread:
pthread_detach(readerid);
pthread_cancel(readerid);
errout_with_null_writer_thread:
pthread_detach(writerid); pthread_detach(writerid);
pthread_cancel(writerid); pthread_cancel(writerid);
errout_with_fifo: errout_with_fifo:
/* unlink(FIFO_PATH2); */ ret = remove(FIFO_PATH2);
if (ret != 0)
{
fprintf(stderr, \
"interlock_test: remove failed with errno=%d\n", errno);
}
printf("interlock_test: Returning %d\n", ret); printf("interlock_test: Returning %d\n", ret);
return ret; return ret;

View File

@ -33,6 +33,31 @@
#include "pipe.h" #include "pipe.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: open_write_only
****************************************************************************/
static void *open_write_only(pthread_addr_t pvarg)
{
void *fd_addr = (void *)pvarg;
printf("open_write_only: Opening FIFO for write access\n");
((int *)fd_addr)[1] = open(FIFO_PATH1, O_WRONLY);
if (((int *)fd_addr)[1] < 0)
{
fprintf(stderr, \
"open_write_only: Failed to open FIFO %s for writing,"
"errno=%d\n", FIFO_PATH1, errno);
return (void *)(uintptr_t)1;
}
return NULL;
}
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -53,6 +78,15 @@ int main(int argc, FAR char *argv[])
printf("\npipe_main: Performing FIFO test\n"); printf("\npipe_main: Performing FIFO test\n");
pthread_t writeonly;
void *status;
/* Open one end of the FIFO for reading and the other end for writing.
* NOTE: This test's result is expected to be the same on any other
* POSIX-compliant system. NuttX FIFOs block for write-only and
* read-only (see interlock_test()).
*/
ret = mkfifo(FIFO_PATH1, 0666); ret = mkfifo(FIFO_PATH1, 0666);
if (ret < 0) if (ret < 0)
{ {
@ -60,18 +94,12 @@ int main(int argc, FAR char *argv[])
return 1; return 1;
} }
/* Open one end of the FIFO for reading and the other end for writing. ret = pthread_create(&writeonly, NULL, open_write_only, &fd);
* NOTE: The following might not work on most FIFO implementations because
* the attempt to open just one end of the FIFO for writing might block.
* The NuttX FIFOs block only on open for read-only (see interlock_test()).
*/
fd[1] = open(FIFO_PATH1, O_WRONLY); if (ret < 0)
if (fd[1] < 0)
{ {
fprintf(stderr, fprintf(stderr, "redirection_test: "
"pipe_main: Failed to open FIFO %s for writing, errno=%d\n", "Failed to create open_write_only task: %d\n", ret);
FIFO_PATH1, errno);
return 2; return 2;
} }
@ -89,9 +117,28 @@ int main(int argc, FAR char *argv[])
return 3; return 3;
} }
/* Wait for open_write_only thread to complete */
fprintf(stderr, "open_write_only: Waiting for open_write_only thread\n");
ret = pthread_join(writeonly, &status);
if (ret != 0)
{
fprintf(stderr, "pipe_main: pthread_join failed, error=%d\n", ret);
return 4;
}
else
{
fprintf(stderr, "pipe_main: open_write_only returned %p\n", status);
if (status != NULL)
{
return 5;
}
}
/* Then perform the test using those file descriptors */ /* Then perform the test using those file descriptors */
ret = transfer_test(fd[0], fd[1]); ret = transfer_test(fd[0], fd[1]);
if (close(fd[0]) != 0) if (close(fd[0]) != 0)
{ {
fprintf(stderr, "pipe_main: close failed: %d\n", errno); fprintf(stderr, "pipe_main: close failed: %d\n", errno);
@ -107,9 +154,21 @@ int main(int argc, FAR char *argv[])
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "pipe_main: FIFO test FAILED (%d)\n", ret); fprintf(stderr, "pipe_main: FIFO test FAILED (%d)\n", ret);
return 4; return 6;
} }
/* Perform the FIFO interlock test */
printf("\npipe_main: Performing pipe interlock test\n");
ret = interlock_test();
if (ret != 0)
{
fprintf(stderr, "pipe_main: FIFO interlock test FAILED (%d)\n", ret);
return 7;
}
printf("pipe_main: FIFO interlock test PASSED\n");
printf("pipe_main: FIFO test PASSED\n"); printf("pipe_main: FIFO test PASSED\n");
#else #else
@ -126,16 +185,18 @@ int main(int argc, FAR char *argv[])
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, "pipe_main: pipe failed with errno=%d\n", errno); fprintf(stderr, "pipe_main: pipe failed with errno=%d\n", errno);
return 5; return 8;
} }
/* Then perform the test using those file descriptors */ /* Then perform the test using those file descriptors */
ret = transfer_test(fd[0], fd[1]); ret = transfer_test(fd[0], fd[1]);
if (close(fd[0]) != 0) if (close(fd[0]) != 0)
{ {
fprintf(stderr, "pipe_main: close failed: %d\n", errno); fprintf(stderr, "pipe_main: close failed: %d\n", errno);
} }
if (close(fd[1]) != 0) if (close(fd[1]) != 0)
{ {
fprintf(stderr, "pipe_main: close failed: %d\n", errno); fprintf(stderr, "pipe_main: close failed: %d\n", errno);
@ -144,35 +205,23 @@ int main(int argc, FAR char *argv[])
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "pipe_main: PIPE test FAILED (%d)\n", ret); fprintf(stderr, "pipe_main: PIPE test FAILED (%d)\n", ret);
return 6; return 9;
} }
printf("pipe_main: PIPE test PASSED\n");
/* Perform the FIFO interlock test */
printf("\npipe_main: Performing pipe interlock test\n");
ret = interlock_test();
if (ret != 0)
{
fprintf(stderr, "pipe_main: FIFO interlock test FAILED (%d)\n", ret);
return 7;
}
printf("pipe_main: PIPE interlock test PASSED\n");
/* Perform the pipe redirection test */ /* Perform the pipe redirection test */
printf("\npipe_main: Performing redirection test\n"); printf("\npipe_main: Performing redirection test\n");
ret = redirection_test(); ret = redirection_test();
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "pipe_main: FIFO redirection test FAILED (%d)\n", ret); fprintf(stderr, "pipe_main: PIPE redirection test FAILED (%d)\n", ret);
return 7; return 10;
} }
printf("pipe_main: PIPE redirection test PASSED\n"); printf("pipe_main: PIPE redirection test PASSED\n");
printf("pipe_main: PIPE test PASSED\n");
#else #else
printf("\npipe_main: Skipping pipe test\n"); printf("\npipe_main: Skipping pipe test\n");

View File

@ -24,11 +24,11 @@
#include <nuttx/config.h> #include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sched.h> #include <sched.h>
#include <semaphore.h>
#include <errno.h> #include <errno.h>
#include "pipe.h" #include "pipe.h"
@ -39,12 +39,6 @@
#define READ_SIZE 37 #define READ_SIZE 37
/****************************************************************************
* Private Data
****************************************************************************/
static sem_t g_rddone;
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@ -53,53 +47,30 @@ static sem_t g_rddone;
* Name: redirect_reader * Name: redirect_reader
****************************************************************************/ ****************************************************************************/
static int redirect_reader(int argc, char *argv[]) static void *redirect_reader(pthread_addr_t pvarg)
{ {
char buffer[READ_SIZE]; char buffer[READ_SIZE];
int *fd = (int *)pvarg;
int fdin; int fdin;
int fdout;
int ret; int ret;
int nbytes = 0; int nbytes = 0;
printf("redirect_reader: started with fdin=%s\n", argv[1]); fprintf(stderr, "redirect_reader: started with fdin=%d\n", fd[0]);
/* Convert the fdin to binary */ fdin = fd[0];
fdin = atoi(argv[1]);
fdout = atoi(argv[2]);
/* Close fdout -- we don't need it */
ret = close(fdout);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fdout=%d\n",
fdout);
return 1;
}
/* Re-direct the fdin to stdin */ /* Re-direct the fdin to stdin */
ret = dup2(fdin, 0); ret = dup2(fdin, 0);
if (ret != 0) if (ret < 0)
{ {
fprintf(stderr, "redirect_reader: dup2 failed: %d\n", errno); fprintf(stderr, "redirect_reader: dup2 failed: %d\n", errno);
close(fdin); close(fdin);
return 2; return (void *)(uintptr_t)1;
}
/* Close the original file descriptor */
ret = close(fdin);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
return 3;
} }
/* Then read from stdin until we hit the end of file */ /* Then read from stdin until we hit the end of file */
fflush(stdout);
for (; ; ) for (; ; )
{ {
/* Read from stdin */ /* Read from stdin */
@ -107,9 +78,8 @@ static int redirect_reader(int argc, char *argv[])
ret = read(0, buffer, READ_SIZE); ret = read(0, buffer, READ_SIZE);
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, "redirect_reader: read failed, errno=%d\n", fprintf(stderr, "redirect_reader: read failed, errno=%d\n", errno);
errno); return (void *)(uintptr_t)2;
return 4;
} }
else if (ret == 0) else if (ret == 0)
{ {
@ -120,93 +90,85 @@ static int redirect_reader(int argc, char *argv[])
/* Echo to stdout */ /* Echo to stdout */
ret = write(1, buffer, ret); ret = write(2, buffer, ret);
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, "redirect_reader: read failed, errno=%d\n", fprintf(stderr, "redirect_reader: write failed, errno=%d\n",
errno); errno);
return 5; return (void *)(uintptr_t)3;
} }
} }
printf("redirect_reader: %d bytes read\n", nbytes); fprintf(stderr, "redirect_reader: %d bytes read\n", nbytes);
ret = close(0); ret = close(0);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "redirect_reader: failed to close fd=0\n"); fprintf(stderr, "redirect_reader: failed to close fd=0\n");
return 6; return (void *)(uintptr_t)4;
} }
sem_post(&g_rddone); fprintf(stderr, "redirect_reader: Returning success\n");
printf("redirect_reader: Returning success\n"); return NULL;
return 0;
} }
/**************************************************************************** /****************************************************************************
* Name: redirect_writer * Name: redirect_writer
****************************************************************************/ ****************************************************************************/
static int redirect_writer(int argc, char *argv[]) static void *redirect_writer(pthread_addr_t pvarg)
{ {
int fdin; int *fd = (int *)pvarg;
int fdout; int fdout;
int nbytes = 0; int nbytes = 0;
int ret; int ret;
fprintf(stderr, "redirect_writer: started with fdout=%s\n", argv[2]); fprintf(stderr, "redirect_writer: started with fdout=%d\n", fd[1]);
/* Convert the fdout to binary */ fdout = fd[1];
fdin = atoi(argv[1]);
fdout = atoi(argv[2]);
/* Close fdin -- we don't need it */
ret = close(fdin);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
return 1;
}
/* Re-direct the fdout to stdout */ /* Re-direct the fdout to stdout */
ret = dup2(fdout, 1); ret = dup2(fdout, 1);
if (ret != 0) if (ret < 0)
{ {
fprintf(stderr, "redirect_writer: dup2 failed: %d\n", errno); fprintf(stderr, "redirect_writer: dup2 failed: %d\n", ret);
return 2; return (void *)(uintptr_t)1;
}
/* Close the original file descriptor */
ret = close(fdout);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fdout=%d\n",
fdout);
return 3;
} }
/* Then write a bunch of stuff to stdout */ /* Then write a bunch of stuff to stdout */
fflush(stderr); fflush(stdout);
nbytes += printf("\nFour score and seven years ago our fathers brought forth on this continent a new nation,\n"); nbytes += printf("\nFour score and seven years ago our fathers brought"
nbytes += printf("conceived in Liberty, and dedicated to the proposition that all men are created equal.\n"); "forth on this continent a new nation,\n");
nbytes += printf("\nNow we are engaged in a great civil war, testing whether that nation, or any nation, so\n"); nbytes += printf("conceived in Liberty, and dedicated to the proposition"
nbytes += printf("conceived and so dedicated, can long endure. We are met on a great battle-field of that war.\n"); "that all men are created equal.\n");
nbytes += printf("We have come to dedicate a portion of that field, as a final resting place for those who here\n"); nbytes += printf("\nNow we are engaged in a great civil war, testing"
nbytes += printf("gave their lives that that nation might live. It is altogether fitting and proper that we\n"); "whether that nation, or any nation, so\n");
nbytes += printf("conceived and so dedicated, can long endure. We are met"
"on a great battle-field of that war.\n");
nbytes += printf("We have come to dedicate a portion of that field, as a"
"final resting place for those who here\n");
nbytes += printf("gave their lives that that nation might live. It is"
"altogether fitting and proper that we\n");
nbytes += printf("should do this.\n"); nbytes += printf("should do this.\n");
nbytes += printf("\nBut, in a larger sense, we can not dedicate - we can not consecrate - we can not hallow - this ground.\n"); nbytes += printf("\nBut, in a larger sense, we can not dedicate - we can"
nbytes += printf("The brave men, living and dead, who struggled here, have consecrated it, far above our poor power\n"); "not consecrate - we can not hallow - this ground.\n");
nbytes += printf("to add or detract. The world will little note, nor long remember what we say here, but it can\n"); nbytes += printf("The brave men, living and dead, who struggled here, have"
nbytes += printf("never forget what they did here. It is for us the living, rather, to be dedicated here to the\n"); "consecrated it, far above our poor power\n");
nbytes += printf("unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to\n"); nbytes += printf("to add or detract. The world will little note, nor long"
nbytes += printf("be here dedicated to the great task remaining before us - that from these honored dead we take\n"); "remember what we say here, but it can\n");
nbytes += printf("increased devotion to that cause for which they gave the last full measure of devotion - that we\n"); nbytes += printf("never forget what they did here. It is for us the"
nbytes += printf("here highly resolve that these dead shall not have died in vain - that this nation, under God,\n"); "living, rather, to be dedicated here to the\n");
nbytes += printf("shall have a new birth of freedom - and that government of the people, by the people, for the\n"); nbytes += printf("unfinished work which they who fought here have thus far"
"so nobly advanced. It is rather for us to\n");
nbytes += printf("be here dedicated to the great task remaining before us"
"- that from these honored dead we take\n");
nbytes += printf("increased devotion to that cause for which they gave the"
"last full measure of devotion - that we\n");
nbytes += printf("here highly resolve that these dead shall not have died"
"in vain - that this nation, under God,\n");
nbytes += printf("shall have a new birth of freedom - and that government"
"of the people, by the people, for the\n");
nbytes += printf("people, shall not perish from the earth.\n\n"); nbytes += printf("people, shall not perish from the earth.\n\n");
fflush(stdout); fflush(stdout);
@ -216,11 +178,18 @@ static int redirect_writer(int argc, char *argv[])
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "redirect_writer: failed to close fd=1\n"); fprintf(stderr, "redirect_writer: failed to close fd=1\n");
return 4; return (void *)(uintptr_t)2;
}
ret = close(fdout);
if (ret != 0)
{
fprintf(stderr, "redirect_writer: failed to close fdout\n");
return (void *)(uintptr_t)4;
} }
fprintf(stderr, "redirect_writer: Returning success\n"); fprintf(stderr, "redirect_writer: Returning success\n");
return 0; return NULL;
} }
/**************************************************************************** /****************************************************************************
@ -233,16 +202,12 @@ static int redirect_writer(int argc, char *argv[])
int redirection_test(void) int redirection_test(void)
{ {
char *argv[3]; pthread_t readerid;
char buffer1[8]; pthread_t writerid;
char buffer2[8]; void *value;
int readerid;
int writerid;
int fd[2]; int fd[2];
int ret; int ret;
sem_init(&g_rddone, 0, 0);
/* Create the pipe */ /* Create the pipe */
ret = pipe(fd); ret = pipe(fd);
@ -250,42 +215,20 @@ int redirection_test(void)
{ {
fprintf(stderr, "redirection_test: pipe failed with errno=%d\n", fprintf(stderr, "redirection_test: pipe failed with errno=%d\n",
errno); errno);
return 5;
}
sprintf(buffer1, "%d", fd[0]);
argv[0] = buffer1;
sprintf(buffer2, "%d", fd[1]);
argv[1] = buffer2;
argv[2] = NULL;
/* Start redirect_reader thread */
printf("redirection_test: Starting redirect_reader task with fd=%d\n",
fd[0]);
readerid = task_create("redirect_reader",
50, CONFIG_EXAMPLES_PIPE_STACKSIZE,
redirect_reader, argv);
if (readerid < 0)
{
fprintf(stderr, "redirection_test: "
"Failed to create redirect_writer task: %d\n", errno);
return 1; return 1;
} }
/* Start redirect_writer task */ /* Start redirect_writer task */
printf("redirection_test: Starting redirect_writer task with fd=%d\n", fprintf(stderr,
fd[1]); "redirection_test: Starting redirect_writer task with fd=%d\n",
writerid = task_create("redirect_writer", fd[1]);
50, CONFIG_EXAMPLES_PIPE_STACKSIZE, ret = pthread_create(&writerid, NULL, redirect_writer, fd);
redirect_writer, argv); if (ret < 0)
if (writerid < 0)
{ {
fprintf(stderr, "redirection_test: " fprintf(stderr, "redirection_test: "
"Failed to create redirect_writer task: %d\n", errno); "Failed to create redirect_writer task: %d\n", errno);
ret = pthread_cancel(writerid);
ret = task_delete(readerid);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "redirection_test: " fprintf(stderr, "redirection_test: "
@ -295,6 +238,39 @@ int redirection_test(void)
return 2; return 2;
} }
/* Start redirect_reader thread */
fprintf(stderr,
"redirection_test: Starting redirect_reader task with fd=%d\n",
fd[0]);
ret = pthread_create(&readerid, NULL, redirect_reader, fd);
if (ret < 0)
{
fprintf(stderr, "redirection_test: "
"Failed to create redirect_writer task: %d\n", ret);
return 3;
}
/* Wait for redirect_reader thread to complete */
fprintf(stderr, "redirection_test: Waiting for null_reader thread\n");
ret = pthread_join(readerid, &value);
if (ret != 0)
{
fprintf(stderr, \
"interlock_test: pthread_join failed, error=%d\n", ret);
ret = 4;
}
else
{
printf("interlock_test: reader returned %p\n", value);
if (value != NULL)
{
ret = 5;
}
}
/* We should be able to close the pipe file descriptors now. */ /* We should be able to close the pipe file descriptors now. */
if (close(fd[0]) != 0) if (close(fd[0]) != 0)
@ -302,23 +278,11 @@ int redirection_test(void)
fprintf(stderr, "pipe_main: close failed: %d\n", errno); fprintf(stderr, "pipe_main: close failed: %d\n", errno);
} }
if (close(fd[1]) != 0)
{
fprintf(stderr, "pipe_main: close failed: %d\n", errno);
}
if (ret != 0)
{
fprintf(stderr, "pipe_main: PIPE test FAILED (%d)\n", ret);
return 6;
}
/* Wait for redirect_writer thread to complete */ /* Wait for redirect_writer thread to complete */
printf("redirection_test: Waiting...\n"); fprintf(stderr, "redirection_test: Waiting...\n");
fflush(stdout); fflush(stdout);
sem_wait(&g_rddone);
printf("redirection_test: returning %d\n", ret); fprintf(stderr, "redirection_test: returning %d\n", ret);
return ret; return ret;
} }

View File

@ -77,8 +77,8 @@ static void *transfer_reader(pthread_addr_t pvarg)
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_reader: read failed, errno=%d\n", \ "transfer_reader: read failed, errno=%d\n",
errno); errno);
return (void *)(uintptr_t)1; return (void *)(uintptr_t)1;
} }
@ -87,8 +87,8 @@ static void *transfer_reader(pthread_addr_t pvarg)
if (nbytes < NREAD_BYTES) if (nbytes < NREAD_BYTES)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_reader: Too few bytes read -- aborting: %d\n", \ "transfer_reader: Too few bytes read. Aborting: %d\n",
nbytes); nbytes);
return (void *)(uintptr_t)2; return (void *)(uintptr_t)2;
} }
@ -105,8 +105,8 @@ static void *transfer_reader(pthread_addr_t pvarg)
if (buffer[ndx] != value) if (buffer[ndx] != value)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_reader: Byte %d, expected %d, found %d\n", "transfer_reader: Byte %d, expected %d, found %d\n",
nbytes + ndx, value, buffer[ndx]); nbytes + ndx, value, buffer[ndx]);
return (void *)(uintptr_t)3; return (void *)(uintptr_t)3;
} }
@ -118,8 +118,8 @@ static void *transfer_reader(pthread_addr_t pvarg)
if (nbytes > NREAD_BYTES) if (nbytes > NREAD_BYTES)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_reader: Too many bytes read -- aborting: %d\n", \ "transfer_reader: Too many bytes read. Aborting: %d\n",
nbytes); nbytes);
return (void *)(uintptr_t)4; return (void *)(uintptr_t)4;
} }
@ -153,13 +153,13 @@ static void *transfer_writer(pthread_addr_t pvarg)
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_writer: write failed, errno=%d\n", errno); "transfer_writer: write failed, errno=%d\n", errno);
return (void *)(uintptr_t)1; return (void *)(uintptr_t)1;
} }
else if (ret != WRITE_SIZE) else if (ret != WRITE_SIZE)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_writer: Unexpected write size=%d\n", ret); "transfer_writer: Unexpected write size=%d\n", ret);
return (void *)(uintptr_t)2; return (void *)(uintptr_t)2;
} }
} }
@ -184,17 +184,18 @@ int transfer_test(int fdin, int fdout)
int tmp; int tmp;
int ret; int ret;
printf("transfer_test: fdin=%d fdout=%d\n", fdin, fdout);
/* Start transfer_reader thread */ /* Start transfer_reader thread */
printf("transfer_test: \ printf("transfer_test: Starting transfer_reader thread\n");
Starting transfer_reader thread\n");
ret = pthread_create(&readerid, NULL, \ ret = pthread_create(&readerid, NULL, \
transfer_reader, (void *)(intptr_t)fdin); transfer_reader, (void *)(intptr_t)fdin);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_test: Failed to create transfer_reader thread, \ "transfer_test: Failed to create transfer_reader thread,"
error=%d\n", ret); "error=%d\n", ret);
return 1; return 1;
} }
@ -206,15 +207,15 @@ int transfer_test(int fdin, int fdout)
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_test: Failed to create transfer_writer thread, \ "transfer_test: Failed to create transfer_writer thread,"
error=%d\n", ret); "error=%d\n", ret);
pthread_detach(readerid); pthread_detach(readerid);
ret = pthread_cancel(readerid); ret = pthread_cancel(readerid);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, \ fprintf(stderr, \
"transfer_test: Failed to cancel transfer_reader thread, \ "transfer_test: Failed to cancel transfer_reader thread,"
error=%d\n", ret); "error=%d\n", ret);
} }
return 2; return 2;
@ -256,4 +257,3 @@ int transfer_test(int fdin, int fdout)
printf("transfer_test: returning %d\n", ret); printf("transfer_test: returning %d\n", ret);
return ret; return ret;
} }