2009-06-15 20:58:22 +02:00
|
|
|
/****************************************************************************
|
2014-10-06 18:53:25 +02:00
|
|
|
* fs/vfs/fs_dupfd.c
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
2020-03-30 01:36:19 +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
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
2020-03-30 01:36:19 +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.
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2013-09-28 22:47:49 +02:00
|
|
|
#include <assert.h>
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-04-16 03:04:17 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2009-06-15 20:58:22 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-12-15 01:18:32 +01:00
|
|
|
* Pre-processor Definitions
|
2009-06-15 20:58:22 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
#define DUP_ISOPEN(filep) (filep->f_inode != NULL)
|
2009-06-15 20:58:22 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-10-06 18:53:25 +02:00
|
|
|
* Public Functions
|
2009-06-15 20:58:22 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-10-06 18:53:25 +02:00
|
|
|
* Name: file_dup
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-10-06 18:53:25 +02:00
|
|
|
* Equivalent to the non-standard fs_dupfd() function except that it
|
2020-05-03 21:07:01 +02:00
|
|
|
* accepts a struct file instance instead of a file descriptor.
|
2017-09-30 18:41:21 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
2009-06-15 20:58:22 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
int file_dup(FAR struct file *filep, int minfd)
|
2009-06-15 20:58:22 +02:00
|
|
|
{
|
2013-09-29 00:50:07 +02:00
|
|
|
int fd2;
|
2020-03-30 01:36:19 +02:00
|
|
|
int ret;
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2013-09-29 00:50:07 +02:00
|
|
|
/* Verify that fd is a valid, open file descriptor */
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
if (!DUP_ISOPEN(filep))
|
2009-06-15 20:58:22 +02:00
|
|
|
{
|
2017-09-30 18:41:21 +02:00
|
|
|
return -EBADF;
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the reference count on the contained inode */
|
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
ret = inode_addref(filep->f_inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2009-06-15 20:58:22 +02:00
|
|
|
|
|
|
|
/* Then allocate a new file descriptor for the inode */
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
fd2 = files_allocate(filep->f_inode, filep->f_oflags, filep->f_pos, minfd);
|
2013-09-29 00:50:07 +02:00
|
|
|
if (fd2 < 0)
|
2009-06-15 20:58:22 +02:00
|
|
|
{
|
2014-10-06 18:53:25 +02:00
|
|
|
inode_release(filep->f_inode);
|
2017-09-30 18:41:21 +02:00
|
|
|
return -EMFILE;
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2013-09-29 00:50:07 +02:00
|
|
|
return fd2;
|
2009-06-15 20:58:22 +02:00
|
|
|
}
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
/****************************************************************************
|
2020-05-03 21:07:01 +02:00
|
|
|
* Name: fs_dupfd
|
2014-10-06 18:53:25 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-03-30 01:36:19 +02:00
|
|
|
* Clone a file descriptor 'fd' to an arbitrary descriptor number (any
|
2020-05-03 21:07:01 +02:00
|
|
|
* value greater than or equal to 'minfd').
|
2014-10-06 18:53:25 +02:00
|
|
|
*
|
2017-09-30 18:41:21 +02:00
|
|
|
* Returned Value:
|
2020-05-03 21:07:01 +02:00
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
2017-09-30 18:41:21 +02:00
|
|
|
*
|
2014-10-06 18:53:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int fs_dupfd(int fd, int minfd)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
2017-09-30 18:41:21 +02:00
|
|
|
int ret;
|
2014-10-06 18:53:25 +02:00
|
|
|
|
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
2014-10-06 18:53:25 +02:00
|
|
|
{
|
2020-05-03 21:07:01 +02:00
|
|
|
return ret;
|
2014-10-06 18:53:25 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
DEBUGASSERT(filep != NULL);
|
|
|
|
|
2014-10-06 18:53:25 +02:00
|
|
|
/* Let file_dup() do the real work */
|
|
|
|
|
2020-05-03 21:07:01 +02:00
|
|
|
return file_dup(filep, minfd);
|
2014-10-06 18:53:25 +02:00
|
|
|
}
|