source: rtems/c/src/lib/libc/fcntl.c @ 2d733c42

4.104.114.84.95
Last change on this file since 2d733c42 was 2d733c42, checked in by Joel Sherrill <joel.sherrill@…>, on 01/20/99 at 15:48:22

More general fix based on bug report and patch from Ian Lance Taylor
<ian@…> to fix this problem:

There is a small bug in rtems_close in c/src/lib/libc/libio.c. It
does not check whether the file descriptor it is passed is open. This
can cause it to make a null dereference if it is passed a file
descriptor which is in the valid range but which was not opened, or
which was already closed.

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 *   fcntl() - POSIX 1003.1b 6.5.2 - File Control
3 *
4 *  COPYRIGHT (c) 1989-1998.
5 *  On-Line Applications Research Corporation (OAR).
6 *  Copyright assigned to U.S. Government, 1994.
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.OARcorp.com/rtems/license.html.
11 *
12 *  $Id$
13 */
14
15#include <unistd.h>
16#include <fcntl.h>
17#include <errno.h>
18
19#include <rtems.h>
20#include "libio_.h"
21
22int fcntl(
23  int fd,
24  int cmd,
25  ...
26)
27{
28  va_list        ap;
29  rtems_libio_t *iop;
30  rtems_libio_t *diop;
31  int            fd2;
32 
33  va_start( ap, cmd );
34
35  rtems_libio_check_fd( fd );
36  iop = rtems_libio_iop( fd );
37  rtems_libio_check_is_open(iop);
38
39  /*
40   *  If this is not a file system based entity, it is an error.
41   */
42
43  if ( iop->flags & LIBIO_FLAGS_HANDLER_MASK )
44    set_errno_and_return_minus_one( EBADF );
45
46  /*
47   *  Now process the fcntl().
48   */
49
50  /*
51   *  This switch should contain all the cases from POSIX.
52   */
53
54  switch ( cmd ) {
55    case F_DUPFD:        /* dup */
56      fd2 = va_arg( ap, int );
57      if ( fd2 )
58        diop = rtems_libio_iop( fd2 );
59      else {
60        /* allocate a file control block */
61        diop = rtems_libio_allocate();
62        if ( diop == 0 )
63          return -1;
64      }
65
66      diop->handlers   = iop->handlers;
67      diop->file_info  = iop->file_info;
68      diop->flags      = iop->flags;
69      diop->pathinfo   = iop->pathinfo;
70     
71      return 0;
72
73    case F_GETFD:        /* get f_flags */
74      if ( iop->flags & LIBIO_FLAGS_CLOSE_ON_EXEC )
75        return 1;
76      return 0;
77
78    case F_SETFD:        /* set f_flags */
79      /*
80       *  Interpret the third argument as the "close on exec()" flag.
81       *  If this argument is 1, then the file descriptor is to be closed
82       *  if a new process is exec()'ed.  Since RTEMS does not support
83       *  processes, then we can ignore this one except to make
84       *  F_GETFD work.
85       */
86
87      if ( va_arg( ap, int ) )
88        iop->flags |= LIBIO_FLAGS_CLOSE_ON_EXEC;
89      else
90        iop->flags &= ~LIBIO_FLAGS_CLOSE_ON_EXEC;
91      return 0;
92
93    case F_GETFL:        /* more flags (cloexec) */
94      return -1;
95
96    case F_SETFL:
97      return -1;
98
99    case F_GETLK:
100      return -1;
101
102    case F_SETLK:
103      return -1;
104
105    case F_SETLKW:
106      return -1;
107
108    case F_SETOWN:       /*  for sockets. */
109      return -1;
110
111    case F_GETOWN:       /*  for sockets. */
112      return -1;
113
114    default:
115      break;
116  }
117  return -1;
118}
Note: See TracBrowser for help on using the repository browser.