[sheepdog] [PATCH V2]Replace malloc with xmalloc for collie

harryxiyou at gmail.com harryxiyou at gmail.com
Thu Feb 21 05:57:00 CET 2013


v2: Merge Hitoshi Mitake and Morita's suggestions
	1, Replace malloc with xmalloc
	2, Catch SIGABRT for xmalloc

Signed-off-by: Harry Wei <harryxiyou at gmail.com>
Subject: Replace malloc with xmalloc for collie

---
 collie/collie.c |   22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/collie/collie.c b/collie/collie.c
index a84baba..e952596 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -15,6 +15,7 @@
 #include "sheepdog_proto.h"
 #include "sheep.h"
 #include "collie.h"
+#include "util.h"
 
 static const char program_name[] = "collie";
 const char *sdhost = "localhost";
@@ -143,7 +144,7 @@ static void init_commands(const struct command **commands)
 	};
 
 	if (!cmds) {
-		cmds = (struct command *)malloc(sizeof(command_list));
+		cmds = (struct command *)xmalloc(sizeof(command_list));
 		memcpy(cmds, command_list, sizeof(command_list));
 	}
 
@@ -312,6 +313,16 @@ static const struct sd_option *build_sd_options(const char *opts)
 	return sd_opts;
 }
 
+void crash_handler(int signo)
+{
+	if (signo == SIGABRT)
+		fprintf(stderr, "collie abort.\n");
+	else
+		fprintf(stderr, "collie got unexpected signal %d.\n", signo);
+
+	exit(1);
+}
+
 int main(int argc, char **argv)
 {
 	int ch, longindex, ret;
@@ -321,8 +332,17 @@ int main(int argc, char **argv)
 	const char *short_options;
 	char *p;
 	const struct sd_option *sd_opts;
+	struct sigaction sa_old;
+	struct sigaction sa_new;
 
 	init_commands(&commands);
+
+	/* check xmalloc SIGABRT signal in init_commands func */
+	sa_new.sa_handler = crash_handler;
+	sa_new.sa_flags = 0;
+	sigemptyset(&sa_new.sa_mask);
+
+	sigaction(SIGABRT, &sa_new, &sa_old);
 
 	if (argc < 3)
 		usage(commands, 0);
-- 
1.7.9.5




More information about the sheepdog mailing list