summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>2024-09-25 13:16:10 +0200
committerMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>2024-09-27 11:16:22 +0200
commitee3a6cab09c8acaf6706b3710f5652e9be43b57e (patch)
tree67b0a477d132ea1347c4dd462e9d3b6cea3644e5
parentMdmonitor: Fix startup with missing directory (diff)
downloadmdadm-ee3a6cab09c8acaf6706b3710f5652e9be43b57e.tar.xz
mdadm-ee3a6cab09c8acaf6706b3710f5652e9be43b57e.zip
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 <mariusz.tkaczyk@linux.intel.com>
-rw-r--r--Assemble.c2
-rw-r--r--Create.c4
-rw-r--r--Detail.c2
-rw-r--r--Examine.c8
-rw-r--r--Grow.c2
-rw-r--r--Incremental.c2
-rw-r--r--Manage.c2
-rw-r--r--Monitor.c4
-rw-r--r--bitmap.c1
-rw-r--r--config.c2
-rw-r--r--lib.c2
-rw-r--r--managemon.c2
-rw-r--r--mapfile.c2
-rw-r--r--mdadm.c2
-rw-r--r--mdadm.h5
-rw-r--r--mdmon.c1
-rw-r--r--mdopen.c2
-rw-r--r--mdstat.c2
-rw-r--r--msg.c1
-rw-r--r--platform-intel.c2
-rw-r--r--policy.c2
-rw-r--r--restripe.c2
-rw-r--r--super-ddf.c2
-rw-r--r--super-gpt.c1
-rw-r--r--super-intel.c2
-rw-r--r--super-mbr.c1
-rw-r--r--super0.c2
-rw-r--r--super1.c2
-rw-r--r--sysfs.c4
-rw-r--r--udev.c2
-rw-r--r--util.c2
-rw-r--r--xmalloc.c59
-rw-r--r--xmalloc.h13
33 files changed, 100 insertions, 44 deletions
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 <ctype.h>
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 <ctype.h>
#include <fcntl.h>
#include <signal.h>
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 <ctype.h>
#include <dirent.h>
diff --git a/Examine.c b/Examine.c
index fe162167..036b7a56 100644
--- a/Examine.c
+++ b/Examine.c
@@ -22,14 +22,16 @@
* 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 60076f56..ef1285ec 100644
--- 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>
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 <sys/wait.h>
#include <dirent.h>
#include <ctype.h>
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 <ctype.h>
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 <sys/wait.h>
#include <limits.h>
#include <syslog.h>
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 <dirent.h>
#include <glob.h>
#include <fnmatch.h>
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 <ctype.h>
#include <limits.h>
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 <sys/syscall.h>
#include <sys/socket.h>
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 <sys/file.h>
#include <ctype.h>
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 <ctype.h>
/**
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 <ctype.h>
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 <sys/select.h>
#include <ctype.h>
diff --git a/msg.c b/msg.c
index b6da91d3..0cc7fc5d 100644
--- 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;
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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 <dirent.h>
#include <fnmatch.h>
#include <ctype.h>
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 <stdint.h>
/* 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 <values.h>
#include <stddef.h>
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 <values.h>
#include <scsi/sg.h>
#include <ctype.h>
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 <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 7a81cc5b..0f0506ca 100644
--- a/sysfs.c
+++ b/sysfs.c
@@ -24,9 +24,11 @@
*/
#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 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 <sys/wait.h>
#include <signal.h>
#include <limits.h>
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 <sys/socket.h>
#include <sys/utsname.h>
#include <sys/wait.h>
diff --git a/xmalloc.c b/xmalloc.c
index 8b3f78a6..dac30eb6 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -21,64 +21,57 @@
* 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
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 <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