2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/pipe/redirect_test.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "pipe.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define READ_SIZE 37
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static sem_t g_rddone;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: redirect_reader
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int redirect_reader(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char buffer[READ_SIZE];
|
|
|
|
int fdin;
|
|
|
|
int fdout;
|
|
|
|
int ret;
|
|
|
|
int nbytes = 0;
|
2013-09-29 00:50:07 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("redirect_reader: started with fdin=%s\n", argv[1]);
|
|
|
|
|
|
|
|
/* Convert the fdin to binary */
|
|
|
|
|
|
|
|
fdin = atoi(argv[1]);
|
|
|
|
fdout = atoi(argv[2]);
|
|
|
|
|
|
|
|
/* Close fdout -- we don't need it */
|
|
|
|
|
|
|
|
ret = close(fdout);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: failed to close fdout=%d\n",
|
|
|
|
fdout);
|
|
|
|
return 1;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-direct the fdin to stdin */
|
|
|
|
|
|
|
|
ret = dup2(fdin, 0);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: dup2 failed: %d\n", errno);
|
|
|
|
close(fdin);
|
|
|
|
return 2;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the original file descriptor */
|
|
|
|
|
|
|
|
ret = close(fdin);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
|
|
|
|
return 3;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then read from stdin until we hit the end of file */
|
|
|
|
|
|
|
|
fflush(stdout);
|
2022-10-08 10:31:21 +02:00
|
|
|
for (; ; )
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
/* Read from stdin */
|
|
|
|
|
|
|
|
ret = read(0, buffer, READ_SIZE);
|
2022-10-08 10:31:21 +02:00
|
|
|
if (ret < 0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: read failed, errno=%d\n",
|
|
|
|
errno);
|
|
|
|
return 4;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-11-29 22:27:20 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
nbytes += ret;
|
|
|
|
|
|
|
|
/* Echo to stdout */
|
2013-09-29 00:50:07 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
ret = write(1, buffer, ret);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: read failed, errno=%d\n",
|
|
|
|
errno);
|
|
|
|
return 5;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("redirect_reader: %d bytes read\n", nbytes);
|
|
|
|
ret = close(0);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: failed to close fd=0\n");
|
|
|
|
return 6;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sem_post(&g_rddone);
|
|
|
|
printf("redirect_reader: Returning success\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: redirect_writer
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int redirect_writer(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fdin;
|
|
|
|
int fdout;
|
|
|
|
int nbytes = 0;
|
|
|
|
int ret;
|
2013-09-29 00:50:07 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
fprintf(stderr, "redirect_writer: started with fdout=%s\n", argv[2]);
|
2013-09-29 00:50:07 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Convert the fdout to binary */
|
|
|
|
|
|
|
|
fdin = atoi(argv[1]);
|
|
|
|
fdout = atoi(argv[2]);
|
|
|
|
|
|
|
|
/* Close fdin -- we don't need it */
|
|
|
|
|
|
|
|
ret = close(fdin);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
|
|
|
|
return 1;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-direct the fdout to stdout */
|
|
|
|
|
|
|
|
ret = dup2(fdout, 1);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_writer: dup2 failed: %d\n", errno);
|
|
|
|
return 2;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the original file descriptor */
|
|
|
|
|
|
|
|
ret = close(fdout);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_reader: failed to close fdout=%d\n",
|
|
|
|
fdout);
|
|
|
|
return 3;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then write a bunch of stuff to stdout */
|
|
|
|
|
|
|
|
fflush(stderr);
|
|
|
|
nbytes += printf("\nFour score and seven years ago our fathers brought forth on this continent a new nation,\n");
|
|
|
|
nbytes += printf("conceived in Liberty, and dedicated to the proposition that all men are created equal.\n");
|
|
|
|
nbytes += printf("\nNow we are engaged in a great civil war, testing 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("\nBut, in a larger sense, we can not dedicate - we can not consecrate - we can not hallow - this ground.\n");
|
|
|
|
nbytes += printf("The brave men, living and dead, who struggled here, have consecrated it, far above our poor power\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("never forget what they did here. It is for us the living, rather, to be dedicated here to 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");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
fprintf(stderr, "redirect_writer: %d bytes written\n", nbytes);
|
|
|
|
|
|
|
|
ret = close(1);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirect_writer: failed to close fd=1\n");
|
|
|
|
return 4;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "redirect_writer: Returning success\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: redirection_test
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int redirection_test(void)
|
|
|
|
{
|
2022-10-15 19:55:16 +02:00
|
|
|
char *argv[3];
|
2011-03-20 19:18:19 +01:00
|
|
|
char buffer1[8];
|
|
|
|
char buffer2[8];
|
|
|
|
int readerid;
|
|
|
|
int writerid;
|
2013-09-29 00:50:07 +02:00
|
|
|
int fd[2];
|
2011-03-20 19:18:19 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
sem_init(&g_rddone, 0, 0);
|
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
/* Create the pipe */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2013-09-29 00:50:07 +02:00
|
|
|
ret = pipe(fd);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirection_test: pipe failed with errno=%d\n",
|
|
|
|
errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
return 5;
|
|
|
|
}
|
2013-09-29 00:50:07 +02:00
|
|
|
|
|
|
|
sprintf(buffer1, "%d", fd[0]);
|
2011-03-20 19:18:19 +01:00
|
|
|
argv[0] = buffer1;
|
2013-09-29 00:50:07 +02:00
|
|
|
sprintf(buffer2, "%d", fd[1]);
|
2011-03-20 19:18:19 +01:00
|
|
|
argv[1] = buffer2;
|
|
|
|
argv[2] = NULL;
|
|
|
|
|
|
|
|
/* Start redirect_reader thread */
|
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
printf("redirection_test: Starting redirect_reader task with fd=%d\n",
|
|
|
|
fd[0]);
|
|
|
|
readerid = task_create("redirect_reader",
|
|
|
|
50, CONFIG_EXAMPLES_PIPE_STACKSIZE,
|
2022-10-15 19:55:16 +02:00
|
|
|
redirect_reader, argv);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (readerid < 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirection_test: "
|
|
|
|
"Failed to create redirect_writer task: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start redirect_writer task */
|
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
printf("redirection_test: Starting redirect_writer task with fd=%d\n",
|
|
|
|
fd[1]);
|
|
|
|
writerid = task_create("redirect_writer",
|
|
|
|
50, CONFIG_EXAMPLES_PIPE_STACKSIZE,
|
2022-10-15 19:55:16 +02:00
|
|
|
redirect_writer, argv);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (writerid < 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirection_test: "
|
|
|
|
"Failed to create redirect_writer task: %d\n", errno);
|
2017-11-29 22:27:20 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
ret = task_delete(readerid);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, "redirection_test: "
|
|
|
|
"Failed to delete redirect_reader task %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2017-11-29 22:27:20 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We should be able to close the pipe file descriptors now. */
|
|
|
|
|
2013-09-29 00:50:07 +02:00
|
|
|
if (close(fd[0]) != 0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
fprintf(stderr, "pipe_main: close failed: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2017-11-29 22:27:20 +01:00
|
|
|
|
2013-09-29 00:50:07 +02:00
|
|
|
if (close(fd[1]) != 0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
fprintf(stderr, "pipe_main: close failed: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
fprintf(stderr, "pipe_main: PIPE test FAILED (%d)\n", ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for redirect_writer thread to complete */
|
|
|
|
|
|
|
|
printf("redirection_test: Waiting...\n");
|
|
|
|
fflush(stdout);
|
|
|
|
sem_wait(&g_rddone);
|
|
|
|
|
|
|
|
printf("redirection_test: returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|