Skip to content

Commit

Permalink
treewide: remove inappropriate use of POSIX off_t and ssize_t
Browse files Browse the repository at this point in the history
A shortcut was taken many years ago by "cherry-picking" the
POSIX off_t and ssize_t types for use throughout Zephyr.

Additionally, the POSIX header fcntl.h, as well as constants
defined in that header, were being used inappropriately
throughout Zephyr.

Doing so created a dependency cycle: below POSIX's position
in the stack, code depended on POSIX; above POSIX's position
in the stack, code depends on the lower parts.

Such dependency cycles usually result in fragility and
instability of the software stack.

Use the newly defined k_off_t and k_ssize_t types throughout
Zephyr's stack, where off_t and ssize_t were previously used
inappropriately.

Additionally, use ZVFS-prefixed constants instead of their
POSIX counterparts.

Additionally, where appropriate, ensure the POSIX fcntl.h
header is prefixed with <zephyr/posix/fcntl.h>.

We effectively create a mutual dependency to resolve the
cyclic dependency, as described in GitHub issue #51211.

For clarification, it is inappropriate to use POSIX types or
functions within the kernel, core OS, OS services, or really
anywhere that is not equal to or above POSIX's position in the
software stack.

In other words, if a library uses POSIX, Zephyr's POSIX
implementation cannot depend on that library.

Similarly, if a system service uses POSIX, Zephyr's POSIX
implementation cannot depend on that system service.

Signed-off-by: Chris Friedt <[email protected]>
  • Loading branch information
