]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdadm: add xmalloc.h
authorMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Wed, 25 Sep 2024 11:16:10 +0000 (13:16 +0200)
committerMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Fri, 27 Sep 2024 09:16:22 +0000 (11:16 +0200)
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 <mariusz.tkaczyk@linux.intel.com>
33 files changed:
Assemble.c
Create.c
Detail.c
Examine.c
Grow.c
Incremental.c
Manage.c
Monitor.c
bitmap.c
config.c
lib.c
managemon.c
mapfile.c
mdadm.c
mdadm.h
mdmon.c
mdopen.c
mdstat.c
msg.c
platform-intel.c
policy.c
restripe.c
super-ddf.c
super-gpt.c
super-intel.c
super-mbr.c
super0.c
super1.c
sysfs.c
udev.c
util.c
xmalloc.c
xmalloc.h [new file with mode: 0644]

index a2bb7b6465d99a7dba902fb777831b61919d0fc7..d3dbfbc6d79f83865a0deae7c9f4c1f253dc4285 100644 (file)
@@ -23,6 +23,8 @@
  */
 
 #include       "mdadm.h"
+#include       "xmalloc.h"
+
 #include       <ctype.h>
 
 mapping_t assemble_statuses[] = {
index 7fde1c1695226b28e6ca17a28f4b4e7804db19ca..1640f21e9a6a6ee72e04bcc961ea2646c288483b 100644 (file)
--- a/Create.c
+++ b/Create.c
  */
 
 #include       "mdadm.h"
-#include       "udev.h"
 #include       "md_u.h"
 #include       "md_p.h"
+#include       "udev.h"
+#include       "xmalloc.h"
+
 #include       <ctype.h>
 #include       <fcntl.h>
 #include       <signal.h>
index 331e1da300acf7cf1f6c27dfa0c6cbbac7ea6224..5819ced9a9fcf2750e90c97592fd6f54f8b1e476 100644 (file)
--- 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       <ctype.h>
 #include       <dirent.h>
 
index fe16216763b71362a2cc4561ceb44bc591b5a46b..036b7a5634ad3d0f00c6adeacc98812f390d74b7 100644 (file)
--- a/Examine.c
+++ b/Examine.c
  *    Email: <neilb@suse.de>
  */
 
-#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 60076f56054ca2c5c38a1fd90aed48a732f0194c..ef1285ecebcf6d36593b14fb84dac2c68e12fce2 100644 (file)
--- a/Grow.c
+++ b/Grow.c
@@ -23,6 +23,8 @@
  */
 #include       "mdadm.h"
 #include       "dlink.h"
+#include       "xmalloc.h"
+
 #include       <sys/mman.h>
 #include       <stddef.h>
 #include       <stdint.h>
index e7987d1d210fd597749b3e21e283973cd884f32c..8c915783486221ca91e4d55a5b4c4feb3f073867 100644 (file)
@@ -29,6 +29,8 @@
  */
 
 #include       "mdadm.h"
+#include       "xmalloc.h"
+
 #include       <sys/wait.h>
 #include       <dirent.h>
 #include       <ctype.h>
index 0f232a579d9ecf28f3ca360dc4b5d4a38e4c8bd5..246ef3194aaaf69b8fe86e5f117357422d593490 100644 (file)
--- 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 <ctype.h>
 
 int Manage_ro(char *devname, int fd, int readonly)
index d260b0f13435745c0034cd4b104ed8cfb8fe64c8..d1cfbf947191e073ed6643daaabfdffcb249e49f 100644 (file)
--- a/Monitor.c
+++ b/Monitor.c
  */
 
 #include       "mdadm.h"
-#include       "udev.h"
 #include       "md_p.h"
 #include       "md_u.h"
+#include       "udev.h"
+#include       "xmalloc.h"
+
 #include       <sys/wait.h>
 #include       <limits.h>
 #include       <syslog.h>
index 9a7ffe3bb1bf209d2246ef539800272444ec63ce..5110ae2f5ec85a8a04b65463b1473b406ceae7c6 100644 (file)
--- 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)
 {
index 5411a4804ac37720fa36e6480b378744212ff02d..022fb2a0f59846137bd4198f3192bfb60648d9ac 100644 (file)
--- a/config.c
+++ b/config.c
@@ -24,6 +24,8 @@
 
 #include       "mdadm.h"
 #include       "dlink.h"
+#include       "xmalloc.h"
+
 #include       <dirent.h>
 #include       <glob.h>
 #include       <fnmatch.h>
diff --git a/lib.c b/lib.c
index 13d4e4f1f87a071531ee86f9f736b57f728c816f..f36ae03a3fa096a2c68e6ce9fbdc6d68179264ef 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -24,6 +24,8 @@
 
 #include       "mdadm.h"
 #include       "dlink.h"
+#include       "xmalloc.h"
+
 #include       <ctype.h>
 #include       <limits.h>
 
index 877e8605bcb0b530118990f3014ae32aeb96d8c0..6ca592b19046ce8347150d106d01f03d7990c431 100644 (file)
 #endif
 #include       "mdadm.h"
 #include       "mdmon.h"
+#include       "xmalloc.h"
+
 #include       <sys/syscall.h>
 #include       <sys/socket.h>
 
index 632cf5e89f39b5993781f29adbedd9a203c78678..33d40d9e60fab957f0d0d54cf4a0c3308d902b50 100644 (file)
--- 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       <sys/file.h>
 #include       <ctype.h>
 
diff --git a/mdadm.c b/mdadm.c
index 90fd157575c9bf637b9d4b960041c8df9ffb148c..de7cde07c0a1dd4cdc1c4382de3126d766b493d2 100644 (file)
--- a/mdadm.c
+++ b/mdadm.c
@@ -27,6 +27,8 @@
 
 #include "mdadm.h"
 #include "md_p.h"
+#include "xmalloc.h"
+
 #include <ctype.h>
 
 /**
diff --git a/mdadm.h b/mdadm.h
index ce8155b51077cc59dfcd2c1c5132b141ce349001..bbc1b97f566da07918ac8574541f087d0565fafb 100644 (file)
--- 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 6e28b56e361b5e17cc2c2df0cab1efbfbac05940..a16647c6a85f75f92538dc4349f8de78920e4115 100644 (file)
--- a/mdmon.c
+++ b/mdmon.c
@@ -65,6 +65,7 @@
 
 #include       "mdadm.h"
 #include       "mdmon.h"
+#include       "xmalloc.h"
 
 char const Name[] = "mdmon";
 
index e49defb6744d0691b322aee7a3bf7e7699206267..8587524b09b832e34ac379b0ba2d988d1f778779 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -25,6 +25,8 @@
 #include "mdadm.h"
 #include "udev.h"
 #include "md_p.h"
+#include "xmalloc.h"
+
 #include <ctype.h>
 
 void make_parts(char *dev, int cnt)
index 29e7836288c3243b7cacc7690c88596e6ab157b3..8d97a2fc5dd8aae5769f398102935b4dd4fe28f7 100644 (file)
--- a/mdstat.c
+++ b/mdstat.c
@@ -80,6 +80,8 @@
 
 #include       "mdadm.h"
 #include       "dlink.h"
+#include       "xmalloc.h"
+
 #include       <sys/select.h>
 #include       <ctype.h>
 
diff --git a/msg.c b/msg.c
index b6da91d387e0767fce2241bd28069ea92d0f90a2..0cc7fc5d040ffbd7ef013d5bba25b4f05b8a01ef 100644 (file)
--- a/msg.c
+++ b/msg.c
@@ -30,6 +30,7 @@
 #include <sys/un.h>
 #include "mdadm.h"
 #include "mdmon.h"
+#include "xmalloc.h"
 
 static const __u32 start_magic = 0x5a5aa5a5;
 static const __u32 end_magic = 0xa5a55a5a;
index 215913175eed8a9561248b72531cce9a4932d07f..95bc4929e2c6a483591b9a62d62205c5750e4c81 100644 (file)
@@ -19,6 +19,8 @@
 #include "mdadm.h"
 #include "platform-intel.h"
 #include "probe_roms.h"
+#include "xmalloc.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
index 4d4b248d511443f8b25831aedc6a8c69adf49fa5..f417a9af0c9d6012122c6292f57550800ad963c8 100644 (file)
--- a/policy.c
+++ b/policy.c
@@ -23,6 +23,8 @@
  */
 
 #include "mdadm.h"
+#include "xmalloc.h"
+
 #include <dirent.h>
 #include <fnmatch.h>
 #include <ctype.h>
index a7a7229fc06ba4a7ced0ecde0f5b003aef49330a..5e126eb7bfa54a15af8c7d3369d9f1f2a0b2221a 100644 (file)
@@ -23,6 +23,8 @@
  */
 
 #include "mdadm.h"
+#include "xmalloc.h"
+
 #include <stdint.h>
 
 /* To restripe, we read from old geometry to a buffer, and
index d870102d91e5164559ec20563dc37f89eb10082e..6cd099abe893a1ecf4b026fb95de60bdebdb8ffd 100644 (file)
@@ -29,6 +29,8 @@
 #include "mdadm.h"
 #include "mdmon.h"
 #include "sha1.h"
+#include "xmalloc.h"
+
 #include <values.h>
 #include <stddef.h>
 
index ec3cf53f8620978783dc67a0eb2a230a9423a385..50c267f6cfbc2f3adac216e0796a858eef78fa11 100644 (file)
@@ -40,6 +40,7 @@
 
 #include "mdadm.h"
 #include "part.h"
+#include "xmalloc.h"
 
 static void free_gpt(struct supertype *st)
 {
index 30c2939a3df660a6afa877d1286bdba2eb6b8be9..7c5119c530a49ba0a6bd07a0ea98f6e9aca0acaf 100644 (file)
@@ -23,6 +23,8 @@
 #include "dlink.h"
 #include "sha1.h"
 #include "platform-intel.h"
+#include "xmalloc.h"
+
 #include <values.h>
 #include <scsi/sg.h>
 #include <ctype.h>
index 839f00032f844b2aae3e82076b551823abb7e112..988101707a142f2149e0e1b1d8052cb9c48eded4 100644 (file)
@@ -41,6 +41,7 @@
 
 #include "mdadm.h"
 #include "part.h"
+#include "xmalloc.h"
 
 static void free_mbr(struct supertype *st)
 {
index c428e2a68dae6915ae1251c8f503ade1ce949684..ff4905b964112f899987e3c1cb67ba43eb84dbe7 100644 (file)
--- 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.
index 9c9c7dd14c150d8c04e9ac92ba9c3c49ca5a4d65..fe3c4c64c66bf5b6e3280d440eefe4bdf4ddc12b 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -24,6 +24,8 @@
 
 #include <stddef.h>
 #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 7a81cc5b4ffd65073d322b88aaea63d7606c8e19..0f0506caca65a79f5068c59f3fc0525a89ba43fa 100644 (file)
--- a/sysfs.c
+++ b/sysfs.c
  */
 
 #include       "mdadm.h"
+#include       "dlink.h"
+#include       "xmalloc.h"
+
 #include       <dirent.h>
 #include       <ctype.h>
-#include       "dlink.h"
 
 #define MAX_SYSFS_PATH_LEN     120
 
diff --git a/udev.c b/udev.c
index 066e6ab1c2924c9cfd3bb057bcc02440d1cb9da9..88a997818115995677e025aa47db27c4827ad265 100644 (file)
--- 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       <sys/wait.h>
 #include       <signal.h>
 #include       <limits.h>
diff --git a/util.c b/util.c
index 2b6607252402668be786ddc087124ef43664f3d3..4dfe757d49ee6d47372e7d25c52c2ba735e54833 100644 (file)
--- a/util.c
+++ b/util.c
@@ -24,6 +24,8 @@
 
 #include       "mdadm.h"
 #include       "md_p.h"
+#include       "xmalloc.h"
+
 #include       <sys/socket.h>
 #include       <sys/utsname.h>
 #include       <sys/wait.h>
index 8b3f78a6d59e898b2648bb7b1285f4a23b854515..dac30eb6107f8cb2442b2ec89cce3513549393e9 100644 (file)
--- a/xmalloc.c
+++ b/xmalloc.c
  *    Email: <neilb@suse.de>
  */
 
-#include       "mdadm.h"
-/*#include     <sys/socket.h>
-#include       <sys/utsname.h>
-#include       <sys/wait.h>
-#include       <sys/un.h>
-#include       <ctype.h>
-#include       <dirent.h>
-#include       <signal.h>
-*/
+#include "xmalloc.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+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 (file)
index 0000000..0904b0a
--- /dev/null
+++ b/xmalloc.h
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#ifndef XMALLOC_H
+#define XMALLOC_H
+
+#include <stddef.h>
+
+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