Fix bug: Using unsigned to detect errno<0

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@791 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2008-07-31 00:28:24 +00:00
parent 378b050ffa
commit faf168d93c
8 changed files with 228 additions and 93 deletions

View File

@ -381,4 +381,8 @@
* Removed limitation: task_create() was only dup'ing 3 file descriptors (now * Removed limitation: task_create() was only dup'ing 3 file descriptors (now
dups all open file descriptors). dups all open file descriptors).
* Added a test for redirection of stdio through pipes * Added a test for redirection of stdio through pipes
* Fixed error in dup and dup2: Must call open/close methods in fs/driver so that
driver can correctly maintain open reference counts.
* Fixed in error in stdio flush logic. Needed ssize_t vs size_t for error
check.

View File

@ -8,7 +8,7 @@
<tr align="center" bgcolor="#e4e4e4"> <tr align="center" bgcolor="#e4e4e4">
<td> <td>
<h1><big><font color="#3c34ec"><i>NuttX RTOS</i></font></big></h1> <h1><big><font color="#3c34ec"><i>NuttX RTOS</i></font></big></h1>
<p>Last Updated: July 29, 2008</p> <p>Last Updated: July 30, 2008</p>
</td> </td>
</tr> </tr>
</table> </table>
@ -1030,6 +1030,10 @@ nuttx-0.3.12 2008-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt;
* Removed limitation: task_create() was only dup'ing 3 file descriptors (now * Removed limitation: task_create() was only dup'ing 3 file descriptors (now
dups all open file descriptors). dups all open file descriptors).
* Added a test for redirection of stdio through pipes * Added a test for redirection of stdio through pipes
* Fixed error in dup and dup2: Must call open/close methods in fs/driver so that
driver can correctly maintain open reference counts.
* Fixed in error in stdio flush logic. Needed ssize_t vs size_t for error
check.
pascal-0.1.3 2008-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt; pascal-0.1.3 2008-xx-xx Gregory Nutt &lt;spudmonkey@racsa.co.cr&gt;

View File

