From f2675bf333e0378c842717a94a13c73dabb76a52 Mon Sep 17 00:00:00 2001 From: patacongo Date: Mon, 15 Jun 2009 18:58:22 +0000 Subject: dup() and dup2() support for socket descriptors git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1884 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/fs/fs_dup.c | 109 +++++++++++++++++++----------------------------------- 1 file changed, 38 insertions(+), 71 deletions(-) (limited to 'nuttx/fs/fs_dup.c') diff --git a/nuttx/fs/fs_dup.c b/nuttx/fs/fs_dup.c index 8f8b332b1..20b7fbf27 100644 --- a/nuttx/fs/fs_dup.c +++ b/nuttx/fs/fs_dup.c @@ -1,7 +1,7 @@ /**************************************************************************** * fs/fs_dup.c * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -43,16 +43,21 @@ #include #include #include + +#include #include "fs_internal.h" +/* This logic in this applies only when both socket and file descriptors are + * in that case, this function descriminates which type of dup is being + * performed. + */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 + /**************************************************************************** * Definitions ****************************************************************************/ -#define DUP_ISOPEN(fd, list) \ - ((unsigned int)fd < CONFIG_NFILE_DESCRIPTORS && \ - list->fl_files[fd].f_inode != NULL) - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -61,82 +66,44 @@ * Global Functions ****************************************************************************/ +/**************************************************************************** + * Name: dup + * + * Description: + * Clone a file or socket descriptor to an arbitray descriptor number + * + ****************************************************************************/ + int dup(int fildes) { - FAR struct filelist *list; - int fildes2; + /* Check the range of the descriptor to see if we got a file or a socket + * descriptor. */ - /* Get the thread-specific file list */ - - list = sched_getfiles(); - if (!list) + if ((unsigned int)fildes >= CONFIG_NFILE_DESCRIPTORS) { - *get_errno_ptr() = EMFILE; - return ERROR; - } - - /* Verify that fildes is a valid, open file descriptor */ + /* Not a vailid file descriptor. Did we get a valid socket descriptor? */ - if (!DUP_ISOPEN(fildes, list)) - { - *get_errno_ptr() = EBADF; - return ERROR; - } + if ((unsigned int)fildes < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS)) + { + /* Yes.. dup the socket descriptor */ - /* Increment the reference count on the contained inode */ + return net_dup(fildes); + } + else + { + /* No.. then it is a bad descriptor number */ - inode_addref(list->fl_files[fildes].f_inode); - - /* Then allocate a new file descriptor for the inode */ - - fildes2 = files_allocate(list->fl_files[fildes].f_inode, - list->fl_files[fildes].f_oflags, - list->fl_files[fildes].f_pos); - if (fildes2 < 0) - { - *get_errno_ptr() = EMFILE; - inode_release(list->fl_files[fildes].f_inode); - return ERROR; + errno = EBADF; + return ERROR; + } } - return fildes2; -} - -int dup2(int fildes1, int fildes2) -{ - FAR struct filelist *list; - - /* Get the thread-specific file list */ - - list = sched_getfiles(); - if (!list) + else { - *get_errno_ptr() = EMFILE; - return ERROR; - } + /* Its a valid file descriptor.. dup the file descriptor */ - /* Verify that fildes is a valid, open file descriptor */ - - if (!DUP_ISOPEN(fildes1, list)) - { - *get_errno_ptr() = EBADF; - return ERROR; + return file_dup(fildes); } - - /* Handle a special case */ - - if (fildes1 == fildes2) - { - return fildes1; - } - - /* Verify fildes2 */ - - if ((unsigned int)fildes2 >= CONFIG_NFILE_DESCRIPTORS) - { - *get_errno_ptr() = EBADF; - return ERROR; - } - - return files_dup(&list->fl_files[fildes1], &list->fl_files[fildes2]); } +#endif /* CONFIG_NFILE_DESCRIPTORS > 0 ... */ + -- cgit v1.2.3