Skip to content

Commit 407691d

Browse files
committed
lib: posix: add include/posix to search path and drop posix header prefix
It is a pain point to prefix standard POSIX APIs (e.g. `#include <posix/unistd.h>`). This is true, in particular, when integrating third-party code into an external module. With this, if users choose to include standard POSIX headers via Kconfig, then those headers will be part of the default search path. The headers themselves do not move. POSIX headers will not appear in the default search path unless so configured. Additional changes: * removed `posix/time.h` to circumvent `#include_next` workaround * gathered `NSEC_PER_MSEC` and `USEC_PER_MSEC` defs into `sys_clock.h` * restructured some syscalls (unistd, ioctl, clock_getttime) * sprinkled `#include <unistd.h>` where `close()` is used * removed a fair amount of duplicate code Fixes zephyrproject-rtos#43998 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 7dd3331 commit 407691d

File tree

114 files changed

+933
-862
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+933
-862
lines changed

arch/posix/include/posix_cheats.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void zephyr_app_main(void);
5858
}
5959
#endif
6060

61-
#ifdef CONFIG_POSIX_API
61+
#if defined(CONFIG_POSIX_API) || defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
6262

6363
/*
6464
* The defines below in this header exist only to enable the Zephyr POSIX API
@@ -220,6 +220,7 @@ void zephyr_app_main(void);
220220
#define unlink zap_unlink
221221
#define stat zap_stat
222222
#define mkdir zap_mkdir
223+
#define fcntl zap_fcntl
223224

224225
/* eventfd */
225226
#define eventfd zap_eventfd

