source: rtems/cpukit/libcsupport/src/open.c

Last change on this file was 1ad26cd, checked in by Sebastian Huber <sebastian.huber@…>, on Oct 18, 2018 at 10:38:58 AM

Support O_NOFOLLOW open() flag

Close #3546.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Open a File
5 *  @ingroup libcsupport
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2010.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  Modifications to support reference counting in the file system are
13 *  Copyright (c) 2012 embedded brains GmbH.
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.org/license/LICENSE.
18 */
19
20#if HAVE_CONFIG_H
21  #include "config.h"
22#endif
23
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <stdarg.h>
27#include <unistd.h>
28
29#include <rtems/libio_.h>
30
31static void create_regular_file(
32  rtems_filesystem_eval_path_context_t *ctx,
33  mode_t mode
34)
35{
36  int rv = 0;
37  const rtems_filesystem_location_info_t *currentloc = 
38    rtems_filesystem_eval_path_get_currentloc( ctx );
39  const char *token = rtems_filesystem_eval_path_get_token( ctx );
40  size_t tokenlen = rtems_filesystem_eval_path_get_tokenlen( ctx );
41
42  rv = rtems_filesystem_mknod(
43    currentloc,
44    token,
45    tokenlen,
46    S_IFREG | mode,
47    0
48  );
49
50  if ( rv == 0 ) {
51    /* The mode only applies to future accesses of the newly created file */
52    rtems_filesystem_eval_path_set_flags( ctx, 0 );
53
54    rtems_filesystem_eval_path_set_path( ctx, token, tokenlen );
55    rtems_filesystem_eval_path_continue( ctx );
56  } else {
57    rtems_filesystem_eval_path_error( ctx, 0 );
58  }
59}
60
61static int do_open(
62  rtems_libio_t *iop,
63  const char *path,
64  int oflag,
65  mode_t mode
66)
67{
68  int rv = 0;
69  int fd = rtems_libio_iop_to_descriptor( iop );
70  int rwflag = oflag + 1;
71  bool read_access = (rwflag & _FREAD) == _FREAD;
72  bool write_access = (rwflag & _FWRITE) == _FWRITE;
73  bool make = (oflag & O_CREAT) == O_CREAT;
74  bool exclusive = (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
75  bool truncate = (oflag & O_TRUNC) == O_TRUNC;
76  bool open_dir;
77#ifdef O_NOFOLLOW
78  int follow = (oflag & O_NOFOLLOW) == O_NOFOLLOW ? 0 : RTEMS_FS_FOLLOW_LINK;
79#else
80  int follow = RTEMS_FS_FOLLOW_LINK;
81#endif
82  int eval_flags = follow
83    | (read_access ? RTEMS_FS_PERMS_READ : 0)
84    | (write_access ? RTEMS_FS_PERMS_WRITE : 0)
85    | (make ? RTEMS_FS_MAKE : 0)
86    | (exclusive ?  RTEMS_FS_EXCLUSIVE : 0);
87  rtems_filesystem_eval_path_context_t ctx;
88
89  rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
90
91  if ( rtems_filesystem_eval_path_has_token( &ctx ) ) {
92    create_regular_file( &ctx, mode );
93  }
94
95#ifdef O_DIRECTORY
96  open_dir = ( oflag & O_DIRECTORY ) == O_DIRECTORY;
97#else
98  open_dir = false;
99#endif
100
101  if ( write_access || open_dir ) {
102    const rtems_filesystem_location_info_t *currentloc =
103      rtems_filesystem_eval_path_get_currentloc( &ctx );
104    mode_t type = rtems_filesystem_location_type( currentloc );
105
106    if ( write_access && S_ISDIR( type ) ) {
107      rtems_filesystem_eval_path_error( &ctx, EISDIR );
108    }
109
110    if ( open_dir && !S_ISDIR( type ) ) {
111      rtems_filesystem_eval_path_error( &ctx, ENOTDIR );
112    }
113  }
114
115  rtems_filesystem_eval_path_extract_currentloc( &ctx, &iop->pathinfo );
116  rtems_filesystem_eval_path_cleanup( &ctx );
117
118  _Atomic_Store_uint(
119    &iop->flags,
120    rtems_libio_fcntl_flags( oflag ),
121    ATOMIC_ORDER_RELAXED
122  );
123
124  rv = (*iop->pathinfo.handlers->open_h)( iop, path, oflag, mode );
125
126  if ( rv == 0 ) {
127    rtems_libio_iop_flags_set( iop, LIBIO_FLAGS_OPEN );
128
129    if ( truncate ) {
130      rv = ftruncate( fd, 0 );
131      if ( rv != 0 ) {
132        (*iop->pathinfo.handlers->close_h)( iop );
133      }
134    }
135
136    if ( rv == 0 ) {
137      rv = fd;
138    } else {
139      rv = -1;
140    }
141  }
142
143  if ( rv < 0 ) {
144    rtems_libio_free( iop );
145  }
146
147  return rv;
148}
149
150/**
151*  POSIX 1003.1 5.3.1 - Open a File
152*/
153int open( const char *path, int oflag, ... )
154{
155  int rv = 0;
156  va_list ap;
157  mode_t mode = 0;
158  rtems_libio_t *iop = NULL;
159
160  va_start( ap, oflag );
161
162  mode = va_arg( ap, mode_t );
163
164  iop = rtems_libio_allocate();
165  if ( iop != NULL ) {
166    rv = do_open( iop, path, oflag, mode );
167  } else {
168    errno = ENFILE;
169    rv = -1;
170  }
171
172  va_end( ap );
173
174  return rv;
175}
176
177
178
179#if defined(RTEMS_NEWLIB) && !defined(HAVE__OPEN_R)
180
181#include <reent.h>
182
183/**
184 *  This is the Newlib dependent reentrant version of open().
185 */
186int _open_r(
187  struct _reent *ptr RTEMS_UNUSED,
188  const char    *buf,
189  int            oflag,
190  int            mode
191)
192{
193  return open( buf, oflag, mode );
194}
195#endif
Note: See TracBrowser for help on using the repository browser.