source: rtems/cpukit/libcsupport/include/rtems/libio_.h @ 48dbb6cf

5
Last change on this file since 48dbb6cf was 48dbb6cf, checked in by Sebastian Huber <sebastian.huber@…>, on 09/13/17 at 07:08:34

libio: Remove rtems_libio_check_permissions()

Remove rtems_libio_check_permissions() and convert single user to
rtems_libio_check_permissions_with_error().

Update #3132.

  • Property mode set to 100644
File size: 23.5 KB
Line 
1/**
2 * @file
3 *
4 * @brief LibIO Internal Interface
5 *
6 * This file is the libio internal interface.
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2011.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  Modifications to support reference counting in the file system are
14 *  Copyright (c) 2012 embedded brains GmbH.
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#ifndef _RTEMS_RTEMS_LIBIO__H
22#define _RTEMS_RTEMS_LIBIO__H
23
24#include <sys/uio.h>
25#include <errno.h>
26#include <limits.h>
27#include <pthread.h>
28
29#include <rtems.h>
30#include <rtems/libio.h>
31#include <rtems/seterr.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/**
38 * @defgroup LibIOInternal IO Internal Library
39 *
40 * @brief Internal IO library API and implementation.
41 *
42 */
43/**@{**/
44
45#define RTEMS_FILESYSTEM_SYMLOOP_MAX 32
46
47/*
48 * Not defined in newlib so provide here. Users should use dup2 and
49 * not this non-portable fcntl command. Provided here to allow the
50 * RTEMS implementation to work.
51 */
52#define F_DUP2FD 20
53
54/*
55 *  Semaphore to protect the io table
56 */
57
58#define RTEMS_LIBIO_SEM         rtems_build_name('L', 'B', 'I', 'O')
59#define RTEMS_LIBIO_IOP_SEM(n)  rtems_build_name('L', 'B', 'I', n)
60
61extern rtems_id                          rtems_libio_semaphore;
62
63/*
64 *  File descriptor Table Information
65 */
66
67extern const uint32_t rtems_libio_number_iops;
68extern rtems_libio_t rtems_libio_iops[];
69extern rtems_libio_t *rtems_libio_iop_freelist;
70
71extern const rtems_filesystem_file_handlers_r rtems_filesystem_null_handlers;
72
73extern rtems_filesystem_mount_table_entry_t rtems_filesystem_null_mt_entry;
74
75/**
76 * @brief The global null location.
77 *
78 * Every operation and the open and fstat handlers of this location returns an
79 * error status.  The errno is not touched by these operations and handlers.
80 * The purpose of this location is to deliver the error return status for a
81 * previous error condition which must set the errno accordingly.
82 *
83 * The usage of this null location instead of the NULL pointer eliminates
84 * a lot of branches.
85 *
86 * The user environment root and current directory are statically initialized
87 * with the null location.  Due to that all file system services are in a
88 * defined state even if no root file system was mounted.
89 */
90extern rtems_filesystem_global_location_t rtems_filesystem_global_location_null;
91
92/**
93 * @brief Maps a file descriptor to the iop.
94 *
95 * The file descriptor must be a valid index into the iop table.
96 *
97 * @param[in] fd The file descriptor.
98 *
99 * @return The iop corresponding to the specified file descriptor.
100 *
101 * @see rtems_libio_check_fd().
102 */
103static inline rtems_libio_t *rtems_libio_iop( int fd )
104{
105  return &rtems_libio_iops[ fd ];
106}
107
108/*
109 *  rtems_libio_iop_to_descriptor
110 *
111 *  Macro to convert an internal file descriptor pointer (iop) into
112 *  the integer file descriptor used by the "section 2" system calls.
113 */
114
115#define rtems_libio_iop_to_descriptor(_iop) \
116  ((_iop) - &rtems_libio_iops[0])
117
118/*
119 *  rtems_libio_check_is_open
120 *
121 *  Macro to check if a file descriptor is actually open.
122 */
123
124#define rtems_libio_check_is_open(_iop) \
125  do {                                               \
126      if (((_iop)->flags & LIBIO_FLAGS_OPEN) == 0) { \
127          errno = EBADF;                             \
128          return -1;                                 \
129      }                                              \
130  } while (0)
131
132/*
133 *  rtems_libio_check_fd
134 *
135 *  Macro to check if a file descriptor number is valid.
136 */
137
138#define rtems_libio_check_fd(_fd) \
139  do {                                                     \
140      if ((uint32_t) (_fd) >= rtems_libio_number_iops) {   \
141          errno = EBADF;                                   \
142          return -1;                                       \
143      }                                                    \
144  } while (0)
145
146/*
147 *  rtems_libio_check_buffer
148 *
149 *  Macro to check if a buffer pointer is valid.
150 */
151
152#define rtems_libio_check_buffer(_buffer) \
153  do {                                    \
154      if ((_buffer) == 0) {               \
155          errno = EINVAL;                 \
156          return -1;                      \
157      }                                   \
158  } while (0)
159
160/*
161 *  rtems_libio_check_count
162 *
163 *  Macro to check if a count or length is valid.
164 */
165
166#define rtems_libio_check_count(_count) \
167  do {                                  \
168      if ((_count) == 0) {              \
169          return 0;                     \
170      }                                 \
171  } while (0)
172
173/*
174 *  rtems_libio_check_permissions_with_error
175 *
176 *  Macro to check if a file descriptor is open for this operation.
177 *  On failure, return the user specified error.
178 */
179
180#define rtems_libio_check_permissions_with_error(_iop, _flag, _errno) \
181  do {                                                      \
182      if (((_iop)->flags & (_flag)) == 0) {                 \
183            rtems_set_errno_and_return_minus_one( _errno ); \
184            return -1;                                      \
185      }                                                     \
186  } while (0)
187
188/**
189 * @brief Clones a node.
190 *
191 * The caller must hold the file system instance lock.
192 *
193 * @param[out] clone The cloned location.
194 * @param[in] master The master location.
195 *
196 * @see rtems_filesystem_instance_lock().
197 */
198void rtems_filesystem_location_clone(
199  rtems_filesystem_location_info_t *clone,
200  const rtems_filesystem_location_info_t *master
201);
202
203/**
204 * @brief Releases all resources of a location.
205 *
206 * This function may block on a mutex and may complete an unmount process.
207 *
208 * @param[in] loc The location to free.
209 *
210 * @note The file system root location is released by the file system
211 * instance destruction handler (see @ref rtems_filesystem_fsunmount_me_t).
212 *
213 * @see rtems_filesystem_freenode_t.
214 */
215void rtems_filesystem_location_free( rtems_filesystem_location_info_t *loc );
216
217/*
218 *  External structures
219 */
220#include <rtems/userenv.h>
221
222void rtems_libio_free_user_env( void *env );
223
224extern pthread_key_t rtems_current_user_env_key;
225
226static inline void rtems_libio_lock( void )
227{
228  rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
229}
230
231static inline void rtems_libio_unlock( void )
232{
233  rtems_semaphore_release( rtems_libio_semaphore );
234}
235
236static inline void rtems_filesystem_mt_lock( void )
237{
238  rtems_libio_lock();
239}
240
241static inline void rtems_filesystem_mt_unlock( void )
242{
243  rtems_libio_unlock();
244}
245
246extern rtems_interrupt_lock rtems_filesystem_mt_entry_lock_control;
247
248#define rtems_filesystem_mt_entry_declare_lock_context( ctx ) \
249  rtems_interrupt_lock_context ctx
250
251#define rtems_filesystem_mt_entry_lock( ctx ) \
252  rtems_interrupt_lock_acquire( &rtems_filesystem_mt_entry_lock_control, &ctx )
253
254#define rtems_filesystem_mt_entry_unlock( ctx ) \
255  rtems_interrupt_lock_release( &rtems_filesystem_mt_entry_lock_control, &ctx )
256
257static inline void rtems_filesystem_instance_lock(
258  const rtems_filesystem_location_info_t *loc
259)
260{
261  const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry;
262
263  (*mt_entry->ops->lock_h)( mt_entry );
264}
265
266static inline void rtems_filesystem_instance_unlock(
267  const rtems_filesystem_location_info_t *loc
268)
269{
270  const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry;
271
272  (*mt_entry->ops->unlock_h)( mt_entry );
273}
274
275/*
276 *  File Descriptor Routine Prototypes
277 */
278
279/**
280 * This routine searches the IOP Table for an unused entry.  If it
281 * finds one, it returns it.  Otherwise, it returns NULL.
282 */
283rtems_libio_t *rtems_libio_allocate(void);
284
285/**
286 * Convert UNIX fnctl(2) flags to ones that RTEMS drivers understand
287 */
288uint32_t rtems_libio_fcntl_flags( int fcntl_flags );
289
290/**
291 * Convert RTEMS internal flags to UNIX fnctl(2) flags
292 */
293int rtems_libio_to_fcntl_flags( uint32_t flags );
294
295/**
296 * This routine frees the resources associated with an IOP (file descriptor)
297 * and clears the slot in the IOP Table.
298 */
299void rtems_libio_free(
300  rtems_libio_t *iop
301);
302
303/*
304 *  File System Routine Prototypes
305 */
306
307rtems_filesystem_location_info_t *
308rtems_filesystem_eval_path_start(
309  rtems_filesystem_eval_path_context_t *ctx,
310  const char *path,
311  int eval_flags
312);
313
314rtems_filesystem_location_info_t *
315rtems_filesystem_eval_path_start_with_parent(
316  rtems_filesystem_eval_path_context_t *ctx,
317  const char *path,
318  int eval_flags,
319  rtems_filesystem_location_info_t *parentloc,
320  int parent_eval_flags
321);
322
323rtems_filesystem_location_info_t *
324rtems_filesystem_eval_path_start_with_root_and_current(
325  rtems_filesystem_eval_path_context_t *ctx,
326  const char *path,
327  size_t pathlen,
328  int eval_flags,
329  rtems_filesystem_global_location_t *const *global_root_ptr,
330  rtems_filesystem_global_location_t *const *global_current_ptr
331);
332
333void rtems_filesystem_eval_path_continue(
334  rtems_filesystem_eval_path_context_t *ctx
335);
336
337void rtems_filesystem_eval_path_cleanup(
338  rtems_filesystem_eval_path_context_t *ctx
339);
340
341void rtems_filesystem_eval_path_recursive(
342  rtems_filesystem_eval_path_context_t *ctx,
343  const char *path,
344  size_t pathlen
345);
346
347void rtems_filesystem_eval_path_cleanup_with_parent(
348  rtems_filesystem_eval_path_context_t *ctx,
349  rtems_filesystem_location_info_t *parentloc
350);
351
352/**
353 * @brief Requests a path evaluation restart.
354 *
355 * Sets the start and current location to the new start location.  The caller
356 * must terminate its current evaluation process.  The path evaluation
357 * continues in the next loop iteration within
358 * rtems_filesystem_eval_path_continue().  This avoids recursive invocations.
359 * The function obtains the new start location and clones it to set the new
360 * current location.  The previous start and current locations are released.
361 *
362 * @param[in, out] ctx The path evaluation context.
363 * @param[in, out] newstartloc_ptr Pointer to the new start location.
364 */
365void rtems_filesystem_eval_path_restart(
366  rtems_filesystem_eval_path_context_t *ctx,
367  rtems_filesystem_global_location_t **newstartloc_ptr
368);
369
370typedef enum {
371  RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_CONTINUE,
372  RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_DONE,
373  RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_NO_ENTRY
374} rtems_filesystem_eval_path_generic_status;
375
376/**
377 * @brief Tests if the current location is a directory.
378 *
379 * @param[in, out] ctx The path evaluation context.
380 * @param[in, out] arg The handler argument.
381 *
382 * @retval true The current location is a directory.
383 * @retval false Otherwise.
384 *
385 * @see rtems_filesystem_eval_path_generic().
386 */
387typedef bool (*rtems_filesystem_eval_path_is_directory)(
388  rtems_filesystem_eval_path_context_t *ctx,
389  void *arg
390);
391
392/**
393 * @brief Evaluates a token.
394 *
395 * @param[in, out] ctx The path evaluation context.
396 * @param[in, out] arg The handler argument.
397 * @param[in] token The token contents.
398 * @param[in] tokenlen The token length in characters.
399 *
400 * @retval status The generic path evaluation status.
401 *
402 * @see rtems_filesystem_eval_path_generic().
403 */
404typedef rtems_filesystem_eval_path_generic_status
405(*rtems_filesystem_eval_path_eval_token)(
406  rtems_filesystem_eval_path_context_t *ctx,
407  void *arg,
408  const char *token,
409  size_t tokenlen
410);
411
412typedef struct {
413  rtems_filesystem_eval_path_is_directory is_directory;
414  rtems_filesystem_eval_path_eval_token eval_token;
415} rtems_filesystem_eval_path_generic_config;
416
417void rtems_filesystem_eval_path_generic(
418  rtems_filesystem_eval_path_context_t *ctx,
419  void *arg,
420  const rtems_filesystem_eval_path_generic_config *config
421);
422
423void rtems_filesystem_initialize(void);
424
425/**
426 * @brief Copies a location.
427 *
428 * A bitwise copy is performed.  The destination location will be added to the
429 * corresponding mount entry.
430 *
431 * @param[out] dst The destination location.
432 * @param[in] src The  source location.
433 *
434 * @retval dst The destination location.
435 *
436 * @see rtems_filesystem_location_clone().
437 */
438rtems_filesystem_location_info_t *rtems_filesystem_location_copy(
439  rtems_filesystem_location_info_t *dst,
440  const rtems_filesystem_location_info_t *src
441);
442
443static inline rtems_filesystem_location_info_t *
444rtems_filesystem_location_initialize_to_null(
445  rtems_filesystem_location_info_t *loc
446)
447{
448  return rtems_filesystem_location_copy(
449    loc,
450    &rtems_filesystem_global_location_null.location
451  );
452}
453
454rtems_filesystem_global_location_t *
455rtems_filesystem_location_transform_to_global(
456  rtems_filesystem_location_info_t *loc
457);
458
459/**
460 * @brief Assigns a global file system location.
461 *
462 * @param[in, out] lhs_global_loc_ptr Pointer to the global left hand side file
463 * system location.  The current left hand side location will be released.
464 * @param[in] rhs_global_loc The global right hand side file system location.
465 */
466void rtems_filesystem_global_location_assign(
467  rtems_filesystem_global_location_t **lhs_global_loc_ptr,
468  rtems_filesystem_global_location_t *rhs_global_loc
469);
470
471/**
472 * @brief Obtains a global file system location.
473 *
474 * Deferred releases will be processed in this function.
475 *
476 * This function must be called from normal thread context and may block on a
477 * mutex.  Thread dispatching is disabled to protect some critical sections.
478 *
479 * @param[in] global_loc_ptr Pointer to the global file system location.
480 *
481 * @return A global file system location.  It returns always a valid object.
482 * In case of an error, the global null location will be returned.  Each
483 * operation or handler of the null location returns an error status.  The
484 * errno indicates the error.  The NULL pointer is never returned.
485 *
486 * @see rtems_filesystem_location_transform_to_global(),
487 * rtems_filesystem_global_location_obtain_null(), and
488 * rtems_filesystem_global_location_release().
489 */
490rtems_filesystem_global_location_t *rtems_filesystem_global_location_obtain(
491  rtems_filesystem_global_location_t *const *global_loc_ptr
492);
493
494/**
495 * @brief Releases a global file system location.
496 *
497 * In case the reference count reaches zero, all associated resources will be
498 * released.  This may include the complete unmount of the corresponding file
499 * system instance.
500 *
501 * This function may block on a mutex.  It may be called within critical
502 * sections of the operating system.  In this case the release will be
503 * deferred.  The next obtain call will do the actual release.
504 *
505 * @param[in] global_loc The global file system location.  It must not be NULL.
506 * @param[in] deferred If true, then do a deferred release, otherwise release
507 *   it immediately.
508 *
509 * @see rtems_filesystem_global_location_obtain().
510 */
511void rtems_filesystem_global_location_release(
512  rtems_filesystem_global_location_t *global_loc,
513  bool deferred
514);
515
516void rtems_filesystem_location_detach(
517  rtems_filesystem_location_info_t *detach
518);
519
520void rtems_filesystem_location_copy_and_detach(
521  rtems_filesystem_location_info_t *copy,
522  rtems_filesystem_location_info_t *detach
523);
524
525static inline rtems_filesystem_global_location_t *
526rtems_filesystem_global_location_obtain_null(void)
527{
528  rtems_filesystem_global_location_t *global_loc = NULL;
529
530  return rtems_filesystem_global_location_obtain( &global_loc );
531}
532
533static inline bool rtems_filesystem_location_is_null(
534  const rtems_filesystem_location_info_t *loc
535)
536{
537  return loc->handlers == &rtems_filesystem_null_handlers;
538}
539
540static inline bool rtems_filesystem_global_location_is_null(
541  const rtems_filesystem_global_location_t *global_loc
542)
543{
544  return rtems_filesystem_location_is_null( &global_loc->location );
545}
546
547static inline void rtems_filesystem_location_error(
548  const rtems_filesystem_location_info_t *loc,
549  int eno
550)
551{
552  if ( !rtems_filesystem_location_is_null( loc ) ) {
553    errno = eno;
554  }
555}
556
557int rtems_filesystem_mknod(
558  const rtems_filesystem_location_info_t *parentloc,
559  const char *name,
560  size_t namelen,
561  mode_t mode,
562  dev_t dev
563);
564
565int rtems_filesystem_chdir( rtems_filesystem_location_info_t *loc );
566
567int rtems_filesystem_chmod(
568  const rtems_filesystem_location_info_t *loc,
569  mode_t mode
570);
571
572int rtems_filesystem_chown(
573  const rtems_filesystem_location_info_t *loc,
574  uid_t owner,
575  gid_t group
576);
577
578static inline bool rtems_filesystem_is_ready_for_unmount(
579  rtems_filesystem_mount_table_entry_t *mt_entry
580)
581{
582  bool ready = !mt_entry->mounted
583    && rtems_chain_has_only_one_node( &mt_entry->location_chain )
584    && mt_entry->mt_fs_root->reference_count == 1;
585
586  if ( ready ) {
587    rtems_chain_initialize_empty( &mt_entry->location_chain );
588  }
589
590  return ready;
591}
592
593static inline void rtems_filesystem_location_add_to_mt_entry(
594  rtems_filesystem_location_info_t *loc
595)
596{
597  rtems_filesystem_mt_entry_declare_lock_context( lock_context );
598
599  rtems_filesystem_mt_entry_lock( lock_context );
600  rtems_chain_append_unprotected(
601    &loc->mt_entry->location_chain,
602    &loc->mt_entry_node
603  );
604  rtems_filesystem_mt_entry_unlock( lock_context );
605}
606
607void rtems_filesystem_location_remove_from_mt_entry(
608  rtems_filesystem_location_info_t *loc
609);
610
611void rtems_filesystem_do_unmount(
612  rtems_filesystem_mount_table_entry_t *mt_entry
613);
614
615static inline bool rtems_filesystem_location_is_instance_root(
616  const rtems_filesystem_location_info_t *loc
617)
618{
619  const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry;
620
621  return (*mt_entry->ops->are_nodes_equal_h)(
622    loc,
623    &mt_entry->mt_fs_root->location
624  );
625}
626
627static inline const char *rtems_filesystem_eval_path_get_path(
628  rtems_filesystem_eval_path_context_t *ctx
629)
630{
631  return ctx->path;
632}
633
634static inline size_t rtems_filesystem_eval_path_get_pathlen(
635  rtems_filesystem_eval_path_context_t *ctx
636)
637{
638  return ctx->pathlen;
639}
640
641static inline void rtems_filesystem_eval_path_set_path(
642  rtems_filesystem_eval_path_context_t *ctx,
643  const char *path,
644  size_t pathlen
645)
646{
647  ctx->path = path;
648  ctx->pathlen = pathlen;
649}
650
651static inline void rtems_filesystem_eval_path_clear_path(
652  rtems_filesystem_eval_path_context_t *ctx
653)
654{
655  ctx->pathlen = 0;
656}
657
658static inline const char *rtems_filesystem_eval_path_get_token(
659  rtems_filesystem_eval_path_context_t *ctx
660)
661{
662  return ctx->token;
663}
664
665static inline size_t rtems_filesystem_eval_path_get_tokenlen(
666  rtems_filesystem_eval_path_context_t *ctx
667)
668{
669  return ctx->tokenlen;
670}
671
672static inline void rtems_filesystem_eval_path_set_token(
673  rtems_filesystem_eval_path_context_t *ctx,
674  const char *token,
675  size_t tokenlen
676)
677{
678  ctx->token = token;
679  ctx->tokenlen = tokenlen;
680}
681
682static inline void rtems_filesystem_eval_path_clear_token(
683  rtems_filesystem_eval_path_context_t *ctx
684)
685{
686  ctx->tokenlen = 0;
687}
688
689static inline void rtems_filesystem_eval_path_put_back_token(
690  rtems_filesystem_eval_path_context_t *ctx
691)
692{
693  size_t tokenlen = ctx->tokenlen;
694
695  ctx->path -= tokenlen;
696  ctx->pathlen += tokenlen;
697  ctx->tokenlen = 0;
698}
699
700void rtems_filesystem_eval_path_eat_delimiter(
701  rtems_filesystem_eval_path_context_t *ctx
702);
703
704void rtems_filesystem_eval_path_next_token(
705  rtems_filesystem_eval_path_context_t *ctx
706);
707
708static inline void rtems_filesystem_eval_path_get_next_token(
709  rtems_filesystem_eval_path_context_t *ctx,
710  const char **token,
711  size_t *tokenlen
712)
713{
714  rtems_filesystem_eval_path_next_token(ctx);
715  *token = ctx->token;
716  *tokenlen = ctx->tokenlen;
717}
718
719static inline rtems_filesystem_location_info_t *
720rtems_filesystem_eval_path_get_currentloc(
721  rtems_filesystem_eval_path_context_t *ctx
722)
723{
724  return &ctx->currentloc;
725}
726
727static inline bool rtems_filesystem_eval_path_has_path(
728  const rtems_filesystem_eval_path_context_t *ctx
729)
730{
731  return ctx->pathlen > 0;
732}
733
734static inline bool rtems_filesystem_eval_path_has_token(
735  const rtems_filesystem_eval_path_context_t *ctx
736)
737{
738  return ctx->tokenlen > 0;
739}
740
741static inline int rtems_filesystem_eval_path_get_flags(
742  const rtems_filesystem_eval_path_context_t *ctx
743)
744{
745  return ctx->flags;
746}
747
748static inline void rtems_filesystem_eval_path_set_flags(
749  rtems_filesystem_eval_path_context_t *ctx,
750  int flags
751)
752{
753  ctx->flags = flags;
754}
755
756static inline void rtems_filesystem_eval_path_clear_and_set_flags(
757  rtems_filesystem_eval_path_context_t *ctx,
758  int clear,
759  int set
760)
761{
762  int flags = ctx->flags;
763
764  flags &= ~clear;
765  flags |= set;
766
767  ctx->flags = flags;
768}
769
770static inline void rtems_filesystem_eval_path_extract_currentloc(
771  rtems_filesystem_eval_path_context_t *ctx,
772  rtems_filesystem_location_info_t *get
773)
774{
775  rtems_filesystem_location_copy_and_detach(
776    get,
777    &ctx->currentloc
778  );
779}
780
781void rtems_filesystem_eval_path_error(
782  rtems_filesystem_eval_path_context_t *ctx,
783  int eno
784);
785
786/**
787 * @brief Checks that the locations exist in the same file system instance.
788 *
789 * @retval 0 The locations exist and are in the same file system instance.
790 * @retval -1 An error occurred.  The @c errno indicates the error.
791 */
792int rtems_filesystem_location_exists_in_same_instance_as(
793  const rtems_filesystem_location_info_t *a,
794  const rtems_filesystem_location_info_t *b
795);
796
797/**
798 * @brief Checks if access to an object is allowed for the current user.
799 *
800 * If the effective UID is zero or equals the UID of the object, then the user
801 * permission flags of the object will be used.  Otherwise if the effective GID
802 * is zero or equals the GID of the object or one of the supplementary group
803 * IDs is equal to the GID of the object, then the group permission flags of
804 * the object will be used.  Otherwise the other permission flags of the object
805 * will be used.
806 *
807 * @param[in] flags The flags determining the access type.  It can be
808 *   RTEMS_FS_PERMS_READ, RTEMS_FS_PERMS_WRITE or RTEMS_FS_PERMS_EXEC.
809 * @param[in] object_mode The mode of the object specifying the permission flags.
810 * @param[in] object_uid The UID of the object.
811 * @param[in] object_gid The GID of the object.
812 *
813 * @retval true Access is allowed.
814 * @retval false Otherwise.
815 */
816bool rtems_filesystem_check_access(
817  int flags,
818  mode_t object_mode,
819  uid_t object_uid,
820  gid_t object_gid
821);
822
823bool rtems_filesystem_eval_path_check_access(
824  rtems_filesystem_eval_path_context_t *ctx,
825  int eval_flags,
826  mode_t node_mode,
827  uid_t node_uid,
828  gid_t node_gid
829);
830
831static inline bool rtems_filesystem_is_delimiter(char c)
832{
833  return c == '/' || c == '\\';
834}
835
836static inline bool rtems_filesystem_is_current_directory(
837  const char *token,
838  size_t tokenlen
839)
840{
841  return tokenlen == 1 && token [0] == '.';
842}
843
844static inline bool rtems_filesystem_is_parent_directory(
845  const char *token,
846  size_t tokenlen
847)
848{
849  return tokenlen == 2 && token [0] == '.' && token [1] == '.';
850}
851
852static inline ssize_t rtems_libio_iovec_eval(
853  int fd,
854  const struct iovec *iov,
855  int iovcnt,
856  uint32_t flags,
857  rtems_libio_t **iopp
858)
859{
860  ssize_t        total;
861  int            v;
862  rtems_libio_t *iop;
863
864  /*
865   *  Argument validation on IO vector
866   */
867  if ( iov == NULL )
868    rtems_set_errno_and_return_minus_one( EINVAL );
869
870  if ( iovcnt <= 0 )
871    rtems_set_errno_and_return_minus_one( EINVAL );
872
873  if ( iovcnt > IOV_MAX )
874    rtems_set_errno_and_return_minus_one( EINVAL );
875
876  /*
877   *  OpenGroup says that you are supposed to return EINVAL if the
878   *  sum of the iov_len values in the iov array would overflow a
879   *  ssize_t.
880   */
881  total = 0;
882  for ( v = 0 ; v < iovcnt ; ++v ) {
883    size_t len = iov[ v ].iov_len;
884
885    if ( len > ( size_t ) ( SSIZE_MAX - total ) ) {
886      rtems_set_errno_and_return_minus_one( EINVAL );
887    }
888
889    total += ( ssize_t ) len;
890
891    if ( iov[ v ].iov_base == NULL && len != 0 ) {
892      rtems_set_errno_and_return_minus_one( EINVAL );
893    }
894  }
895
896  rtems_libio_check_fd( fd );
897  iop = rtems_libio_iop( fd );
898  rtems_libio_check_is_open( iop );
899  rtems_libio_check_permissions_with_error( iop, flags, EBADF );
900
901  *iopp = iop;
902
903  return total;
904}
905
906/**
907 * @brief Returns the file type of the file referenced by the filesystem
908 * location.
909 *
910 * @brief[in] loc The filesystem location.
911 *
912 * @return The type of the file or an invalid file type in case of an error.
913 */
914static inline mode_t rtems_filesystem_location_type(
915  const rtems_filesystem_location_info_t *loc
916)
917{
918  struct stat st;
919
920  st.st_mode = 0;
921  (void) ( *loc->handlers->fstat_h )( loc, &st );
922
923  return st.st_mode;
924}
925
926/** @} */
927
928#ifdef __cplusplus
929}
930#endif
931
932#endif
933/* end of include file */
Note: See TracBrowser for help on using the repository browser.