Add support so that fdopen() may be used on socket descriptors

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3659 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-06-02 15:05:48 +00:00
parent 3867f05f0f
commit 45acebc2f9
8 changed files with 246 additions and 46 deletions

View file

@ -336,7 +336,7 @@ CONFIG_START_DAY=23
CONFIG_GREGORIAN_TIME=n
CONFIG_JULIAN_TIME=n
CONFIG_DEV_CONSOLE=y
CONFIG_DEV_LOWCONSOLE=y
CONFIG_DEV_LOWCONSOLE=n
CONFIG_MUTEX_TYPES=n
CONFIG_PRIORITY_INHERITANCE=n
CONFIG_SEM_PREALLOCHOLDERS=0

View file

@ -39,11 +39,28 @@ ASRCS =
AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS =
# If there are no file descriptors configured, then a small part of the
# logic in this directory may still apply to socket descriptors
ifeq ($(CONFIG_NFILE_DESCRIPTORS),0)
ifneq ($(CONFIG_NSOCKET_DESCRIPTORS),0)
# Socket descriptor support
CSRCS += fs_close.c fs_read.c fs_write.c fs_ioctl.c fs_poll.c fs_select.c
endif
# Support for network access using streams
ifneq ($(CONFIG_NFILE_STREAMS),0)
CSRCS += fs_fdopen.c
endif
else
# Common file/socket descriptor support
CSRCS += fs_open.c fs_close.c fs_read.c fs_write.c fs_ioctl.c \
fs_poll.c fs_select.c fs_lseek.c fs_dup.c fs_filedup.c \
fs_dup2.c fs_fcntl.c fs_filedup2.c fs_opendir.c fs_closedir.c \
@ -56,10 +73,14 @@ CSRCS += fs_registerblockdriver.c fs_unregisterblockdriver.c \
include mmap/Make.defs
# Stream support
ifneq ($(CONFIG_NFILE_STREAMS),0)
CSRCS += fs_fdopen.c
endif
# Additional files required is mount-able file systems are supported
ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y)
CSRCS += fs_mount.c fs_umount.c fs_fsync.c fs_unlink.c fs_rename.c \
fs_mkdir.c fs_rmdir.c

View file

@ -47,24 +47,79 @@
#include <nuttx/kmalloc.h>
#include <nuttx/fs.h>
#include <nuttx/net.h>
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: fs_checkfd
*
* Description:
* Check if the file descriptor is valid for the provided TCB and if it
* supports the requested access.
*
****************************************************************************/
#if CONFIG_NFILE_DESCRIPTORS > 0
static inline int fs_checkfd(FAR _TCB *tcb, int fd, int oflags)
{
FAR struct filelist *flist;
FAR struct inode *inode;
/* Get the file list from the TCB */
flist = tcb->filelist;
/* 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)
{
/* No inode -- descriptor does not correspond to an open file */
return -ENOENT;
}
/* 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
* already been created.
*/
if (inode_checkflags(inode, oflags) != OK)
{
/* Cannot support the requested access */
return -EACCES;
}
/* Looks good to me */
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: fs_fdopen
*
* Description:
* This function does the core operations for fopen and fdopen.
*
****************************************************************************/
FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb)
{
FAR struct filelist *flist;
FAR struct streamlist *slist;
FAR struct inode *inode;
FAR FILE *stream;
int err = OK;
int ret;
@ -88,37 +143,55 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb)
tcb = sched_self();
}
/* Get the file and stream list from the TCB */
/* Verify that this is a valid file/socket descriptor and that the
* requested access can be support.
*
* Is this fd in the range of valid file descriptors? Socket descriptors
* lie in a different range.
*/
#if CONFIG_NFILE_DESCRIPTORS > 0
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
#endif
{
/* No.. If networking is enabled then this might be a socket
* descriptor.
*/
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
ret = net_checksd;
#else
/* No networking... it is just a bad descriptor */
err = EBADF;
return ERROR;
#endif
}
/* The descriptor is in a valid range to file descriptor... do the read */
#if CONFIG_NFILE_DESCRIPTORS > 0
else
{
ret = fs_checkfd(tcb, fd, oflags);
}
#endif
/* Do we have a good descriptor of some sort? */
if (ret < 0)
{
/* No... return the reported error */
err = -ret;
goto errout;
}
/* Get the stream list from the TCB */
flist = tcb->filelist;
slist = tcb->streams;
/* 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
* already been created.
*/
if (inode_checkflags(inode, oflags) != OK)
{
err = EACCES;
goto errout;
}
/* Find an unallocated FILE structure */
/* Find an unallocated FILE structure in the stream list */
ret = sem_wait(&slist->sl_sem);
if (ret != OK)

