[libnbd PATCH v3 00/29] pass LISTEN_FDNAMES with systemd socket activation
Rich posted v2 at: https://listman.redhat.com/archives/libguestfs/2023-January/030555.html This is my stab at the feature, with a number of changes (that I felt were prerequisites) prepended. The series is structured as follows. Note that almost every segment of the series strongly depends on prior segment(s); in other words, the patches are in reasonably strict dependency order. - Patches #1 through #4 are generic, mostly mechanical, tree-wide cleanups, dealing with whitespace and reserved identifiers. - Patch #5 extracts a vector usage pattern we use 11 times in the tree to a new vector API called "name##_empty". - Patches #6 through #8 introduce an assert() variant that is async-signal-safe. This means we can make assertions in a child process before reaching one of the exec functions, even if the parent process was multi-threaded at the time of fork(). - Patches #9 and #10 introduce an execvpe() variant that is async-signal-safe. - Patches #11 through #25 implement the same series of cleanups *twice*, once for CONNECT_SA.START ("socket activation"), and another time for CONNECT_COMMAND.START. The table below describes this sub-structure: change summary CONNECT_SA CONNECT_COMMAND ---------------------------------------------- ---------- --------------- minor thinkos and warts #11 - #13 #18 - #21 check syscalls for errors in the child process #14 #22 centralize resource release #15 #23 plug resource leak on error #16 #24 replace execvp() call with fork-safe variant #17 #25 - Patches #26 through #29 implement the LISTEN_FDNAMES passing. This sub-series includes two patches from Rich's v2 series (with proper attribution). The series buils, and passes "make check" and "make check-valgrind", at every stage. In the Notes sections of some of the patches, there are lines of the form "context:-U<number>" or "context:-W". I've always wanted to control the number of context lines at the *individual* patch level -- for reviewing some patches, the default 3 is just fine, but for reviewing many other patches, significantly larger contexts are beneficial. I've finally bitten the bullet and written a (rather awkward) script that parses this "context:" note out of each patch (where "-W" is the short form of "--function--context") and applies it to formatting. That's why the context size varies over the series. The larger context size may make it harder to apply the series from the list if the master branch advances meanwhile. I don't expect this to happen, but in any case, I've passed "--base=master" to git-format-patch, so that the base commit (the current HEAD of the master branch) be noted at the end of the cover letter. That way the series can be applied precisely, and then rebased with a separate local step (because git has more information that way). Laszlo Laszlo Ersek (27): use space consistently in function and function-like macro invocations generator/C.ml: use space consistently in func. and func.-like macro calls socket activation: rename sa_(tmpdir|sockpath) to sact_(tmpdir|sockpath) ocaml: rename "sa_u" to "saddr_u" vector: (mostly) factor out DEFINE_VECTOR_EMPTY lib/utils: introduce xwrite() as a more robust write() lib/utils: add async-signal-safe assert() lib/utils: add unit test for async-signal-safe assert() lib/utils: introduce async-signal-safe execvpe() lib/utils: add unit tests for async-signal-safe execvpe() socket activation: fix error message upon asprintf() failure socket activation: clean up responsibilities of prep.sock.act.env.() socket activation: avoid manipulating the sign bit socket activation: check syscalls for errors in the child process socket activation: centralize resource release socket activation: plug AF_UNIX socket address (filesystem) leak on error socket activation: replace execvp() call with fork-safe variant CONNECT_COMMAND.START: fix small comment thinko about socket pair usage CONNECT_COMMAND.START: set the NBD error when fcntl() fails CONNECT_COMMAND.START: use symbolic constants for fd#0 and fd#1 CONNECT_COMMAND.START: sanitize close() calls in the child process CONNECT_COMMAND.START: check syscalls for errors in the child process CONNECT_COMMAND.START: centralize resource release CONNECT_COMMAND.START: plug child process leak on error CONNECT_COMMAND.START: replace execvp() call with fork-safe variant socket activation: generalize environment construction socket activation: set LISTEN_FDNAMES Richard W.M. Jones (2): common/include: Copy ascii-ctype functions from nbdkit generator: Add APIs to get/set the socket activation socket name .gitignore | 4 + common/include/Makefile.am | 6 + common/include/array-size.h | 2 +- common/include/ascii-ctype.h | 75 ++++ common/include/byte-swapping.h | 24 +- common/include/checked-overflow.h | 42 +- common/include/compiler-macros.h | 2 +- common/include/iszero.h | 2 +- common/include/minmax.h | 4 +- common/include/test-array-size.c | 26 +- common/include/test-ascii-ctype.c | 88 ++++ common/utils/const-string-vector.h | 2 +- common/utils/nbdkit-string.h | 2 +- common/utils/string-vector.h | 3 +- common/utils/test-human-size.c | 10 +- common/utils/test-vector.c | 7 +- common/utils/vector.h | 45 +- configure.ac | 5 + copy/file-ops.c | 2 +- copy/main.c | 2 +- copy/nbdcopy.h | 2 +- dump/dump.c | 2 +- examples/copy-libev.c | 30 +- examples/list-exports.c | 2 +- examples/strict-structured-reads.c | 2 +- examples/threaded-reads-and-writes.c | 2 +- fuse/nbdfuse.c | 2 +- generator/API.ml | 49 +++ generator/C.ml | 20 +- generator/states-connect-socket-activation.c | 294 +++++++++---- generator/states-connect.c | 123 ++++-- info/main.c | 2 +- info/show.c | 3 +- interop/interop.c | 2 +- lib/Makefile.am | 48 ++- lib/errors.c | 6 +- lib/handle.c | 80 +++- lib/internal.h | 112 +++-- lib/nbd-protocol.h | 12 +- lib/opt.c | 4 +- lib/test-fork-safe-assert.c | 63 +++ lib/test-fork-safe-assert.sh | 31 ++ lib/test-fork-safe-execvpe.c | 117 ++++++ lib/test-fork-safe-execvpe.sh | 270 ++++++++++++ lib/uri.c | 2 +- lib/utils.c | 430 +++++++++++++++++++- ocaml/helpers.c | 8 +- ocaml/nbd-c.h | 6 +- tests/eflags.c | 6 +- tests/get-size.c | 4 +- tests/newstyle-limited.c | 6 +- tests/oldstyle.c | 4 +- ublk/nbdublk.c | 4 +- ublk/tgt.c | 20 +- 54 files changed, 1771 insertions(+), 350 deletions(-) create mode 100644 common/include/ascii-ctype.h create mode 100644 common/include/test-ascii-ctype.c create mode 100644 lib/test-fork-safe-assert.c create mode 100755 lib/test-fork-safe-assert.sh create mode 100644 lib/test-fork-safe-execvpe.c create mode 100755 lib/test-fork-safe-execvpe.sh base-commit: 5a02c7d2cc6a201f9e5531c0c20c2f3c22b805a2
We intend to place a space character between the function designator and the opening parenthesis in a function call. We've been executing on that mostly consistently; fix the few exceptions now. The same convention should be applied to the invocations of function-like macros, and to instances of "__attribute__ ((attr))". (The latter is exemplified, although not consistently, by the GCC manual.) Implement this, by inserting the necessary spaces. Furthermore, some compiler attributes take multiple parameters, such as "nonnull". The GCC manual clearly shows that arguments passed to such attributes may be separated with ", " as well, not just ",". Use the former, as it's more readable. Finally, the C standard calls "defined" -- as in "#if defined identifier" and (equivalently) "#if defined (identifier)" -- a unary preprocessing operator. We can spell the parenthesized form as "defined (identifier)" rather than "defined(identifier)", so choose the former. I collected the locations possibly missing spaces with: git grep -EHn '\<[a-zA-Z0-9_]+\(' -- '*.c' '*.h' and then manually updated each as necessary. I didn't change occurrences in comments, except where the comment clearly indicated copying and pasting an expression into new code. "git show -w" outputs nothing for this patch. The test suite passes. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 72 ++++++++++---------- lib/nbd-protocol.h | 12 ++-- common/include/array-size.h | 2 +- common/include/byte-swapping.h | 24 +++---- common/include/checked-overflow.h | 42 ++++++------ common/include/compiler-macros.h | 2 +- common/include/iszero.h | 2 +- common/include/minmax.h | 4 +- common/utils/const-string-vector.h | 2 +- common/utils/nbdkit-string.h | 2 +- common/utils/string-vector.h | 2 +- common/utils/vector.h | 20 +++--- lib/errors.c | 6 +- lib/opt.c | 4 +- lib/uri.c | 2 +- lib/utils.c | 12 ++-- common/include/test-array-size.c | 26 +++---- common/utils/test-human-size.c | 10 +-- common/utils/test-vector.c | 4 +- ocaml/nbd-c.h | 6 +- tests/eflags.c | 6 +- tests/get-size.c | 4 +- tests/newstyle-limited.c | 6 +- tests/oldstyle.c | 4 +- examples/copy-libev.c | 30 ++++---- examples/list-exports.c | 2 +- examples/strict-structured-reads.c | 2 +- examples/threaded-reads-and-writes.c | 2 +- interop/interop.c | 2 +- copy/file-ops.c | 2 +- copy/main.c | 2 +- copy/nbdcopy.h | 2 +- dump/dump.c | 2 +- fuse/nbdfuse.c | 2 +- info/main.c | 2 +- ublk/nbdublk.c | 4 +- ublk/tgt.c | 20 +++--- 37 files changed, 175 insertions(+), 175 deletions(-) diff --git a/lib/internal.h b/lib/internal.h index bbbd26393f5a..c5827f4f0037 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -46,7 +46,7 @@ * debug and error handling code out of hot paths, making the hot path * into common functions use less instruction cache. */ -#if defined(__GNUC__) +#if defined (__GNUC__) #define unlikely(x) __builtin_expect (!!(x), 0) #define if_debug(h) if (unlikely ((h)->debug)) #else @@ -71,7 +71,7 @@ struct meta_context { char *name; /* Name of meta context. */ uint32_t context_id; /* Context ID negotiated with the server. */ }; -DEFINE_VECTOR_TYPE(meta_vector, struct meta_context); +DEFINE_VECTOR_TYPE (meta_vector, struct meta_context); struct export { char *name; @@ -220,20 +220,20 @@ struct nbd_handle { struct { struct nbd_fixed_new_option_reply_server server; char str[NBD_MAX_STRING * 2 + 1]; /* name, description, NUL */ - } __attribute__((packed)) server; + } __attribute__ ((packed)) server; struct nbd_fixed_new_option_reply_info_export export; struct nbd_fixed_new_option_reply_info_block_size block_size; struct { struct nbd_fixed_new_option_reply_info_name_or_desc info; char str[NBD_MAX_STRING]; - } __attribute__((packed)) name_desc; + } __attribute__ ((packed)) name_desc; struct { struct nbd_fixed_new_option_reply_meta_context context; char str[NBD_MAX_STRING]; - } __attribute__((packed)) context; + } __attribute__ ((packed)) context; char err_msg[NBD_MAX_STRING]; } payload; - } __attribute__((packed)) or; + } __attribute__ ((packed)) or; struct nbd_export_name_option_reply export_name_reply; struct nbd_simple_reply simple_reply; struct { @@ -245,9 +245,9 @@ struct nbd_handle { struct nbd_structured_reply_error error; char msg[NBD_MAX_STRING]; /* Common to all error types */ uint64_t offset; /* Only used for NBD_REPLY_TYPE_ERROR_OFFSET */ - } __attribute__((packed)) error; + } __attribute__ ((packed)) error; } payload; - } __attribute__((packed)) sr; + } __attribute__ ((packed)) sr; uint16_t gflags; uint32_t cflags; uint32_t len; @@ -387,26 +387,26 @@ struct command { /* aio.c */ extern void nbd_internal_retire_and_free_command (struct command *) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); /* connect.c */ extern int nbd_internal_wait_until_connected (struct nbd_handle *h) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); /* crypto.c */ extern struct socket *nbd_internal_crypto_create_session (struct nbd_handle *, struct socket *oldsock) - LIBNBD_ATTRIBUTE_NONNULL(1, 2); + LIBNBD_ATTRIBUTE_NONNULL (1, 2); extern bool nbd_internal_crypto_is_reading (struct nbd_handle *) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern int nbd_internal_crypto_handshake (struct nbd_handle *) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern void nbd_internal_crypto_debug_tls_enabled (struct nbd_handle *) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); /* debug.c */ extern void nbd_internal_debug (struct nbd_handle *h, const char *context, const char *fs, ...) - LIBNBD_ATTRIBUTE_NONNULL(1, 3); + LIBNBD_ATTRIBUTE_NONNULL (1, 3); #define debug(h, fs, ...) \ do { \ if_debug ((h)) \ @@ -420,10 +420,10 @@ extern void nbd_internal_debug (struct nbd_handle *h, const char *context, /* errors.c */ extern void nbd_internal_set_error_context (const char *context) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern const char *nbd_internal_get_error_context (void); extern void nbd_internal_set_last_error (int errnum, char *error) - LIBNBD_ATTRIBUTE_NONNULL(2); + LIBNBD_ATTRIBUTE_NONNULL (2); #define set_error(errnum, fs, ...) \ do { \ int _e = (errnum); \ @@ -442,16 +442,16 @@ extern void nbd_internal_set_last_error (int errnum, char *error) /* flags.c */ extern void nbd_internal_reset_size_and_flags (struct nbd_handle *h) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern int nbd_internal_set_size_and_flags (struct nbd_handle *h, uint64_t exportsize, uint16_t eflags) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern int nbd_internal_set_block_size (struct nbd_handle *h, uint32_t min, uint32_t pref, uint32_t max) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern void nbd_internal_set_payload (struct nbd_handle *h) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); /* is-state.c */ extern bool nbd_internal_is_state_created (enum state state); @@ -464,7 +464,7 @@ extern bool nbd_internal_is_state_closed (enum state state); /* opt.c */ extern void nbd_internal_free_option (struct nbd_handle *h) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); /* protocol.c */ extern int nbd_internal_errno_of_nbd_error (uint32_t error); @@ -476,7 +476,7 @@ extern int64_t nbd_internal_command_common (struct nbd_handle *h, uint64_t offset, uint64_t count, int count_err, void *data, struct command_cb *cb) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); /* socket.c */ struct socket *nbd_internal_socket_create (int fd); @@ -484,9 +484,9 @@ struct socket *nbd_internal_socket_create (int fd); /* states.c */ extern void nbd_internal_abort_commands (struct nbd_handle *h, struct command **list) - LIBNBD_ATTRIBUTE_NONNULL(1,2); + LIBNBD_ATTRIBUTE_NONNULL (1, 2); extern int nbd_internal_run (struct nbd_handle *h, enum external_event ev) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern const char *nbd_internal_state_short_string (enum state state); extern enum state_group nbd_internal_state_group (enum state state); extern enum state_group nbd_internal_state_group_parent (enum state_group group); @@ -498,24 +498,24 @@ extern int nbd_internal_aio_get_direction (enum state state); /* utils.c */ extern void nbd_internal_hexdump (const void *data, size_t len, FILE *fp) - LIBNBD_ATTRIBUTE_NONNULL(1,3); + LIBNBD_ATTRIBUTE_NONNULL (1, 3); extern int nbd_internal_copy_string_list (string_vector *v, char **in) - LIBNBD_ATTRIBUTE_NONNULL(1,2); + LIBNBD_ATTRIBUTE_NONNULL (1, 2); extern int nbd_internal_set_argv (struct nbd_handle *h, char **argv) - LIBNBD_ATTRIBUTE_NONNULL(1,2); + LIBNBD_ATTRIBUTE_NONNULL (1, 2); extern int nbd_internal_set_querylist (struct nbd_handle *h, char **queries) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern const char *nbd_internal_fork_safe_itoa (long v, char *buf, size_t len) - LIBNBD_ATTRIBUTE_NONNULL(2); + LIBNBD_ATTRIBUTE_NONNULL (2); extern void nbd_internal_fork_safe_perror (const char *s) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern char *nbd_internal_printable_buffer (const void *buf, size_t count) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_NONNULL (1); extern char *nbd_internal_printable_string (const char *str) - LIBNBD_ATTRIBUTE_ALLOC_DEALLOC(free) - LIBNBD_ATTRIBUTE_NONNULL(1); + LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free) + LIBNBD_ATTRIBUTE_NONNULL (1); extern char *nbd_internal_printable_string_list (char **list) - LIBNBD_ATTRIBUTE_ALLOC_DEALLOC(free); + LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free); /* These are wrappers around socket(2) and socketpair(2). They * always set SOCK_CLOEXEC. nbd_internal_socket can set SOCK_NONBLOCK @@ -525,6 +525,6 @@ extern int nbd_internal_socket (int domain, int type, int protocol, bool nonblock); extern int nbd_internal_socketpair (int domain, int type, int protocol, int *fds) - LIBNBD_ATTRIBUTE_NONNULL(4); + LIBNBD_ATTRIBUTE_NONNULL (4); #endif /* LIBNBD_INTERNAL_H */ diff --git a/lib/nbd-protocol.h b/lib/nbd-protocol.h index e5d6404b8eaa..9353668605ff 100644 --- a/lib/nbd-protocol.h +++ b/lib/nbd-protocol.h @@ -40,8 +40,8 @@ * these structures. */ -#if defined(__GNUC__) || defined(__clang__) -#define NBD_ATTRIBUTE_PACKED __attribute__((__packed__)) +#if defined (__GNUC__) || defined (__clang__) +#define NBD_ATTRIBUTE_PACKED __attribute__ ((__packed__)) #else #error "Please port to your compiler's notion of a packed struct" #endif @@ -58,8 +58,8 @@ struct nbd_old_handshake { char zeroes[124]; /* must be sent as zero bytes */ } NBD_ATTRIBUTE_PACKED; -#define NBD_MAGIC UINT64_C(0x4e42444d41474943) /* ASCII "NBDMAGIC" */ -#define NBD_OLD_VERSION UINT64_C(0x0000420281861253) +#define NBD_MAGIC UINT64_C (0x4e42444d41474943) /* ASCII "NBDMAGIC" */ +#define NBD_OLD_VERSION UINT64_C (0x0000420281861253) /* New-style handshake. */ struct nbd_new_handshake { @@ -68,7 +68,7 @@ struct nbd_new_handshake { uint16_t gflags; /* global flags */ } NBD_ATTRIBUTE_PACKED; -#define NBD_NEW_VERSION UINT64_C(0x49484156454F5054) /* ASCII "IHAVEOPT" */ +#define NBD_NEW_VERSION UINT64_C (0x49484156454F5054) /* ASCII "IHAVEOPT" */ /* New-style handshake option (sent by the client to us). */ struct nbd_new_option { @@ -95,7 +95,7 @@ struct nbd_fixed_new_option_reply { uint32_t replylen; } NBD_ATTRIBUTE_PACKED; -#define NBD_REP_MAGIC UINT64_C(0x3e889045565a9) +#define NBD_REP_MAGIC UINT64_C (0x3e889045565a9) /* Global flags. */ #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) diff --git a/common/include/array-size.h b/common/include/array-size.h index 3212d9dc6cdb..6f0a1ae87233 100644 --- a/common/include/array-size.h +++ b/common/include/array-size.h @@ -36,6 +36,6 @@ #include "compiler-macros.h" #define ARRAY_SIZE(a) \ - ((sizeof (a) / sizeof ((a)[0])) + BUILD_BUG_UNLESS_TRUE (TYPE_IS_ARRAY(a))) + ((sizeof (a) / sizeof ((a)[0])) + BUILD_BUG_UNLESS_TRUE (TYPE_IS_ARRAY (a))) #endif /* NBDKIT_ARRAY_SIZE_H */ diff --git a/common/include/byte-swapping.h b/common/include/byte-swapping.h index c53fba2e8f24..fce56df585ca 100644 --- a/common/include/byte-swapping.h +++ b/common/include/byte-swapping.h @@ -58,20 +58,20 @@ #ifdef __HAIKU__ #include <ByteOrder.h> -#define htobe16(x) B_HOST_TO_BENDIAN_INT16(x) -#define htole16(x) B_HOST_TO_LENDIAN_INT16(x) -#define be16toh(x) B_BENDIAN_TO_HOST_INT16(x) -#define le16toh(x) B_LENDIAN_TO_HOST_INT16(x) +#define htobe16(x) B_HOST_TO_BENDIAN_INT16 (x) +#define htole16(x) B_HOST_TO_LENDIAN_INT16 (x) +#define be16toh(x) B_BENDIAN_TO_HOST_INT16 (x) +#define le16toh(x) B_LENDIAN_TO_HOST_INT16 (x) -#define htobe32(x) B_HOST_TO_BENDIAN_INT32(x) -#define htole32(x) B_HOST_TO_LENDIAN_INT32(x) -#define be32toh(x) B_BENDIAN_TO_HOST_INT32(x) -#define le32toh(x) B_LENDIAN_TO_HOST_INT32(x) +#define htobe32(x) B_HOST_TO_BENDIAN_INT32 (x) +#define htole32(x) B_HOST_TO_LENDIAN_INT32 (x) +#define be32toh(x) B_BENDIAN_TO_HOST_INT32 (x) +#define le32toh(x) B_LENDIAN_TO_HOST_INT32 (x) -#define htobe64(x) B_HOST_TO_BENDIAN_INT64(x) -#define htole64(x) B_HOST_TO_LENDIAN_INT64(x) -#define be64toh(x) B_BENDIAN_TO_HOST_INT64(x) -#define le64toh(x) B_LENDIAN_TO_HOST_INT64(x) +#define htobe64(x) B_HOST_TO_BENDIAN_INT64 (x) +#define htole64(x) B_HOST_TO_LENDIAN_INT64 (x) +#define be64toh(x) B_BENDIAN_TO_HOST_INT64 (x) +#define le64toh(x) B_LENDIAN_TO_HOST_INT64 (x) #endif /* If we didn't define bswap_16, bswap_32 and bswap_64 already above, diff --git a/common/include/checked-overflow.h b/common/include/checked-overflow.h index 546c930b33d2..a1852adcdc7a 100644 --- a/common/include/checked-overflow.h +++ b/common/include/checked-overflow.h @@ -46,7 +46,7 @@ #ifndef NBDKIT_CHECKED_OVERFLOW_H #define NBDKIT_CHECKED_OVERFLOW_H -#if !defined(__GNUC__) && !defined(__clang__) +#if !defined (__GNUC__) && !defined (__clang__) #error "this file may need to be ported to your compiler" #endif @@ -66,9 +66,9 @@ * mathematical sum are stored to "*r". */ #if HAVE_DECL___BUILTIN_ADD_OVERFLOW -#define ADD_OVERFLOW(a, b, r) ADD_OVERFLOW_BUILTIN((a), (b), (r)) +#define ADD_OVERFLOW(a, b, r) ADD_OVERFLOW_BUILTIN ((a), (b), (r)) #else -#define ADD_OVERFLOW(a, b, r) ADD_OVERFLOW_FALLBACK((a), (b), (r)) +#define ADD_OVERFLOW(a, b, r) ADD_OVERFLOW_FALLBACK ((a), (b), (r)) #endif /* Multiply "a" and "b", both of (possibly different) unsigned integer types, @@ -83,9 +83,9 @@ * the mathematical product are stored to "*r". */ #if HAVE_DECL___BUILTIN_MUL_OVERFLOW -#define MUL_OVERFLOW(a, b, r) MUL_OVERFLOW_BUILTIN((a), (b), (r)) +#define MUL_OVERFLOW(a, b, r) MUL_OVERFLOW_BUILTIN ((a), (b), (r)) #else -#define MUL_OVERFLOW(a, b, r) MUL_OVERFLOW_FALLBACK((a), (b), (r)) +#define MUL_OVERFLOW(a, b, r) MUL_OVERFLOW_FALLBACK ((a), (b), (r)) #endif /* The ADD_OVERFLOW_BUILTIN and MUL_OVERFLOW_BUILTIN function-like macros @@ -99,22 +99,22 @@ * variably modified type. */ #if HAVE_DECL___BUILTIN_ADD_OVERFLOW -#define ADD_OVERFLOW_BUILTIN(a, b, r) \ - ({ \ - STATIC_ASSERT_UNSIGNED_INT (a); \ - STATIC_ASSERT_UNSIGNED_INT (b); \ - STATIC_ASSERT_UNSIGNED_INT (*(r)); \ - __builtin_add_overflow((a), (b), (r)); \ +#define ADD_OVERFLOW_BUILTIN(a, b, r) \ + ({ \ + STATIC_ASSERT_UNSIGNED_INT (a); \ + STATIC_ASSERT_UNSIGNED_INT (b); \ + STATIC_ASSERT_UNSIGNED_INT (*(r)); \ + __builtin_add_overflow ((a), (b), (r)); \ }) #endif #if HAVE_DECL___BUILTIN_MUL_OVERFLOW -#define MUL_OVERFLOW_BUILTIN(a, b, r) \ - ({ \ - STATIC_ASSERT_UNSIGNED_INT (a); \ - STATIC_ASSERT_UNSIGNED_INT (b); \ - STATIC_ASSERT_UNSIGNED_INT (*(r)); \ - __builtin_mul_overflow((a), (b), (r)); \ +#define MUL_OVERFLOW_BUILTIN(a, b, r) \ + ({ \ + STATIC_ASSERT_UNSIGNED_INT (a); \ + STATIC_ASSERT_UNSIGNED_INT (b); \ + STATIC_ASSERT_UNSIGNED_INT (*(r)); \ + __builtin_mul_overflow ((a), (b), (r)); \ }) #endif @@ -173,10 +173,10 @@ * * The expression "x" is not evaluated, unless it has variably modified type. */ -#define STATIC_ASSERT_UNSIGNED_INT(x) \ - do { \ - typedef char NBDKIT_UNIQUE_NAME(_x_has_uint_type)[(typeof (x))-1 > 0 ? 1 : -1] \ - __attribute__((__unused__)); \ +#define STATIC_ASSERT_UNSIGNED_INT(x) \ + do { \ + typedef char NBDKIT_UNIQUE_NAME (_x_has_uint_type)[(typeof (x))-1 > 0 ? 1 : -1] \ + __attribute__ ((__unused__)); \ } while (0) /* Assign the sum "a + b" to "*r", using uintmax_t modular arithmetic. diff --git a/common/include/compiler-macros.h b/common/include/compiler-macros.h index d41166614f95..7933bb87a5bf 100644 --- a/common/include/compiler-macros.h +++ b/common/include/compiler-macros.h @@ -48,7 +48,7 @@ #define BUILD_BUG_STRUCT_SIZE(cond) \ (sizeof (struct { int: (cond) ? 1 : -1; })) #define BUILD_BUG_UNLESS_TRUE(cond) \ - (BUILD_BUG_STRUCT_SIZE(cond) - BUILD_BUG_STRUCT_SIZE(cond)) + (BUILD_BUG_STRUCT_SIZE (cond) - BUILD_BUG_STRUCT_SIZE (cond)) #define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) diff --git a/common/include/iszero.h b/common/include/iszero.h index 7fdbb52c2a03..657ed5bac4ef 100644 --- a/common/include/iszero.h +++ b/common/include/iszero.h @@ -45,7 +45,7 @@ * See also: * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69908 */ -static inline bool __attribute__((__nonnull__ (1))) +static inline bool __attribute__ ((__nonnull__ (1))) is_zero (const char *buffer, size_t size) { size_t i; diff --git a/common/include/minmax.h b/common/include/minmax.h index 8d1fbf19bc23..8b0e6b7a4dbb 100644 --- a/common/include/minmax.h +++ b/common/include/minmax.h @@ -48,10 +48,10 @@ #undef MIN #define MIN(x, y) \ - MIN_1((x), (y), NBDKIT_UNIQUE_NAME(_x), NBDKIT_UNIQUE_NAME(_y)) + MIN_1 ((x), (y), NBDKIT_UNIQUE_NAME (_x), NBDKIT_UNIQUE_NAME (_y)) #undef MAX #define MAX(x, y) \ - MAX_1((x), (y), NBDKIT_UNIQUE_NAME(_x), NBDKIT_UNIQUE_NAME(_y)) + MAX_1 ((x), (y), NBDKIT_UNIQUE_NAME (_x), NBDKIT_UNIQUE_NAME (_y)) #ifdef HAVE_AUTO_TYPE diff --git a/common/utils/const-string-vector.h b/common/utils/const-string-vector.h index 91307ca1cf91..c15f8952b3b3 100644 --- a/common/utils/const-string-vector.h +++ b/common/utils/const-string-vector.h @@ -37,6 +37,6 @@ #include "vector.h" -DEFINE_VECTOR_TYPE(const_string_vector, const char *); +DEFINE_VECTOR_TYPE (const_string_vector, const char *); #endif /* CONST_STRING_VECTOR_H */ diff --git a/common/utils/nbdkit-string.h b/common/utils/nbdkit-string.h index 14998703b80a..b85f05991a82 100644 --- a/common/utils/nbdkit-string.h +++ b/common/utils/nbdkit-string.h @@ -37,6 +37,6 @@ #include "vector.h" -DEFINE_VECTOR_TYPE(string, char); +DEFINE_VECTOR_TYPE (string, char); #endif /* NBDKIT_STRING_H */ diff --git a/common/utils/string-vector.h b/common/utils/string-vector.h index 80d7311debfb..aa33fd48ceb5 100644 --- a/common/utils/string-vector.h +++ b/common/utils/string-vector.h @@ -37,6 +37,6 @@ #include "vector.h" -DEFINE_VECTOR_TYPE(string_vector, char *); +DEFINE_VECTOR_TYPE (string_vector, char *); #endif /* STRING_VECTOR_H */ diff --git a/common/utils/vector.h b/common/utils/vector.h index 60577eaefd74..fb2482c853ff 100644 --- a/common/utils/vector.h +++ b/common/utils/vector.h @@ -52,7 +52,7 @@ /* Use of this macro defines a new type called ‘name’ containing an * extensible vector of ‘type’ elements. For example: * - * DEFINE_VECTOR_TYPE(string_vector, char *) + * DEFINE_VECTOR_TYPE (string_vector, char *) * * defines a new type called ‘string_vector’ as a vector of ‘char *’. * You can create variables of this type: @@ -94,7 +94,7 @@ * allocated and capacity is increased, but the vector length is \ * not increased and the new elements are not initialized. \ */ \ - static inline int __attribute__((__unused__)) \ + static inline int __attribute__ ((__unused__)) \ name##_reserve (name *v, size_t n) \ { \ return generic_vector_reserve ((struct generic_vector *)v, n, \ @@ -104,7 +104,7 @@ /* Same as _reserve, but the allocation will be page aligned. Note \ * that the machine page size must be divisible by sizeof (type). \ */ \ - static inline int __attribute__((__unused__)) \ + static inline int __attribute__ ((__unused__)) \ name##_reserve_page_aligned (name *v, size_t n) \ { \ return generic_vector_reserve_page_aligned ((struct generic_vector *)v, \ @@ -112,7 +112,7 @@ } \ \ /* Insert at i'th element. i=0 => beginning i=len => append */ \ - static inline int __attribute__((__unused__)) \ + static inline int __attribute__ ((__unused__)) \ name##_insert (name *v, type elem, size_t i) \ { \ assert (i <= v->len); \ @@ -126,14 +126,14 @@ } \ \ /* Append a new element to the end of the vector. */ \ - static inline int __attribute__((__unused__)) \ + static inline int __attribute__ ((__unused__)) \ name##_append (name *v, type elem) \ { \ return name##_insert (v, elem, v->len); \ } \ \ /* Remove i'th element. i=0 => beginning i=len-1 => end */ \ - static inline void __attribute__((__unused__)) \ + static inline void __attribute__ ((__unused__)) \ name##_remove (name *v, size_t i) \ { \ assert (i < v->len); \ @@ -142,7 +142,7 @@ } \ \ /* Remove all elements and deallocate the vector. */ \ - static inline void __attribute__((__unused__)) \ + static inline void __attribute__ ((__unused__)) \ name##_reset (name *v) \ { \ free (v->ptr); \ @@ -151,7 +151,7 @@ } \ \ /* Iterate over the vector, calling f() on each element. */ \ - static inline void __attribute__((__unused__)) \ + static inline void __attribute__ ((__unused__)) \ name##_iter (name *v, void (*f) (type elem)) \ { \ size_t i; \ @@ -160,7 +160,7 @@ } \ \ /* Sort the elements of the vector. */ \ - static inline void __attribute__((__unused__)) \ + static inline void __attribute__ ((__unused__)) \ name##_sort (name *v, \ int (*compare) (const type *p1, const type *p2)) \ { \ @@ -170,7 +170,7 @@ /* Search for an exactly matching element in the vector using a \ * binary search. Returns a pointer to the element or NULL. \ */ \ - static inline type * __attribute__((__unused__)) \ + static inline type * __attribute__ ((__unused__)) \ name##_search (const name *v, const void *key, \ int (*compare) (const void *key, const type *v)) \ { \ diff --git a/lib/errors.c b/lib/errors.c index 8859ae739528..c601a204d2b9 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -35,13 +35,13 @@ struct last_error { /* Thread-local storage of the last error. */ static pthread_key_t errors_key; -static void free_errors_key (void *vp) LIBNBD_ATTRIBUTE_NONNULL(1); +static void free_errors_key (void *vp) LIBNBD_ATTRIBUTE_NONNULL (1); /* Constructor/destructor to create the thread-local key when the * library is loaded and unloaded. */ -static void errors_init (void) __attribute__((constructor)); -static void errors_free (void) __attribute__((destructor)); +static void errors_init (void) __attribute__ ((constructor)); +static void errors_free (void) __attribute__ ((destructor)); static void errors_init (void) diff --git a/lib/opt.c b/lib/opt.c index 776a3612056a..9346d6b5f792 100644 --- a/lib/opt.c +++ b/lib/opt.c @@ -30,13 +30,13 @@ static int opt_meta_context_queries (struct nbd_handle *h, uint32_t opt, char **queries, nbd_context_callback *context) - LIBNBD_ATTRIBUTE_NONNULL(1,4); + LIBNBD_ATTRIBUTE_NONNULL (1, 4); static int aio_opt_meta_context_queries (struct nbd_handle *h, uint32_t opt, char **queries, nbd_context_callback *context, nbd_completion_callback *complete) - LIBNBD_ATTRIBUTE_NONNULL(1,4,5); + LIBNBD_ATTRIBUTE_NONNULL (1, 4, 5); /* Internal function which frees an option with callback. */ void diff --git a/lib/uri.c b/lib/uri.c index ae5b9adbc3cc..367621d40208 100644 --- a/lib/uri.c +++ b/lib/uri.c @@ -403,7 +403,7 @@ nbd_unlocked_aio_connect_uri (struct nbd_handle *h, const char *raw_uri) static int append_query_params (char **query_params, const char *key, const char *value) - LIBNBD_ATTRIBUTE_NONNULL(1,2,3); + LIBNBD_ATTRIBUTE_NONNULL (1, 2, 3); char * nbd_unlocked_get_uri (struct nbd_handle *h) diff --git a/lib/utils.c b/lib/utils.c index 9c5c38060a29..c229ebfc6106 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -178,7 +178,7 @@ nbd_internal_fork_safe_itoa (long v, char *buf, size_t bufsize) return &buf[i]; } -#if defined(__GNUC__) +#if defined (__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-result" #endif @@ -213,7 +213,7 @@ nbd_internal_fork_safe_perror (const char *s) errno = err; } -#if defined(__GNUC__) +#if defined (__GNUC__) #pragma GCC diagnostic pop #endif @@ -334,10 +334,10 @@ nbd_internal_printable_string_list (char **list) } -int nbd_internal_socket(int domain, - int type, - int protocol, - bool nonblock) +int nbd_internal_socket (int domain, + int type, + int protocol, + bool nonblock) { int fd; diff --git a/common/include/test-array-size.c b/common/include/test-array-size.c index 1ce2142cd39c..8b0972aaabe1 100644 --- a/common/include/test-array-size.c +++ b/common/include/test-array-size.c @@ -41,21 +41,21 @@ struct st { const char *s; int i; }; -static const char *s0[] __attribute__((__unused__)) = { }; -static const char *s1[] __attribute__((__unused__)) = { "a" }; -static const char *s3[] __attribute__((__unused__)) = { "a", "b", "c" }; -static const char *s4[4] __attribute__((__unused__)) = { "a", "b", "c", "d" }; -static int i0[] __attribute__((__unused__)) = { }; -static int i1[] __attribute__((__unused__)) = { 1 }; -static int i3[] __attribute__((__unused__)) = { 1, 2, 3 }; -static int i4[4] __attribute__((__unused__)) = { 1, 2, 3, 4 }; -static struct st st0[] __attribute__((__unused__)) = { }; -static struct st st1[] __attribute__((__unused__)) = { { "a", 1 } }; -static struct st st3[] __attribute__((__unused__)) = +static const char *s0[] __attribute__ ((__unused__)) = { }; +static const char *s1[] __attribute__ ((__unused__)) = { "a" }; +static const char *s3[] __attribute__ ((__unused__)) = { "a", "b", "c" }; +static const char *s4[4] __attribute__ ((__unused__)) = { "a", "b", "c", "d" }; +static int i0[] __attribute__ ((__unused__)) = { }; +static int i1[] __attribute__ ((__unused__)) = { 1 }; +static int i3[] __attribute__ ((__unused__)) = { 1, 2, 3 }; +static int i4[4] __attribute__ ((__unused__)) = { 1, 2, 3, 4 }; +static struct st st0[] __attribute__ ((__unused__)) = { }; +static struct st st1[] __attribute__ ((__unused__)) = { { "a", 1 } }; +static struct st st3[] __attribute__ ((__unused__)) = { { "a", 1 }, { "b", 2 }, { "c", 3 } }; -static struct st st4[4] __attribute__((__unused__)) = +static struct st st4[4] __attribute__ ((__unused__)) = { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; -static struct st st4_0[4] __attribute__((__unused__)); +static struct st st4_0[4] __attribute__ ((__unused__)); int main (void) diff --git a/common/utils/test-human-size.c b/common/utils/test-human-size.c index 193f535f5ac6..2747ee67ffe6 100644 --- a/common/utils/test-human-size.c +++ b/common/utils/test-human-size.c @@ -73,13 +73,13 @@ main (int argc, char *argv[]) test (1048576, "1M", true); test (1048577, "1048577", false); - test (UINT64_C(1073741824), "1G", true); + test (UINT64_C (1073741824), "1G", true); - test (UINT64_C(1099511627776), "1T", true); - test (UINT64_C(1099511627777), "1099511627777", false); - test (UINT64_C(1099511627776) + 1024, "1073741825K", true); + test (UINT64_C (1099511627776), "1T", true); + test (UINT64_C (1099511627777), "1099511627777", false); + test (UINT64_C (1099511627776) + 1024, "1073741825K", true); - test (UINT64_C(1125899906842624), "1P", true); + test (UINT64_C (1125899906842624), "1P", true); test ((uint64_t)INT64_MAX+1, "8E", true); test (UINT64_MAX-1023, "18014398509481983K", true); diff --git a/common/utils/test-vector.c b/common/utils/test-vector.c index ecd5c1610e3c..55c8ebeb8a1e 100644 --- a/common/utils/test-vector.c +++ b/common/utils/test-vector.c @@ -50,8 +50,8 @@ #define APPENDS 1000000 -DEFINE_VECTOR_TYPE(int64_vector, int64_t); -DEFINE_VECTOR_TYPE(uint32_vector, uint32_t); +DEFINE_VECTOR_TYPE (int64_vector, int64_t); +DEFINE_VECTOR_TYPE (uint32_vector, uint32_t); static int compare (const int64_t *a, const int64_t *b) diff --git a/ocaml/nbd-c.h b/ocaml/nbd-c.h index 8b0c088da7ec..24505a0e0478 100644 --- a/ocaml/nbd-c.h +++ b/ocaml/nbd-c.h @@ -32,7 +32,7 @@ /* Workaround for OCaml < 4.06.0 */ #ifndef Bytes_val -#define Bytes_val(x) String_val(x) +#define Bytes_val(x) String_val (x) #endif /* Wrapper around caml_alloc_custom_mem for pre-2019 versions of OCaml. */ @@ -68,7 +68,7 @@ extern void nbd_internal_unix_sockaddr_to_sa (value, struct sockaddr_storage *, extern void nbd_internal_ocaml_exception_in_wrapper (const char *, value); /* Extract an NBD handle from an OCaml heap value. */ -#define NBD_val(v) (*((struct nbd_handle **)Data_custom_val(v))) +#define NBD_val(v) (*((struct nbd_handle **)Data_custom_val (v))) static struct custom_operations libnbd_custom_operations = { (char *) "libnbd_custom_operations", @@ -108,7 +108,7 @@ struct nbd_buffer { * whole struct is stored in the custom, not a pointer. This macro * returns a pointer to the struct. */ -#define NBD_buffer_val(v) ((struct nbd_buffer *)Data_custom_val(v)) +#define NBD_buffer_val(v) ((struct nbd_buffer *)Data_custom_val (v)) static struct custom_operations nbd_buffer_custom_operations = { (char *) "nbd_buffer_custom_operations", diff --git a/tests/eflags.c b/tests/eflags.c index 78fb6d3ec15a..023b7845c483 100644 --- a/tests/eflags.c +++ b/tests/eflags.c @@ -37,10 +37,10 @@ /* https://stackoverflow.com/a/1489985 */ #define XNBD_FLAG_FUNCTION(f) nbd_ ## f -#define NBD_FLAG_FUNCTION(f) XNBD_FLAG_FUNCTION(f) +#define NBD_FLAG_FUNCTION(f) XNBD_FLAG_FUNCTION (f) #define XSTR(x) #x -#define STR(x) XSTR(x) +#define STR(x) XSTR (x) int main (int argc, char *argv[]) @@ -108,7 +108,7 @@ main (int argc, char *argv[]) #error "unknown value" #endif - if ((r = NBD_FLAG_FUNCTION(flag) (nbd)) == -1) { + if ((r = NBD_FLAG_FUNCTION (flag) (nbd)) == -1) { fprintf (stderr, "%s\n", nbd_get_error ()); exit (EXIT_FAILURE); } diff --git a/tests/get-size.c b/tests/get-size.c index e6f44f776f13..d09e0d046d4c 100644 --- a/tests/get-size.c +++ b/tests/get-size.c @@ -32,7 +32,7 @@ #define SIZE 123456789 #define XSTR(s) #s -#define STR(s) XSTR(s) +#define STR(s) XSTR (s) int main (int argc, char *argv[]) @@ -41,7 +41,7 @@ main (int argc, char *argv[]) int64_t r; /* -n forces newstyle even if someone is still using nbdkit < 1.3 */ char *args[] = { "nbdkit", "-s", "--exit-with-parent", "-n", "-v", - "null", "size=" STR(SIZE), NULL }; + "null", "size=" STR (SIZE), NULL }; const char *s; nbd = nbd_create (); diff --git a/tests/newstyle-limited.c b/tests/newstyle-limited.c index a8f16bc88707..54cba442b28a 100644 --- a/tests/newstyle-limited.c +++ b/tests/newstyle-limited.c @@ -33,7 +33,7 @@ #define SIZE 65536 #define XSTR(s) #s -#define STR(s) XSTR(s) +#define STR(s) XSTR (s) static char wbuf[512] = { 1, 2, 3, 4 }, rbuf[512]; static const char *progname; @@ -105,9 +105,9 @@ main (int argc, char *argv[]) char *args[] = { "nbdkit", "-s", "--mask-handshake", "0", "--exit-with-parent", "-v", "eval", "list_exports=printf a\\nb\\n", - "get_size=echo " STR(SIZE), + "get_size=echo " STR (SIZE), "open=if [ \"$3\" != a ]; then exit 1; fi\n" - " truncate --size=" STR(SIZE) " $tmpdir/a", + " truncate --size=" STR (SIZE) " $tmpdir/a", "pread=dd bs=1 skip=$4 count=$3 if=$tmpdir/a || exit 1", "pwrite=dd bs=1 conv=notrunc seek=$4 of=$tmpdir/a || exit 1", "can_write=exit 0", diff --git a/tests/oldstyle.c b/tests/oldstyle.c index fd8bdc5fdd7c..5667e388d3d2 100644 --- a/tests/oldstyle.c +++ b/tests/oldstyle.c @@ -31,7 +31,7 @@ #define SIZE 65536 #define XSTR(s) #s -#define STR(s) XSTR(s) +#define STR(s) XSTR (s) static char wbuf[512] = { 1, 2, 3, 4 }, rbuf[512]; static const char *progname; @@ -82,7 +82,7 @@ main (int argc, char *argv[]) struct nbd_handle *nbd; int64_t r; char *args[] = { "nbdkit", "-s", "-o", "--exit-with-parent", "-v", - "memory", "size=" STR(SIZE), NULL }; + "memory", "size=" STR (SIZE), NULL }; int calls = 0; const char *s; diff --git a/examples/copy-libev.c b/examples/copy-libev.c index 418d99f1419d..02f9cad492b9 100644 --- a/examples/copy-libev.c +++ b/examples/copy-libev.c @@ -121,12 +121,12 @@ static ev_timer progress; static inline void start_request_soon (struct request *r); static void start_request_cb (struct ev_loop *loop, ev_timer *w, int revents); -static void start_request(struct request *r); -static void start_read(struct request *r); -static void start_write(struct request *r); -static void start_zero(struct request *r); -static int read_completed(void *user_data, int *error); -static int request_completed(void *user_data, int *error); +static void start_request (struct request *r); +static void start_read (struct request *r); +static void start_write (struct request *r); +static void start_zero (struct request *r); +static int read_completed (void *user_data, int *error); +static int request_completed (void *user_data, int *error); /* Return true iff data is all zero bytes. * @@ -158,13 +158,13 @@ request_state (struct request *r) } static inline int -get_fd(struct connection *c) +get_fd (struct connection *c) { return nbd_aio_get_fd (c->nbd); } static inline int -get_events(struct connection *c) +get_events (struct connection *c) { unsigned dir = nbd_aio_get_direction (c->nbd); @@ -388,7 +388,7 @@ start_request_cb (struct ev_loop *loop, ev_timer *w, int revents) /* Start async copy or zero request. */ static void -start_request(struct request *r) +start_request (struct request *r) { /* Cancel the request if we are done. */ if (offset == size) @@ -430,7 +430,7 @@ start_request(struct request *r) } static void -start_read(struct request *r) +start_read (struct request *r) { int64_t cookie; @@ -468,7 +468,7 @@ read_completed (void *user_data, int *error) } static void -start_write(struct request *r) +start_write (struct request *r) { int64_t cookie; @@ -487,7 +487,7 @@ start_write(struct request *r) } static void -start_zero(struct request *r) +start_zero (struct request *r) { int64_t cookie; @@ -535,7 +535,7 @@ request_completed (void *user_data, int *error) * iteration, to avoid deadlock if we need to start a zero or write. */ if (offset < size) - start_request_soon(r); + start_request_soon (r); return 1; } @@ -590,7 +590,7 @@ finish_progress () static inline void update_watcher (struct connection *c) { - int events = get_events(c); + int events = get_events (c); if (events != c->watcher.events) { ev_io_stop (loop, &c->watcher); @@ -671,7 +671,7 @@ main (int argc, char *argv[]) if (r->data == NULL) FAIL ("Cannot allocate buffer: %s", strerror (errno)); - start_request(r); + start_request (r); } /* Start watching events on src and dst handles. */ diff --git a/examples/list-exports.c b/examples/list-exports.c index f6200f480a63..c7780ffd5bb7 100644 --- a/examples/list-exports.c +++ b/examples/list-exports.c @@ -50,7 +50,7 @@ list_one (void *opaque, const char *name, printf ("[%d] %s\n", l->i, name); if (*description) - printf(" (%s)\n", description); + printf (" (%s)\n", description); names = realloc (l->names, (l->i + 1) * sizeof *names); if (!names) { diff --git a/examples/strict-structured-reads.c b/examples/strict-structured-reads.c index 5a61caa8681d..1c9664260654 100644 --- a/examples/strict-structured-reads.c +++ b/examples/strict-structured-reads.c @@ -225,7 +225,7 @@ main (int argc, char *argv[]) assert (d && r); offset = rand () % (exportsize - maxsize); - if (rand() & 1) + if (rand () & 1) flags = LIBNBD_CMD_FLAG_DF; *r = (struct range) { .first = offset, .last = offset + maxsize, }; *d = (struct data) { .offset = offset, .count = maxsize, .flags = flags, diff --git a/examples/threaded-reads-and-writes.c b/examples/threaded-reads-and-writes.c index fd3e677d7d9c..e01727c8d072 100644 --- a/examples/threaded-reads-and-writes.c +++ b/examples/threaded-reads-and-writes.c @@ -248,7 +248,7 @@ start_thread (void *arg) * Simulate a mix of large and small requests. */ while (i > 0 && in_flight < MAX_IN_FLIGHT) { - size = (rand() & 1) ? BUFFER_SIZE : 512; + size = (rand () & 1) ? BUFFER_SIZE : 512; offset = rand () % (exportsize - size); cmd = rand () & 1; if (cmd == 0) diff --git a/interop/interop.c b/interop/interop.c index bd5dc2e19656..371bfc47c672 100644 --- a/interop/interop.c +++ b/interop/interop.c @@ -88,7 +88,7 @@ main (int argc, char *argv[]) * GNUTLS_NO_SIGNAL flag, either because it predates GnuTLS 3.4.2 or * because the OS lacks MSG_NOSIGNAL support. */ -#if TLS && !defined(HAVE_GNUTLS_NO_SIGNAL) +#if TLS && !defined (HAVE_GNUTLS_NO_SIGNAL) signal (SIGPIPE, SIG_IGN); #endif diff --git a/copy/file-ops.c b/copy/file-ops.c index cc225de99d9b..18cae74a617d 100644 --- a/copy/file-ops.c +++ b/copy/file-ops.c @@ -90,7 +90,7 @@ struct rw_file { #ifdef PAGE_CACHE_MAPPING static long page_size; -static void page_size_init (void) __attribute__((constructor)); +static void page_size_init (void) __attribute__ ((constructor)); static void page_size_init (void) { diff --git a/copy/main.c b/copy/main.c index eb10c84dcc5d..ae46b76ef053 100644 --- a/copy/main.c +++ b/copy/main.c @@ -66,7 +66,7 @@ static bool is_nbd_uri (const char *s); static struct rw *open_local (const char *filename, direction d); static void print_rw (struct rw *rw, const char *prefix, FILE *fp); -static void __attribute__((noreturn)) +static void __attribute__ ((noreturn)) usage (FILE *fp, int exitcode) { fprintf (fp, diff --git a/copy/nbdcopy.h b/copy/nbdcopy.h index 9438cce4179b..b6257cc13951 100644 --- a/copy/nbdcopy.h +++ b/copy/nbdcopy.h @@ -116,7 +116,7 @@ struct extent { uint64_t length; bool zero; }; -DEFINE_VECTOR_TYPE(extent_list, struct extent); +DEFINE_VECTOR_TYPE (extent_list, struct extent); /* The operations struct hides some of the differences between local * file, NBD and pipes from the copying code. diff --git a/dump/dump.c b/dump/dump.c index bdbc90401fe2..d0da28790eb7 100644 --- a/dump/dump.c +++ b/dump/dump.c @@ -58,7 +58,7 @@ static void do_connect (void); static void do_dump (void); static void catch_signal (int); -static void __attribute__((noreturn)) +static void __attribute__ ((noreturn)) usage (FILE *fp, int exitcode) { fprintf (fp, diff --git a/fuse/nbdfuse.c b/fuse/nbdfuse.c index b9d54abd988a..b18cce83652b 100644 --- a/fuse/nbdfuse.c +++ b/fuse/nbdfuse.c @@ -66,7 +66,7 @@ enum mode { MODE_VSOCK, /* --vsock */ }; -static void __attribute__((noreturn)) +static void __attribute__ ((noreturn)) usage (FILE *fp, int exitcode) { fprintf (fp, diff --git a/info/main.c b/info/main.c index 5cd91fe1a3cf..0b5a5190ba26 100644 --- a/info/main.c +++ b/info/main.c @@ -51,7 +51,7 @@ bool totals = false; /* --totals option */ static enum { MODE_URI = 1, MODE_SQUARE_BRACKET } mode; static char **args; -static void __attribute__((noreturn)) +static void __attribute__ ((noreturn)) usage (FILE *fp, int exitcode) { fprintf (fp, diff --git a/ublk/nbdublk.c b/ublk/nbdublk.c index 00aa23ec03b5..89976fabcb17 100644 --- a/ublk/nbdublk.c +++ b/ublk/nbdublk.c @@ -66,7 +66,7 @@ enum mode { MODE_VSOCK, /* --vsock */ }; -static void __attribute__((noreturn)) +static void __attribute__ ((noreturn)) usage (FILE *fp, int exitcode) { fprintf (fp, @@ -421,7 +421,7 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - dinfo = ublksrv_ctrl_get_dev_info(dev); + dinfo = ublksrv_ctrl_get_dev_info (dev); /* Register signal handlers to try to stop the device. */ sa.sa_handler = signal_handler; diff --git a/ublk/tgt.c b/ublk/tgt.c index 3fb222333143..5d88e33dcc4b 100644 --- a/ublk/tgt.c +++ b/ublk/tgt.c @@ -62,7 +62,7 @@ struct thread_info { struct ublksrv_aio_ctx *aio_ctx; struct ublksrv_aio_list compl; }; -DEFINE_VECTOR_TYPE(thread_infos, struct thread_info) +DEFINE_VECTOR_TYPE (thread_infos, struct thread_info) static thread_infos thread_info; static pthread_barrier_t barrier; @@ -209,7 +209,7 @@ nbd_work_thread (void *vpinfo) ublksrv_aio_complete_worker (aio_ctx, &compl); - if (nbd_poll2 (h, ublksrv_aio_get_efd(aio_ctx), -1) == -1) { + if (nbd_poll2 (h, ublksrv_aio_get_efd (aio_ctx), -1) == -1) { fprintf (stderr, "%s\n", nbd_get_error ()); exit (EXIT_FAILURE); } @@ -223,13 +223,13 @@ io_uring_thread (void *vpinfo) { struct thread_info *thread_info = vpinfo; const struct ublksrv_dev *dev = thread_info->dev; - const struct ublksrv_ctrl_dev *cdev = ublksrv_get_ctrl_dev(dev); - const struct ublksrv_ctrl_dev_info *dinfo = ublksrv_ctrl_get_dev_info(cdev); + const struct ublksrv_ctrl_dev *cdev = ublksrv_get_ctrl_dev (dev); + const struct ublksrv_ctrl_dev_info *dinfo = ublksrv_ctrl_get_dev_info (cdev); const unsigned dev_id = dinfo->dev_id; const size_t q_id = thread_info->i; const struct ublksrv_queue *q; int r; - int tid = gettid(); + int tid = gettid (); pthread_mutex_lock (&jbuf_lock); ublksrv_json_write_queue_info (cdev, jbuf, sizeof jbuf, q_id, tid); @@ -268,7 +268,7 @@ static int set_parameters (struct ublksrv_ctrl_dev *ctrl_dev, const struct ublksrv_dev *dev) { - const struct ublksrv_ctrl_dev_info *dinfo = ublksrv_ctrl_get_dev_info(ctrl_dev); + const struct ublksrv_ctrl_dev_info *dinfo = ublksrv_ctrl_get_dev_info (ctrl_dev); const unsigned attrs = (readonly ? UBLK_ATTR_READ_ONLY : 0) | (rotational ? UBLK_ATTR_ROTATIONAL : 0) | @@ -308,7 +308,7 @@ set_parameters (struct ublksrv_ctrl_dev *ctrl_dev, int start_daemon (struct ublksrv_ctrl_dev *ctrl_dev) { - const struct ublksrv_ctrl_dev_info *dinfo = ublksrv_ctrl_get_dev_info(ctrl_dev); + const struct ublksrv_ctrl_dev_info *dinfo = ublksrv_ctrl_get_dev_info (ctrl_dev); const struct ublksrv_dev *dev; size_t i; int r; @@ -421,8 +421,8 @@ start_daemon (struct ublksrv_ctrl_dev *ctrl_dev) static int init_tgt (struct ublksrv_dev *dev, int type, int argc, char *argv[]) { - const struct ublksrv_ctrl_dev *cdev = ublksrv_get_ctrl_dev(dev); - const struct ublksrv_ctrl_dev_info *info = ublksrv_ctrl_get_dev_info(cdev); + const struct ublksrv_ctrl_dev *cdev = ublksrv_get_ctrl_dev (dev); + const struct ublksrv_ctrl_dev_info *info = ublksrv_ctrl_get_dev_info (cdev); struct ublksrv_tgt_info *tgt = &dev->tgt; struct ublksrv_tgt_base_json tgt_json = { .type = type, @@ -439,7 +439,7 @@ init_tgt (struct ublksrv_dev *dev, int type, int argc, char *argv[]) tgt->tgt_ring_depth = info->queue_depth; tgt->nr_fds = 0; - ublksrv_json_write_dev_info (ublksrv_get_ctrl_dev(dev), jbuf, sizeof jbuf); + ublksrv_json_write_dev_info (ublksrv_get_ctrl_dev (dev), jbuf, sizeof jbuf); ublksrv_json_write_target_base_info (jbuf, sizeof jbuf, &tgt_json); return 0;
On Wed, Feb 15, 2023 at 03:11:30PM +0100, Laszlo Ersek wrote:
We intend to place a space character between the function designator and the opening parenthesis in a function call. We've been executing on that mostly consistently; fix the few exceptions now.
The same convention should be applied to the invocations of function-like macros, and to instances of "__attribute__ ((attr))". (The latter is exemplified, although not consistently, by the GCC manual.) Implement this, by inserting the necessary spaces.
Furthermore, some compiler attributes take multiple parameters, such as "nonnull". The GCC manual clearly shows that arguments passed to such attributes may be separated with ", " as well, not just ",". Use the former, as it's more readable.
Finally, the C standard calls "defined" -- as in "#if defined identifier" and (equivalently) "#if defined (identifier)" -- a unary preprocessing operator. We can spell the parenthesized form as "defined (identifier)" rather than "defined(identifier)", so choose the former.
I collected the locations possibly missing spaces with:
git grep -EHn '\<[a-zA-Z0-9_]+\(' -- '*.c' '*.h'
and then manually updated each as necessary.
I didn't change occurrences in comments, except where the comment clearly indicated copying and pasting an expression into new code.
"git show -w" outputs nothing for this patch.
Which would not remain the case if we reflow a long line made longer (see [1] below).
The test suite passes.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
+++ b/lib/internal.h @@ -46,7 +46,7 @@ * debug and error handling code out of hot paths, making the hot path * into common functions use less instruction cache. */ -#if defined(__GNUC__) +#if defined (__GNUC__)
In my experience with GNU code (which this is not), the style I've seen there is to omit () whenever possible, as in: #if defined __GNUC__ or even #ifdef __GNUC__ ...
+++ b/common/include/checked-overflow.h @@ -46,7 +46,7 @@ #ifndef NBDKIT_CHECKED_OVERFLOW_H #define NBDKIT_CHECKED_OVERFLOW_H
-#if !defined(__GNUC__) && !defined(__clang__) +#if !defined (__GNUC__) && !defined (__clang__)
...obviously, the shorter #ifdef version can't be used here, but it could still be shortened to: #if !defined __GNUC__ && !defined __clang__
@@ -173,10 +173,10 @@ * * The expression "x" is not evaluated, unless it has variably modified type. */ -#define STATIC_ASSERT_UNSIGNED_INT(x) \ - do { \ - typedef char NBDKIT_UNIQUE_NAME(_x_has_uint_type)[(typeof (x))-1 > 0 ? 1 : -1] \ - __attribute__((__unused__)); \ +#define STATIC_ASSERT_UNSIGNED_INT(x) \ + do { \ + typedef char NBDKIT_UNIQUE_NAME (_x_has_uint_type)[(typeof (x))-1 > 0 ? 1 : -1] \ + __attribute__ ((__unused__)); \
[1] This change widened out beyond 80 columns. Is it worth splitting that typedef line in two, perhaps as: typedef char NBDKIT_UNIQUE_NAME (_x_has_uint_type) \ [(typeof (x))-1 > 0 ? 1 : -1] __attribute__ ((__unused__)); \ or does that make it look worse? At any rate, even if we do want to reflow the line to be shorter, you have to consider the commit message blurb about 'git show -w'.
+++ b/common/utils/string-vector.h @@ -37,6 +37,6 @@
#include "vector.h"
-DEFINE_VECTOR_TYPE(string_vector, char *); +DEFINE_VECTOR_TYPE (string_vector, char *);
We'll want to reflect changes to common files back to nbdkit; but it doesn't hold up the libnbd review.
+++ b/common/include/test-array-size.c @@ -41,21 +41,21 @@
struct st { const char *s; int i; };
-static const char *s0[] __attribute__((__unused__)) = { }; -static const char *s1[] __attribute__((__unused__)) = { "a" }; -static const char *s3[] __attribute__((__unused__)) = { "a", "b", "c" }; -static const char *s4[4] __attribute__((__unused__)) = { "a", "b", "c", "d" }; -static int i0[] __attribute__((__unused__)) = { }; -static int i1[] __attribute__((__unused__)) = { 1 }; -static int i3[] __attribute__((__unused__)) = { 1, 2, 3 }; -static int i4[4] __attribute__((__unused__)) = { 1, 2, 3, 4 }; -static struct st st0[] __attribute__((__unused__)) = { }; -static struct st st1[] __attribute__((__unused__)) = { { "a", 1 } }; -static struct st st3[] __attribute__((__unused__)) = +static const char *s0[] __attribute__ ((__unused__)) = { }; +static const char *s1[] __attribute__ ((__unused__)) = { "a" }; +static const char *s3[] __attribute__ ((__unused__)) = { "a", "b", "c" }; +static const char *s4[4] __attribute__ ((__unused__)) = { "a", "b", "c", "d" };
This one is still barely inside 80 columns, but did grab my eye as getting longer.
+++ b/ocaml/nbd-c.h @@ -32,7 +32,7 @@
/* Workaround for OCaml < 4.06.0 */ #ifndef Bytes_val -#define Bytes_val(x) String_val(x) +#define Bytes_val(x) String_val (x) #endif
/* Wrapper around caml_alloc_custom_mem for pre-2019 versions of OCaml. */ @@ -68,7 +68,7 @@ extern void nbd_internal_unix_sockaddr_to_sa (value, struct sockaddr_storage *, extern void nbd_internal_ocaml_exception_in_wrapper (const char *, value);
/* Extract an NBD handle from an OCaml heap value. */ -#define NBD_val(v) (*((struct nbd_handle **)Data_custom_val(v))) +#define NBD_val(v) (*((struct nbd_handle **)Data_custom_val (v)))
Hmm. Another potential cleanup patch (NOT for this one) would be settling on a preferred style for whether the cast prefix operator has a following space. For example, in copy/file-ops.c, we use both styles (git grep ' \*) \?[a-zA-Z]') when casting to a single pointer type (I didn't check double pointers or casts to a type name): copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *) rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r; I think I'm a fan of having the space after the cast operator, and as long as we're doing tree-wide cleanups, this would be a good time to inject such a patch if we wanted. Also, it might be cool to have a code formatting tool automatically check that patches conform to a given style (but that presupposes that there is a tool out there that gives us a style we like, or where the differences to our current style are minimal to instead go with one of its builtin styles - AND that such a tool can be present on at least one of the CI machines to avoid regressions). But that's a much bigger task, and I'm not up to doing it myself at the moment. Overall, I don't have any strong reasons to insist on shorter #ifdef spellings, and the rest of your changes, while mechanical, do make the codebase seem more consistent. Tweaking the long line is minor enough that it could be done later if at all. Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On Wed, Feb 15, 2023 at 01:48:39PM -0600, Eric Blake wrote:
In my experience with GNU code (which this is not), the style I've seen there is to omit () whenever possible, as in:
#if defined __GNUC__
or even
#ifdef __GNUC__
I didn't know this was possible. I checked it. GCC and clang with -std=c89 and -std=c99 allow this, so I don't object. Although we don't support (eg) MSVC do you know if this is really standard C? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
On Fri, Feb 17, 2023 at 11:49:58AM +0000, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 01:48:39PM -0600, Eric Blake wrote:
In my experience with GNU code (which this is not), the style I've seen there is to omit () whenever possible, as in:
#if defined __GNUC__
or even
#ifdef __GNUC__
I didn't know this was possible.
I checked it. GCC and clang with -std=c89 and -std=c99 allow this, so I don't object.
Although we don't support (eg) MSVC do you know if this is really standard C?
C17 section 6.10.1P1: "The expression that controls conditional inclusion shall be an integer constant expression except that: identifiers (including those lexically identical to keywords) are interpreted as described below;168) and it may contain unary operator expressions of the form defined identifier or defined ( identifier ) which evaluate to 1 if the identifier is currently defined as a macro name (that is, if it is predefined or if it has been the subject of a #define preprocessing directive without an intervening #undef directive with the same subject identifier), 0 if it is not." Presumably similar in the upcoming C23, although I didn't check it. It's standard. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 20:48, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:30PM +0100, Laszlo Ersek wrote:
We intend to place a space character between the function designator and the opening parenthesis in a function call. We've been executing on that mostly consistently; fix the few exceptions now.
The same convention should be applied to the invocations of function-like macros, and to instances of "__attribute__ ((attr))". (The latter is exemplified, although not consistently, by the GCC manual.) Implement this, by inserting the necessary spaces.
Furthermore, some compiler attributes take multiple parameters, such as "nonnull". The GCC manual clearly shows that arguments passed to such attributes may be separated with ", " as well, not just ",". Use the former, as it's more readable.
Finally, the C standard calls "defined" -- as in "#if defined identifier" and (equivalently) "#if defined (identifier)" -- a unary preprocessing operator. We can spell the parenthesized form as "defined (identifier)" rather than "defined(identifier)", so choose the former.
I collected the locations possibly missing spaces with:
git grep -EHn '\<[a-zA-Z0-9_]+\(' -- '*.c' '*.h'
and then manually updated each as necessary.
I didn't change occurrences in comments, except where the comment clearly indicated copying and pasting an expression into new code.
"git show -w" outputs nothing for this patch.
Which would not remain the case if we reflow a long line made longer (see [1] below).
The test suite passes.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
+++ b/lib/internal.h @@ -46,7 +46,7 @@ * debug and error handling code out of hot paths, making the hot path * into common functions use less instruction cache. */ -#if defined(__GNUC__) +#if defined (__GNUC__)
In my experience with GNU code (which this is not), the style I've seen there is to omit () whenever possible, as in:
#if defined __GNUC__
or even
#ifdef __GNUC__
...
+++ b/common/include/checked-overflow.h @@ -46,7 +46,7 @@ #ifndef NBDKIT_CHECKED_OVERFLOW_H #define NBDKIT_CHECKED_OVERFLOW_H
-#if !defined(__GNUC__) && !defined(__clang__) +#if !defined (__GNUC__) && !defined (__clang__)
...obviously, the shorter #ifdef version can't be used here, but it could still be shortened to:
#if !defined __GNUC__ && !defined __clang__
@@ -173,10 +173,10 @@ * * The expression "x" is not evaluated, unless it has variably modified type. */ -#define STATIC_ASSERT_UNSIGNED_INT(x) \ - do { \ - typedef char NBDKIT_UNIQUE_NAME(_x_has_uint_type)[(typeof (x))-1 > 0 ? 1 : -1] \ - __attribute__((__unused__)); \ +#define STATIC_ASSERT_UNSIGNED_INT(x) \ + do { \ + typedef char NBDKIT_UNIQUE_NAME (_x_has_uint_type)[(typeof (x))-1 > 0 ? 1 : -1] \ + __attribute__ ((__unused__)); \
[1] This change widened out beyond 80 columns. Is it worth splitting that typedef line in two, perhaps as:
typedef char NBDKIT_UNIQUE_NAME (_x_has_uint_type) \ [(typeof (x))-1 > 0 ? 1 : -1] __attribute__ ((__unused__)); \
or does that make it look worse? At any rate, even if we do want to reflow the line to be shorter, you have to consider the commit message blurb about 'git show -w'.
This patch concerns itself with one thing only: the space character between the function designator (or macro name) and the paren that opens the argument list. Everything else is out of scope for the patch. In the particular case, the original NBDKIT_UNIQUE_NAME line was already 84 characters long; in fact that original line, when introduced, broke the alignment of the backslashes at the right hand side. That state stems from nbdkit (not libnbd) commit cf2b6297646a ("common/include/unique-name.h: Rename UNIQUE_NAME macro", 2022-01-11). It was later ported to libnbd in commit 485f5ddf2d48 ("common/include/unique-name.h: Rename UNIQUE_NAME macro", 2022-02-23). So, this patch highlights another pre-existent task, and creates a new one: - the overlong lines from the above-noted commits should be cleaned up - the whitespace updates from the present patch should be reflected back to nbdkit. I was aware of both of those tasks, it's just that my cleanup stack simply couldn't accommodate further recursions. I was already cleaning up a thing for a cleanup that I needed for another cleanup. I couldn't nest them any deeper, I had to stop the scope creep somewhere. More generally, we should probably invent a way to avoid porting such utility code back and forth between libnbd and nbdkit. For example, libguestfs, guestfs-tools, and virt-v2v share the "common" submodule.
+++ b/common/utils/string-vector.h @@ -37,6 +37,6 @@
#include "vector.h"
-DEFINE_VECTOR_TYPE(string_vector, char *); +DEFINE_VECTOR_TYPE (string_vector, char *);
We'll want to reflect changes to common files back to nbdkit; but it doesn't hold up the libnbd review.
+++ b/common/include/test-array-size.c @@ -41,21 +41,21 @@
struct st { const char *s; int i; };
-static const char *s0[] __attribute__((__unused__)) = { }; -static const char *s1[] __attribute__((__unused__)) = { "a" }; -static const char *s3[] __attribute__((__unused__)) = { "a", "b", "c" }; -static const char *s4[4] __attribute__((__unused__)) = { "a", "b", "c", "d" }; -static int i0[] __attribute__((__unused__)) = { }; -static int i1[] __attribute__((__unused__)) = { 1 }; -static int i3[] __attribute__((__unused__)) = { 1, 2, 3 }; -static int i4[4] __attribute__((__unused__)) = { 1, 2, 3, 4 }; -static struct st st0[] __attribute__((__unused__)) = { }; -static struct st st1[] __attribute__((__unused__)) = { { "a", 1 } }; -static struct st st3[] __attribute__((__unused__)) = +static const char *s0[] __attribute__ ((__unused__)) = { }; +static const char *s1[] __attribute__ ((__unused__)) = { "a" }; +static const char *s3[] __attribute__ ((__unused__)) = { "a", "b", "c" }; +static const char *s4[4] __attribute__ ((__unused__)) = { "a", "b", "c", "d" };
This one is still barely inside 80 columns, but did grab my eye as getting longer.
+++ b/ocaml/nbd-c.h @@ -32,7 +32,7 @@
/* Workaround for OCaml < 4.06.0 */ #ifndef Bytes_val -#define Bytes_val(x) String_val(x) +#define Bytes_val(x) String_val (x) #endif
/* Wrapper around caml_alloc_custom_mem for pre-2019 versions of OCaml. */ @@ -68,7 +68,7 @@ extern void nbd_internal_unix_sockaddr_to_sa (value, struct sockaddr_storage *, extern void nbd_internal_ocaml_exception_in_wrapper (const char *, value);
/* Extract an NBD handle from an OCaml heap value. */ -#define NBD_val(v) (*((struct nbd_handle **)Data_custom_val(v))) +#define NBD_val(v) (*((struct nbd_handle **)Data_custom_val (v)))
Hmm. Another potential cleanup patch (NOT for this one) would be settling on a preferred style for whether the cast prefix operator has a following space. For example, in copy/file-ops.c, we use both styles (git grep ' \*) \?[a-zA-Z]') when casting to a single pointer type (I didn't check double pointers or casts to a type name):
copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *) rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r;
Yes, good observation.
I think I'm a fan of having the space after the cast operator, and as long as we're doing tree-wide cleanups, this would be a good time to inject such a patch if we wanted.
I actually dislike the space there; the reason being that the cast operator has one of the strongest bindings in C, and the space evokes the opposite impression. We've had actual bugs introduced in edk2 because someone was misled by this. (The cast prefix op is in the same group as "*", "&", "!", "~", and unary "-"; we don't use a space with those either. ... Well, I've seen a space character after "!" in some spots, and I happen to strongly disagree with that, for the exact same reason -- but that's another discussion. :))
Also, it might be cool to have a code formatting tool automatically check that patches conform to a given style (but that presupposes that there is a tool out there that gives us a style we like, or where the differences to our current style are minimal to instead go with one of its builtin styles - AND that such a tool can be present on at least one of the CI machines to avoid regressions). But that's a much bigger task, and I'm not up to doing it myself at the moment.
It's a monumental task. In edk2, just searching for the right had taken very long, and once they settled on uncrustify, several patches were required for upstream uncrustify, *plus* a humongous config file in edk2.
Overall, I don't have any strong reasons to insist on shorter #ifdef spellings, and the rest of your changes, while mechanical, do make the codebase seem more consistent. Tweaking the long line is minor enough that it could be done later if at all.
I certainly don't want to dismiss these observations, but I'll definitely forget about them unless we record them somewhere. Do these deserve a bugzilla (or multiple bugzillas)?
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! Laszlo
On Mon, Feb 20, 2023 at 02:38:25PM +0100, Laszlo Ersek wrote:
[1] This change widened out beyond 80 columns. Is it worth splitting that typedef line in two, perhaps as:
typedef char NBDKIT_UNIQUE_NAME (_x_has_uint_type) \ [(typeof (x))-1 > 0 ? 1 : -1] __attribute__ ((__unused__)); \
or does that make it look worse? At any rate, even if we do want to reflow the line to be shorter, you have to consider the commit message blurb about 'git show -w'.
This patch concerns itself with one thing only: the space character between the function designator (or macro name) and the paren that opens the argument list. Everything else is out of scope for the patch.
Fully agreed. Doing JUST spacing is the only thing appropriate for this patch.
In the particular case, the original NBDKIT_UNIQUE_NAME line was already 84 characters long; in fact that original line, when introduced, broke the alignment of the backslashes at the right hand side. That state stems from nbdkit (not libnbd) commit cf2b6297646a ("common/include/unique-name.h: Rename UNIQUE_NAME macro", 2022-01-11). It was later ported to libnbd in commit 485f5ddf2d48 ("common/include/unique-name.h: Rename UNIQUE_NAME macro", 2022-02-23).
So, this patch highlights another pre-existent task, and creates a new one:
- the overlong lines from the above-noted commits should be cleaned up
- the whitespace updates from the present patch should be reflected back to nbdkit.
I was aware of both of those tasks, it's just that my cleanup stack simply couldn't accommodate further recursions. I was already cleaning up a thing for a cleanup that I needed for another cleanup. I couldn't nest them any deeper, I had to stop the scope creep somewhere.
Fair enough. We do indeed seem good at adding to our TODO list without meaning to. We don't need to hold up this series waiting for other cleanup tasks to be done.
More generally, we should probably invent a way to avoid porting such utility code back and forth between libnbd and nbdkit. For example, libguestfs, guestfs-tools, and virt-v2v share the "common" submodule.
The idea makes sense to me, although it does add a bit of a pain (git submodules are not always the easiest to work with).
Hmm. Another potential cleanup patch (NOT for this one) would be settling on a preferred style for whether the cast prefix operator has a following space. For example, in copy/file-ops.c, we use both styles (git grep ' \*) \?[a-zA-Z]') when casting to a single pointer type (I didn't check double pointers or casts to a type name):
copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *) rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r;
Yes, good observation.
I think I'm a fan of having the space after the cast operator, and as long as we're doing tree-wide cleanups, this would be a good time to inject such a patch if we wanted.
I actually dislike the space there; the reason being that the cast operator has one of the strongest bindings in C, and the space evokes the opposite impression. We've had actual bugs introduced in edk2 because someone was misled by this.
(The cast prefix op is in the same group as "*", "&", "!", "~", and unary "-"; we don't use a space with those either.
... Well, I've seen a space character after "!" in some spots, and I happen to strongly disagree with that, for the exact same reason -- but that's another discussion. :))
You actually make a strong argument for not having the space after a cast. Thinking about it more, that also means that visually, you can distinguish between (foo) (bar, baz) (foo)(bar, baz) where the former is a function call through a function pointer foo with two arguments, while the latter is an (unusual) cast of the result of the comma operator to type foo. Not that I'd ever expect to encounter code where this visual distinction makes the code easier to read (and proof that parsing C is not trivial, because how to parse depends on the compiler knowing a priori whether the name on the left is a type name or a function pointer name).
Also, it might be cool to have a code formatting tool automatically check that patches conform to a given style (but that presupposes that there is a tool out there that gives us a style we like, or where the differences to our current style are minimal to instead go with one of its builtin styles - AND that such a tool can be present on at least one of the CI machines to avoid regressions). But that's a much bigger task, and I'm not up to doing it myself at the moment.
It's a monumental task. In edk2, just searching for the right had taken very long, and once they settled on uncrustify, several patches were required for upstream uncrustify, *plus* a humongous config file in edk2.
edk2 has the drawback of a widely disparate group of contributors each with their own preferred style and with a large existing corpus of code. At least with libnbd and nbdkit, we have a small enough group of regular contributors and a smaller code base, where adopting an existing style would require a one-time painful conversion (which in turn would be awkward across git blame), but where getting consensus on a style that could be automatically be enforced need not carry baggage of a large config file. But yeah, definitely not a priority for today.
Overall, I don't have any strong reasons to insist on shorter #ifdef spellings, and the rest of your changes, while mechanical, do make the codebase seem more consistent. Tweaking the long line is minor enough that it could be done later if at all.
I certainly don't want to dismiss these observations, but I'll definitely forget about them unless we record them somewhere. Do these deserve a bugzilla (or multiple bugzillas)?
Bugzillas will help us remember the ideas. The question is whether they are worth sinking enough time into, or whether they just add to a backlog that never rises to top priority.
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! Laszlo
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/20/23 18:41, Eric Blake wrote:
On Mon, Feb 20, 2023 at 02:38:25PM +0100, Laszlo Ersek wrote:
[1] This change widened out beyond 80 columns. Is it worth splitting that typedef line in two, perhaps as:
typedef char NBDKIT_UNIQUE_NAME (_x_has_uint_type) \ [(typeof (x))-1 > 0 ? 1 : -1] __attribute__ ((__unused__)); \
or does that make it look worse? At any rate, even if we do want to reflow the line to be shorter, you have to consider the commit message blurb about 'git show -w'.
This patch concerns itself with one thing only: the space character between the function designator (or macro name) and the paren that opens the argument list. Everything else is out of scope for the patch.
Fully agreed. Doing JUST spacing is the only thing appropriate for this patch.
In the particular case, the original NBDKIT_UNIQUE_NAME line was already 84 characters long; in fact that original line, when introduced, broke the alignment of the backslashes at the right hand side. That state stems from nbdkit (not libnbd) commit cf2b6297646a ("common/include/unique-name.h: Rename UNIQUE_NAME macro", 2022-01-11). It was later ported to libnbd in commit 485f5ddf2d48 ("common/include/unique-name.h: Rename UNIQUE_NAME macro", 2022-02-23).
So, this patch highlights another pre-existent task, and creates a new one:
- the overlong lines from the above-noted commits should be cleaned up
- the whitespace updates from the present patch should be reflected back to nbdkit.
I was aware of both of those tasks, it's just that my cleanup stack simply couldn't accommodate further recursions. I was already cleaning up a thing for a cleanup that I needed for another cleanup. I couldn't nest them any deeper, I had to stop the scope creep somewhere.
Fair enough. We do indeed seem good at adding to our TODO list without meaning to. We don't need to hold up this series waiting for other cleanup tasks to be done.
I didn't notice we had a TODO file! :)
More generally, we should probably invent a way to avoid porting such utility code back and forth between libnbd and nbdkit. For example, libguestfs, guestfs-tools, and virt-v2v share the "common" submodule.
The idea makes sense to me, although it does add a bit of a pain (git submodules are not always the easiest to work with).
Hmm. Another potential cleanup patch (NOT for this one) would be settling on a preferred style for whether the cast prefix operator has a following space. For example, in copy/file-ops.c, we use both styles (git grep ' \*) \?[a-zA-Z]') when casting to a single pointer type (I didn't check double pointers or casts to a type name):
copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *) rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r; copy/file-ops.c: struct rw_file *rwf = (struct rw_file *)rw; copy/file-ops.c: data = (char *) data + r;
Yes, good observation.
I think I'm a fan of having the space after the cast operator, and as long as we're doing tree-wide cleanups, this would be a good time to inject such a patch if we wanted.
I actually dislike the space there; the reason being that the cast operator has one of the strongest bindings in C, and the space evokes the opposite impression. We've had actual bugs introduced in edk2 because someone was misled by this.
(The cast prefix op is in the same group as "*", "&", "!", "~", and unary "-"; we don't use a space with those either.
... Well, I've seen a space character after "!" in some spots, and I happen to strongly disagree with that, for the exact same reason -- but that's another discussion. :))
You actually make a strong argument for not having the space after a cast. Thinking about it more, that also means that visually, you can distinguish between (foo) (bar, baz) (foo)(bar, baz)
where the former is a function call through a function pointer foo with two arguments, while the latter is an (unusual) cast of the result of the comma operator to type foo. Not that I'd ever expect to encounter code where this visual distinction makes the code easier to read (and proof that parsing C is not trivial, because how to parse depends on the compiler knowing a priori whether the name on the left is a type name or a function pointer name).
Also, it might be cool to have a code formatting tool automatically check that patches conform to a given style (but that presupposes that there is a tool out there that gives us a style we like, or where the differences to our current style are minimal to instead go with one of its builtin styles - AND that such a tool can be present on at least one of the CI machines to avoid regressions). But that's a much bigger task, and I'm not up to doing it myself at the moment.
It's a monumental task. In edk2, just searching for the right had taken very long, and once they settled on uncrustify, several patches were required for upstream uncrustify, *plus* a humongous config file in edk2.
edk2 has the drawback of a widely disparate group of contributors each with their own preferred style and with a large existing corpus of code. At least with libnbd and nbdkit, we have a small enough group of regular contributors and a smaller code base, where adopting an existing style would require a one-time painful conversion (which in turn would be awkward across git blame), but where getting consensus on a style that could be automatically be enforced need not carry baggage of a large config file. But yeah, definitely not a priority for today.
Overall, I don't have any strong reasons to insist on shorter #ifdef spellings, and the rest of your changes, while mechanical, do make the codebase seem more consistent. Tweaking the long line is minor enough that it could be done later if at all.
I certainly don't want to dismiss these observations, but I'll definitely forget about them unless we record them somewhere. Do these deserve a bugzilla (or multiple bugzillas)?
Bugzillas will help us remember the ideas. The question is whether they are worth sinking enough time into, or whether they just add to a backlog that never rises to top priority.
OK, when I sit down to go through the complete set of review comments for this series, I'll capture the "for later" items in a bugzilla ticket (or several tickets). I don't mind working on these items, and I've had luck with bugzilla in general, for tracking stuff. Thanks! Laszlo
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! Laszlo
On Mon, Feb 20, 2023 at 02:38:25PM +0100, Laszlo Ersek wrote:
I certainly don't want to dismiss these observations, but I'll definitely forget about them unless we record them somewhere. Do these deserve a bugzilla (or multiple bugzillas)?
You could drop a note in the TODO file. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
On 2/15/23 15:11, Laszlo Ersek wrote:
We intend to place a space character between the function designator and the opening parenthesis in a function call. We've been executing on that mostly consistently; fix the few exceptions now.
The same convention should be applied to the invocations of function-like macros, and to instances of "__attribute__ ((attr))". (The latter is exemplified, although not consistently, by the GCC manual.) Implement this, by inserting the necessary spaces.
Furthermore, some compiler attributes take multiple parameters, such as "nonnull". The GCC manual clearly shows that arguments passed to such attributes may be separated with ", " as well, not just ",". Use the former, as it's more readable.
Finally, the C standard calls "defined" -- as in "#if defined identifier" and (equivalently) "#if defined (identifier)" -- a unary preprocessing operator. We can spell the parenthesized form as "defined (identifier)" rather than "defined(identifier)", so choose the former.
I collected the locations possibly missing spaces with:
git grep -EHn '\<[a-zA-Z0-9_]+\(' -- '*.c' '*.h'
and then manually updated each as necessary.
I didn't change occurrences in comments, except where the comment clearly indicated copying and pasting an expression into new code.
"git show -w" outputs nothing for this patch.
The test suite passes.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 72 ++++++++++---------- lib/nbd-protocol.h | 12 ++-- common/include/array-size.h | 2 +- common/include/byte-swapping.h | 24 +++---- common/include/checked-overflow.h | 42 ++++++------ common/include/compiler-macros.h | 2 +- common/include/iszero.h | 2 +- common/include/minmax.h | 4 +- common/utils/const-string-vector.h | 2 +- common/utils/nbdkit-string.h | 2 +- common/utils/string-vector.h | 2 +- common/utils/vector.h | 20 +++--- lib/errors.c | 6 +- lib/opt.c | 4 +- lib/uri.c | 2 +- lib/utils.c | 12 ++-- common/include/test-array-size.c | 26 +++---- common/utils/test-human-size.c | 10 +-- common/utils/test-vector.c | 4 +- ocaml/nbd-c.h | 6 +- tests/eflags.c | 6 +- tests/get-size.c | 4 +- tests/newstyle-limited.c | 6 +- tests/oldstyle.c | 4 +- examples/copy-libev.c | 30 ++++---- examples/list-exports.c | 2 +- examples/strict-structured-reads.c | 2 +- examples/threaded-reads-and-writes.c | 2 +- interop/interop.c | 2 +- copy/file-ops.c | 2 +- copy/main.c | 2 +- copy/nbdcopy.h | 2 +- dump/dump.c | 2 +- fuse/nbdfuse.c | 2 +- info/main.c | 2 +- ublk/nbdublk.c | 4 +- ublk/tgt.c | 20 +++--- 37 files changed, 175 insertions(+), 175 deletions(-)
Merged this patch as commit b5101fbc59cb ("use space consistently in function and function-like macro invocations", 2023-02-22), with Eric's R-b from this thread, and with Rich's R-b from under patch#04. A longer-term subset of the new or newly highlighted tasks I've recorded in another commit: 46d60f807eca ("TODO: coding style and nbdkit code sharing ideas", 2023-02-22). The more direct tasks I've captured in <https://bugzilla.redhat.com/show_bug.cgi?id=2172516>. Laszlo
Apply the ideas in the previous patch to the C-language bindings generator. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/C.ml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/generator/C.ml b/generator/C.ml index f9171996dde0..07c924c48ccf 100644 --- a/generator/C.ml +++ b/generator/C.ml @@ -250,15 +250,15 @@ let *) (match ret with | RString -> - pr "\n LIBNBD_ATTRIBUTE_ALLOC_DEALLOC(__builtin_free)"; + pr "\n LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (__builtin_free)"; | _ -> () ); (* Output __attribute__((nonnull)) for the function parameters: * eg. struct nbd_handle *, int, char * * => [ true, false, true ] - * => LIBNBD_ATTRIBUTE_NONNULL(1,3) - * => __attribute__((nonnull(1,3))) + * => LIBNBD_ATTRIBUTE_NONNULL (1, 3) + * => __attribute__ ((nonnull (1, 3))) *) let nns : bool list = [ true ] (* struct nbd_handle * *) @@ -267,7 +267,7 @@ let let nns = List.mapi (fun i b -> (i+1, b)) nns in let nns = filter_map (fun (i, b) -> if b then Some i else None) nns in let nns : string list = List.map string_of_int nns in - pr "\n LIBNBD_ATTRIBUTE_NONNULL(%s);\n" (String.concat "," nns) + pr "\n LIBNBD_ATTRIBUTE_NONNULL (%s);\n" (String.concat ", " nns) let rec print_cbarg_list ?(wrap = false) ?maxcol ?types ?(parens = true) cbargs = @@ -400,22 +400,22 @@ let pr "extern \"C\" {\n"; pr "#endif\n"; pr "\n"; - pr "#if defined(__GNUC__)\n"; + pr "#if defined (__GNUC__)\n"; pr "#define LIBNBD_GCC_VERSION \\\n"; pr " (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)\n"; pr "#endif\n"; pr "\n"; pr "#ifndef LIBNBD_ATTRIBUTE_NONNULL\n"; - pr "#if defined(__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; - pr "#define LIBNBD_ATTRIBUTE_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))\n"; + pr "#if defined (__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; + pr "#define LIBNBD_ATTRIBUTE_NONNULL(...) __attribute__ ((__nonnull__ (__VA_ARGS__)))\n"; pr "#else\n"; pr "#define LIBNBD_ATTRIBUTE_NONNULL(...)\n"; pr "#endif\n"; pr "#endif /* ! defined LIBNBD_ATTRIBUTE_NONNULL */\n"; pr "\n"; - pr "#if defined(__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; + pr "#if defined (__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; pr "#define LIBNBD_ATTRIBUTE_ALLOC_DEALLOC(fn) \\\n"; - pr " __attribute__((__malloc__, __malloc__(fn, 1)))\n"; + pr " __attribute__ ((__malloc__, __malloc__ (fn, 1)))\n"; pr "#else\n"; pr "#define LIBNBD_ATTRIBUTE_ALLOC_DEALLOC(fn)\n"; pr "#endif\n"; @@ -454,7 +454,7 @@ let pr "#define LIBNBD_HAVE_NBD_CLOSE 1\n"; pr "\n"; pr "extern struct nbd_handle *nbd_create (void)\n"; - pr " LIBNBD_ATTRIBUTE_ALLOC_DEALLOC(nbd_close);\n"; + pr " LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (nbd_close);\n"; pr "#define LIBNBD_HAVE_NBD_CREATE 1\n"; pr "\n"; pr "extern const char *nbd_get_error (void);\n";
On Wed, Feb 15, 2023 at 03:11:31PM +0100, Laszlo Ersek wrote:
Apply the ideas in the previous patch to the C-language bindings generator.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/C.ml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
pr "#ifndef LIBNBD_ATTRIBUTE_NONNULL\n"; - pr "#if defined(__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; - pr "#define LIBNBD_ATTRIBUTE_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))\n"; + pr "#if defined (__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; + pr "#define LIBNBD_ATTRIBUTE_NONNULL(...) __attribute__ ((__nonnull__ (__VA_ARGS__)))\n";
Not only are these lines long in our source, they are long in the generated libnbd.h. I would not be opposed to a followup patch that tries harder at keeping the generated file under 80 columns (but there are already places where that is harder than this snippet - such as the generated declaration of nbd_aio_opt_list_meta_context), but it does not need to happen in this patch. And while I mentioned in 01/29 about the possibility of a C code formatter as a CI step for *.[ch], it would be even harder to insist that generated code matches a given style (it's always a nice goal for generated files to be human-readable where possible, but I'd much rather sacrifice that if it gets in the way of actually implementing the generator concisely). Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 20:54, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:31PM +0100, Laszlo Ersek wrote:
Apply the ideas in the previous patch to the C-language bindings generator.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/C.ml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
pr "#ifndef LIBNBD_ATTRIBUTE_NONNULL\n"; - pr "#if defined(__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; - pr "#define LIBNBD_ATTRIBUTE_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))\n"; + pr "#if defined (__GNUC__) && LIBNBD_GCC_VERSION >= 120000 /* gcc >= 12.0 */\n"; + pr "#define LIBNBD_ATTRIBUTE_NONNULL(...) __attribute__ ((__nonnull__ (__VA_ARGS__)))\n";
Not only are these lines long in our source, they are long in the generated libnbd.h. I would not be opposed to a followup patch that tries harder at keeping the generated file under 80 columns (but there are already places where that is harder than this snippet - such as the generated declaration of nbd_aio_opt_list_meta_context), but it does not need to happen in this patch.
Right, thanks -- if we agree we should have a new BZ for these subsequent tasks, I'll go over the comments again and capture them in the BZ. Laszlo
And while I mentioned in 01/29 about the possibility of a C code formatter as a CI step for *.[ch], it would be even harder to insist that generated code matches a given style (it's always a nice goal for generated files to be human-readable where possible, but I'd much rather sacrifice that if it gets in the way of actually implementing the generator concisely).
Reviewed-by: Eric Blake <eblake@redhat.com>
On 2/15/23 15:11, Laszlo Ersek wrote:
Apply the ideas in the previous patch to the C-language bindings generator.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/C.ml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
Merged this patch as commit b490830a9a78 ("generator/C.ml: use space consistently in func. and func.-like macro calls", 2023-02-22). Updated the "previous patch" term in the commit message to an actual commit reference. Picked up Eric's R-b from this thread, and Rich's from the patch#4 subthread. Recorded another task (recommended by Eric) at <https://bugzilla.redhat.com/show_bug.cgi?id=2172516#c1>. Laszlo
<signal.h> in POSIX reserves the "sa_" prefix: https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta... Let's use "sact_" instead. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 4 ++-- generator/states-connect-socket-activation.c | 16 ++++++++-------- lib/handle.c | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/internal.h b/lib/internal.h index c5827f4f0037..0b5f793011b8 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -278,8 +278,8 @@ struct nbd_handle { /* When using systemd socket activation, this directory and socket * must be deleted, and the pid above must be killed. */ - char *sa_tmpdir; - char *sa_sockpath; + char *sact_tmpdir; + char *sact_sockpath; /* When connecting to TCP ports, these fields are used. */ char *hostname, *port; diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 9a83834915db..7138e7638e30 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -111,22 +111,22 @@ CONNECT_SA.START: * short enough to store in the sockaddr_un. On some platforms this * may cause problems so we may need to revisit it. XXX */ - h->sa_tmpdir = strdup ("/tmp/libnbdXXXXXX"); - if (h->sa_tmpdir == NULL) { + h->sact_tmpdir = strdup ("/tmp/libnbdXXXXXX"); + if (h->sact_tmpdir == NULL) { SET_NEXT_STATE (%.DEAD); set_error (errno, "strdup"); return 0; } - if (mkdtemp (h->sa_tmpdir) == NULL) { + if (mkdtemp (h->sact_tmpdir) == NULL) { SET_NEXT_STATE (%.DEAD); set_error (errno, "mkdtemp"); /* Avoid cleanup in nbd_close. */ - free (h->sa_tmpdir); - h->sa_tmpdir = NULL; + free (h->sact_tmpdir); + h->sact_tmpdir = NULL; return 0; } - if (asprintf (&h->sa_sockpath, "%s/sock", h->sa_tmpdir) == -1) { + if (asprintf (&h->sact_sockpath, "%s/sock", h->sact_tmpdir) == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "strdup"); return 0; @@ -140,10 +140,10 @@ CONNECT_SA.START: } addr.sun_family = AF_UNIX; - memcpy (addr.sun_path, h->sa_sockpath, strlen (h->sa_sockpath) + 1); + memcpy (addr.sun_path, h->sact_sockpath, strlen (h->sact_sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { SET_NEXT_STATE (%.DEAD); - set_error (errno, "bind: %s", h->sa_sockpath); + set_error (errno, "bind: %s", h->sact_sockpath); close (s); return 0; } diff --git a/lib/handle.c b/lib/handle.c index 4a186f8fa946..dfd8c2e5cdb9 100644 --- a/lib/handle.c +++ b/lib/handle.c @@ -141,15 +141,15 @@ nbd_close (struct nbd_handle *h) free_cmd_list (h->cmds_done); string_vector_iter (&h->argv, (void *) free); free (h->argv.ptr); - if (h->sa_sockpath) { + if (h->sact_sockpath) { if (h->pid > 0) kill (h->pid, SIGTERM); - unlink (h->sa_sockpath); - free (h->sa_sockpath); + unlink (h->sact_sockpath); + free (h->sact_sockpath); } - if (h->sa_tmpdir) { - rmdir (h->sa_tmpdir); - free (h->sa_tmpdir); + if (h->sact_tmpdir) { + rmdir (h->sact_tmpdir); + free (h->sact_tmpdir); } free (h->hostname); free (h->port);
On Wed, Feb 15, 2023 at 03:11:32PM +0100, Laszlo Ersek wrote:
<signal.h> in POSIX reserves the "sa_" prefix:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta...
Let's use "sact_" instead.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 4 ++-- generator/states-connect-socket-activation.c | 16 ++++++++-------- lib/handle.c | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com> Stepping on reserved names is something that you can often get away with (if you don't conflict now, how likely is it that libc will actually introduce a future symbol that will conflict later?), so this is change is more on the theoretical side. But I have no objections, and as you proved, it's a very easy mechanical fix once we even bother to identify that reserved name clashes could be a potential porting issue. I've probably spent more time writing this email than you did in creating the patch proper (if you exclude the subsequent time testing that it builds and passes 'make check-valgrind'...) Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 21:00, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:32PM +0100, Laszlo Ersek wrote:
<signal.h> in POSIX reserves the "sa_" prefix:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta...
Let's use "sact_" instead.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 4 ++-- generator/states-connect-socket-activation.c | 16 ++++++++-------- lib/handle.c | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com>
Stepping on reserved names is something that you can often get away with (if you don't conflict now, how likely is it that libc will actually introduce a future symbol that will conflict later?), so this is change is more on the theoretical side. But I have no objections, and as you proved, it's a very easy mechanical fix once we even bother to identify that reserved name clashes could be a potential porting issue. I've probably spent more time writing this email than you did in creating the patch proper (if you exclude the subsequent time testing that it builds and passes 'make check-valgrind'...)
I've actually reviewed (eyeballed) this patch myself *multiple times*, very carefully. Mechanical changes can be deceptive :) Implementing them seems easy, but their impact can be surprising. In fact, whenever I need to review such a patch for someone else, I tend to redo it myself from zero, then compare the patches (or the resultant trees).
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! Laszlo
On 2/15/23 15:11, Laszlo Ersek wrote:
<signal.h> in POSIX reserves the "sa_" prefix:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta...
Let's use "sact_" instead.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 4 ++-- generator/states-connect-socket-activation.c | 16 ++++++++-------- lib/handle.c | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-)
Commit 86c8056dcab3 ("socket activation: rename sa_(tmpdir|sockpath) to sact_(tmpdir|sockpath)", 2023-02-22).
<signal.h> in POSIX reserves the "sa_" prefix: https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta... Let's use "saddr_" instead. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- ocaml/helpers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ocaml/helpers.c b/ocaml/helpers.c index 6568755bc664..42bcf997a237 100644 --- a/ocaml/helpers.c +++ b/ocaml/helpers.c @@ -140,19 +140,19 @@ nbd_internal_unix_sockaddr_to_sa (value sockaddrv, socklen_t *len) { CAMLparam1 (sockaddrv); - union sock_addr_union sa_u; + union sock_addr_union saddr_u; socklen_param_type sl; /* this is really an int or socklen_t */ memset (ss, 0, sizeof *ss); #ifdef HAVE_CAML_UNIX_GET_SOCKADDR - caml_unix_get_sockaddr (sockaddrv, &sa_u, &sl); + caml_unix_get_sockaddr (sockaddrv, &saddr_u, &sl); #else /* OCaml <= 4.14 exports this unnamespaced symbol. */ - get_sockaddr (sockaddrv, &sa_u, &sl); + get_sockaddr (sockaddrv, &saddr_u, &sl); #endif assert (sl <= sizeof *ss); - memcpy (ss, &sa_u, sl); + memcpy (ss, &saddr_u, sl); *len = sl; CAMLreturn0;
For #1, #2, #3, #4: Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
On Wed, Feb 15, 2023 at 03:11:33PM +0100, Laszlo Ersek wrote:
<signal.h> in POSIX reserves the "sa_" prefix:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta...
Let's use "saddr_" instead.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- ocaml/helpers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 15:11, Laszlo Ersek wrote:
<signal.h> in POSIX reserves the "sa_" prefix:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta...
Let's use "saddr_" instead.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- ocaml/helpers.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
Commit 0823a2da7527 ("ocaml: rename "sa_u" to "saddr_u"", 2023-02-22).
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free": string_vector_iter (..., (void *) free); Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.) Furthermore, in all 11 cases, the freeing of the vector's strings is immediately followed by the release of the vector's array-of-pointers too. (This additional step is not expressed consistently across libnbd: it's sometimes spelled as free(vec.ptr), sometimes as string_vector_reset(&vec).) Remove the generic "name##_iter" function definition, and introduce "name##_empty", which performs both steps at the same time. Convert the call sites. (Note that the converted code performs more cleanup steps in some cases than strictly necessary, but the extra work is harmless, and arguably beneficial for clarity / consistency.) Expose the "name##_empty" function definition with a new, separate macro: DEFINE_VECTOR_EMPTY(). The existent DEFINE_VECTOR_TYPE() macro permits such element types that are not pointers, or are pointers to const- and/or volatile-qualified objects. Whereas "name##_empty" requires that the elements be pointers to dynamically allocated, non-const, non-volatile objects. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- common/utils/string-vector.h | 1 + common/utils/vector.h | 27 +++++++++++++------- generator/states-connect-socket-activation.c | 9 +++---- lib/handle.c | 12 +++------ lib/utils.c | 6 ++--- common/utils/test-vector.c | 3 +-- info/show.c | 3 +-- 7 files changed, 30 insertions(+), 31 deletions(-) diff --git a/common/utils/string-vector.h b/common/utils/string-vector.h index aa33fd48ceb5..78d0b4f9bf10 100644 --- a/common/utils/string-vector.h +++ b/common/utils/string-vector.h @@ -38,5 +38,6 @@ #include "vector.h" DEFINE_VECTOR_TYPE (string_vector, char *); +DEFINE_VECTOR_EMPTY (string_vector); #endif /* STRING_VECTOR_H */ diff --git a/common/utils/vector.h b/common/utils/vector.h index fb2482c853ff..14bf5b0ddbc0 100644 --- a/common/utils/vector.h +++ b/common/utils/vector.h @@ -150,15 +150,6 @@ v->len = v->cap = 0; \ } \ \ - /* Iterate over the vector, calling f() on each element. */ \ - static inline void __attribute__ ((__unused__)) \ - name##_iter (name *v, void (*f) (type elem)) \ - { \ - size_t i; \ - for (i = 0; i < v->len; ++i) \ - f (v->ptr[i]); \ - } \ - \ /* Sort the elements of the vector. */ \ static inline void __attribute__ ((__unused__)) \ name##_sort (name *v, \ @@ -180,6 +171,24 @@ #define empty_vector { .ptr = NULL, .len = 0, .cap = 0 } +/* This macro should only be used if: + * - the vector contains pointers, and + * - the pointed-to objects are: + * - neither const- nor volatile-qualified, and + * - allocated with malloc() or equivalent. + */ +#define DEFINE_VECTOR_EMPTY(name) \ + /* Call free() on each element of the vector, then reset the vector. \ + */ \ + static inline void __attribute__ ((__unused__)) \ + name##_empty (name *v) \ + { \ + size_t i; \ + for (i = 0; i < v->len; ++i) \ + free (v->ptr[i]); \ + name##_reset (v); \ + } + struct generic_vector { void *ptr; size_t len; diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 7138e7638e30..3b621b8be44f 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -91,8 +91,7 @@ prepare_socket_activation_environment (string_vector *env) err: set_error (errno, "malloc"); - string_vector_iter (env, (void *) free); - free (env->ptr); + string_vector_empty (env); return -1; } @@ -166,8 +165,7 @@ CONNECT_SA.START: SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (s); - string_vector_iter (&env, (void *) free); - free (env.ptr); + string_vector_empty (&env); return 0; } if (pid == 0) { /* child - run command */ @@ -210,8 +208,7 @@ CONNECT_SA.START: /* Parent. */ close (s); - string_vector_iter (&env, (void *) free); - free (env.ptr); + string_vector_empty (&env); h->pid = pid; h->connaddrlen = sizeof addr; diff --git a/lib/handle.c b/lib/handle.c index dfd8c2e5cdb9..fb92f16e6c91 100644 --- a/lib/handle.c +++ b/lib/handle.c @@ -128,8 +128,7 @@ nbd_close (struct nbd_handle *h) /* Free user callbacks first. */ nbd_unlocked_clear_debug_callback (h); - string_vector_iter (&h->querylist, (void *) free); - free (h->querylist.ptr); + string_vector_empty (&h->querylist); free (h->bs_entries); nbd_internal_reset_size_and_flags (h); for (i = 0; i < h->meta_contexts.len; ++i) @@ -139,8 +138,7 @@ nbd_close (struct nbd_handle *h) free_cmd_list (h->cmds_to_issue); free_cmd_list (h->cmds_in_flight); free_cmd_list (h->cmds_done); - string_vector_iter (&h->argv, (void *) free); - free (h->argv.ptr); + string_vector_empty (&h->argv); if (h->sact_sockpath) { if (h->pid > 0) kill (h->pid, SIGTERM); @@ -164,8 +162,7 @@ nbd_close (struct nbd_handle *h) free (h->tls_certificates); free (h->tls_username); free (h->tls_psk_file); - string_vector_iter (&h->request_meta_contexts, (void *) free); - free (h->request_meta_contexts.ptr); + string_vector_empty (&h->request_meta_contexts); free (h->hname); pthread_mutex_destroy (&h->lock); free (h); @@ -379,8 +376,7 @@ nbd_unlocked_get_meta_context (struct nbd_handle *h, size_t i) int nbd_unlocked_clear_meta_contexts (struct nbd_handle *h) { - string_vector_iter (&h->request_meta_contexts, (void *) free); - string_vector_reset (&h->request_meta_contexts); + string_vector_empty (&h->request_meta_contexts); return 0; } diff --git a/lib/utils.c b/lib/utils.c index c229ebfc6106..bba4b3846e77 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -93,8 +93,7 @@ nbd_internal_set_argv (struct nbd_handle *h, char **argv) return -1; } - string_vector_iter (&h->argv, (void *) free); - string_vector_reset (&h->argv); + string_vector_empty (&h->argv); if (nbd_internal_copy_string_list (&h->argv, argv) == -1) { set_error (errno, "realloc"); @@ -110,8 +109,7 @@ nbd_internal_set_argv (struct nbd_handle *h, char **argv) int nbd_internal_set_querylist (struct nbd_handle *h, char **queries) { - string_vector_iter (&h->querylist, (void *) free); - string_vector_reset (&h->querylist); + string_vector_empty (&h->querylist); if (queries) { if (nbd_internal_copy_string_list (&h->querylist, queries) == -1) { diff --git a/common/utils/test-vector.c b/common/utils/test-vector.c index 55c8ebeb8a1e..05ac5cec3dba 100644 --- a/common/utils/test-vector.c +++ b/common/utils/test-vector.c @@ -151,8 +151,7 @@ test_string_vector (void) printf ("%s\n", v.ptr[i]); assert (i == 10); - string_vector_iter (&v, (void*)free); - string_vector_reset (&v); + string_vector_empty (&v); } static void diff --git a/info/show.c b/info/show.c index 4bf596715cf9..e6c9b9bf1243 100644 --- a/info/show.c +++ b/info/show.c @@ -275,8 +275,7 @@ show_one_export (struct nbd_handle *nbd, const char *desc, fprintf (fp, "\t},\n"); } - string_vector_iter (&contexts, (void *) free); - free (contexts.ptr); + string_vector_empty (&contexts); free (content); free (export_name); free (export_desc);
On Wed, Feb 15, 2023 at 03:11:34PM +0100, Laszlo Ersek wrote:
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free":
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Furthermore, in all 11 cases, the freeing of the vector's strings is immediately followed by the release of the vector's array-of-pointers too. (This additional step is not expressed consistently across libnbd: it's sometimes spelled as free(vec.ptr), sometimes as string_vector_reset(&vec).)
Remove the generic "name##_iter" function definition, and introduce "name##_empty", which performs both steps at the same time. Convert the call sites. (Note that the converted code performs more cleanup steps in some cases than strictly necessary, but the extra work is harmless, and arguably beneficial for clarity / consistency.)
Expose the "name##_empty" function definition with a new, separate macro: DEFINE_VECTOR_EMPTY(). The existent DEFINE_VECTOR_TYPE() macro permits such element types that are not pointers, or are pointers to const- and/or volatile-qualified objects. Whereas "name##_empty" requires that the elements be pointers to dynamically allocated, non-const, non-volatile objects.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- common/utils/string-vector.h | 1 + common/utils/vector.h | 27 +++++++++++++------- generator/states-connect-socket-activation.c | 9 +++---- lib/handle.c | 12 +++------ lib/utils.c | 6 ++--- common/utils/test-vector.c | 3 +-- info/show.c | 3 +-- 7 files changed, 30 insertions(+), 31 deletions(-)
diff --git a/common/utils/string-vector.h b/common/utils/string-vector.h index aa33fd48ceb5..78d0b4f9bf10 100644 --- a/common/utils/string-vector.h +++ b/common/utils/string-vector.h @@ -38,5 +38,6 @@ #include "vector.h"
DEFINE_VECTOR_TYPE (string_vector, char *); +DEFINE_VECTOR_EMPTY (string_vector);
#endif /* STRING_VECTOR_H */ diff --git a/common/utils/vector.h b/common/utils/vector.h index fb2482c853ff..14bf5b0ddbc0 100644 --- a/common/utils/vector.h +++ b/common/utils/vector.h @@ -150,15 +150,6 @@ v->len = v->cap = 0; \ } \ \ - /* Iterate over the vector, calling f() on each element. */ \ - static inline void __attribute__ ((__unused__)) \ - name##_iter (name *v, void (*f) (type elem)) \ - { \ - size_t i; \ - for (i = 0; i < v->len; ++i) \ - f (v->ptr[i]); \ - } \ - \
What's the reason for removing iter? It's used by nbdkit in ways that don't involve free(). I'd prefer if we leave this definition in.
/* Sort the elements of the vector. */ \ static inline void __attribute__ ((__unused__)) \ name##_sort (name *v, \ @@ -180,6 +171,24 @@
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
+/* This macro should only be used if: + * - the vector contains pointers, and + * - the pointed-to objects are: + * - neither const- nor volatile-qualified, and + * - allocated with malloc() or equivalent. + */ +#define DEFINE_VECTOR_EMPTY(name) \
I'm going to be that guy ... Can we call it ADD_VECTOR_EMPTY_METHOD or similar/better? Otherwise it seems like DEFINE_VECTOR_EMPTY is an alternative to DEFINE_VECTOR_TYPE (which I thought was the case initially), rather than something you have to call in addition to DEFINE_VECTOR_TYPE. The rest seems fine. Rich.
+ /* Call free() on each element of the vector, then reset the vector. \ + */ \ + static inline void __attribute__ ((__unused__)) \ + name##_empty (name *v) \ + { \ + size_t i; \ + for (i = 0; i < v->len; ++i) \ + free (v->ptr[i]); \ + name##_reset (v); \ + } + struct generic_vector { void *ptr; size_t len; diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 7138e7638e30..3b621b8be44f 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -91,8 +91,7 @@ prepare_socket_activation_environment (string_vector *env)
err: set_error (errno, "malloc"); - string_vector_iter (env, (void *) free); - free (env->ptr); + string_vector_empty (env); return -1; }
@@ -166,8 +165,7 @@ CONNECT_SA.START: SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (s); - string_vector_iter (&env, (void *) free); - free (env.ptr); + string_vector_empty (&env); return 0; } if (pid == 0) { /* child - run command */ @@ -210,8 +208,7 @@ CONNECT_SA.START:
/* Parent. */ close (s); - string_vector_iter (&env, (void *) free); - free (env.ptr); + string_vector_empty (&env); h->pid = pid;
h->connaddrlen = sizeof addr; diff --git a/lib/handle.c b/lib/handle.c index dfd8c2e5cdb9..fb92f16e6c91 100644 --- a/lib/handle.c +++ b/lib/handle.c @@ -128,8 +128,7 @@ nbd_close (struct nbd_handle *h) /* Free user callbacks first. */ nbd_unlocked_clear_debug_callback (h);
- string_vector_iter (&h->querylist, (void *) free); - free (h->querylist.ptr); + string_vector_empty (&h->querylist); free (h->bs_entries); nbd_internal_reset_size_and_flags (h); for (i = 0; i < h->meta_contexts.len; ++i) @@ -139,8 +138,7 @@ nbd_close (struct nbd_handle *h) free_cmd_list (h->cmds_to_issue); free_cmd_list (h->cmds_in_flight); free_cmd_list (h->cmds_done); - string_vector_iter (&h->argv, (void *) free); - free (h->argv.ptr); + string_vector_empty (&h->argv); if (h->sact_sockpath) { if (h->pid > 0) kill (h->pid, SIGTERM); @@ -164,8 +162,7 @@ nbd_close (struct nbd_handle *h) free (h->tls_certificates); free (h->tls_username); free (h->tls_psk_file); - string_vector_iter (&h->request_meta_contexts, (void *) free); - free (h->request_meta_contexts.ptr); + string_vector_empty (&h->request_meta_contexts); free (h->hname); pthread_mutex_destroy (&h->lock); free (h); @@ -379,8 +376,7 @@ nbd_unlocked_get_meta_context (struct nbd_handle *h, size_t i) int nbd_unlocked_clear_meta_contexts (struct nbd_handle *h) { - string_vector_iter (&h->request_meta_contexts, (void *) free); - string_vector_reset (&h->request_meta_contexts); + string_vector_empty (&h->request_meta_contexts); return 0; }
diff --git a/lib/utils.c b/lib/utils.c index c229ebfc6106..bba4b3846e77 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -93,8 +93,7 @@ nbd_internal_set_argv (struct nbd_handle *h, char **argv) return -1; }
- string_vector_iter (&h->argv, (void *) free); - string_vector_reset (&h->argv); + string_vector_empty (&h->argv);
if (nbd_internal_copy_string_list (&h->argv, argv) == -1) { set_error (errno, "realloc"); @@ -110,8 +109,7 @@ nbd_internal_set_argv (struct nbd_handle *h, char **argv) int nbd_internal_set_querylist (struct nbd_handle *h, char **queries) { - string_vector_iter (&h->querylist, (void *) free); - string_vector_reset (&h->querylist); + string_vector_empty (&h->querylist);
if (queries) { if (nbd_internal_copy_string_list (&h->querylist, queries) == -1) { diff --git a/common/utils/test-vector.c b/common/utils/test-vector.c index 55c8ebeb8a1e..05ac5cec3dba 100644 --- a/common/utils/test-vector.c +++ b/common/utils/test-vector.c @@ -151,8 +151,7 @@ test_string_vector (void) printf ("%s\n", v.ptr[i]); assert (i == 10);
- string_vector_iter (&v, (void*)free); - string_vector_reset (&v); + string_vector_empty (&v); }
static void diff --git a/info/show.c b/info/show.c index 4bf596715cf9..e6c9b9bf1243 100644 --- a/info/show.c +++ b/info/show.c @@ -275,8 +275,7 @@ show_one_export (struct nbd_handle *nbd, const char *desc, fprintf (fp, "\t},\n"); }
- string_vector_iter (&contexts, (void *) free); - free (contexts.ptr); + string_vector_empty (&contexts); free (content); free (export_name); free (export_desc);
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
On 2/15/23 17:23, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:34PM +0100, Laszlo Ersek wrote:
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free":
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Furthermore, in all 11 cases, the freeing of the vector's strings is immediately followed by the release of the vector's array-of-pointers too. (This additional step is not expressed consistently across libnbd: it's sometimes spelled as free(vec.ptr), sometimes as string_vector_reset(&vec).)
Remove the generic "name##_iter" function definition, and introduce "name##_empty", which performs both steps at the same time. Convert the call sites. (Note that the converted code performs more cleanup steps in some cases than strictly necessary, but the extra work is harmless, and arguably beneficial for clarity / consistency.)
Expose the "name##_empty" function definition with a new, separate macro: DEFINE_VECTOR_EMPTY(). The existent DEFINE_VECTOR_TYPE() macro permits such element types that are not pointers, or are pointers to const- and/or volatile-qualified objects. Whereas "name##_empty" requires that the elements be pointers to dynamically allocated, non-const, non-volatile objects.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- common/utils/string-vector.h | 1 + common/utils/vector.h | 27 +++++++++++++------- generator/states-connect-socket-activation.c | 9 +++---- lib/handle.c | 12 +++------ lib/utils.c | 6 ++--- common/utils/test-vector.c | 3 +-- info/show.c | 3 +-- 7 files changed, 30 insertions(+), 31 deletions(-)
diff --git a/common/utils/string-vector.h b/common/utils/string-vector.h index aa33fd48ceb5..78d0b4f9bf10 100644 --- a/common/utils/string-vector.h +++ b/common/utils/string-vector.h @@ -38,5 +38,6 @@ #include "vector.h"
DEFINE_VECTOR_TYPE (string_vector, char *); +DEFINE_VECTOR_EMPTY (string_vector);
#endif /* STRING_VECTOR_H */ diff --git a/common/utils/vector.h b/common/utils/vector.h index fb2482c853ff..14bf5b0ddbc0 100644 --- a/common/utils/vector.h +++ b/common/utils/vector.h @@ -150,15 +150,6 @@ v->len = v->cap = 0; \ } \ \ - /* Iterate over the vector, calling f() on each element. */ \ - static inline void __attribute__ ((__unused__)) \ - name##_iter (name *v, void (*f) (type elem)) \ - { \ - size_t i; \ - for (i = 0; i < v->len; ++i) \ - f (v->ptr[i]); \ - } \ - \
What's the reason for removing iter? It's used by nbdkit in ways that don't involve free(). I'd prefer if we leave this definition in.
Good point -- the header is shared by nbdkit and libnbd, but (because the latter are two separate projects) any single git-grep will cover only one. I'll restore _iter.
/* Sort the elements of the vector. */ \ static inline void __attribute__ ((__unused__)) \ name##_sort (name *v, \ @@ -180,6 +171,24 @@
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
+/* This macro should only be used if: + * - the vector contains pointers, and + * - the pointed-to objects are: + * - neither const- nor volatile-qualified, and + * - allocated with malloc() or equivalent. + */ +#define DEFINE_VECTOR_EMPTY(name) \
I'm going to be that guy ...
Yes, someone has to be! I knew it was virtually impossible for me to get the name right at the first try :)
Can we call it ADD_VECTOR_EMPTY_METHOD or similar/better?
Eric, any comments?
Otherwise it seems like DEFINE_VECTOR_EMPTY is an alternative to DEFINE_VECTOR_TYPE (which I thought was the case initially), rather than something you have to call in addition to DEFINE_VECTOR_TYPE.
The rest seems fine.
Thanks! Laszlo
Rich.
+ /* Call free() on each element of the vector, then reset the vector. \ + */ \ + static inline void __attribute__ ((__unused__)) \ + name##_empty (name *v) \ + { \ + size_t i; \ + for (i = 0; i < v->len; ++i) \ + free (v->ptr[i]); \ + name##_reset (v); \ + } + struct generic_vector { void *ptr; size_t len; diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 7138e7638e30..3b621b8be44f 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -91,8 +91,7 @@ prepare_socket_activation_environment (string_vector *env)
err: set_error (errno, "malloc"); - string_vector_iter (env, (void *) free); - free (env->ptr); + string_vector_empty (env); return -1; }
@@ -166,8 +165,7 @@ CONNECT_SA.START: SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (s); - string_vector_iter (&env, (void *) free); - free (env.ptr); + string_vector_empty (&env); return 0; } if (pid == 0) { /* child - run command */ @@ -210,8 +208,7 @@ CONNECT_SA.START:
/* Parent. */ close (s); - string_vector_iter (&env, (void *) free); - free (env.ptr); + string_vector_empty (&env); h->pid = pid;
h->connaddrlen = sizeof addr; diff --git a/lib/handle.c b/lib/handle.c index dfd8c2e5cdb9..fb92f16e6c91 100644 --- a/lib/handle.c +++ b/lib/handle.c @@ -128,8 +128,7 @@ nbd_close (struct nbd_handle *h) /* Free user callbacks first. */ nbd_unlocked_clear_debug_callback (h);
- string_vector_iter (&h->querylist, (void *) free); - free (h->querylist.ptr); + string_vector_empty (&h->querylist); free (h->bs_entries); nbd_internal_reset_size_and_flags (h); for (i = 0; i < h->meta_contexts.len; ++i) @@ -139,8 +138,7 @@ nbd_close (struct nbd_handle *h) free_cmd_list (h->cmds_to_issue); free_cmd_list (h->cmds_in_flight); free_cmd_list (h->cmds_done); - string_vector_iter (&h->argv, (void *) free); - free (h->argv.ptr); + string_vector_empty (&h->argv); if (h->sact_sockpath) { if (h->pid > 0) kill (h->pid, SIGTERM); @@ -164,8 +162,7 @@ nbd_close (struct nbd_handle *h) free (h->tls_certificates); free (h->tls_username); free (h->tls_psk_file); - string_vector_iter (&h->request_meta_contexts, (void *) free); - free (h->request_meta_contexts.ptr); + string_vector_empty (&h->request_meta_contexts); free (h->hname); pthread_mutex_destroy (&h->lock); free (h); @@ -379,8 +376,7 @@ nbd_unlocked_get_meta_context (struct nbd_handle *h, size_t i) int nbd_unlocked_clear_meta_contexts (struct nbd_handle *h) { - string_vector_iter (&h->request_meta_contexts, (void *) free); - string_vector_reset (&h->request_meta_contexts); + string_vector_empty (&h->request_meta_contexts); return 0; }
diff --git a/lib/utils.c b/lib/utils.c index c229ebfc6106..bba4b3846e77 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -93,8 +93,7 @@ nbd_internal_set_argv (struct nbd_handle *h, char **argv) return -1; }
- string_vector_iter (&h->argv, (void *) free); - string_vector_reset (&h->argv); + string_vector_empty (&h->argv);
if (nbd_internal_copy_string_list (&h->argv, argv) == -1) { set_error (errno, "realloc"); @@ -110,8 +109,7 @@ nbd_internal_set_argv (struct nbd_handle *h, char **argv) int nbd_internal_set_querylist (struct nbd_handle *h, char **queries) { - string_vector_iter (&h->querylist, (void *) free); - string_vector_reset (&h->querylist); + string_vector_empty (&h->querylist);
if (queries) { if (nbd_internal_copy_string_list (&h->querylist, queries) == -1) { diff --git a/common/utils/test-vector.c b/common/utils/test-vector.c index 55c8ebeb8a1e..05ac5cec3dba 100644 --- a/common/utils/test-vector.c +++ b/common/utils/test-vector.c @@ -151,8 +151,7 @@ test_string_vector (void) printf ("%s\n", v.ptr[i]); assert (i == 10);
- string_vector_iter (&v, (void*)free); - string_vector_reset (&v); + string_vector_empty (&v); }
static void diff --git a/info/show.c b/info/show.c index 4bf596715cf9..e6c9b9bf1243 100644 --- a/info/show.c +++ b/info/show.c @@ -275,8 +275,7 @@ show_one_export (struct nbd_handle *nbd, const char *desc, fprintf (fp, "\t},\n"); }
- string_vector_iter (&contexts, (void *) free); - free (contexts.ptr); + string_vector_empty (&contexts); free (content); free (export_name); free (export_desc);
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
On Wed, Feb 15, 2023 at 03:11:34PM +0100, Laszlo Ersek wrote:
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free":
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Tangentially related: casting function pointers in general may get harder as more compilers move towards C23 and its newer rules (see for example https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00055.html or this gcc 13 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108694 which highlights some of the warnings that newer compilers will start warning us about). While this patch focuses on avoiding casts between fn(*)(type*) and void*, I don't know off-hand if C23 will permit fn(*)(void*) as a compatible function pointer with fn(*)(type).
Furthermore, in all 11 cases, the freeing of the vector's strings is immediately followed by the release of the vector's array-of-pointers too. (This additional step is not expressed consistently across libnbd: it's sometimes spelled as free(vec.ptr), sometimes as string_vector_reset(&vec).)
As Rich pointed out, just because libnbd is always passing 'free' does not mean that nbdkit is doing likewise, and we want to keep this file shared between the two projects. Being able to conditionally add the cleanup function (where we don't want it on non-pointer vectors) makes sense, but removing the iterator function at the same time is a step too far.
Remove the generic "name##_iter" function definition, and introduce "name##_empty", which performs both steps at the same time. Convert the call sites. (Note that the converted code performs more cleanup steps in some cases than strictly necessary, but the extra work is harmless, and arguably beneficial for clarity / consistency.)
Agreed that the extra cleanup is not going to affect our hot path.
Expose the "name##_empty" function definition with a new, separate macro: DEFINE_VECTOR_EMPTY(). The existent DEFINE_VECTOR_TYPE() macro permits such element types that are not pointers, or are pointers to const- and/or volatile-qualified objects. Whereas "name##_empty" requires that the elements be pointers to dynamically allocated, non-const, non-volatile objects.
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
+++ b/common/utils/vector.h
+/* This macro should only be used if: + * - the vector contains pointers, and + * - the pointed-to objects are: + * - neither const- nor volatile-qualified, and + * - allocated with malloc() or equivalent. + */ +#define DEFINE_VECTOR_EMPTY(name) \ + /* Call free() on each element of the vector, then reset the vector. \ + */ \ + static inline void __attribute__ ((__unused__)) \ + name##_empty (name *v) \ + { \ + size_t i; \ + for (i = 0; i < v->len; ++i) \ + free (v->ptr[i]); \ + name##_reset (v); \ + } +
Thinking higher-level now, your new macro is something where we have to do a two-step declaration of macro types where we want the new function. Would it make more sense to change the signature of the DEFINE_VECTOR_TYPE() macro to take a third argument containing the function name to call on cleanup paths, with the ability to easily write/reuse a no-op function for vectors that don't need to call free(), where we can then unconditionally declare name##_empty() that will work with all vector types? That is, should we consider instead doing something like: DEFINE_VECTOR_TYPE (string_vector, char *, free); DEFINE_VECTOR_TYPE (int_vector, int, noop);
+++ b/generator/states-connect-socket-activation.c @@ -91,8 +91,7 @@ prepare_socket_activation_environment (string_vector *env)
err: set_error (errno, "malloc"); - string_vector_iter (env, (void *) free); - free (env->ptr); + string_vector_empty (env); return -1;
At any rate, I like how the callers look improved, even if we aren't quite settled on how the vector.h changes should end up. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 21:27, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:34PM +0100, Laszlo Ersek wrote:
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free":
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Tangentially related: casting function pointers in general may get harder as more compilers move towards C23 and its newer rules (see for example https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00055.html or this gcc 13 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108694 which highlights some of the warnings that newer compilers will start warning us about). While this patch focuses on avoiding casts between fn(*)(type*) and void*, I don't know off-hand if C23 will permit fn(*)(void*) as a compatible function pointer with fn(*)(type).
My understanding is that, per C99 at least, ret_type(*)(void*) is compatible with ret_type(*)(type*) precisely if void* is compatible with type* (6.7.5.3p15). Whether void* is compatible with type* depends on... ugh, that's hard to deduce from the standard. 6.7.5.1p2 says, "For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types". I don't think "void" (as a type in itself) is compatible with any type! Now, there is one particular statement on void* -- 6.2.5p27 says, "A pointer to void shall have the same representation and alignment requirements as a pointer to a character type." (I think the statements about *converting* void* to type* and vice versa do not apply here; AFAICT "compatibility" is about reinterpreting the bit patterns, not converting values.) In Annex I (Common warnings, "informative"), the following is listed: "An implicit narrowing conversion is encountered, such as the assignment of [...] a pointer to void to a pointer to any type other than a character type". All in all I don't think ret_type(*)(type*) is compatible with ret_type(*)(void*) in the general case, and that's why in this patch I didn't want to go more general than I absolutely needed to.
Furthermore, in all 11 cases, the freeing of the vector's strings is immediately followed by the release of the vector's array-of-pointers too. (This additional step is not expressed consistently across libnbd: it's sometimes spelled as free(vec.ptr), sometimes as string_vector_reset(&vec).)
As Rich pointed out, just because libnbd is always passing 'free' does not mean that nbdkit is doing likewise, and we want to keep this file shared between the two projects. Being able to conditionally add the cleanup function (where we don't want it on non-pointer vectors) makes sense, but removing the iterator function at the same time is a step too far.
OK.
Remove the generic "name##_iter" function definition, and introduce "name##_empty", which performs both steps at the same time. Convert the call sites. (Note that the converted code performs more cleanup steps in some cases than strictly necessary, but the extra work is harmless, and arguably beneficial for clarity / consistency.)
Agreed that the extra cleanup is not going to affect our hot path.
Expose the "name##_empty" function definition with a new, separate macro: DEFINE_VECTOR_EMPTY(). The existent DEFINE_VECTOR_TYPE() macro permits such element types that are not pointers, or are pointers to const- and/or volatile-qualified objects. Whereas "name##_empty" requires that the elements be pointers to dynamically allocated, non-const, non-volatile objects.
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
+++ b/common/utils/vector.h
+/* This macro should only be used if: + * - the vector contains pointers, and + * - the pointed-to objects are: + * - neither const- nor volatile-qualified, and + * - allocated with malloc() or equivalent. + */ +#define DEFINE_VECTOR_EMPTY(name) \ + /* Call free() on each element of the vector, then reset the vector. \ + */ \ + static inline void __attribute__ ((__unused__)) \ + name##_empty (name *v) \ + { \ + size_t i; \ + for (i = 0; i < v->len; ++i) \ + free (v->ptr[i]); \ + name##_reset (v); \ + } +
Thinking higher-level now, your new macro is something where we have to do a two-step declaration of macro types where we want the new function. Would it make more sense to change the signature of the DEFINE_VECTOR_TYPE() macro to take a third argument containing the function name to call on cleanup paths, with the ability to easily write/reuse a no-op function for vectors that don't need to call free(), where we can then unconditionally declare name##_empty() that will work with all vector types? That is, should we consider instead doing something like:
DEFINE_VECTOR_TYPE (string_vector, char *, free);
DEFINE_VECTOR_TYPE (int_vector, int, noop);
My counter-arguments: - this requires updates to all existent DEFINE_VECTOR_TYPE macro invocations, - with "noop" passed to _reset, _reset and _empty become effectively the same, so we get (at least partially) duplicate APIs, - this would be a step towards combinatorial explosion - if "noop" does nothing, then why call it on each element of the vector anyway? It's not only the function call that becomes superfluous in the loop bodym with the function being "noop", but the loop *itself* becomes superfluous. So then we might want to compare the function pointer against "noop" outside of the loop... and that way we get a bunch of complications :) I chose this approach because it is purely additive and precisely as generic/specific as it needs to be. We already have 11 use cases, so I don't think it's *too* specific.
+++ b/generator/states-connect-socket-activation.c @@ -91,8 +91,7 @@ prepare_socket_activation_environment (string_vector *env)
err: set_error (errno, "malloc"); - string_vector_iter (env, (void *) free); - free (env->ptr); + string_vector_empty (env); return -1;
At any rate, I like how the callers look improved, even if we aren't quite settled on how the vector.h changes should end up.
Thanks! Laszlo
On Mon, Feb 20, 2023 at 06:03:13PM +0100, Laszlo Ersek wrote:
On 2/15/23 21:27, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:34PM +0100, Laszlo Ersek wrote:
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free":
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Tangentially related: casting function pointers in general may get harder as more compilers move towards C23 and its newer rules (see for example https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00055.html or this gcc 13 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108694 which highlights some of the warnings that newer compilers will start warning us about). While this patch focuses on avoiding casts between fn(*)(type*) and void*, I don't know off-hand if C23 will permit fn(*)(void*) as a compatible function pointer with fn(*)(type).
My understanding is that, per C99 at least, ret_type(*)(void*) is compatible with ret_type(*)(type*) precisely if void* is compatible with type* (6.7.5.3p15).
Whether void* is compatible with type* depends on... ugh, that's hard to deduce from the standard. 6.7.5.1p2 says, "For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types". I don't think "void" (as a type in itself) is compatible with any type!
Now, there is one particular statement on void* -- 6.2.5p27 says, "A pointer to void shall have the same representation and alignment requirements as a pointer to a character type."
(I think the statements about *converting* void* to type* and vice versa do not apply here; AFAICT "compatibility" is about reinterpreting the bit patterns, not converting values.)
In Annex I (Common warnings, "informative"), the following is listed: "An implicit narrowing conversion is encountered, such as the assignment of [...] a pointer to void to a pointer to any type other than a character type".
All in all I don't think ret_type(*)(type*) is compatible with ret_type(*)(void*) in the general case, and that's why in this patch I didn't want to go more general than I absolutely needed to.
Thanks for at least trying to find something definitive in the standard. Now you know why I skipped researching this particular issue - it's not straightforward to figure out when function pointers with differing parameter types are compatible.
Thinking higher-level now, your new macro is something where we have to do a two-step declaration of macro types where we want the new function. Would it make more sense to change the signature of the DEFINE_VECTOR_TYPE() macro to take a third argument containing the function name to call on cleanup paths, with the ability to easily write/reuse a no-op function for vectors that don't need to call free(), where we can then unconditionally declare name##_empty() that will work with all vector types? That is, should we consider instead doing something like:
DEFINE_VECTOR_TYPE (string_vector, char *, free);
DEFINE_VECTOR_TYPE (int_vector, int, noop);
My counter-arguments:
- this requires updates to all existent DEFINE_VECTOR_TYPE macro invocations,
- with "noop" passed to _reset, _reset and _empty become effectively the same, so we get (at least partially) duplicate APIs,
- this would be a step towards combinatorial explosion
- if "noop" does nothing, then why call it on each element of the vector anyway? It's not only the function call that becomes superfluous in the loop bodym with the function being "noop", but the loop *itself* becomes superfluous. So then we might want to compare the function pointer against "noop" outside of the loop... and that way we get a bunch of complications :)
I chose this approach because it is purely additive and precisely as generic/specific as it needs to be. We already have 11 use cases, so I don't think it's *too* specific.
We may still want some division of: DEFINE_VECTOR_TYPE (int_vector, int); DEFINE_POINTER_VECTOR_TYPE (string_vector, char *, free); where under the hood, DEFINE_POINTER_VECTOR_TYPE(type, base, fn) invokes both DEFINE_VECTOR_TYPE(type, base) and DEFINE_VECTOR_EMPTY(type, fn), or whatever we name the second function.
From your other mail in this subthread:
+#define DEFINE_VECTOR_EMPTY(name) \
I'm going to be that guy ...
Yes, someone has to be! I knew it was virtually impossible for me to get the name right at the first try :)
Can we call it ADD_VECTOR_EMPTY_METHOD or similar/better?
Eric, any comments?
ADD_VECTOR_EMPTY_METHOD() instead of DEFINE_VECTOR_EMPTY() works for me. Whether we hard-code 'free()' as the lone function that will be utilized in the generated TYPE_vector_empty(), or if the macro takes fn as a parameter, is up to you. Comparing: DEFINE_VECTOR_TYPE(string_vector, char *); ADD_VECTOR_EMPTY_METHOD(string_vector); vs. DEFINE_VECTOR_TYPE(string_vector, char *); ADD_VECTOR_EMPTY_METHOD(string_vector, free); does either one make it more obvious that we are doing a two-step definition (first of the type, then of the added cleanup method)? And if so, does my idea of a single wrapper function that calls both intermediate macros make sense so that the 11 pointer vector types in libnbd are still one-liner macro calls, without penalizing the scalar vector types in nbdkit? -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/20/23 21:38, Eric Blake wrote:
On Mon, Feb 20, 2023 at 06:03:13PM +0100, Laszlo Ersek wrote:
On 2/15/23 21:27, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:34PM +0100, Laszlo Ersek wrote:
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free":
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Tangentially related: casting function pointers in general may get harder as more compilers move towards C23 and its newer rules (see for example https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00055.html or this gcc 13 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108694 which highlights some of the warnings that newer compilers will start warning us about). While this patch focuses on avoiding casts between fn(*)(type*) and void*, I don't know off-hand if C23 will permit fn(*)(void*) as a compatible function pointer with fn(*)(type).
My understanding is that, per C99 at least, ret_type(*)(void*) is compatible with ret_type(*)(type*) precisely if void* is compatible with type* (6.7.5.3p15).
Whether void* is compatible with type* depends on... ugh, that's hard to deduce from the standard. 6.7.5.1p2 says, "For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types". I don't think "void" (as a type in itself) is compatible with any type!
Now, there is one particular statement on void* -- 6.2.5p27 says, "A pointer to void shall have the same representation and alignment requirements as a pointer to a character type."
(I think the statements about *converting* void* to type* and vice versa do not apply here; AFAICT "compatibility" is about reinterpreting the bit patterns, not converting values.)
In Annex I (Common warnings, "informative"), the following is listed: "An implicit narrowing conversion is encountered, such as the assignment of [...] a pointer to void to a pointer to any type other than a character type".
All in all I don't think ret_type(*)(type*) is compatible with ret_type(*)(void*) in the general case, and that's why in this patch I didn't want to go more general than I absolutely needed to.
Thanks for at least trying to find something definitive in the standard. Now you know why I skipped researching this particular issue - it's not straightforward to figure out when function pointers with differing parameter types are compatible.
Let's be honest: it's staggeringly difficult to collect whatever "compatible type" means, in the standard.
Thinking higher-level now, your new macro is something where we have to do a two-step declaration of macro types where we want the new function. Would it make more sense to change the signature of the DEFINE_VECTOR_TYPE() macro to take a third argument containing the function name to call on cleanup paths, with the ability to easily write/reuse a no-op function for vectors that don't need to call free(), where we can then unconditionally declare name##_empty() that will work with all vector types? That is, should we consider instead doing something like:
DEFINE_VECTOR_TYPE (string_vector, char *, free);
DEFINE_VECTOR_TYPE (int_vector, int, noop);
My counter-arguments:
- this requires updates to all existent DEFINE_VECTOR_TYPE macro invocations,
- with "noop" passed to _reset, _reset and _empty become effectively the same, so we get (at least partially) duplicate APIs,
- this would be a step towards combinatorial explosion
- if "noop" does nothing, then why call it on each element of the vector anyway? It's not only the function call that becomes superfluous in the loop bodym with the function being "noop", but the loop *itself* becomes superfluous. So then we might want to compare the function pointer against "noop" outside of the loop... and that way we get a bunch of complications :)
I chose this approach because it is purely additive and precisely as generic/specific as it needs to be. We already have 11 use cases, so I don't think it's *too* specific.
We may still want some division of:
DEFINE_VECTOR_TYPE (int_vector, int); DEFINE_POINTER_VECTOR_TYPE (string_vector, char *, free);
where under the hood, DEFINE_POINTER_VECTOR_TYPE(type, base, fn) invokes both DEFINE_VECTOR_TYPE(type, base) and DEFINE_VECTOR_EMPTY(type, fn), or whatever we name the second function.
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
From your other mail in this subthread:
+#define DEFINE_VECTOR_EMPTY(name) \
I'm going to be that guy ...
Yes, someone has to be! I knew it was virtually impossible for me to get the name right at the first try :)
Can we call it ADD_VECTOR_EMPTY_METHOD or similar/better?
Eric, any comments?
ADD_VECTOR_EMPTY_METHOD() instead of DEFINE_VECTOR_EMPTY() works for me.
OK, ADD_VECTOR_EMPTY_METHOD() can work with the above.
Whether we hard-code 'free()' as the lone function that will be utilized in the generated TYPE_vector_empty(), or if the macro takes fn as a parameter, is up to you.
I prefer hard-coding free(). Once we expose the callback, we should arguably -- for generality's sake! -- expose a context pointer too, one that the callback receives invariantly alongside the element to free. And then we're back to where we are now: we can't just pass "free", because "free" does not take a context pointer.
Comparing:
DEFINE_VECTOR_TYPE(string_vector, char *); ADD_VECTOR_EMPTY_METHOD(string_vector);
vs.
DEFINE_VECTOR_TYPE(string_vector, char *); ADD_VECTOR_EMPTY_METHOD(string_vector, free);
does either one make it more obvious that we are doing a two-step definition (first of the type, then of the added cleanup method)?
I think both are equally functional, but the latter is at a worse spot IMO on the "generality spectrum". It's too generic, yet not generic enough. Too generic because we only need "free" for now, and not generic enough because a truly generic callback would take an additional void* context pointer.
And if so, does my idea of a single wrapper function that calls both
s/wrapper function/wrapper macro/
intermediate macros make sense
Sure. In practical terms, the difference is whether I *add* a new macro invocation to "common/utils/string-vector.h", or *replace* the existing macro invocation there. I *think* the wrapper macro is slightly overkill (we have 1 use for it!), but it's certainly doable.
so that the 11 pointer vector types in libnbd are still one-liner macro calls, without penalizing the scalar vector types in nbdkit?
Yep. Thanks! Laszlo
On Tue, Feb 21, 2023 at 05:23:52PM +0100, Laszlo Ersek wrote:
On 2/20/23 21:38, Eric Blake wrote:
On Mon, Feb 20, 2023 at 06:03:13PM +0100, Laszlo Ersek wrote:
On 2/15/23 21:27, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:34PM +0100, Laszlo Ersek wrote:
The "name##_iter" function is used 11 times in libnbd; in all those cases, "name" is "string_vector", and the "f" callback is "free":
string_vector_iter (..., (void *) free);
Casting "free" to (void*) is ugly. (Well-defined by POSIX, but still.)
Tangentially related: casting function pointers in general may get harder as more compilers move towards C23 and its newer rules (see for example https://lists.gnu.org/archive/html/bug-gnulib/2023-02/msg00055.html or this gcc 13 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108694 which highlights some of the warnings that newer compilers will start warning us about). While this patch focuses on avoiding casts between fn(*)(type*) and void*, I don't know off-hand if C23 will permit fn(*)(void*) as a compatible function pointer with fn(*)(type).
My understanding is that, per C99 at least, ret_type(*)(void*) is compatible with ret_type(*)(type*) precisely if void* is compatible with type* (6.7.5.3p15).
Whether void* is compatible with type* depends on... ugh, that's hard to deduce from the standard. 6.7.5.1p2 says, "For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types". I don't think "void" (as a type in itself) is compatible with any type!
Now, there is one particular statement on void* -- 6.2.5p27 says, "A pointer to void shall have the same representation and alignment requirements as a pointer to a character type."
(I think the statements about *converting* void* to type* and vice versa do not apply here; AFAICT "compatibility" is about reinterpreting the bit patterns, not converting values.)
In Annex I (Common warnings, "informative"), the following is listed: "An implicit narrowing conversion is encountered, such as the assignment of [...] a pointer to void to a pointer to any type other than a character type".
All in all I don't think ret_type(*)(type*) is compatible with ret_type(*)(void*) in the general case, and that's why in this patch I didn't want to go more general than I absolutely needed to.
Thanks for at least trying to find something definitive in the standard. Now you know why I skipped researching this particular issue - it's not straightforward to figure out when function pointers with differing parameter types are compatible.
Let's be honest: it's staggeringly difficult to collect whatever "compatible type" means, in the standard.
Thinking higher-level now, your new macro is something where we have to do a two-step declaration of macro types where we want the new function. Would it make more sense to change the signature of the DEFINE_VECTOR_TYPE() macro to take a third argument containing the function name to call on cleanup paths, with the ability to easily write/reuse a no-op function for vectors that don't need to call free(), where we can then unconditionally declare name##_empty() that will work with all vector types? That is, should we consider instead doing something like:
DEFINE_VECTOR_TYPE (string_vector, char *, free);
DEFINE_VECTOR_TYPE (int_vector, int, noop);
My counter-arguments:
- this requires updates to all existent DEFINE_VECTOR_TYPE macro invocations,
- with "noop" passed to _reset, _reset and _empty become effectively the same, so we get (at least partially) duplicate APIs,
- this would be a step towards combinatorial explosion
- if "noop" does nothing, then why call it on each element of the vector anyway? It's not only the function call that becomes superfluous in the loop bodym with the function being "noop", but the loop *itself* becomes superfluous. So then we might want to compare the function pointer against "noop" outside of the loop... and that way we get a bunch of complications :)
I chose this approach because it is purely additive and precisely as generic/specific as it needs to be. We already have 11 use cases, so I don't think it's *too* specific.
We may still want some division of:
DEFINE_VECTOR_TYPE (int_vector, int); DEFINE_POINTER_VECTOR_TYPE (string_vector, char *, free);
where under the hood, DEFINE_POINTER_VECTOR_TYPE(type, base, fn) invokes both DEFINE_VECTOR_TYPE(type, base) and DEFINE_VECTOR_EMPTY(type, fn), or whatever we name the second function.
I quite like Eric's suggestion, but it's probably too much complexity for this patch series.
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
You might ignore this for a first draft, but it is apparently possible to statically detect this (at least, if using GCC/clang): https://stackoverflow.com/questions/19255148/check-if-a-macro-argument-is-a-...
ADD_VECTOR_EMPTY_METHOD() instead of DEFINE_VECTOR_EMPTY() works for me.
OK, ADD_VECTOR_EMPTY_METHOD() can work with the above.
This sounds fine for now, and since these are implementation details we can always revisit them in future. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
On 2/22/23 09:17, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 05:23:52PM +0100, Laszlo Ersek wrote:
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
You might ignore this for a first draft, but it is apparently possible to statically detect this (at least, if using GCC/clang):
https://stackoverflow.com/questions/19255148/check-if-a-macro-argument-is-a-...
Right, we already use at least __builtin_types_compatible_p in TYPE_IS_ARRAY(); that's what I wouldn't want more of, at least via this series. Laszlo
On 2/22/23 10:40, Laszlo Ersek wrote:
On 2/22/23 09:17, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 05:23:52PM +0100, Laszlo Ersek wrote:
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
You might ignore this for a first draft, but it is apparently possible to statically detect this (at least, if using GCC/clang):
https://stackoverflow.com/questions/19255148/check-if-a-macro-argument-is-a-...
Right, we already use at least __builtin_types_compatible_p in TYPE_IS_ARRAY(); that's what I wouldn't want more of, at least via this series.
OK, summarizing the TODOs for this particular patch: 1. keep _iter 2. rename DEFINE_VECTOR_EMPTY to ADD_VECTOR_EMPTY_METHOD 3. introduce DEFINE_POINTER_VECTOR_TYPE that expands to DEFINE_VECTOR_TYPE + ADD_VECTOR_EMPTY_METHOD 4. DEFINE_POINTER_VECTOR_TYPE (and ADD_VECTOR_EMPTY_METHOD) should not take "free" as a parameter; ADD_VECTOR_EMPTY_METHOD should hard-code it 5. in "common/utils/string-vector.h", don't ADD_VECTOR_EMPTY_METHOD; instead, replace DEFINE_VECTOR_TYPE with DEFINE_POINTER_VECTOR_TYPE. 6. in ADD_VECTOR_EMPTY_METHOD, consider checking that the element type is a pointer type. --*-- Re: 6, I find that the stackoverflow solution above is too complicated. I mentioned our existent TYPE_IS_ARRAY macro: #define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) This is perfectly usable for our purposes, as !TYPE_IS_ARRAY(). The reason is that when this macro is applied to anything that's *neither* a pointer *nor* an array, we get a build error at once: #define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) int main(void) { int x[5]; int *y; int z; TYPE_IS_ARRAY (x); TYPE_IS_ARRAY (y); TYPE_IS_ARRAY (z); return 0; } ---> isptr.c: In function ‘main’: isptr.c:2:59: error: subscripted value is neither array nor pointer nor vector 2 | (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) | ^ isptr.c:12:3: note: in expansion of macro ‘TYPE_IS_ARRAY’ 12 | TYPE_IS_ARRAY (z); | ^~~~~~~~~~~~~ (the "nor vector" part of the error message can most likely be ignored, I believe it refers to the C++ standard library vector class) Thus, with types that are neither pointers nor arrays nicely caught at compilation time, !TYPE_IS_ARRAY stands for "pointer". I'll experiment with this a bit, but if it becomes too complex, I'll likely drop step 6. Thanks Laszlo
On Wed, Feb 22, 2023 at 05:22:57PM +0100, Laszlo Ersek wrote:
On 2/22/23 10:40, Laszlo Ersek wrote:
On 2/22/23 09:17, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 05:23:52PM +0100, Laszlo Ersek wrote:
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
You might ignore this for a first draft, but it is apparently possible to statically detect this (at least, if using GCC/clang):
https://stackoverflow.com/questions/19255148/check-if-a-macro-argument-is-a-...
Right, we already use at least __builtin_types_compatible_p in TYPE_IS_ARRAY(); that's what I wouldn't want more of, at least via this series.
OK, summarizing the TODOs for this particular patch:
1. keep _iter
2. rename DEFINE_VECTOR_EMPTY to ADD_VECTOR_EMPTY_METHOD
3. introduce DEFINE_POINTER_VECTOR_TYPE that expands to DEFINE_VECTOR_TYPE + ADD_VECTOR_EMPTY_METHOD
4. DEFINE_POINTER_VECTOR_TYPE (and ADD_VECTOR_EMPTY_METHOD) should not take "free" as a parameter; ADD_VECTOR_EMPTY_METHOD should hard-code it
5. in "common/utils/string-vector.h", don't ADD_VECTOR_EMPTY_METHOD; instead, replace DEFINE_VECTOR_TYPE with DEFINE_POINTER_VECTOR_TYPE.
6. in ADD_VECTOR_EMPTY_METHOD, consider checking that the element type is a pointer type.
Sounds like a plan.
--*--
Re: 6, I find that the stackoverflow solution above is too complicated. I mentioned our existent TYPE_IS_ARRAY macro:
#define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0])))
This is perfectly usable for our purposes, as !TYPE_IS_ARRAY(). The reason is that when this macro is applied to anything that's *neither* a pointer *nor* an array, we get a build error at once:
#define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0])))
int main(void) { int x[5]; int *y; int z;
TYPE_IS_ARRAY (x); TYPE_IS_ARRAY (y); TYPE_IS_ARRAY (z); return 0; }
--->
isptr.c: In function ‘main’: isptr.c:2:59: error: subscripted value is neither array nor pointer nor vector 2 | (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) | ^ isptr.c:12:3: note: in expansion of macro ‘TYPE_IS_ARRAY’ 12 | TYPE_IS_ARRAY (z); | ^~~~~~~~~~~~~
(the "nor vector" part of the error message can most likely be ignored, I believe it refers to the C++ standard library vector class)
Thus, with types that are neither pointers nor arrays nicely caught at compilation time, !TYPE_IS_ARRAY stands for "pointer".
Nice .. but probably want to define: #define TYPE_IS_POINTER(p) (!TYPE_IS_ARRAY(p))
I'll experiment with this a bit, but if it becomes too complex, I'll likely drop step 6.
Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
On 2/22/23 18:01, Richard W.M. Jones wrote:
On Wed, Feb 22, 2023 at 05:22:57PM +0100, Laszlo Ersek wrote:
On 2/22/23 10:40, Laszlo Ersek wrote:
On 2/22/23 09:17, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 05:23:52PM +0100, Laszlo Ersek wrote:
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
You might ignore this for a first draft, but it is apparently possible to statically detect this (at least, if using GCC/clang):
https://stackoverflow.com/questions/19255148/check-if-a-macro-argument-is-a-...
Right, we already use at least __builtin_types_compatible_p in TYPE_IS_ARRAY(); that's what I wouldn't want more of, at least via this series.
OK, summarizing the TODOs for this particular patch:
1. keep _iter
2. rename DEFINE_VECTOR_EMPTY to ADD_VECTOR_EMPTY_METHOD
3. introduce DEFINE_POINTER_VECTOR_TYPE that expands to DEFINE_VECTOR_TYPE + ADD_VECTOR_EMPTY_METHOD
4. DEFINE_POINTER_VECTOR_TYPE (and ADD_VECTOR_EMPTY_METHOD) should not take "free" as a parameter; ADD_VECTOR_EMPTY_METHOD should hard-code it
5. in "common/utils/string-vector.h", don't ADD_VECTOR_EMPTY_METHOD; instead, replace DEFINE_VECTOR_TYPE with DEFINE_POINTER_VECTOR_TYPE.
6. in ADD_VECTOR_EMPTY_METHOD, consider checking that the element type is a pointer type.
Sounds like a plan.
--*--
Re: 6, I find that the stackoverflow solution above is too complicated. I mentioned our existent TYPE_IS_ARRAY macro:
#define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0])))
This is perfectly usable for our purposes, as !TYPE_IS_ARRAY(). The reason is that when this macro is applied to anything that's *neither* a pointer *nor* an array, we get a build error at once:
#define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0])))
int main(void) { int x[5]; int *y; int z;
TYPE_IS_ARRAY (x); TYPE_IS_ARRAY (y); TYPE_IS_ARRAY (z); return 0; }
--->
isptr.c: In function ‘main’: isptr.c:2:59: error: subscripted value is neither array nor pointer nor vector 2 | (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) | ^ isptr.c:12:3: note: in expansion of macro ‘TYPE_IS_ARRAY’ 12 | TYPE_IS_ARRAY (z); | ^~~~~~~~~~~~~
(the "nor vector" part of the error message can most likely be ignored, I believe it refers to the C++ standard library vector class)
Thus, with types that are neither pointers nor arrays nicely caught at compilation time, !TYPE_IS_ARRAY stands for "pointer".
Nice .. but probably want to define:
#define TYPE_IS_POINTER(p) (!TYPE_IS_ARRAY(p))
I'll experiment with this a bit, but if it becomes too complex, I'll likely drop step 6.
Sigh, this patch has now turned into a series of 5 patches. I'll post it separately tomorrow. /smh Laszlo
On 2/23/23 18:54, Laszlo Ersek wrote:
On 2/22/23 18:01, Richard W.M. Jones wrote:
On Wed, Feb 22, 2023 at 05:22:57PM +0100, Laszlo Ersek wrote:
On 2/22/23 10:40, Laszlo Ersek wrote:
On 2/22/23 09:17, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 05:23:52PM +0100, Laszlo Ersek wrote:
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
You might ignore this for a first draft, but it is apparently possible to statically detect this (at least, if using GCC/clang):
https://stackoverflow.com/questions/19255148/check-if-a-macro-argument-is-a-...
Right, we already use at least __builtin_types_compatible_p in TYPE_IS_ARRAY(); that's what I wouldn't want more of, at least via this series.
OK, summarizing the TODOs for this particular patch:
1. keep _iter
2. rename DEFINE_VECTOR_EMPTY to ADD_VECTOR_EMPTY_METHOD
3. introduce DEFINE_POINTER_VECTOR_TYPE that expands to DEFINE_VECTOR_TYPE + ADD_VECTOR_EMPTY_METHOD
4. DEFINE_POINTER_VECTOR_TYPE (and ADD_VECTOR_EMPTY_METHOD) should not take "free" as a parameter; ADD_VECTOR_EMPTY_METHOD should hard-code it
5. in "common/utils/string-vector.h", don't ADD_VECTOR_EMPTY_METHOD; instead, replace DEFINE_VECTOR_TYPE with DEFINE_POINTER_VECTOR_TYPE.
6. in ADD_VECTOR_EMPTY_METHOD, consider checking that the element type is a pointer type.
Sounds like a plan.
--*--
Re: 6, I find that the stackoverflow solution above is too complicated. I mentioned our existent TYPE_IS_ARRAY macro:
#define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0])))
This is perfectly usable for our purposes, as !TYPE_IS_ARRAY(). The reason is that when this macro is applied to anything that's *neither* a pointer *nor* an array, we get a build error at once:
#define TYPE_IS_ARRAY(a) \ (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0])))
int main(void) { int x[5]; int *y; int z;
TYPE_IS_ARRAY (x); TYPE_IS_ARRAY (y); TYPE_IS_ARRAY (z); return 0; }
--->
isptr.c: In function ‘main’: isptr.c:2:59: error: subscripted value is neither array nor pointer nor vector 2 | (!__builtin_types_compatible_p (typeof (a), typeof (&(a)[0]))) | ^ isptr.c:12:3: note: in expansion of macro ‘TYPE_IS_ARRAY’ 12 | TYPE_IS_ARRAY (z); | ^~~~~~~~~~~~~
(the "nor vector" part of the error message can most likely be ignored, I believe it refers to the C++ standard library vector class)
Thus, with types that are neither pointers nor arrays nicely caught at compilation time, !TYPE_IS_ARRAY stands for "pointer".
Nice .. but probably want to define:
#define TYPE_IS_POINTER(p) (!TYPE_IS_ARRAY(p))
I'll experiment with this a bit, but if it becomes too complex, I'll likely drop step 6.
Sigh, this patch has now turned into a series of 5 patches. I'll post it separately tomorrow. /smh
This patch has been rewritten and merged as commit range 254ac22dde42..c7ff70101e8c. Laszlo
On Wed, Feb 22, 2023 at 08:17:09AM +0000, Richard W.M. Jones wrote:
We may still want some division of:
DEFINE_VECTOR_TYPE (int_vector, int); DEFINE_POINTER_VECTOR_TYPE (string_vector, char *, free);
where under the hood, DEFINE_POINTER_VECTOR_TYPE(type, base, fn) invokes both DEFINE_VECTOR_TYPE(type, base) and DEFINE_VECTOR_EMPTY(type, fn), or whatever we name the second function.
I quite like Eric's suggestion, but it's probably too much complexity for this patch series.
This is doable, but I hope it's not expected that DEFINE_POINTER_VECTOR_TYPE() *enforce* that the element type be a pointer :)
You might ignore this for a first draft, but it is apparently possible to statically detect this (at least, if using GCC/clang):
https://stackoverflow.com/questions/19255148/check-if-a-macro-argument-is-a-...
That particular question worries about a macro that compiles without error for both pointer and scalar types as arguments; a simpler macro compiles correctly for pointers (and perhaps an array that decays to a pointer, if we want that), while causing compilation error on a scalar, reinforcing that it is an error to use the macro on a non-pointer. But ideas about the future don't need to hold up the present.
ADD_VECTOR_EMPTY_METHOD() instead of DEFINE_VECTOR_EMPTY() works for me.
OK, ADD_VECTOR_EMPTY_METHOD() can work with the above.
This sounds fine for now, and since these are implementation details we can always revisit them in future.
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
While nbd_internal_fork_safe_perror() must indeed call write(), and arguably justifiedly ignores the return value of write(), we can still make the write operations slightly more robust. Let's do that by introducing xwrite(): - don't call write() with nbyte > SSIZE_MAX, for avoiding implementation-defined behavior, - handle partial writes, - cope with EINTR and EAGAIN errors. (A busy loop around EAGAIN on a non-blocking file is not great in the general case, but it's good enough for writing out diagnostics before giving up.) Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/utils.c | 39 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/utils.c b/lib/utils.c index bba4b3846e77..7fb16f6402d1 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -25,6 +25,7 @@ #include <ctype.h> #include <errno.h> #include <fcntl.h> +#include <limits.h> #include "minmax.h" @@ -181,6 +182,36 @@ nbd_internal_fork_safe_itoa (long v, char *buf, size_t bufsize) #pragma GCC diagnostic ignored "-Wunused-result" #endif +/* "Best effort" function for writing out a buffer to a file descriptor. + * Chunking with SSIZE_MAX (for avoiding implementation-defined behavior), + * partial writes, and EINTR and EAGAIN failures are handled internally. No + * value is returned; only use this for writing diagnostic data on error paths, + * when giving up on a higher-level action anyway. Note that this function is + * supposed to remain async-signal-safe. + */ +static void +xwrite (int fd, const void *buf, size_t count) +{ + const unsigned char *pos; + size_t left; + + pos = buf; + left = count; + while (left > 0) { + ssize_t written; + + do + written = write (fd, pos, MIN (left, SSIZE_MAX)); + while (written == -1 && (errno == EINTR || errno == EAGAIN)); + + if (written == -1) + return; + + pos += written; + left -= written; + }; +} + /* Fork-safe version of perror. ONLY use this after fork and before * exec, the rest of the time use set_error(). */ @@ -191,8 +222,8 @@ nbd_internal_fork_safe_perror (const char *s) const char *m = NULL; char buf[32]; - write (2, s, strlen (s)); - write (2, ": ", 2); + xwrite (2, s, strlen (s)); + xwrite (2, ": ", 2); #ifdef HAVE_STRERRORDESC_NP m = strerrordesc_np (errno); #else @@ -202,8 +233,8 @@ nbd_internal_fork_safe_perror (const char *s) #endif if (!m) m = nbd_internal_fork_safe_itoa ((long) errno, buf, sizeof buf); - write (2, m, strlen (m)); - write (2, "\n", 1); + xwrite (2, m, strlen (m)); + xwrite (2, "\n", 1); /* Restore original errno in case it was disturbed by the system * calls above.
On Wed, Feb 15, 2023 at 03:11:35PM +0100, Laszlo Ersek wrote:
While nbd_internal_fork_safe_perror() must indeed call write(), and arguably justifiedly ignores the return value of write(), we can still make the write operations slightly more robust. Let's do that by introducing xwrite():
- don't call write() with nbyte > SSIZE_MAX, for avoiding implementation-defined behavior,
- handle partial writes,
- cope with EINTR and EAGAIN errors. (A busy loop around EAGAIN on a non-blocking file is not great in the general case, but it's good enough for writing out diagnostics before giving up.)
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/utils.c | 39 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Add an assert() variant that we may call between fork() and exec*(). Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U13 lib/internal.h | 13 ++++++++++++ lib/utils.c | 22 ++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/internal.h b/lib/internal.h index 0b5f793011b8..9c9021ed366e 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -517,14 +517,27 @@ extern char *nbd_internal_printable_string (const char *str) extern char *nbd_internal_printable_string_list (char **list) LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free); /* These are wrappers around socket(2) and socketpair(2). They * always set SOCK_CLOEXEC. nbd_internal_socket can set SOCK_NONBLOCK * according to the nonblock parameter. */ extern int nbd_internal_socket (int domain, int type, int protocol, bool nonblock); extern int nbd_internal_socketpair (int domain, int type, int protocol, int *fds) LIBNBD_ATTRIBUTE_NONNULL (4); +extern void nbd_internal_fork_safe_assert (int result, const char *file, + long line, const char *func, + const char *assertion) + LIBNBD_ATTRIBUTE_NONNULL (2, 4, 5); + +#ifdef NDEBUG +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) ((void)0) +#else +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) \ + (nbd_internal_fork_safe_assert ((expression) != 0, __FILE__, __LINE__, \ + __func__, #expression)) +#endif + #endif /* LIBNBD_INTERNAL_H */ diff --git a/lib/utils.c b/lib/utils.c index 7fb16f6402d1..5dd00f82e073 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -431,13 +431,35 @@ nbd_internal_socketpair (int domain, int type, int protocol, int *fds) if (ret == 0) { for (i = 0; i < 2; i++) { if (fcntl (fds[i], F_SETFD, FD_CLOEXEC) == -1) { close (fds[0]); close (fds[1]); return -1; } } } #endif return ret; } + +void +nbd_internal_fork_safe_assert (int result, const char *file, long line, + const char *func, const char *assertion) +{ + const char *line_out; + char line_buf[32]; + + if (result) + return; + + xwrite (STDERR_FILENO, file, strlen (file)); + xwrite (STDERR_FILENO, ":", 1); + line_out = nbd_internal_fork_safe_itoa (line, line_buf, sizeof line_buf); + xwrite (STDERR_FILENO, line_out, strlen (line_out)); + xwrite (STDERR_FILENO, ": ", 2); + xwrite (STDERR_FILENO, func, strlen (func)); + xwrite (STDERR_FILENO, ": Assertion `", 13); + xwrite (STDERR_FILENO, assertion, strlen (assertion)); + xwrite (STDERR_FILENO, "' failed.\n", 10); + abort (); +}
On Wed, Feb 15, 2023 at 03:11:36PM +0100, Laszlo Ersek wrote:
Add an assert() variant that we may call between fork() and exec*().
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
+++ b/lib/internal.h
+ +#ifdef NDEBUG +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) ((void)0)
May be affected by outcome of side discussion on 01/29 about whether we want space after cast.
+#else +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) \ + (nbd_internal_fork_safe_assert ((expression) != 0, __FILE__, __LINE__, \ + __func__, #expression)) +#endif + #endif /* LIBNBD_INTERNAL_H */
+++ b/lib/utils.c @@ -431,13 +431,35 @@ nbd_internal_socketpair (int domain, int type, int protocol, int *fds) if (ret == 0) { for (i = 0; i < 2; i++) { if (fcntl (fds[i], F_SETFD, FD_CLOEXEC) == -1) { close (fds[0]); close (fds[1]); return -1; } } } #endif
return ret; } + +void +nbd_internal_fork_safe_assert (int result, const char *file, long line, + const char *func, const char *assertion) +{ + const char *line_out; + char line_buf[32]; + + if (result) + return;
Since no code should ever directly call nbd_internal_fork_safe_assert(0,...), but instead go through our macro that has already filtered on expression's value, this conditional is technically dead code; but I'm okay whether it stays in or gets removed.
+ + xwrite (STDERR_FILENO, file, strlen (file)); + xwrite (STDERR_FILENO, ":", 1);
Presumably, if our first best-effort xwrite() fails to produce full output, all later xwrite() will hopefully hit the same error condition so that we aren't producing even more-mangled output where later syscalls succeed despite missing context earlier in the overall output. If it were something we truly wanted to worry about, the solution would be pre-loading the entire message into a single buffer, then calling xwrite() just once - but that's far more effort for something we don't anticipate hitting in normal usage anyways. I'm happy if you ignore this whole paragraph of mine.
+ line_out = nbd_internal_fork_safe_itoa (line, line_buf, sizeof line_buf); + xwrite (STDERR_FILENO, line_out, strlen (line_out)); + xwrite (STDERR_FILENO, ": ", 2); + xwrite (STDERR_FILENO, func, strlen (func)); + xwrite (STDERR_FILENO, ": Assertion `", 13); + xwrite (STDERR_FILENO, assertion, strlen (assertion)); + xwrite (STDERR_FILENO, "' failed.\n", 10);
These days, the quoting style `' seems to be disappearing in favor of ''. But a quick test showed me that at least glibc 2.36 is still producing the message with the quotes as you have spelled it here.
+ abort ();
Huh. I checked the source to glibc's assert.c, where it includes a comment about having to work cleanly even if a second assertion ends up being raised from within a SIGABRT handler active during the first attempt to abort(). But POSIX says that abort() shall attempt to override any SIGABRT handler in place, so I wonder if glibc's implementation is a bit too defensive. At any rate, your implementation looks reasonable, and more to the point, satisfies your desire of being async-signal-safe and thus appropriate between fork() and exec(). Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 21:57, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:36PM +0100, Laszlo Ersek wrote:
Add an assert() variant that we may call between fork() and exec*().
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
+++ b/lib/internal.h
+ +#ifdef NDEBUG +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) ((void)0)
May be affected by outcome of side discussion on 01/29 about whether we want space after cast.
+#else +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) \ + (nbd_internal_fork_safe_assert ((expression) != 0, __FILE__, __LINE__, \ + __func__, #expression)) +#endif + #endif /* LIBNBD_INTERNAL_H */
+++ b/lib/utils.c @@ -431,13 +431,35 @@ nbd_internal_socketpair (int domain, int type, int protocol, int *fds) if (ret == 0) { for (i = 0; i < 2; i++) { if (fcntl (fds[i], F_SETFD, FD_CLOEXEC) == -1) { close (fds[0]); close (fds[1]); return -1; } } } #endif
return ret; } + +void +nbd_internal_fork_safe_assert (int result, const char *file, long line, + const char *func, const char *assertion) +{ + const char *line_out; + char line_buf[32]; + + if (result) + return;
Since no code should ever directly call nbd_internal_fork_safe_assert(0,...), but instead go through our macro that has already filtered on expression's value,
I either don't understand, or I disagree. With NDEBUG not defined, the NBD_INTERNAL_FORK_SAFE_ASSERT macro is always expanded to a call to nbd_internal_fork_safe_assert(). And the expression to check is evaluated in the first argument of that function call: nbd_internal_fork_safe_assert ((expression) != 0, ... So I think the early return is definitely necessary here.
this conditional is technically dead code; but I'm okay whether it stays in or gets removed.
+ + xwrite (STDERR_FILENO, file, strlen (file)); + xwrite (STDERR_FILENO, ":", 1);
Presumably, if our first best-effort xwrite() fails to produce full output, all later xwrite() will hopefully hit the same error condition so that we aren't producing even more-mangled output where later syscalls succeed despite missing context earlier in the overall output. If it were something we truly wanted to worry about, the solution would be pre-loading the entire message into a single buffer, then calling xwrite() just once - but that's far more effort for something we don't anticipate hitting in normal usage anyways. I'm happy if you ignore this whole paragraph of mine.
Any single buffer presents the problem of sizing the buffer appropriately, which we can't do in this context :)
+ line_out = nbd_internal_fork_safe_itoa (line, line_buf, sizeof line_buf); + xwrite (STDERR_FILENO, line_out, strlen (line_out)); + xwrite (STDERR_FILENO, ": ", 2); + xwrite (STDERR_FILENO, func, strlen (func)); + xwrite (STDERR_FILENO, ": Assertion `", 13); + xwrite (STDERR_FILENO, assertion, strlen (assertion)); + xwrite (STDERR_FILENO, "' failed.\n", 10);
These days, the quoting style `' seems to be disappearing in favor of ''. But a quick test showed me that at least glibc 2.36 is still producing the message with the quotes as you have spelled it here.
Personally I dislike `', and like '' (and ""), but I specifically modeled the message on the standard assert() failure message on RHEL9.
+ abort ();
Huh. I checked the source to glibc's assert.c, where it includes a comment about having to work cleanly even if a second assertion ends up being raised from within a SIGABRT handler active during the first attempt to abort(). But POSIX says that abort() shall attempt to override any SIGABRT handler in place, so I wonder if glibc's implementation is a bit too defensive.
What I did was, I checked POSIX on assert(), and the spec there simply says that assert() calls abort(): "When it is executed, if expression (which shall have a scalar type) is false (that is, compares equal to 0), assert() shall write information about the particular call that failed on stderr and shall call abort()."
At any rate, your implementation looks reasonable, and more to the point, satisfies your desire of being async-signal-safe and thus appropriate between fork() and exec().
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! Laszlo
On Mon, Feb 20, 2023 at 07:21:05PM +0100, Laszlo Ersek wrote:
On 2/15/23 21:57, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:36PM +0100, Laszlo Ersek wrote:
Add an assert() variant that we may call between fork() and exec*().
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
+++ b/lib/internal.h
+ +#ifdef NDEBUG +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) ((void)0)
May be affected by outcome of side discussion on 01/29 about whether we want space after cast.
+#else +#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) \ + (nbd_internal_fork_safe_assert ((expression) != 0, __FILE__, __LINE__, \ + __func__, #expression)) +#endif + #endif /* LIBNBD_INTERNAL_H */
+++ b/lib/utils.c @@ -431,13 +431,35 @@ nbd_internal_socketpair (int domain, int type, int protocol, int *fds) if (ret == 0) { for (i = 0; i < 2; i++) { if (fcntl (fds[i], F_SETFD, FD_CLOEXEC) == -1) { close (fds[0]); close (fds[1]); return -1; } } } #endif
return ret; } + +void +nbd_internal_fork_safe_assert (int result, const char *file, long line, + const char *func, const char *assertion) +{ + const char *line_out; + char line_buf[32]; + + if (result) + return;
Since no code should ever directly call nbd_internal_fork_safe_assert(0,...), but instead go through our macro that has already filtered on expression's value,
I either don't understand, or I disagree. With NDEBUG not defined, the NBD_INTERNAL_FORK_SAFE_ASSERT macro is always expanded to a call to nbd_internal_fork_safe_assert(). And the expression to check is evaluated in the first argument of that function call:
nbd_internal_fork_safe_assert ((expression) != 0, ...
So I think the early return is definitely necessary here.
Oh, I was confused. I went back and re-read your macro; I guess I was thinking it was something like: do if (!(expression)) nbd_internal_fork_safe_assert (args); while (0) where the function is only called on failure, but you instead wrote it as: nbd_internal_fork_safe_assert ((expression) != 0), args) So I stand corrected - we do need the early exit here, because we are unconditionally calling the function (when assertions are enabled) at least in the current spelling of the macro. ...
At any rate, your implementation looks reasonable, and more to the point, satisfies your desire of being async-signal-safe and thus appropriate between fork() and exec().
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks!
My R-b still stands, and I think you cleared up my confusion without having to change any code on your part. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/20/23 19:21, Laszlo Ersek wrote:
On 2/15/23 21:57, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:36PM +0100, Laszlo Ersek wrote:
+ + xwrite (STDERR_FILENO, file, strlen (file)); + xwrite (STDERR_FILENO, ":", 1);
Presumably, if our first best-effort xwrite() fails to produce full output, all later xwrite() will hopefully hit the same error condition so that we aren't producing even more-mangled output where later syscalls succeed despite missing context earlier in the overall output. If it were something we truly wanted to worry about, the solution would be pre-loading the entire message into a single buffer, then calling xwrite() just once - but that's far more effort for something we don't anticipate hitting in normal usage anyways. I'm happy if you ignore this whole paragraph of mine.
Any single buffer presents the problem of sizing the buffer appropriately, which we can't do in this context :)
Actually, we *can* do better: https://pubs.opengroup.org/onlinepubs/9699919799/functions/writev.html What we're doing here is the textbook use case for scatter-gather (well, in this case, gather). It's strange that it has taken me one night to realize this (it occurred to me before falling asleep last night), given that I heavily used scatter-gather in other, not-so-old, code. (Namely the edk2 virtiofs driver.) I'll attempt to replace xwrite() with xwritev(). Laszlo
On 2/21/23 07:33, Laszlo Ersek wrote:
On 2/20/23 19:21, Laszlo Ersek wrote:
On 2/15/23 21:57, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:36PM +0100, Laszlo Ersek wrote:
+ + xwrite (STDERR_FILENO, file, strlen (file)); + xwrite (STDERR_FILENO, ":", 1);
Presumably, if our first best-effort xwrite() fails to produce full output, all later xwrite() will hopefully hit the same error condition so that we aren't producing even more-mangled output where later syscalls succeed despite missing context earlier in the overall output. If it were something we truly wanted to worry about, the solution would be pre-loading the entire message into a single buffer, then calling xwrite() just once - but that's far more effort for something we don't anticipate hitting in normal usage anyways. I'm happy if you ignore this whole paragraph of mine.
Any single buffer presents the problem of sizing the buffer appropriately, which we can't do in this context :)
Actually, we *can* do better:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/writev.html
What we're doing here is the textbook use case for scatter-gather (well, in this case, gather). It's strange that it has taken me one night to realize this (it occurred to me before falling asleep last night), given that I heavily used scatter-gather in other, not-so-old, code. (Namely the edk2 virtiofs driver.)
I'll attempt to replace xwrite() with xwritev().
No, I won't -- writev() is not required to be async-signal-safe, so it's not good enough for NBD_INTERNAL_FORK_SAFE_ASSERT(). Laszlo
On Fri, Feb 24, 2023 at 01:57:37PM +0100, Laszlo Ersek wrote:
On 2/21/23 07:33, Laszlo Ersek wrote:
On 2/20/23 19:21, Laszlo Ersek wrote:
On 2/15/23 21:57, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:36PM +0100, Laszlo Ersek wrote:
+ + xwrite (STDERR_FILENO, file, strlen (file)); + xwrite (STDERR_FILENO, ":", 1);
Presumably, if our first best-effort xwrite() fails to produce full output, all later xwrite() will hopefully hit the same error condition so that we aren't producing even more-mangled output where later syscalls succeed despite missing context earlier in the overall output. If it were something we truly wanted to worry about, the solution would be pre-loading the entire message into a single buffer, then calling xwrite() just once - but that's far more effort for something we don't anticipate hitting in normal usage anyways. I'm happy if you ignore this whole paragraph of mine.
Any single buffer presents the problem of sizing the buffer appropriately, which we can't do in this context :)
Actually, we *can* do better:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/writev.html
What we're doing here is the textbook use case for scatter-gather (well, in this case, gather). It's strange that it has taken me one night to realize this (it occurred to me before falling asleep last night), given that I heavily used scatter-gather in other, not-so-old, code. (Namely the edk2 virtiofs driver.)
I'll attempt to replace xwrite() with xwritev().
No, I won't -- writev() is not required to be async-signal-safe, so it's not good enough for NBD_INTERNAL_FORK_SAFE_ASSERT().
Actually, that's a bug in POSIX; writev() is async-safe on all known platforms: https://www.austingroupbugs.net/view.php?id=1455 In part because writev() is documented as being "identical to write() except for..." where the exception does NOT mention async safety, and therefore writev() must be async safe if write() is. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Don't try to test async-signal-safety, only that NBD_INTERNAL_FORK_SAFE_ASSERT() works similarly to assert(): - it prints diagnostics to stderr, - it calls abort(). Some unfortunate gymnastics are necessary to avoid littering the system with unwanted core dumps. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- configure.ac | 5 ++ lib/test-fork-safe-assert.c | 63 ++++++++++++++++++++ lib/Makefile.am | 38 ++++++++++-- lib/test-fork-safe-assert.sh | 31 ++++++++++ .gitignore | 2 + 5 files changed, 135 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 893e82ac6fea..3549188ec5fb 100644 --- a/configure.ac +++ b/configure.ac @@ -129,11 +129,16 @@ dnl Check for various libc functions, all optional. dnl dnl posix_fadvise helps to optimise linear reads and writes. dnl +dnl When /proc/sys/kernel/core_pattern starts with a pipe (|) symbol, Linux +dnl ignores "ulimit -c" and (equivalent) setrlimit(RLIMIT_CORE) actions, for +dnl disabling core dumping. Only prctl() can be used then, for that purpose. +dnl dnl strerrordesc_np (glibc only) is preferred over sys_errlist: dnl https://lists.fedoraproject.org/archives/list/glibc@lists.fedoraproject.org/... AC_CHECK_FUNCS([\ posix_fadvise \ posix_memalign \ + prctl \ strerrordesc_np \ valloc]) diff --git a/lib/test-fork-safe-assert.c b/lib/test-fork-safe-assert.c new file mode 100644 index 000000000000..131cbaa0abca --- /dev/null +++ b/lib/test-fork-safe-assert.c @@ -0,0 +1,63 @@ +/* nbd client library in userspace + * Copyright (C) 2013-2023 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#ifdef HAVE_PRCTL +#include <sys/prctl.h> +#endif +#include <sys/resource.h> + +#undef NDEBUG + +#include "internal.h" + +#define TRUE 1 +#define FALSE 0 + +int +main (void) +{ + struct rlimit rlimit; + + /* The standard approach for disabling core dumping. Has no effect on Linux if + * /proc/sys/kernel/core_pattern starts with a pipe (|) symbol. + */ + if (getrlimit (RLIMIT_CORE, &rlimit) == -1) { + perror ("getrlimit"); + return EXIT_FAILURE; + } + rlimit.rlim_cur = 0; + if (setrlimit (RLIMIT_CORE, &rlimit) == -1) { + perror ("setrlimit"); + return EXIT_FAILURE; + } + +#ifdef HAVE_PRCTL + if (prctl (PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) { + perror ("prctl"); + return EXIT_FAILURE; + } +#endif + + NBD_INTERNAL_FORK_SAFE_ASSERT (TRUE); + NBD_INTERNAL_FORK_SAFE_ASSERT (FALSE); + return 0; +} diff --git a/lib/Makefile.am b/lib/Makefile.am index ece50770326e..1f6501ceb49a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -25,15 +25,30 @@ generator_built = \ unlocked.h \ $(NULL) +CLEANFILES += \ + test-fork-safe-assert.err \ + $(NULL) + EXTRA_DIST = \ $(generator_built) \ libnbd.syms \ + test-fork-safe-assert.sh \ $(NULL) lib_LTLIBRARIES = libnbd.la BUILT_SOURCES = $(generator_built) +AM_CPPFLAGS = \ + -I$(top_srcdir)/include \ + -I$(top_srcdir)/common/include \ + -I$(top_srcdir)/common/utils \ + $(NULL) + +AM_CFLAGS = \ + $(WARNINGS_CFLAGS) \ + $(NULL) + libnbd_la_SOURCES = \ aio.c \ api.c \ @@ -60,13 +75,11 @@ libnbd_la_SOURCES = \ utils.c \ $(NULL) libnbd_la_CPPFLAGS = \ - -I$(top_srcdir)/include \ - -I$(top_srcdir)/common/include \ - -I$(top_srcdir)/common/utils \ + $(AM_CPPFLAGS) \ -Dsysconfdir=\"$(sysconfdir)\" \ $(NULL) libnbd_la_CFLAGS = \ - $(WARNINGS_CFLAGS) \ + $(AM_CFLAGS) \ $(PTHREAD_CFLAGS) \ $(GNUTLS_CFLAGS) \ $(LIBXML2_CFLAGS) \ @@ -86,3 +99,20 @@ libnbd_la_LDFLAGS = \ pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libnbd.pc + +# Unit tests. + +TESTS = \ + test-fork-safe-assert.sh \ + $(NULL) + +check_PROGRAMS = \ + test-fork-safe-assert \ + $(NULL) + +test_fork_safe_assert_SOURCES = \ + $(top_srcdir)/common/utils/vector.c \ + errors.c \ + test-fork-safe-assert.c \ + utils.c \ + $(NULL) diff --git a/lib/test-fork-safe-assert.sh b/lib/test-fork-safe-assert.sh new file mode 100755 index 000000000000..88fc553b484d --- /dev/null +++ b/lib/test-fork-safe-assert.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# nbd client library in userspace +# Copyright (C) 2013-2023 Red Hat Inc. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +set +e + +./test-fork-safe-assert 2>test-fork-safe-assert.err +exit_status=$? + +set -e + +test $exit_status -gt 128 +signal_name=$(kill -l $exit_status) +test "x$signal_name" = xABRT || test "x$signal_name" = xSIGABRT + +ptrn="^test-fork-safe-assert\\.c:[0-9]+: main: Assertion \`FALSE' failed\\.\$" +grep -E -q -- "$ptrn" test-fork-safe-assert.err diff --git a/.gitignore b/.gitignore index f42737131d8c..cd27a4ed7557 100644 --- a/.gitignore +++ b/.gitignore @@ -124,6 +124,8 @@ Makefile.in /lib/states-run.c /lib/states.c /lib/states.h +/lib/test-fork-safe-assert +/lib/test-fork-safe-assert.err /lib/unlocked.h /libtool /ltmain.sh
On Wed, Feb 15, 2023 at 03:11:37PM +0100, Laszlo Ersek wrote:
Don't try to test async-signal-safety, only that NBD_INTERNAL_FORK_SAFE_ASSERT() works similarly to assert():
- it prints diagnostics to stderr,
- it calls abort().
Some unfortunate gymnastics are necessary to avoid littering the system with unwanted core dumps.
Wow - impressive work for a unit test.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- configure.ac | 5 ++ lib/test-fork-safe-assert.c | 63 ++++++++++++++++++++ lib/Makefile.am | 38 ++++++++++-- lib/test-fork-safe-assert.sh | 31 ++++++++++ .gitignore | 2 + 5 files changed, 135 insertions(+), 4 deletions(-)
+++ b/lib/test-fork-safe-assert.c
+ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#ifdef HAVE_PRCTL +#include <sys/prctl.h> +#endif +#include <sys/resource.h> + +#undef NDEBUG + +#include "internal.h" + +#define TRUE 1 +#define FALSE 0
Any reason for defining these, other than to test that our macro properly stringifies them into the resulting assertion text? /me reads ahead Yep, your .sh script does check that a literal "`FALSE'" is part of the expected output string. A comment here might be worthwhile?
+ +int +main (void) +{ + struct rlimit rlimit; + + /* The standard approach for disabling core dumping. Has no effect on Linux if + * /proc/sys/kernel/core_pattern starts with a pipe (|) symbol. + */ + if (getrlimit (RLIMIT_CORE, &rlimit) == -1) { + perror ("getrlimit"); + return EXIT_FAILURE; + } + rlimit.rlim_cur = 0; + if (setrlimit (RLIMIT_CORE, &rlimit) == -1) { + perror ("setrlimit"); + return EXIT_FAILURE; + } + +#ifdef HAVE_PRCTL + if (prctl (PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) { + perror ("prctl"); + return EXIT_FAILURE;
Would 'return 77' (ie 'skipped test', in automake terminology) be better if we can't pre-set the environment to our liking? [1]
+ } +#endif + + NBD_INTERNAL_FORK_SAFE_ASSERT (TRUE); + NBD_INTERNAL_FORK_SAFE_ASSERT (FALSE); + return 0;
+++ b/lib/test-fork-safe-assert.sh @@ -0,0 +1,31 @@
+ +set +e + +./test-fork-safe-assert 2>test-fork-safe-assert.err +exit_status=$? + +set -e + +test $exit_status -gt 128
[1] Hmm - if you do return 77 when we can't avoid the core dump, this would need to forward that on.
+signal_name=$(kill -l $exit_status) +test "x$signal_name" = xABRT || test "x$signal_name" = xSIGABRT + +ptrn="^test-fork-safe-assert\\.c:[0-9]+: main: Assertion \`FALSE' failed\\.\$"
Would it be any easier writing ptrn with '' instead of "" shell quoting? Do we also want to test that the pattern "`TRUE'" is NOT present in the output file (that is, our passing assertion does not generate unexpected output)? Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 22:10, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:37PM +0100, Laszlo Ersek wrote:
Don't try to test async-signal-safety, only that NBD_INTERNAL_FORK_SAFE_ASSERT() works similarly to assert():
- it prints diagnostics to stderr,
- it calls abort().
Some unfortunate gymnastics are necessary to avoid littering the system with unwanted core dumps.
Wow - impressive work for a unit test.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- configure.ac | 5 ++ lib/test-fork-safe-assert.c | 63 ++++++++++++++++++++ lib/Makefile.am | 38 ++++++++++-- lib/test-fork-safe-assert.sh | 31 ++++++++++ .gitignore | 2 + 5 files changed, 135 insertions(+), 4 deletions(-)
+++ b/lib/test-fork-safe-assert.c
+ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#ifdef HAVE_PRCTL +#include <sys/prctl.h> +#endif +#include <sys/resource.h> + +#undef NDEBUG + +#include "internal.h" + +#define TRUE 1 +#define FALSE 0
Any reason for defining these, other than to test that our macro properly stringifies them into the resulting assertion text?
/me reads ahead
Yep, your .sh script does check that a literal "`FALSE'" is part of the expected output string. A comment here might be worthwhile?
Right, I'll add a comment.
+ +int +main (void) +{ + struct rlimit rlimit; + + /* The standard approach for disabling core dumping. Has no effect on Linux if + * /proc/sys/kernel/core_pattern starts with a pipe (|) symbol. + */ + if (getrlimit (RLIMIT_CORE, &rlimit) == -1) { + perror ("getrlimit"); + return EXIT_FAILURE; + } + rlimit.rlim_cur = 0; + if (setrlimit (RLIMIT_CORE, &rlimit) == -1) { + perror ("setrlimit"); + return EXIT_FAILURE; + } + +#ifdef HAVE_PRCTL + if (prctl (PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) { + perror ("prctl"); + return EXIT_FAILURE;
Would 'return 77' (ie 'skipped test', in automake terminology) be better if we can't pre-set the environment to our liking? [1]
My thinking went like this: on Linux, we'll surely have prctl(), so we expect it to work. On OpenBSD and FreeBSD (the other two platforms we support), we might not have prctl(), but on those platforms, I actually expect setrlimit() to suffice for disabling core dumps. So I don't see a case where we should skip the test.
+ } +#endif + + NBD_INTERNAL_FORK_SAFE_ASSERT (TRUE); + NBD_INTERNAL_FORK_SAFE_ASSERT (FALSE); + return 0;
+++ b/lib/test-fork-safe-assert.sh @@ -0,0 +1,31 @@
+ +set +e + +./test-fork-safe-assert 2>test-fork-safe-assert.err +exit_status=$? + +set -e + +test $exit_status -gt 128
[1] Hmm - if you do return 77 when we can't avoid the core dump, this would need to forward that on.
+signal_name=$(kill -l $exit_status) +test "x$signal_name" = xABRT || test "x$signal_name" = xSIGABRT + +ptrn="^test-fork-safe-assert\\.c:[0-9]+: main: Assertion \`FALSE' failed\\.\$"
Would it be any easier writing ptrn with '' instead of "" shell quoting?
I considered that, but it doesn't do the whole job: there's an apostrophe in the expected output, so minimally I'd have to write ptrn='that'\''s still messy' and then I figured double quotes are just more "consequent".
Do we also want to test that the pattern "`TRUE'" is NOT present in the output file (that is, our passing assertion does not generate unexpected output)?
For TRUE to be present together with FALSE, the NBD_INTERNAL_FORK_SAFE_ASSERT (TRUE) macro invocation in the C code would have to produce output and not abort() at the same time. I thought that that didn't need checking (seemed too outlandish), but yes, I can add a "grep -v" too.
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! Laszlo
execvp() [01] is powerful: - it performs PATH search [02] if necessary, - it falls back to executing the file with the shell as a shell script in case the underlying execv() or execve() fails with ENOEXEC. However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be. Introduce three APIs for safely emulating execvp() with execve(): - nbd_internal_execvpe_init(): to be called in the parent process, before fork(). Allocate and format candidate pathnames for execution with execve(), based on PATH. Allocate a buffer for the longer command line that the shell script fallback needs. - nbd_internal_fork_safe_execvpe(): to be called in the child process. Try the candidates formatted previously in sequence with execve(), as long as execve() fails with errors related to pathname resolution / inode-level file type / execution permission. Stop iterating on fatal errors; if the fatal error is ENOEXEC, activate the shell script fallback. As a small added feature, take the environment from an explicit parameter (effectively imitating the GNU-specific execvpe() function -- cf. commit dc64ac5cdd0b, "states: Use execvp instead of execvpe", 2019-11-15). - nbd_internal_execvpe_uninit(): to be called in the parent process, after fork(). Release the resources allocated with nbd_internal_execvpe_init(). The main idea with the above distribution of responsibilities is that any pre-scanning of PATH -- which was suggested by Eric Blake -- should not include activities that are performed by execve() anyway. I've considered and abandoned two approaches: - During scanning (i.e., in nbd_internal_execvpe_init()), check each candidate with access(X_OK) [05]. I dropped this because the informative APPLICATION USAGE section of the same specification [06] advises against it, saying "[a]n application should instead attempt the action itself and handle the [EACCES] error that occurs if the file is not accessible". - During scanning, open each candidate with O_EXEC [07] until one open() succeeds. Save the sole file descriptor for nbd_internal_fork_safe_execvpe(). In the latter, call fexecve() [08], which is free of all error conditions that necessitate iteration over candidates. Still implement the ENOEXEC (shell script) fallback. I dropped this because (a) fexecve() is a quite recent interface (highlighted by Eric); (b) for an application to open a file for execution with fexecve(), it's more robust to pass O_EXEC to open() than not to pass it, per APPLICATION USAGE [09], but on Linux/glibc, O_EXEC does not seem supported, only O_PATH does [10]. Thus the chosen approach -- pre-generate filenames -- contains a small TOCTTOU race (highlighted by Eric) after all, but it should be harmless. Implementation-defined details: - If PATH searching is necessary, but PATH is absent [01], then fall back to confstr(_CS_PATH) [11], similarly to Linux/glibc execvpe() [12]. If PATH is set but empty ("set to null") [02], or PATH is unset and confstr(_CS_PATH) fails or returns no information or returns the empty string, we fail the initial scanning (!) with ENOENT. This is consistent with bash's behavior on Linux/glibc: $ PATH= ls bash: ls: No such file or directory Details chosen for unspecified behavior: - Hardwire "/bin/sh" as the shell's pathname [01]. [01] https://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html [02] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag... [03] https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta... [04] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html [05] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html [06] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html#tag_1... [07] https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html [08] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html [09] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html#tag_... [10] https://man7.org/linux/man-pages/man2/open.2.html [11] https://pubs.opengroup.org/onlinepubs/9699919799/functions/confstr.html [12] https://man7.org/linux/man-pages/man3/execvpe.3.html Suggested-by: Eric Blake <eblake@redhat.com> Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 22 ++ lib/utils.c | 351 ++++++++++++++++++++ 2 files changed, 373 insertions(+) diff --git a/lib/internal.h b/lib/internal.h index 9c9021ed366e..8c4e32013e40 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -368,6 +368,18 @@ struct command { uint32_t error; /* Local errno value */ }; +struct execvpe { + string_vector pathnames; + + /* Note: "const_string_vector" is not a good type for "sh_argv" below. Even if + * we reserved enough space in a "const_string_vector", + * const_string_vector_append() would still not be async-signal-safe, due to + * the underlying const_string_vector_insert() calling assert(). + */ + char **sh_argv; + size_t num_sh_args; +}; + /* Test if a callback is "null" or not, and set it to null. */ #define CALLBACK_IS_NULL(cb) ((cb).callback == NULL && (cb).free == NULL) #define CALLBACK_IS_NOT_NULL(cb) (! CALLBACK_IS_NULL ((cb))) @@ -540,4 +552,14 @@ extern void nbd_internal_fork_safe_assert (int result, const char *file, __func__, #expression)) #endif +extern int nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args) + LIBNBD_ATTRIBUTE_NONNULL (1, 2); +extern void nbd_internal_execvpe_uninit (struct execvpe *ctx) + LIBNBD_ATTRIBUTE_NONNULL (1); +extern int nbd_internal_fork_safe_execvpe (struct execvpe *ctx, + const string_vector *argv, + char * const *envp) + LIBNBD_ATTRIBUTE_NONNULL (1, 2, 3); + #endif /* LIBNBD_INTERNAL_H */ diff --git a/lib/utils.c b/lib/utils.c index 5dd00f82e073..9a96ed6ed674 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -28,6 +28,7 @@ #include <limits.h> #include "minmax.h" +#include "checked-overflow.h" #include "internal.h" @@ -463,3 +464,353 @@ nbd_internal_fork_safe_assert (int result, const char *file, long line, xwrite (STDERR_FILENO, "' failed.\n", 10); abort (); } + +/* Returns the value of the PATH environment variable -- falling back to + * confstr(_CS_PATH) if PATH is absent -- as a dynamically allocated string. On + * failure, sets "errno" and returns NULL. + */ +static char * +get_path (void) + LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free) +{ + char *path; + bool env_path_found; + size_t path_size, path_size2; + + /* FIXME: lock the environment. */ + path = getenv ("PATH"); + if ((env_path_found = (path != NULL))) + path = strdup (path); + /* FIXME: unlock the environment. */ + + if (env_path_found) { + /* This handles out-of-memory as well. */ + return path; + } + + errno = 0; + path_size = confstr (_CS_PATH, NULL, 0); + if (path_size == 0) { + /* If _CS_PATH does not have a configuration-defined value, just store + * ENOENT to "errno". + */ + if (errno == 0) + errno = ENOENT; + + return NULL; + } + + path = malloc (path_size); + if (path == NULL) + return NULL; + + path_size2 = confstr (_CS_PATH, path, path_size); + assert (path_size2 == path_size); + return path; +} + +/* nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe() together + * present an execvp() alternative that is async-signal-safe. + * + * nbd_internal_execvpe_init() may only be called before fork(), for filling in + * the caller-allocated, uninitialized "ctx" structure. If + * nbd_internal_execvpe_init() succeeds, then fork() may be called. + * Subsequently, in the child process, nbd_internal_fork_safe_execvpe() may be + * called with the inherited "ctx" structure, while in the parent process, + * nbd_internal_execvpe_uninit() must be called to uninitialize (evacuate) the + * "ctx" structure. + * + * On failure, "ctx" will not have been modified, "errno" is set, and -1 is + * returned. Failures include: + * + * - Errors forwarded from underlying functions such as strdup(), confstr(), + * malloc(), string_vector_append(). + * + * - ENOENT: "file" is an empty string. + * + * - ENOENT: "file" does not contain a <slash> "/" character, the PATH + * environment variable is not set, and confstr() doesn't associate a + * configuration-defined value with _CS_PATH. + * + * - ENOENT: "file" does not contain a <slash> "/" character, and: (a) the PATH + * environment variable is set to the empty string, or (b) PATH is not + * set, and confstr() outputs the empty string for _CS_PATH. + * + * - EOVERFLOW: the sizes or counts of necessary objects could not be expressed. + * + * - EINVAL: "num_args" is less than 2. + * + * On success, the "ctx" structure will have been filled in, and 0 is returned. + * + * - "pathnames" member: + * + * - All strings pointed-to by elements of the "pathnames" string_vector + * member are owned by "pathnames". + * + * - If "file" contains a <slash> "/" character, then the sole entry in + * "pathnames" is a copy of "file". + * + * - If "file" does not contain a <slash> "/" character: + * + * Let "system path" be defined as the value of the PATH environment + * variable, if the latter exists, and as the value output by confstr() for + * _CS_PATH otherwise. Per the ENOENT specifications above, "system path" is + * a non-empty string. Let "system path" further be of the form + * + * <prefix_0> [n = 1] + * + * or + * + * <prefix_0>:<prefix_1>:...:<prefix_(n-1)> [n >= 2] + * + * where for each 0 <= i < n, <prefix_i> does not contain the <colon> ":" + * character. In the (n = 1) case, <prefix_0> is never empty (see ENOENT + * above), while in the (n >= 2) case, any individual <prefix_i> may or may + * not be empty. + * + * The "pathnames" string_vector member has n elements; for each 0 <= i < n, + * element i is of the form + * + * suffix(curdir(<prefix_i>))file + * + * where + * + * curdir(x) := "." if x = "" + * curdir(x) := x otherwise + * + * and + * + * suffix(x) := x if "x" ends with a <slash> "/" + * suffix(x) := x "/" otherwise + * + * This transformation implements the POSIX XBD / PATH environment variable + * semantics, creating candidate pathnames for execution by + * nbd_internal_fork_safe_execvpe(). nbd_internal_fork_safe_execvpe() will + * iterate over the candidate pathnames with execve() until execve() + * succeeds, or fails with an error that is due to neither pathname + * resolution, nor the candidate not being a regular file, nor the candidate + * lacking execution permission. + * + * - The "sh_argv" array member will have at least (num_args + 1) elements + * allocated, and none populated. + * + * (The minimum value of "num_args" is 2 -- see EINVAL above. According to + * POSIX, "[t]he argument /arg0/ should point to a filename string that is + * associated with the process being started by one of the /exec/ functions", + * plus "num_args" includes the null pointer that terminates the argument + * list.) + * + * This allocation is made in anticipation of execve() failing for a + * particular candidate inside nbd_internal_fork_safe_execvpe() with ENOEXEC + * ("[t]he new process image file has the appropriate access permission but + * has an unrecognized format"). While that failure terminates the iteration, + * the failed call + * + * execve (pathnames[i], + * { argv[0], argv[1], ..., NULL }, // (num_args >= 2) elements + * { envp[0], envp[1], ..., NULL }) + * + * must be repeated as + * + * execve (<shell-path>, + * { argv[0], pathnames[i], // ((num_args + 1) >= 3) + argv[1], ..., NULL }, // elements + * { envp[0], envp[1], ..., NULL }) + * + * for emulating execvp(). The allocation in the "sh_argv" member makes it + * possible just to *link* the original "argv" elements and the "pathnames[i]" + * candidate into the right positions. + * + * (POSIX leaves the shell pathname unspecified; "/bin/sh" should be good + * enough.) + * + * The shell *binary* will see itself being executed under the name "argv[0]", + * will receive "pathnames[i]" as the pathname of the shell *script* to read + * and interpret ("command_file" in POSIX terminology), will expose + * "pathnames[i]" as the positional parameter $0 to the script, and will + * forward "argv[1]" and the rest to the script as positional parameters $1 + * and onward. + */ +int +nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args) +{ + int rc; + char *sys_path; + string_vector pathnames; + char *pathname; + size_t num_sh_args; + char **sh_argv; + size_t sh_argv_bytes; + + rc = -1; + + if (file[0] == '\0') { + errno = ENOENT; + return rc; + } + + /* First phase. */ + sys_path = NULL; + pathnames = (string_vector)empty_vector; + + if (strchr (file, '/') == NULL) { + size_t file_len; + const char *sys_path_element, *scan; + bool finish; + + sys_path = get_path (); + if (sys_path == NULL) + return rc; + + if (sys_path[0] == '\0') { + errno = ENOENT; + goto free_sys_path; + } + + pathname = NULL; + file_len = strlen (file); + sys_path_element = sys_path; + scan = sys_path; + do { + assert (sys_path_element <= scan); + finish = (*scan == '\0'); + if (finish || *scan == ':') { + const char *sys_path_copy_start; + size_t sys_path_copy_size; + size_t sep_copy_size; + size_t pathname_size; + char *p; + + if (scan == sys_path_element) { + sys_path_copy_start = "."; + sys_path_copy_size = 1; + } else { + sys_path_copy_start = sys_path_element; + sys_path_copy_size = scan - sys_path_element; + } + + assert (sys_path_copy_size >= 1); + sep_copy_size = (sys_path_copy_start[sys_path_copy_size - 1] != '/'); + + if (ADD_OVERFLOW (sys_path_copy_size, sep_copy_size, &pathname_size) || + ADD_OVERFLOW (pathname_size, file_len, &pathname_size) || + ADD_OVERFLOW (pathname_size, 1u, &pathname_size)) { + errno = EOVERFLOW; + goto empty_pathnames; + } + + pathname = malloc (pathname_size); + if (pathname == NULL) + goto empty_pathnames; + p = pathname; + + memcpy (p, sys_path_copy_start, sys_path_copy_size); + p += sys_path_copy_size; + + memcpy (p, "/", sep_copy_size); + p += sep_copy_size; + + memcpy (p, file, file_len); + p += file_len; + + *p++ = '\0'; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + + sys_path_element = scan + 1; + } + + ++scan; + } while (!finish); + } else { + pathname = strdup (file); + if (pathname == NULL) + return rc; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + } + + /* Second phase. */ + if (num_args < 2) { + errno = EINVAL; + goto empty_pathnames; + } + if (ADD_OVERFLOW (num_args, 1u, &num_sh_args) || + MUL_OVERFLOW (num_sh_args, sizeof *sh_argv, &sh_argv_bytes)) { + errno = EOVERFLOW; + goto empty_pathnames; + } + sh_argv = malloc (sh_argv_bytes); + if (sh_argv == NULL) + goto empty_pathnames; + + /* Commit. */ + ctx->pathnames = pathnames; + ctx->sh_argv = sh_argv; + ctx->num_sh_args = num_sh_args; + rc = 0; + /* Fall through, for freeing temporaries. */ + +empty_pathnames: + if (rc == -1) { + free (pathname); + string_vector_empty (&pathnames); + } + +free_sys_path: + free (sys_path); + + return rc; +} + +void +nbd_internal_execvpe_uninit (struct execvpe *ctx) +{ + free (ctx->sh_argv); + ctx->num_sh_args = 0; + string_vector_empty (&ctx->pathnames); +} + +int +nbd_internal_fork_safe_execvpe (struct execvpe *ctx, const string_vector *argv, + char * const *envp) +{ + size_t pathname_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->pathnames.len > 0); + + pathname_idx = 0; + do { + (void)execve (ctx->pathnames.ptr[pathname_idx], argv->ptr, envp); + if (errno != EACCES && errno != ELOOP && errno != ENAMETOOLONG && + errno != ENOENT && errno != ENOTDIR) + break; + + ++pathname_idx; + } while (pathname_idx < ctx->pathnames.len); + + if (errno == ENOEXEC) { + char **sh_argp; + size_t argv_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args >= argv->len); + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args - argv->len == 1); + + sh_argp = ctx->sh_argv; + *sh_argp++ = argv->ptr[0]; + *sh_argp++ = ctx->pathnames.ptr[pathname_idx]; + for (argv_idx = 1; argv_idx < argv->len; ++argv_idx) + *sh_argp++ = argv->ptr[argv_idx]; + + (void)execve ("/bin/sh", ctx->sh_argv, envp); + } + + return -1; +}
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
You KNOW this is going to be a juicy commit message when the first line contains a 2-digit footnote ;)
- it performs PATH search [02] if necessary,
- it falls back to executing the file with the shell as a shell script in case the underlying execv() or execve() fails with ENOEXEC.
However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be.
Introduce three APIs for safely emulating execvp() with execve():
- nbd_internal_execvpe_init(): to be called in the parent process, before fork(). Allocate and format candidate pathnames for execution with execve(), based on PATH. Allocate a buffer for the longer command line that the shell script fallback needs.
- nbd_internal_fork_safe_execvpe(): to be called in the child process. Try the candidates formatted previously in sequence with execve(), as long as execve() fails with errors related to pathname resolution / inode-level file type / execution permission. Stop iterating on fatal errors; if the fatal error is ENOEXEC, activate the shell script fallback. As a small added feature, take the environment from an explicit parameter (effectively imitating the GNU-specific execvpe() function -- cf. commit dc64ac5cdd0b, "states: Use execvp instead of execvpe", 2019-11-15).
- nbd_internal_execvpe_uninit(): to be called in the parent process, after fork(). Release the resources allocated with nbd_internal_execvpe_init().
Makes sense. A shame that it requires 3 functions, and that we have to duplicate so much work that libc would normally do for us if we don't care about async-safety, but I think the result is worth it.
The main idea with the above distribution of responsibilities is that any pre-scanning of PATH -- which was suggested by Eric Blake -- should not include activities that are performed by execve() anyway. I've considered and abandoned two approaches:
- During scanning (i.e., in nbd_internal_execvpe_init()), check each candidate with access(X_OK) [05].
I dropped this because the informative APPLICATION USAGE section of the same specification [06] advises against it, saying "[a]n application should instead attempt the action itself and handle the [EACCES] error that occurs if the file is not accessible".
- During scanning, open each candidate with O_EXEC [07] until one open() succeeds. Save the sole file descriptor for nbd_internal_fork_safe_execvpe(). In the latter, call fexecve() [08], which is free of all error conditions that necessitate iteration over candidates. Still implement the ENOEXEC (shell script) fallback.
I dropped this because (a) fexecve() is a quite recent interface (highlighted by Eric); (b) for an application to open a file for execution with fexecve(), it's more robust to pass O_EXEC to open() than not to pass it, per APPLICATION USAGE [09], but on Linux/glibc, O_EXEC does not seem supported, only O_PATH does [10].
Thus the chosen approach -- pre-generate filenames -- contains a small
Accidental double space.
TOCTTOU race (highlighted by Eric) after all, but it should be harmless.
Yeah - if you are actively changing out binaries on PATH at the same time as anything else running in earnest on a production system, you've got more issues than just libnbd's potential race.
Implementation-defined details:
- If PATH searching is necessary, but PATH is absent [01], then fall back to confstr(_CS_PATH) [11], similarly to Linux/glibc execvpe() [12].
If PATH is set but empty ("set to null") [02], or PATH is unset and confstr(_CS_PATH) fails or returns no information or returns the empty string, we fail the initial scanning (!) with ENOENT. This is consistent with bash's behavior on Linux/glibc:
$ PATH= ls bash: ls: No such file or directory
_Technically_, PATH= should behave identically to PATH=.; if you have an executable named './ls' in your working directory, bash WILL execute it! $ mkdir -p /tmp/blah $ cd /tmp/blah $ cp /bin/ls . $ PATH= ls -a . .. ls But whether you want to tweak this patch to actually comply with that corner case is up to you (no one in their right mind should set PATH to empty). Below, I'll refer to this as [1].
Details chosen for unspecified behavior:
- Hardwire "/bin/sh" as the shell's pathname [01].
[01] https://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html [02] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag... [03] https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta... [04] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html [05] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html [06] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html#tag_1... [07] https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html [08] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html [09] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html#tag_... [10] https://man7.org/linux/man-pages/man2/open.2.html [11] https://pubs.opengroup.org/onlinepubs/9699919799/functions/confstr.html [12] https://man7.org/linux/man-pages/man3/execvpe.3.html
Very useful bibliography.
Suggested-by: Eric Blake <eblake@redhat.com> Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 22 ++ lib/utils.c | 351 ++++++++++++++++++++ 2 files changed, 373 insertions(+)
diff --git a/lib/internal.h b/lib/internal.h index 9c9021ed366e..8c4e32013e40 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -368,6 +368,18 @@ struct command { uint32_t error; /* Local errno value */ };
+struct execvpe { + string_vector pathnames; + + /* Note: "const_string_vector" is not a good type for "sh_argv" below. Even if + * we reserved enough space in a "const_string_vector", + * const_string_vector_append() would still not be async-signal-safe, due to + * the underlying const_string_vector_insert() calling assert(). + */ + char **sh_argv; + size_t num_sh_args; +}; + /* Test if a callback is "null" or not, and set it to null. */ #define CALLBACK_IS_NULL(cb) ((cb).callback == NULL && (cb).free == NULL) #define CALLBACK_IS_NOT_NULL(cb) (! CALLBACK_IS_NULL ((cb))) @@ -540,4 +552,14 @@ extern void nbd_internal_fork_safe_assert (int result, const char *file, __func__, #expression)) #endif
+extern int nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args) + LIBNBD_ATTRIBUTE_NONNULL (1, 2); +extern void nbd_internal_execvpe_uninit (struct execvpe *ctx) + LIBNBD_ATTRIBUTE_NONNULL (1); +extern int nbd_internal_fork_safe_execvpe (struct execvpe *ctx, + const string_vector *argv, + char * const *envp) + LIBNBD_ATTRIBUTE_NONNULL (1, 2, 3);
The interface looks good for now (if you can't tell, I'm doing a stream-of-consciousness review, and may have further comments about the interface once I see the implementation and/or usage).
+ #endif /* LIBNBD_INTERNAL_H */ diff --git a/lib/utils.c b/lib/utils.c index 5dd00f82e073..9a96ed6ed674 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -28,6 +28,7 @@ #include <limits.h>
#include "minmax.h" +#include "checked-overflow.h"
#include "internal.h"
@@ -463,3 +464,353 @@ nbd_internal_fork_safe_assert (int result, const char *file, long line, xwrite (STDERR_FILENO, "' failed.\n", 10); abort (); } + +/* Returns the value of the PATH environment variable -- falling back to + * confstr(_CS_PATH) if PATH is absent -- as a dynamically allocated string. On + * failure, sets "errno" and returns NULL. + */ +static char * +get_path (void) + LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free) +{ + char *path; + bool env_path_found; + size_t path_size, path_size2; + + /* FIXME: lock the environment. */ + path = getenv ("PATH"); + if ((env_path_found = (path != NULL))) + path = strdup (path); + /* FIXME: unlock the environment. */
Are the FIXME comments still necessary? I know we discussed about getenv() being technically non-async-safe in POSIX, but that in all practical aspects, we consider that a multi-threaded app should be smart enough to never modify the environment in parallel with calling into a library like libnbd. At best, you could reword the comment to get rid of FIXME and instead just state our reasoning for why we don't bother with locking the environment: glibc and any other high-quality libc will not be modifying environ during getenv().
+ + if (env_path_found) { + /* This handles out-of-memory as well. */ + return path; + }
Clever, and definitely worth that comment.
+ + errno = 0; + path_size = confstr (_CS_PATH, NULL, 0); + if (path_size == 0) { + /* If _CS_PATH does not have a configuration-defined value, just store + * ENOENT to "errno". + */ + if (errno == 0) + errno = ENOENT; + + return NULL; + } + + path = malloc (path_size); + if (path == NULL) + return NULL; + + path_size2 = confstr (_CS_PATH, path, path_size); + assert (path_size2 == path_size); + return path; +}
Nice.
+ +/* nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe() together + * present an execvp() alternative that is async-signal-safe. + * + * nbd_internal_execvpe_init() may only be called before fork(), for filling in + * the caller-allocated, uninitialized "ctx" structure. If + * nbd_internal_execvpe_init() succeeds, then fork() may be called. + * Subsequently, in the child process, nbd_internal_fork_safe_execvpe() may be + * called with the inherited "ctx" structure, while in the parent process, + * nbd_internal_execvpe_uninit() must be called to uninitialize (evacuate) the + * "ctx" structure. + * + * On failure, "ctx" will not have been modified, "errno" is set, and -1 is + * returned. Failures include: + * + * - Errors forwarded from underlying functions such as strdup(), confstr(), + * malloc(), string_vector_append(). + * + * - ENOENT: "file" is an empty string. + * + * - ENOENT: "file" does not contain a <slash> "/" character, the PATH + * environment variable is not set, and confstr() doesn't associate a + * configuration-defined value with _CS_PATH. + * + * - ENOENT: "file" does not contain a <slash> "/" character, and: (a) the PATH + * environment variable is set to the empty string, or (b) PATH is not + * set, and confstr() outputs the empty string for _CS_PATH.
[1] This might need tweaking if you treat empty PATH or _CS_PATH as equivalent to "." instead of an unconditional ENOENT.
+ * + * - EOVERFLOW: the sizes or counts of necessary objects could not be expressed. + * + * - EINVAL: "num_args" is less than 2. + * + * On success, the "ctx" structure will have been filled in, and 0 is returned. + * + * - "pathnames" member: + * + * - All strings pointed-to by elements of the "pathnames" string_vector + * member are owned by "pathnames". + * + * - If "file" contains a <slash> "/" character, then the sole entry in + * "pathnames" is a copy of "file". + * + * - If "file" does not contain a <slash> "/" character: + * + * Let "system path" be defined as the value of the PATH environment + * variable, if the latter exists, and as the value output by confstr() for + * _CS_PATH otherwise. Per the ENOENT specifications above, "system path" is + * a non-empty string. Let "system path" further be of the form + * + * <prefix_0> [n = 1] + * + * or + * + * <prefix_0>:<prefix_1>:...:<prefix_(n-1)> [n >= 2] + * + * where for each 0 <= i < n, <prefix_i> does not contain the <colon> ":" + * character. In the (n = 1) case, <prefix_0> is never empty (see ENOENT + * above), while in the (n >= 2) case, any individual <prefix_i> may or may + * not be empty.
[1] And this simplifies if empty PATH can imply searching in ".".
+ * + * The "pathnames" string_vector member has n elements; for each 0 <= i < n, + * element i is of the form + * + * suffix(curdir(<prefix_i>))file + * + * where + * + * curdir(x) := "." if x = "" + * curdir(x) := x otherwise + * + * and + * + * suffix(x) := x if "x" ends with a <slash> "/" + * suffix(x) := x "/" otherwise + * + * This transformation implements the POSIX XBD / PATH environment variable + * semantics, creating candidate pathnames for execution by + * nbd_internal_fork_safe_execvpe(). nbd_internal_fork_safe_execvpe() will + * iterate over the candidate pathnames with execve() until execve() + * succeeds, or fails with an error that is due to neither pathname + * resolution, nor the candidate not being a regular file, nor the candidate + * lacking execution permission. + * + * - The "sh_argv" array member will have at least (num_args + 1) elements + * allocated, and none populated. + * + * (The minimum value of "num_args" is 2 -- see EINVAL above. According to + * POSIX, "[t]he argument /arg0/ should point to a filename string that is + * associated with the process being started by one of the /exec/ functions", + * plus "num_args" includes the null pointer that terminates the argument + * list.) + * + * This allocation is made in anticipation of execve() failing for a + * particular candidate inside nbd_internal_fork_safe_execvpe() with ENOEXEC + * ("[t]he new process image file has the appropriate access permission but + * has an unrecognized format"). While that failure terminates the iteration, + * the failed call + * + * execve (pathnames[i], + * { argv[0], argv[1], ..., NULL }, // (num_args >= 2) elements + * { envp[0], envp[1], ..., NULL }) + * + * must be repeated as + * + * execve (<shell-path>, + * { argv[0], pathnames[i], // ((num_args + 1) >= 3)
Are we guaranteed that if we get to this fallback, pathnames[i] does not begin with '-' or '+'? Or is it possible to write PATH=/usr/bin:+my/evil/relative/path:... and/or the user to attempt to execute '+my/evil/name' which skips the PATH search because the binary name contains '/', such that the resulting argument to /bin/sh might need to be protected with an extra "--" argument to avoid being misinterpreted as options to the shell rather than the script name to be executed? Is the current working directory something we change between fork() and exec()? If so, should we canonicalize the entries in pathnames[] to always start with '/', so that the child process can reposition directories at will without changing the meaning of the pre-scanned candidates? (Note that canonicalizing names would automatically resolve my above question about safety in the presence of an unusual PATH or binary name beginning with [-+]). [2]
+ argv[1], ..., NULL }, // elements + * { envp[0], envp[1], ..., NULL }) + * + * for emulating execvp(). The allocation in the "sh_argv" member makes it + * possible just to *link* the original "argv" elements and the "pathnames[i]" + * candidate into the right positions. + * + * (POSIX leaves the shell pathname unspecified; "/bin/sh" should be good + * enough.) + * + * The shell *binary* will see itself being executed under the name "argv[0]", + * will receive "pathnames[i]" as the pathname of the shell *script* to read + * and interpret ("command_file" in POSIX terminology), will expose + * "pathnames[i]" as the positional parameter $0 to the script, and will + * forward "argv[1]" and the rest to the script as positional parameters $1 + * and onward. + */ +int +nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args)
Should the interface take the proposed argv[] here pre-fork(), or are we trying to keep this interface generic enough where the child may alter some of the argv post-fork()? On the other hand, I already know we are altering the environment post-fork() (setting LISTEN_PID= cannot be done pre-fork, for starters), so keeping the selection of argv solely in the child rather than making the parent generate it pre-fork seems like the right division of labor.
+{ + int rc; + char *sys_path; + string_vector pathnames; + char *pathname; + size_t num_sh_args; + char **sh_argv; + size_t sh_argv_bytes; + + rc = -1; + + if (file[0] == '\0') { + errno = ENOENT; + return rc; + } + + /* First phase. */ + sys_path = NULL; + pathnames = (string_vector)empty_vector;
Another point where we may want consistency on whether we do space after cast.
+ + if (strchr (file, '/') == NULL) { + size_t file_len; + const char *sys_path_element, *scan; + bool finish; + + sys_path = get_path (); + if (sys_path == NULL) + return rc; + + if (sys_path[0] == '\0') { + errno = ENOENT; + goto free_sys_path; + }
[1] Here is where you would code up an empty PATH as being equivalent to ".", if we think it's worth having, probably by just dropping this condition and letting the do-loop below handle it.
+ + pathname = NULL; + file_len = strlen (file); + sys_path_element = sys_path; + scan = sys_path; + do { + assert (sys_path_element <= scan); + finish = (*scan == '\0'); + if (finish || *scan == ':') { + const char *sys_path_copy_start; + size_t sys_path_copy_size; + size_t sep_copy_size; + size_t pathname_size; + char *p; + + if (scan == sys_path_element) { + sys_path_copy_start = "."; + sys_path_copy_size = 1; + } else { + sys_path_copy_start = sys_path_element; + sys_path_copy_size = scan - sys_path_element; + } + + assert (sys_path_copy_size >= 1); + sep_copy_size = (sys_path_copy_start[sys_path_copy_size - 1] != '/'); + + if (ADD_OVERFLOW (sys_path_copy_size, sep_copy_size, &pathname_size) || + ADD_OVERFLOW (pathname_size, file_len, &pathname_size) || + ADD_OVERFLOW (pathname_size, 1u, &pathname_size)) { + errno = EOVERFLOW; + goto empty_pathnames; + }
Since we are in the parent process during this _init() function, can't we just use asprintf() instead of concatenating it all out by hand?
+ + pathname = malloc (pathname_size); + if (pathname == NULL) + goto empty_pathnames; + p = pathname; + + memcpy (p, sys_path_copy_start, sys_path_copy_size); + p += sys_path_copy_size;
[2] Here is where we would want to consider whether to (semi-)canonicalize any relative names into a corresponding absolute prefix, to avoid issues with a relative PATH element starting with [-+].
+ + memcpy (p, "/", sep_copy_size); + p += sep_copy_size;
Clever way to avoid turning PATH=/:/usr/bin:... into an attempt to execute "//binary" which is NOT the same as "/binary". But also possible if you use asprintf(..., "...%.*s...", ..., (int) sep_copy_size, "/", ...) or declare sep_copy_size as int to begin with (because we know it will only be 0 or 1).
+ + memcpy (p, file, file_len); + p += file_len; + + *p++ = '\0'; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + + sys_path_element = scan + 1; + } + + ++scan; + } while (!finish); + } else { + pathname = strdup (file);
[2] and here we'd have to canonicalize 'file' itself if we are worrying about the child changing directories and/or starting with a character that could change the behavior of /bin/sh if mistaken as an option instead of a script name.
+ if (pathname == NULL) + return rc; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + } + + /* Second phase. */ + if (num_args < 2) { + errno = EINVAL; + goto empty_pathnames; + } + if (ADD_OVERFLOW (num_args, 1u, &num_sh_args) || + MUL_OVERFLOW (num_sh_args, sizeof *sh_argv, &sh_argv_bytes)) { + errno = EOVERFLOW; + goto empty_pathnames; + } + sh_argv = malloc (sh_argv_bytes); + if (sh_argv == NULL) + goto empty_pathnames;
Should we prepopulate sh_argv[0] as a placeholder that can then be easily swapped to ctx->pathnames[i], where the child first attempts execve(pathnames[i], argv+1, env) with the ENOEXEC fallback to argv[0]=pathnames[i]; execve("/bin/sh", argv, env), rather than having to shuffle things in the child process? [3]
+ + /* Commit. */ + ctx->pathnames = pathnames; + ctx->sh_argv = sh_argv; + ctx->num_sh_args = num_sh_args; + rc = 0; + /* Fall through, for freeing temporaries. */ + +empty_pathnames: + if (rc == -1) { + free (pathname); + string_vector_empty (&pathnames); + } + +free_sys_path: + free (sys_path); + + return rc; +} + +void +nbd_internal_execvpe_uninit (struct execvpe *ctx) +{ + free (ctx->sh_argv); + ctx->num_sh_args = 0; + string_vector_empty (&ctx->pathnames); +} + +int +nbd_internal_fork_safe_execvpe (struct execvpe *ctx, const string_vector *argv, + char * const *envp) +{ + size_t pathname_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->pathnames.len > 0); + + pathname_idx = 0; + do { + (void)execve (ctx->pathnames.ptr[pathname_idx], argv->ptr, envp); + if (errno != EACCES && errno != ELOOP && errno != ENAMETOOLONG && + errno != ENOENT && errno != ENOTDIR) + break; + + ++pathname_idx; + } while (pathname_idx < ctx->pathnames.len); + + if (errno == ENOEXEC) { + char **sh_argp; + size_t argv_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args >= argv->len); + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args - argv->len == 1); + + sh_argp = ctx->sh_argv; + *sh_argp++ = argv->ptr[0]; + *sh_argp++ = ctx->pathnames.ptr[pathname_idx]; + for (argv_idx = 1; argv_idx < argv->len; ++argv_idx) + *sh_argp++ = argv->ptr[argv_idx]; + + (void)execve ("/bin/sh", ctx->sh_argv, envp);
[3] Oh, I see - the shuffle is still probably needed, because ctx->sh_argv is different than user's argv which does not have a pre-allocated filler slot that we can just swap out. Unless we decide that the _init() function should take argv itself rather than making the child do the work of shifting everything from one array to another, but then we are inconsistent for grabbing argv in the parent and env in the child.
+ } + + return -1; +}
Overall looks like nice work. Based on my review, you may decide to make further tweaks, but I didn't see any hard reason why we couldn't use the patch as-written if you don't think my review warrants changes. Thus: Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/15/23 23:28, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
You KNOW this is going to be a juicy commit message when the first line contains a 2-digit footnote ;)
Haha, yes :) Clearly I originally started the commit message with single-digit footnotes, but I ended up needing another digit :)
- it performs PATH search [02] if necessary,
- it falls back to executing the file with the shell as a shell script in case the underlying execv() or execve() fails with ENOEXEC.
However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be.
Introduce three APIs for safely emulating execvp() with execve():
- nbd_internal_execvpe_init(): to be called in the parent process, before fork(). Allocate and format candidate pathnames for execution with execve(), based on PATH. Allocate a buffer for the longer command line that the shell script fallback needs.
- nbd_internal_fork_safe_execvpe(): to be called in the child process. Try the candidates formatted previously in sequence with execve(), as long as execve() fails with errors related to pathname resolution / inode-level file type / execution permission. Stop iterating on fatal errors; if the fatal error is ENOEXEC, activate the shell script fallback. As a small added feature, take the environment from an explicit parameter (effectively imitating the GNU-specific execvpe() function -- cf. commit dc64ac5cdd0b, "states: Use execvp instead of execvpe", 2019-11-15).
- nbd_internal_execvpe_uninit(): to be called in the parent process, after fork(). Release the resources allocated with nbd_internal_execvpe_init().
Makes sense. A shame that it requires 3 functions, and that we have to duplicate so much work that libc would normally do for us if we don't care about async-safety, but I think the result is worth it.
The main idea with the above distribution of responsibilities is that any pre-scanning of PATH -- which was suggested by Eric Blake -- should not include activities that are performed by execve() anyway. I've considered and abandoned two approaches:
- During scanning (i.e., in nbd_internal_execvpe_init()), check each candidate with access(X_OK) [05].
I dropped this because the informative APPLICATION USAGE section of the same specification [06] advises against it, saying "[a]n application should instead attempt the action itself and handle the [EACCES] error that occurs if the file is not accessible".
- During scanning, open each candidate with O_EXEC [07] until one open() succeeds. Save the sole file descriptor for nbd_internal_fork_safe_execvpe(). In the latter, call fexecve() [08], which is free of all error conditions that necessitate iteration over candidates. Still implement the ENOEXEC (shell script) fallback.
I dropped this because (a) fexecve() is a quite recent interface (highlighted by Eric); (b) for an application to open a file for execution with fexecve(), it's more robust to pass O_EXEC to open() than not to pass it, per APPLICATION USAGE [09], but on Linux/glibc, O_EXEC does not seem supported, only O_PATH does [10].
Thus the chosen approach -- pre-generate filenames -- contains a small
Accidental double space.
Meh, those are the bane of my existence. :) Thanks for catching it!
TOCTTOU race (highlighted by Eric) after all, but it should be harmless.
Yeah - if you are actively changing out binaries on PATH at the same time as anything else running in earnest on a production system, you've got more issues than just libnbd's potential race.
Implementation-defined details:
- If PATH searching is necessary, but PATH is absent [01], then fall back to confstr(_CS_PATH) [11], similarly to Linux/glibc execvpe() [12].
If PATH is set but empty ("set to null") [02], or PATH is unset and confstr(_CS_PATH) fails or returns no information or returns the empty string, we fail the initial scanning (!) with ENOENT. This is consistent with bash's behavior on Linux/glibc:
$ PATH= ls bash: ls: No such file or directory
_Technically_, PATH= should behave identically to PATH=.; if you have an executable named './ls' in your working directory, bash WILL execute it!
$ mkdir -p /tmp/blah $ cd /tmp/blah $ cp /bin/ls . $ PATH= ls -a . .. ls
But whether you want to tweak this patch to actually comply with that corner case is up to you (no one in their right mind should set PATH to empty). Below, I'll refer to this as [1].
Thanks for the observation. I'll modify the commit message -- remove the note that this behavior is consistent with bash/Linux/glibc. I've re-checked [02], and it's implementation-defined what we do when PATH is set to the empty string. POSIX does not mandate (even as "legacy") the fallback to "." when PATH is empty. The code was extremely hard to review for myself (much harder than just writing it), so I'd *really* like to avoid "tweaking" it. I'd need to review the whole thing from the ground up, and I'm kind of "out of steam" for that, and this is such a corner case indeed that I don't think maintaining the above-noted consistency with linux/glibc/bash is worth repeating the whole effort. (Not to mention the unit tests in the next patch, which were super difficult to *write* as well.)
Details chosen for unspecified behavior:
- Hardwire "/bin/sh" as the shell's pathname [01].
[01] https://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html [02] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag... [03] https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta... [04] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html [05] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html [06] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html#tag_1... [07] https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html [08] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html [09] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html#tag_... [10] https://man7.org/linux/man-pages/man2/open.2.html [11] https://pubs.opengroup.org/onlinepubs/9699919799/functions/confstr.html [12] https://man7.org/linux/man-pages/man3/execvpe.3.html
Very useful bibliography.
Suggested-by: Eric Blake <eblake@redhat.com> Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 22 ++ lib/utils.c | 351 ++++++++++++++++++++ 2 files changed, 373 insertions(+)
diff --git a/lib/internal.h b/lib/internal.h index 9c9021ed366e..8c4e32013e40 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -368,6 +368,18 @@ struct command { uint32_t error; /* Local errno value */ };
+struct execvpe { + string_vector pathnames; + + /* Note: "const_string_vector" is not a good type for "sh_argv" below. Even if + * we reserved enough space in a "const_string_vector", + * const_string_vector_append() would still not be async-signal-safe, due to + * the underlying const_string_vector_insert() calling assert(). + */ + char **sh_argv; + size_t num_sh_args; +}; + /* Test if a callback is "null" or not, and set it to null. */ #define CALLBACK_IS_NULL(cb) ((cb).callback == NULL && (cb).free == NULL) #define CALLBACK_IS_NOT_NULL(cb) (! CALLBACK_IS_NULL ((cb))) @@ -540,4 +552,14 @@ extern void nbd_internal_fork_safe_assert (int result, const char *file, __func__, #expression)) #endif
+extern int nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args) + LIBNBD_ATTRIBUTE_NONNULL (1, 2); +extern void nbd_internal_execvpe_uninit (struct execvpe *ctx) + LIBNBD_ATTRIBUTE_NONNULL (1); +extern int nbd_internal_fork_safe_execvpe (struct execvpe *ctx, + const string_vector *argv, + char * const *envp) + LIBNBD_ATTRIBUTE_NONNULL (1, 2, 3);
The interface looks good for now (if you can't tell, I'm doing a stream-of-consciousness review, and may have further comments about the interface once I see the implementation and/or usage).
+ #endif /* LIBNBD_INTERNAL_H */ diff --git a/lib/utils.c b/lib/utils.c index 5dd00f82e073..9a96ed6ed674 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -28,6 +28,7 @@ #include <limits.h>
#include "minmax.h" +#include "checked-overflow.h"
#include "internal.h"
@@ -463,3 +464,353 @@ nbd_internal_fork_safe_assert (int result, const char *file, long line, xwrite (STDERR_FILENO, "' failed.\n", 10); abort (); } + +/* Returns the value of the PATH environment variable -- falling back to + * confstr(_CS_PATH) if PATH is absent -- as a dynamically allocated string. On + * failure, sets "errno" and returns NULL. + */ +static char * +get_path (void) + LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free) +{ + char *path; + bool env_path_found; + size_t path_size, path_size2; + + /* FIXME: lock the environment. */ + path = getenv ("PATH"); + if ((env_path_found = (path != NULL))) + path = strdup (path); + /* FIXME: unlock the environment. */
Are the FIXME comments still necessary? I know we discussed about getenv() being technically non-async-safe in POSIX, but that in all practical aspects, we consider that a multi-threaded app should be smart enough to never modify the environment in parallel with calling into a library like libnbd. At best, you could reword the comment to get rid of FIXME and instead just state our reasoning for why we don't bother with locking the environment: glibc and any other high-quality libc will not be modifying environ during getenv().
Yes, I'll replace the FIXME with a NOTE -- that strictly per POSIX, we should lock the environment even for getenv(), but "glibc and any other high-quality libc will not be modifying environ during getenv()", and that no sane application should modify the environment after launching threads.
+ + if (env_path_found) { + /* This handles out-of-memory as well. */ + return path; + }
Clever, and definitely worth that comment.
+ + errno = 0; + path_size = confstr (_CS_PATH, NULL, 0); + if (path_size == 0) { + /* If _CS_PATH does not have a configuration-defined value, just store + * ENOENT to "errno". + */ + if (errno == 0) + errno = ENOENT; + + return NULL; + } + + path = malloc (path_size); + if (path == NULL) + return NULL; + + path_size2 = confstr (_CS_PATH, path, path_size); + assert (path_size2 == path_size); + return path; +}
Nice.
+ +/* nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe() together + * present an execvp() alternative that is async-signal-safe. + * + * nbd_internal_execvpe_init() may only be called before fork(), for filling in + * the caller-allocated, uninitialized "ctx" structure. If + * nbd_internal_execvpe_init() succeeds, then fork() may be called. + * Subsequently, in the child process, nbd_internal_fork_safe_execvpe() may be + * called with the inherited "ctx" structure, while in the parent process, + * nbd_internal_execvpe_uninit() must be called to uninitialize (evacuate) the + * "ctx" structure. + * + * On failure, "ctx" will not have been modified, "errno" is set, and -1 is + * returned. Failures include: + * + * - Errors forwarded from underlying functions such as strdup(), confstr(), + * malloc(), string_vector_append(). + * + * - ENOENT: "file" is an empty string. + * + * - ENOENT: "file" does not contain a <slash> "/" character, the PATH + * environment variable is not set, and confstr() doesn't associate a + * configuration-defined value with _CS_PATH. + * + * - ENOENT: "file" does not contain a <slash> "/" character, and: (a) the PATH + * environment variable is set to the empty string, or (b) PATH is not + * set, and confstr() outputs the empty string for _CS_PATH.
[1] This might need tweaking if you treat empty PATH or _CS_PATH as equivalent to "." instead of an unconditional ENOENT.
+ * + * - EOVERFLOW: the sizes or counts of necessary objects could not be expressed. + * + * - EINVAL: "num_args" is less than 2. + * + * On success, the "ctx" structure will have been filled in, and 0 is returned. + * + * - "pathnames" member: + * + * - All strings pointed-to by elements of the "pathnames" string_vector + * member are owned by "pathnames". + * + * - If "file" contains a <slash> "/" character, then the sole entry in + * "pathnames" is a copy of "file". + * + * - If "file" does not contain a <slash> "/" character: + * + * Let "system path" be defined as the value of the PATH environment + * variable, if the latter exists, and as the value output by confstr() for + * _CS_PATH otherwise. Per the ENOENT specifications above, "system path" is + * a non-empty string. Let "system path" further be of the form + * + * <prefix_0> [n = 1] + * + * or + * + * <prefix_0>:<prefix_1>:...:<prefix_(n-1)> [n >= 2] + * + * where for each 0 <= i < n, <prefix_i> does not contain the <colon> ":" + * character. In the (n = 1) case, <prefix_0> is never empty (see ENOENT + * above), while in the (n >= 2) case, any individual <prefix_i> may or may + * not be empty.
[1] And this simplifies if empty PATH can imply searching in ".".
If we ever get practical problems with PATH="", let's update the code then, incrementally.
+ * + * The "pathnames" string_vector member has n elements; for each 0 <= i < n, + * element i is of the form + * + * suffix(curdir(<prefix_i>))file + * + * where + * + * curdir(x) := "." if x = "" + * curdir(x) := x otherwise + * + * and + * + * suffix(x) := x if "x" ends with a <slash> "/" + * suffix(x) := x "/" otherwise + * + * This transformation implements the POSIX XBD / PATH environment variable + * semantics, creating candidate pathnames for execution by + * nbd_internal_fork_safe_execvpe(). nbd_internal_fork_safe_execvpe() will + * iterate over the candidate pathnames with execve() until execve() + * succeeds, or fails with an error that is due to neither pathname + * resolution, nor the candidate not being a regular file, nor the candidate + * lacking execution permission. + * + * - The "sh_argv" array member will have at least (num_args + 1) elements + * allocated, and none populated. + * + * (The minimum value of "num_args" is 2 -- see EINVAL above. According to + * POSIX, "[t]he argument /arg0/ should point to a filename string that is + * associated with the process being started by one of the /exec/ functions", + * plus "num_args" includes the null pointer that terminates the argument + * list.) + * + * This allocation is made in anticipation of execve() failing for a + * particular candidate inside nbd_internal_fork_safe_execvpe() with ENOEXEC + * ("[t]he new process image file has the appropriate access permission but + * has an unrecognized format"). While that failure terminates the iteration, + * the failed call + * + * execve (pathnames[i], + * { argv[0], argv[1], ..., NULL }, // (num_args >= 2) elements + * { envp[0], envp[1], ..., NULL }) + * + * must be repeated as + * + * execve (<shell-path>, + * { argv[0], pathnames[i], // ((num_args + 1) >= 3)
Are we guaranteed that if we get to this fallback, pathnames[i] does not begin with '-' or '+'?
No, but it's not something that POSIX requires either.
Or is it possible to write PATH=/usr/bin:+my/evil/relative/path:... and/or the user to attempt to execute '+my/evil/name' which skips the PATH search because the binary name contains '/', such that the resulting argument to /bin/sh might need to be protected with an extra "--" argument to avoid being misinterpreted as options to the shell rather than the script name to be executed?
Certainly possible, but I don't think it's the job of this code to prevent that. I don't think we expect any libnbd application to run as a part of a setuid or setgid application (definitely not without the application properly separating privileges), so I think the user launching the libnbd application is responsible for setting their environment (including PATH) correctly. Also I think libnbd application writers are responsible for passing sane arguments to nbd_connect_systemd_socket_activation() and nbd_connect_command(). (To begin with -- if we such privilege escalations were relevant threats, we should be using the non-portable secure_getenv() already, not getenv()!)
Is the current working directory something we change between fork() and exec()?
No, we don't do that. While chdir() is async-signal-safe, our two fork->exec paths have no reason for calling chdir().
If so, should we canonicalize the entries in pathnames[] to always start with '/', so that the child process can reposition directories at will without changing the meaning of the pre-scanned candidates?
Again, we don't call chdir() beween fork and exec, but even if we did -- from the user's perspective (and from the application's perspective that calls nbd_connect_systemd_socket_activation() or nbd_connect_command()), there's no difference between: - having a PATH with relative prefixes on it and changing the working directory *before* fork (either in the application or in libnbd), and - having a PATH with relative prefixes on it and changing the working directory in libnbd between fork and exec, for something the user might want. Having relative prefixes on PATH means that execvpe() will be cwd-sensitive one way or another. We have no reason for calling chdir() ourselves -- i.e., a libnbd-internal reason -- between fork and exec.
(Note that canonicalizing names would automatically resolve my above question about safety in the presence of an unusual PATH or binary name beginning with [-+]). [2]
+ argv[1], ..., NULL }, // elements + * { envp[0], envp[1], ..., NULL }) + * + * for emulating execvp(). The allocation in the "sh_argv" member makes it + * possible just to *link* the original "argv" elements and the "pathnames[i]" + * candidate into the right positions. + * + * (POSIX leaves the shell pathname unspecified; "/bin/sh" should be good + * enough.) + * + * The shell *binary* will see itself being executed under the name "argv[0]", + * will receive "pathnames[i]" as the pathname of the shell *script* to read + * and interpret ("command_file" in POSIX terminology), will expose + * "pathnames[i]" as the positional parameter $0 to the script, and will + * forward "argv[1]" and the rest to the script as positional parameters $1 + * and onward. + */ +int +nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args)
Should the interface take the proposed argv[] here pre-fork(), or are we trying to keep this interface generic enough where the child may alter some of the argv post-fork()? On the other hand, I already know we are altering the environment post-fork() (setting LISTEN_PID= cannot be done pre-fork, for starters), so keeping the selection of argv solely in the child rather than making the parent generate it pre-fork seems like the right division of labor.
The intent is to move the absolute minimum of work into the parent. Argv is indeed not meant to change between _init and execvpe, but that's just not enough reason for capturing argv in the context structure. The only piece of information needed for _init is the element count of argv, so that's what the function takes. If the contents of argv are modified afterwards (excluding resizing), things would work for now, but the intent is neither to prevent that practice programmatically nor to encourage it.
+{ + int rc; + char *sys_path; + string_vector pathnames; + char *pathname; + size_t num_sh_args; + char **sh_argv; + size_t sh_argv_bytes; + + rc = -1; + + if (file[0] == '\0') { + errno = ENOENT; + return rc; + } + + /* First phase. */ + sys_path = NULL; + pathnames = (string_vector)empty_vector;
Another point where we may want consistency on whether we do space after cast.
+ + if (strchr (file, '/') == NULL) { + size_t file_len; + const char *sys_path_element, *scan; + bool finish; + + sys_path = get_path (); + if (sys_path == NULL) + return rc; + + if (sys_path[0] == '\0') { + errno = ENOENT; + goto free_sys_path; + }
[1] Here is where you would code up an empty PATH as being equivalent to ".", if we think it's worth having, probably by just dropping this condition and letting the do-loop below handle it.
+ + pathname = NULL; + file_len = strlen (file); + sys_path_element = sys_path; + scan = sys_path; + do { + assert (sys_path_element <= scan); + finish = (*scan == '\0'); + if (finish || *scan == ':') { + const char *sys_path_copy_start; + size_t sys_path_copy_size; + size_t sep_copy_size; + size_t pathname_size; + char *p; + + if (scan == sys_path_element) { + sys_path_copy_start = "."; + sys_path_copy_size = 1; + } else { + sys_path_copy_start = sys_path_element; + sys_path_copy_size = scan - sys_path_element; + } + + assert (sys_path_copy_size >= 1); + sep_copy_size = (sys_path_copy_start[sys_path_copy_size - 1] != '/'); + + if (ADD_OVERFLOW (sys_path_copy_size, sep_copy_size, &pathname_size) || + ADD_OVERFLOW (pathname_size, file_len, &pathname_size) || + ADD_OVERFLOW (pathname_size, 1u, &pathname_size)) { + errno = EOVERFLOW; + goto empty_pathnames; + }
Since we are in the parent process during this _init() function, can't we just use asprintf() instead of concatenating it all out by hand?
I thought long and hard about asprintf(), and certainly expected this question :) My problem with asprintf (and to an extent with sprintf / snprintf) is that they return the number of bytes printed (or the number of bytes that would have been printed), without the terminating NUL, as a *signed int*. I can't fathom how that is an acceptable interface design when strlen() and sizeof both produce size_t, and when -- specifically regarding asprintf() -- malloc() takes a size_t. The glibc manual is super vague on this "int" overflow ("If memory allocation wasn't possible, or some other error occurs"): https://man7.org/linux/man-pages/man3/asprintf.3.html and I can't refer to the POSIX standard, because asprintf() is not POSIX. If I simply refer to fprintf() in C99 (which has similar return value semantics), I find (7.19.6.1p14-15): Returns The fprintf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred. Environmental limits The number of characters that can be produced by any single conversion shall be at least 4095. Ugh... thanks? Sigh. If I refer to fprintf() in POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html it *does* say what I want to see: [EOVERFLOW] [CX] [Option Start] The value to be returned is greater than {INT_MAX}. [Option End] but it *does not* connect to asprintf(). Asprintf() is not in POSIX, and the EOVERFLOW language is not in the linux/glibc manual. (The linux/glibc manual on asprintf doesn't mention "errno" *at all*.) So, writing the code like this was actually not a huge problem (it's a relatively small complication compared to the rest), and it saved me an *assumption* about asprintf(). (And no, I didn't want to rely on the glibc source code happening to do what we want.)
+ + pathname = malloc (pathname_size); + if (pathname == NULL) + goto empty_pathnames; + p = pathname; + + memcpy (p, sys_path_copy_start, sys_path_copy_size); + p += sys_path_copy_size;
[2] Here is where we would want to consider whether to (semi-)canonicalize any relative names into a corresponding absolute prefix, to avoid issues with a relative PATH element starting with [-+].
+ + memcpy (p, "/", sep_copy_size); + p += sep_copy_size;
Clever way to avoid turning PATH=/:/usr/bin:... into an attempt to execute "//binary" which is NOT the same as "/binary".
Well to be honest, I did this specifically because reference [02] on PATH states: When a non-zero-length prefix is applied to this filename, a <slash> shall be inserted between the prefix and the filename if the prefix did not end in <slash>. It happens to prevent "//binary" too, but that wasn't specifically my intent. How does "//binary" differ from "/binary", in pathname resolution?
But also possible if you use asprintf(..., "...%.*s...", ..., (int) sep_copy_size, "/", ...)
or declare sep_copy_size as int to begin with (because we know it will only be 0 or 1).
Yes, I'd agree about "int" being fine for "sep_copy_size" here; I just wanted to avoid asprintf altogether. (Once I do the checked additions up-front, asprintf() doesn't really save much.)
+ + memcpy (p, file, file_len); + p += file_len; + + *p++ = '\0'; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + + sys_path_element = scan + 1; + } + + ++scan; + } while (!finish); + } else { + pathname = strdup (file);
[2] and here we'd have to canonicalize 'file' itself if we are worrying about the child changing directories and/or starting with a character that could change the behavior of /bin/sh if mistaken as an option instead of a script name.
+ if (pathname == NULL) + return rc; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + } + + /* Second phase. */ + if (num_args < 2) { + errno = EINVAL; + goto empty_pathnames; + } + if (ADD_OVERFLOW (num_args, 1u, &num_sh_args) || + MUL_OVERFLOW (num_sh_args, sizeof *sh_argv, &sh_argv_bytes)) { + errno = EOVERFLOW; + goto empty_pathnames; + } + sh_argv = malloc (sh_argv_bytes); + if (sh_argv == NULL) + goto empty_pathnames;
Should we prepopulate sh_argv[0] as a placeholder that can then be easily swapped to ctx->pathnames[i], where the child first attempts execve(pathnames[i], argv+1, env) with the ENOEXEC fallback to argv[0]=pathnames[i]; execve("/bin/sh", argv, env), rather than having to shuffle things in the child process? [3]
(commenting under [3] below)
+ + /* Commit. */ + ctx->pathnames = pathnames; + ctx->sh_argv = sh_argv; + ctx->num_sh_args = num_sh_args; + rc = 0; + /* Fall through, for freeing temporaries. */ + +empty_pathnames: + if (rc == -1) { + free (pathname); + string_vector_empty (&pathnames); + } + +free_sys_path: + free (sys_path); + + return rc; +} + +void +nbd_internal_execvpe_uninit (struct execvpe *ctx) +{ + free (ctx->sh_argv); + ctx->num_sh_args = 0; + string_vector_empty (&ctx->pathnames); +} + +int +nbd_internal_fork_safe_execvpe (struct execvpe *ctx, const string_vector *argv, + char * const *envp) +{ + size_t pathname_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->pathnames.len > 0); + + pathname_idx = 0; + do { + (void)execve (ctx->pathnames.ptr[pathname_idx], argv->ptr, envp); + if (errno != EACCES && errno != ELOOP && errno != ENAMETOOLONG && + errno != ENOENT && errno != ENOTDIR) + break; + + ++pathname_idx; + } while (pathname_idx < ctx->pathnames.len); + + if (errno == ENOEXEC) { + char **sh_argp; + size_t argv_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args >= argv->len); + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args - argv->len == 1); + + sh_argp = ctx->sh_argv; + *sh_argp++ = argv->ptr[0]; + *sh_argp++ = ctx->pathnames.ptr[pathname_idx]; + for (argv_idx = 1; argv_idx < argv->len; ++argv_idx) + *sh_argp++ = argv->ptr[argv_idx]; + + (void)execve ("/bin/sh", ctx->sh_argv, envp);
[3] Oh, I see - the shuffle is still probably needed, because ctx->sh_argv is different than user's argv which does not have a pre-allocated filler slot that we can just swap out. Unless we decide that the _init() function should take argv itself rather than making the child do the work of shifting everything from one array to another, but then we are inconsistent for grabbing argv in the parent and env in the child.
Indeed. Again, the intent was to move the minimum amount of work required into the parent; in parallel, to move the minimum amount of state into the explicit "context" structure. Other than that, I'll make a comment of my own here. I could have used memcpy() in place of the loop, and I actually prototyped how that would look. (Memcpy() is async-signal-safe, and arguably faster on a high-quality libc than the manual loop.) However, for the last argument of memcpy(), I would have wanted to assert that the element size of "ctx->sh_argv" and that of argv->ptr were the same. Furthermore, I would have wanted to make that a static assertion. But a general static assertion would have been so much work (cf. STATIC_ASSERT_UNSIGNED_INT), in comparison to how few elements we tend to copy here, that I decided to go with the loop. The assignment operator in the loop is really nice because the compiler checks for element compatibility (i.e., it enforces that both arrays have (char*) elements).
+ } + + return -1; +}
Overall looks like nice work.
Based on my review, you may decide to make further tweaks, but I didn't see any hard reason why we couldn't use the patch as-written if you don't think my review warrants changes. Thus:
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! I will update the commit message (fix the double space typo, and remove the allusion to bash compatibility near PATH=''), and replace the FIXME with a NOTE in get_path(). Thanks for the thorough review! Laszlo
On Tue, Feb 21, 2023 at 12:55:49PM +0100, Laszlo Ersek wrote:
On 2/15/23 23:28, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
You KNOW this is going to be a juicy commit message when the first line contains a 2-digit footnote ;)
Haha, yes :) Clearly I originally started the commit message with single-digit footnotes, but I ended up needing another digit :)
Trimming my responses to just your open questions...
_Technically_, PATH= should behave identically to PATH=.; if you have an executable named './ls' in your working directory, bash WILL execute it!
$ mkdir -p /tmp/blah $ cd /tmp/blah $ cp /bin/ls . $ PATH= ls -a . .. ls
But whether you want to tweak this patch to actually comply with that corner case is up to you (no one in their right mind should set PATH to empty). Below, I'll refer to this as [1].
Thanks for the observation.
I'll modify the commit message -- remove the note that this behavior is consistent with bash/Linux/glibc. I've re-checked [02], and it's implementation-defined what we do when PATH is set to the empty string. POSIX does not mandate (even as "legacy") the fallback to "." when PATH is empty. The code was extremely hard to review for myself (much harder than just writing it), so I'd *really* like to avoid "tweaking" it. I'd need to review the whole thing from the ground up, and I'm kind of "out of steam" for that, and this is such a corner case indeed that I don't think maintaining the above-noted consistency with linux/glibc/bash is worth repeating the whole effort.
(Not to mention the unit tests in the next patch, which were super difficult to *write* as well.)
Yes, I can accept your reasoning that PATH="" is different than PATH=":" (the latter is well-defined, albeit obsolete, as a synonym to PATH=".:.", the former is explicitly "implementation-defined"). So your argument of leaving this corner-case explicitly documented as our implementation choice different from glibc, all in order to avoid further coding efforts, is fine with me. ...
[1] And this simplifies if empty PATH can imply searching in ".".
If we ever get practical problems with PATH="", let's update the code then, incrementally.
Indeed. No need to fret about a corner case that no one will hit; or if they can prove that it matters, it's an easy incremental patch at that time.
+ * This allocation is made in anticipation of execve() failing for a + * particular candidate inside nbd_internal_fork_safe_execvpe() with ENOEXEC + * ("[t]he new process image file has the appropriate access permission but + * has an unrecognized format"). While that failure terminates the iteration, + * the failed call + * + * execve (pathnames[i], + * { argv[0], argv[1], ..., NULL }, // (num_args >= 2) elements + * { envp[0], envp[1], ..., NULL }) + * + * must be repeated as + * + * execve (<shell-path>, + * { argv[0], pathnames[i], // ((num_args + 1) >= 3)
Are we guaranteed that if we get to this fallback, pathnames[i] does not begin with '-' or '+'?
No, but it's not something that POSIX requires either.
Or is it possible to write PATH=/usr/bin:+my/evil/relative/path:... and/or the user to attempt to execute '+my/evil/name' which skips the PATH search because the binary name contains '/', such that the resulting argument to /bin/sh might need to be protected with an extra "--" argument to avoid being misinterpreted as options to the shell rather than the script name to be executed?
Certainly possible, but I don't think it's the job of this code to prevent that. I don't think we expect any libnbd application to run as a part of a setuid or setgid application (definitely not without the application properly separating privileges), so I think the user launching the libnbd application is responsible for setting their environment (including PATH) correctly. Also I think libnbd application writers are responsible for passing sane arguments to nbd_connect_systemd_socket_activation() and nbd_connect_command().
(To begin with -- if we such privilege escalations were relevant threats, we should be using the non-portable secure_getenv() already, not getenv()!)
Again agreed - any user desperate enough to pass in an atypical binary name or put atypical relative directory names in PATH gets what they deserve; as long as we don't think our code misbehaving on argv[0] of "+s" can be abused as a security hole, then not worrying about the corner case in our code is just fine for this patch, and an incremental patch on top if we can think of why it is security-sensitive after all.
Since we are in the parent process during this _init() function, can't we just use asprintf() instead of concatenating it all out by hand?
I thought long and hard about asprintf(), and certainly expected this question :)
My problem with asprintf (and to an extent with sprintf / snprintf) is that they return the number of bytes printed (or the number of bytes that would have been printed), without the terminating NUL, as a *signed int*. I can't fathom how that is an acceptable interface design when strlen() and sizeof both produce size_t, and when -- specifically regarding asprintf() -- malloc() takes a size_t. The glibc manual is super vague on this "int" overflow ("If memory allocation wasn't possible, or some other error occurs"):
https://man7.org/linux/man-pages/man3/asprintf.3.html
and I can't refer to the POSIX standard, because asprintf() is not POSIX.
Not yet, but coming to POSIX soon: https://www.austingroupbugs.net/view.php?id=1496 But avoiding it in the short term doesn't hurt my feelings; it's just a different set of boilerplate code. ...
+ + memcpy (p, "/", sep_copy_size); + p += sep_copy_size;
Clever way to avoid turning PATH=/:/usr/bin:... into an attempt to execute "//binary" which is NOT the same as "/binary".
Well to be honest, I did this specifically because reference [02] on PATH states:
When a non-zero-length prefix is applied to this filename, a <slash> shall be inserted between the prefix and the filename if the prefix did not end in <slash>.
It happens to prevent "//binary" too, but that wasn't specifically my intent.
How does "//binary" differ from "/binary", in pathname resolution?
POSIX intentionally says that file names beginning with leading "//" but not "///" have implementation-defined semantics. Linux's definition is that "//" is a synonym for "/", so it makes no difference there. But on Cygwin, "/machine" is a file (or directory) living in the root directory, while "//machine/share" is a network share access path that locates the shared resource "share" on network-addressable "machine" (think Windows \\MACHINE\SHARE syntax). Native windows does not let you access bare \\ or \\MACHINE, but Cygwin has further generalized those so that "ls //" shows you a list of all machines currently advertising shares in your local subnet (do not try it on a large enterprise subnet - it can take a LOONG time). Cygwin also defaults to having "/cygdrive/c" be a synonym to Windows C:\, but has an option for you to re-spell it as "//c" (that is, expose all of your drive letters the same as remote-access machines by setting the cygdrive prefix to empty instead of "cygdrive"). ...
Overall looks like nice work.
Based on my review, you may decide to make further tweaks, but I didn't see any hard reason why we couldn't use the patch as-written if you don't think my review warrants changes. Thus:
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks!
I will update the commit message (fix the double space typo, and remove the allusion to bash compatibility near PATH=''), and replace the FIXME with a NOTE in get_path().
Thanks for the thorough review! Laszlo
And thanks again for a detailed commit message and code comments for something that would otherwise be hard to maintain if we didn't have strong rationale for why we are doing it. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/21/23 18:06, Eric Blake wrote:
On Tue, Feb 21, 2023 at 12:55:49PM +0100, Laszlo Ersek wrote:
On 2/15/23 23:28, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
You KNOW this is going to be a juicy commit message when the first line contains a 2-digit footnote ;)
Haha, yes :) Clearly I originally started the commit message with single-digit footnotes, but I ended up needing another digit :)
Trimming my responses to just your open questions...
Sorry, I should have been trimming too.
[1] And this simplifies if empty PATH can imply searching in ".".
If we ever get practical problems with PATH="", let's update the code then, incrementally.
Indeed. No need to fret about a corner case that no one will hit; or if they can prove that it matters, it's an easy incremental patch at that time.
Well, given Daniel's comments meanwhile, it seems like the original execvp() is something we shouldn't fret about. :/
Again agreed - any user desperate enough to pass in an atypical binary name or put atypical relative directory names in PATH gets what they deserve; as long as we don't think our code misbehaving on argv[0] of "+s" can be abused as a security hole, then not worrying about the corner case in our code is just fine for this patch, and an incremental patch on top if we can think of why it is security-sensitive after all.
Can you please elaborate on "+s"? (I'd like to understand your point regardless of this patch, too.)
How does "//binary" differ from "/binary", in pathname resolution?
POSIX intentionally says that file names beginning with leading "//" but not "///" have implementation-defined semantics. Linux's definition is that "//" is a synonym for "/", so it makes no difference there. But on Cygwin, "/machine" is a file (or directory) living in the root directory, while "//machine/share" is a network share access path that locates the shared resource "share" on network-addressable "machine" (think Windows \\MACHINE\SHARE syntax). Native windows does not let you access bare \\ or \\MACHINE, but Cygwin has further generalized those so that "ls //" shows you a list of all machines currently advertising shares in your local subnet (do not try it on a large enterprise subnet - it can take a LOONG time). Cygwin also defaults to having "/cygdrive/c" be a synonym to Windows C:\, but has an option for you to re-spell it as "//c" (that is, expose all of your drive letters the same as remote-access machines by setting the cygdrive prefix to empty instead of "cygdrive").
Ah, found it: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag... """ If a pathname begins with two successive <slash> characters, the first component following the leading <slash> characters may be interpreted in an implementation-defined manner, although more than two leading <slash> characters shall be treated as a single <slash> character. """ (... I wanted to say that "there's no replacement for reading POSIX in full", but now I'm wondering if reading POSIX *at all* makes sense...)
And thanks again for a detailed commit message and code comments for something that would otherwise be hard to maintain if we didn't have strong rationale for why we are doing it.
... That rationale may just have fallen away. Laszlo
On Tue, Feb 21, 2023 at 07:07:38PM +0100, Laszlo Ersek wrote:
On 2/21/23 18:06, Eric Blake wrote:
On Tue, Feb 21, 2023 at 12:55:49PM +0100, Laszlo Ersek wrote:
On 2/15/23 23:28, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
You KNOW this is going to be a juicy commit message when the first line contains a 2-digit footnote ;)
Haha, yes :) Clearly I originally started the commit message with single-digit footnotes, but I ended up needing another digit :)
Trimming my responses to just your open questions...
Sorry, I should have been trimming too.
No worries. Some email clients make it easier to gloss over quoted text than others, but I tend to trim to try and focus the reader's attention on where I am actually adding to the conversation. At the other extreme, sometimes I trim too much context for someone jumping into the conversation late - in open source this is fine because there are public archives, but in other contexts it can have negative consequences.
[1] And this simplifies if empty PATH can imply searching in ".".
If we ever get practical problems with PATH="", let's update the code then, incrementally.
Indeed. No need to fret about a corner case that no one will hit; or if they can prove that it matters, it's an easy incremental patch at that time.
Well, given Daniel's comments meanwhile, it seems like the original execvp() is something we shouldn't fret about. :/
glibc marks execvp() and exevpe() as 'MT-Safe env', which means it does not modify 'environ' and presumably does not use 'malloc'. If the only reason POSIX marks execvp() as thread-unsafe is because of its interaction with getenv() for PATH and therefore unsafe to exec a child in one thread while another is calling putenv(), then it is no worse than our use of getenv() without locking on the grounds that no sane app will be doing setenv() after spawning threads. But you've gone to a lot of work on this series; I'm still in favor of including your work, even if decide our premise behind it is weaker than intended. As I understood it, the premise is that actively avoiding as many non-async-safe functions as possible between fork() and exec() is always a good idea to avoid the deadlock created when a multithreaded process fork()s in one thread while another thread holds a mutex on the non-async-safe resource (in the child process, the other thread no longer exists and therefore can never release the resource). Knowing WHICH resources are liable to be locked in other threads makes it easier to reason about which generically non-async-safe functions can be used if we make other limiting restrictions (such as glibc's 'MT-Safe env' designation), but that requires more thought than blindly avoiding all non-async-safe functions.
Again agreed - any user desperate enough to pass in an atypical binary name or put atypical relative directory names in PATH gets what they deserve; as long as we don't think our code misbehaving on argv[0] of "+s" can be abused as a security hole, then not worrying about the corner case in our code is just fine for this patch, and an incremental patch on top if we can think of why it is security-sensitive after all.
Can you please elaborate on "+s"? (I'd like to understand your point regardless of this patch, too.)
https://www.austingroupbugs.net/view.php?id=1440 is where it came up in the Austin Group. Basically, having system("+s") be _required_ to invoke ["/path/to/sh", "-c", "+s"] is risky - "+s" is ambiguous between being the name of a real script and being a shell option (POSIX already warns that naming an executable with a leading "-" is unwise because of potential conflicts with options, but 'sh' has the special rules that + as well as - can introduce options for historical reasons). Calling ["/path/to/sh", "-c", "--", "+s"] is unambiguous for a POSIX sh that understands "--" as the end-of-options marker, but not all historical sh did that. It is also possible to use ["/path/to/sh", "-c", " +s"] (note the added leading space, which is ignored by the shell), but that requires space for injecting the leading space, and how do you do that concisely while still maintaining async-safety?
How does "//binary" differ from "/binary", in pathname resolution?
POSIX intentionally says that file names beginning with leading "//" but not "///" have implementation-defined semantics. Linux's definition is that "//" is a synonym for "/", so it makes no difference there. But on Cygwin, "/machine" is a file (or directory) living in the root directory, while "//machine/share" is a network share access path that locates the shared resource "share" on network-addressable "machine" (think Windows \\MACHINE\SHARE syntax). Native windows does not let you access bare \\ or \\MACHINE, but Cygwin has further generalized those so that "ls //" shows you a list of all machines currently advertising shares in your local subnet (do not try it on a large enterprise subnet - it can take a LOONG time). Cygwin also defaults to having "/cygdrive/c" be a synonym to Windows C:\, but has an option for you to re-spell it as "//c" (that is, expose all of your drive letters the same as remote-access machines by setting the cygdrive prefix to empty instead of "cygdrive").
Ah, found it:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag...
""" If a pathname begins with two successive <slash> characters, the first component following the leading <slash> characters may be interpreted in an implementation-defined manner, although more than two leading <slash> characters shall be treated as a single <slash> character. """
(... I wanted to say that "there's no replacement for reading POSIX in full", but now I'm wondering if reading POSIX *at all* makes sense...)
Alas, this rings too close to home... Standards are only useful if they are likely to be followed, which in turn is harder if they are hard to read.
And thanks again for a detailed commit message and code comments for something that would otherwise be hard to maintain if we didn't have strong rationale for why we are doing it.
... That rationale may just have fallen away.
It certainly got weakened.
Laszlo
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/21/23 20:32, Eric Blake wrote:
On Tue, Feb 21, 2023 at 07:07:38PM +0100, Laszlo Ersek wrote:
Well, given Daniel's comments meanwhile, it seems like the original execvp() is something we shouldn't fret about. :/
glibc marks execvp() and exevpe() as 'MT-Safe env', which means it does not modify 'environ' and presumably does not use 'malloc'. If the only reason POSIX marks execvp() as thread-unsafe is because of its interaction with getenv() for PATH and therefore unsafe to exec a child in one thread while another is calling putenv(), then it is no worse than our use of getenv() without locking on the grounds that no sane app will be doing setenv() after spawning threads.
But you've gone to a lot of work on this series; I'm still in favor of including your work, even if decide our premise behind it is weaker than intended. As I understood it, the premise is that actively avoiding as many non-async-safe functions as possible between fork() and exec() is always a good idea to avoid the deadlock created when a multithreaded process fork()s in one thread while another thread holds a mutex on the non-async-safe resource (in the child process, the other thread no longer exists and therefore can never release the resource). Knowing WHICH resources are liable to be locked in other threads makes it easier to reason about which generically non-async-safe functions can be used if we make other limiting restrictions (such as glibc's 'MT-Safe env' designation), but that requires more thought than blindly avoiding all non-async-safe functions.
It's killing me to see my work turn out as waste, but I don't want it to be included (and to present maintenance burden) just for "saving" the work. I kind of expected it to be unwelcome (due to it being opinionated and not fixing acute symptoms); I didn't expect it to be lost from the specification / premise up. This early paragraph in the commit message is torpedoed: However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be. That "and so" implication is busted because the glibc manual (from GNU, not the Linux manual pages!) *implies* that fork(), as opposed to _Fork(), does not restrict the child process to AS-Safe functions. Anyway, I think your characterization is right. My approach was, "avoid calling anything that's not explicitly async-signal-safe". A laxer approach is "avoid calling anything that might interfere with particular resources". Unfortunately, this laxer approach, while it may require some "further caution" in case we want to call further functions in the affected context, quite summarily obviates this particular execvp() replacement. Plus, regarding said "further caution" in general, I'm getting the impression (from RHBZ#906468) that glibc's intent is actually the opposite -- i.e., the intent seems to be that any particular API should opt out of, rather than opt in to, usability after fork(). Considering how much work is still needed to address the review feedback thus far (and the necessary v4 reviews!), I'm seriously torn if we should just drop the whole thing. (And there's a really sore lesson for me in this, regarding cleanups for standards conformance.)
Can you please elaborate on "+s"? (I'd like to understand your point regardless of this patch, too.)
https://www.austingroupbugs.net/view.php?id=1440 is where it came up in the Austin Group. Basically, having system("+s") be _required_ to invoke ["/path/to/sh", "-c", "+s"] is risky - "+s" is ambiguous between being the name of a real script and being a shell option
So what does the "+s" option do specifically? "-s" makes the shell read commands from stdin, but "+s" would only apply to such an "-s" option that were taken by the "set" special builtin -- it would *disable* that set-option (while "set -s" would enable it). However, I don't see *any* "-s" under "set", so I don't know what "set +s" would do.
(POSIX already warns that naming an executable with a leading "-" is unwise because of potential conflicts with options, but 'sh' has the special rules that + as well as - can introduce options for historical reasons).
So is "+s" just a synonym for "-s", meaning the shell will read commands from stdin?
Calling ["/path/to/sh", "-c", "--", "+s"] is unambiguous for a POSIX sh that understands "--" as the end-of-options marker, but not all historical sh did that. It is also possible to use ["/path/to/sh", "-c", " +s"] (note the added leading space, which is ignored by the shell),
ignored why?
but that requires space for injecting the leading space, and how do you do that concisely while still maintaining async-safety?
(right, not doing it, just learning about +s)
(... I wanted to say that "there's no replacement for reading POSIX in full", but now I'm wondering if reading POSIX *at all* makes sense...)
Alas, this rings too close to home... Standards are only useful if they are likely to be followed, which in turn is harder if they are hard to read.
"Hard to read" is a problem, but not the core issue IMO here. The problem is that for the sake of various technological advances, POSIX is intentionally abandoned, and that leaves us with *zero* common documentation that might uniformly cover even a small handful of modern OSes. And we have no alternative to individually testing out every "standard" API on each platform of interest. I have no problem with prctl() being Linux-specific; I'm very much irritated by fork() relaxing its standard restrictions without proper documentation. It's a recipe for wasting work. Anyway I'll log out now because I'm hardly coherent. Laszlo
On Wed, Feb 22, 2023 at 01:47:02AM +0100, Laszlo Ersek wrote:
On 2/21/23 20:32, Eric Blake wrote:
On Tue, Feb 21, 2023 at 07:07:38PM +0100, Laszlo Ersek wrote:
Well, given Daniel's comments meanwhile, it seems like the original execvp() is something we shouldn't fret about. :/
glibc marks execvp() and exevpe() as 'MT-Safe env', which means it does not modify 'environ' and presumably does not use 'malloc'. If the only reason POSIX marks execvp() as thread-unsafe is because of its interaction with getenv() for PATH and therefore unsafe to exec a child in one thread while another is calling putenv(), then it is no worse than our use of getenv() without locking on the grounds that no sane app will be doing setenv() after spawning threads.
But you've gone to a lot of work on this series; I'm still in favor of including your work, even if decide our premise behind it is weaker than intended. As I understood it, the premise is that actively avoiding as many non-async-safe functions as possible between fork() and exec() is always a good idea to avoid the deadlock created when a multithreaded process fork()s in one thread while another thread holds a mutex on the non-async-safe resource (in the child process, the other thread no longer exists and therefore can never release the resource). Knowing WHICH resources are liable to be locked in other threads makes it easier to reason about which generically non-async-safe functions can be used if we make other limiting restrictions (such as glibc's 'MT-Safe env' designation), but that requires more thought than blindly avoiding all non-async-safe functions.
It's killing me to see my work turn out as waste, but I don't want it to be included (and to present maintenance burden) just for "saving" the work. I kind of expected it to be unwelcome (due to it being opinionated and not fixing acute symptoms); I didn't expect it to be lost from the specification / premise up. This early paragraph in the commit message is torpedoed:
And even with your advance communication that you did not want to work on something that might be deemed overkill, I was not expecting the reaction in this thread to turn out in quite the way it has. I sincerely hope that whether or not your execvp replacement code lands, you do not consider time spent on writing/reviewing it as wasted, as we are all learning things in this thread.
However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be.
That "and so" implication is busted because the glibc manual (from GNU, not the Linux manual pages!) *implies* that fork(), as opposed to _Fork(), does not restrict the child process to AS-Safe functions.
Or that glibc has taken special pains to make reliance on certain AS-unsafe functions (namely, malloc() and getenv()) "safer" in practice, despite permission from POSIX allowing glibc to throw caution to the wind and claim deadlocks to be the progammer's bug rather than glibc's. The fact that documentation does not match reality is indeed disheartening, but this may be a good time to open up bug requests to the documentation team(s) that can improve it for the next reader. Deadlock avoidance is one of the harder topics in computer science; I'm glad that glibc is going out of their way to make fork()/exec() work in more cases than promised by POSIX, but it also means a lot of programmers don't realize the complexities that glibc is solving on their behalf, and how their code may not be portable to a lesser libc.
Anyway, I think your characterization is right. My approach was, "avoid calling anything that's not explicitly async-signal-safe". A laxer approach is "avoid calling anything that might interfere with particular resources".
Unfortunately, this laxer approach, while it may require some "further caution" in case we want to call further functions in the affected context, quite summarily obviates this particular execvp() replacement.
Plus, regarding said "further caution" in general, I'm getting the impression (from RHBZ#906468) that glibc's intent is actually the opposite -- i.e., the intent seems to be that any particular API should opt out of, rather than opt in to, usability after fork().
Considering how much work is still needed to address the review feedback thus far (and the necessary v4 reviews!), I'm seriously torn if we should just drop the whole thing. (And there's a really sore lesson for me in this, regarding cleanups for standards conformance.)
Can you please elaborate on "+s"? (I'd like to understand your point regardless of this patch, too.)
https://www.austingroupbugs.net/view.php?id=1440 is where it came up in the Austin Group. Basically, having system("+s") be _required_ to invoke ["/path/to/sh", "-c", "+s"] is risky - "+s" is ambiguous between being the name of a real script and being a shell option
So what does the "+s" option do specifically?
"-s" makes the shell read commands from stdin, but "+s" would only apply to such an "-s" option that were taken by the "set" special builtin -- it would *disable* that set-option (while "set -s" would enable it). However, I don't see *any* "-s" under "set", so I don't know what "set +s" would do.
POSIX doesn't define 'set -s', and in fact, bash treats 'set -s' and 'set +s' as errors. But sh itself DOES have a -s option (mandated by POSIX), and POSIX also mandates that shells must accept options with a leading '+'. So in practice, what happens is that both bash and dash _happen_ to parse and silently ignore '+s' as an unknown option when passed as a command line argument under the invocation name 'sh', since they already have to do similar command-line parsing for 'sh -a' vs. 'sh +a'. Maybe the problem is more readily visible when using '+a' as a script name, because that's something that POSIX does require shells to support. If I write system("+a") and have an executable by that name on my PATH, I want to execute that program, not get an error about the shell not having an argument to -c. And if I don't have it on my PATH, I want to be told that. But my initial example used "+s" instead of "+a" because that was the example discussed in the Austin Group (and I don't know why the original poster in that context picked that as their example). $ /bin/bash -c +a; echo $? /bin/bash: -c: option requires an argument 2 $ /bin/bash -c -- +a; echo $? /bin/bash: line 1: +a: command not found 127 $ /bin/dash -c +a; echo $? /bin/dash: 0: -c requires an argument 2 $ /bin/dash -c -- +a; echo $? /bin/dash: 1: +a: not found 127
(POSIX already warns that naming an executable with a leading "-" is unwise because of potential conflicts with options, but 'sh' has the special rules that + as well as - can introduce options for historical reasons).
So is "+s" just a synonym for "-s", meaning the shell will read commands from stdin?
As far as I can tell, neither bash nor dash documents +s behavior; it is probably more likely an accident of implementation than something intentional.
Calling ["/path/to/sh", "-c", "--", "+s"] is unambiguous for a POSIX sh that understands "--" as the end-of-options marker, but not all historical sh did that. It is also possible to use ["/path/to/sh", "-c", " +s"] (note the added leading space, which is ignored by the shell),
ignored why?
The string argument present when -c is in use parses the entire string (in this case, " +s") as a command line to run, and command lines ignore leading space characters. The problem with bare "+s" is that it is potentially ambiguous as an option name (at which point -c fails when there is no matching string), while " +s" is unambiguously not an option and therefore can only be the contents of the script that -c asked to run, at which point the leading space in the string no longer matters, having gotten us past the option-parsing ambiguity.
but that requires space for injecting the leading space, and how do you do that concisely while still maintaining async-safety?
(right, not doing it, just learning about +s)
(... I wanted to say that "there's no replacement for reading POSIX in full", but now I'm wondering if reading POSIX *at all* makes sense...)
Alas, this rings too close to home... Standards are only useful if they are likely to be followed, which in turn is harder if they are hard to read.
"Hard to read" is a problem, but not the core issue IMO here. The problem is that for the sake of various technological advances, POSIX is intentionally abandoned, and that leaves us with *zero* common documentation that might uniformly cover even a small handful of modern OSes. And we have no alternative to individually testing out every "standard" API on each platform of interest. I have no problem with prctl() being Linux-specific; I'm very much irritated by fork() relaxing its standard restrictions without proper documentation. It's a recipe for wasting work.
Anyway I'll log out now because I'm hardly coherent.
There's no rush here; I understand that this effort has been emotionally draining on you, so please take the time you need to get back into the right mental frame when following up. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/22/23 22:50, Eric Blake wrote:
Or that glibc has taken special pains to make reliance on certain AS-unsafe functions (namely, malloc() and getenv()) "safer" in practice, despite permission from POSIX allowing glibc to throw caution to the wind and claim deadlocks to be the progammer's bug rather than glibc's. The fact that documentation does not match reality is indeed disheartening, but this may be a good time to open up bug requests to the documentation team(s) that can improve it for the next reader.
Deadlock avoidance is one of the harder topics in computer science; I'm glad that glibc is going out of their way to make fork()/exec() work in more cases than promised by POSIX, but it also means a lot of programmers don't realize the complexities that glibc is solving on their behalf, and how their code may not be portable to a lesser libc.
I've filed: https://bugzilla.redhat.com/show_bug.cgi?id=2172869
https://www.austingroupbugs.net/view.php?id=1440 is where it came up in the Austin Group. Basically, having system("+s") be _required_ to invoke ["/path/to/sh", "-c", "+s"] is risky - "+s" is ambiguous between being the name of a real script and being a shell option
[...] OK, now I understand the point about the particular invocation being "required" versus just "permitted". If it gets relaxed to "permitted", then implementations may (but do not have to) improve their system() and similar functions to insert "--". An implementation that did not react to such a potential relaxation of requirements would not become non-conforming, so this is effectively about accommodating a wider range of Quality of Implementation. I think I'll stick with the execvpe() patch as it is, for the time being. Thanks! Laszlo
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
- it performs PATH search [02] if necessary,
- it falls back to executing the file with the shell as a shell script in case the underlying execv() or execve() fails with ENOEXEC.
However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be.
Is that actually a problem in the real world ? There are various things not technically listed as async-signal-safe by POSIX, which all sane impls none the less make safe, or are practically safe in any sane program using them. IIUC with execvp the risk would be that setenv makes any use of the environment potentially unsafe, and possibly execvp might use malloc which is also technically unsafe. Both of these factors would technically make your replacement unsafe too. Apps simply shouldn't ever call setenv once threads are created, and malloc() is safe in any impl that is relevant these days.
Introduce three APIs for safely emulating execvp() with execve():
- nbd_internal_execvpe_init(): to be called in the parent process, before fork(). Allocate and format candidate pathnames for execution with execve(), based on PATH. Allocate a buffer for the longer command line that the shell script fallback needs.
- nbd_internal_fork_safe_execvpe(): to be called in the child process. Try the candidates formatted previously in sequence with execve(), as long as execve() fails with errors related to pathname resolution / inode-level file type / execution permission. Stop iterating on fatal errors; if the fatal error is ENOEXEC, activate the shell script fallback. As a small added feature, take the environment from an explicit parameter (effectively imitating the GNU-specific execvpe() function -- cf. commit dc64ac5cdd0b, "states: Use execvp instead of execvpe", 2019-11-15).
- nbd_internal_execvpe_uninit(): to be called in the parent process, after fork(). Release the resources allocated with nbd_internal_execvpe_init().
The main idea with the above distribution of responsibilities is that any pre-scanning of PATH -- which was suggested by Eric Blake -- should not include activities that are performed by execve() anyway. I've considered and abandoned two approaches:
- During scanning (i.e., in nbd_internal_execvpe_init()), check each candidate with access(X_OK) [05].
I dropped this because the informative APPLICATION USAGE section of the same specification [06] advises against it, saying "[a]n application should instead attempt the action itself and handle the [EACCES] error that occurs if the file is not accessible".
- During scanning, open each candidate with O_EXEC [07] until one open() succeeds. Save the sole file descriptor for nbd_internal_fork_safe_execvpe(). In the latter, call fexecve() [08], which is free of all error conditions that necessitate iteration over candidates. Still implement the ENOEXEC (shell script) fallback.
I dropped this because (a) fexecve() is a quite recent interface (highlighted by Eric); (b) for an application to open a file for execution with fexecve(), it's more robust to pass O_EXEC to open() than not to pass it, per APPLICATION USAGE [09], but on Linux/glibc, O_EXEC does not seem supported, only O_PATH does [10].
Thus the chosen approach -- pre-generate filenames -- contains a small TOCTTOU race (highlighted by Eric) after all, but it should be harmless.
Implementation-defined details:
- If PATH searching is necessary, but PATH is absent [01], then fall back to confstr(_CS_PATH) [11], similarly to Linux/glibc execvpe() [12].
If PATH is set but empty ("set to null") [02], or PATH is unset and confstr(_CS_PATH) fails or returns no information or returns the empty string, we fail the initial scanning (!) with ENOENT. This is consistent with bash's behavior on Linux/glibc:
$ PATH= ls bash: ls: No such file or directory
Details chosen for unspecified behavior:
- Hardwire "/bin/sh" as the shell's pathname [01].
[01] https://pubs.opengroup.org/onlinepubs/9699919799/functions/execvp.html [02] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag... [03] https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#ta... [04] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html [05] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html [06] https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html#tag_1... [07] https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html [08] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html [09] https://pubs.opengroup.org/onlinepubs/9699919799/functions/fexecve.html#tag_... [10] https://man7.org/linux/man-pages/man2/open.2.html [11] https://pubs.opengroup.org/onlinepubs/9699919799/functions/confstr.html [12] https://man7.org/linux/man-pages/man3/execvpe.3.html
Suggested-by: Eric Blake <eblake@redhat.com> Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 22 ++ lib/utils.c | 351 ++++++++++++++++++++ 2 files changed, 373 insertions(+)
diff --git a/lib/internal.h b/lib/internal.h index 9c9021ed366e..8c4e32013e40 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -368,6 +368,18 @@ struct command { uint32_t error; /* Local errno value */ };
+struct execvpe { + string_vector pathnames; + + /* Note: "const_string_vector" is not a good type for "sh_argv" below. Even if + * we reserved enough space in a "const_string_vector", + * const_string_vector_append() would still not be async-signal-safe, due to + * the underlying const_string_vector_insert() calling assert(). + */ + char **sh_argv; + size_t num_sh_args; +}; + /* Test if a callback is "null" or not, and set it to null. */ #define CALLBACK_IS_NULL(cb) ((cb).callback == NULL && (cb).free == NULL) #define CALLBACK_IS_NOT_NULL(cb) (! CALLBACK_IS_NULL ((cb))) @@ -540,4 +552,14 @@ extern void nbd_internal_fork_safe_assert (int result, const char *file, __func__, #expression)) #endif
+extern int nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args) + LIBNBD_ATTRIBUTE_NONNULL (1, 2); +extern void nbd_internal_execvpe_uninit (struct execvpe *ctx) + LIBNBD_ATTRIBUTE_NONNULL (1); +extern int nbd_internal_fork_safe_execvpe (struct execvpe *ctx, + const string_vector *argv, + char * const *envp) + LIBNBD_ATTRIBUTE_NONNULL (1, 2, 3); + #endif /* LIBNBD_INTERNAL_H */ diff --git a/lib/utils.c b/lib/utils.c index 5dd00f82e073..9a96ed6ed674 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -28,6 +28,7 @@ #include <limits.h>
#include "minmax.h" +#include "checked-overflow.h"
#include "internal.h"
@@ -463,3 +464,353 @@ nbd_internal_fork_safe_assert (int result, const char *file, long line, xwrite (STDERR_FILENO, "' failed.\n", 10); abort (); } + +/* Returns the value of the PATH environment variable -- falling back to + * confstr(_CS_PATH) if PATH is absent -- as a dynamically allocated string. On + * failure, sets "errno" and returns NULL. + */ +static char * +get_path (void) + LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free) +{ + char *path; + bool env_path_found; + size_t path_size, path_size2; + + /* FIXME: lock the environment. */ + path = getenv ("PATH"); + if ((env_path_found = (path != NULL))) + path = strdup (path); + /* FIXME: unlock the environment. */ + + if (env_path_found) { + /* This handles out-of-memory as well. */ + return path; + } + + errno = 0; + path_size = confstr (_CS_PATH, NULL, 0); + if (path_size == 0) { + /* If _CS_PATH does not have a configuration-defined value, just store + * ENOENT to "errno". + */ + if (errno == 0) + errno = ENOENT; + + return NULL; + } + + path = malloc (path_size); + if (path == NULL) + return NULL; + + path_size2 = confstr (_CS_PATH, path, path_size); + assert (path_size2 == path_size); + return path; +} + +/* nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe() together + * present an execvp() alternative that is async-signal-safe. + * + * nbd_internal_execvpe_init() may only be called before fork(), for filling in + * the caller-allocated, uninitialized "ctx" structure. If + * nbd_internal_execvpe_init() succeeds, then fork() may be called. + * Subsequently, in the child process, nbd_internal_fork_safe_execvpe() may be + * called with the inherited "ctx" structure, while in the parent process, + * nbd_internal_execvpe_uninit() must be called to uninitialize (evacuate) the + * "ctx" structure. + * + * On failure, "ctx" will not have been modified, "errno" is set, and -1 is + * returned. Failures include: + * + * - Errors forwarded from underlying functions such as strdup(), confstr(), + * malloc(), string_vector_append(). + * + * - ENOENT: "file" is an empty string. + * + * - ENOENT: "file" does not contain a <slash> "/" character, the PATH + * environment variable is not set, and confstr() doesn't associate a + * configuration-defined value with _CS_PATH. + * + * - ENOENT: "file" does not contain a <slash> "/" character, and: (a) the PATH + * environment variable is set to the empty string, or (b) PATH is not + * set, and confstr() outputs the empty string for _CS_PATH. + * + * - EOVERFLOW: the sizes or counts of necessary objects could not be expressed. + * + * - EINVAL: "num_args" is less than 2. + * + * On success, the "ctx" structure will have been filled in, and 0 is returned. + * + * - "pathnames" member: + * + * - All strings pointed-to by elements of the "pathnames" string_vector + * member are owned by "pathnames". + * + * - If "file" contains a <slash> "/" character, then the sole entry in + * "pathnames" is a copy of "file". + * + * - If "file" does not contain a <slash> "/" character: + * + * Let "system path" be defined as the value of the PATH environment + * variable, if the latter exists, and as the value output by confstr() for + * _CS_PATH otherwise. Per the ENOENT specifications above, "system path" is + * a non-empty string. Let "system path" further be of the form + * + * <prefix_0> [n = 1] + * + * or + * + * <prefix_0>:<prefix_1>:...:<prefix_(n-1)> [n >= 2] + * + * where for each 0 <= i < n, <prefix_i> does not contain the <colon> ":" + * character. In the (n = 1) case, <prefix_0> is never empty (see ENOENT + * above), while in the (n >= 2) case, any individual <prefix_i> may or may + * not be empty. + * + * The "pathnames" string_vector member has n elements; for each 0 <= i < n, + * element i is of the form + * + * suffix(curdir(<prefix_i>))file + * + * where + * + * curdir(x) := "." if x = "" + * curdir(x) := x otherwise + * + * and + * + * suffix(x) := x if "x" ends with a <slash> "/" + * suffix(x) := x "/" otherwise + * + * This transformation implements the POSIX XBD / PATH environment variable + * semantics, creating candidate pathnames for execution by + * nbd_internal_fork_safe_execvpe(). nbd_internal_fork_safe_execvpe() will + * iterate over the candidate pathnames with execve() until execve() + * succeeds, or fails with an error that is due to neither pathname + * resolution, nor the candidate not being a regular file, nor the candidate + * lacking execution permission. + * + * - The "sh_argv" array member will have at least (num_args + 1) elements + * allocated, and none populated. + * + * (The minimum value of "num_args" is 2 -- see EINVAL above. According to + * POSIX, "[t]he argument /arg0/ should point to a filename string that is + * associated with the process being started by one of the /exec/ functions", + * plus "num_args" includes the null pointer that terminates the argument + * list.) + * + * This allocation is made in anticipation of execve() failing for a + * particular candidate inside nbd_internal_fork_safe_execvpe() with ENOEXEC + * ("[t]he new process image file has the appropriate access permission but + * has an unrecognized format"). While that failure terminates the iteration, + * the failed call + * + * execve (pathnames[i], + * { argv[0], argv[1], ..., NULL }, // (num_args >= 2) elements + * { envp[0], envp[1], ..., NULL }) + * + * must be repeated as + * + * execve (<shell-path>, + * { argv[0], pathnames[i], // ((num_args + 1) >= 3) + argv[1], ..., NULL }, // elements + * { envp[0], envp[1], ..., NULL }) + * + * for emulating execvp(). The allocation in the "sh_argv" member makes it + * possible just to *link* the original "argv" elements and the "pathnames[i]" + * candidate into the right positions. + * + * (POSIX leaves the shell pathname unspecified; "/bin/sh" should be good + * enough.) + * + * The shell *binary* will see itself being executed under the name "argv[0]", + * will receive "pathnames[i]" as the pathname of the shell *script* to read + * and interpret ("command_file" in POSIX terminology), will expose + * "pathnames[i]" as the positional parameter $0 to the script, and will + * forward "argv[1]" and the rest to the script as positional parameters $1 + * and onward. + */ +int +nbd_internal_execvpe_init (struct execvpe *ctx, const char *file, + size_t num_args) +{ + int rc; + char *sys_path; + string_vector pathnames; + char *pathname; + size_t num_sh_args; + char **sh_argv; + size_t sh_argv_bytes; + + rc = -1; + + if (file[0] == '\0') { + errno = ENOENT; + return rc; + } + + /* First phase. */ + sys_path = NULL; + pathnames = (string_vector)empty_vector; + + if (strchr (file, '/') == NULL) { + size_t file_len; + const char *sys_path_element, *scan; + bool finish; + + sys_path = get_path (); + if (sys_path == NULL) + return rc; + + if (sys_path[0] == '\0') { + errno = ENOENT; + goto free_sys_path; + } + + pathname = NULL; + file_len = strlen (file); + sys_path_element = sys_path; + scan = sys_path; + do { + assert (sys_path_element <= scan); + finish = (*scan == '\0'); + if (finish || *scan == ':') { + const char *sys_path_copy_start; + size_t sys_path_copy_size; + size_t sep_copy_size; + size_t pathname_size; + char *p; + + if (scan == sys_path_element) { + sys_path_copy_start = "."; + sys_path_copy_size = 1; + } else { + sys_path_copy_start = sys_path_element; + sys_path_copy_size = scan - sys_path_element; + } + + assert (sys_path_copy_size >= 1); + sep_copy_size = (sys_path_copy_start[sys_path_copy_size - 1] != '/'); + + if (ADD_OVERFLOW (sys_path_copy_size, sep_copy_size, &pathname_size) || + ADD_OVERFLOW (pathname_size, file_len, &pathname_size) || + ADD_OVERFLOW (pathname_size, 1u, &pathname_size)) { + errno = EOVERFLOW; + goto empty_pathnames; + } + + pathname = malloc (pathname_size); + if (pathname == NULL) + goto empty_pathnames; + p = pathname; + + memcpy (p, sys_path_copy_start, sys_path_copy_size); + p += sys_path_copy_size; + + memcpy (p, "/", sep_copy_size); + p += sep_copy_size; + + memcpy (p, file, file_len); + p += file_len; + + *p++ = '\0'; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + + sys_path_element = scan + 1; + } + + ++scan; + } while (!finish); + } else { + pathname = strdup (file); + if (pathname == NULL) + return rc; + + if (string_vector_append (&pathnames, pathname) == -1) + goto empty_pathnames; + /* Ownership transferred. */ + pathname = NULL; + } + + /* Second phase. */ + if (num_args < 2) { + errno = EINVAL; + goto empty_pathnames; + } + if (ADD_OVERFLOW (num_args, 1u, &num_sh_args) || + MUL_OVERFLOW (num_sh_args, sizeof *sh_argv, &sh_argv_bytes)) { + errno = EOVERFLOW; + goto empty_pathnames; + } + sh_argv = malloc (sh_argv_bytes); + if (sh_argv == NULL) + goto empty_pathnames; + + /* Commit. */ + ctx->pathnames = pathnames; + ctx->sh_argv = sh_argv; + ctx->num_sh_args = num_sh_args; + rc = 0; + /* Fall through, for freeing temporaries. */ + +empty_pathnames: + if (rc == -1) { + free (pathname); + string_vector_empty (&pathnames); + } + +free_sys_path: + free (sys_path); + + return rc; +} + +void +nbd_internal_execvpe_uninit (struct execvpe *ctx) +{ + free (ctx->sh_argv); + ctx->num_sh_args = 0; + string_vector_empty (&ctx->pathnames); +} + +int +nbd_internal_fork_safe_execvpe (struct execvpe *ctx, const string_vector *argv, + char * const *envp) +{ + size_t pathname_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->pathnames.len > 0); + + pathname_idx = 0; + do { + (void)execve (ctx->pathnames.ptr[pathname_idx], argv->ptr, envp); + if (errno != EACCES && errno != ELOOP && errno != ENAMETOOLONG && + errno != ENOENT && errno != ENOTDIR) + break; + + ++pathname_idx; + } while (pathname_idx < ctx->pathnames.len); + + if (errno == ENOEXEC) { + char **sh_argp; + size_t argv_idx; + + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args >= argv->len); + NBD_INTERNAL_FORK_SAFE_ASSERT (ctx->num_sh_args - argv->len == 1); + + sh_argp = ctx->sh_argv; + *sh_argp++ = argv->ptr[0]; + *sh_argp++ = ctx->pathnames.ptr[pathname_idx]; + for (argv_idx = 1; argv_idx < argv->len; ++argv_idx) + *sh_argp++ = argv->ptr[argv_idx]; + + (void)execve ("/bin/sh", ctx->sh_argv, envp); + } + + return -1; +}
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On 2/21/23 13:08, Daniel P. Berrangé wrote:
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
- it performs PATH search [02] if necessary,
- it falls back to executing the file with the shell as a shell script in case the underlying execv() or execve() fails with ENOEXEC.
However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be.
Is that actually a problem in the real world ? There are various things not technically listed as async-signal-safe by POSIX, which all sane impls none the less make safe, or are practically safe in any sane program using them.
By my count, it's virtually impossible to implement exec*p*() and friends without calling malloc() internally. That's the case even if you can get PATH in there without any problems. In turn, malloc() relies on libc-internal mutex(es) or other synchronization mechanisms, so that multiple threads can call malloc() / free() etc simultaneously, for managing the one shared address space of the process. The problem is with calling fork() in a multi-threaded process. The child process created by fork() only has one thread -- the thread returning from fork(). All other threads simply "disappear", as viewed from the child process's perspective. This means that, if one of those "other" (disappearing) threads was in the middle of malloc(), holding for example a mutex, then in the child process, that mutex will appear as acquired, but no thread will own it -- so no thread will ever release it. Then, when the one thread returning from fork() in the child process calls exec*p*(), the malloc() call internal to exec*p*() will deadlock on that mutex. Rich mentioned that libnbd had actually encountered a bug of this kind, just not specifically in exec*p*(). So the "async-signal-safe" term is a bit misleading here; I think what POSIX actually means is a kind of general reentrancy. Anyway, the POSIX language does use "async-signal-safe" when discussing this topic of threads, in the fork() specification. Now, the exec*p*() manual at https://man7.org/linux/man-pages/man3/execvp.3.html calls execlp(), execvp(), execvpe() "MT-Safe env", so you are right about at least Linux/glibc. I've (intentionally) not looked at the glibc implementation of execvp(), but I agree that, *if* we are happy with getenv() just because the linux/glibc manual calls it "MT-Safe env", *then* we could arguably be satisfied with execvp() as well. I don't know if/how that applies to FreeBSD / OpenBSD though. https://man.freebsd.org/cgi/man.cgi?query=execvp https://man.openbsd.org/execvp.3 These man pages do not seem to contain any of the strings "mt", "async", "signal", "safe", "thread".
IIUC with execvp the risk would be that setenv makes any use of the environment potentially unsafe,
to a smaller extent, yes
and possibly execvp might use malloc which is also technically unsafe.
primarily this one, yes
Both of these factors would technically make your replacement unsafe too.
No, they wouldn't. That's the whole point. Both getenv() and malloc() are restricted to the _init API, which is called in the parent process, before fork(). The necessary information is prepared in a context structure, which the child only inherits. After fork(), the child calls a second API -- the effective execvpe() replacement -- which operates on the inherited context structure. The only functions called from this second API are write() and abort() -- indirectly, in case the new async-signal-safe assert() variant fails --, and execve(). All three of these functions are async-signal-safe.
Apps simply shouldn't ever call setenv once threads are created, and malloc() is safe in any impl that is relevant these days.
FWIW, the linux/glibc manual states, in <https://man7.org/linux/man-pages/man2/fork.2.html>:
* After a fork() in a multithreaded program, the child can safely call only async-signal-safe functions (see signal-safety(7)) until such time as it calls execve(2).
and <https://man7.org/linux/man-pages/man7/signal-safety.7.html> does not list either execvp() or malloc(). The glibc manual is more verbose <https://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html>:
The _Fork function is similar to fork, but it does not invoke any callbacks registered with pthread_atfork, nor does it reset any internal state or locks (such as the malloc locks). In the new subprocess, only async-signal-safe functions may be called, such as dup2 or execve.
This implies that malloc() and fork() in glibc actually inter-operate, so that fork() -- not _Fork() -- "reset malloc locks". This confirms your point about latest glibc, but I don't know if/how the same applies to the BSDs, or to earlier versions of glibc (for example the one in RHEL7, which libnbd still cares about, IIUC). (I'm unsure if it's wise for me to dig into the glibc commit history with git-blame, for finding the earliest release that adds this extra safety beyond POSIX.) Laszlo
On Tue, Feb 21, 2023 at 05:03:23PM +0100, Laszlo Ersek wrote:
On 2/21/23 13:08, Daniel P. Berrangé wrote:
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
- it performs PATH search [02] if necessary,
- it falls back to executing the file with the shell as a shell script in case the underlying execv() or execve() fails with ENOEXEC.
However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be.
Is that actually a problem in the real world ? There are various things not technically listed as async-signal-safe by POSIX, which all sane impls none the less make safe, or are practically safe in any sane program using them.
By my count, it's virtually impossible to implement exec*p*() and friends without calling malloc() internally. That's the case even if you can get PATH in there without any problems. In turn, malloc() relies on libc-internal mutex(es) or other synchronization mechanisms, so that multiple threads can call malloc() / free() etc simultaneously, for managing the one shared address space of the process.
The problem is with calling fork() in a multi-threaded process. The child process created by fork() only has one thread -- the thread returning from fork(). All other threads simply "disappear", as viewed from the child process's perspective. This means that, if one of those "other" (disappearing) threads was in the middle of malloc(), holding for example a mutex, then in the child process, that mutex will appear as acquired, but no thread will own it -- so no thread will ever release it. Then, when the one thread returning from fork() in the child process calls exec*p*(), the malloc() call internal to exec*p*() will deadlock on that mutex.
Rich mentioned that libnbd had actually encountered a bug of this kind, just not specifically in exec*p*().
So the "async-signal-safe" term is a bit misleading here; I think what POSIX actually means is a kind of general reentrancy. Anyway, the POSIX language does use "async-signal-safe" when discussing this topic of threads, in the fork() specification.
Now, the exec*p*() manual at
https://man7.org/linux/man-pages/man3/execvp.3.html
calls execlp(), execvp(), execvpe() "MT-Safe env", so you are right about at least Linux/glibc. I've (intentionally) not looked at the glibc implementation of execvp(), but I agree that, *if* we are happy with getenv() just because the linux/glibc manual calls it "MT-Safe env", *then* we could arguably be satisfied with execvp() as well.
I don't know if/how that applies to FreeBSD / OpenBSD though.
https://man.freebsd.org/cgi/man.cgi?query=execvp https://man.openbsd.org/execvp.3
These man pages do not seem to contain any of the strings "mt", "async", "signal", "safe", "thread".
IIUC with execvp the risk would be that setenv makes any use of the environment potentially unsafe,
to a smaller extent, yes
and possibly execvp might use malloc which is also technically unsafe.
primarily this one, yes
Both of these factors would technically make your replacement unsafe too.
No, they wouldn't. That's the whole point. Both getenv() and malloc() are restricted to the _init API, which is called in the parent process, before fork(). The necessary information is prepared in a context structure, which the child only inherits. After fork(), the child calls a second API -- the effective execvpe() replacement -- which operates on the inherited context structure. The only functions called from this second API are write() and abort() -- indirectly, in case the new async-signal-safe assert() variant fails --, and execve(). All three of these functions are async-signal-safe.
Oh I missed that subtlety.
Apps simply shouldn't ever call setenv once threads are created, and malloc() is safe in any impl that is relevant these days.
FWIW, the linux/glibc manual states, in <https://man7.org/linux/man-pages/man2/fork.2.html>:
* After a fork() in a multithreaded program, the child can safely call only async-signal-safe functions (see signal-safety(7)) until such time as it calls execve(2).
and <https://man7.org/linux/man-pages/man7/signal-safety.7.html> does not list either execvp() or malloc().
The glibc manual is more verbose <https://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html>:
Yes, the docs are written by language lawyers, but the reality is that malloc is safe in glibc for 15+ years and that's not going to change.
The _Fork function is similar to fork, but it does not invoke any callbacks registered with pthread_atfork, nor does it reset any internal state or locks (such as the malloc locks). In the new subprocess, only async-signal-safe functions may be called, such as dup2 or execve.
This implies that malloc() and fork() in glibc actually inter-operate, so that fork() -- not _Fork() -- "reset malloc locks".
This confirms your point about latest glibc, but I don't know if/how the same applies to the BSDs, or to earlier versions of glibc (for example the one in RHEL7, which libnbd still cares about, IIUC).
macOS, Linux glib, Linux musl, FreeBSD are all malloc safe after fork with threads. Windows doesn't have fork. I'm fairly certain that I validated the OpenBSD/NetBSD malloc impl too, but it was a while ago. Every single non-trivial program today uses threads, so you can more or less guarantee that malloc is going to be used between fork & exec on a app that uses threads. Any platform that doesn't make malloc safe is going to be on the receiving end of many bug reports. Musl tried to be a hold out in this respect, refusing to make their malloc safe, but eventually they just accepted the modern reality and made it safe. IMHO it just isn't worth spending time avoiding malloc between fork/exec, and apps which call setenv()/unsetenv from outside their initial main() method are almost certainly already broken, because so many library APIs create threads behind your back. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On 2/21/23 17:16, Daniel P. Berrangé wrote:
On Tue, Feb 21, 2023 at 05:03:23PM +0100, Laszlo Ersek wrote:
On 2/21/23 13:08, Daniel P. Berrangé wrote:
On Wed, Feb 15, 2023 at 03:11:38PM +0100, Laszlo Ersek wrote:
execvp() [01] is powerful:
- it performs PATH search [02] if necessary,
- it falls back to executing the file with the shell as a shell script in case the underlying execv() or execve() fails with ENOEXEC.
However, execvp() is not async-signal-safe [03], and so we shouldn't call it in a child process forked from a multi-threaded parent process [04], which the libnbd client application may well be.
Is that actually a problem in the real world ? There are various things not technically listed as async-signal-safe by POSIX, which all sane impls none the less make safe, or are practically safe in any sane program using them.
By my count, it's virtually impossible to implement exec*p*() and friends without calling malloc() internally. That's the case even if you can get PATH in there without any problems. In turn, malloc() relies on libc-internal mutex(es) or other synchronization mechanisms, so that multiple threads can call malloc() / free() etc simultaneously, for managing the one shared address space of the process.
The problem is with calling fork() in a multi-threaded process. The child process created by fork() only has one thread -- the thread returning from fork(). All other threads simply "disappear", as viewed from the child process's perspective. This means that, if one of those "other" (disappearing) threads was in the middle of malloc(), holding for example a mutex, then in the child process, that mutex will appear as acquired, but no thread will own it -- so no thread will ever release it. Then, when the one thread returning from fork() in the child process calls exec*p*(), the malloc() call internal to exec*p*() will deadlock on that mutex.
Rich mentioned that libnbd had actually encountered a bug of this kind, just not specifically in exec*p*().
So the "async-signal-safe" term is a bit misleading here; I think what POSIX actually means is a kind of general reentrancy. Anyway, the POSIX language does use "async-signal-safe" when discussing this topic of threads, in the fork() specification.
Now, the exec*p*() manual at
https://man7.org/linux/man-pages/man3/execvp.3.html
calls execlp(), execvp(), execvpe() "MT-Safe env", so you are right about at least Linux/glibc. I've (intentionally) not looked at the glibc implementation of execvp(), but I agree that, *if* we are happy with getenv() just because the linux/glibc manual calls it "MT-Safe env", *then* we could arguably be satisfied with execvp() as well.
I don't know if/how that applies to FreeBSD / OpenBSD though.
https://man.freebsd.org/cgi/man.cgi?query=execvp https://man.openbsd.org/execvp.3
These man pages do not seem to contain any of the strings "mt", "async", "signal", "safe", "thread".
IIUC with execvp the risk would be that setenv makes any use of the environment potentially unsafe,
to a smaller extent, yes
and possibly execvp might use malloc which is also technically unsafe.
primarily this one, yes
Both of these factors would technically make your replacement unsafe too.
No, they wouldn't. That's the whole point. Both getenv() and malloc() are restricted to the _init API, which is called in the parent process, before fork(). The necessary information is prepared in a context structure, which the child only inherits. After fork(), the child calls a second API -- the effective execvpe() replacement -- which operates on the inherited context structure. The only functions called from this second API are write() and abort() -- indirectly, in case the new async-signal-safe assert() variant fails --, and execve(). All three of these functions are async-signal-safe.
Oh I missed that subtlety.
Apps simply shouldn't ever call setenv once threads are created, and malloc() is safe in any impl that is relevant these days.
FWIW, the linux/glibc manual states, in <https://man7.org/linux/man-pages/man2/fork.2.html>:
* After a fork() in a multithreaded program, the child can safely call only async-signal-safe functions (see signal-safety(7)) until such time as it calls execve(2).
and <https://man7.org/linux/man-pages/man7/signal-safety.7.html> does not list either execvp() or malloc().
The glibc manual is more verbose <https://www.gnu.org/software/libc/manual/html_node/Creating-a-Process.html>:
Yes, the docs are written by language lawyers, but the reality is that malloc is safe in glibc for 15+ years and that's not going to change.
The _Fork function is similar to fork, but it does not invoke any callbacks registered with pthread_atfork, nor does it reset any internal state or locks (such as the malloc locks). In the new subprocess, only async-signal-safe functions may be called, such as dup2 or execve.
This implies that malloc() and fork() in glibc actually inter-operate, so that fork() -- not _Fork() -- "reset malloc locks".
This confirms your point about latest glibc, but I don't know if/how the same applies to the BSDs, or to earlier versions of glibc (for example the one in RHEL7, which libnbd still cares about, IIUC).
macOS, Linux glib, Linux musl, FreeBSD are all malloc safe after fork with threads. Windows doesn't have fork. I'm fairly certain that I validated the OpenBSD/NetBSD malloc impl too, but it was a while ago.
Every single non-trivial program today uses threads, so you can more or less guarantee that malloc is going to be used between fork & exec on a app that uses threads. Any platform that doesn't make malloc safe is going to be on the receiving end of many bug reports. Musl tried to be a hold out in this respect, refusing to make their malloc safe, but eventually they just accepted the modern reality and made it safe.
IMHO it just isn't worth spending time avoiding malloc between fork/exec, and apps which call setenv()/unsetenv from outside their initial main() method are almost certainly already broken, because so many library APIs create threads behind your back.
I wish I had known this (i.e., about your research regarding multiple OSes) *before* starting this work. Unfortunately, the execvpe impl in this patch, and the unit tests in the next, required the majority of effort in this series. I'm not really seeing the point for the bulk of this series if we can simply deem -- and do so justifiedly -- the existent code as "good enough". Now, I did anticipate that most of this work could be perceived as needless pedantry, and I still decided to go ahead with it. So I can accept it if we drop the series (and return to Rich's v2 for extending the environment with LISTEN_FDNAMES). What I definitely wanted to achieve was for this series to reach the list, and that has been accomplished. What I *cannot* offer though myself is to go over the series and cherry-pick what patches make practical sense and what don't. To me, they all matter; I consider them all pre-requisites, because I approach the whole thing from a standards conformance background. If we drop that ("do whatever works"), that's fine, but then I can't help; more precisely, then there's no *need* for this series in the first place. I'm honestly at loss as to how to implement the last few patches *without* the cleanups -- but luckily, I don't need to figure that out, because Rich did it already in v2. Reviewing that, I asked for a chance to take a stab at the problem, so I'm good. More in general, this lesson tells me that POSIX is effectively irrelevant -- which is quite sad in itself; the bigger problem however is that *nothing replaces it*. If the one formal standard we have for portability does not reflect reality closely enough, and we need to rely on personal experience with various platforms, then we're back to where we were *before* POSIX. That is, having to check several separate documentation sets, and testing each API on every relevant platform in *each project* where the API is used. The idea is "ignore POSIX, care about Linux / modern systems only", but then it turns out those modern systems *do* differ sufficiently that extracting a common programming base *would* be useful. It's just that POSIX is not that common base; more precisely, there is no formalized, explicit common base. I guess "whatever passes CI" is the common base. That's... terrible, and it makes me seriously question if I want to program userspace in C at all. Laszlo
On Tue, Feb 21, 2023 at 06:53:39PM +0100, Laszlo Ersek wrote:
More in general, this lesson tells me that POSIX is effectively irrelevant -- which is quite sad in itself; the bigger problem however is that *nothing replaces it*. If the one formal standard we have for portability does not reflect reality closely enough, and we need to rely on personal experience with various platforms, then we're back to where we were *before* POSIX. That is, having to check several separate documentation sets, and testing each API on every relevant platform in *each project* where the API is used. The idea is "ignore POSIX, care about Linux / modern systems only", but then it turns out those modern systems *do* differ sufficiently that extracting a common programming base *would* be useful. It's just that POSIX is not that common base; more precisely, there is no formalized, explicit common base. I guess "whatever passes CI" is the common base. That's... terrible, and it makes me seriously question if I want to program userspace in C at all.
FWIW, I wouldn't say that POSIX is irrelevant in general. If you are trying to maximimse portability it is worth paying attention to. Rather I'd say that maintainers of projects may be opinionated about which platforms they wish to support, to eliminate the burden of caring about platforms which have few if any users in the modern world. In libvirt and QEMU context we set explicit platform support targets: https://libvirt.org/platforms.html https://www.qemu.org/docs/master/about/build-platforms.html which effectively limit us to only care about actively developed OS from the last ~4 years, and even then only fairly mainstream stuff. We don't care about a hobbyist/toy UNIX OS. The burden is on other OS to attain compatibility with mainstream modern OS, not for apps to adapt to osbscure feature-poor platforms. With this attitude, we don't care about compliance with countless obsolete vendor's UNIX platforms, and thus many of the edge cases that POSIX worries about can be ignored. This frees the project maintainers time to focus on work that benefits a broader set of users.
From this, libvirt/QEMU could both explicitly decide to not care about any C compilers other than CLang/GCC. Vendor compilers and most especially MSVC are out of scope. CLang/GCC are able to support any of the OS platforms we target. This frees us from caring about ISO C standards, letting us use GNU extensions.
AFAIK, libnbd/nbdkit haven't made a statement about what platforms they aim to target. In my response I'm more or less assuming though that you would only care about similar modern platforms to QEMU/libvirt, and thus POSIX conformance would not be needed in all areas. Maybe libnbd/nbdkit want to be more explicit about what they target as platforms to make the portability requirements clear to contributors ? With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On 2/21/23 19:04, Daniel P. Berrangé wrote:
AFAIK, libnbd/nbdkit haven't made a statement about what platforms they aim to target. In my response I'm more or less assuming though that you would only care about similar modern platforms to QEMU/libvirt, and thus POSIX conformance would not be needed in all areas. Maybe libnbd/nbdkit want to be more explicit about what they target as platforms to make the portability requirements clear to contributors ?
libnbd's README.md requires * Linux, FreeBSD or OpenBSD. Other OSes may also work but we have only tested these three. * GCC or Clang * GNU make * bash * [...] nbdkit's requires * Linux, macOS, Windows, FreeBSD, OpenBSD or Haiku * GCC or Clang * bash * GNU make * [...] To me, anything beyond Linux on those OS lists is entirely untestable *locally*, hence my reliance on POSIX. CI is a horrible way (compared to a published technical standard) to figure out whether each individual interface works as needed everywhere, even just across this small set of OSes. Having to look at multiple OS manual pages is just slightly less horrible (and I consider those less trustworthy than POSIX; see again the conflict between the linux man pages and the glibc documentation from GNU). The POSIX people have done *huge work* to save us that effort. Sticking with POSIX might make us work more (as in, write technically superfluous code), but I've always felt fewer nasty surprises are waiting to ambush us that way. I don't think we have documentation that describes the broadest intersection of these OSes specifically. (We don't even have conflict-free documentation just for Linux!) Laszlo
On Tue, Feb 21, 2023 at 11:59:30PM +0100, Laszlo Ersek wrote:
On 2/21/23 19:04, Daniel P. Berrangé wrote:
AFAIK, libnbd/nbdkit haven't made a statement about what platforms they aim to target. In my response I'm more or less assuming though that you would only care about similar modern platforms to QEMU/libvirt, and thus POSIX conformance would not be needed in all areas. Maybe libnbd/nbdkit want to be more explicit about what they target as platforms to make the portability requirements clear to contributors ?
libnbd's README.md requires
* Linux, FreeBSD or OpenBSD. Other OSes may also work but we have only tested these three. * GCC or Clang * GNU make * bash * [...]
nbdkit's requires
* Linux, macOS, Windows, FreeBSD, OpenBSD or Haiku * GCC or Clang * bash * GNU make * [...]
To me, anything beyond Linux on those OS lists is entirely untestable *locally*, hence my reliance on POSIX. CI is a horrible way (compared to a published technical standard) to figure out whether each individual interface works as needed everywhere, even just across this small set of OSes. Having to look at multiple OS manual pages is just slightly less horrible (and I consider those less trustworthy than POSIX; see again the conflict between the linux man pages and the glibc documentation from GNU). The POSIX people have done *huge work* to save us that effort.
FWIW, my (bitter) experience from both libvirt and QEMU is that unless you have CI validating it, you don't have portability to a platform, no matter how much effort the developers put in complying with standards and/or manual pages. The reality of bizarre/broken platform impls always gets in the way of good intentions. FWIW, in terms of testing locally, all those platforms are trivial, with the exception of macOS. For Windows, mingw toolchain will get you through all compilation portability problems which is 90% of the work. While you can use WINE for some functional testing, a real Windows VM is better. For the *BSD / Haiku, a VM can be spun up with KVM quite easily. macOS is the real pain point because of its restrictive EULA meaning you basically have to buy it. It makes it very much a second class citizen for developers, unless they happen to have personal apple hardware. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On 2/22/23 10:25, Daniel P. Berrangé wrote:
On Tue, Feb 21, 2023 at 11:59:30PM +0100, Laszlo Ersek wrote:
On 2/21/23 19:04, Daniel P. Berrangé wrote:
AFAIK, libnbd/nbdkit haven't made a statement about what platforms they aim to target. In my response I'm more or less assuming though that you would only care about similar modern platforms to QEMU/libvirt, and thus POSIX conformance would not be needed in all areas. Maybe libnbd/nbdkit want to be more explicit about what they target as platforms to make the portability requirements clear to contributors ?
libnbd's README.md requires
* Linux, FreeBSD or OpenBSD. Other OSes may also work but we have only tested these three. * GCC or Clang * GNU make * bash * [...]
nbdkit's requires
* Linux, macOS, Windows, FreeBSD, OpenBSD or Haiku * GCC or Clang * bash * GNU make * [...]
To me, anything beyond Linux on those OS lists is entirely untestable *locally*, hence my reliance on POSIX. CI is a horrible way (compared to a published technical standard) to figure out whether each individual interface works as needed everywhere, even just across this small set of OSes. Having to look at multiple OS manual pages is just slightly less horrible (and I consider those less trustworthy than POSIX; see again the conflict between the linux man pages and the glibc documentation from GNU). The POSIX people have done *huge work* to save us that effort.
FWIW, my (bitter) experience from both libvirt and QEMU is that unless you have CI validating it, you don't have portability to a platform, no matter how much effort the developers put in complying with standards and/or manual pages. The reality of bizarre/broken platform impls always gets in the way of good intentions.
FWIW, in terms of testing locally, all those platforms are trivial, with the exception of macOS. For Windows, mingw toolchain will get you through all compilation portability problems which is 90% of the work. While you can use WINE for some functional testing, a real Windows VM is better. For the *BSD / Haiku, a VM can be spun up with KVM quite easily.
macOS is the real pain point because of its restrictive EULA meaning you basically have to buy it. It makes it very much a second class citizen for developers, unless they happen to have personal apple hardware.
Thank you. Laszlo
On Wed, Feb 22, 2023 at 09:25:04AM +0000, Daniel P. Berrangé wrote:
macOS is the real pain point because of its restrictive EULA meaning you basically have to buy it. It makes it very much a second class citizen for developers, unless they happen to have personal apple hardware.
FWIW I have a Mac Mini M1 I can do tests on. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Sorry about replying for the second time. After having slept on it (not much, but some), some thoughts are emerging / being distilled about my own attitude. On 2/21/23 19:04, Daniel P. Berrangé wrote:
On Tue, Feb 21, 2023 at 06:53:39PM +0100, Laszlo Ersek wrote:
More in general, this lesson tells me that POSIX is effectively irrelevant -- which is quite sad in itself; the bigger problem however is that *nothing replaces it*. If the one formal standard we have for portability does not reflect reality closely enough, and we need to rely on personal experience with various platforms, then we're back to where we were *before* POSIX. That is, having to check several separate documentation sets, and testing each API on every relevant platform in *each project* where the API is used. The idea is "ignore POSIX, care about Linux / modern systems only", but then it turns out those modern systems *do* differ sufficiently that extracting a common programming base *would* be useful. It's just that POSIX is not that common base; more precisely, there is no formalized, explicit common base. I guess "whatever passes CI" is the common base. That's... terrible, and it makes me seriously question if I want to program userspace in C at all.
FWIW, I wouldn't say that POSIX is irrelevant in general. If you are trying to maximimse portability it is worth paying attention to.
Rather I'd say that maintainers of projects may be opinionated about which platforms they wish to support, to eliminate the burden of caring about platforms which have few if any users in the modern world.
In libvirt and QEMU context we set explicit platform support targets:
https://libvirt.org/platforms.html https://www.qemu.org/docs/master/about/build-platforms.html
which effectively limit us to only care about actively developed OS from the last ~4 years, and even then only fairly mainstream stuff. We don't care about a hobbyist/toy UNIX OS. The burden is on other OS to attain compatibility with mainstream modern OS, not for apps to adapt to osbscure feature-poor platforms.
With this attitude, we don't care about compliance with countless obsolete vendor's UNIX platforms, and thus many of the edge cases that POSIX worries about can be ignored. This frees the project maintainers time to focus on work that benefits a broader set of users.
From this, libvirt/QEMU could both explicitly decide to not care about any C compilers other than CLang/GCC. Vendor compilers and most especially MSVC are out of scope. CLang/GCC are able to support any of the OS platforms we target. This frees us from caring about ISO C standards, letting us use GNU extensions.
The attitude you describe above and my attitude are largely driven by the same goal: target development as narrowly as possible. Portability is essential in both cases; the big difference is in the workflow chosen for achieving portability. Approach #1: A number of OSes and a number of tools (compilers etc) are hand-picked, based on "practical" factors. This set of components taken as a whole does not have uniform, central documentation. Therefore, development is driven by (a) continuously consulting multiple -- often conflicting -- sets of documentation, and by (b) trial-and-error. By "trial-and-error" I mean that a "CI pass" is taken as strong evidence of absence of bugs, including portability bugs. The workflow relies heavily on CI to root out portability bugs. The advantage of this approach is that it deduces -- with documentation reconciliation, trial-and-error, and compiler / OS / libc source code investigation -- such a "common denominator" that is fairly likely the *greatest* common denominator. Therefore less/simpler code has to be written and maintained for feature and bugfix delivery. The disadvantage is that there is no single source of truth; the workflow is centered on reconciling incomplete and/or conflicting documentation sets, and "happens to work in CI" is taken as the final argument. CI is costly in computing time (energy), developer time (waiting, bad presentation of results), and money (minutes are expensive), and locally testing all targeted platforms is a huge chore. CI development/management in itself consumes immense human effort. Approach #2: target a published technical standard, as a single source of truth. Still employ CI, but not as a guiding tool, more like "just in case". CI failures originating from portability issues are not expected by general. CI success is not taken as the primary evidence of lack of portability bugs. The advantage of this approach is that developers can focus on a single source of truth, for driving development -- POSIX. Patching up portability problems may occasionally be necessary, but that should be the exception. The disadvantage of this approach is that POSIX, while arguably a common denominator, is almost certainly not the *greatest* common denominator. Therefore, more code needs to be written and maintained, plus recent developments that "eventually" appear in all of the targeted platforms / tools, are not consumable until they become centrally standardized. So, here's the thing: at a personal level, I can entirely identify with approach #2, and I'm unable to identify with approach #1, as the development workflow that I am supposed to follow and practice. To me, being torn to pieces between 3-4 conflicting documentation sets, and writing code such that the *primary metric* be "let me see if this passes CI -- let me throw code at the wall and see what sticks" is unbearable. Having to submit several tweaks in succession and waiting tens of minutes for CI to finish every time, rules out software development as a profession for me. (CI remains relevant anyway, but not for dictating or driving portability decisions.) Having to test out interfaces manually that are supposed to be standard, for determining and exploiting their *accidental* greatest common denominator, again rules out software development for me as a profession. Such work *is* valuable, but it's called standardization / standards development, not software development. I don't mind participating in standards development, but the *output* of that activity needs to be a *central formal standard* that programmers can rely upon in the future, not some implicit understanding that gets encoded in / dispersed over a bunch of disparate applications and libraries -- such as "we can call execvp() here because our particular fork() version lets us" -- that merely happen to target the same arbitrary set of platforms. QEMU actually gets this *quite* right, with "devel/style.rst". It still doesn't say anything about fork()/execvp() though, for example. On the same note, I honestly think that the conflict between the linux manual pages and the GNU manual, regarding the child process restrictions, is *unforgivable*. Note that I'm not trying to assert an objective truth here. All I'm saying is I'm personally incompatible with approach#1. To me, *how* I work is generally more important than *what* I achieve for users. Under that umbrella, the justification for introducing our own async-signal-safe execvpe in this patch is simply the fact that the official documentations (plural) available on Linux are *inconsistent* about fork()+execvp(). The fact that it "happens" to work in practice is just happenstance. If you will, call this my denial of practical reality. So: if the libnbd project can tolerate my attitude (approach#2), then I'd like to proceed with this series (full scope), with me addressing the v3 review feedback in v4, and so on. If not, then I'll abandon the series, and try to make myself useful with something else -- where my basic stance, towards whatever documentation I read, need not be *distrust*. Laszlo
On Wed, Feb 22, 2023 at 09:48:04AM +0100, Laszlo Ersek wrote:
So: if the libnbd project can tolerate my attitude (approach#2), then I'd like to proceed with this series (full scope), with me addressing the v3 review feedback in v4, and so on. If not, then I'll abandon the series, and try to make myself useful with something else -- where my basic stance, towards whatever documentation I read, need not be *distrust*.
I'd definitely like to see further revisions of this series. Also, is there a subset of patches which have been reviewed and agreed on by myself & Eric that we can push now? This will make the series less formidable to review. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
On 2/22/23 10:09, Richard W.M. Jones wrote:
On Wed, Feb 22, 2023 at 09:48:04AM +0100, Laszlo Ersek wrote:
So: if the libnbd project can tolerate my attitude (approach#2), then I'd like to proceed with this series (full scope), with me addressing the v3 review feedback in v4, and so on. If not, then I'll abandon the series, and try to make myself useful with something else -- where my basic stance, towards whatever documentation I read, need not be *distrust*.
I'd definitely like to see further revisions of this series.
Also, is there a subset of patches which have been reviewed and agreed on by myself & Eric that we can push now? This will make the series less formidable to review.
Yes, that's exactly what I've had in mind. There's an initial sub-sequence that should be pushable (with some further tasks noted in feedback that I should capture in BZs). Then, for the rest, now that we've all seen the full scope, I plan to advance in baby steps; effectively splitting the series for v4 into multiple small waves. This is a frequent problem with a long tail of dependencies. Initially, one is reluctant to post any "prelude" patches because there's no evidence yet that the larger series is going *anywhere*. There must be *some* path first that fully connects the start with the end. But, once that path exists, and the scope is agreed upon, we can iterate in smaller sprints. Thanks! Laszlo
On 2/21/23 17:16, Daniel P. Berrangé wrote:
Apps simply shouldn't ever call setenv once threads are created, and malloc() is safe in any impl that is relevant these days.
Yes, the docs are written by language lawyers, but the reality is that malloc is safe in glibc for 15+ years and that's not going to change.
macOS, Linux glib, Linux musl, FreeBSD are all malloc safe after fork with threads. Windows doesn't have fork. I'm fairly certain that I validated the OpenBSD/NetBSD malloc impl too, but it was a while ago.
Consider two patterns: (1) an application calling setenv() in one thread and getenv() in another thread (2) a multi-threaded application forking, and then calling malloc() in the child process (Assume there are no user-provided libraries here, just the standard C library, and applications.) POSIX rules each practice a bug in the application. "We" (for some uncertain definition of "we") however call (1) a bug in the application, and (2) a bug in the standard C library. Put differently, glibc chooses to break under (1), and to protect iself against (2). If users report bugs about issue (1), the tickets are routed to the application's bug tracker; if they report bugs about issue (2), the tickets are routed to glibc's bug tracker. Why? More precisely: why this inconsistency between the choices for (1) and (2)? I'm not even asking why diverge from the published technical standard -- my question is, why diverge *inconsistently* from the standard. A different standard C library implementation might choose the inverse "armoring". I can't wrap my brain around the *arbitrariness* of this. I guess I could entertain an argument like issue (1) is easy to fix in the application, while issue (2) is hard to fix in an application, so considering total development cost, it's best to fix issue (2) in a central location: glibc; i.e., it's best to extend POSIX just for issue (2) *IF* your run-of-the-mill Linux distribution's *own* documentation were not inconsistent with such an argument. The fact that we have two divergent, conflicting documentation sets on just that *specific* programming environment -- the linux manual pages, vs. the glibc info pages from GNU --, where the former calls malloc() unsafe for (2) but the latter calls it safe, turns the whole thing into a travesty. No statement ever in either manual can be then taken seriously, everything needs to be tested. What justification do you see for the different treatment of (1) vs (2)? Related tickets (both about the same issue): - https://bugzilla.redhat.com/show_bug.cgi?id=906468 - https://sourceware.org/bugzilla/show_bug.cgi?id=19431 These were fixed in 2016-2017, so *not* 15+ years ago. malloc() may be "exceptionally safe" in this regard, in practice, but other functions that POSIX similarly calls unsafe, may be just "relatively safe", or not safe at all, in glibc. The problem is that we now can't make *any assessment at all* based on the documentation, because POSIX is being called "overly cautious", and because the Linux manual vs. the glibc documentation have been proved obsolete and/or contradictory. With this, it's effectively impossible, by way of code review and docs checking, to determine if API usage is safe or not. What sense does code review make like this in the first place? Laszlo
On Tue, Feb 21, 2023 at 05:03:23PM +0100, Laszlo Ersek wrote:
Rich mentioned that libnbd had actually encountered a bug of this kind, just not specifically in exec*p*().
Probably this one? https://github.com/libguestfs/libguestfs/commit/e1c9bbb3d1d5ef81490977060120... glibc was pretty tolerant of this code bug, and the error only manifested itself when we used glibc.malloc.check=1 Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
On 2/22/23 09:09, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 05:03:23PM +0100, Laszlo Ersek wrote:
Rich mentioned that libnbd had actually encountered a bug of this kind, just not specifically in exec*p*().
Probably this one?
https://github.com/libguestfs/libguestfs/commit/e1c9bbb3d1d5ef81490977060120...
Yes, that's the one you mentioned before! Here: http://mid.mail-archive.com/20230131130753.GA7636@redhat.com
glibc was pretty tolerant of this code bug, and the error only manifested itself when we used glibc.malloc.check=1
Thank you for pointing me to the same commit again. Yesterday, when Daniel described that malloc() was -- in practice -- safe to call in a child process forked from a multi-threaded process, I wrote the following test program: (The program starts 8 threads calling malloc+free in a busy loop, then the main thread enters an infinite loop, forking and reaping a child process in each iteration, and printing a dot for each child reaped. The child process, forked from the multi-threaded parent process, calls a single malloc+free pair, and then exits.) 1 #define _XOPEN_SOURCE 700 2 3 #include <pthread.h> 4 #include <stdlib.h> 5 #include <sys/wait.h> 6 #include <unistd.h> 7 8 static const size_t size = 16 * 1024 * 1024; 9 10 static void * 11 threadfn (void *arg) 12 { 13 while (1) 14 free (malloc (size)); 15 } 16 17 int 18 main (void) 19 { 20 unsigned i; 21 22 for (i = 0; i < 8; ++i) { 23 pthread_t thread; 24 25 if (pthread_create (&thread, NULL, threadfn, NULL) != 0) 26 _exit (EXIT_FAILURE); 27 } 28 29 while (1) { 30 pid_t pid; 31 32 pid = fork (); 33 switch (pid) { 34 case -1: 35 _exit (EXIT_FAILURE); 36 37 case 0: 38 /* child */ 39 free (malloc (size)); 40 _exit (EXIT_SUCCESS); 41 42 default: 43 /* parent */ 44 if (waitpid (pid, NULL, 0) == -1 || 45 write (STDOUT_FILENO, ".", 1) == -1) 46 _exit (EXIT_FAILURE); 47 } 48 } 49 } To my shock, the program ran totally fine (on RHEL-9.1), producing a constant stream of dots on standard output, proving Daniel *right*. However, the commit message you now reference highlights "GLIBC_TUNABLES glibc.malloc.check=1". Doing a web search for that, I'm led to https://www.gnu.org/software/libc/manual/html_node/Memory-Allocation-Tunable... which states that this tunable actually depends on pre-loading "libc_malloc_debug". So, if I re-run the program like this: $ LD_PRELOAD=/usr/lib64/libc_malloc_debug.so.0 \ ./malloc-test then it continues running; if I re-run it like this: $ GLIBC_TUNABLES=glibc.malloc.check=1 \ ./malloc-test then it continues running; but if I re-run it like *this*: $ LD_PRELOAD=/usr/lib64/libc_malloc_debug.so.0 \ GLIBC_TUNABLES=glibc.malloc.check=1 \ ./malloc-test then it *instantly* deadlocks; it doesn't print a single dot. According to gdb, Thread 1 of the parent process is blocked in waitpid() on line 44, the other threads of the parent process are executing threadfn() -- I can see that on my CPU load indicator too --, and the child process is deadlocked in malloc(): #0 0x00007ffbae0934fb in __lll_lock_wait_private () from /lib64/libc.so.6 #1 0x00007ffbae218f38 in malloc_check () from /usr/lib64/libc_malloc_debug.so.0 #2 0x00007ffbae219c05 in malloc () from /usr/lib64/libc_malloc_debug.so.0 #3 0x000000000040121a in main () at malloc-test.c:39 (Yes, I understand that libc_malloc_debug is not meant for production use; still...) Laszlo
Don't try to test async-signal-safety, but strive to exercise as many as possible paths through nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe(). Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/test-fork-safe-execvpe.c | 117 +++++++++ lib/Makefile.am | 10 + lib/test-fork-safe-execvpe.sh | 270 ++++++++++++++++++++ .gitignore | 1 + 4 files changed, 398 insertions(+) diff --git a/lib/test-fork-safe-execvpe.c b/lib/test-fork-safe-execvpe.c new file mode 100644 index 000000000000..09b91102b613 --- /dev/null +++ b/lib/test-fork-safe-execvpe.c @@ -0,0 +1,117 @@ + /* nbd client library in userspace + * Copyright (C) 2013-2023 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <config.h> + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "internal.h" + +extern char **environ; + +/* This is a perror() replacement that makes the error message more + * machine-readable, for a select few error numbers. Do not use it as a general + * error() replacement, only upon nbd_internal_execvpe_init() and + * nbd_internal_fork_safe_execvpe() failure. + */ +static void +xperror (const char *s) +{ + const char *err; + + if (s != NULL && *s != '\0') + (void)fprintf (stderr, "%s: ", s); + + switch (errno) { + case EACCES: + err = "EACCES"; + break; + case ELOOP: + err = "ELOOP"; + break; + case ENOENT: + err = "ENOENT"; + break; + case ENOTDIR: + err = "ENOTDIR"; + break; + default: + err = strerror (errno); + } + (void)fprintf (stderr, "%s\n", err); +} + +int +main (int argc, char **argv) +{ + struct execvpe ctx; + const char *prog_file; + string_vector prog_argv; + size_t i; + + if (argc < 3) { + fprintf (stderr, "%1$s: usage: %1$s program-to-exec argv0 ...\n", argv[0]); + return EXIT_FAILURE; + } + + prog_file = argv[1]; + + /* For the argv of the program to execute, we need to drop our argv[0] (= our + * own name) and argv[1] (= the program we need to execute), and to tack on a + * terminating null pointer. Note that "argc" does not include the terminating + * NULL. + */ + prog_argv = (string_vector)empty_vector; + if (string_vector_reserve (&prog_argv, argc - 2 + 1) == -1) { + perror ("string_vector_reserve"); + return EXIT_FAILURE; + } + + for (i = 2; i < argc; ++i) + (void)string_vector_append (&prog_argv, argv[i]); + (void)string_vector_append (&prog_argv, NULL); + + if (nbd_internal_execvpe_init (&ctx, prog_file, prog_argv.len) == -1) { + xperror ("nbd_internal_execvpe_init"); + goto reset_prog_argv; + } + + /* Print out the generated candidates. */ + for (i = 0; i < ctx.pathnames.len; ++i) + (void)fprintf (stdout, "%s\n", ctx.pathnames.ptr[i]); + + if (fflush (stdout) == EOF) { + perror ("fflush"); + goto uninit_execvpe; + } + + (void)nbd_internal_fork_safe_execvpe (&ctx, &prog_argv, environ); + xperror ("nbd_internal_fork_safe_execvpe"); + /* fall through */ + +uninit_execvpe: + nbd_internal_execvpe_uninit (&ctx); + +reset_prog_argv: + string_vector_reset (&prog_argv); + + return EXIT_FAILURE; +} diff --git a/lib/Makefile.am b/lib/Makefile.am index 1f6501ceb49a..65a2f49e4baa 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -33,6 +33,7 @@ EXTRA_DIST = \ $(generator_built) \ libnbd.syms \ test-fork-safe-assert.sh \ + test-fork-safe-execvpe.sh \ $(NULL) lib_LTLIBRARIES = libnbd.la @@ -104,10 +105,12 @@ pkgconfig_DATA = libnbd.pc TESTS = \ test-fork-safe-assert.sh \ + test-fork-safe-execvpe.sh \ $(NULL) check_PROGRAMS = \ test-fork-safe-assert \ + test-fork-safe-execvpe \ $(NULL) test_fork_safe_assert_SOURCES = \ @@ -116,3 +119,10 @@ test_fork_safe_assert_SOURCES = \ test-fork-safe-assert.c \ utils.c \ $(NULL) + +test_fork_safe_execvpe_SOURCES = \ + $(top_srcdir)/common/utils/vector.c \ + errors.c \ + test-fork-safe-execvpe.c \ + utils.c \ + $(NULL) diff --git a/lib/test-fork-safe-execvpe.sh b/lib/test-fork-safe-execvpe.sh new file mode 100755 index 000000000000..ac9c48bb83c6 --- /dev/null +++ b/lib/test-fork-safe-execvpe.sh @@ -0,0 +1,270 @@ +#!/usr/bin/env bash +# nbd client library in userspace +# Copyright (C) 2013-2023 Red Hat Inc. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +set -e + +# Determine the absolute pathname of the execvpe helper binary. The "realpath" +# utility is not in POSIX, but Linux, FreeBSD and OpenBSD all have it. +bname=$(basename -- "$0" .sh) +dname=$(dirname -- "$0") +execvpe=$(realpath -- "$dname/$bname") + +# This is an elaborate way to control the PATH variable around the $execvpe +# helper binary as narrowly as possible. +# +# If $1 is "_", then the $execvpe helper binary is invoked with PATH unset. +# Otherwise, the binary is invoked with PATH set to $1. +# +# $2 and onward are passed to $execvpe; note that $2 becomes *both* +# "program-to-exec" for the helper *and* argv[0] for the program executed by the +# helper. +# +# The command itself (including the PATH setting) is written to "cmd" (for error +# reporting purposes only); the standard output and error are saved in "out" and +# "err" respectively; the exit status is written to "status". This function +# should never fail; if it does, then that's a bug in this unit test script, or +# the disk is full etc. +run() +{ + local pathctl=$1 + local program=$2 + local exit_status + + shift 1 + + if test _ = "$pathctl"; then + printf 'unset PATH; %s %s %s\n' "$execvpe" "$program" "$*" >cmd + set +e + ( + unset PATH + "$execvpe" "$program" "$@" >out 2>err + ) + exit_status=$? + set -e + else + printf 'PATH=%s %s %s %s\n' "$pathctl" "$execvpe" "$program" "$*" >cmd + set +e + PATH=$pathctl "$execvpe" "$program" "$@" >out 2>err + exit_status=$? + set -e + fi + printf '%d\n' $exit_status >status +} + +# After "run" returns, the following three functions can verify the result. +# +# Check if the helper binary failed in nbd_internal_execvpe_init(). +# +# $1 is the error number (a macro name such as ENOENT) that's expected of +# nbd_internal_execvpe_init(). +init_fail() +{ + local expected_error="nbd_internal_execvpe_init: $1" + local cmd=$(< cmd) + local err=$(< err) + local status=$(< status) + + if test 0 -eq "$status"; then + printf "'%s' should have failed\\n" "$cmd" >&2 + return 1 + fi + if test x"$err" != x"$expected_error"; then + printf "'%s' should have failed with '%s', got '%s'\\n" \ + "$cmd" "$expected_error" "$err" >&2 + return 1 + fi +} + +# Check if the helper binary failed in nbd_internal_fork_safe_execvpe(). +# +# $1 is the output (the list of candidate pathnames) that +# nbd_internal_execvpe_init() is expected to produce; with inner <newline> +# characters replaced with <comma> characters, and the last <newline> stripped. +# +# $2 is the error number (a macro name such as ENOENT) that's expected of +# nbd_internal_fork_safe_execvpe(). +execve_fail() +{ + local expected_output=$1 + local expected_error="nbd_internal_fork_safe_execvpe: $2" + local cmd=$(< cmd) + local out=$(< out) + local err=$(< err) + local status=$(< status) + + if test 0 -eq "$status"; then + printf "'%s' should have failed\\n" "$cmd" >&2 + return 1 + fi + if test x"$err" != x"$expected_error"; then + printf "'%s' should have failed with '%s', got '%s'\\n" \ + "$cmd" "$expected_error" "$err" >&2 + return 1 + fi + out=${out//$'\n'/,} + if test x"$out" != x"$expected_output"; then + printf "'%s' should have output '%s', got '%s'\\n" \ + "$cmd" "$expected_output" "$out" >&2 + return 1 + fi +} + +# Check if the helper binary and the program executed by it succeeded. +# +# $1 is the output (the list of candidate pathnames) that +# nbd_internal_execvpe_init() is expected to produce, followed by any output +# expected of the program that's executed by the helper; with inner <newline> +# characters replaced with <comma> characters, and the last <newline> stripped. +success() +{ + local expected_output=$1 + local cmd=$(< cmd) + local out=$(< out) + local status=$(< status) + + if test 0 -ne "$status"; then + printf "'%s' should have succeeded\\n" "$cmd" >&2 + return 1 + fi + out=${out//$'\n'/,} + if test x"$out" != x"$expected_output"; then + printf "'%s' should have output '%s', got '%s'\\n" \ + "$cmd" "$expected_output" "$out" >&2 + return 1 + fi +} + +# Create a temporary directory and change the working directory to it. +tmpd=$(mktemp -d) +trap 'rm -r -- "$tmpd"' EXIT +cd "$tmpd" + +# If the "file" parameter of execvpe() is an empty string, then we must fail -- +# in nbd_internal_execvpe_init() -- regardless of PATH. +run _ ""; init_fail ENOENT +run "" ""; init_fail ENOENT +run . ""; init_fail ENOENT + +# Create subdirectories for triggering non-fatal internal error conditions of +# execvpe(). (Almost) every subdirectory will contain one entry, called "f". +# +# Create a directory that's empty. +mkdir empty + +# Create a directory with a named pipe (FIFO) in it. +mkdir fifo +mkfifo fifo/f + +# Create a directory with a non-executable file in it. +mkdir nxregf +touch nxregf/f + +# Create a symlink loop. +ln -s symlink symlink + +# Create a directory with a (most likely) binary executable in it. +mkdir bin +expr_pathname=$(command -p -v expr) +cp -- "$expr_pathname" bin/f + +# Create a directory with an executable shell script that does not contain a +# shebang (#!). The script will print $0 and $1, and not depend on PATH for it. +mkdir sh +printf "command -p printf '%%s %%s\\\\n' \"\$0\" \"\$1\"\\n" >sh/f +chmod u+x sh/f + +# In the tests below, invoke each "f" such that the "file" parameter of +# execvpe() contain a <slash> character. +# +# Therefore, PATH does not matter. Set it to the empty string. (Which in this +# implementation would cause nbd_internal_execvpe_init() to fail with ENOENT, if +# the "file" parameter didn't contain a <slash>.) +run "" empty/f; execve_fail empty/f ENOENT +run "" fifo/f; execve_fail fifo/f EACCES +run "" nxregf/f; execve_fail nxregf/f EACCES +run "" nxregf/f/; execve_fail nxregf/f/ ENOTDIR +run "" symlink/f; execve_fail symlink/f ELOOP + +# This runs "expr 1 + 1". +run "" bin/f 1 + 1; success bin/f,2 + +# This triggers the ENOEXEC branch in nbd_internal_fork_safe_execvpe(). +# nbd_internal_fork_safe_execvpe() will first try +# +# execve("sh/f", {"sh/f", "arg1", NULL}, envp) +# +# hitting ENOEXEC. Then it will successfully call +# +# execve("/bin/sh", {"sh/f", "sh/f", "arg1", NULL}, envp) +# +# The shell script will get "sh/f" for $0 and "arg1" for $1, and print those +# out. +run "" sh/f arg1; success sh/f,"sh/f arg1" + +# In the tests below, the "file" parameter of execvpe() will not contain a +# <slash> character. +# +# Show that PATH matters that way -- first, trigger an ENOENT in +# nbd_internal_execvpe_init() by setting PATH to the empty string. +run "" expr 1 + 1; init_fail ENOENT + +# Fall back to confstr(_CS_PATH) in nbd_internal_execvpe_init(), by unsetting +# PATH. Verify the generated candidates by invoking "getconf PATH" here, and +# appending "/expr" to each prefix. +expected_output=$( + path=$(command -p getconf PATH) + IFS=: + for p in $path; do + printf '%s/%s\n' $p expr + done + command -p expr 1 + 1 +) +run _ expr 1 + 1; success "${expected_output//$'\n'/,}" + +# Continue with tests where the "file" parameter of execvpe() does not contain a +# <slash> character, but now set PATH to explicit prefix lists. +# +# Show that, if the last candidate fails execve() with an error number that +# would not be fatal otherwise, we do get that error number. +run empty:fifo:nxregf:symlink f +execve_fail empty/f,fifo/f,nxregf/f,symlink/f ELOOP + +# Put a single prefix in PATH, such that it lead to a successful execution. This +# exercises two things at the same time: (a) that nbd_internal_execvpe_init() +# produces *one* candidate (i.e., that no <colon> is seen), and (b) that +# nbd_internal_fork_safe_execvpe() succeeds for the *last* candidate. Repeat the +# test with "expr" (called "f" under "bin") and the shell script (called "f" +# under "sh", triggering the ENOEXEC branch). +run bin f 1 + 1; success bin/f,2 +run sh f arg1; success sh/f,"sh/f arg1" + +# Demonstrate that the order of candidates matters. The first invocation finds +# "expr" (called "f" under "bin"), the second invocation finds the shell script +# under "sh" (triggering the ENOEXEC branch). +run empty:bin:sh f 1 + 1; success empty/f,bin/f,sh/f,2 +run empty:sh:bin f arg1; success empty/f,sh/f,bin/f,"sh/f arg1" + +# Check the expansion of zero-length prefixes in PATH to ".", plus the +# (non-)insertion of the "/" separator. +run a/: f; execve_fail a/f,./f ENOENT +run :a/ f; execve_fail ./f,a/f ENOENT +run : f; execve_fail ./f,./f ENOENT +pushd bin +run : f 1 + 1; success ./f,./f,2 +popd +run :a/:::b/: f; execve_fail ./f,a/f,./f,./f,b/f,./f ENOENT diff --git a/.gitignore b/.gitignore index cd27a4ed7557..5c42060dc0dd 100644 --- a/.gitignore +++ b/.gitignore @@ -126,6 +126,7 @@ Makefile.in /lib/states.h /lib/test-fork-safe-assert /lib/test-fork-safe-assert.err +/lib/test-fork-safe-execvpe /lib/unlocked.h /libtool /ltmain.sh
For #6, #7, #8, #9, #10: Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
On Wed, Feb 15, 2023 at 03:11:39PM +0100, Laszlo Ersek wrote:
Don't try to test async-signal-safety, but strive to exercise as many as possible paths through nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe().
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/test-fork-safe-execvpe.c | 117 +++++++++ lib/Makefile.am | 10 + lib/test-fork-safe-execvpe.sh | 270 ++++++++++++++++++++ .gitignore | 1 + 4 files changed, 398 insertions(+)
diff --git a/lib/test-fork-safe-execvpe.c b/lib/test-fork-safe-execvpe.c new file mode 100644 index 000000000000..09b91102b613 --- /dev/null +++ b/lib/test-fork-safe-execvpe.c
+ +/* This is a perror() replacement that makes the error message more + * machine-readable, for a select few error numbers. Do not use it as a general + * error() replacement, only upon nbd_internal_execvpe_init() and + * nbd_internal_fork_safe_execvpe() failure. + */ +static void +xperror (const char *s)
Handy for the .sh counterpart that reads expected output.
+ +int +main (int argc, char **argv) +{ + struct execvpe ctx; + const char *prog_file; + string_vector prog_argv; + size_t i; + + if (argc < 3) { + fprintf (stderr, "%1$s: usage: %1$s program-to-exec argv0 ...\n", argv[0]); + return EXIT_FAILURE; + } + + prog_file = argv[1]; + + /* For the argv of the program to execute, we need to drop our argv[0] (= our + * own name) and argv[1] (= the program we need to execute), and to tack on a + * terminating null pointer. Note that "argc" does not include the terminating + * NULL. + */ + prog_argv = (string_vector)empty_vector; + if (string_vector_reserve (&prog_argv, argc - 2 + 1) == -1) { + perror ("string_vector_reserve"); + return EXIT_FAILURE; + } + + for (i = 2; i < argc; ++i) + (void)string_vector_append (&prog_argv, argv[i]); + (void)string_vector_append (&prog_argv, NULL);
I don't know if I would have used the cast-to-void. But it doesn't hurt (this is a unit test and you are documenting that we are basically ignoring append errors as unlikely), and at this point it's more effort to rip than out than leave well enough alone.
+ + if (nbd_internal_execvpe_init (&ctx, prog_file, prog_argv.len) == -1) { + xperror ("nbd_internal_execvpe_init"); + goto reset_prog_argv; + } + + /* Print out the generated candidates. */ + for (i = 0; i < ctx.pathnames.len; ++i) + (void)fprintf (stdout, "%s\n", ctx.pathnames.ptr[i]);
Peering into the internals. Naughty for normal code, but perfectly acceptable for the unit test.
+ + if (fflush (stdout) == EOF) { + perror ("fflush"); + goto uninit_execvpe; + } + + (void)nbd_internal_fork_safe_execvpe (&ctx, &prog_argv, environ); + xperror ("nbd_internal_fork_safe_execvpe"); + /* fall through */ + +uninit_execvpe: + nbd_internal_execvpe_uninit (&ctx); + +reset_prog_argv: + string_vector_reset (&prog_argv); + + return EXIT_FAILURE; +}
+++ b/lib/test-fork-safe-execvpe.sh
+ +set -e + +# Determine the absolute pathname of the execvpe helper binary. The "realpath" +# utility is not in POSIX, but Linux, FreeBSD and OpenBSD all have it.
not in POSIX yet, but see https://www.austingroupbugs.net/view.php?id=1457
+bname=$(basename -- "$0" .sh) +dname=$(dirname -- "$0") +execvpe=$(realpath -- "$dname/$bname") + +# This is an elaborate way to control the PATH variable around the $execvpe +# helper binary as narrowly as possible. +# +# If $1 is "_", then the $execvpe helper binary is invoked with PATH unset. +# Otherwise, the binary is invoked with PATH set to $1. +# +# $2 and onward are passed to $execvpe; note that $2 becomes *both* +# "program-to-exec" for the helper *and* argv[0] for the program executed by the +# helper. +# +# The command itself (including the PATH setting) is written to "cmd" (for error +# reporting purposes only); the standard output and error are saved in "out" and +# "err" respectively; the exit status is written to "status". This function +# should never fail; if it does, then that's a bug in this unit test script, or +# the disk is full etc. +run()
Looks nice.
+ +# Create a temporary directory and change the working directory to it. +tmpd=$(mktemp -d) +trap 'rm -r -- "$tmpd"' EXIT
Our testsuite has cleanup_fn in functions.sh to make this sort of cleanup work more robust (more than just on EXIT).
+cd "$tmpd" + +# If the "file" parameter of execvpe() is an empty string, then we must fail -- +# in nbd_internal_execvpe_init() -- regardless of PATH. +run _ ""; init_fail ENOENT +run "" ""; init_fail ENOENT +run . ""; init_fail ENOENT + +# Create subdirectories for triggering non-fatal internal error conditions of +# execvpe(). (Almost) every subdirectory will contain one entry, called "f". +# +# Create a directory that's empty. +mkdir empty + +# Create a directory with a named pipe (FIFO) in it. +mkdir fifo +mkfifo fifo/f + +# Create a directory with a non-executable file in it. +mkdir nxregf +touch nxregf/f + +# Create a symlink loop. +ln -s symlink symlink
There are two places where ELOOP could matter: both in PATH (symlink/f can't resolve, regardless of the binary name we are looking up) and in the binary (./symlink or PATH=. symlink). Looking ahead, I'm seeing your tests of symlink as a directory name, but not as a binary name. But I don't know if adding it would increase coverage of your code, so much as coverage of the underlying execve() call.
+ +# Create a directory with a (most likely) binary executable in it. +mkdir bin +expr_pathname=$(command -p -v expr) +cp -- "$expr_pathname" bin/f + +# Create a directory with an executable shell script that does not contain a +# shebang (#!). The script will print $0 and $1, and not depend on PATH for it. +mkdir sh +printf "command -p printf '%%s %%s\\\\n' \"\$0\" \"\$1\"\\n" >sh/f +chmod u+x sh/f
Based on your reactions to my comments on 09/29 about handling empty PATH= the same as PATH=., and on whether to allow the child process to chdir() and/or deal with a PATH element or binary name such as "+s" that could impact /bin/sh's behavior, there may be other cases you want to add to the testsuite.
+ +# In the tests below, invoke each "f" such that the "file" parameter of +# execvpe() contain a <slash> character. +# +# Therefore, PATH does not matter. Set it to the empty string. (Which in this +# implementation would cause nbd_internal_execvpe_init() to fail with ENOENT, if +# the "file" parameter didn't contain a <slash>.) +run "" empty/f; execve_fail empty/f ENOENT +run "" fifo/f; execve_fail fifo/f EACCES +run "" nxregf/f; execve_fail nxregf/f EACCES +run "" nxregf/f/; execve_fail nxregf/f/ ENOTDIR +run "" symlink/f; execve_fail symlink/f ELOOP + +# This runs "expr 1 + 1". +run "" bin/f 1 + 1; success bin/f,2 + +# This triggers the ENOEXEC branch in nbd_internal_fork_safe_execvpe(). +# nbd_internal_fork_safe_execvpe() will first try +# +# execve("sh/f", {"sh/f", "arg1", NULL}, envp) +# +# hitting ENOEXEC. Then it will successfully call +# +# execve("/bin/sh", {"sh/f", "sh/f", "arg1", NULL}, envp) +# +# The shell script will get "sh/f" for $0 and "arg1" for $1, and print those +# out. +run "" sh/f arg1; success sh/f,"sh/f arg1" + +# In the tests below, the "file" parameter of execvpe() will not contain a +# <slash> character. +# +# Show that PATH matters that way -- first, trigger an ENOENT in +# nbd_internal_execvpe_init() by setting PATH to the empty string. +run "" expr 1 + 1; init_fail ENOENT
Another spot that might need tweaking based on your reaction to my comments on the previous patch.
+ +# Fall back to confstr(_CS_PATH) in nbd_internal_execvpe_init(), by unsetting +# PATH. Verify the generated candidates by invoking "getconf PATH" here, and +# appending "/expr" to each prefix. +expected_output=$( + path=$(command -p getconf PATH)
We may have to check that getconf works, or skip the test if it doesn't (POSIX says it should exist, but not all the world is POSIX). Can be done as a followup if CI shows a failure.
+ IFS=: + for p in $path; do + printf '%s/%s\n' $p expr + done + command -p expr 1 + 1 +) +run _ expr 1 + 1; success "${expected_output//$'\n'/,}" + +# Continue with tests where the "file" parameter of execvpe() does not contain a +# <slash> character, but now set PATH to explicit prefix lists. +# +# Show that, if the last candidate fails execve() with an error number that +# would not be fatal otherwise, we do get that error number. +run empty:fifo:nxregf:symlink f +execve_fail empty/f,fifo/f,nxregf/f,symlink/f ELOOP + +# Put a single prefix in PATH, such that it lead to a successful execution. This
leads
+# exercises two things at the same time: (a) that nbd_internal_execvpe_init() +# produces *one* candidate (i.e., that no <colon> is seen), and (b) that +# nbd_internal_fork_safe_execvpe() succeeds for the *last* candidate. Repeat the +# test with "expr" (called "f" under "bin") and the shell script (called "f" +# under "sh", triggering the ENOEXEC branch). +run bin f 1 + 1; success bin/f,2 +run sh f arg1; success sh/f,"sh/f arg1" + +# Demonstrate that the order of candidates matters. The first invocation finds +# "expr" (called "f" under "bin"), the second invocation finds the shell script +# under "sh" (triggering the ENOEXEC branch). +run empty:bin:sh f 1 + 1; success empty/f,bin/f,sh/f,2 +run empty:sh:bin f arg1; success empty/f,sh/f,bin/f,"sh/f arg1" + +# Check the expansion of zero-length prefixes in PATH to ".", plus the +# (non-)insertion of the "/" separator. +run a/: f; execve_fail a/f,./f ENOENT +run :a/ f; execve_fail ./f,a/f ENOENT +run : f; execve_fail ./f,./f ENOENT +pushd bin +run : f 1 + 1; success ./f,./f,2 +popd +run :a/:::b/: f; execve_fail ./f,a/f,./f,./f,b/f,./f ENOENT
Overall a nice unit test. Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On Wed, Feb 15, 2023 at 05:03:48PM -0600, Eric Blake wrote:
+ +# Create subdirectories for triggering non-fatal internal error conditions of +# execvpe(). (Almost) every subdirectory will contain one entry, called "f". +# +# Create a directory that's empty. +mkdir empty + +# Create a directory with a named pipe (FIFO) in it. +mkdir fifo +mkfifo fifo/f + +# Create a directory with a non-executable file in it. +mkdir nxregf +touch nxregf/f + +# Create a symlink loop. +ln -s symlink symlink
Another interesting thing you might want to add to the test: mkdir -p subdir/f then show that PATH=...:subdir:... does not get tripped up by subdir/f/ having the execute bit set (aka search bit since it's a directory) but not being an executable. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/16/23 00:33, Eric Blake wrote:
On Wed, Feb 15, 2023 at 05:03:48PM -0600, Eric Blake wrote:
+ +# Create subdirectories for triggering non-fatal internal error conditions of +# execvpe(). (Almost) every subdirectory will contain one entry, called "f". +# +# Create a directory that's empty. +mkdir empty + +# Create a directory with a named pipe (FIFO) in it. +mkdir fifo +mkfifo fifo/f + +# Create a directory with a non-executable file in it. +mkdir nxregf +touch nxregf/f + +# Create a symlink loop. +ln -s symlink symlink
Another interesting thing you might want to add to the test:
mkdir -p subdir/f
then show that PATH=...:subdir:... does not get tripped up by subdir/f/ having the execute bit set (aka search bit since it's a directory) but not being an executable.
Hmm. There could be two spots to test this (because what we're triggering here is EACCES). One, *between* the following two tests: run "" fifo/f; execve_fail fifo/f EACCES run "" nxregf/f; execve_fail nxregf/f EACCES because "fifo/f" is effectively the same test as "subdir/f" -- not a regular file in the first place. Two, inside: run empty:fifo:nxregf:symlink f execve_fail empty/f,fifo/f,nxregf/f,symlink/f ELOOP (again inserted between the fifo and the nxregf cases). Now, while developing the test cases, I did (somewhat unwittingly) end up testing what you propose, but then I decided that trying to execute a directory was not much different from trying to execute a fifo -- the x file mode bit shouldn't matter because the file type wouldn't be right in the first place (the x bit should only matter on regular files). So... Do you think "subdir/f" improves code coverage? If so, do you agree that I should extend one (or both!) of the above two tests -- or else, should I add something entirely new? Thanks! Laszlo
On Tue, Feb 21, 2023 at 02:06:33PM +0100, Laszlo Ersek wrote:
On 2/16/23 00:33, Eric Blake wrote:
On Wed, Feb 15, 2023 at 05:03:48PM -0600, Eric Blake wrote:
+ +# Create subdirectories for triggering non-fatal internal error conditions of +# execvpe(). (Almost) every subdirectory will contain one entry, called "f". +# +# Create a directory that's empty. +mkdir empty + +# Create a directory with a named pipe (FIFO) in it. +mkdir fifo +mkfifo fifo/f + +# Create a directory with a non-executable file in it. +mkdir nxregf +touch nxregf/f + +# Create a symlink loop. +ln -s symlink symlink
Another interesting thing you might want to add to the test:
mkdir -p subdir/f
then show that PATH=...:subdir:... does not get tripped up by subdir/f/ having the execute bit set (aka search bit since it's a directory) but not being an executable.
Hmm. There could be two spots to test this (because what we're triggering here is EACCES). One, *between* the following two tests:
run "" fifo/f; execve_fail fifo/f EACCES run "" nxregf/f; execve_fail nxregf/f EACCES
because "fifo/f" is effectively the same test as "subdir/f" -- not a regular file in the first place.
Two, inside:
run empty:fifo:nxregf:symlink f execve_fail empty/f,fifo/f,nxregf/f,symlink/f ELOOP
(again inserted between the fifo and the nxregf cases).
Now, while developing the test cases, I did (somewhat unwittingly) end up testing what you propose, but then I decided that trying to execute a directory was not much different from trying to execute a fifo -- the x file mode bit shouldn't matter because the file type wouldn't be right in the first place (the x bit should only matter on regular files).
There's no reason for a fifo to ever have the x bit, but a directory regularly has the search bit (which overlays the x bit) set. I've never accidentally attempted to execute a fifo, but I have, more than once, asked to execute something I thought was a file only to have it turn out to be a directory name. See also commit b17fa77b in nbdkit, where it actually hurt us (our shim script was sticking /path/to/nbdkit first in PATH, but when we had /path/to/nbdkit/bash/ as a subdirectory, we actively triggered attempts to execute the subdirectory instead of the intended /bin/bash).
So... Do you think "subdir/f" improves code coverage? If so, do you agree that I should extend one (or both!) of the above two tests -- or else, should I add something entirely new?
I'd be happy with extending at least one of the above two tests, if that's easier than adding something new. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/21/23 18:12, Eric Blake wrote:
On Tue, Feb 21, 2023 at 02:06:33PM +0100, Laszlo Ersek wrote:
On 2/16/23 00:33, Eric Blake wrote:
On Wed, Feb 15, 2023 at 05:03:48PM -0600, Eric Blake wrote:
+ +# Create subdirectories for triggering non-fatal internal error conditions of +# execvpe(). (Almost) every subdirectory will contain one entry, called "f". +# +# Create a directory that's empty. +mkdir empty + +# Create a directory with a named pipe (FIFO) in it. +mkdir fifo +mkfifo fifo/f + +# Create a directory with a non-executable file in it. +mkdir nxregf +touch nxregf/f + +# Create a symlink loop. +ln -s symlink symlink
Another interesting thing you might want to add to the test:
mkdir -p subdir/f
then show that PATH=...:subdir:... does not get tripped up by subdir/f/ having the execute bit set (aka search bit since it's a directory) but not being an executable.
Hmm. There could be two spots to test this (because what we're triggering here is EACCES). One, *between* the following two tests:
run "" fifo/f; execve_fail fifo/f EACCES run "" nxregf/f; execve_fail nxregf/f EACCES
because "fifo/f" is effectively the same test as "subdir/f" -- not a regular file in the first place.
Two, inside:
run empty:fifo:nxregf:symlink f execve_fail empty/f,fifo/f,nxregf/f,symlink/f ELOOP
(again inserted between the fifo and the nxregf cases).
Now, while developing the test cases, I did (somewhat unwittingly) end up testing what you propose, but then I decided that trying to execute a directory was not much different from trying to execute a fifo -- the x file mode bit shouldn't matter because the file type wouldn't be right in the first place (the x bit should only matter on regular files).
There's no reason for a fifo to ever have the x bit, but a directory regularly has the search bit (which overlays the x bit) set. I've never accidentally attempted to execute a fifo, but I have, more than once, asked to execute something I thought was a file only to have it turn out to be a directory name. See also commit b17fa77b in nbdkit, where it actually hurt us (our shim script was sticking /path/to/nbdkit first in PATH, but when we had /path/to/nbdkit/bash/ as a subdirectory, we actively triggered attempts to execute the subdirectory instead of the intended /bin/bash).
But wouldn't that actually highlight a discrepancy? On one hand, you have the system behavior described in nbdkit commit b17fa77b, where the prepended /path/to/nbdkit PATH prefix caused the traversal to *stop* (and fail) at /path/to/nbdkit/bash, without locating "bash" with the help of the rest of PATH. On the other hand, the execvpe() implementation from this patch proceeds to finding "bash" successfully in the above situation, because it survives EACCES. And I think the *latter* is what conforms to POSIX; the system behavior described in nbdkit commit b17fa77b does not IMO. So... is this discrepancy OK in the first place? (If it is OK, I kind of agree now that we should test it explicitly -- well, that's assuming I'll continue working on this part of the series at all.) (BTW, I can't even reproduce the problem described in nbdkit commit b17fa77b on the command line!)
So... Do you think "subdir/f" improves code coverage? If so, do you agree that I should extend one (or both!) of the above two tests -- or else, should I add something entirely new?
I'd be happy with extending at least one of the above two tests, if that's easier than adding something new.
Right, I think extending both tests noted above would cover it fine. Laszlo
On 2/16/23 00:03, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:39PM +0100, Laszlo Ersek wrote:
Don't try to test async-signal-safety, but strive to exercise as many as possible paths through nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe().
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/test-fork-safe-execvpe.c | 117 +++++++++ lib/Makefile.am | 10 + lib/test-fork-safe-execvpe.sh | 270 ++++++++++++++++++++ .gitignore | 1 + 4 files changed, 398 insertions(+)
diff --git a/lib/test-fork-safe-execvpe.c b/lib/test-fork-safe-execvpe.c new file mode 100644 index 000000000000..09b91102b613 --- /dev/null +++ b/lib/test-fork-safe-execvpe.c
+ +/* This is a perror() replacement that makes the error message more + * machine-readable, for a select few error numbers. Do not use it as a general + * error() replacement, only upon nbd_internal_execvpe_init() and + * nbd_internal_fork_safe_execvpe() failure. + */ +static void +xperror (const char *s)
Handy for the .sh counterpart that reads expected output.
+ +int +main (int argc, char **argv) +{ + struct execvpe ctx; + const char *prog_file; + string_vector prog_argv; + size_t i; + + if (argc < 3) { + fprintf (stderr, "%1$s: usage: %1$s program-to-exec argv0 ...\n", argv[0]); + return EXIT_FAILURE; + } + + prog_file = argv[1]; + + /* For the argv of the program to execute, we need to drop our argv[0] (= our + * own name) and argv[1] (= the program we need to execute), and to tack on a + * terminating null pointer. Note that "argc" does not include the terminating + * NULL. + */ + prog_argv = (string_vector)empty_vector; + if (string_vector_reserve (&prog_argv, argc - 2 + 1) == -1) { + perror ("string_vector_reserve"); + return EXIT_FAILURE; + } + + for (i = 2; i < argc; ++i) + (void)string_vector_append (&prog_argv, argv[i]); + (void)string_vector_append (&prog_argv, NULL);
I don't know if I would have used the cast-to-void. But it doesn't hurt (this is a unit test and you are documenting that we are basically ignoring append errors as unlikely), and at this point it's more effort to rip than out than leave well enough alone.
s/unlikely/impossible/, per successful string_vector_reserve() above.
+ + if (nbd_internal_execvpe_init (&ctx, prog_file, prog_argv.len) == -1) { + xperror ("nbd_internal_execvpe_init"); + goto reset_prog_argv; + } + + /* Print out the generated candidates. */ + for (i = 0; i < ctx.pathnames.len; ++i) + (void)fprintf (stdout, "%s\n", ctx.pathnames.ptr[i]);
Peering into the internals. Naughty for normal code, but perfectly acceptable for the unit test.
Well put :)
+ + if (fflush (stdout) == EOF) { + perror ("fflush"); + goto uninit_execvpe; + } + + (void)nbd_internal_fork_safe_execvpe (&ctx, &prog_argv, environ); + xperror ("nbd_internal_fork_safe_execvpe"); + /* fall through */ + +uninit_execvpe: + nbd_internal_execvpe_uninit (&ctx); + +reset_prog_argv: + string_vector_reset (&prog_argv); + + return EXIT_FAILURE; +}
+++ b/lib/test-fork-safe-execvpe.sh
+ +set -e + +# Determine the absolute pathname of the execvpe helper binary. The "realpath" +# utility is not in POSIX, but Linux, FreeBSD and OpenBSD all have it.
not in POSIX yet, but see https://www.austingroupbugs.net/view.php?id=1457
Very cool! Thanks!
+bname=$(basename -- "$0" .sh) +dname=$(dirname -- "$0") +execvpe=$(realpath -- "$dname/$bname") + +# This is an elaborate way to control the PATH variable around the $execvpe +# helper binary as narrowly as possible. +# +# If $1 is "_", then the $execvpe helper binary is invoked with PATH unset. +# Otherwise, the binary is invoked with PATH set to $1. +# +# $2 and onward are passed to $execvpe; note that $2 becomes *both* +# "program-to-exec" for the helper *and* argv[0] for the program executed by the +# helper. +# +# The command itself (including the PATH setting) is written to "cmd" (for error +# reporting purposes only); the standard output and error are saved in "out" and +# "err" respectively; the exit status is written to "status". This function +# should never fail; if it does, then that's a bug in this unit test script, or +# the disk is full etc. +run()
Looks nice.
+ +# Create a temporary directory and change the working directory to it. +tmpd=$(mktemp -d) +trap 'rm -r -- "$tmpd"' EXIT
Our testsuite has cleanup_fn in functions.sh to make this sort of cleanup work more robust (more than just on EXIT).
(1) cleanup_fn is in "tests/functions.sh", but this unit test is in lib/. Hm... still, I see lots of . ../tests/functions.sh lines across various test cases. So, based on that, and for uniformity's sake, I can use cleanup_fn here, indeed. (2) But, what do you mean by "more than just on EXIT"? In "functions.sh", I see: trap _run_cleanup_hooks INT QUIT TERM EXIT ERR
From these, INT, TERM, EXIT and ERR should *already* be covered by just EXIT; ERR in particular because of "set -e" in this script.
And regarding QUIT, I disagree that the cleanup handlers should run upon QUIT. QUIT ("Terminal quit signal") is a signal specifically designed for interactive abnormal termination, without cleaning up, in practice even including a core dump -- that is, for debugging purposes. I do use Ctrl-\ on occasion, for debugging. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#ta... Any temporary file created by the implementation shall be removed by the implementation upon a utility's successful exit, exit because of errors, or before termination by any of the SIGHUP, SIGINT, or SIGTERM signals, unless specified otherwise by the utility description. Receipt of the SIGQUIT signal should generally cause termination (unless in some debugging mode) that would bypass any attempted recovery actions.
+cd "$tmpd" + +# If the "file" parameter of execvpe() is an empty string, then we must fail -- +# in nbd_internal_execvpe_init() -- regardless of PATH. +run _ ""; init_fail ENOENT +run "" ""; init_fail ENOENT +run . ""; init_fail ENOENT + +# Create subdirectories for triggering non-fatal internal error conditions of +# execvpe(). (Almost) every subdirectory will contain one entry, called "f". +# +# Create a directory that's empty. +mkdir empty + +# Create a directory with a named pipe (FIFO) in it. +mkdir fifo +mkfifo fifo/f + +# Create a directory with a non-executable file in it. +mkdir nxregf +touch nxregf/f + +# Create a symlink loop. +ln -s symlink symlink
There are two places where ELOOP could matter: both in PATH (symlink/f can't resolve, regardless of the binary name we are looking up) and in the binary (./symlink or PATH=. symlink). Looking ahead, I'm seeing your tests of symlink as a directory name, but not as a binary name. But I don't know if adding it would increase coverage of your code, so much as coverage of the underlying execve() call.
Exactly -- what particular internal step of execve() leads to execve() returning -1/ELOOP doesn't matter here.
+ +# Create a directory with a (most likely) binary executable in it. +mkdir bin +expr_pathname=$(command -p -v expr) +cp -- "$expr_pathname" bin/f + +# Create a directory with an executable shell script that does not contain a +# shebang (#!). The script will print $0 and $1, and not depend on PATH for it. +mkdir sh +printf "command -p printf '%%s %%s\\\\n' \"\$0\" \"\$1\"\\n" >sh/f +chmod u+x sh/f
Based on your reactions to my comments on 09/29 about handling empty PATH= the same as PATH=., and on whether to allow the child process to chdir() and/or deal with a PATH element or binary name such as "+s" that could impact /bin/sh's behavior, there may be other cases you want to add to the testsuite.
I agree, and that's another reason why I wouldn't like to modify the behavior on PATH='' :) Writing the present patch was the single most time consuming / difficult step across the entire series.
+ +# In the tests below, invoke each "f" such that the "file" parameter of +# execvpe() contain a <slash> character. +# +# Therefore, PATH does not matter. Set it to the empty string. (Which in this +# implementation would cause nbd_internal_execvpe_init() to fail with ENOENT, if +# the "file" parameter didn't contain a <slash>.) +run "" empty/f; execve_fail empty/f ENOENT +run "" fifo/f; execve_fail fifo/f EACCES +run "" nxregf/f; execve_fail nxregf/f EACCES +run "" nxregf/f/; execve_fail nxregf/f/ ENOTDIR +run "" symlink/f; execve_fail symlink/f ELOOP + +# This runs "expr 1 + 1". +run "" bin/f 1 + 1; success bin/f,2 + +# This triggers the ENOEXEC branch in nbd_internal_fork_safe_execvpe(). +# nbd_internal_fork_safe_execvpe() will first try +# +# execve("sh/f", {"sh/f", "arg1", NULL}, envp) +# +# hitting ENOEXEC. Then it will successfully call +# +# execve("/bin/sh", {"sh/f", "sh/f", "arg1", NULL}, envp) +# +# The shell script will get "sh/f" for $0 and "arg1" for $1, and print those +# out. +run "" sh/f arg1; success sh/f,"sh/f arg1" + +# In the tests below, the "file" parameter of execvpe() will not contain a +# <slash> character. +# +# Show that PATH matters that way -- first, trigger an ENOENT in +# nbd_internal_execvpe_init() by setting PATH to the empty string. +run "" expr 1 + 1; init_fail ENOENT
Another spot that might need tweaking based on your reaction to my comments on the previous patch.
+ +# Fall back to confstr(_CS_PATH) in nbd_internal_execvpe_init(), by unsetting +# PATH. Verify the generated candidates by invoking "getconf PATH" here, and +# appending "/expr" to each prefix. +expected_output=$( + path=$(command -p getconf PATH)
We may have to check that getconf works, or skip the test if it doesn't (POSIX says it should exist, but not all the world is POSIX). Can be done as a followup if CI shows a failure.
Hm, good point! Our README file says, under requirements: "Linux, FreeBSD or OpenBSD". Both BSDs seem to know about "getconf": https://man.openbsd.org/getconf.1 https://man.freebsd.org/cgi/man.cgi?query=getconf Let's hope CI will be happy. :)
+ IFS=: + for p in $path; do + printf '%s/%s\n' $p expr + done + command -p expr 1 + 1 +) +run _ expr 1 + 1; success "${expected_output//$'\n'/,}" + +# Continue with tests where the "file" parameter of execvpe() does not contain a +# <slash> character, but now set PATH to explicit prefix lists. +# +# Show that, if the last candidate fails execve() with an error number that +# would not be fatal otherwise, we do get that error number. +run empty:fifo:nxregf:symlink f +execve_fail empty/f,fifo/f,nxregf/f,symlink/f ELOOP + +# Put a single prefix in PATH, such that it lead to a successful execution. This
leads
I disagree (as a non-native speaker); I meant to express a singular third person imperative, with subjunctive mood: https://en.wikipedia.org/wiki/English_subjunctive
+# exercises two things at the same time: (a) that nbd_internal_execvpe_init() +# produces *one* candidate (i.e., that no <colon> is seen), and (b) that +# nbd_internal_fork_safe_execvpe() succeeds for the *last* candidate. Repeat the +# test with "expr" (called "f" under "bin") and the shell script (called "f" +# under "sh", triggering the ENOEXEC branch). +run bin f 1 + 1; success bin/f,2 +run sh f arg1; success sh/f,"sh/f arg1" + +# Demonstrate that the order of candidates matters. The first invocation finds +# "expr" (called "f" under "bin"), the second invocation finds the shell script +# under "sh" (triggering the ENOEXEC branch). +run empty:bin:sh f 1 + 1; success empty/f,bin/f,sh/f,2 +run empty:sh:bin f arg1; success empty/f,sh/f,bin/f,"sh/f arg1" + +# Check the expansion of zero-length prefixes in PATH to ".", plus the +# (non-)insertion of the "/" separator. +run a/: f; execve_fail a/f,./f ENOENT +run :a/ f; execve_fail ./f,a/f ENOENT +run : f; execve_fail ./f,./f ENOENT +pushd bin +run : f 1 + 1; success ./f,./f,2 +popd +run :a/:::b/: f; execve_fail ./f,a/f,./f,./f,b/f,./f ENOENT
Overall a nice unit test.
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks! Laszlo
On Tue, Feb 21, 2023 at 01:53:25PM +0100, Laszlo Ersek wrote:
On 2/16/23 00:03, Eric Blake wrote:
On Wed, Feb 15, 2023 at 03:11:39PM +0100, Laszlo Ersek wrote:
Don't try to test async-signal-safety, but strive to exercise as many as possible paths through nbd_internal_execvpe_init() and nbd_internal_fork_safe_execvpe().
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/test-fork-safe-execvpe.c | 117 +++++++++ lib/Makefile.am | 10 + lib/test-fork-safe-execvpe.sh | 270 ++++++++++++++++++++ .gitignore | 1 + 4 files changed, 398 insertions(+)
Skipping ahead...
+ +# Create a temporary directory and change the working directory to it. +tmpd=$(mktemp -d) +trap 'rm -r -- "$tmpd"' EXIT
Our testsuite has cleanup_fn in functions.sh to make this sort of cleanup work more robust (more than just on EXIT).
(1) cleanup_fn is in "tests/functions.sh", but this unit test is in lib/.
Hm... still, I see lots of
. ../tests/functions.sh
lines across various test cases. So, based on that, and for uniformity's sake, I can use cleanup_fn here, indeed.
Yep, the relative reach out-of-directory to ../tests/functions.sh is common in both libnbd and nbdkit. The two projects' functions.sh are not quite identical at the moment, but if we do refactor things into providing common code in a git submodule, that would be another candidate for unification efforts. (Not this series, though)
(2) But, what do you mean by "more than just on EXIT"? In "functions.sh", I see:
trap _run_cleanup_hooks INT QUIT TERM EXIT ERR
From these, INT, TERM, EXIT and ERR should *already* be covered by just EXIT; ERR in particular because of "set -e" in this script.
Bash happens to trigger EXIT after handling other signals, but this is not guaranteed by POSIX: https://www.austingroupbugs.net/view.php?id=621#c5938 If you want cleanup to be uniformly applied, regardless of whether the script itself exited or an external process injected a signal (other than the impossible-to-catch SIGKILL), you have to list both EXIT and the various signals of interest in your trap handler. Hence putting it in a common library, so we don't have to reimplement it correctly every time.
And regarding QUIT, I disagree that the cleanup handlers should run upon QUIT. QUIT ("Terminal quit signal") is a signal specifically designed for interactive abnormal termination, without cleaning up, in practice even including a core dump -- that is, for debugging purposes. I do use Ctrl-\ on occasion, for debugging.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#ta...
Any temporary file created by the implementation shall be removed by the implementation upon a utility's successful exit, exit because of errors, or before termination by any of the SIGHUP, SIGINT, or SIGTERM signals, unless specified otherwise by the utility description.
Receipt of the SIGQUIT signal should generally cause termination (unless in some debugging mode) that would bypass any attempted recovery actions.
Interesting take on things. That would be a separate patch to functions.sh, with this argument as reasoning for why we intentionally want to change things. It may even have a positive impact to CI behavior, if CI sends SIGQUIT on a test timeout, if it lets us leave more artifacts behind (but here, I'm not sure enough about what CIs do on timeouts to know if this is an actual benefit, or just wishful thinking on my part).
+# Show that, if the last candidate fails execve() with an error number that +# would not be fatal otherwise, we do get that error number. +run empty:fifo:nxregf:symlink f +execve_fail empty/f,fifo/f,nxregf/f,symlink/f ELOOP + +# Put a single prefix in PATH, such that it lead to a successful execution. This
leads
I disagree (as a non-native speaker); I meant to express a singular third person imperative, with subjunctive mood:
Without context, the word 'lead' can be pronounced /LED/ (generally a noun, meaning the metal) or /LEED/ (generally a verb, meaning to demonstrate a path). The context here makes it obvious we are using the verb, so I'm not misunderstanding you as having used the wrong homophone to /LED/, where that sound would work fine in a purely past-tense construction: "I did XYZ, such that it led /LED/ to a successful execution". But as you pointed out (and I agree), you are not going for past tense, but subjunctive. At which point, we can compare to a more explicit conditional wording "I will do XYZ, so that _pronoun verb_ to a successful execution", where we then change the first clause into a more imperative form forming the desired antecedant. The alternatives are either singular form ("it leads") or plural form ("they lead"). Since our antecdant "[you] Put a single prefix in PATH" is acting as just one imperative instruction (just one action, singular), rather than a set of instructions (plural), "it leads" sounds better than "they lead". Yes, English is weird. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Correctly report "asprintf" in the error message when asprintf() fails. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 3b621b8be44f..c46a0bf5c0a3 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -127,7 +127,7 @@ CONNECT_SA.START: if (asprintf (&h->sact_sockpath, "%s/sock", h->sact_tmpdir) == -1) { SET_NEXT_STATE (%.DEAD); - set_error (errno, "strdup"); + set_error (errno, "asprintf"); return 0; }
On Wed, Feb 15, 2023 at 03:11:40PM +0100, Laszlo Ersek wrote:
Correctly report "asprintf" in the error message when asprintf() fails.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 3b621b8be44f..c46a0bf5c0a3 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -127,7 +127,7 @@ CONNECT_SA.START:
if (asprintf (&h->sact_sockpath, "%s/sock", h->sact_tmpdir) == -1) { SET_NEXT_STATE (%.DEAD); - set_error (errno, "strdup"); + set_error (errno, "asprintf"); return 0;
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
On Wed, Feb 15, 2023 at 03:11:40PM +0100, Laszlo Ersek wrote:
Correctly report "asprintf" in the error message when asprintf() fails.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 3b621b8be44f..c46a0bf5c0a3 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -127,7 +127,7 @@ CONNECT_SA.START:
if (asprintf (&h->sact_sockpath, "%s/sock", h->sact_tmpdir) == -1) { SET_NEXT_STATE (%.DEAD); - set_error (errno, "strdup"); + set_error (errno, "asprintf");
At the end of the day, "malloc" would work too (we have several places where we aren't precise on WHAT allocation failed, but where it's obvious from the accompanying ENOMEM strerror() message that it was an allocation failure). But when it's easy to be precise, go for it. Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways: - it expects the caller to pass in a previously re-set string_vector, - if it fails, it calls set_error() internally (with a blanket reference to "malloc"). Fix both warts: - pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(), - move the set_error() call out to the caller. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i; - assert (env->len == 0); + *env = (string_vector)empty_vector; /* Reserve slots env[0] and env[1]. */ p = strdup ("LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); @@ -90,7 +90,6 @@ prepare_socket_activation_environment (string_vector *env) return 0; err: - set_error (errno, "malloc"); string_vector_empty (env); return -1; } @@ -99,7 +98,7 @@ STATE_MACHINE { CONNECT_SA.START: int s; struct sockaddr_un addr; - string_vector env = empty_vector; + string_vector env; pid_t pid; assert (!h->sock); @@ -156,6 +155,7 @@ CONNECT_SA.START: if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment"); close (s); return 0; }
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
/* Reserve slots env[0] and env[1]. */ p = strdup ("LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); @@ -90,7 +90,6 @@ prepare_socket_activation_environment (string_vector *env) return 0;
err: - set_error (errno, "malloc"); string_vector_empty (env); return -1; } @@ -99,7 +98,7 @@ STATE_MACHINE { CONNECT_SA.START: int s; struct sockaddr_un addr; - string_vector env = empty_vector; + string_vector env; pid_t pid;
assert (!h->sock); @@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function? Rich.
close (s); return 0; }
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
On Wed, Feb 15, 2023 at 04:39:35PM +0000, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
Elsewhere in the code, we overwhelmingly do not use the cast. C++ might require it, but we're using C.
/* Reserve slots env[0] and env[1]. */ p = strdup ("LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); @@ -90,7 +90,6 @@ prepare_socket_activation_environment (string_vector *env) return 0;
err: - set_error (errno, "malloc");
On a rough level, this was correct but unspecific (we only expect failure due to memory allocations, but didn't do the allocation locally and don't know which function allocated).
string_vector_empty (env); return -1; } @@ -99,7 +98,7 @@ STATE_MACHINE { CONNECT_SA.START: int s; struct sockaddr_un addr; - string_vector env = empty_vector; + string_vector env; pid_t pid;
assert (!h->sock); @@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function?
Moving it here lets us give a more specific message about a function at a different layer in the stack. Most memory failures are already going to be a pain where we don't know if the caller will ever get back a desired error message, so being inspecific doesn't necessarily hurt. But I also don't see any technical reasons to avoid this patch. Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/17/23 19:36, Eric Blake wrote:
On Wed, Feb 15, 2023 at 04:39:35PM +0000, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
Elsewhere in the code, we overwhelmingly do not use the cast. C++ might require it, but we're using C.
No, that's not the right explanation. Elsewhere in the code, we do not use *assignments* for prepping our vector structures with "empty_vector". Instead, everywhere else in the code, we use *initialization*. Those are different contexts. If you expand the "empty_vector" macro in the below: string_vector v1 = empty_vector; string_vector v2; v2 = empty_vector; we get: string_vector v1 = { .ptr = NULL, .len = 0, .cap = 0 }; string_vector v2; v2 = { .ptr = NULL, .len = 0, .cap = 0 }; For "v1", we have a definition that is also an initialization, so the initializer { .ptr = NULL, .len = 0, .cap = 0 } is fine there. For "v2", we have a definition that is not an initialization, and we have an additional *assignment*. For the right hand side of the assignment, the initializer { .ptr = NULL, .len = 0, .cap = 0 } is *not* fine. However, if we write: v2 = (string_vector){ .ptr = NULL, .len = 0, .cap = 0 }; then the RHS of the assignment becomes a C99 *compound literal*, which the initializer { .ptr = NULL, .len = 0, .cap = 0 } is a *proper part of*. The compound literal (string_vector){ .ptr = NULL, .len = 0, .cap = 0 }; is a nameless object with automatic storage duration. It's something like: string_vector v2; string_vector nameless_empty = { .ptr = NULL, .len = 0, .cap = 0 }; v2 = nameless_empty; ... I have a few more comments here: - I generally prefer to *avoid* initialization (especially initialization where the initializer is complex, like a function call!), and use assignments that are performed as late (i.e., as narrowly) as possible. In my opinion, this conveys information about data flow. (I don't like arguments such as "let's initialize this just to be sure" -- we should *know* when a variable is first consumed!) Anyway, in the present case, this argument of mine is not relevant, because the object in question (*env) already exists, we're not *defining* it in the first place (~ definition is such a declaration that allocates space), so we cannot initialize it either (in the C language sense); we can only assign another structure to it, or access its fields, or use memset(). C code that predates C99 would certainly use individual field accesses: env->ptr = NULL; env->len = 0; env->cap = 0; or call memset: memset (env, 0, sizeof *env); or use a small "initializer function" that would thinly wrap one of the above variants. However, in C99, we have compound literals, and that's a great way for reusing the "empty_vector" macro in a (structure) *assignment*.
/* Reserve slots env[0] and env[1]. */ p = strdup ("LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); @@ -90,7 +90,6 @@ prepare_socket_activation_environment (string_vector *env) return 0;
err: - set_error (errno, "malloc");
On a rough level, this was correct but unspecific (we only expect failure due to memory allocations, but didn't do the allocation locally and don't know which function allocated).
I agree, to an extent. The particular "malloc" string literal is somewhat inexact at this point, but semantically OK. It will become more inexact only later. However, my main point here is that this set_error() call does not belong in the callee. It belongs in the caller. First because the callee does lots of things (and will do more). Second, because the caller already has explicit set_error() calls on all other error branches.
string_vector_empty (env); return -1; } @@ -99,7 +98,7 @@ STATE_MACHINE { CONNECT_SA.START: int s; struct sockaddr_un addr; - string_vector env = empty_vector; + string_vector env; pid_t pid;
assert (!h->sock); @@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function?
Moving it here lets us give a more specific message about a function at a different layer in the stack.
Yes, that's it, basically.
Most memory failures are already going to be a pain where we don't know if the caller will ever get back a desired error message, so being inspecific doesn't necessarily hurt. But I also don't see any technical reasons to avoid this patch.
Reviewed-by: Eric Blake <eblake@redhat.com>
Thanks, Laszlo
On 2/15/23 17:39, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
This is not a cast, but a C99 compound literal. And yes, it is necessary, as empty_vector is just: #define empty_vector { .ptr = NULL, .len = 0, .cap = 0 } So this is *not* initialization, but assignment. We have a string_vector object (a structure) on the LHS, so we ned a structure on the RHS as well. The compound literal provides that (unnamed, automatic storage duration) structure. It looks like a cast (quite intentionally, I'd hazard), but it's not a cast.
/* Reserve slots env[0] and env[1]. */ p = strdup ("LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); @@ -90,7 +90,6 @@ prepare_socket_activation_environment (string_vector *env) return 0;
err: - set_error (errno, "malloc"); string_vector_empty (env); return -1; } @@ -99,7 +98,7 @@ STATE_MACHINE { CONNECT_SA.START: int s; struct sockaddr_un addr; - string_vector env = empty_vector; + string_vector env; pid_t pid;
assert (!h->sock); @@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function?
Two reasons: - in the caller (CONNECT_SA.START handler), every other failure branch calls set_error explicitly (and subsequent patches in the series will uphold the same pattern), - as the commit message says, the blanket "malloc" reference in prepare_socket_activation_environment() is not accurate enough, and certainly will not be accurate any longer with later patches (e.g. patch #26, which returns -1/EOVERFLOW upon ADD_OVERFLOW() failing). Note that in patch #19, a very similar cleanup is performed for CONNECT_COMMAND.START; there, we supply a missing set_error() for fcntl(), plus a *comment* that nbd_internal_socket_create() sets the error internally. (I disagree with nbd_internal_socket_create() setting the error internally, but that function is too widely called to move set_error() out of it, to all its callers, and again I needed to contain the scope creep. So, for at least restoring the "visual" uniformity of set_error() calls in CONNECT_COMMAND.START, I added the comment.) Thanks! Laszlo
Rich.
close (s); return 0; }
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
On Tue, Feb 21, 2023 at 02:17:15PM +0100, Laszlo Ersek wrote:
On 2/15/23 17:39, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
This is not a cast, but a C99 compound literal. And yes, it is necessary, as empty_vector is just:
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
So this is *not* initialization, but assignment. We have a string_vector object (a structure) on the LHS, so we ned a structure on the RHS as well. The compound literal provides that (unnamed, automatic storage duration) structure. It looks like a cast (quite intentionally, I'd hazard), but it's not a cast.
OK it's not a cast, but struct assignment is well defined so is the change necessary?
@@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function?
Two reasons:
- in the caller (CONNECT_SA.START handler), every other failure branch calls set_error explicitly (and subsequent patches in the series will uphold the same pattern),
The pattern is actually that we call set_error once on each error path [which is surprisingly hard to get right -- we've even tried to write verifier tools for this in the past]. If a function f() calls function g(), where the g() will call set_error, then there's no need for function f() to call set_error on that path. That applies even if there are other disjoint paths where function f() calls set_error, eg. because f() calls malloc directly.
- as the commit message says, the blanket "malloc" reference in prepare_socket_activation_environment() is not accurate enough, and certainly will not be accurate any longer with later patches (e.g. patch #26, which returns -1/EOVERFLOW upon ADD_OVERFLOW() failing).
I'm unconvinced, couldn't you change the original message to be something like this? set_error (errno, "prepare_socket_activation_environment: malloc");
Note that in patch #19, a very similar cleanup is performed for CONNECT_COMMAND.START; there, we supply a missing set_error() for fcntl(), plus a *comment* that nbd_internal_socket_create() sets the error internally.
Adding missing calls to set_error is good, no problem with that.
(I disagree with nbd_internal_socket_create() setting the error internally, but that function is too widely called to move set_error() out of it, to all its callers, and again I needed to contain the scope creep. So, for at least restoring the "visual" uniformity of set_error() calls in CONNECT_COMMAND.START, I added the comment.)
Thanks! Laszlo
Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
On 2/21/23 14:24, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 02:17:15PM +0100, Laszlo Ersek wrote:
On 2/15/23 17:39, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
This is not a cast, but a C99 compound literal. And yes, it is necessary, as empty_vector is just:
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
So this is *not* initialization, but assignment. We have a string_vector object (a structure) on the LHS, so we ned a structure on the RHS as well. The compound literal provides that (unnamed, automatic storage duration) structure. It looks like a cast (quite intentionally, I'd hazard), but it's not a cast.
OK it's not a cast, but struct assignment is well defined so is the change necessary?
Apologies, I don't understand. I think you may be asking one of two questions: (1) is it useful to move the zeroing of "env" into prepare_socket_activation_environment()? (2) if we decide that (1) is useful, then is the "cast-like" (string_vector) construct necessary? The answer to (2) is absolutely "yes"; if we don't put (string_vector) in front of "empty_vector", that is, if we try *env = empty_vector; then that's not a structure assignment, it is a syntax error. The answer to (1) is not "absolute". My *opinion* (as stated in the commit message) is that yes, "env" should be zeroed in the callee, not the caller -- because "env" is a purely output parameter for the callee, not an input-output parameter. That is, pre-patch, the responsibilities are incorrectly distributed: the caller zeroes, the callee populates, the caller consumes. This would only make sense if the callee's population step actually depended on pre-existent information in "env", but that's not the case. Therefore the right distribution of responsibilities (in my opinion!) is that the callee should both zero and populate, and the caller should consume.
@@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function?
Two reasons:
- in the caller (CONNECT_SA.START handler), every other failure branch calls set_error explicitly (and subsequent patches in the series will uphold the same pattern),
The pattern is actually that we call set_error once on each error path [which is surprisingly hard to get right -- we've even tried to write verifier tools for this in the past].
If a function f() calls function g(), where the g() will call set_error, then there's no need for function f() to call set_error on that path. That applies even if there are other disjoint paths where function f() calls set_error, eg. because f() calls malloc directly.
I agree. This pattern (invariant) is satisfied both pre-patch and post-patch. My point concerns the *depth* on the one particular error path here at which the error should be set.
- as the commit message says, the blanket "malloc" reference in prepare_socket_activation_environment() is not accurate enough, and certainly will not be accurate any longer with later patches (e.g. patch #26, which returns -1/EOVERFLOW upon ADD_OVERFLOW() failing).
I'm unconvinced, couldn't you change the original message to be something like this?
set_error (errno, "prepare_socket_activation_environment: malloc");
This is the weaker part of my argument. The stronger part (as I see it) is that set_error(), while it should *indeed* remain the sole unique set_error() call on the affected error *path*, belongs in the caller, not the callee -- that is, to a different depth of the same path. That's all. If you disagree with that, then I'll have to drop this patch. Thanks, Laszlo
Note that in patch #19, a very similar cleanup is performed for CONNECT_COMMAND.START; there, we supply a missing set_error() for fcntl(), plus a *comment* that nbd_internal_socket_create() sets the error internally.
Adding missing calls to set_error is good, no problem with that.
(I disagree with nbd_internal_socket_create() setting the error internally, but that function is too widely called to move set_error() out of it, to all its callers, and again I needed to contain the scope creep. So, for at least restoring the "visual" uniformity of set_error() calls in CONNECT_COMMAND.START, I added the comment.)
Thanks! Laszlo
Rich.
On Tue, Feb 21, 2023 at 05:11:53PM +0100, Laszlo Ersek wrote:
On 2/21/23 14:24, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 02:17:15PM +0100, Laszlo Ersek wrote:
On 2/15/23 17:39, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
This is not a cast, but a C99 compound literal. And yes, it is necessary, as empty_vector is just:
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
So this is *not* initialization, but assignment. We have a string_vector object (a structure) on the LHS, so we ned a structure on the RHS as well. The compound literal provides that (unnamed, automatic storage duration) structure. It looks like a cast (quite intentionally, I'd hazard), but it's not a cast.
OK it's not a cast, but struct assignment is well defined so is the change necessary?
Apologies, I don't understand.
I think you may be asking one of two questions:
(1) is it useful to move the zeroing of "env" into prepare_socket_activation_environment()?
So I agree with this one, but ...
(2) if we decide that (1) is useful, then is the "cast-like" (string_vector) construct necessary?
... this was my question and ...
The answer to (2) is absolutely "yes"; if we don't put (string_vector) in front of "empty_vector", that is, if we try
*env = empty_vector;
then that's not a structure assignment, it is a syntax error.
Right, good point, so ACK to this change, thanks for explaining it to me slowly!
The answer to (1) is not "absolute". My *opinion* (as stated in the commit message) is that yes, "env" should be zeroed in the callee, not the caller -- because "env" is a purely output parameter for the callee, not an input-output parameter. That is, pre-patch, the responsibilities are incorrectly distributed: the caller zeroes, the callee populates, the caller consumes. This would only make sense if the callee's population step actually depended on pre-existent information in "env", but that's not the case. Therefore the right distribution of responsibilities (in my opinion!) is that the callee should both zero and populate, and the caller should consume.
@@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function?
Two reasons:
- in the caller (CONNECT_SA.START handler), every other failure branch calls set_error explicitly (and subsequent patches in the series will uphold the same pattern),
The pattern is actually that we call set_error once on each error path [which is surprisingly hard to get right -- we've even tried to write verifier tools for this in the past].
If a function f() calls function g(), where the g() will call set_error, then there's no need for function f() to call set_error on that path. That applies even if there are other disjoint paths where function f() calls set_error, eg. because f() calls malloc directly.
I agree. This pattern (invariant) is satisfied both pre-patch and post-patch. My point concerns the *depth* on the one particular error path here at which the error should be set.
- as the commit message says, the blanket "malloc" reference in prepare_socket_activation_environment() is not accurate enough, and certainly will not be accurate any longer with later patches (e.g. patch #26, which returns -1/EOVERFLOW upon ADD_OVERFLOW() failing).
I'm unconvinced, couldn't you change the original message to be something like this?
set_error (errno, "prepare_socket_activation_environment: malloc");
This is the weaker part of my argument. The stronger part (as I see it) is that set_error(), while it should *indeed* remain the sole unique set_error() call on the affected error *path*, belongs in the caller, not the callee -- that is, to a different depth of the same path. That's all.
If you disagree with that, then I'll have to drop this patch.
Can we keep the first bit (moving the zeroing of *env), and drop this change that moves set_error out of the function? Rich.
Thanks, Laszlo
Note that in patch #19, a very similar cleanup is performed for CONNECT_COMMAND.START; there, we supply a missing set_error() for fcntl(), plus a *comment* that nbd_internal_socket_create() sets the error internally.
Adding missing calls to set_error is good, no problem with that.
(I disagree with nbd_internal_socket_create() setting the error internally, but that function is too widely called to move set_error() out of it, to all its callers, and again I needed to contain the scope creep. So, for at least restoring the "visual" uniformity of set_error() calls in CONNECT_COMMAND.START, I added the comment.)
Thanks! Laszlo
Rich.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
On 2/21/23 17:22, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 05:11:53PM +0100, Laszlo Ersek wrote:
On 2/21/23 14:24, Richard W.M. Jones wrote:
On Tue, Feb 21, 2023 at 02:17:15PM +0100, Laszlo Ersek wrote:
On 2/15/23 17:39, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:41PM +0100, Laszlo Ersek wrote:
prepare_socket_activation_environment() is a construction function that is supposed to fill in a string_vector object from the ground up. Right now it has its responsibilities mixed up in two ways:
- it expects the caller to pass in a previously re-set string_vector,
- if it fails, it calls set_error() internally (with a blanket reference to "malloc").
Fix both warts:
- pass in an *uninitialized* (only allocated) string vector from the caller, and initialize it in prepare_socket_activation_environment(),
- move the set_error() call out to the caller.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index c46a0bf5c0a3..b5e146539cc8 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
This is not a cast, but a C99 compound literal. And yes, it is necessary, as empty_vector is just:
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
So this is *not* initialization, but assignment. We have a string_vector object (a structure) on the LHS, so we ned a structure on the RHS as well. The compound literal provides that (unnamed, automatic storage duration) structure. It looks like a cast (quite intentionally, I'd hazard), but it's not a cast.
OK it's not a cast, but struct assignment is well defined so is the change necessary?
Apologies, I don't understand.
I think you may be asking one of two questions:
(1) is it useful to move the zeroing of "env" into prepare_socket_activation_environment()?
So I agree with this one, but ...
(2) if we decide that (1) is useful, then is the "cast-like" (string_vector) construct necessary?
... this was my question and ...
The answer to (2) is absolutely "yes"; if we don't put (string_vector) in front of "empty_vector", that is, if we try
*env = empty_vector;
then that's not a structure assignment, it is a syntax error.
Right, good point, so ACK to this change, thanks for explaining it to me slowly!
The answer to (1) is not "absolute". My *opinion* (as stated in the commit message) is that yes, "env" should be zeroed in the callee, not the caller -- because "env" is a purely output parameter for the callee, not an input-output parameter. That is, pre-patch, the responsibilities are incorrectly distributed: the caller zeroes, the callee populates, the caller consumes. This would only make sense if the callee's population step actually depended on pre-existent information in "env", but that's not the case. Therefore the right distribution of responsibilities (in my opinion!) is that the callee should both zero and populate, and the caller should consume.
@@ -156,6 +155,7 @@ CONNECT_SA.START:
if (prepare_socket_activation_environment (&env) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "prepare_socket_activation_environment");
Why move this out of the function?
Two reasons:
- in the caller (CONNECT_SA.START handler), every other failure branch calls set_error explicitly (and subsequent patches in the series will uphold the same pattern),
The pattern is actually that we call set_error once on each error path [which is surprisingly hard to get right -- we've even tried to write verifier tools for this in the past].
If a function f() calls function g(), where the g() will call set_error, then there's no need for function f() to call set_error on that path. That applies even if there are other disjoint paths where function f() calls set_error, eg. because f() calls malloc directly.
I agree. This pattern (invariant) is satisfied both pre-patch and post-patch. My point concerns the *depth* on the one particular error path here at which the error should be set.
- as the commit message says, the blanket "malloc" reference in prepare_socket_activation_environment() is not accurate enough, and certainly will not be accurate any longer with later patches (e.g. patch #26, which returns -1/EOVERFLOW upon ADD_OVERFLOW() failing).
I'm unconvinced, couldn't you change the original message to be something like this?
set_error (errno, "prepare_socket_activation_environment: malloc");
This is the weaker part of my argument. The stronger part (as I see it) is that set_error(), while it should *indeed* remain the sole unique set_error() call on the affected error *path*, belongs in the caller, not the callee -- that is, to a different depth of the same path. That's all.
If you disagree with that, then I'll have to drop this patch.
Can we keep the first bit (moving the zeroing of *env), and drop this change that moves set_error out of the function?
It will create a whole lot of rebase conflicts, but yes, I can try. Laszlo
Rich.
Thanks, Laszlo
Note that in patch #19, a very similar cleanup is performed for CONNECT_COMMAND.START; there, we supply a missing set_error() for fcntl(), plus a *comment* that nbd_internal_socket_create() sets the error internally.
Adding missing calls to set_error is good, no problem with that.
(I disagree with nbd_internal_socket_create() setting the error internally, but that function is too widely called to move set_error() out of it, to all its callers, and again I needed to contain the scope creep. So, for at least restoring the "visual" uniformity of set_error() calls in CONNECT_COMMAND.START, I added the comment.)
Thanks! Laszlo
Rich.
On Tue, Feb 21, 2023 at 01:24:17PM +0000, Richard W.M. Jones wrote:
+++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
This is not a cast, but a C99 compound literal. And yes, it is necessary, as empty_vector is just:
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
So this is *not* initialization, but assignment. We have a string_vector object (a structure) on the LHS, so we ned a structure on the RHS as well. The compound literal provides that (unnamed, automatic storage duration) structure. It looks like a cast (quite intentionally, I'd hazard), but it's not a cast.
OK it's not a cast, but struct assignment is well defined so is the change necessary?
Struct assignment requires a source struct. Either a named one (which we do not have here without declaring one), or via a compound literal (which resembles a cast). It sounds like it would be nice to have a type-specific macro, as in 'empty_string_vector', 'empty_int_vector', and so on, per each use of DEFINE_VECTOR_TYPE() - but while our one macro can easily declare other type-specific functions, you can't really use the preprocessor to define further type-specific macros. So having to tell the user to supply their own type name when reusing the type-agnostic initializer portion of a compound literal is not too bad in the long run, although it might be worth documenting the practice in vector.h. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
On 2/21/23 19:38, Eric Blake wrote:
On Tue, Feb 21, 2023 at 01:24:17PM +0000, Richard W.M. Jones wrote:
+++ b/generator/states-connect-socket-activation.c @@ -51,7 +51,7 @@ prepare_socket_activation_environment (string_vector *env) char *p; size_t i;
- assert (env->len == 0); + *env = (string_vector)empty_vector;
Do you actually need to cast this?
This is not a cast, but a C99 compound literal. And yes, it is necessary, as empty_vector is just:
#define empty_vector { .ptr = NULL, .len = 0, .cap = 0 }
So this is *not* initialization, but assignment. We have a string_vector object (a structure) on the LHS, so we ned a structure on the RHS as well. The compound literal provides that (unnamed, automatic storage duration) structure. It looks like a cast (quite intentionally, I'd hazard), but it's not a cast.
OK it's not a cast, but struct assignment is well defined so is the change necessary?
Struct assignment requires a source struct. Either a named one (which we do not have here without declaring one), or via a compound literal (which resembles a cast).
It sounds like it would be nice to have a type-specific macro, as in 'empty_string_vector', 'empty_int_vector', and so on, per each use of DEFINE_VECTOR_TYPE() - but while our one macro can easily declare other type-specific functions, you can't really use the preprocessor to define further type-specific macros. So having to tell the user to supply their own type name when reusing the type-agnostic initializer portion of a compound literal is not too bad in the long run, although it might be worth documenting the practice in vector.h.
... another follow-up item :)
F_SETFD takes an "int", so it stands to reason that FD_CLOEXEC expands to an "int". In turn, it's bad hygiene to manipulate the sign bit of (signed) integers with bit operations: ~FD_CLOEXEC Convert FD_CLOEXEC to "unsigned int" for the bitwise complement operator: ~(unsigned)FD_CLOEXEC The bitwise complement then results in an "unsigned int". "Flags" (of type "int", and having, per POSIX, a non-negative value returned by fcntl(F_GETFD)) will be automatically converted to "unsigned int" by the usual arithmetic conversions for the bitwise AND operator: flags & ~(unsigned)FD_CLOEXEC We could pass the result of *that* to fcntl(), due to (a) the value being in range for "signed int" ("flags" is a non-negative "int", and we're only clearing a value bit), and (b) non-negative "int" values being represented identically by "unsigned int" (C99 6.2.5 p9). But, for clarity, let's cast the result to "int" explicitly: (int)(flags & ~(unsigned)FD_CLOEXEC) Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U5 generator/states-connect-socket-activation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index b5e146539cc8..729f37d897fb 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -181,11 +181,11 @@ CONNECT_SA.START: int flags = fcntl (s, F_GETFD, 0); if (flags == -1) { nbd_internal_fork_safe_perror ("fcntl: F_GETFD"); _exit (126); } - if (fcntl (s, F_SETFD, flags & ~FD_CLOEXEC) == -1) { + if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) { nbd_internal_fork_safe_perror ("fcntl: F_SETFD"); _exit (126); } }
[I've no idea about this, adding Eric to CC for attention] On Wed, Feb 15, 2023 at 03:11:42PM +0100, Laszlo Ersek wrote:
F_SETFD takes an "int", so it stands to reason that FD_CLOEXEC expands to an "int". In turn, it's bad hygiene to manipulate the sign bit of (signed) integers with bit operations:
~FD_CLOEXEC
Convert FD_CLOEXEC to "unsigned int" for the bitwise complement operator:
~(unsigned)FD_CLOEXEC
The bitwise complement then results in an "unsigned int". "Flags" (of type "int", and having, per POSIX, a non-negative value returned by fcntl(F_GETFD)) will be automatically converted to "unsigned int" by the usual arithmetic conversions for the bitwise AND operator:
flags & ~(unsigned)FD_CLOEXEC
We could pass the result of *that* to fcntl(), due to (a) the value being in range for "signed int" ("flags" is a non-negative "int", and we're only clearing a value bit), and (b) non-negative "int" values being represented identically by "unsigned int" (C99 6.2.5 p9). But, for clarity, let's cast the result to "int" explicitly:
(int)(flags & ~(unsigned)FD_CLOEXEC)
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U5
generator/states-connect-socket-activation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index b5e146539cc8..729f37d897fb 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -181,11 +181,11 @@ CONNECT_SA.START: int flags = fcntl (s, F_GETFD, 0); if (flags == -1) { nbd_internal_fork_safe_perror ("fcntl: F_GETFD"); _exit (126); } - if (fcntl (s, F_SETFD, flags & ~FD_CLOEXEC) == -1) { + if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) { nbd_internal_fork_safe_perror ("fcntl: F_SETFD"); _exit (126); } }
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
It's bad hygiene to just assume dup2(), close() and signal() will succeed, especially considering the inconsistency that we do check fcntl(). Check all return values. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U12 generator/states-connect-socket-activation.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 729f37d897fb..766f46177bf3 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -161,50 +161,59 @@ CONNECT_SA.START: } pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (s); string_vector_empty (&env); return 0; } if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { - dup2 (s, FIRST_SOCKET_ACTIVATION_FD); - close (s); + if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { + nbd_internal_fork_safe_perror ("dup2"); + _exit (126); + } + if (close (s) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + } } else { /* We must unset CLOEXEC on the fd. (dup2 above does this * implicitly because CLOEXEC is set on the fd, not on the * socket). */ int flags = fcntl (s, F_GETFD, 0); if (flags == -1) { nbd_internal_fork_safe_perror ("fcntl: F_GETFD"); _exit (126); } if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) { nbd_internal_fork_safe_perror ("fcntl: F_SETFD"); _exit (126); } } char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); strcpy (&env.ptr[0][PREFIX_LENGTH], v); /* Restore SIGPIPE back to SIG_DFL. */ - signal (SIGPIPE, SIG_DFL); + if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { + nbd_internal_fork_safe_perror ("signal"); + _exit (126); + } environ = env.ptr; execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } /* Parent. */ close (s);
On Wed, Feb 15, 2023 at 03:11:43PM +0100, Laszlo Ersek wrote:
It's bad hygiene to just assume dup2(), close() and signal() will succeed, especially considering the inconsistency that we do check fcntl(). Check all return values.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U12
generator/states-connect-socket-activation.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 729f37d897fb..766f46177bf3 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -161,50 +161,59 @@ CONNECT_SA.START: }
pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (s); string_vector_empty (&env); return 0; } if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { - dup2 (s, FIRST_SOCKET_ACTIVATION_FD); - close (s); + if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { + nbd_internal_fork_safe_perror ("dup2"); + _exit (126); + } + if (close (s) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + } } else { /* We must unset CLOEXEC on the fd. (dup2 above does this * implicitly because CLOEXEC is set on the fd, not on the * socket). */ int flags = fcntl (s, F_GETFD, 0); if (flags == -1) { nbd_internal_fork_safe_perror ("fcntl: F_GETFD"); _exit (126); } if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) { nbd_internal_fork_safe_perror ("fcntl: F_SETFD"); _exit (126); } }
char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); strcpy (&env.ptr[0][PREFIX_LENGTH], v);
/* Restore SIGPIPE back to SIG_DFL. */ - signal (SIGPIPE, SIG_DFL); + if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { + nbd_internal_fork_safe_perror ("signal"); + _exit (126); + }
environ = env.ptr; execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); }
/* Parent. */ close (s);
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
The CONNECT_SA.START handler constructs several resources; some of those are needed permanently if the entire handler succeeds, while others are needed only temporarily, for constructing the permanent objects. Currently, the various error branches in the handler deal with resource release individually, leading to code duplication and dispersed responsibilities; if we want to construct a new resource, then, under the existent pattern, we'll have to modify multiple error branches to release it as well. In my opinion, the best pattern for dealing with this scenario is the following structure: - Place cascading error handling labels at the end of the function -- an error handling slide in effect. The order of destruction on this slide is the inverse of construction. - Whenever an operation fails mid-construction, enter the slide such that precisely the thus far constructed objects be freed. Note that the goto statements and the destination labels nest properly. - The very last construction step needs no rollback, because the last successful step completes the entire function / handler. When this happens, *commit* by (a) outputting the permanent resources, (b) setting a top-level "final result" variable to "success" (aka "ownership of permanent resources transferred to the caller"), and (c) falling through to the slide. On the slide, use the "final result" variable to skip the release of those resources that have been output as permanent ones. This way, each resource is freed in exactly one location, and whenever an error occurs, no resource is even *checked* for rollback if its construction could not be tried, or completed, in the first place. The introduction of a new resource needs one properly placed construction step and one matchingly placed error handling label. The restructuring highlights the following leak: whenever any construction step after bind() fails, the UNIX domain socket address (i.e., the pathname in the filesystem) is leaked. (The filesystem object may well be removed once the application decides to call nbd_close(), but until then, after the CONNECT_SA.START handler returns with failure, unusable (half-constructed) resources linger around indefinitely. A library API should either fully succeed or fully fail.) We'll plug the leak later in this series. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-W generator/states-connect-socket-activation.c | 82 ++++++++++++-------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 766f46177bf3..164b8e6828f2 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -96,132 +96,146 @@ prepare_socket_activation_environment (string_vector *env) STATE_MACHINE { CONNECT_SA.START: + enum state next; + char *tmpdir; + char *sockpath; int s; struct sockaddr_un addr; string_vector env; pid_t pid; assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); + next = %.DEAD; + /* Use /tmp instead of TMPDIR because we must ensure the path is * short enough to store in the sockaddr_un. On some platforms this * may cause problems so we may need to revisit it. XXX */ - h->sact_tmpdir = strdup ("/tmp/libnbdXXXXXX"); - if (h->sact_tmpdir == NULL) { - SET_NEXT_STATE (%.DEAD); + tmpdir = strdup ("/tmp/libnbdXXXXXX"); + if (tmpdir == NULL) { set_error (errno, "strdup"); - return 0; + goto done; } - if (mkdtemp (h->sact_tmpdir) == NULL) { - SET_NEXT_STATE (%.DEAD); + + if (mkdtemp (tmpdir) == NULL) { set_error (errno, "mkdtemp"); - /* Avoid cleanup in nbd_close. */ - free (h->sact_tmpdir); - h->sact_tmpdir = NULL; - return 0; + goto free_tmpdir; } - if (asprintf (&h->sact_sockpath, "%s/sock", h->sact_tmpdir) == -1) { - SET_NEXT_STATE (%.DEAD); + if (asprintf (&sockpath, "%s/sock", tmpdir) == -1) { set_error (errno, "asprintf"); - return 0; + goto rmdir_tmpdir; } s = nbd_internal_socket (AF_UNIX, SOCK_STREAM, 0, false); if (s == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "socket"); - return 0; + goto free_sockpath; } addr.sun_family = AF_UNIX; - memcpy (addr.sun_path, h->sact_sockpath, strlen (h->sact_sockpath) + 1); + memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { - SET_NEXT_STATE (%.DEAD); - set_error (errno, "bind: %s", h->sact_sockpath); - close (s); - return 0; + set_error (errno, "bind: %s", sockpath); + goto close_socket; } if (listen (s, SOMAXCONN) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "listen"); - close (s); - return 0; + goto close_socket; } if (prepare_socket_activation_environment (&env) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "prepare_socket_activation_environment"); - close (s); - return 0; + goto close_socket; } pid = fork (); if (pid == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); - close (s); - string_vector_empty (&env); - return 0; + goto empty_env; } + if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { nbd_internal_fork_safe_perror ("dup2"); _exit (126); } if (close (s) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } } else { /* We must unset CLOEXEC on the fd. (dup2 above does this * implicitly because CLOEXEC is set on the fd, not on the * socket). */ int flags = fcntl (s, F_GETFD, 0); if (flags == -1) { nbd_internal_fork_safe_perror ("fcntl: F_GETFD"); _exit (126); } if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) { nbd_internal_fork_safe_perror ("fcntl: F_SETFD"); _exit (126); } } char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); strcpy (&env.ptr[0][PREFIX_LENGTH], v); /* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); } environ = env.ptr; execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } - /* Parent. */ - close (s); - string_vector_empty (&env); + /* Parent -- we're done; commit. */ + h->sact_tmpdir = tmpdir; + h->sact_sockpath = sockpath; h->pid = pid; h->connaddrlen = sizeof addr; memcpy (&h->connaddr, &addr, h->connaddrlen); - SET_NEXT_STATE (%^CONNECT.START); + next = %^CONNECT.START; + + /* fall through, for releasing the temporaries */ + +empty_env: + string_vector_empty (&env); + +close_socket: + close (s); + +free_sockpath: + if (next == %.DEAD) + free (sockpath); + +rmdir_tmpdir: + if (next == %.DEAD) + rmdir (tmpdir); + +free_tmpdir: + if (next == %.DEAD) + free (tmpdir); + +done: + SET_NEXT_STATE (next); return 0; } /* END STATE MACHINE */
On Wed, Feb 15, 2023 at 03:11:44PM +0100, Laszlo Ersek wrote:
The CONNECT_SA.START handler constructs several resources; some of those are needed permanently if the entire handler succeeds, while others are needed only temporarily, for constructing the permanent objects. Currently, the various error branches in the handler deal with resource release individually, leading to code duplication and dispersed responsibilities; if we want to construct a new resource, then, under the existent pattern, we'll have to modify multiple error branches to release it as well.
In my opinion, the best pattern for dealing with this scenario is the following structure:
- Place cascading error handling labels at the end of the function -- an error handling slide in effect. The order of destruction on this slide is the inverse of construction.
- Whenever an operation fails mid-construction, enter the slide such that precisely the thus far constructed objects be freed. Note that the goto statements and the destination labels nest properly.
- The very last construction step needs no rollback, because the last successful step completes the entire function / handler. When this happens, *commit* by (a) outputting the permanent resources, (b) setting a top-level "final result" variable to "success" (aka "ownership of permanent resources transferred to the caller"), and (c) falling through to the slide. On the slide, use the "final result" variable to skip the release of those resources that have been output as permanent ones.
This way, each resource is freed in exactly one location, and whenever an error occurs, no resource is even *checked* for rollback if its construction could not be tried, or completed, in the first place. The introduction of a new resource needs one properly placed construction step and one matchingly placed error handling label.
The restructuring highlights the following leak: whenever any construction step after bind() fails, the UNIX domain socket address (i.e., the pathname in the filesystem) is leaked. (The filesystem object may well be removed once the application decides to call nbd_close(), but until then, after the CONNECT_SA.START handler returns with failure, unusable (half-constructed) resources linger around indefinitely. A library API should either fully succeed or fully fail.) We'll plug the leak later in this series.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-W
generator/states-connect-socket-activation.c | 82 ++++++++++++-------- 1 file changed, 48 insertions(+), 34 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 766f46177bf3..164b8e6828f2 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -96,132 +96,146 @@ prepare_socket_activation_environment (string_vector *env)
STATE_MACHINE { CONNECT_SA.START: + enum state next; + char *tmpdir; + char *sockpath; int s; struct sockaddr_un addr; string_vector env; pid_t pid;
assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]);
+ next = %.DEAD; + /* Use /tmp instead of TMPDIR because we must ensure the path is * short enough to store in the sockaddr_un. On some platforms this * may cause problems so we may need to revisit it. XXX */ - h->sact_tmpdir = strdup ("/tmp/libnbdXXXXXX"); - if (h->sact_tmpdir == NULL) { - SET_NEXT_STATE (%.DEAD); + tmpdir = strdup ("/tmp/libnbdXXXXXX"); + if (tmpdir == NULL) { set_error (errno, "strdup"); - return 0; + goto done; } - if (mkdtemp (h->sact_tmpdir) == NULL) { - SET_NEXT_STATE (%.DEAD); + + if (mkdtemp (tmpdir) == NULL) { set_error (errno, "mkdtemp"); - /* Avoid cleanup in nbd_close. */ - free (h->sact_tmpdir); - h->sact_tmpdir = NULL; - return 0; + goto free_tmpdir; }
- if (asprintf (&h->sact_sockpath, "%s/sock", h->sact_tmpdir) == -1) { - SET_NEXT_STATE (%.DEAD); + if (asprintf (&sockpath, "%s/sock", tmpdir) == -1) { set_error (errno, "asprintf"); - return 0; + goto rmdir_tmpdir; }
s = nbd_internal_socket (AF_UNIX, SOCK_STREAM, 0, false); if (s == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "socket"); - return 0; + goto free_sockpath; }
addr.sun_family = AF_UNIX; - memcpy (addr.sun_path, h->sact_sockpath, strlen (h->sact_sockpath) + 1); + memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { - SET_NEXT_STATE (%.DEAD); - set_error (errno, "bind: %s", h->sact_sockpath); - close (s); - return 0; + set_error (errno, "bind: %s", sockpath); + goto close_socket; }
if (listen (s, SOMAXCONN) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "listen"); - close (s); - return 0; + goto close_socket; }
if (prepare_socket_activation_environment (&env) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "prepare_socket_activation_environment"); - close (s); - return 0; + goto close_socket; }
pid = fork (); if (pid == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); - close (s); - string_vector_empty (&env); - return 0; + goto empty_env; } + if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { nbd_internal_fork_safe_perror ("dup2"); _exit (126); } if (close (s) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } } else { /* We must unset CLOEXEC on the fd. (dup2 above does this * implicitly because CLOEXEC is set on the fd, not on the * socket). */ int flags = fcntl (s, F_GETFD, 0); if (flags == -1) { nbd_internal_fork_safe_perror ("fcntl: F_GETFD"); _exit (126); } if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) { nbd_internal_fork_safe_perror ("fcntl: F_SETFD"); _exit (126); } }
char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); strcpy (&env.ptr[0][PREFIX_LENGTH], v);
/* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); }
environ = env.ptr; execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); }
- /* Parent. */ - close (s); - string_vector_empty (&env); + /* Parent -- we're done; commit. */ + h->sact_tmpdir = tmpdir; + h->sact_sockpath = sockpath; h->pid = pid;
h->connaddrlen = sizeof addr; memcpy (&h->connaddr, &addr, h->connaddrlen); - SET_NEXT_STATE (%^CONNECT.START); + next = %^CONNECT.START; + + /* fall through, for releasing the temporaries */ + +empty_env: + string_vector_empty (&env); + +close_socket: + close (s); + +free_sockpath: + if (next == %.DEAD) + free (sockpath); + +rmdir_tmpdir: + if (next == %.DEAD) + rmdir (tmpdir); + +free_tmpdir: + if (next == %.DEAD) + free (tmpdir); + +done: + SET_NEXT_STATE (next); return 0; } /* END STATE MACHINE */
A note that state machine labels match the basic regexp pattern: ^ \\([A-Z0-9][A-Z0-9_\\.]*\\):$ (see generator/state_machine_generator.ml), and because of the all-capitalization of state names, they won't match the lower case ordinary C labels we're using here. Reviewed-by: Richard W.M. Jones <rjones@redhat.com> Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
On 2/15/23 17:53, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:44PM +0100, Laszlo Ersek wrote:
The CONNECT_SA.START handler constructs several resources; some of those are needed permanently if the entire handler succeeds, while others are needed only temporarily, for constructing the permanent objects. Currently, the various error branches in the handler deal with resource release individually, leading to code duplication and dispersed responsibilities; if we want to construct a new resource, then, under the existent pattern, we'll have to modify multiple error branches to release it as well.
In my opinion, the best pattern for dealing with this scenario is the following structure:
- Place cascading error handling labels at the end of the function -- an error handling slide in effect. The order of destruction on this slide is the inverse of construction.
- Whenever an operation fails mid-construction, enter the slide such that precisely the thus far constructed objects be freed. Note that the goto statements and the destination labels nest properly.
- The very last construction step needs no rollback, because the last successful step completes the entire function / handler. When this happens, *commit* by (a) outputting the permanent resources, (b) setting a top-level "final result" variable to "success" (aka "ownership of permanent resources transferred to the caller"), and (c) falling through to the slide. On the slide, use the "final result" variable to skip the release of those resources that have been output as permanent ones.
This way, each resource is freed in exactly one location, and whenever an error occurs, no resource is even *checked* for rollback if its construction could not be tried, or completed, in the first place. The introduction of a new resource needs one properly placed construction step and one matchingly placed error handling label.
The restructuring highlights the following leak: whenever any construction step after bind() fails, the UNIX domain socket address (i.e., the pathname in the filesystem) is leaked. (The filesystem object may well be removed once the application decides to call nbd_close(), but until then, after the CONNECT_SA.START handler returns with failure, unusable (half-constructed) resources linger around indefinitely. A library API should either fully succeed or fully fail.) We'll plug the leak later in this series.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-W
generator/states-connect-socket-activation.c | 82 ++++++++++++-------- 1 file changed, 48 insertions(+), 34 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 766f46177bf3..164b8e6828f2 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -96,132 +96,146 @@ prepare_socket_activation_environment (string_vector *env)
STATE_MACHINE { CONNECT_SA.START: + enum state next; + char *tmpdir; + char *sockpath; int s; struct sockaddr_un addr; string_vector env; pid_t pid;
assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]);
+ next = %.DEAD; + /* Use /tmp instead of TMPDIR because we must ensure the path is * short enough to store in the sockaddr_un. On some platforms this * may cause problems so we may need to revisit it. XXX */ - h->sact_tmpdir = strdup ("/tmp/libnbdXXXXXX"); - if (h->sact_tmpdir == NULL) { - SET_NEXT_STATE (%.DEAD); + tmpdir = strdup ("/tmp/libnbdXXXXXX"); + if (tmpdir == NULL) { set_error (errno, "strdup"); - return 0; + goto done; } - if (mkdtemp (h->sact_tmpdir) == NULL) { - SET_NEXT_STATE (%.DEAD); + + if (mkdtemp (tmpdir) == NULL) { set_error (errno, "mkdtemp"); - /* Avoid cleanup in nbd_close. */ - free (h->sact_tmpdir); - h->sact_tmpdir = NULL; - return 0; + goto free_tmpdir; }
- if (asprintf (&h->sact_sockpath, "%s/sock", h->sact_tmpdir) == -1) { - SET_NEXT_STATE (%.DEAD); + if (asprintf (&sockpath, "%s/sock", tmpdir) == -1) { set_error (errno, "asprintf"); - return 0; + goto rmdir_tmpdir; }
s = nbd_internal_socket (AF_UNIX, SOCK_STREAM, 0, false); if (s == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "socket"); - return 0; + goto free_sockpath; }
addr.sun_family = AF_UNIX; - memcpy (addr.sun_path, h->sact_sockpath, strlen (h->sact_sockpath) + 1); + memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { - SET_NEXT_STATE (%.DEAD); - set_error (errno, "bind: %s", h->sact_sockpath); - close (s); - return 0; + set_error (errno, "bind: %s", sockpath); + goto close_socket; }
if (listen (s, SOMAXCONN) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "listen"); - close (s); - return 0; + goto close_socket; }
if (prepare_socket_activation_environment (&env) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "prepare_socket_activation_environment"); - close (s); - return 0; + goto close_socket; }
pid = fork (); if (pid == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); - close (s); - string_vector_empty (&env); - return 0; + goto empty_env; } + if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { nbd_internal_fork_safe_perror ("dup2"); _exit (126); } if (close (s) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } } else { /* We must unset CLOEXEC on the fd. (dup2 above does this * implicitly because CLOEXEC is set on the fd, not on the * socket). */ int flags = fcntl (s, F_GETFD, 0); if (flags == -1) { nbd_internal_fork_safe_perror ("fcntl: F_GETFD"); _exit (126); } if (fcntl (s, F_SETFD, (int)(flags & ~(unsigned)FD_CLOEXEC)) == -1) { nbd_internal_fork_safe_perror ("fcntl: F_SETFD"); _exit (126); } }
char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); strcpy (&env.ptr[0][PREFIX_LENGTH], v);
/* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); }
environ = env.ptr; execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); }
- /* Parent. */ - close (s); - string_vector_empty (&env); + /* Parent -- we're done; commit. */ + h->sact_tmpdir = tmpdir; + h->sact_sockpath = sockpath; h->pid = pid;
h->connaddrlen = sizeof addr; memcpy (&h->connaddr, &addr, h->connaddrlen); - SET_NEXT_STATE (%^CONNECT.START); + next = %^CONNECT.START; + + /* fall through, for releasing the temporaries */ + +empty_env: + string_vector_empty (&env); + +close_socket: + close (s); + +free_sockpath: + if (next == %.DEAD) + free (sockpath); + +rmdir_tmpdir: + if (next == %.DEAD) + rmdir (tmpdir); + +free_tmpdir: + if (next == %.DEAD) + free (tmpdir); + +done: + SET_NEXT_STATE (next); return 0; } /* END STATE MACHINE */
A note that state machine labels match the basic regexp pattern:
^ \\([A-Z0-9][A-Z0-9_\\.]*\\):$
(see generator/state_machine_generator.ml), and because of the all-capitalization of state names, they won't match the lower case ordinary C labels we're using here.
Ah, thank you for pointing that out! I'll capture this in the commit message. Laszlo
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Rich.
A prior patch highlighted the following leak: whenever any construction step after bind() fails, the UNIX domain socket address (i.e., the pathname in the filesystem) is leaked. Plug the leak by inserting a new error handling section that unlinks the pathname. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U12 generator/states-connect-socket-activation.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 164b8e6828f2..81bb850c7d8c 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -136,30 +136,30 @@ CONNECT_SA.START: goto free_sockpath; } addr.sun_family = AF_UNIX; memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { set_error (errno, "bind: %s", sockpath); goto close_socket; } if (listen (s, SOMAXCONN) == -1) { set_error (errno, "listen"); - goto close_socket; + goto unlink_sockpath; } if (prepare_socket_activation_environment (&env) == -1) { set_error (errno, "prepare_socket_activation_environment"); - goto close_socket; + goto unlink_sockpath; } pid = fork (); if (pid == -1) { set_error (errno, "fork"); goto empty_env; } if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { nbd_internal_fork_safe_perror ("dup2"); @@ -211,24 +211,28 @@ CONNECT_SA.START: h->sact_sockpath = sockpath; h->pid = pid; h->connaddrlen = sizeof addr; memcpy (&h->connaddr, &addr, h->connaddrlen); next = %^CONNECT.START; /* fall through, for releasing the temporaries */ empty_env: string_vector_empty (&env); +unlink_sockpath: + if (next == %.DEAD) + unlink (sockpath); + close_socket: close (s); free_sockpath: if (next == %.DEAD) free (sockpath); rmdir_tmpdir: if (next == %.DEAD) rmdir (tmpdir); free_tmpdir:
On Wed, Feb 15, 2023 at 03:11:45PM +0100, Laszlo Ersek wrote:
A prior patch highlighted the following leak: whenever any construction step after bind() fails, the UNIX domain socket address (i.e., the pathname in the filesystem) is leaked. Plug the leak by inserting a new error handling section that unlinks the pathname.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U12
generator/states-connect-socket-activation.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 164b8e6828f2..81bb850c7d8c 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -136,30 +136,30 @@ CONNECT_SA.START: goto free_sockpath; }
addr.sun_family = AF_UNIX; memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { set_error (errno, "bind: %s", sockpath); goto close_socket; }
if (listen (s, SOMAXCONN) == -1) { set_error (errno, "listen"); - goto close_socket; + goto unlink_sockpath; }
if (prepare_socket_activation_environment (&env) == -1) { set_error (errno, "prepare_socket_activation_environment"); - goto close_socket; + goto unlink_sockpath; }
pid = fork (); if (pid == -1) { set_error (errno, "fork"); goto empty_env; }
if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { nbd_internal_fork_safe_perror ("dup2"); @@ -211,24 +211,28 @@ CONNECT_SA.START: h->sact_sockpath = sockpath; h->pid = pid;
h->connaddrlen = sizeof addr; memcpy (&h->connaddr, &addr, h->connaddrlen); next = %^CONNECT.START;
/* fall through, for releasing the temporaries */
empty_env: string_vector_empty (&env);
+unlink_sockpath: + if (next == %.DEAD) + unlink (sockpath); + close_socket: close (s);
free_sockpath: if (next == %.DEAD) free (sockpath);
rmdir_tmpdir: if (next == %.DEAD) rmdir (tmpdir);
free_tmpdir:
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
Per POSIX, execvp() is not safe to call in a child process forked from a multi-threaded process. We can now replace the execvp() call in the child process with a call to our fork-safe (async-signal-safe) variant. Prepare our internal execvpe context on the parent's construction path, use the context in the child, and release the context in the parent on the way out, regardless of whether the handler as a whole succeeded or not. (The context is only a temporary resource.) Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U11 generator/states-connect-socket-activation.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 81bb850c7d8c..cb3b1c6f4ede 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -93,22 +93,23 @@ prepare_socket_activation_environment (string_vector *env) string_vector_empty (env); return -1; } STATE_MACHINE { CONNECT_SA.START: enum state next; char *tmpdir; char *sockpath; int s; struct sockaddr_un addr; + struct execvpe execvpe_ctx; string_vector env; pid_t pid; assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); next = %.DEAD; /* Use /tmp instead of TMPDIR because we must ensure the path is * short enough to store in the sockaddr_un. On some platforms this @@ -140,25 +141,31 @@ CONNECT_SA.START: memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { set_error (errno, "bind: %s", sockpath); goto close_socket; } if (listen (s, SOMAXCONN) == -1) { set_error (errno, "listen"); goto unlink_sockpath; } + if (nbd_internal_execvpe_init (&execvpe_ctx, h->argv.ptr[0], h->argv.len) == + -1) { + set_error (errno, "nbd_internal_execvpe_init"); + goto unlink_sockpath; + } + if (prepare_socket_activation_environment (&env) == -1) { set_error (errno, "prepare_socket_activation_environment"); - goto unlink_sockpath; + goto uninit_execvpe; } pid = fork (); if (pid == -1) { set_error (errno, "fork"); goto empty_env; } if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { @@ -189,45 +196,47 @@ CONNECT_SA.START: char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); strcpy (&env.ptr[0][PREFIX_LENGTH], v); /* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); } - environ = env.ptr; - execvp (h->argv.ptr[0], h->argv.ptr); + (void)nbd_internal_fork_safe_execvpe (&execvpe_ctx, &h->argv, env.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } /* Parent -- we're done; commit. */ h->sact_tmpdir = tmpdir; h->sact_sockpath = sockpath; h->pid = pid; h->connaddrlen = sizeof addr; memcpy (&h->connaddr, &addr, h->connaddrlen); next = %^CONNECT.START; /* fall through, for releasing the temporaries */ empty_env: string_vector_empty (&env); +uninit_execvpe: + nbd_internal_execvpe_uninit (&execvpe_ctx); + unlink_sockpath: if (next == %.DEAD) unlink (sockpath); close_socket: close (s); free_sockpath: if (next == %.DEAD) free (sockpath);
On Wed, Feb 15, 2023 at 03:11:46PM +0100, Laszlo Ersek wrote:
Per POSIX, execvp() is not safe to call in a child process forked from a multi-threaded process. We can now replace the execvp() call in the child process with a call to our fork-safe (async-signal-safe) variant.
Prepare our internal execvpe context on the parent's construction path, use the context in the child, and release the context in the parent on the way out, regardless of whether the handler as a whole succeeded or not. (The context is only a temporary resource.)
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U11
generator/states-connect-socket-activation.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 81bb850c7d8c..cb3b1c6f4ede 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -93,22 +93,23 @@ prepare_socket_activation_environment (string_vector *env) string_vector_empty (env); return -1; }
STATE_MACHINE { CONNECT_SA.START: enum state next; char *tmpdir; char *sockpath; int s; struct sockaddr_un addr; + struct execvpe execvpe_ctx; string_vector env; pid_t pid;
assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]);
next = %.DEAD;
/* Use /tmp instead of TMPDIR because we must ensure the path is * short enough to store in the sockaddr_un. On some platforms this @@ -140,25 +141,31 @@ CONNECT_SA.START: memcpy (addr.sun_path, sockpath, strlen (sockpath) + 1); if (bind (s, (struct sockaddr *) &addr, sizeof addr) == -1) { set_error (errno, "bind: %s", sockpath); goto close_socket; }
if (listen (s, SOMAXCONN) == -1) { set_error (errno, "listen"); goto unlink_sockpath; }
+ if (nbd_internal_execvpe_init (&execvpe_ctx, h->argv.ptr[0], h->argv.len) == + -1) { + set_error (errno, "nbd_internal_execvpe_init"); + goto unlink_sockpath; + } + if (prepare_socket_activation_environment (&env) == -1) { set_error (errno, "prepare_socket_activation_environment"); - goto unlink_sockpath; + goto uninit_execvpe; }
pid = fork (); if (pid == -1) { set_error (errno, "fork"); goto empty_env; }
if (pid == 0) { /* child - run command */ if (s != FIRST_SOCKET_ACTIVATION_FD) { if (dup2 (s, FIRST_SOCKET_ACTIVATION_FD) == -1) { @@ -189,45 +196,47 @@ CONNECT_SA.START: char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); strcpy (&env.ptr[0][PREFIX_LENGTH], v);
/* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); }
- environ = env.ptr; - execvp (h->argv.ptr[0], h->argv.ptr); + (void)nbd_internal_fork_safe_execvpe (&execvpe_ctx, &h->argv, env.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); }
/* Parent -- we're done; commit. */ h->sact_tmpdir = tmpdir; h->sact_sockpath = sockpath; h->pid = pid;
h->connaddrlen = sizeof addr; memcpy (&h->connaddr, &addr, h->connaddrlen); next = %^CONNECT.START;
/* fall through, for releasing the temporaries */
empty_env: string_vector_empty (&env);
+uninit_execvpe: + nbd_internal_execvpe_uninit (&execvpe_ctx); + unlink_sockpath: if (next == %.DEAD) unlink (sockpath);
close_socket: close (s);
free_sockpath: if (next == %.DEAD) free (sockpath);
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
A comment currently states that "*the* socket must be set to non-blocking only in the parent" (emphasis mine). This is a thinko: it implies that we have just one file descriptor, and that the underlying file description (which is manipulated by fcntl()'s F_GETFL and F_SETFL) is shared by the parent and the child processes. That's not the case: we have *two* sockets here (a socket pair), one of which is used by the parent exclusively, and the other is used by the child exclusively. Clarify the comment: say that we set the parent-side end of the socket pair to non-blocking. This clarification will play a role in a subsequent patch. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U5 generator/states-connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/states-connect.c b/generator/states-connect.c index 8a286e6630ff..95a96b9114c6 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -281,11 +281,11 @@ CONNECT_COMMAND.START: /* Parent. */ close (sv[1]); h->pid = pid; - /* The socket must be set to non-blocking only in the parent, + /* Only the parent-side end of the socket pair must be set to non-blocking, * because the child may not be expecting a non-blocking socket. */ flags = fcntl (sv[0], F_GETFL, 0); if (flags == -1 || fcntl (sv[0], F_SETFL, flags|O_NONBLOCK) == -1) {
On Wed, Feb 15, 2023 at 03:11:47PM +0100, Laszlo Ersek wrote:
A comment currently states that "*the* socket must be set to non-blocking only in the parent" (emphasis mine). This is a thinko: it implies that we have just one file descriptor, and that the underlying file description (which is manipulated by fcntl()'s F_GETFL and F_SETFL) is shared by the parent and the child processes. That's not the case: we have *two* sockets here (a socket pair), one of which is used by the parent exclusively, and the other is used by the child exclusively.
Clarify the comment: say that we set the parent-side end of the socket pair to non-blocking.
This clarification will play a role in a subsequent patch.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U5
generator/states-connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/generator/states-connect.c b/generator/states-connect.c index 8a286e6630ff..95a96b9114c6 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -281,11 +281,11 @@ CONNECT_COMMAND.START: /* Parent. */ close (sv[1]);
h->pid = pid;
- /* The socket must be set to non-blocking only in the parent, + /* Only the parent-side end of the socket pair must be set to non-blocking, * because the child may not be expecting a non-blocking socket. */ flags = fcntl (sv[0], F_GETFL, 0); if (flags == -1 || fcntl (sv[0], F_SETFL, flags|O_NONBLOCK) == -1) {
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
Furthermore, explain in a comment that *not* calling set_error() upon nbd_internal_socket_create() failing is intentional, and not an omission. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-W generator/states-connect.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/generator/states-connect.c b/generator/states-connect.c index 95a96b9114c6..83d7f316828e 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -240,71 +240,73 @@ CONNECT_TCP.NEXT_ADDRESS: CONNECT_COMMAND.START: int sv[2]; pid_t pid; int flags; assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); if (nbd_internal_socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "socketpair"); return 0; } pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (sv[0]); close (sv[1]); return 0; } if (pid == 0) { /* child - run command */ close (0); close (1); close (sv[0]); dup2 (sv[1], 0); dup2 (sv[1], 1); close (sv[1]); /* Restore SIGPIPE back to SIG_DFL. */ signal (SIGPIPE, SIG_DFL); execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } /* Parent. */ close (sv[1]); h->pid = pid; /* Only the parent-side end of the socket pair must be set to non-blocking, * because the child may not be expecting a non-blocking socket. */ flags = fcntl (sv[0], F_GETFL, 0); if (flags == -1 || fcntl (sv[0], F_SETFL, flags|O_NONBLOCK) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "fcntl"); close (sv[0]); return 0; } h->sock = nbd_internal_socket_create (sv[0]); if (!h->sock) { SET_NEXT_STATE (%.DEAD); + /* nbd_internal_socket_create() calls set_error() internally */ close (sv[0]); return 0; } /* The sockets are connected already, we can jump directly to * receiving the server magic. */ SET_NEXT_STATE (%^MAGIC.START); return 0; } /* END STATE MACHINE */
On Wed, Feb 15, 2023 at 03:11:48PM +0100, Laszlo Ersek wrote:
Furthermore, explain in a comment that *not* calling set_error() upon nbd_internal_socket_create() failing is intentional, and not an omission.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-W
generator/states-connect.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/generator/states-connect.c b/generator/states-connect.c index 95a96b9114c6..83d7f316828e 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -240,71 +240,73 @@ CONNECT_TCP.NEXT_ADDRESS: CONNECT_COMMAND.START: int sv[2]; pid_t pid; int flags;
assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); if (nbd_internal_socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "socketpair"); return 0; }
pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (sv[0]); close (sv[1]); return 0; } if (pid == 0) { /* child - run command */ close (0); close (1); close (sv[0]); dup2 (sv[1], 0); dup2 (sv[1], 1); close (sv[1]);
/* Restore SIGPIPE back to SIG_DFL. */ signal (SIGPIPE, SIG_DFL);
execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); }
/* Parent. */ close (sv[1]);
h->pid = pid;
/* Only the parent-side end of the socket pair must be set to non-blocking, * because the child may not be expecting a non-blocking socket. */ flags = fcntl (sv[0], F_GETFL, 0); if (flags == -1 || fcntl (sv[0], F_SETFL, flags|O_NONBLOCK) == -1) { SET_NEXT_STATE (%.DEAD); + set_error (errno, "fcntl"); close (sv[0]); return 0; }
h->sock = nbd_internal_socket_create (sv[0]); if (!h->sock) { SET_NEXT_STATE (%.DEAD); + /* nbd_internal_socket_create() calls set_error() internally */ close (sv[0]); return 0; }
/* The sockets are connected already, we can jump directly to * receiving the server magic. */ SET_NEXT_STATE (%^MAGIC.START); return 0;
} /* END STATE MACHINE */
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
Refer to fd#0 and fd#1 with the symbolic constants STDIN_FILENO and STDOUT_FILENO from <unistd.h>, for better readability. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/generator/states-connect.c b/generator/states-connect.c index 83d7f316828e..f0020d6f3b26 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -260,11 +260,11 @@ CONNECT_COMMAND.START: return 0; } if (pid == 0) { /* child - run command */ - close (0); - close (1); + close (STDIN_FILENO); + close (STDOUT_FILENO); close (sv[0]); - dup2 (sv[1], 0); - dup2 (sv[1], 1); + dup2 (sv[1], STDIN_FILENO); + dup2 (sv[1], STDOUT_FILENO); close (sv[1]); /* Restore SIGPIPE back to SIG_DFL. */
On Wed, Feb 15, 2023 at 03:11:49PM +0100, Laszlo Ersek wrote:
Refer to fd#0 and fd#1 with the symbolic constants STDIN_FILENO and STDOUT_FILENO from <unistd.h>, for better readability.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/generator/states-connect.c b/generator/states-connect.c index 83d7f316828e..f0020d6f3b26 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -260,11 +260,11 @@ CONNECT_COMMAND.START: return 0; } if (pid == 0) { /* child - run command */ - close (0); - close (1); + close (STDIN_FILENO); + close (STDOUT_FILENO); close (sv[0]); - dup2 (sv[1], 0); - dup2 (sv[1], 1); + dup2 (sv[1], STDIN_FILENO); + dup2 (sv[1], STDOUT_FILENO); close (sv[1]);
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
In the child process: - we have no need for the parent-side end of the socket pair (sv[0]), - we duplicate the child-side end of the socket pair (sv[1]) to both of the child's fd#0 and fd#1, and then close sv[1]. close (STDIN_FILENO); close (STDOUT_FILENO); close (sv[0]); dup2 (sv[1], STDIN_FILENO); dup2 (sv[1], STDOUT_FILENO); close (sv[1]); This code silently assumes that sv[1] falls outside of the the fd set {0,1} -- put differently, the code assumes that each dup2() call will duplicate sv[1] to a file descriptor that is *different* from sv[1]. Let's investigate (a) if the logic is correct under said assumption, and (b) what happened if the assumption were wrong, (c) whether the assumption is valid to make. (a) Under the assumption, the initial two close() calls turn out to be superfluous. That's because dup2 (oldfd, newfd) closes "newfd" first, if "newfd" is open and *differs* from "oldfd". (b) If the assumption were wrong -- that is, if sv[1] equaled 0 or 1 --, then the logic would misbehave, at several levels. First, one of the initial close() calls would cause sv[1] to be closed, thereby breaking both dup2() calls. Second, even if we removed the first two close() calls, the final close() would still be wrong. In this case, one of the dup2() calls would be a no-op (oldfd == newfd), and the other dup2() would work correctly. However, the final close would retroactively invalidate the one dup2() call that had behaved as a no-op. (c) Now let's see whether we need to fix issue (b); that is, whether the assumption that sv[1] differs from both 0 and 1 is valid to make. This assumption is actually valid. ISO C guarantees that the stdin, stdout and stderr I/O streams are open at program startup, and POSIX translates the same requirement to file descriptors. In particular, the *informative* rationale in POSIX tallies its *normative* requirements as follows: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html#tag... | B.2.5 Standard I/O Streams | | Although the ISO C standard guarantees that, at program start-up, | /stdin/ is open for reading and /stdout/ and /stderr/ are open for | writing, this guarantee is contingent (as are all guarantees made by | the ISO C and POSIX standards) on the program being executed in a | conforming environment. Programs executed with file descriptor 0 not | open for reading or with file descriptor 1 or 2 not open for writing | are executed in a non-conforming environment. Application writers | are warned (in [exec], [posix_spawn], and [Redirection]) not to | execute a standard utility or a conforming application with file | descriptor 0 not open for reading or with file descriptor 1 or 2 not | open for writing. The proper way for "disabling" these file descriptors (as described by XRAT as well) is to redirect them from/to "/dev/null". Now, if the libnbd client application closed file descriptors 0 and/or 1 after its startup, it would effectively invalidate itself. Consider the (informative) APPLICATION USAGE section of the fclose() spec: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html#tag_1... | Since after the call to /fclose()/ any use of stream results in | undefined behavior, /fclose()/ should not be used on /stdin/, | /stdout/, or /stderr/ except immediately before process termination | (see XBD [Process Termination]), so as to avoid triggering undefined | behavior in other standard interfaces that rely on these streams. If | there are any [atexit()] handlers registered by the application, | such a call to /fclose()/ should not occur until the last handler is | finishing. Once /fclose()/ has been used to close /stdin/, /stdout/, | or /stderr/, there is no standard way to reopen any of these | streams. | | Use of [freopen()] to change /stdin/, /stdout/, or /stderr/ instead | of closing them avoids the danger of a file unexpectedly being | opened as one of the special file descriptors STDIN_FILENO, | STDOUT_FILENO, or STDERR_FILENO at a later time in the application. Thus, the assumption is deemed valid. Therefore: - While valid, the assumption is not trivial. So, assert it in the child process. Furthermore, because regular assert()'s in the parent process may be easier to read for the user, assert a slightly more comprehensive predicate about socketpair()'s output there, too. - Remove the first two close() calls, which are superfluous. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U6 generator/states-connect.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/generator/states-connect.c b/generator/states-connect.c index f0020d6f3b26..e08608336fd6 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -248,26 +248,35 @@ CONNECT_COMMAND.START: if (nbd_internal_socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "socketpair"); return 0; } + /* A process is effectively in an unusable state if any of STDIN_FILENO + * (fd#0), STDOUT_FILENO (fd#1) and STDERR_FILENO (fd#2) don't exist. If they + * exist however, then the socket pair created above will not intersect with + * the fd set { 0, 1, 2 }. This is relevant for the child-side dup2() logic + * below. + */ + assert (sv[0] > STDERR_FILENO); + assert (sv[1] > STDERR_FILENO); + pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (sv[0]); close (sv[1]); return 0; } if (pid == 0) { /* child - run command */ - close (STDIN_FILENO); - close (STDOUT_FILENO); close (sv[0]); dup2 (sv[1], STDIN_FILENO); dup2 (sv[1], STDOUT_FILENO); + NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDIN_FILENO); + NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDOUT_FILENO); close (sv[1]); /* Restore SIGPIPE back to SIG_DFL. */ signal (SIGPIPE, SIG_DFL); execvp (h->argv.ptr[0], h->argv.ptr);
On Wed, Feb 15, 2023 at 03:11:50PM +0100, Laszlo Ersek wrote:
In the child process:
- we have no need for the parent-side end of the socket pair (sv[0]),
- we duplicate the child-side end of the socket pair (sv[1]) to both of the child's fd#0 and fd#1, and then close sv[1].
close (STDIN_FILENO); close (STDOUT_FILENO); close (sv[0]); dup2 (sv[1], STDIN_FILENO); dup2 (sv[1], STDOUT_FILENO); close (sv[1]);
This code silently assumes that sv[1] falls outside of the the fd set {0,1} -- put differently, the code assumes that each dup2() call will duplicate sv[1] to a file descriptor that is *different* from sv[1].
Let's investigate (a) if the logic is correct under said assumption, and (b) what happened if the assumption were wrong, (c) whether the assumption is valid to make.
(a) Under the assumption, the initial two close() calls turn out to be superfluous. That's because dup2 (oldfd, newfd) closes "newfd" first, if "newfd" is open and *differs* from "oldfd".
(b) If the assumption were wrong -- that is, if sv[1] equaled 0 or 1 --, then the logic would misbehave, at several levels.
First, one of the initial close() calls would cause sv[1] to be closed, thereby breaking both dup2() calls.
Second, even if we removed the first two close() calls, the final close() would still be wrong. In this case, one of the dup2() calls would be a no-op (oldfd == newfd), and the other dup2() would work correctly. However, the final close would retroactively invalidate the one dup2() call that had behaved as a no-op.
(c) Now let's see whether we need to fix issue (b); that is, whether the assumption that sv[1] differs from both 0 and 1 is valid to make.
This assumption is actually valid. ISO C guarantees that the stdin, stdout and stderr I/O streams are open at program startup, and POSIX translates the same requirement to file descriptors. In particular, the *informative* rationale in POSIX tallies its *normative* requirements as follows:
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html#tag...
| B.2.5 Standard I/O Streams | | Although the ISO C standard guarantees that, at program start-up, | /stdin/ is open for reading and /stdout/ and /stderr/ are open for | writing, this guarantee is contingent (as are all guarantees made by | the ISO C and POSIX standards) on the program being executed in a | conforming environment. Programs executed with file descriptor 0 not | open for reading or with file descriptor 1 or 2 not open for writing | are executed in a non-conforming environment. Application writers | are warned (in [exec], [posix_spawn], and [Redirection]) not to | execute a standard utility or a conforming application with file | descriptor 0 not open for reading or with file descriptor 1 or 2 not | open for writing.
The proper way for "disabling" these file descriptors (as described by XRAT as well) is to redirect them from/to "/dev/null".
Now, if the libnbd client application closed file descriptors 0 and/or 1 after its startup, it would effectively invalidate itself. Consider the (informative) APPLICATION USAGE section of the fclose() spec:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html#tag_1...
| Since after the call to /fclose()/ any use of stream results in | undefined behavior, /fclose()/ should not be used on /stdin/, | /stdout/, or /stderr/ except immediately before process termination | (see XBD [Process Termination]), so as to avoid triggering undefined | behavior in other standard interfaces that rely on these streams. If | there are any [atexit()] handlers registered by the application, | such a call to /fclose()/ should not occur until the last handler is | finishing. Once /fclose()/ has been used to close /stdin/, /stdout/, | or /stderr/, there is no standard way to reopen any of these | streams. | | Use of [freopen()] to change /stdin/, /stdout/, or /stderr/ instead | of closing them avoids the danger of a file unexpectedly being | opened as one of the special file descriptors STDIN_FILENO, | STDOUT_FILENO, or STDERR_FILENO at a later time in the application.
Thus, the assumption is deemed valid.
Therefore:
- While valid, the assumption is not trivial. So, assert it in the child process. Furthermore, because regular assert()'s in the parent process may be easier to read for the user, assert a slightly more comprehensive predicate about socketpair()'s output there, too.
- Remove the first two close() calls, which are superfluous.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U6
generator/states-connect.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/generator/states-connect.c b/generator/states-connect.c index f0020d6f3b26..e08608336fd6 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -248,26 +248,35 @@ CONNECT_COMMAND.START: if (nbd_internal_socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "socketpair"); return 0; }
+ /* A process is effectively in an unusable state if any of STDIN_FILENO + * (fd#0), STDOUT_FILENO (fd#1) and STDERR_FILENO (fd#2) don't exist. If they + * exist however, then the socket pair created above will not intersect with + * the fd set { 0, 1, 2 }. This is relevant for the child-side dup2() logic + * below. + */ + assert (sv[0] > STDERR_FILENO); + assert (sv[1] > STDERR_FILENO); + pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (sv[0]); close (sv[1]); return 0; } if (pid == 0) { /* child - run command */ - close (STDIN_FILENO); - close (STDOUT_FILENO); close (sv[0]); dup2 (sv[1], STDIN_FILENO); dup2 (sv[1], STDOUT_FILENO); + NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDIN_FILENO); + NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDOUT_FILENO); close (sv[1]);
/* Restore SIGPIPE back to SIG_DFL. */ signal (SIGPIPE, SIG_DFL);
execvp (h->argv.ptr[0], h->argv.ptr);
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
It's bad hygiene to just assume dup2(), close() and signal() will succeed. Check all return values. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U10 generator/states-connect.c | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/generator/states-connect.c b/generator/states-connect.c index e08608336fd6..d2476dc11c60 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -262,29 +262,41 @@ CONNECT_COMMAND.START: pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (sv[0]); close (sv[1]); return 0; } if (pid == 0) { /* child - run command */ - close (sv[0]); - dup2 (sv[1], STDIN_FILENO); - dup2 (sv[1], STDOUT_FILENO); + if (close (sv[0]) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + } + if (dup2 (sv[1], STDIN_FILENO) == -1 || + dup2 (sv[1], STDOUT_FILENO) == -1) { + nbd_internal_fork_safe_perror ("dup2"); + _exit (126); + } NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDIN_FILENO); NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDOUT_FILENO); - close (sv[1]); + if (close (sv[1]) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + } /* Restore SIGPIPE back to SIG_DFL. */ - signal (SIGPIPE, SIG_DFL); + if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { + nbd_internal_fork_safe_perror ("signal"); + _exit (126); + } execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } /* Parent. */
On Wed, Feb 15, 2023 at 03:11:51PM +0100, Laszlo Ersek wrote:
It's bad hygiene to just assume dup2(), close() and signal() will succeed. Check all return values.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U10
generator/states-connect.c | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/generator/states-connect.c b/generator/states-connect.c index e08608336fd6..d2476dc11c60 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -262,29 +262,41 @@ CONNECT_COMMAND.START:
pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (sv[0]); close (sv[1]); return 0; } if (pid == 0) { /* child - run command */ - close (sv[0]); - dup2 (sv[1], STDIN_FILENO); - dup2 (sv[1], STDOUT_FILENO); + if (close (sv[0]) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + } + if (dup2 (sv[1], STDIN_FILENO) == -1 || + dup2 (sv[1], STDOUT_FILENO) == -1) { + nbd_internal_fork_safe_perror ("dup2"); + _exit (126); + } NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDIN_FILENO); NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDOUT_FILENO); - close (sv[1]); + if (close (sv[1]) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + }
/* Restore SIGPIPE back to SIG_DFL. */ - signal (SIGPIPE, SIG_DFL); + if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { + nbd_internal_fork_safe_perror ("signal"); + _exit (126); + }
execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); }
/* Parent. */
For use of exit code 126, see: https://listman.redhat.com/archives/libguestfs/2019-September/022737.html https://listman.redhat.com/archives/libguestfs/2019-September/022739.html https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html Reviewed-by: Richard W.M. Jones <rjones@redhat.com> Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
On 2/15/23 18:04, Richard W.M. Jones wrote:
On Wed, Feb 15, 2023 at 03:11:51PM +0100, Laszlo Ersek wrote:
It's bad hygiene to just assume dup2(), close() and signal() will succeed. Check all return values.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> ---
Notes: context:-U10
generator/states-connect.c | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/generator/states-connect.c b/generator/states-connect.c index e08608336fd6..d2476dc11c60 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -262,29 +262,41 @@ CONNECT_COMMAND.START:
pid = fork (); if (pid == -1) { SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); close (sv[0]); close (sv[1]); return 0; } if (pid == 0) { /* child - run command */ - close (sv[0]); - dup2 (sv[1], STDIN_FILENO); - dup2 (sv[1], STDOUT_FILENO); + if (close (sv[0]) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + } + if (dup2 (sv[1], STDIN_FILENO) == -1 || + dup2 (sv[1], STDOUT_FILENO) == -1) { + nbd_internal_fork_safe_perror ("dup2"); + _exit (126); + } NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDIN_FILENO); NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDOUT_FILENO); - close (sv[1]); + if (close (sv[1]) == -1) { + nbd_internal_fork_safe_perror ("close"); + _exit (126); + }
/* Restore SIGPIPE back to SIG_DFL. */ - signal (SIGPIPE, SIG_DFL); + if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { + nbd_internal_fork_safe_perror ("signal"); + _exit (126); + }
execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); }
/* Parent. */
For use of exit code 126, see:
https://listman.redhat.com/archives/libguestfs/2019-September/022737.html https://listman.redhat.com/archives/libguestfs/2019-September/022739.html https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
Yes, this is also from POSIX. In fact, I sought to design the execvpe() replacement -- the _init API and the actual execution API -- such that ENOENT here would continue to stand for what it used to stand for before. But, for this particular patch, the following argument is more important than the above one: I added _exit (126) on all these new error branches for *consistency* with existent code. Namely, in the socket activation counterpart patch (#14), the *pre-patch* state is that the child does nicely catch the fcntl() failures -- and exits with status 126. So that's the primary reason why I used _exit (126) on the new error branches in both patch #14 and here (#22).
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Thanks! Laszlo
This patch implements the same ideas as those presented in the prior patch socket activation: centralize resource release of this same series, only for CONNECT_COMMAND.START. The one temporary resource we have here is sv[1]. Similarly to the above-cited patch, the present patch also exposes a resource leak on the error path. Namely, when any construction step fails in the parent after fork(), the child process is leaked. (The child process may well be killed and reaped once the application decides to call nbd_close(), but until then, after the CONNECT_COMMAND.START handler returns with failure, unusable (half-constructed) resources linger around indefinitely. A library API should either fully succeed or fully fail.) We are going to plug this leak in a subsequent patch. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-W generator/states-connect.c | 52 +++++++++++--------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/generator/states-connect.c b/generator/states-connect.c index d2476dc11c60..bd78f890a2c5 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -238,96 +238,104 @@ CONNECT_TCP.NEXT_ADDRESS: return 0; CONNECT_COMMAND.START: + enum state next; int sv[2]; pid_t pid; int flags; + struct socket *sock; assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); + + next = %.DEAD; + if (nbd_internal_socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "socketpair"); - return 0; + goto done; } /* A process is effectively in an unusable state if any of STDIN_FILENO * (fd#0), STDOUT_FILENO (fd#1) and STDERR_FILENO (fd#2) don't exist. If they * exist however, then the socket pair created above will not intersect with * the fd set { 0, 1, 2 }. This is relevant for the child-side dup2() logic * below. */ assert (sv[0] > STDERR_FILENO); assert (sv[1] > STDERR_FILENO); pid = fork (); if (pid == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "fork"); - close (sv[0]); - close (sv[1]); - return 0; + goto close_socket_pair; } + if (pid == 0) { /* child - run command */ if (close (sv[0]) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } if (dup2 (sv[1], STDIN_FILENO) == -1 || dup2 (sv[1], STDOUT_FILENO) == -1) { nbd_internal_fork_safe_perror ("dup2"); _exit (126); } NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDIN_FILENO); NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDOUT_FILENO); if (close (sv[1]) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } /* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); } execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } - /* Parent. */ - close (sv[1]); - - h->pid = pid; - - /* Only the parent-side end of the socket pair must be set to non-blocking, + /* Parent. + * + * Only the parent-side end of the socket pair must be set to non-blocking, * because the child may not be expecting a non-blocking socket. */ flags = fcntl (sv[0], F_GETFL, 0); if (flags == -1 || fcntl (sv[0], F_SETFL, flags|O_NONBLOCK) == -1) { - SET_NEXT_STATE (%.DEAD); set_error (errno, "fcntl"); - close (sv[0]); - return 0; + goto close_socket_pair; } - h->sock = nbd_internal_socket_create (sv[0]); - if (!h->sock) { - SET_NEXT_STATE (%.DEAD); + sock = nbd_internal_socket_create (sv[0]); + if (!sock) /* nbd_internal_socket_create() calls set_error() internally */ - close (sv[0]); - return 0; - } + goto close_socket_pair; + + /* Commit. */ + h->pid = pid; + h->sock = sock; /* The sockets are connected already, we can jump directly to * receiving the server magic. */ - SET_NEXT_STATE (%^MAGIC.START); + next = %^MAGIC.START; + + /* fall through, for releasing the temporaries */ + +close_socket_pair: + if (next == %.DEAD) + close (sv[0]); + close (sv[1]); + +done: + SET_NEXT_STATE (next); return 0; } /* END STATE MACHINE */
A prior patch highlighted the following leak: when any construction step fails in the parent after fork(), the child process is leaked. We could plug the leak by inserting a new error handling section for killing the child process and reaping it with waitpid(). However, it would raise some new questions (what signal to send, ensure the child not ignore that signal, hope that whatever process image we execute in the child handle that signal properly, etc). Instead, rearrange the steps in the CONNECT_COMMAND.START handler so that fork() be the last operation in the parent process, on the construction path. If fork() succeeds, let the entire handler succeed. For this, move the fcntl() and nbd_internal_socket_create() calls above fork(). (The hoisting of the fcntl() calls is where we rely on the earlier observation that O_NONBLOCK only applies to the parent-side end of the socket pair, so it is safe to do before forking.) The trouble in turn is that nbd_internal_socket_create() takes ownership of the parent-side end of the socket pair (sv[0]). So once that function returns successfully, we can't close sv[0] even on the error path -- we close the "higher level" socket, and that invalidates sv[0] at once. Track this ownership transfer with a new boolean flag therefore. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-W generator/states-connect.c | 48 +++++++++++--------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/generator/states-connect.c b/generator/states-connect.c index bd78f890a2c5..283481bb7f91 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -239,103 +239,109 @@ CONNECT_TCP.NEXT_ADDRESS: CONNECT_COMMAND.START: enum state next; + bool parentfd_transferred; int sv[2]; - pid_t pid; int flags; struct socket *sock; + pid_t pid; assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); next = %.DEAD; + parentfd_transferred = false; if (nbd_internal_socketpair (AF_UNIX, SOCK_STREAM, 0, sv) == -1) { set_error (errno, "socketpair"); goto done; } /* A process is effectively in an unusable state if any of STDIN_FILENO * (fd#0), STDOUT_FILENO (fd#1) and STDERR_FILENO (fd#2) don't exist. If they * exist however, then the socket pair created above will not intersect with * the fd set { 0, 1, 2 }. This is relevant for the child-side dup2() logic * below. */ assert (sv[0] > STDERR_FILENO); assert (sv[1] > STDERR_FILENO); + /* Only the parent-side end of the socket pair must be set to non-blocking, + * because the child may not be expecting a non-blocking socket. + */ + flags = fcntl (sv[0], F_GETFL, 0); + if (flags == -1 || + fcntl (sv[0], F_SETFL, flags|O_NONBLOCK) == -1) { + set_error (errno, "fcntl"); + goto close_socket_pair; + } + + sock = nbd_internal_socket_create (sv[0]); + if (!sock) + /* nbd_internal_socket_create() calls set_error() internally */ + goto close_socket_pair; + parentfd_transferred = true; + pid = fork (); if (pid == -1) { set_error (errno, "fork"); - goto close_socket_pair; + goto close_high_level_socket; } if (pid == 0) { /* child - run command */ if (close (sv[0]) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } if (dup2 (sv[1], STDIN_FILENO) == -1 || dup2 (sv[1], STDOUT_FILENO) == -1) { nbd_internal_fork_safe_perror ("dup2"); _exit (126); } NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDIN_FILENO); NBD_INTERNAL_FORK_SAFE_ASSERT (sv[1] != STDOUT_FILENO); if (close (sv[1]) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } /* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); } execvp (h->argv.ptr[0], h->argv.ptr); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } - /* Parent. - * - * Only the parent-side end of the socket pair must be set to non-blocking, - * because the child may not be expecting a non-blocking socket. - */ - flags = fcntl (sv[0], F_GETFL, 0); - if (flags == -1 || - fcntl (sv[0], F_SETFL, flags|O_NONBLOCK) == -1) { - set_error (errno, "fcntl"); - goto close_socket_pair; - } - - sock = nbd_internal_socket_create (sv[0]); - if (!sock) - /* nbd_internal_socket_create() calls set_error() internally */ - goto close_socket_pair; - - /* Commit. */ + /* Parent -- we're done; commit. */ h->pid = pid; h->sock = sock; /* The sockets are connected already, we can jump directly to * receiving the server magic. */ next = %^MAGIC.START; /* fall through, for releasing the temporaries */ -close_socket_pair: +close_high_level_socket: if (next == %.DEAD) + sock->ops->close (sock); + +close_socket_pair: + assert (next == %.DEAD || parentfd_transferred); + if (!parentfd_transferred) close (sv[0]); close (sv[1]); done: SET_NEXT_STATE (next); return 0; } /* END STATE MACHINE */
Per POSIX, execvp() is not safe to call in a child process forked from a multi-threaded process. We can now replace the execvp() call in the child process with a call to our fork-safe (async-signal-safe) variant. Prepare our internal execvpe context on the parent's construction path, use the context in the child, and release the context in the parent on the way out, regardless of whether the handler as a whole succeeded or not. (The context is only a temporary resource.) Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U7 generator/states-connect.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/generator/states-connect.c b/generator/states-connect.c index 283481bb7f91..4fc01f3b46cb 100644 --- a/generator/states-connect.c +++ b/generator/states-connect.c @@ -32,14 +32,16 @@ #include <signal.h> #include <netdb.h> #include <netinet/in.h> #include <netinet/tcp.h> #include <sys/types.h> #include <sys/socket.h> +extern char **environ; + /* Disable Nagle's algorithm on the socket, but don't fail. */ static void disable_nagle (int sock) { const int flag = 1; setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof flag); @@ -239,14 +241,15 @@ CONNECT_TCP.NEXT_ADDRESS: CONNECT_COMMAND.START: enum state next; bool parentfd_transferred; int sv[2]; int flags; struct socket *sock; + struct execvpe execvpe_ctx; pid_t pid; assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); next = %.DEAD; @@ -278,18 +281,24 @@ CONNECT_COMMAND.START: sock = nbd_internal_socket_create (sv[0]); if (!sock) /* nbd_internal_socket_create() calls set_error() internally */ goto close_socket_pair; parentfd_transferred = true; + if (nbd_internal_execvpe_init (&execvpe_ctx, h->argv.ptr[0], h->argv.len) == + -1) { + set_error (errno, "nbd_internal_execvpe_init"); + goto close_high_level_socket; + } + pid = fork (); if (pid == -1) { set_error (errno, "fork"); - goto close_high_level_socket; + goto uninit_execvpe; } if (pid == 0) { /* child - run command */ if (close (sv[0]) == -1) { nbd_internal_fork_safe_perror ("close"); _exit (126); } @@ -307,15 +316,15 @@ CONNECT_COMMAND.START: /* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); } - execvp (h->argv.ptr[0], h->argv.ptr); + (void)nbd_internal_fork_safe_execvpe (&execvpe_ctx, &h->argv, environ); nbd_internal_fork_safe_perror (h->argv.ptr[0]); if (errno == ENOENT) _exit (127); else _exit (126); } @@ -326,14 +335,17 @@ CONNECT_COMMAND.START: /* The sockets are connected already, we can jump directly to * receiving the server magic. */ next = %^MAGIC.START; /* fall through, for releasing the temporaries */ +uninit_execvpe: + nbd_internal_execvpe_uninit (&execvpe_ctx); + close_high_level_socket: if (next == %.DEAD) sock->ops->close (sock); close_socket_pair: assert (next == %.DEAD || parentfd_transferred); if (!parentfd_transferred)
For #23, #24, #25: Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
In the future, we'd like to add systemd socket activation environment variables such that: - their values may not be constants (not even for pre-allocating space), - they may be optional, - regardless of some optional variables being added or not, the positions of those that we do add can be captured, for later reference. Generalize prepare_socket_activation_environment() accordingly. Write the child PID to "LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" with the new "capturing" interface. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: context:-U6 generator/states-connect-socket-activation.c | 160 +++++++++++++++----- 1 file changed, 125 insertions(+), 35 deletions(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index cb3b1c6f4ede..94d641cde6d9 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -27,84 +27,168 @@ #include <errno.h> #include <assert.h> #include <sys/socket.h> #include <sys/un.h> #include "internal.h" +#include "compiler-macros.h" +#include "unique-name.h" +#include "array-size.h" +#include "checked-overflow.h" /* This is baked into the systemd socket activation API. */ #define FIRST_SOCKET_ACTIVATION_FD 3 -/* == strlen ("LISTEN_PID=") | strlen ("LISTEN_FDS=") */ -#define PREFIX_LENGTH 11 +/* Describes a systemd socket activation environment variable. */ +struct sact_var { + const char *prefix; /* variable name and equal sign */ + size_t prefix_len; + const char *value; + size_t value_len; +}; + +/* Determine the length of a string, using "sizeof" whenever possible. + * + * Do not use this macro on an argument that has side effects, as no guarantees + * are given regarding the number of times the argument may be evaluated. + * TYPE_IS_ARRAY(s) itself may contribute a different number of evaluations + * dependent on whether "s" has variably modified type, and then the conditional + * operator either evaluates "sizeof s" (which contributes 0 or 1 evaluations, + * dependent on whether "s" has variably modified type) or strlen(s) (which + * contributes 1 evaluation). Also note that the argument of the "sizeof" + * operator is *only* parenthesized because "s" is a macro parameter here. +*/ +#define STRLEN1(s) ((TYPE_IS_ARRAY (s) ? sizeof (s) - 1 : strlen (s))) + +/* Push a new element to an array of "sact_var" structures. + * + * "vars" is the array to extend. "num_vars" (of type (size_t *)) points to the + * number of elements that the array, on input, contains; (*num_vars) is + * increased by one on output. "prefix" and "value" serve as the values for + * setting the fields in the new element. "ofs" (of type (size_t *)) may be + * NULL; if it isn't, then on output, (*ofs) is set to the input value of + * (*num_vars): the offset of the just-pushed element. + * + * Avoid arguments with side-effects here as well. + */ +#define SACT_VAR_PUSH(vars, num_vars, prefix, value, ofs) \ + SACT_VAR_PUSH1 ((vars), (num_vars), (prefix), (value), (ofs), \ + NBDKIT_UNIQUE_NAME (_ofs)) +#define SACT_VAR_PUSH1(vars, num_vars, prefix, value, ofs, ofs1) \ + do { \ + size_t *ofs1; \ + \ + assert (*(num_vars) < ARRAY_SIZE (vars)); \ + ofs1 = (ofs); \ + if (ofs1 != NULL) \ + *ofs1 = *(num_vars); \ + (vars)[(*(num_vars))++] = (struct sact_var){ (prefix), STRLEN1 (prefix), \ + (value), STRLEN1 (value) }; \ + } while (0) extern char **environ; -/* Prepare environment for calling execvp when doing systemd socket - * activation. Takes the current environment and copies it. Removes - * any existing LISTEN_PID or LISTEN_FDS and replaces them with new - * variables. env[0] is "LISTEN_PID=..." which is filled in by - * CONNECT_SA.START, and env[1] is "LISTEN_FDS=1". +/* Prepare environment for calling execvp when doing systemd socket activation. + * Takes the current environment and copies it. Removes any existing socket + * activation variables and replaces them with new ones. Variables in "sact_var" + * will be placed at the front of "env", preserving the order from "sact_var". */ static int -prepare_socket_activation_environment (string_vector *env) +prepare_socket_activation_environment (string_vector *env, + const struct sact_var *sact_var, + size_t num_vars) { - char *p; + const struct sact_var *var_end; + char *new_var; + const struct sact_var *var; size_t i; *env = (string_vector)empty_vector; - /* Reserve slots env[0] and env[1]. */ - p = strdup ("LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); - if (p == NULL) - goto err; - if (string_vector_append (env, p) == -1) { - free (p); - goto err; - } - p = strdup ("LISTEN_FDS=1"); - if (p == NULL) - goto err; - if (string_vector_append (env, p) == -1) { - free (p); - goto err; + /* Set the exclusive limit for loops over "sact_var". */ + var_end = sact_var + num_vars; + + /* New environment variable being constructed for "env". */ + new_var = NULL; + + /* Copy "sact_var" to the front of "env". */ + for (var = sact_var; var < var_end; ++var) { + size_t new_var_size; + char *p; + + /* Calculate the size of the "NAME=value" string. */ + if (ADD_OVERFLOW (var->prefix_len, var->value_len, &new_var_size) || + ADD_OVERFLOW (new_var_size, 1u, &new_var_size)) { + errno = EOVERFLOW; + goto err; + } + + /* Allocate and format "NAME=value". */ + new_var = malloc (new_var_size); + if (new_var == NULL) + goto err; + p = new_var; + + memcpy (p, var->prefix, var->prefix_len); + p += var->prefix_len; + + memcpy (p, var->value, var->value_len); + p += var->value_len; + + *p++ = '\0'; + + /* Push "NAME=value" to the vector. */ + if (string_vector_append (env, new_var) == -1) + goto err; + /* Ownership transferred. */ + new_var = NULL; } - /* Append the current environment, but remove LISTEN_PID, LISTEN_FDS. */ + /* Append the current environment to "env", but remove "sact_var". */ for (i = 0; environ[i] != NULL; ++i) { - if (strncmp (environ[i], "LISTEN_PID=", PREFIX_LENGTH) != 0 && - strncmp (environ[i], "LISTEN_FDS=", PREFIX_LENGTH) != 0) { - char *copy = strdup (environ[i]); - if (copy == NULL) - goto err; - if (string_vector_append (env, copy) == -1) { - free (copy); - goto err; - } + for (var = sact_var; var < var_end; ++var) { + if (strncmp (environ[i], var->prefix, var->prefix_len) == 0) + break; } + /* Drop known socket activation variable from the current environment. */ + if (var < var_end) + continue; + + new_var = strdup (environ[i]); + if (new_var == NULL) + goto err; + + if (string_vector_append (env, new_var) == -1) + goto err; + /* Ownership transferred. */ + new_var = NULL; } /* The environ must be NULL-terminated. */ if (string_vector_append (env, NULL) == -1) goto err; return 0; err: + free (new_var); string_vector_empty (env); return -1; } STATE_MACHINE { CONNECT_SA.START: enum state next; char *tmpdir; char *sockpath; int s; struct sockaddr_un addr; struct execvpe execvpe_ctx; + size_t num_vars; + struct sact_var sact_var[2]; + size_t pid_ofs; string_vector env; pid_t pid; assert (!h->sock); assert (h->argv.ptr); assert (h->argv.ptr[0]); @@ -152,13 +236,18 @@ CONNECT_SA.START: if (nbd_internal_execvpe_init (&execvpe_ctx, h->argv.ptr[0], h->argv.len) == -1) { set_error (errno, "nbd_internal_execvpe_init"); goto unlink_sockpath; } - if (prepare_socket_activation_environment (&env) == -1) { + num_vars = 0; + SACT_VAR_PUSH (sact_var, &num_vars, + "LISTEN_PID=", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", &pid_ofs); + SACT_VAR_PUSH (sact_var, &num_vars, + "LISTEN_FDS=", "1", NULL); + if (prepare_socket_activation_environment (&env, sact_var, num_vars) == -1) { set_error (errno, "prepare_socket_activation_environment"); goto uninit_execvpe; } pid = fork (); if (pid == -1) { @@ -193,13 +282,14 @@ CONNECT_SA.START: } } char buf[32]; const char *v = nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf); - strcpy (&env.ptr[0][PREFIX_LENGTH], v); + NBD_INTERNAL_FORK_SAFE_ASSERT (strlen (v) <= sact_var[pid_ofs].value_len); + strcpy (env.ptr[pid_ofs] + sact_var[pid_ofs].prefix_len, v); /* Restore SIGPIPE back to SIG_DFL. */ if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) { nbd_internal_fork_safe_perror ("signal"); _exit (126); }
From: "Richard W.M. Jones" <rjones@redhat.com> [Laszlo's note: "ascii-ctype.h" and "test-ascii-ctype.c" match those of nbdkit at commit 5fd4262ef07f. Originally posted by Rich at <https://listman.redhat.com/archives/libguestfs/2023-January/030559.html>.] Message-Id: <20230130225521.1771496-3-rjones@redhat.com> Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- common/include/ascii-ctype.h | 75 +++++++++++++++++ common/include/Makefile.am | 6 ++ common/include/test-ascii-ctype.c | 88 ++++++++++++++++++++ .gitignore | 1 + 4 files changed, 170 insertions(+) diff --git a/common/include/ascii-ctype.h b/common/include/ascii-ctype.h new file mode 100644 index 000000000000..a55dee11b8b1 --- /dev/null +++ b/common/include/ascii-ctype.h @@ -0,0 +1,75 @@ +/* nbdkit + * Copyright (C) 2013-2020 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of Red Hat nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* Normal ctype functions are affected by the current locale. For + * example isupper() might recognize Ä in some but not all locales. + * These functions match only 7 bit ASCII characters. + */ + +#ifndef NBDKIT_ASCII_CTYPE_H +#define NBDKIT_ASCII_CTYPE_H + +#define ascii_isalnum(c) (ascii_isalpha (c) || ascii_isdigit (c)) + +#define ascii_isalpha(c) \ + (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) + +#define ascii_isdigit(c) \ + ((c) >= '0' && (c) <= '9') + +#define ascii_isspace(c) \ + ((c) == '\t' || (c) == '\n' || (c) == '\f' || (c) == '\r' || (c) == ' ') + +#define ascii_isupper(c) \ + ((c) >= 'A' && (c) <= 'Z') + +#define ascii_islower(c) \ + ((c) >= 'a' && (c) <= 'z') + +/* See also hexdigit.h */ +#define ascii_isxdigit(c) \ + ((c) == '0' || (c) == '1' || (c) == '2' || (c) == '3' || (c) == '4' || \ + (c) == '5' || (c) == '6' || (c) == '7' || (c) == '8' || (c) == '9' || \ + (c) == 'a' || (c) == 'b' || (c) == 'c' || \ + (c) == 'd' || (c) == 'e' || (c) == 'f' || \ + (c) == 'A' || (c) == 'B' || (c) == 'C' || \ + (c) == 'D' || (c) == 'E' || (c) == 'F') + +#define ascii_tolower(c) \ + (ascii_isupper ((c)) ? (c) + 32 : (c)) + +#define ascii_toupper(c) \ + (ascii_islower ((c)) ? (c) - 32 : (c)) + +#define ascii_isprint(c) ((c) >= 32 && (c) <= 126) + +#endif /* NBDKIT_ASCII_CTYPE_H */ diff --git a/common/include/Makefile.am b/common/include/Makefile.am index fa2633c25ac3..04553a367d84 100644 --- a/common/include/Makefile.am +++ b/common/include/Makefile.am @@ -20,6 +20,7 @@ include $(top_srcdir)/subdir-rules.mk EXTRA_DIST = \ ansi-colours.h \ array-size.h \ + ascii-ctype.h \ byte-swapping.h \ checked-overflow.h \ compiler-macros.h \ @@ -35,6 +36,7 @@ EXTRA_DIST = \ TESTS = \ test-array-size \ + test-ascii-ctype \ test-checked-overflow \ test-ispowerof2 \ $(NULL) @@ -44,6 +46,10 @@ test_array_size_SOURCES = test-array-size.c array-size.h test_array_size_CPPFLAGS = -I$(srcdir) test_array_size_CFLAGS = $(WARNINGS_CFLAGS) +test_ascii_ctype_SOURCES = test-ascii-ctype.c ascii-ctype.h +test_ascii_ctype_CPPFLAGS = -I$(srcdir) +test_ascii_ctype_CFLAGS = $(WARNINGS_CFLAGS) + test_checked_overflow_SOURCES = test-checked-overflow.c checked-overflow.h test_checked_overflow_CPPFLAGS = -I$(srcdir) test_checked_overflow_CFLAGS = $(WARNINGS_CFLAGS) diff --git a/common/include/test-ascii-ctype.c b/common/include/test-ascii-ctype.c new file mode 100644 index 000000000000..4fbb0259f83a --- /dev/null +++ b/common/include/test-ascii-ctype.c @@ -0,0 +1,88 @@ +/* nbdkit + * Copyright (C) 2020 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of Red Hat nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#undef NDEBUG /* Keep test strong even for nbdkit built without assertions */ +#include <assert.h> + +#include "ascii-ctype.h" + +int +main (void) +{ + assert (ascii_isspace (' ')); + assert (ascii_isspace ('\t')); + assert (ascii_isspace ('\n')); + assert (! ascii_isspace ('a')); + + assert (ascii_isalpha ('a')); + assert (ascii_isalpha ('Z')); + assert (ascii_isalpha ('z')); + assert (! ascii_isalpha (' ')); + assert (! ascii_isalpha ('0')); + { const char *s = "Ä"; assert (! ascii_isalpha (s[0])); } + { const char *s = "®"; assert (! ascii_isalpha (s[0])); } + + assert (ascii_isdigit ('0')); + assert (ascii_isdigit ('9')); + { const char *s = "Ø"; assert (! ascii_isdigit (s[0])); } /* U+00D8 */ + { const char *s = "9"; assert (! ascii_isdigit (s[0])); } /* U+FF19 */ + + assert (ascii_islower ('a')); + assert (ascii_islower ('z')); + assert (! ascii_islower ('Z')); + { const char *s = "Ä"; assert (! ascii_islower (s[0])); } + + assert (ascii_isupper ('A')); + assert (ascii_isupper ('Z')); + assert (! ascii_isupper ('z')); + { const char *s = "Ä"; assert (! ascii_isupper (s[0])); } + + assert (ascii_tolower ('A') == 'a'); + assert (ascii_tolower ('Z') == 'z'); + assert (ascii_tolower ('a') == 'a'); + assert (ascii_tolower ('z') == 'z'); + assert (ascii_tolower ('0') == '0'); + { const char *s = "Ä"; assert (ascii_tolower (s[0]) == s[0]); } + + assert (ascii_toupper ('a') == 'A'); + assert (ascii_toupper ('z') == 'Z'); + assert (ascii_toupper ('A') == 'A'); + assert (ascii_toupper ('Z') == 'Z'); + assert (ascii_toupper ('0') == '0'); + { const char *s = "à"; assert (ascii_toupper (s[0]) == s[0]); } + + exit (EXIT_SUCCESS); +} diff --git a/.gitignore b/.gitignore index 5c42060dc0dd..af7a8285fec7 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ Makefile.in /bash-completion/nbdinfo /bash-completion/nbdublk /common/include/test-array-size +/common/include/test-ascii-ctype /common/include/test-checked-overflow /common/include/test-ispowerof2 /common/utils/test-human-size
From: "Richard W.M. Jones" <rjones@redhat.com> To allow us to name the socket passed down to the NBD server when calling nbd_connect_systemd_socket_activation(3), we need to add the field to the handle and add access functions. [Laszlo's note: originally posted by Rich at <https://listman.redhat.com/archives/libguestfs/2023-January/030557.html>. I've renamed "sa_name" to "sact_name", due to <signal.h> reserving symbols with the "sa_" prefix. This corresponds to earlier patches in this series, such as 'socket activation: rename sa_(tmpdir|sockpath) to sact_(tmpdir|sockpath)' and 'ocaml: rename "sa_u" to "saddr_u"'.] Message-Id: <20230130225521.1771496-4-rjones@redhat.com> Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- lib/internal.h | 1 + generator/API.ml | 49 +++++++++++++++++ lib/handle.c | 56 ++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/lib/internal.h b/lib/internal.h index 8c4e32013e40..a63fbe05641c 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -101,6 +101,7 @@ struct nbd_handle { _Atomic uintptr_t private_data; char *export_name; /* Export name, never NULL. */ + char *sact_name; /* Socket activation name, can be NULL. */ /* TLS settings. */ int tls; /* 0 = disable, 1 = enable, 2 = require */ diff --git a/generator/API.ml b/generator/API.ml index 25a612a2e864..08fc80960b15 100644 --- a/generator/API.ml +++ b/generator/API.ml @@ -2036,15 +2036,62 @@ "connect_systemd_socket_activation", { When the NBD handle is closed the server subprocess is killed. + +=head3 Socket name + +The socket activation protocol lets you optionally give +the socket a name. If used, the name is passed to the +NBD server using the C<LISTEN_FDNAMES> environment +variable. To provide a socket name, call +L<nbd_set_socket_activation_name(3)> before calling +the connect function. " ^ blocking_connect_call_description; see_also = [Link "aio_connect_systemd_socket_activation"; Link "connect_command"; Link "kill_subprocess"; Link "set_opt_mode"; + Link "set_socket_activation_name"; + Link "get_socket_activation_name"; ExternalLink ("qemu-nbd", 1); URLLink "http://0pointer.de/blog/projects/socket-activation.html"]; example = Some "examples/open-qcow2.c"; }; + "set_socket_activation_name", { + default_call with + args = [ String "socket_name" ]; ret = RErr; + shortdesc = "set the socket activation name"; + longdesc = "\ +When running an NBD server using +L<nbd_connect_systemd_socket_activation(3)> you can optionally +name the socket. Call this function before connecting to the +server. + +Some servers such as L<qemu-storage-daemon(1)> +can use this information to associate the socket with a name +used on the command line, but most servers will ignore it. +The name is passed through the C<LISTEN_FDNAMES> environment +variable. + +The parameter C<socket_name> can be a short alphanumeric string. +If it is set to the empty string (also the default when the handle +is created) then no name is passed to the server."; + see_also = [Link "connect_systemd_socket_activation"; + Link "get_socket_activation_name"]; + }; + + "get_socket_activation_name", { + default_call with + args = []; ret = RString; + shortdesc = "get the socket activation name"; + longdesc = "\ +Return the socket name used when you call +L<nbd_connect_systemd_socket_activation(3)> on the same +handle. By default this will return the empty string +meaning that no name is passed to the server."; + see_also = [Link "connect_systemd_socket_activation"; + Link "set_socket_activation_name"]; + }; + "is_read_only", { default_call with args = []; ret = RBool; @@ -3844,6 +3891,8 @@ "get_uri", { "aio_opt_structured_reply", (1, 16); "opt_starttls", (1, 16); "aio_opt_starttls", (1, 16); + "set_socket_activation_name", (1, 16); + "get_socket_activation_name", (1, 16); (* These calls are proposed for a future version of libnbd, but * have not been added to any released version so far. diff --git a/lib/handle.c b/lib/handle.c index fb92f16e6c91..d78372cb0e07 100644 --- a/lib/handle.c +++ b/lib/handle.c @@ -28,6 +28,7 @@ #include <sys/types.h> #include <sys/wait.h> +#include "ascii-ctype.h" #include "internal.h" static void @@ -159,6 +160,7 @@ nbd_close (struct nbd_handle *h) waitpid (h->pid, NULL, 0); free (h->export_name); + free (h->sact_name); free (h->tls_certificates); free (h->tls_username); free (h->tls_psk_file); @@ -197,6 +199,60 @@ nbd_unlocked_get_handle_name (struct nbd_handle *h) return copy; } +int +nbd_unlocked_set_socket_activation_name (struct nbd_handle *h, + const char *name) +{ + size_t i, len; + char *new_name; + + len = strlen (name); + + /* Setting it to empty string stores NULL in the handle. */ + if (len == 0) { + free (h->sact_name); + h->sact_name = NULL; + return 0; + } + + /* Check the proposed name is short and alphanumeric. */ + if (len > 32) { + set_error (ENAMETOOLONG, "socket activation name should be " + "<= 32 characters"); + return -1; + } + for (i = 0; i < len; ++i) { + if (! ascii_isalnum (name[i])) { + set_error (EINVAL, "socket activation name should contain " + "only alphanumeric ASCII characters"); + return -1; + } + } + + new_name = strdup (name); + if (!new_name) { + set_error (errno, "strdup"); + return -1; + } + + free (h->sact_name); + h->sact_name = new_name; + return 0; +} + +char * +nbd_unlocked_get_socket_activation_name (struct nbd_handle *h) +{ + char *copy = strdup (h->sact_name ? h->sact_name : ""); + + if (!copy) { + set_error (errno, "strdup"); + return NULL; + } + + return copy; +} + uintptr_t nbd_unlocked_set_private_data (struct nbd_handle *h, uintptr_t data) {
When the user calls nbd_set_socket_activation_name before calling nbd_connect_system_socket_activation, pass the name down to the server through LISTEN_FDNAMES. This has no effect unless the new API has been called to set the socket name to a non-empty string. https://lists.nongnu.org/archive/html/qemu-devel/2023-01/msg06114.html [Original commit message and upstream discussion reference by Rich Jones; at <https://listman.redhat.com/archives/libguestfs/2023-January/030558.html> / msgid <20230130225521.1771496-5-rjones@redhat.com>.] Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/states-connect-socket-activation.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c index 94d641cde6d9..150b5d6ac005 100644 --- a/generator/states-connect-socket-activation.c +++ b/generator/states-connect-socket-activation.c @@ -184,7 +184,7 @@ CONNECT_SA.START: struct sockaddr_un addr; struct execvpe execvpe_ctx; size_t num_vars; - struct sact_var sact_var[2]; + struct sact_var sact_var[3]; size_t pid_ofs; string_vector env; pid_t pid; @@ -244,6 +244,9 @@ CONNECT_SA.START: "LISTEN_PID=", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", &pid_ofs); SACT_VAR_PUSH (sact_var, &num_vars, "LISTEN_FDS=", "1", NULL); + if (h->sact_name != NULL) + SACT_VAR_PUSH (sact_var, &num_vars, + "LISTEN_FDNAMES=", h->sact_name, NULL); if (prepare_socket_activation_environment (&env, sact_var, num_vars) == -1) { set_error (errno, "prepare_socket_activation_environment"); goto uninit_execvpe;
For #26, #27, #28, #29: Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
participants (4)
-
Daniel P. Berrangé -
Eric Blake -
Laszlo Ersek -
Richard W.M. Jones