From ee3a6cab09c8acaf6706b3710f5652e9be43b57e Mon Sep 17 00:00:00 2001 From: Mariusz Tkaczyk Date: Wed, 25 Sep 2024 13:16:10 +0200 Subject: [PATCH 189/201] mdadm: add xmalloc.h Move memory declaration helpers outside mdadm.h. They seems to be useful so keep them but include separatelly. Rework them to not reffer to Name[] declared internally in mdadm/mdmon. This is first step to start decomplexing mdadm.h. Signed-off-by: Mariusz Tkaczyk --- Assemble.c | 2 ++ Create.c | 4 +++- Detail.c | 2 ++ Examine.c | 8 ++++--- Grow.c | 2 ++ Incremental.c | 2 ++ Manage.c | 2 ++ Monitor.c | 4 +++- bitmap.c | 1 + config.c | 2 ++ lib.c | 2 ++ managemon.c | 2 ++ mapfile.c | 2 ++ mdadm.c | 2 ++ mdadm.h | 5 ---- mdmon.c | 1 + mdopen.c | 2 ++ mdstat.c | 2 ++ msg.c | 1 + platform-intel.c | 2 ++ policy.c | 2 ++ restripe.c | 2 ++ super-ddf.c | 2 ++ super-gpt.c | 1 + super-intel.c | 2 ++ super-mbr.c | 1 + super0.c | 2 ++ super1.c | 2 ++ sysfs.c | 4 +++- udev.c | 2 ++ util.c | 2 ++ xmalloc.c | 59 +++++++++++++++++++++--------------------------- xmalloc.h | 13 +++++++++++ 33 files changed, 100 insertions(+), 44 deletions(-) create mode 100644 xmalloc.h diff --git a/Assemble.c b/Assemble.c index a2bb7b64..d3dbfbc6 100644 --- a/Assemble.c +++ b/Assemble.c @@ -23,6 +23,8 @@ */ #include "mdadm.h" +#include "xmalloc.h" + #include mapping_t assemble_statuses[] = { diff --git a/Create.c b/Create.c index 7fde1c16..1640f21e 100644 --- a/Create.c +++ b/Create.c @@ -23,9 +23,11 @@ */ #include "mdadm.h" -#include "udev.h" #include "md_u.h" #include "md_p.h" +#include "udev.h" +#include "xmalloc.h" + #include #include #include diff --git a/Detail.c b/Detail.c index 331e1da3..5819ced9 100644 --- a/Detail.c +++ b/Detail.c @@ -25,6 +25,8 @@ #include "mdadm.h" #include "md_p.h" #include "md_u.h" +#include "xmalloc.h" + #include #include diff --git a/Examine.c b/Examine.c index fe162167..036b7a56 100644 --- a/Examine.c +++ b/Examine.c @@ -22,14 +22,16 @@ * Email: */ -#include "mdadm.h" #include "dlink.h" +#include "mdadm.h" +#include "md_u.h" +#include "md_p.h" +#include "xmalloc.h" #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN) #error no endian defined #endif -#include "md_u.h" -#include "md_p.h" + int Examine(struct mddev_dev *devlist, struct context *c, struct supertype *forcest) diff --git a/Grow.c b/Grow.c index 60076f56..ef1285ec 100644 --- a/Grow.c +++ b/Grow.c @@ -23,6 +23,8 @@ */ #include "mdadm.h" #include "dlink.h" +#include "xmalloc.h" + #include #include #include diff --git a/Incremental.c b/Incremental.c index e7987d1d..8c915783 100644 --- a/Incremental.c +++ b/Incremental.c @@ -29,6 +29,8 @@ */ #include "mdadm.h" +#include "xmalloc.h" + #include #include #include diff --git a/Manage.c b/Manage.c index 0f232a57..246ef319 100644 --- a/Manage.c +++ b/Manage.c @@ -26,6 +26,8 @@ #include "md_u.h" #include "md_p.h" #include "udev.h" +#include "xmalloc.h" + #include int Manage_ro(char *devname, int fd, int readonly) diff --git a/Monitor.c b/Monitor.c index d260b0f1..d1cfbf94 100644 --- a/Monitor.c +++ b/Monitor.c @@ -23,9 +23,11 @@ */ #include "mdadm.h" -#include "udev.h" #include "md_p.h" #include "md_u.h" +#include "udev.h" +#include "xmalloc.h" + #include #include #include diff --git a/bitmap.c b/bitmap.c index 9a7ffe3b..5110ae2f 100644 --- a/bitmap.c +++ b/bitmap.c @@ -19,6 +19,7 @@ */ #include "mdadm.h" +#include "xmalloc.h" static inline void sb_le_to_cpu(bitmap_super_t *sb) { diff --git a/config.c b/config.c index 5411a480..022fb2a0 100644 --- a/config.c +++ b/config.c @@ -24,6 +24,8 @@ #include "mdadm.h" #include "dlink.h" +#include "xmalloc.h" + #include #include #include diff --git a/lib.c b/lib.c index 13d4e4f1..f36ae03a 100644 --- a/lib.c +++ b/lib.c @@ -24,6 +24,8 @@ #include "mdadm.h" #include "dlink.h" +#include "xmalloc.h" + #include #include diff --git a/managemon.c b/managemon.c index 877e8605..6ca592b1 100644 --- a/managemon.c +++ b/managemon.c @@ -104,6 +104,8 @@ #endif #include "mdadm.h" #include "mdmon.h" +#include "xmalloc.h" + #include #include diff --git a/mapfile.c b/mapfile.c index 632cf5e8..33d40d9e 100644 --- a/mapfile.c +++ b/mapfile.c @@ -43,6 +43,8 @@ * at compile time via MAP_DIR and MAP_FILE. */ #include "mdadm.h" +#include "xmalloc.h" + #include #include diff --git a/mdadm.c b/mdadm.c index 90fd1575..de7cde07 100644 --- a/mdadm.c +++ b/mdadm.c @@ -27,6 +27,8 @@ #include "mdadm.h" #include "md_p.h" +#include "xmalloc.h" + #include /** diff --git a/mdadm.h b/mdadm.h index ce8155b5..bbc1b97f 100644 --- a/mdadm.h +++ b/mdadm.h @@ -1937,11 +1937,6 @@ static inline int xasprintf(char **strp, const char *fmt, ...) { #define pr_vrb(fmt, arg...) ((void)(verbose && pr_err(fmt, ##arg))) -void *xmalloc(size_t len); -void *xrealloc(void *ptr, size_t len); -void *xcalloc(size_t num, size_t size); -char *xstrdup(const char *str); - #define LEVEL_MULTIPATH (-4) #define LEVEL_LINEAR (-1) #define LEVEL_FAULTY (-5) diff --git a/mdmon.c b/mdmon.c index 6e28b56e..a16647c6 100644 --- a/mdmon.c +++ b/mdmon.c @@ -65,6 +65,7 @@ #include "mdadm.h" #include "mdmon.h" +#include "xmalloc.h" char const Name[] = "mdmon"; diff --git a/mdopen.c b/mdopen.c index e49defb6..8587524b 100644 --- a/mdopen.c +++ b/mdopen.c @@ -25,6 +25,8 @@ #include "mdadm.h" #include "udev.h" #include "md_p.h" +#include "xmalloc.h" + #include void make_parts(char *dev, int cnt) diff --git a/mdstat.c b/mdstat.c index 29e78362..8d97a2fc 100644 --- a/mdstat.c +++ b/mdstat.c @@ -80,6 +80,8 @@ #include "mdadm.h" #include "dlink.h" +#include "xmalloc.h" + #include #include diff --git a/msg.c b/msg.c index b6da91d3..0cc7fc5d 100644 --- a/msg.c +++ b/msg.c @@ -30,6 +30,7 @@ #include #include "mdadm.h" #include "mdmon.h" +#include "xmalloc.h" static const __u32 start_magic = 0x5a5aa5a5; static const __u32 end_magic = 0xa5a55a5a; diff --git a/platform-intel.c b/platform-intel.c index 21591317..95bc4929 100644 --- a/platform-intel.c +++ b/platform-intel.c @@ -19,6 +19,8 @@ #include "mdadm.h" #include "platform-intel.h" #include "probe_roms.h" +#include "xmalloc.h" + #include #include #include diff --git a/policy.c b/policy.c index 4d4b248d..f417a9af 100644 --- a/policy.c +++ b/policy.c @@ -23,6 +23,8 @@ */ #include "mdadm.h" +#include "xmalloc.h" + #include #include #include diff --git a/restripe.c b/restripe.c index a7a7229f..5e126eb7 100644 --- a/restripe.c +++ b/restripe.c @@ -23,6 +23,8 @@ */ #include "mdadm.h" +#include "xmalloc.h" + #include /* To restripe, we read from old geometry to a buffer, and diff --git a/super-ddf.c b/super-ddf.c index d870102d..6cd099ab 100644 --- a/super-ddf.c +++ b/super-ddf.c @@ -29,6 +29,8 @@ #include "mdadm.h" #include "mdmon.h" #include "sha1.h" +#include "xmalloc.h" + #include #include diff --git a/super-gpt.c b/super-gpt.c index ec3cf53f..50c267f6 100644 --- a/super-gpt.c +++ b/super-gpt.c @@ -40,6 +40,7 @@ #include "mdadm.h" #include "part.h" +#include "xmalloc.h" static void free_gpt(struct supertype *st) { diff --git a/super-intel.c b/super-intel.c index 30c2939a..7c5119c5 100644 --- a/super-intel.c +++ b/super-intel.c @@ -23,6 +23,8 @@ #include "dlink.h" #include "sha1.h" #include "platform-intel.h" +#include "xmalloc.h" + #include #include #include diff --git a/super-mbr.c b/super-mbr.c index 839f0003..98810170 100644 --- a/super-mbr.c +++ b/super-mbr.c @@ -41,6 +41,7 @@ #include "mdadm.h" #include "part.h" +#include "xmalloc.h" static void free_mbr(struct supertype *st) { diff --git a/super0.c b/super0.c index c428e2a6..ff4905b9 100644 --- a/super0.c +++ b/super0.c @@ -25,6 +25,8 @@ #define HAVE_STDINT_H 1 #include "mdadm.h" #include "sha1.h" +#include "xmalloc.h" + /* * All handling for the 0.90.0 version superblock is in * this file. diff --git a/super1.c b/super1.c index 9c9c7dd1..fe3c4c64 100644 --- a/super1.c +++ b/super1.c @@ -24,6 +24,8 @@ #include #include "mdadm.h" +#include "xmalloc.h" + /* * The version-1 superblock : * All numeric fields are little-endian. diff --git a/sysfs.c b/sysfs.c index 7a81cc5b..0f0506ca 100644 --- a/sysfs.c +++ b/sysfs.c @@ -24,9 +24,11 @@ */ #include "mdadm.h" +#include "dlink.h" +#include "xmalloc.h" + #include #include -#include "dlink.h" #define MAX_SYSFS_PATH_LEN 120 diff --git a/udev.c b/udev.c index 066e6ab1..88a99781 100644 --- a/udev.c +++ b/udev.c @@ -22,6 +22,8 @@ #include "udev.h" #include "md_p.h" #include "md_u.h" +#include "xmalloc.h" + #include #include #include diff --git a/util.c b/util.c index 2b660725..4dfe757d 100644 --- a/util.c +++ b/util.c @@ -24,6 +24,8 @@ #include "mdadm.h" #include "md_p.h" +#include "xmalloc.h" + #include #include #include diff --git a/xmalloc.c b/xmalloc.c index 8b3f78a6..dac30eb6 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -21,64 +21,57 @@ * Email: */ -#include "mdadm.h" -/*#include -#include -#include -#include -#include -#include -#include -*/ +#include "xmalloc.h" + +#include +#include +#include +#include + +static void *exit_memory_alloc_failure(void) +{ + fprintf(stderr, "Memory allocation failure - aborting\n"); + + /* TODO: replace with MDADM_STATUS_MEM_FAIL */ + exit(1); +} void *xmalloc(size_t len) { void *rv = malloc(len); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } void *xrealloc(void *ptr, size_t len) { void *rv = realloc(ptr, len); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } void *xcalloc(size_t num, size_t size) { void *rv = calloc(num, size); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } char *xstrdup(const char *str) { char *rv = strdup(str); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } diff --git a/xmalloc.h b/xmalloc.h new file mode 100644 index 00000000..0904b0ab --- /dev/null +++ b/xmalloc.h @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#ifndef XMALLOC_H +#define XMALLOC_H + +#include + +void *xmalloc(size_t len); +void *xrealloc(void *ptr, size_t len); +void *xcalloc(size_t num, size_t size); +char *xstrdup(const char *str); + +#endif -- 2.41.0