View file

@ -131,6 +131,14 @@ extern "C" {
#define EXTERN extern
#endif
/* net_checksd.c *************************************************************/
/* Check if the socket descriptor is valid for the provided TCB and if it
* supports the requested access.
*/
typedef struct _TCB _TCB; /* Forward reference */
EXTERN int net_checksd(FAR _TCB *tcb, int fd, int oflags);
/* net_sockets.c *************************************************************/
/* There interfaces are called only from OS scheduling and iniialization logic
* under sched/

View file

@ -36,27 +36,42 @@
-include $(TOPDIR)/Make.defs
ifeq ($(CONFIG_NET),y)
SOCK_ASRCS =
SOCK_CSRCS = bind.c connect.c getsockname.c recv.c recvfrom.c socket.c \
# Basic networking support
SOCK_ASRCS =
SOCK_CSRCS = bind.c connect.c getsockname.c recv.c recvfrom.c socket.c \
sendto.c net_sockets.c net_close.c net_dup.c net_dup2.c net_clone.c \
net_vfcntl.c
# TCP/IP support
ifeq ($(CONFIG_NET_TCP),y)
SOCK_CSRCS += send.c listen.c accept.c
SOCK_CSRCS += send.c listen.c accept.c
endif
# Socket options
ifeq ($(CONFIG_NET_SOCKOPTS),y)
SOCK_CSRCS += setsockopt.c getsockopt.c
SOCK_CSRCS += setsockopt.c getsockopt.c
ifneq ($(CONFIG_DISABLE_CLOCK),y)
SOCK_CSRCS += net_timeo.c net_dsec2timeval.c net_timeval2dsec.c
SOCK_CSRCS += net_timeo.c net_dsec2timeval.c net_timeval2dsec.c
ifneq ($(CONFIG_NET_SLIP),y)
SOCK_CSRCS += net_arptimer.c
SOCK_CSRCS += net_arptimer.c
endif
endif
endif
NETDEV_ASRCS =
NETDEV_CSRCS = netdev_register.c netdev_ioctl.c net_poll.c netdev_txnotify.c \
# Support for network access using streams
ifneq ($(CONFIG_NFILE_STREAMS),0)
SOCK_CSRCS += net_checksd.c
endif
# Support for operations on network devices
NETDEV_ASRCS =
NETDEV_CSRCS = netdev_register.c netdev_ioctl.c net_poll.c netdev_txnotify.c \
netdev_findbyname.c netdev_findbyaddr.c netdev_count.c \
netdev_foreach.c netdev_unregister.c

View file

@ -372,12 +372,7 @@ static inline int tcp_connect(FAR struct socket *psock, const struct sockaddr_in
if (ret < 0)
{
int err = errno;
if (err >= 0)
{
err = ENOSYS;
}
ret = -err;
ret = -errno;
}
else
{

88
net/net_checksd.c Normal file
View file

@ -0,0 +1,88 @@
/****************************************************************************
* net/net_checksd.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 NuttX 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 OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/socket.h>
#include <sched.h>
#include <errno.h>
#include <debug.h>
#include "net_internal.h"
/****************************************************************************
* Global Functions
****************************************************************************/
/****************************************************************************
* Name: net_checksd
*
* Description:
* Check if the socket descriptor is valid for the provided TCB and if it
* supports the requested access. This trivial operation is part of the
* fdopen() operation when the fdopen() is performed on a socket descriptor.
* It simply performs some sanity checking before permitting the socket
* descriptor to be wrapped as a C FILE stream.
*
****************************************************************************/
#if defined(CONFIG_NET) && CONFIG_NFILE_DESCRIPTORS > 0
int net_checksd(FAR _TCB *tcb, int sd, int oflags)
{
FAR struct socket *psock = sockfd_socket(sd);
/* Verify that the sockfd corresponds to valid, allocated socket */
if (!psock || psock->s_crefs <= 0)
{
nvdbg("No valid socket for sd: %d\n", sd);
return -EBADF;
}
/* NOTE: We permit the socket FD to be "wrapped" in a stream as
* soon as the socket descriptor is created by socket(). Therefore
* (1) we don't care if the socket is connected yet, and (2) there
* are no access restrictions that can be enforced yet.
*/
return OK;
}
#endif /* CONIG_NET && ONFIG_NFILE_DESCRIPTORS > 0 */

View file

@ -415,7 +415,7 @@ ssize_t send(int sockfd, const void *buf, size_t len, int flags)
goto errout;
}
/* If this is a connected socket, then return ENOTCONN */
/* If this is an un-connected socket, then return ENOTCONN */
if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags))
{