source: rtems/cpukit/libcsupport/src/fchmod.c @ b2ed712

5
Last change on this file since b2ed712 was b2ed712, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 08:58:58

Include missing <string.h>

Update #2133.

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Change File Modes
5 *  @ingroup libcsupport
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2011.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18  #include "config.h"
19#endif
20
21#include <sys/stat.h>
22#include <string.h>
23
24#include <rtems/libio_.h>
25
26int rtems_filesystem_chmod(
27  const rtems_filesystem_location_info_t *loc,
28  mode_t mode
29)
30{
31  const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry;
32  int rv;
33
34  if ( mt_entry->writeable || rtems_filesystem_location_is_null( loc ) ) {
35    struct stat st;
36
37    memset( &st, 0, sizeof(st) );
38
39    rv = (*loc->handlers->fstat_h)( loc, &st );
40    if ( rv == 0 ) {
41      uid_t uid = geteuid();
42
43      if ( uid == 0 || st.st_uid == uid ) {
44        mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX;
45
46        mode = (st.st_mode & ~mask) | (mode & mask);
47
48        rv = (*mt_entry->ops->fchmod_h)( loc, mode );
49      } else {
50        errno = EPERM;
51        rv = -1;
52      }
53    }
54  } else {
55    errno = EROFS;
56    rv = -1;
57  }
58
59  return rv;
60}
61
62/**
63 *  POSIX 1003.1b 5.6.4 - Change File Modes
64 */
65int fchmod( int fd, mode_t mode )
66{
67  int rv;
68  rtems_libio_t *iop;
69
70  rtems_libio_check_fd( fd );
71  iop = rtems_libio_iop( fd );
72  rtems_libio_check_is_open(iop);
73
74  rtems_filesystem_instance_lock( &iop->pathinfo );
75
76  rv = rtems_filesystem_chmod( &iop->pathinfo, mode );
77
78  rtems_filesystem_instance_unlock( &iop->pathinfo );
79
80  return rv;
81}
Note: See TracBrowser for help on using the repository browser.