From 0873eee5514bc7ea10fa7a91b8121a55023f7aea Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Jun 2019 08:49:52 +0300 Subject: [PATCH 1/6] posix: Move sys/stat.h to minimal libc Newlib libc already provides sys/stat.h, so trying to have sys/stat.h on the level of POSIX subsys inevitable leads to include order and definition conflicts. Instead (as most of other sys/* includes) should come from the underlying libc. While moving, made unrelated change of removing #include , to accommodate the change reviewers. Signed-off-by: Paul Sokolovsky --- include/posix/sys/stat.h | 54 ----------------------------- lib/libc/minimal/include/sys/stat.h | 46 ++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 57 deletions(-) delete mode 100644 include/posix/sys/stat.h diff --git a/include/posix/sys/stat.h b/include/posix/sys/stat.h deleted file mode 100644 index 7b4b2eadab80d..0000000000000 --- a/include/posix/sys/stat.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2018 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef ZEPHYR_INCLUDE_POSIX_SYS_STAT_H_ -#define ZEPHYR_INCLUDE_POSIX_SYS_STAT_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef CONFIG_POSIX_API -#include - -#define S_IRWXU 00700 -#define S_IRUSR 00400 -#define S_IWUSR 00200 -#define S_IXUSR 00100 - -#define S_IRWXG 00070 -#define S_IRGRP 00040 -#define S_IWGRP 00020 -#define S_IXGRP 00010 - -#define S_IRWXO 00007 -#define S_IROTH 00004 -#define S_IWOTH 00002 -#define S_IXOTH 00001 - -/* File open modes */ -#define O_ACCMODE 0003 -#define O_RDONLY 00 -#define O_WRONLY 01 -#define O_RDWR 02 - -#define SEEK_SET 0 /* Seek from beginning of file. */ -#define SEEK_CUR 1 /* Seek from current position. */ -#define SEEK_END 2 /* Seek from end of file. */ - -struct stat { - unsigned long st_size; - unsigned long st_blksize; - unsigned long st_blocks; -}; - -#endif /* CONFIG_POSIX_API */ - -#ifdef __cplusplus -} -#endif - -#endif /* ZEPHYR_INCLUDE_POSIX_SYS_STAT_H_ */ diff --git a/lib/libc/minimal/include/sys/stat.h b/lib/libc/minimal/include/sys/stat.h index 4636b54005063..54f05bdd64d48 100644 --- a/lib/libc/minimal/include/sys/stat.h +++ b/lib/libc/minimal/include/sys/stat.h @@ -1,9 +1,49 @@ /* - * Copyright (c) 2018 Linaro Limited + * Copyright (c) 2018 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ +#ifndef ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS_STAT_H_ +#define ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS_STAT_H_ -/* Dummy sys/stat.h to fulfill the requirements of certain libraries. - */ +#ifdef __cplusplus +extern "C" { +#endif + +#define S_IRWXU 00700 +#define S_IRUSR 00400 +#define S_IWUSR 00200 +#define S_IXUSR 00100 + +#define S_IRWXG 00070 +#define S_IRGRP 00040 +#define S_IWGRP 00020 +#define S_IXGRP 00010 + +#define S_IRWXO 00007 +#define S_IROTH 00004 +#define S_IWOTH 00002 +#define S_IXOTH 00001 + +/* File open modes */ +#define O_ACCMODE 0003 +#define O_RDONLY 00 +#define O_WRONLY 01 +#define O_RDWR 02 + +#define SEEK_SET 0 /* Seek from beginning of file. */ +#define SEEK_CUR 1 /* Seek from current position. */ +#define SEEK_END 2 /* Seek from end of file. */ + +struct stat { + unsigned long st_size; + unsigned long st_blksize; + unsigned long st_blocks; +}; + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS_STAT_H_ */ From ce8b3b04b811a810f044b2b326464e040f7463d2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Jun 2019 09:06:08 +0300 Subject: [PATCH 2/6] posix: struct timespec: Move definition to sys/_timespec.h POSIX subsys defines struct timespec in (as POSIX public API requires), but newlib defines in in sys/_timespec.h, which inevitably leads to inclusion order and redifinition conflicts. Follow newlib way and define it in single place, sys/_timespec.h, which belongs to libc namespace. Thus, we move current definition to minimal libc, and will use either minlibc's or newlib's definition, instead of trying to redefine it. This is similar to the introduction of sys/_timeval.h done earlier. Signed-off-by: Paul Sokolovsky --- include/posix/time.h | 9 +-------- lib/libc/minimal/include/sys/_timespec.h | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 lib/libc/minimal/include/sys/_timespec.h diff --git a/include/posix/time.h b/include/posix/time.h index 2838399cd6483..169bd73d7d89a 100644 --- a/include/posix/time.h +++ b/include/posix/time.h @@ -10,14 +10,7 @@ extern "C" { #endif - -#ifndef __timespec_defined -#define __timespec_defined -struct timespec { - signed int tv_sec; - signed int tv_nsec; -}; -#endif +#include /* Older newlib's like 2.{0-2}.0 don't define any newlib version defines, only * __NEWLIB_H__ so we use that to decide if itimerspec was defined in diff --git a/lib/libc/minimal/include/sys/_timespec.h b/lib/libc/minimal/include/sys/_timespec.h new file mode 100644 index 0000000000000..5e8364c772655 --- /dev/null +++ b/lib/libc/minimal/include/sys/_timespec.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2019 Linaro Limited + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS__TIMESPEC_H_ +#define ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS__TIMESPEC_H_ + +#include + +struct timespec { + time_t tv_sec; + long tv_nsec; +}; + +#endif /* ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS__TIMESPEC_H_ */ From aa6f218b5701017aaaba1af345f86f17c1dcd8a6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Jun 2019 14:54:37 +0300 Subject: [PATCH 3/6] posix: time.h: Add workaround for outdated newlib used by Xtensa Unfortunately, Zephyr SDK 0.10.0 ships with outdated Newlib 2.0.0 (from 2015 or earlier) which lacks sys/_timespec.h header, requiring ugly workaround of defining struct timespec inline (the whole idea was to standardize on sys/_timespec.h header for different libc's). This is similar to earlier workaround for struct timeval definition introduced in a6aee9b4c85642053d. Zephyr SDK ticket for this issue is https://github.com/zephyrproject-rtos/sdk-ng/issues/64, and it will ve possible to remove both workarounds when Xtensa toolchain will be upgraded to newlib version consistent with other architectures. Signed-off-by: Paul Sokolovsky --- include/posix/time.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/posix/time.h b/include/posix/time.h index 169bd73d7d89a..72b3b8b386b21 100644 --- a/include/posix/time.h +++ b/include/posix/time.h @@ -10,7 +10,30 @@ extern "C" { #endif +#ifdef CONFIG_NEWLIB_LIBC +/* Kludge to support outdated newlib version as used in SDK 0.10 for Xtensa */ +#include + +#ifdef __NEWLIB__ +/* Newever Newlib 3.x+ */ +#include +#else +/* Workaround for older Newlib 2.x, as used by Xtensa. It lacks sys/_timeval.h, + * so mimic it here. + */ +#include +#ifndef __timespec_defined +struct timespec { + time_t tv_sec; + long tv_nsec; +}; +#endif +#endif + +#else +/* Not Newlib */ #include +#endif /* CONFIG_NEWLIB_LIBC */ /* Older newlib's like 2.{0-2}.0 don't define any newlib version defines, only * __NEWLIB_H__ so we use that to decide if itimerspec was defined in From fa7903951218b8f3179d5940117e9659b23a6ee6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Jun 2019 12:35:57 +0300 Subject: [PATCH 4/6] posix: mqueue.h: Move O_CREAT and friends to fcntl.h That's the header which is supposed to define them, there was even FIXME on that in mqueue.h. Signed-off-by: Paul Sokolovsky --- include/posix/mqueue.h | 16 +--------------- lib/libc/minimal/include/fcntl.h | 2 ++ 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/include/posix/mqueue.h b/include/posix/mqueue.h index 3bb9a0e66407b..44c97932905c5 100644 --- a/include/posix/mqueue.h +++ b/include/posix/mqueue.h @@ -9,6 +9,7 @@ #include #include +#include #include "posix_types.h" #include "sys/stat.h" @@ -25,21 +26,6 @@ typedef struct mq_attr { long mq_curmsgs; /* Number of messages currently queued. */ } mq_attr; -/* FIXME: below should be defined into fcntl.h file. - * This is temporarily put here. - */ - -#ifndef _SYS_FCNTL_H_ -#define O_CREAT_POS 9 -#define O_CREAT (1 << O_CREAT_POS) - -#define O_EXCL_POS 11 -#define O_EXCL (1 << O_EXCL_POS) - -#define O_NONBLOCK_POS 14 -#define O_NONBLOCK (1 << O_NONBLOCK_POS) -#endif /* _SYS_FCNTL_H_ */ - mqd_t mq_open(const char *name, int oflags, ...); int mq_close(mqd_t mqdes); int mq_unlink(const char *name); diff --git a/lib/libc/minimal/include/fcntl.h b/lib/libc/minimal/include/fcntl.h index 453442d44c991..23028f35239e2 100644 --- a/lib/libc/minimal/include/fcntl.h +++ b/lib/libc/minimal/include/fcntl.h @@ -7,6 +7,8 @@ #ifndef ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_FCNTL_H_ #define ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_FCNTL_H_ +#define O_CREAT 0x0200 +#define O_EXCL 0x0800 #define O_NONBLOCK 0x4000 #define F_DUPFD 0 From 66a0f0d3415481531456626f99b1506331f478fb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 4 Jun 2019 23:14:22 +0300 Subject: [PATCH 5/6] posix: unistd.h: open() doesn't belong here Per POSIX, open() is defined in . fcntl.h in turn comes from the underlying libc, either newlib, or minimal libc. Signed-off-by: Paul Sokolovsky --- include/posix/unistd.h | 1 - lib/libc/minimal/include/fcntl.h | 2 ++ tests/posix/common/src/mqueue.c | 1 + tests/posix/fs/src/test_fs_dir.c | 1 + tests/posix/fs/src/test_fs_file.c | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/posix/unistd.h b/include/posix/unistd.h index d25006a557daf..fb1fe806b5cc9 100644 --- a/include/posix/unistd.h +++ b/include/posix/unistd.h @@ -21,7 +21,6 @@ extern "C" { #include /* File related operations */ -extern int open(const char *name, int flags); extern int close(int file); extern ssize_t write(int file, const void *buffer, size_t count); extern ssize_t read(int file, void *buffer, size_t count); diff --git a/lib/libc/minimal/include/fcntl.h b/lib/libc/minimal/include/fcntl.h index 23028f35239e2..caa79c91965f9 100644 --- a/lib/libc/minimal/include/fcntl.h +++ b/lib/libc/minimal/include/fcntl.h @@ -15,4 +15,6 @@ #define F_GETFL 3 #define F_SETFL 4 +int open(const char *name, int flags); + #endif /* ZEPHYR_LIB_LIBC_MINIMAL_INCLUDE_SYS_FCNTL_H_ */ diff --git a/tests/posix/common/src/mqueue.c b/tests/posix/common/src/mqueue.c index d7123ea64e672..c83f3751fbcc1 100644 --- a/tests/posix/common/src/mqueue.c +++ b/tests/posix/common/src/mqueue.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include diff --git a/tests/posix/fs/src/test_fs_dir.c b/tests/posix/fs/src/test_fs_dir.c index 46e4f0311f3c5..4b3d02ebe7c00 100644 --- a/tests/posix/fs/src/test_fs_dir.c +++ b/tests/posix/fs/src/test_fs_dir.c @@ -5,6 +5,7 @@ */ #include +#include #include #include #include "test_fs.h" diff --git a/tests/posix/fs/src/test_fs_file.c b/tests/posix/fs/src/test_fs_file.c index 5b6036a2cc6c8..614f99ac47dd6 100644 --- a/tests/posix/fs/src/test_fs_file.c +++ b/tests/posix/fs/src/test_fs_file.c @@ -5,6 +5,7 @@ */ #include +#include #include #include "test_fs.h" From 69b2bf8d1cea1f60cbe5a081148e3602b1150388 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 25 Jul 2019 15:53:55 +0300 Subject: [PATCH 6/6] libc: minimal: time.h: Don't (re)define struct timespec. By the latest convention, libc's define struct timespec in sys/_timespec.h. This is consistent with Newlib and ensures about errors due to redefinitions. Signed-off-by: Paul Sokolovsky --- lib/libc/minimal/include/time.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/libc/minimal/include/time.h b/lib/libc/minimal/include/time.h index 84c976f72c8d8..918ef864934e2 100644 --- a/lib/libc/minimal/include/time.h +++ b/lib/libc/minimal/include/time.h @@ -34,10 +34,7 @@ struct tm { typedef int64_t time_t; typedef int32_t suseconds_t; -struct timespec { - time_t tv_sec; - long tv_nsec; -}; +#include /* * Conversion between civil time and UNIX time. The companion