source: rtems/cpukit/libcsupport/src/posix_devctl.c @ 3951d4d

Last change on this file since 3951d4d was 3951d4d, checked in by Ryan Long <ryan.long@…>, on 08/23/21 at 16:43:23

pxcdevctl: Adjust for standard

psxdevctl is supposed to return the value in errno. Before, it was
returning -1 and setting errno. Changed the tests to reflect these
changes. Added code from RRADE's posix_devctl.c.

Closes #4506

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2016, 2020 Joel Sherrill <joel@rtems.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#define _POSIX_26_C_SOURCE
32
33#include <devctl.h>
34#include <sys/ioctl.h>
35#include <rtems/seterr.h>
36
37#include  <unistd.h>
38#include  <fcntl.h>
39
40int posix_devctl(
41  int              fd,
42  int              dcmd,
43  void *__restrict dev_data_ptr,
44  size_t           nbyte,
45  int *__restrict  dev_info_ptr
46)
47{
48  int rv = 0;
49
50  /*
51   * posix_devctl() is supposed to return an errno. errno needs to be
52   * preserved in spite of calling methods (e.g., close, fcntl, and ioctl)
53   * that set it.
54   */
55  int errno_copy = errno;
56
57  /*
58   * The POSIX 1003.26 standard allows for library implementations
59   * that implement posix_devctl() using ioctl(). In this case,
60   * the extra parameters are largely ignored.
61   *
62   * The FACE Technical Standard requires only that FIONBIO
63   * be supported for sockets.
64   *
65   * This method appears to be rarely implemented and there are
66   * no known required use cases for this method beyond those
67   * from the FACE Technical Standard.
68   */
69
70  /*
71   * POSIX 1003.26 mentions that nbyte must be non-negative but this
72   * doesn't make sense because size_t is guaranteed to be unsigned.
73   */
74
75  /*
76   * Since this is implemented on top of ioctl(), the device information
77   * is not going to be passed down. Fill it in with zero so the behavior
78   * is defined.
79   */
80  if (dev_info_ptr != NULL) {
81    *dev_info_ptr = 0;
82  }
83
84  /*
85   *
86   */
87  switch (dcmd) {
88
89    /*
90     * The FACE Technical Standard Edition 3.0 and newer requires the SOCKCLOSE
91     * ioctl command. This is because the Security Profile does not include
92     * close() and applications need a way to close sockets. Closing sockets is
93     * a minimum requirement so using close() in the implementation meets that
94     * requirement but also lets the application close other file types.
95     */
96    case SOCKCLOSE:
97      if (close(fd) != 0) {
98        rv    = errno;
99        errno = errno_copy;
100
101        return rv;
102      }
103      break;
104
105    /*
106     * The FACE Technical Standard Edition 3.0 and newer requires the
107     * posix_devctl command to support the FIONBIO subcommand.
108     */
109    case FIONBIO: {
110      int tmp_flag;
111      int flag;
112
113      if (nbyte != sizeof(int)) {
114        return EINVAL;
115      }
116
117      tmp_flag = fcntl(fd, F_GETFL, 0);
118      if (tmp_flag == -1) {
119        rv = errno;
120        errno = errno_copy;
121
122        return rv;
123      }
124
125      flag = *(int *)dev_data_ptr;
126
127      if (flag != 0) {
128        tmp_flag |= O_NONBLOCK;
129      } else {
130        tmp_flag &= ~O_NONBLOCK;
131      }
132
133      (void) fcntl(fd, F_SETFL, tmp_flag);
134      break;
135    }
136
137    default:
138      if (ioctl(fd, dcmd, dev_data_ptr) != 0) {
139        rv = errno;
140        errno = errno_copy;
141
142        return rv;
143      }
144      break;
145  }
146
147  errno = errno_copy;
148
149  return rv;
150}
Note: See TracBrowser for help on using the repository browser.