Chris Friedt committed Jul 6, 2024
1 parent 809e11b commit 55290f0
Show file tree
Hide file tree
Showing 57 changed files with 431 additions and 498 deletions.
29 changes: 12 additions & 17 deletions drivers/modem/quectel-bg9x.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,9 @@ MODEM_CMD_DEFINE(on_cmd_unsol_rdy)
/* Func: send_socket_data
* Desc: This function will send "binary" data over the socket object.
*/
static ssize_t send_socket_data(struct modem_socket *sock,
const struct sockaddr *dst_addr,
struct modem_cmd *handler_cmds,
size_t handler_cmds_len,
const char *buf, size_t buf_len,
k_timeout_t timeout)
static k_ssize_t send_socket_data(struct modem_socket *sock, const struct sockaddr *dst_addr,
struct modem_cmd *handler_cmds, size_t handler_cmds_len,
const char *buf, size_t buf_len, k_timeout_t timeout)
{
int ret;
char send_buf[sizeof("AT+QISEND=##,####")] = {0};
Expand Down Expand Up @@ -517,9 +514,8 @@ static ssize_t send_socket_data(struct modem_socket *sock,
/* Func: offload_sendto
* Desc: This function will send data on the socket object.
*/
static ssize_t offload_sendto(void *obj, const void *buf, size_t len,
int flags, const struct sockaddr *to,
socklen_t tolen)
static k_ssize_t offload_sendto(void *obj, const void *buf, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen)
{
int ret;
struct modem_socket *sock = (struct modem_socket *) obj;
Expand Down Expand Up @@ -574,9 +570,8 @@ static ssize_t offload_sendto(void *obj, const void *buf, size_t len,
/* Func: offload_recvfrom
* Desc: This function will receive data on the socket object.
*/
static ssize_t offload_recvfrom(void *obj, void *buf, size_t len,
int flags, struct sockaddr *from,
socklen_t *fromlen)
static k_ssize_t offload_recvfrom(void *obj, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen)
{
struct modem_socket *sock = (struct modem_socket *)obj;
char sendbuf[sizeof("AT+QIRD=##,####")] = {0};
Expand Down Expand Up @@ -635,15 +630,15 @@ static ssize_t offload_recvfrom(void *obj, void *buf, size_t len,
/* Func: offload_read
* Desc: This function reads data from the given socket object.
*/
static ssize_t offload_read(void *obj, void *buffer, size_t count)
static k_ssize_t offload_read(void *obj, void *buffer, size_t count)
{
return offload_recvfrom(obj, buffer, count, 0, NULL, 0);
}

/* Func: offload_write
* Desc: This function writes data to the given socket object.
*/
static ssize_t offload_write(void *obj, const void *buffer, size_t count)
static k_ssize_t offload_write(void *obj, const void *buffer, size_t count)
{
return offload_sendto(obj, buffer, count, 0, NULL, 0);
}
Expand Down Expand Up @@ -811,9 +806,9 @@ static int offload_close(void *obj)
/* Func: offload_sendmsg
* Desc: This function sends messages to the modem.
*/
static ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
static k_ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
{
ssize_t sent = 0;
k_ssize_t sent = 0;
int rc;

LOG_DBG("msg_iovlen:%zd flags:%d", msg->msg_iovlen, flags);
Expand All @@ -840,7 +835,7 @@ static ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
}
}

return (ssize_t) sent;
return (k_ssize_t)sent;
}

/* Func: modem_rx
Expand Down
16 changes: 8 additions & 8 deletions drivers/modem/simcom-sim7080.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ static int offload_connect(void *obj, const struct sockaddr *addr, socklen_t add
* As terminating byte a STRG+Z (0x1A) is sent. The module will
* then send a OK or ERROR.
*/
static ssize_t offload_sendto(void *obj, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)
static k_ssize_t offload_sendto(void *obj, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)
{
int ret;
struct modem_socket *sock = (struct modem_socket *)obj;
Expand Down Expand Up @@ -372,8 +372,8 @@ MODEM_CMD_DEFINE(on_cmd_carecv)
/*
* Read data from a given socket.
*/
static ssize_t offload_recvfrom(void *obj, void *buf, size_t max_len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen)
static k_ssize_t offload_recvfrom(void *obj, void *buf, size_t max_len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen)
{
struct modem_socket *sock = (struct modem_socket *)obj;
char sendbuf[sizeof("AT+CARECV=##,####")];
Expand Down Expand Up @@ -446,10 +446,10 @@ static ssize_t offload_recvfrom(void *obj, void *buf, size_t max_len, int flags,
/*
* Sends messages to the modem.
*/
static ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
static k_ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
{
struct modem_socket *sock = obj;
ssize_t sent = 0;
k_ssize_t sent = 0;
const char *buf;
size_t len;
int ret;
Expand Down Expand Up @@ -516,15 +516,15 @@ static void socket_close(struct modem_socket *sock)
/*
* Offloads read by reading from a given socket.
*/
static ssize_t offload_read(void *obj, void *buffer, size_t count)
static k_ssize_t offload_read(void *obj, void *buffer, size_t count)
{
return offload_recvfrom(obj, buffer, count, 0, NULL, 0);
}

/*
* Offloads write by writing to a given socket.
*/
static ssize_t offload_write(void *obj, const void *buffer, size_t count)
static k_ssize_t offload_write(void *obj, const void *buffer, size_t count)
{
return offload_sendto(obj, buffer, count, 0, NULL, 0);
}
Expand Down
33 changes: 13 additions & 20 deletions drivers/modem/ublox-sara-r4.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ LOG_MODULE_REGISTER(modem_ublox_sara_r4, CONFIG_MODEM_LOG_LEVEL);
#include <zephyr/drivers/gpio.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/posix/fcntl.h>

#include <zephyr/net/net_if.h>
#include <zephyr/net/net_offload.h>
Expand Down Expand Up @@ -286,9 +285,7 @@ int modem_detect_apn(const char *imsi)
MODEM_CMD_DEFINE(on_cmd_sockwrite);

/* send binary data via the +USO[ST/WR] commands */
static ssize_t send_socket_data(void *obj,
const struct msghdr *msg,
k_timeout_t timeout)
static k_ssize_t send_socket_data(void *obj, const struct msghdr *msg, k_timeout_t timeout)
{
int ret;
char send_buf[sizeof("AT+USO**=###,"
Expand Down Expand Up @@ -446,11 +443,9 @@ static ssize_t send_socket_data(void *obj,

#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
/* send binary data via the +USO[ST/WR] commands */
static ssize_t send_cert(struct modem_socket *sock,
struct modem_cmd *handler_cmds,
size_t handler_cmds_len,
const char *cert_data, size_t cert_len,
int cert_type)
static k_ssize_t send_cert(struct modem_socket *sock, struct modem_cmd *handler_cmds,
size_t handler_cmds_len, const char *cert_data, size_t cert_len,
int cert_type)
{
int ret;
char *filename = "ca";
Expand Down Expand Up @@ -1599,9 +1594,8 @@ static int offload_connect(void *obj, const struct sockaddr *addr,
return 0;
}

static ssize_t offload_recvfrom(void *obj, void *buf, size_t len,
int flags, struct sockaddr *from,
socklen_t *fromlen)
static k_ssize_t offload_recvfrom(void *obj, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen)
{
struct modem_socket *sock = (struct modem_socket *)obj;
int ret, next_packet_size;
Expand Down Expand Up @@ -1684,9 +1678,8 @@ static ssize_t offload_recvfrom(void *obj, void *buf, size_t len,
return ret;
}

static ssize_t offload_sendto(void *obj, const void *buf, size_t len,
int flags, const struct sockaddr *to,
socklen_t tolen)
static k_ssize_t offload_sendto(void *obj, const void *buf, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen)
{
struct iovec msg_iov = {
.iov_base = (void *)buf,
Expand Down Expand Up @@ -1742,19 +1735,19 @@ static int offload_ioctl(void *obj, unsigned int request, va_list args)
}
}

static ssize_t offload_read(void *obj, void *buffer, size_t count)
static k_ssize_t offload_read(void *obj, void *buffer, size_t count)
{
return offload_recvfrom(obj, buffer, count, 0, NULL, 0);
}

static ssize_t offload_write(void *obj, const void *buffer, size_t count)
static k_ssize_t offload_write(void *obj, const void *buffer, size_t count)
{
return offload_sendto(obj, buffer, count, 0, NULL, 0);
}

static ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
static k_ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
{
ssize_t sent = 0;
k_ssize_t sent = 0;
int bkp_iovec_idx;
struct iovec bkp_iovec = {0};
struct msghdr crafted_msg = {
Expand Down Expand Up @@ -1830,7 +1823,7 @@ static ssize_t offload_sendmsg(void *obj, const struct msghdr *msg, int flags)
sent += ret;
}

return (ssize_t)sent;
return (k_ssize_t)sent;
}

#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
Expand Down
21 changes: 10 additions & 11 deletions drivers/net/nsos_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ LOG_MODULE_REGISTER(nsos_sockets);
#include <zephyr/net/net_ip.h>
#include <zephyr/net/offloaded_netdev.h>
#include <zephyr/net/socket_offload.h>
#include <zephyr/posix/fcntl.h>
#include <zephyr/sys/fdtable.h>
#include <zephyr/sys/dlist.h>

Expand Down Expand Up @@ -220,7 +219,7 @@ static int nsos_adapt_get_zephyr_errno(void)
return errno_from_nsos_mid(nsos_adapt_get_errno());
}

static ssize_t nsos_read(void *obj, void *buf, size_t sz)
static k_ssize_t nsos_read(void *obj, void *buf, size_t sz)
{
struct nsos_socket *sock = obj;
int ret;
Expand All @@ -233,7 +232,7 @@ static ssize_t nsos_read(void *obj, void *buf, size_t sz)
return ret;
}

static ssize_t nsos_write(void *obj, const void *buf, size_t sz)
static k_ssize_t nsos_write(void *obj, const void *buf, size_t sz)
{
struct nsos_socket *sock = obj;
int ret;
Expand Down Expand Up @@ -362,15 +361,15 @@ static int nsos_ioctl(void *obj, unsigned int request, va_list args)
case ZFD_IOCTL_POLL_OFFLOAD:
return -EOPNOTSUPP;

case F_GETFL: {
case ZVFS_F_GETFL: {
int flags;

flags = nsos_adapt_fcntl_getfl(sock->poll.mid.fd);

return fl_from_nsos_mid(flags);
}

case F_SETFL: {
case ZVFS_F_SETFL: {
int flags = va_arg(args, int);
int ret;

Expand Down Expand Up @@ -734,8 +733,8 @@ static int nsos_accept(void *obj, struct sockaddr *addr, socklen_t *addrlen)
return -1;
}

static ssize_t nsos_sendto(void *obj, const void *buf, size_t len, int flags,
const struct sockaddr *addr, socklen_t addrlen)
static k_ssize_t nsos_sendto(void *obj, const void *buf, size_t len, int flags,
const struct sockaddr *addr, socklen_t addrlen)
{
struct nsos_socket *sock = obj;
struct nsos_mid_sockaddr_storage addr_storage_mid;
Expand Down Expand Up @@ -773,7 +772,7 @@ static ssize_t nsos_sendto(void *obj, const void *buf, size_t len, int flags,
return ret;
}

static ssize_t nsos_sendmsg(void *obj, const struct msghdr *msg, int flags)
static k_ssize_t nsos_sendmsg(void *obj, const struct msghdr *msg, int flags)
{
struct nsos_socket *sock = obj;
struct nsos_mid_sockaddr_storage addr_storage_mid;
Expand Down Expand Up @@ -834,8 +833,8 @@ static ssize_t nsos_sendmsg(void *obj, const struct msghdr *msg, int flags)
return ret;
}

static ssize_t nsos_recvfrom(void *obj, void *buf, size_t len, int flags,
struct sockaddr *addr, socklen_t *addrlen)
static k_ssize_t nsos_recvfrom(void *obj, void *buf, size_t len, int flags,
struct sockaddr *addr, socklen_t *addrlen)
{
struct nsos_socket *sock = obj;
struct nsos_mid_sockaddr_storage addr_storage_mid;
Expand Down Expand Up @@ -873,7 +872,7 @@ static ssize_t nsos_recvfrom(void *obj, void *buf, size_t len, int flags,
return ret;
}

static ssize_t nsos_recvmsg(void *obj, struct msghdr *msg, int flags)
static k_ssize_t nsos_recvmsg(void *obj, struct msghdr *msg, int flags)
{
errno = ENOTSUP;
return -1;
Expand Down
21 changes: 8 additions & 13 deletions drivers/wifi/eswifi/eswifi_socket_offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ static int eswifi_socket_setsockopt(void *obj, int level, int optname,
return ret;
}

static ssize_t eswifi_socket_send(void *obj, const void *buf, size_t len,
int flags)
static k_ssize_t eswifi_socket_send(void *obj, const void *buf, size_t len, int flags)
{
int sock = OBJ_TO_SD(obj);
struct eswifi_off_socket *socket;
Expand Down Expand Up @@ -305,9 +304,8 @@ static ssize_t eswifi_socket_send(void *obj, const void *buf, size_t len,
return ret;
}

static ssize_t eswifi_socket_sendto(void *obj, const void *buf, size_t len,
int flags, const struct sockaddr *to,
socklen_t tolen)
static k_ssize_t eswifi_socket_sendto(void *obj, const void *buf, size_t len, int flags,
const struct sockaddr *to, socklen_t tolen)
{
if (to != NULL) {
errno = EOPNOTSUPP;
Expand All @@ -317,8 +315,7 @@ static ssize_t eswifi_socket_sendto(void *obj, const void *buf, size_t len,
return eswifi_socket_send(obj, buf, len, flags);
}

static ssize_t eswifi_socket_recv(void *obj, void *buf, size_t max_len,
int flags)
static k_ssize_t eswifi_socket_recv(void *obj, void *buf, size_t max_len, int flags)
{
int sock = OBJ_TO_SD(obj);
struct eswifi_off_socket *socket;
Expand Down Expand Up @@ -386,9 +383,8 @@ static ssize_t eswifi_socket_recv(void *obj, void *buf, size_t max_len,
return len;
}

static ssize_t eswifi_socket_recvfrom(void *obj, void *buf, size_t len,
int flags, struct sockaddr *from,
socklen_t *fromlen)
static k_ssize_t eswifi_socket_recvfrom(void *obj, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen)
{
if (fromlen != NULL) {
errno = EOPNOTSUPP;
Expand Down Expand Up @@ -628,13 +624,12 @@ static int eswifi_socket_ioctl(void *obj, unsigned int request, va_list args)
}
}

static ssize_t eswifi_socket_read(void *obj, void *buffer, size_t count)
static k_ssize_t eswifi_socket_read(void *obj, void *buffer, size_t count)
{
return eswifi_socket_recvfrom(obj, buffer, count, 0, NULL, 0);
}

static ssize_t eswifi_socket_write(void *obj, const void *buffer,
size_t count)
static k_ssize_t eswifi_socket_write(void *obj, const void *buffer, size_t count)
{
return eswifi_socket_sendto(obj, buffer, count, 0, NULL, 0);
}
Expand Down
Loading

0 comments on commit 55290f0

Please sign in to comment.