include/zephyr/net/net_ip.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ struct in_addr {
164164
typedef unsigned short int sa_family_t;
165165

166166
/** Length of a socket address */
167+
# ifndef __socklen_t_defined
167168
typedef size_t socklen_t;
169+
# define __socklen_t_defined
170+
# endif
168171

169172
/*
170173
* Note that the sin_port and sin6_port are in network byte order

include/zephyr/net/sntp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#ifdef CONFIG_NET_SOCKETS_POSIX_NAMES
1212
#include <zephyr/net/socket.h>
1313
#else
14-
#include <zephyr/posix/sys/socket.h>
15-
#include <zephyr/posix/unistd.h>
16-
#include <zephyr/posix/poll.h>
14+
#include <sys/socket.h>
15+
#include <unistd.h>
16+
#include <poll.h>
1717
#endif
1818

1919
#ifdef __cplusplus

include/zephyr/net/socket.h

Lines changed: 8 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -681,243 +681,6 @@ int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
681681
char *host, socklen_t hostlen,
682682
char *serv, socklen_t servlen, int flags);
683683

684-
#if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
685-
686-
#define pollfd zsock_pollfd
687-
688-
/** POSIX wrapper for @ref zsock_socket */
689-
static inline int socket(int family, int type, int proto)
690-
{
691-
return zsock_socket(family, type, proto);
692-
}
693-
694-
/** POSIX wrapper for @ref zsock_socketpair */
695-
static inline int socketpair(int family, int type, int proto, int sv[2])
696-
{
697-
return zsock_socketpair(family, type, proto, sv);
698-
}
699-
700-
/** POSIX wrapper for @ref zsock_close */
701-
static inline int close(int sock)
702-
{
703-
return zsock_close(sock);
704-
}
705-
706-
/** POSIX wrapper for @ref zsock_shutdown */
707-
static inline int shutdown(int sock, int how)
708-
{
709-
return zsock_shutdown(sock, how);
710-
}
711-
712-
/** POSIX wrapper for @ref zsock_bind */
713-
static inline int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
714-
{
715-
return zsock_bind(sock, addr, addrlen);
716-
}
717-
718-
/** POSIX wrapper for @ref zsock_connect */
719-
static inline int connect(int sock, const struct sockaddr *addr,
720-
socklen_t addrlen)
721-
{
722-
return zsock_connect(sock, addr, addrlen);
723-
}
724-
725-
/** POSIX wrapper for @ref zsock_listen */
726-
static inline int listen(int sock, int backlog)
727-
{
728-
return zsock_listen(sock, backlog);
729-
}
730-
731-
/** POSIX wrapper for @ref zsock_accept */
732-
static inline int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
733-
{
734-
return zsock_accept(sock, addr, addrlen);
735-
}
736-
737-
/** POSIX wrapper for @ref zsock_send */
738-
static inline ssize_t send(int sock, const void *buf, size_t len, int flags)
739-
{
740-
return zsock_send(sock, buf, len, flags);
741-
}
742-
743-
/** POSIX wrapper for @ref zsock_recv */
744-
static inline ssize_t recv(int sock, void *buf, size_t max_len, int flags)
745-
{
746-
return zsock_recv(sock, buf, max_len, flags);
747-
}
748-
749-
/*
750-
* Need this wrapper because newer GCC versions got too smart and "typecheck"
751-
* even macros, so '#define fcntl zsock_fcntl' leads to error.
752-
*/
753-
static inline int zsock_fcntl_wrapper(int sock, int cmd, ...)
754-
{
755-
va_list args;
756-
int flags;
757-
758-
va_start(args, cmd);
759-
flags = va_arg(args, int);
760-
va_end(args);
761-
return zsock_fcntl(sock, cmd, flags);
762-
}
763-
764-
#define fcntl zsock_fcntl_wrapper
765-
766-
/** POSIX wrapper for @ref zsock_sendto */
767-
static inline ssize_t sendto(int sock, const void *buf, size_t len, int flags,
768-
const struct sockaddr *dest_addr,
769-
socklen_t addrlen)
770-
{
771-
return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen);
772-
}
773-
774-
/** POSIX wrapper for @ref zsock_sendmsg */
775-
static inline ssize_t sendmsg(int sock, const struct msghdr *message,
776-
int flags)
777-
{
778-
return zsock_sendmsg(sock, message, flags);
779-
}
780-
781-
/** POSIX wrapper for @ref zsock_recvfrom */
782-
static inline ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags,
783-
struct sockaddr *src_addr, socklen_t *addrlen)
784-
{
785-
return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
786-
}
787-
788-
/** POSIX wrapper for @ref zsock_poll */
789-
static inline int poll(struct zsock_pollfd *fds, int nfds, int timeout)
790-
{
791-
return zsock_poll(fds, nfds, timeout);
792-
}
793-
794-
/** POSIX wrapper for @ref zsock_getsockopt */
795-
static inline int getsockopt(int sock, int level, int optname,
796-
void *optval, socklen_t *optlen)
797-
{
798-
return zsock_getsockopt(sock, level, optname, optval, optlen);
799-
}
800-
801-
/** POSIX wrapper for @ref zsock_setsockopt */
802-
static inline int setsockopt(int sock, int level, int optname,
803-
const void *optval, socklen_t optlen)
804-
{
805-
return zsock_setsockopt(sock, level, optname, optval, optlen);
806-
}
807-
808-
/** POSIX wrapper for @ref zsock_getpeername */
809-
static inline int getpeername(int sock, struct sockaddr *addr,
810-
socklen_t *addrlen)
811-
{
812-
return zsock_getpeername(sock, addr, addrlen);
813-
}
814-
815-
/** POSIX wrapper for @ref zsock_getsockname */
816-
static inline int getsockname(int sock, struct sockaddr *addr,
817-
socklen_t *addrlen)
818-
{
819-
return zsock_getsockname(sock, addr, addrlen);
820-
}
821-
822-
/** POSIX wrapper for @ref zsock_getaddrinfo */
823-
static inline int getaddrinfo(const char *host, const char *service,
824-
const struct zsock_addrinfo *hints,
825-
struct zsock_addrinfo **res)
826-
{
827-
return zsock_getaddrinfo(host, service, hints, res);
828-
}
829-
830-
/** POSIX wrapper for @ref zsock_freeaddrinfo */
831-
static inline void freeaddrinfo(struct zsock_addrinfo *ai)
832-
{
833-
zsock_freeaddrinfo(ai);
834-
}
835-
836-
/** POSIX wrapper for @ref zsock_gai_strerror */
837-
static inline const char *gai_strerror(int errcode)
838-
{
839-
return zsock_gai_strerror(errcode);
840-
}
841-
842-
/** POSIX wrapper for @ref zsock_getnameinfo */
843-
static inline int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
844-
char *host, socklen_t hostlen,
845-
char *serv, socklen_t servlen, int flags)
846-
{
847-
return zsock_getnameinfo(addr, addrlen, host, hostlen,
848-
serv, servlen, flags);
849-
}
850-
851-
#define addrinfo zsock_addrinfo
852-
853-
/** POSIX wrapper for @ref zsock_gethostname */
854-
static inline int gethostname(char *buf, size_t len)
855-
{
856-
return zsock_gethostname(buf, len);
857-
}
858-
859-
/** POSIX wrapper for @ref zsock_inet_pton */
860-
static inline int inet_pton(sa_family_t family, const char *src, void *dst)
861-
{
862-
return zsock_inet_pton(family, src, dst);
863-
}
864-
865-
/** POSIX wrapper for @ref zsock_inet_ntop */
866-
static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
867-
size_t size)
868-
{
869-
return zsock_inet_ntop(family, src, dst, size);
870-
}
871-
872-
/** POSIX wrapper for @ref ZSOCK_POLLIN */
873-
#define POLLIN ZSOCK_POLLIN
874-
/** POSIX wrapper for @ref ZSOCK_POLLOUT */
875-
#define POLLOUT ZSOCK_POLLOUT
876-
/** POSIX wrapper for @ref ZSOCK_POLLERR */
877-
#define POLLERR ZSOCK_POLLERR
878-
/** POSIX wrapper for @ref ZSOCK_POLLHUP */
879-
#define POLLHUP ZSOCK_POLLHUP
880-
/** POSIX wrapper for @ref ZSOCK_POLLNVAL */
881-
#define POLLNVAL ZSOCK_POLLNVAL
882-
883-
/** POSIX wrapper for @ref ZSOCK_MSG_PEEK */
884-
#define MSG_PEEK ZSOCK_MSG_PEEK
885-
/** POSIX wrapper for @ref ZSOCK_MSG_TRUNC */
886-
#define MSG_TRUNC ZSOCK_MSG_TRUNC
887-
/** POSIX wrapper for @ref ZSOCK_MSG_DONTWAIT */
888-
#define MSG_DONTWAIT ZSOCK_MSG_DONTWAIT
889-
/** POSIX wrapper for @ref ZSOCK_MSG_WAITALL */
890-
#define MSG_WAITALL ZSOCK_MSG_WAITALL
891-
892-
/** POSIX wrapper for @ref ZSOCK_SHUT_RD */
893-
#define SHUT_RD ZSOCK_SHUT_RD
894-
/** POSIX wrapper for @ref ZSOCK_SHUT_WR */
895-
#define SHUT_WR ZSOCK_SHUT_WR
896-
/** POSIX wrapper for @ref ZSOCK_SHUT_RDWR */
897-
#define SHUT_RDWR ZSOCK_SHUT_RDWR
898-
899-
/** POSIX wrapper for @ref DNS_EAI_BADFLAGS */
900-
#define EAI_BADFLAGS DNS_EAI_BADFLAGS
901-
/** POSIX wrapper for @ref DNS_EAI_NONAME */
902-
#define EAI_NONAME DNS_EAI_NONAME
903-
/** POSIX wrapper for @ref DNS_EAI_AGAIN */
904-
#define EAI_AGAIN DNS_EAI_AGAIN
905-
/** POSIX wrapper for @ref DNS_EAI_FAIL */
906-
#define EAI_FAIL DNS_EAI_FAIL
907-
/** POSIX wrapper for @ref DNS_EAI_NODATA */
908-
#define EAI_NODATA DNS_EAI_NODATA
909-
/** POSIX wrapper for @ref DNS_EAI_MEMORY */
910-
#define EAI_MEMORY DNS_EAI_MEMORY
911-
/** POSIX wrapper for @ref DNS_EAI_SYSTEM */
912-
#define EAI_SYSTEM DNS_EAI_SYSTEM
913-
/** POSIX wrapper for @ref DNS_EAI_SERVICE */
914-
#define EAI_SERVICE DNS_EAI_SERVICE
915-
/** POSIX wrapper for @ref DNS_EAI_SOCKTYPE */
916-
#define EAI_SOCKTYPE DNS_EAI_SOCKTYPE
917-
/** POSIX wrapper for @ref DNS_EAI_FAMILY */
918-
#define EAI_FAMILY DNS_EAI_FAMILY
919-
#endif /* defined(CONFIG_NET_SOCKETS_POSIX_NAMES) */
920-
921684
#define IFNAMSIZ Z_DEVICE_MAX_NAME_LEN
922685

