diff options
-rw-r--r-- | nuttx/lib/lib_fopen.c | 19 | ||||
-rw-r--r-- | nuttx/sched/sched_setupidlefiles.c | 33 | ||||
-rw-r--r-- | nuttx/sched/sched_setupstreams.c | 2 | ||||
-rw-r--r-- | nuttx/sched/sched_setuptaskfiles.c | 8 |
4 files changed, 45 insertions, 17 deletions
diff --git a/nuttx/lib/lib_fopen.c b/nuttx/lib/lib_fopen.c index 672fb5bbe..0bfb275b8 100644 --- a/nuttx/lib/lib_fopen.c +++ b/nuttx/lib/lib_fopen.c @@ -1,7 +1,7 @@ /**************************************************************************** * lib/lib_fopen.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2010 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -137,18 +137,33 @@ FAR struct file_struct *lib_fdopen(int fd, FAR const char *mode, FAR struct filelist *flist, FAR struct streamlist *slist) { - FAR struct inode *inode = flist->fl_files[fd].f_inode; + FAR struct inode *inode = flist; FAR FILE *stream; int oflags = lib_mode2oflags(mode); int err = OK; int i; + /* Check input parameters */ + if (fd < 0 || !flist || !slist) { err = EBADF; goto errout; } + /* Get the inode associated with the file descriptor. This should + * normally be the case if fd >= 0. But not in the case where the + * called attempts to explictly stdin with fdopen(0) but stdin has + * been closed. + */ + + inode = flist->fl_files[fd].f_inode; + if (!inode) + { + err = ENOENT; + goto errout; + } + /* Make sure that the inode supports the requested access. In * the case of fdopen, we are not actually creating the file -- in * particular w and w+ do not truncate the file and any files have diff --git a/nuttx/sched/sched_setupidlefiles.c b/nuttx/sched/sched_setupidlefiles.c index e88d75e12..683a00c7a 100644 --- a/nuttx/sched/sched_setupidlefiles.c +++ b/nuttx/sched/sched_setupidlefiles.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/sched_setupidlefiles.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2010 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -44,6 +44,7 @@ #include <fcntl.h> #include <sched.h> #include <errno.h> +#include <debug.h> #include <nuttx/fs.h> #include <nuttx/net.h> @@ -88,8 +89,7 @@ int sched_setupidlefiles(FAR _TCB *tcb) tcb->filelist = files_alloclist(); if (!tcb->filelist) { - *get_errno_ptr() = ENOMEM; - return ERROR; + return -ENOMEM; } #endif /* CONFIG_NFILE_DESCRIPTORS */ @@ -99,25 +99,40 @@ int sched_setupidlefiles(FAR _TCB *tcb) tcb->sockets = net_alloclist(); if (!tcb->sockets) { - *get_errno_ptr() = ENOMEM; - return ERROR; + return -ENOMEM; } #endif /* CONFIG_NSOCKET_DESCRIPTORS */ #if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE) - /* Open stdin, dup to get stdout and stderr. */ + /* Open stdin, dup to get stdout and stderr. This should always + * be the first file opened and, hence, should always get file + * descriptor 0. + */ fd = open("/dev/console", O_RDWR); if (fd == 0) { + /* Successfully opened /dev/console as stdin (fd == 0) */ + (void)file_dup2(0, 1); (void)file_dup2(0, 2); } else { - (void)close(fd); - *get_errno_ptr() = ENFILE; - return ERROR; + /* We failed to open /dev/console OR for some reason, we opened + * it and got some file descriptor other than 0. + */ + + if (fd >- 0) + { + slldbg("Open /dev/console fd: %d\n", fd); + (void)close(fd); + } + else + { + slldbg("Failed to open /dev/console: %d\n", errno); + } + return -ENFILE; } #if CONFIG_NFILE_STREAMS > 0 diff --git a/nuttx/sched/sched_setupstreams.c b/nuttx/sched/sched_setupstreams.c index 88d0b09c4..28b824f26 100644 --- a/nuttx/sched/sched_setupstreams.c +++ b/nuttx/sched/sched_setupstreams.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched_setupstreams.c * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2008, 2010 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without diff --git a/nuttx/sched/sched_setuptaskfiles.c b/nuttx/sched/sched_setuptaskfiles.c index 23a820a96..95c571b47 100644 --- a/nuttx/sched/sched_setuptaskfiles.c +++ b/nuttx/sched/sched_setuptaskfiles.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/sched_setuptaskfiles.c * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2008, 2010 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -105,8 +105,7 @@ int sched_setuptaskfiles(FAR _TCB *tcb) tcb->filelist = files_alloclist(); if (!tcb->filelist) { - *get_errno_ptr() = ENOMEM; - return ERROR; + return -ENOMEM; } #endif /* CONFIG_NFILE_DESCRIPTORS */ @@ -118,8 +117,7 @@ int sched_setuptaskfiles(FAR _TCB *tcb) tcb->sockets = net_alloclist(); if (!tcb->sockets) { - *get_errno_ptr() = ENOMEM; - return ERROR; + return -ENOMEM; } #endif /* CONFIG_NSOCKET_DESCRIPTORS */ |