cmogstored dev/user discussion/issues/patches/etc
 help / color / mirror / code / Atom feed
* [PATCH] process: try to handle OOM gracefully
@ 2016-06-01 22:32 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2016-06-01 22:32 UTC (permalink / raw)
  To: cmogstored-public

If we fail to register a process, it is not fatal since
a process is already running.  However, we may not know
about when to restart it when it dies.
---
 cmogstored.c |  4 +++-
 process.c    | 26 ++++++++++++++++++--------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/cmogstored.c b/cmogstored.c
index 5625775..2e57def 100644
--- a/cmogstored.c
+++ b/cmogstored.c
@@ -476,9 +476,11 @@ static void process_died(pid_t pid, int status)
 	default:
 		/* could be an inherited iostat if we're using worker+master */
 		name = mog_process_name(id);
+		if (!name)
+			syslog(LOG_ERR, "OOM: %m");
 		syslog(LOG_INFO,
 		       "reaped %s pid=%d with status=%d, ignoring",
-		       name, (int)pid, status);
+		       name ? name : "unknown", (int)pid, status);
 		free(name);
 	}
 }
diff --git a/process.c b/process.c
index 3122e08..8dfa10a 100644
--- a/process.c
+++ b/process.c
@@ -49,16 +49,17 @@ void mog_process_reset(void)
 
 char *mog_process_name(unsigned id)
 {
+	char *s;
 	if (mog_process_is_worker(id))
-		return xasprintf("worker[%u]", id);
+		return asprintf(&s, "worker[%u]", id) >= 0 ? s : 0;
 
 	switch (id) {
-	case MOG_PROC_UNKNOWN: return xstrdup("unknown");
-	case MOG_PROC_IOSTAT: return xstrdup("iostat");
-	case MOG_PROC_UPGRADE: return xstrdup("upgrade");
+	case MOG_PROC_UNKNOWN: return 0;
+	case MOG_PROC_IOSTAT: return strdup("iostat");
+	case MOG_PROC_UPGRADE: return strdup("upgrade");
 	}
-	assert(0 && "Unknown ID");
-	return xasprintf("BUG[%u]", id);
+
+	return asprintf(&s, "BUG[%u]", id) >= 0 ? s : 0;
 }
 
 bool mog_process_is_worker(unsigned id)
@@ -114,15 +115,24 @@ size_t mog_kill_each_worker(int signo)
 /* Registers a process with a given id */
 void mog_process_register(pid_t pid, unsigned id)
 {
-	struct mog_process *p = xmalloc(sizeof(struct mog_process));
+	struct mog_process *p = malloc(sizeof(struct mog_process));
 
 	assert(id != MOG_PROC_UNKNOWN &&
 	      "MOG_PROC_UNKNOWN may not be registered");
 
+	if (!p)
+		goto err;
+
 	p->pid = pid;
 	p->id = id;
 
-	mog_oom_if_null(hash_insert(processes, p));
+	if (hash_insert(processes, p))
+		return; /* success */
+
+	PRESERVE_ERRNO(free(p));
+err:
+	syslog(LOG_ERR, "unable to register PID:%d with id=%u: %m",
+		(int)pid, id);
 }
 
 /*
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-06-01 22:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-01 22:32 [PATCH] process: try to handle OOM gracefully Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/cmogstored.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).