923686
/** Interface description structure */
@@ -1037,6 +800,14 @@ struct net_socket_register {
1037800

1038801
#include <syscalls/socket.h>
1039802

803+
#ifdef CONFIG_NET_SOCKETS_POSIX_NAMES
804+
/* These prefixed with posix as they can be used without CONFIG_POSIX_API */
805+
#include <zephyr/posix/arpa/inet.h>
806+
#include <zephyr/posix/netdb.h>
807+
#include <zephyr/posix/poll.h>
808+
#include <zephyr/posix/sys/socket.h>
809+
#endif
810+
1040811
/**
1041812
* @}
1042813
*/

include/zephyr/net/socket_select.h

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -106,46 +106,17 @@ void ZSOCK_FD_CLR(int fd, zsock_fd_set *set);
106106
*/
107107
void ZSOCK_FD_SET(int fd, zsock_fd_set *set);
108108

109-
#ifdef CONFIG_NET_SOCKETS_POSIX_NAMES
110-
111-
#define fd_set zsock_fd_set
112-
#define FD_SETSIZE ZSOCK_FD_SETSIZE
113-
114-
static inline int select(int nfds, zsock_fd_set *readfds,
115-
zsock_fd_set *writefds, zsock_fd_set *exceptfds,
116-
struct timeval *timeout)
117-
{
118-
return zsock_select(nfds, readfds, writefds, exceptfds, timeout);
119-
}
120-
121-
static inline void FD_ZERO(zsock_fd_set *set)
122-
{
123-
ZSOCK_FD_ZERO(set);
124-
}
125-
126-
static inline int FD_ISSET(int fd, zsock_fd_set *set)
127-
{
128-
return ZSOCK_FD_ISSET(fd, set);
129-
}
130-
131-
static inline void FD_CLR(int fd, zsock_fd_set *set)
132-
{
133-
ZSOCK_FD_CLR(fd, set);
134-
}
135-
136-
static inline void FD_SET(int fd, zsock_fd_set *set)
137-
{
138-
ZSOCK_FD_SET(fd, set);
139-
}
140-
141-
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
142-
143109
#ifdef __cplusplus
144110
}
145111
#endif
146112

147113
#include <syscalls/socket_select.h>
148114

115+
#ifdef CONFIG_NET_SOCKETS_POSIX_NAMES
116+
/* These prefixed with posix as they can be used without CONFIG_POSIX_API */
117+
#include <zephyr/posix/sys/select.h>
118+
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
119+
149120
/**
150121
* @}
151122
*/

include/zephyr/net/socketutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifdef CONFIG_NET_SOCKETS_POSIX_NAMES
1111
#include <zephyr/net/socket.h>
1212
#else
13-
#include <zephyr/posix/netdb.h>
13+
#include <netdb.h>
1414
#endif
1515

1616
/**

include/zephyr/posix/mqueue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define ZEPHYR_INCLUDE_POSIX_MQUEUE_H_
99

1010
#include <zephyr/kernel.h>
11-
#include <zephyr/posix/time.h>
11+
#include <time.h>
1212
#include <fcntl.h>
1313
#include "posix_types.h"
1414
#include "sys/stat.h"

0 commit comments

Comments
 (0)