@ -44,6 +44,7 @@
#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"
@ -62,6 +63,8 @@
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
static sem_t g_rddone;
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
@ -73,37 +76,49 @@
static int redirect_reader(int argc, char *argv[]) static int redirect_reader(int argc, char *argv[])
{ {
char buffer[READ_SIZE]; char buffer[READ_SIZE];
int fd = (int)argv[0]; int fdin;
int fdout;
int ret; int ret;
int nbytes = 0; int nbytes = 0;
printf("redirect_reader: started with fd=%s\n", argv[1]); printf("redirect_reader: started with fdin=%s\n", argv[1]);
/* Convert the fd to binary */ /* Convert the fdin to binary */
fd = atoi(argv[1]); fdin = atoi(argv[1]);
fdout = atoi(argv[2]);
/* Re-direct the fd to stdin */ /* Close fdout -- we don't need it */
ret = dup2(fd, 0); 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 */
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(fd); close(fdin);
return 1; return 2;
} }
/* Close the original file descriptor */ /* Close the original file descriptor */
ret = close(fd); ret = close(fdin);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "redirect_reader: failed to close fd=%d\n", fd); fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
return 2; 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 */
@ -112,7 +127,7 @@ static int redirect_reader(int argc, char *argv[])
if (ret < 0 ) if (ret < 0 )
{ {
fprintf(stderr, "redirect_reader: read failed, errno=%d\n", errno); fprintf(stderr, "redirect_reader: read failed, errno=%d\n", errno);
return 3; return 4;
} }
else if (ret == 0) else if (ret == 0)
{ {
@ -126,7 +141,7 @@ static int redirect_reader(int argc, char *argv[])
if (ret < 0) if (ret < 0)
{ {
fprintf(stderr, "redirect_reader: read failed, errno=%d\n", errno); fprintf(stderr, "redirect_reader: read failed, errno=%d\n", errno);
return 4; return 5;
} }
} }
@ -134,9 +149,11 @@ static int redirect_reader(int argc, char *argv[])
ret = close(0); ret = close(0);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "redirect_reader: failed to close fd=%d\n", fd); fprintf(stderr, "redirect_reader: failed to close fd=0\n");
return 5; return 6;
} }
sem_post(&g_rddone);
printf("redirect_reader: Returning success\n"); printf("redirect_reader: Returning success\n");
return 0; return 0;
} }
@ -147,36 +164,48 @@ static int redirect_reader(int argc, char *argv[])
static int redirect_writer(int argc, char *argv[]) static int redirect_writer(int argc, char *argv[])
{ {
int fd; int fdin;
int fdout;
int nbytes = 0; int nbytes = 0;
int ret; int ret;
printf("redirect_writer: started with fd=%s\n", argv[1]); fprintf(stderr, "redirect_writer: started with fdout=%s\n", argv[2]);
/* Convert the fdout to binary */
/* Convert the fd to binary */ fdin = atoi(argv[1]);
fdout = atoi(argv[2]);
fd = atoi(argv[1]); /* Close fdin -- we don't need it */
/* Re-direct the fd to stdout */ ret = close(fdin);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
return 1;
}
ret = dup2(fd, 1); /* Re-direct the fdout to stdout */
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", errno);
return 1; return 2;
} }
/* Close the original file descriptor */ /* Close the original file descriptor */
ret = close(fd); ret = close(fdout);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "redirect_reader: failed to close fd=%d\n", fd); fprintf(stderr, "redirect_reader: failed to close fdout=%d\n", fdout);
return 2; return 3;
} }
/* Then write a bunch of stuff to stdout */ /* 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("\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("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("\nNow we are engaged in a great civil war, testing whether that nation, or any nation, so\n");
@ -194,15 +223,18 @@ static int redirect_writer(int argc, char *argv[])
nbytes += printf("here highly resolve that these dead shall not have died in vain—that this nation, under God,\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("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");
printf("redirect_writer: %d bytes read\n", nbytes); fflush(stdout);
fprintf(stderr, "redirect_writer: %d bytes written\n", nbytes);
ret = close(1); ret = close(1);
if (ret != 0) if (ret != 0)
{ {
fprintf(stderr, "redirect_writer: failed to close fd=%d\n", fd); fprintf(stderr, "redirect_writer: failed to close fd=1\n");
return 3; return 4;
} }
printf("redirect_writer: Returning success\n");
fprintf(stderr, "redirect_writer: Returning success\n");
return 0; return 0;
} }
@ -216,14 +248,17 @@ static int redirect_writer(int argc, char *argv[])
int redirection_test(void) int redirection_test(void)
{ {
const char *argv[2]; const char *argv[3];
char buffer[8]; char buffer1[8];
char buffer2[8];
int readerid; int readerid;
int writerid; int writerid;
int filedes[2]; int filedes[2];
int ret; int ret;
/* Create the pipe */ sem_init(&g_rddone, 0, 0);
/* Create the pipe */
ret = pipe(filedes); ret = pipe(filedes);
if (ret < 0) if (ret < 0)
@ -232,12 +267,15 @@ int redirection_test(void)
return 5; return 5;
} }
sprintf(buffer1, "%d", filedes[0]);
argv[0] = buffer1;
sprintf(buffer2, "%d", filedes[1]);
argv[1] = buffer2;
argv[2] = NULL;
/* Start redirect_reader thread */ /* Start redirect_reader thread */
printf("redirection_test: Starting redirect_reader task with fd=%d\n", filedes[0]); printf("redirection_test: Starting redirect_reader task with fd=%d\n", filedes[0]);
sprintf(buffer, "%d", filedes[0]);
argv[0] = buffer;
argv[1] = NULL;
readerid = task_create("redirect_reader", 50, CONFIG_EXAMPLES_PIPE_STACKSIZE, redirect_reader, argv); readerid = task_create("redirect_reader", 50, CONFIG_EXAMPLES_PIPE_STACKSIZE, redirect_reader, argv);
if (readerid < 0) if (readerid < 0)
{ {
@ -248,9 +286,6 @@ int redirection_test(void)
/* Start redirect_writer task */ /* Start redirect_writer task */
printf("redirection_test: Starting redirect_writer task with fd=%d\n", filedes[1]); printf("redirection_test: Starting redirect_writer task with fd=%d\n", filedes[1]);
sprintf(buffer, "%d", filedes[1]);
argv[0] = buffer;
argv[1] = NULL;
writerid = task_create("redirect_writer", 50, CONFIG_EXAMPLES_PIPE_STACKSIZE, redirect_writer, argv); writerid = task_create("redirect_writer", 50, CONFIG_EXAMPLES_PIPE_STACKSIZE, redirect_writer, argv);
if (writerid < 0) if (writerid < 0)
{ {
@ -263,10 +298,27 @@ int redirection_test(void)
return 2; return 2;
} }
/* We should be able to close the pipe file descriptors now. */
if (close(filedes[0]) != 0)
{
fprintf(stderr, "user_start: close failed: %d\n", errno);
}
if (close(filedes[1]) != 0)
{
fprintf(stderr, "user_start: close failed: %d\n", errno);
}
if (ret != 0)
{
fprintf(stderr, "user_start: 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"); printf("redirection_test: Waiting...\n");
sleep(10); sem_wait(&g_rddone);
printf("redirection_test: returning %d\n", ret); printf("redirection_test: returning %d\n", ret);
return ret; return ret;

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* fs_close.c * fs/fs_close.c
* *
* Copyright (C) 2007 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -14,7 +14,7 @@
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* 3. Neither the name Gregory Nutt nor the names of its contributors may be * 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software * used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *

View File

@ -1,7 +1,7 @@
/************************************************************ /****************************************************************************
* fs_files.c * fs/s_files.c
* *
* Copyright (C) 2007 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -31,11 +31,11 @@
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Included Files * Included Files
************************************************************/ ****************************************************************************/
#include <nuttx/config.h> #include <nuttx/config.h>
#include <string.h> #include <string.h>
@ -47,25 +47,25 @@
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include "fs_internal.h" #include "fs_internal.h"
/************************************************************ /****************************************************************************
* Definitions * Definitions
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Public Types * Public Types
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Private Variables * Private Variables
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Private Variables * Private Variables
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Private Functions * Private Functions
************************************************************/ ****************************************************************************/
static void _files_semtake(FAR struct filelist *list) static void _files_semtake(FAR struct filelist *list)
{ {
@ -83,9 +83,9 @@ static void _files_semtake(FAR struct filelist *list)
#define _files_semgive(list) sem_post(&list->fl_sem) #define _files_semgive(list) sem_post(&list->fl_sem)
/************************************************************ /****************************************************************************
* Pulblic Functions * Pulblic Functions
************************************************************/ ****************************************************************************/
/* This is called from the FS initialization logic to configure /* This is called from the FS initialization logic to configure
* the files. * the files.
@ -191,42 +191,117 @@ int files_releaselist(FAR struct filelist *list)
int files_dup(FAR struct file *filep1, FAR struct file *filep2) int files_dup(FAR struct file *filep1, FAR struct file *filep2)
{ {
FAR struct filelist *list; FAR struct filelist *list;
FAR struct inode *inode;
int err;
int ret;
if (!filep1 || !filep1->f_inode || !filep2) if (!filep1 || !filep1->f_inode || !filep2)
{ {
*get_errno_ptr() = EBADF; err = EBADF;
return ERROR; goto errout;
} }
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (INODE_IS_MOUNTPT(filep1->f_inode))
{
err = ENOSYS; /* Not yet supported */
goto errout;
}
#endif
list = sched_getfiles(); list = sched_getfiles();
if (!list) if (!list)
{ {
*get_errno_ptr() = EMFILE; err = EMFILE;
return ERROR; goto errout;
} }
_files_semtake(list); _files_semtake(list);
/* If there is already an inode contained in the new file structure, /* If there is already an inode contained in the new file structure,
* release it (effectively closing the file). * close the file and release the inode.
*/ */
if (filep2->f_inode) inode = filep2->f_inode;
if (inode)
{ {
/* Close the file, driver, or mountpoint. */
if (inode->u.i_ops && inode->u.i_ops->close)
{
/* Perform the close operation */
ret = inode->u.i_ops->close(filep2);
if (ret < 0)
{
/* An error occurred while closing the driver */
goto errout_with_ret;
}
}
/* Release the inode */
inode_release(filep2->f_inode); inode_release(filep2->f_inode);
} }
/* Increment the reference count on the contained inode */ /* Increment the reference count on the contained inode */
inode_addref(filep1->f_inode); inode = filep1->f_inode;
inode_addref(inode);
/* Then clone the file structure */ /* Then clone the file structure */
filep2->f_oflags = filep1->f_oflags; filep2->f_oflags = filep1->f_oflags;
filep2->f_pos = filep1->f_pos; filep2->f_pos = filep1->f_pos;
filep2->f_inode = filep1->f_inode; filep2->f_inode = inode;
/* Call the open method on the file, driver, mountpoint so that it
* can maintain the correct open counts.
*/
if (inode->u.i_ops && inode->u.i_ops->open)
{
#ifndef CONFIG_DISABLE_MOUNTPOINT
#if 0 /* Not implemented */
if (INODE_IS_MOUNTPT(inode))
{
/* Open a file on the mountpoint */
ret = inode->u.i_mops->open(filep2, ?, filep2->f_oflags, ?);
}
else
#endif
#endif
{
/* Open the psuedo file or device driver */
ret = inode->u.i_ops->open(filep2);
}
/* Handle open failures */
if (ret < 0)
{
goto errout_with_inode;
}
}
_files_semgive(list); _files_semgive(list);
return OK; return OK;
/* Handler various error conditions */
errout_with_inode:
inode_release(filep2->f_inode);
filep2->f_oflags = 0;
filep2->f_pos = 0;
filep2->f_inode = NULL;
errout_with_ret:
err = -ret;
_files_semgive(list);
errout:
errno = err;
return ERROR;
} }
/* Allocate a struct files instance and associate it with an /* Allocate a struct files instance and associate it with an

View File

@ -1,7 +1,7 @@
/************************************************************ /****************************************************************************
* fs_open.c * fs_open.c
* *
* Copyright (C) 2007 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -14,7 +14,7 @@
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* 3. Neither the name Gregory Nutt nor the names of its contributors may be * 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software * used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@ -31,11 +31,11 @@
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Included Files * Included Files
************************************************************/ ****************************************************************************/
#include <nuttx/config.h> #include <nuttx/config.h>
#include <sys/types.h> #include <sys/types.h>
@ -48,13 +48,13 @@
#include <nuttx/fs.h> #include <nuttx/fs.h>
#include "fs_internal.h" #include "fs_internal.h"
/************************************************************ /****************************************************************************
* Private Functions * Private Functions
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Public Functions * Public Functions
************************************************************/ ****************************************************************************/
int inode_checkflags(FAR struct inode *inode, int oflags) int inode_checkflags(FAR struct inode *inode, int oflags)
{ {

View File

@ -106,7 +106,7 @@ ssize_t lib_fflush(FILE *stream, boolean bforce)
{ {
#if CONFIG_STDIO_BUFFER_SIZE > 0 #if CONFIG_STDIO_BUFFER_SIZE > 0
const unsigned char *src; const unsigned char *src;
size_t bytes_written; ssize_t bytes_written;
size_t nbuffer; size_t nbuffer;
/* Return EBADF if the file is not opened for writing */ /* Return EBADF if the file is not opened for writing */

View File

@ -1,7 +1,7 @@
/************************************************************ /****************************************************************************
* sched_releasefiles.c * sched/sched_releasefiles.c
* *
* Copyright (C) 2007 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -14,7 +14,7 @@
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* 3. Neither the name Gregory Nutt nor the names of its contributors may be * 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software * used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
@ -31,11 +31,11 @@
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Included Files * Included Files
************************************************************/ ****************************************************************************/
#include <nuttx/config.h> #include <nuttx/config.h>
@ -46,15 +46,15 @@
#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 #if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0
/************************************************************ /****************************************************************************
* Private Functions * Private Functions
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Public Functions * Public Functions
************************************************************/ ****************************************************************************/
/************************************************************ /****************************************************************************
* Function: sched_releasefiles * Function: sched_releasefiles
* *
* Description: * Description:
@ -68,7 +68,7 @@
* *
* Assumptions: * Assumptions:
* *
************************************************************/ ****************************************************************************/
int sched_releasefiles(_TCB *tcb) int sched_releasefiles(_TCB *tcb)
{ {