Skip to content

posix: header fixups #51771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
90b1950
lib: posix: standard include paths with CONFIG_POSIX_API
cfriedt Oct 27, 2022
17a871b
posix: rename posix_sched.h to sched.h
cfriedt Oct 28, 2022
bea5b9a
posix: treat pthread_key.h as an implementation detail
cfriedt Oct 28, 2022
45242f6
posix: getopt: select getopt by default with CONFIG_POSIX_API
cfriedt Oct 28, 2022
202f260
posix: eventfd: select eventfd by default with CONFIG_POSIX_API
cfriedt Oct 28, 2022
a540ae7
posix: fs: avoid adding open() alias with newlib and picolibc
cfriedt Oct 28, 2022
5dbbad3
posix: unistd.h: add declaration for _exit()
cfriedt Oct 28, 2022
b47f435
posix: avoid redefinition errors in network and posix headers
cfriedt Oct 28, 2022
39a6128
posix: mqueue: do not typedef mq_attr
cfriedt Oct 28, 2022
f089cb8
posix: ioctl: actually declare a function prototype
cfriedt Oct 28, 2022
6dbd9cd
posix: define in_port_t and in_addr_t in netinet/in.h
cfriedt Oct 28, 2022
7fb8b99
posix: ensure in_addr_t and in_port_t defined via arpa/inet.h
cfriedt Oct 28, 2022
81de964
posix: netdb: ensure that EAI constants are defined
cfriedt Oct 28, 2022
9a1c149
posix: ioctl: define FIONREAD as some applications require it
cfriedt Oct 28, 2022
e2103ab
posix: define struct linger in sys/socket.h
cfriedt Oct 28, 2022
97b51cc
posix: netdb: provide NI_MAXSERV for reasonable app defaults
cfriedt Oct 28, 2022
f19242a
net: socket: additional POSIX constants
cfriedt Oct 28, 2022
49a04c2
tests: posix: existence tests for standard POSIX includes
cfriedt Oct 27, 2022
6fd104c
net: sockets: conditionally include zephyr/posix/fcntl.h
cfriedt Jan 11, 2023
759dc8d
include: posix: add c++ extern scope
cfriedt Jan 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/zephyr/net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,16 @@ struct ifreq {
#define SO_KEEPALIVE 9
/** sockopt: Place out-of-band data into receive stream (ignored, for compatibility) */
#define SO_OOBINLINE 10
/** sockopt: Socket lingers on close (ignored, for compatibility) */
#define SO_LINGER 13
/** sockopt: Allow multiple sockets to reuse a single port (ignored, for compatibility) */
#define SO_REUSEPORT 15

/** sockopt: Receive low watermark (ignored, for compatibility) */
#define SO_RCVLOWAT 18
/** sockopt: Send low watermark (ignored, for compatibility) */
#define SO_SNDLOWAT 19

/**
* sockopt: Receive timeout
* Applies to receive functions like recv(), but not to connect()
Expand Down Expand Up @@ -1005,6 +1012,9 @@ struct ifreq {
/** sockopt: Enable SOCKS5 for Socket */
#define SO_SOCKS5 60

/** listen: The maximum backlog queue length (ignored, for compatibility) */
#define SOMAXCONN 128

/** @cond INTERNAL_HIDDEN */
/**
* @brief Registration information for a given BSD socket family.
Expand Down
9 changes: 9 additions & 0 deletions include/zephyr/posix/arpa/inet.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
#ifndef ZEPHYR_INCLUDE_POSIX_ARPA_INET_H_
#define ZEPHYR_INCLUDE_POSIX_ARPA_INET_H_

#include <stddef.h>

#include <zephyr/posix/netinet/in.h>
#include <zephyr/posix/sys/socket.h>

#include <zephyr/net/socket.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES

static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
size_t size)
{
Expand All @@ -23,6 +30,8 @@ static inline int inet_pton(sa_family_t family, const char *src, void *dst)
return zsock_inet_pton(family, src, dst);
}

#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 8 additions & 0 deletions include/zephyr/posix/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@
#define F_GETFL 3
#define F_SETFL 4

#ifdef __cplusplus
extern "C" {
#endif

int open(const char *name, int flags, ...);
int fcntl(int fildes, int cmd, ...);

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_POSIX_FCNTL_H_ */
8 changes: 4 additions & 4 deletions include/zephyr/posix/mqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@

#include <zephyr/kernel.h>
#include <zephyr/posix/time.h>
#include <fcntl.h>
#include <zephyr/posix/fcntl.h>
#include <zephyr/posix/sys/stat.h>
#include "posix_types.h"
#include "sys/stat.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef void *mqd_t;

typedef struct mq_attr {
struct mq_attr {
long mq_flags;
long mq_maxmsg;
long mq_msgsize;
long mq_curmsgs; /* Number of messages currently queued. */
} mq_attr;
};

mqd_t mq_open(const char *name, int oflags, ...);
int mq_close(mqd_t mqdes);
Expand Down
8 changes: 8 additions & 0 deletions include/zephyr/posix/net/if.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@

#include <zephyr/net/socket.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_NET_IF_H_ */
21 changes: 21 additions & 0 deletions include/zephyr/posix/netdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,29 @@

#include <zephyr/net/socket.h>

#ifndef NI_MAXSERV
/** Provide a reasonable size for apps using getnameinfo */
#define NI_MAXSERV 32
#endif

#define EAI_BADFLAGS DNS_EAI_BADFLAGS
#define EAI_NONAME DNS_EAI_NONAME
#define EAI_AGAIN DNS_EAI_AGAIN
#define EAI_FAIL DNS_EAI_FAIL
#define EAI_NODATA DNS_EAI_NODATA
#define EAI_MEMORY DNS_EAI_MEMORY
#define EAI_SYSTEM DNS_EAI_SYSTEM
#define EAI_SERVICE DNS_EAI_SERVICE
#define EAI_SOCKTYPE DNS_EAI_SOCKTYPE
#define EAI_FAMILY DNS_EAI_FAMILY
#define EAI_OVERFLOW DNS_EAI_OVERFLOW

#ifdef __cplusplus
extern "C" {
#endif

#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES

#define addrinfo zsock_addrinfo

static inline int getaddrinfo(const char *host, const char *service,
Expand Down Expand Up @@ -39,6 +58,8 @@ static inline int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
serv, servlen, flags);
}

#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 13 additions & 0 deletions include/zephyr/posix/netinet/in.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
#ifndef ZEPHYR_INCLUDE_POSIX_NETINET_IN_H_
#define ZEPHYR_INCLUDE_POSIX_NETINET_IN_H_

#include <stdint.h>

#include <zephyr/net/socket.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_NETINET_IN_H_ */
8 changes: 8 additions & 0 deletions include/zephyr/posix/netinet/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@

#include <zephyr/net/socket.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_NETINET_TCP_H_ */
4 changes: 4 additions & 0 deletions include/zephyr/posix/poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
extern "C" {
#endif

#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES

#define pollfd zsock_pollfd

#define POLLIN ZSOCK_POLLIN
Expand All @@ -25,6 +27,8 @@ static inline int poll(struct pollfd *fds, int nfds, int timeout)
return zsock_poll(fds, nfds, timeout);
}

#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions include/zephyr/posix/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <zephyr/posix/time.h>
#include <zephyr/posix/unistd.h>
#include "posix_types.h"
#include "posix_sched.h"
#include <zephyr/posix/pthread_key.h>
#include <zephyr/posix/sched.h>
#include "pthread_key.h"
#include <stdlib.h>
#include <string.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_POSIX_POSIX_SCHED_H_
#define ZEPHYR_INCLUDE_POSIX_POSIX_SCHED_H_
#ifndef ZEPHYR_INCLUDE_POSIX_SCHED_H_
#define ZEPHYR_INCLUDE_POSIX_SCHED_H_
Comment on lines -6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is the right thing to do, this will be a breaking change for the Zephyr users, upgrading to 3.3 from an older Zephyr version, who relied on the availability of posix_sched.h.

There should be a dummy posix_sched.h that prints out a deprecation warning and includes sched.h (can be removed after a few releases).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!


#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -40,4 +40,4 @@ int sched_get_priority_max(int policy);
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_POSIX_SCHED_H_ */
#endif /* ZEPHYR_INCLUDE_POSIX_SCHED_H_ */
11 changes: 11 additions & 0 deletions include/zephyr/posix/sys/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@
#define ZEPHYR_INCLUDE_POSIX_SYS_IOCTL_H_

#define FIONBIO 0x5421
#define FIONREAD 0x541B

#ifdef __cplusplus
extern "C" {
#endif

int ioctl(int fd, unsigned long request, ...);

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_SYS_IOCTL_H_ */
12 changes: 12 additions & 0 deletions include/zephyr/posix/sys/select.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
#include <zephyr/net/socket_select.h>
#include <sys/_timeval.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES

#define fd_set zsock_fd_set
#define FD_SETSIZE ZSOCK_FD_SETSIZE
#define FD_ZERO ZSOCK_FD_ZERO
Expand All @@ -26,4 +32,10 @@ static inline int select(int nfds, fd_set *readfds,
(struct zsock_timeval *)timeout);
}

#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_SYS_SELECT_H_ */
9 changes: 9 additions & 0 deletions include/zephyr/posix/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
extern "C" {
#endif

struct linger {
int l_onoff;
int l_linger;
};

#ifndef CONFIG_NET_SOCKETS_POSIX_NAMES

static inline int socket(int family, int type, int proto)
{
return zsock_socket(family, type, proto);
Expand Down Expand Up @@ -111,6 +118,8 @@ static inline int getsockname(int sock, struct sockaddr *addr,
return zsock_getsockname(sock, addr, addrlen);
}

#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions include/zephyr/posix/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extern int unlink(const char *path);
extern int stat(const char *path, struct stat *buf);
extern int mkdir(const char *path, mode_t mode);

FUNC_NORETURN void _exit(int status);

#ifdef CONFIG_NETWORKING
static inline int gethostname(char *buf, size_t len)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
zephyr_interface_library_named(posix_subsys)

if(CONFIG_POSIX_API)
target_include_directories(posix_subsys INTERFACE ${ZEPHYR_BASE}/include/zephyr/posix)
zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/posix)
endif()
Comment on lines 6 to 8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • When CONFIG_POSIX_API=y
    • applications have explicitly chosen to use
      POSIX APIs.
    • all standard POSIX includes are in the default
      include path.

While I find making the POSIX includes available in the default include path (i.e. without any prefixes) when applications have explicitly chosen to use POSIX APIs reasonable, we should still provide an option to keep using the zephyr/posix prefix (e.g. CONFIG_POSIX_INCLUDE_ZEPHYR_PREFIX) for backwards compatibility reasons and also for the sake of decoupling the include prefix behaviour from the rest of the semantics of the CONFIG_POSIX_API (i.e. CONFIG_POSIX_API does many things, from enabling POSIX I/O functions to compilation of perror.c).

  • When CONFIG_POSIX_API=n
    • applications may include POSIX headers
      explicitly with the namespaced prefix
    • e.g. #include <zephyr/posix/unistd.h>
    • individual Kconfig options can be used to
      enable POSIX features selectively, such as
      getopt or eventfd.

But does it really make sense to include POSIX headers or select the Kconfigs that are supposedly subordinate to the CONFIG_POSIX_API without selecting CONFIG_POSIX_API? Shouldn't these Kconfigs depend on CONFIG_POSIX_API?

Copy link
Member Author

@cfriedt cfriedt Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • When CONFIG_POSIX_API=y

    • applications have explicitly chosen to use
      POSIX APIs.
    • all standard POSIX includes are in the default
      include path.

While I find making the POSIX includes available in the default include path (i.e. without any prefixes) when applications have explicitly chosen to use POSIX APIs reasonable, we should still provide an option to keep using the zephyr/posix prefix (e.g. CONFIG_POSIX_INCLUDE_ZEPHYR_PREFIX) for backwards compatibility reasons and also for the sake of decoupling the include prefix behaviour from the rest of the semantics of the CONFIG_POSIX_API (i.e. CONFIG_POSIX_API does many things, from enabling POSIX I/O functions to compilation of perror.c).

Exactly - that is the default behaviour. I should specify, for all Zephyr internal code, we should also continue to use the <zephyr/posix/..> prefix, just to simplify. It does not make a lot of sense to have internal Zephyr code compile differently based on CONFIG_POSIX_API aside from maybe samples or tests. However, providing that option for applications and 3rd party code is important.

  • When CONFIG_POSIX_API=n

    • applications may include POSIX headers
      explicitly with the namespaced prefix
    • e.g. #include <zephyr/posix/unistd.h>
    • individual Kconfig options can be used to
      enable POSIX features selectively, such as
      getopt or eventfd.

But does it really make sense to include POSIX headers or select the Kconfigs that are supposedly subordinate to the CONFIG_POSIX_API without selecting CONFIG_POSIX_API? Shouldn't these Kconfigs depend on CONFIG_POSIX_API?

That's a valid argument, but technically there is nothing preventing any source file from including any header.

The "subordinate" kconfig options were done that way so that individual options could be built (i.e. linked in) without enabling all of them to be built. So CONFIG_POSIX_API is mainly a shorthand for enabling all of them (plus making sure that the headers are in the standard include path).


if(CONFIG_POSIX_API OR CONFIG_PTHREAD_IPC OR CONFIG_POSIX_CLOCK OR
Expand Down
1 change: 1 addition & 0 deletions lib/posix/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ config APP_LINK_WITH_POSIX_SUBSYS
config EVENTFD
bool "Support for eventfd"
depends on !ARCH_POSIX
default y if POSIX_API
help
Enable support for event file descriptors, eventfd. An eventfd can
be used as an event wait/notify mechanism together with POSIX calls
Expand Down
2 changes: 2 additions & 0 deletions lib/posix/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ int open(const char *name, int flags, ...)
return fd;
}

#if !defined(CONFIG_NEWLIB_LIBC) && !defined(CONFIG_PICOLIBC)
FUNC_ALIAS(open, _open, int);
#endif

static int fs_close_vmeth(void *obj)
{
Expand Down
1 change: 1 addition & 0 deletions lib/posix/getopt/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

menuconfig GETOPT
bool "Getopt library support"
default y if POSIX_API
Comment on lines 5 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this should not depends on POSIX_API?

Copy link
Member Author

@cfriedt cfriedt Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most POSIX options are like that already.

CONFIG_GETOPT is mainly there just to say to compile / link with getopt.c / .o .

In that sense, CONFIG_POSIX_API=y is a shortcut to enable all POSIX features, but people can choose to explicitly build-in only specific POSIX features. Of course, in that case CONFIG_POSIX_API needs to be n, and each specific feature must be manually selected.

help
This option adds support of getopt.
Different shell backends are use their own instance of getopt to
Expand Down
4 changes: 2 additions & 2 deletions lib/posix/mqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mqd_t mq_open(const char *name, int oflags, ...)
{
va_list va;
mode_t mode;
mq_attr *attrs = NULL;
struct mq_attr *attrs = NULL;
long msg_size = 0U, max_msgs = 0U;
mqueue_object *msg_queue;
mqueue_desc *msg_queue_desc = NULL, *mqd = (mqueue_desc *)(-1);
Expand All @@ -70,7 +70,7 @@ mqd_t mq_open(const char *name, int oflags, ...)
va_start(va, oflags);
if ((oflags & O_CREAT) != 0) {
mode = va_arg(va, PROMOTED_MODE_T);
attrs = va_arg(va, mq_attr*);
attrs = va_arg(va, struct mq_attr*);
}
va_end(va);

Expand Down
2 changes: 1 addition & 1 deletion lib/posix/pthread_sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include <zephyr/kernel.h>
#include <zephyr/posix/posix_sched.h>
#include <zephyr/posix/sched.h>

static bool valid_posix_policy(int policy)
{
Expand Down
11 changes: 8 additions & 3 deletions subsys/net/lib/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

/* libc headers */
#include <fcntl.h>

/* Zephyr headers */
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(net_sock, CONFIG_NET_SOCKETS_LOG_LEVEL);
Expand All @@ -17,6 +14,11 @@ LOG_MODULE_REGISTER(net_sock, CONFIG_NET_SOCKETS_LOG_LEVEL);
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/socket.h>
#include <zephyr/net/socket_types.h>
#ifdef CONFIG_ARCH_POSIX
#include <fcntl.h>
#else
#include <zephyr/posix/fcntl.h>
#endif
#include <zephyr/syscall_handler.h>
#include <zephyr/sys/fdtable.h>
#include <zephyr/sys/math_extras.h>
Expand Down Expand Up @@ -2167,6 +2169,9 @@ int zsock_setsockopt_ctx(struct net_context *ctx, int level, int optname,
return 0;
}

case SO_LINGER:
/* ignored. for compatibility purposes only */
return 0;
}

break;
Expand Down
Loading