[sheepdog] [PATCH V2] Replace malloc with xmalloc

harryxiyou at gmail.com harryxiyou at gmail.com
Fri Feb 22 09:32:40 CET 2013


From: Harry Wei <harryxiyou at gmail.com>

This patch replaces malloc with xmalloc for simplicity.
It also catches SIGABRT to exit with EXIT_SYSFAIL in
the case of out of memory.

Signed-off-by: Harry Wei <harryxiyou at gmail.com>
---
 collie/collie.c |   21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/collie/collie.c b/collie/collie.c
index a84baba..0cc64c7 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;
 }
 
+static void crash_handler(int signo)
+{
+	if (signo == SIGABRT)
+		fprintf(stderr, "collie abort.\n");
+	else
+		fprintf(stderr, "collie got unexpected signal %d.\n", signo);
+
+	exit(EXIT_SYSFAIL);
+}
+
 int main(int argc, char **argv)
 {
 	int ch, longindex, ret;
@@ -321,9 +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);
 
+	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