mcelog/mcelog_daemon_create_pid_file.patch
OBS User autobuild d4c5115ba8 Accepting request 29009 from Base:System
Copy from Base:System/mcelog based on submit request 29009 from user coolo

OBS-URL: https://build.opensuse.org/request/show/29009
OBS-URL: https://build.opensuse.org/package/show/openSUSE:Factory/mcelog?expand=0&rev=7
2010-01-12 15:13:00 +00:00

86 lines
1.9 KiB
Diff

mcelog daemon: Add pid file handling
Signed-off-by: Thomas Renninger <trenn@suse.de>
---
mcelog.c | 41 +++++++++++++++++++++++++++++++++++++++--
paths.h | 2 ++
2 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/mcelog.c b/mcelog.c
index e166f57..a556664 100644
--- a/mcelog.c
+++ b/mcelog.c
@@ -35,6 +35,8 @@
#include <stddef.h>
#include <assert.h>
#include <pwd.h>
+#include <signal.h>
+
#include "mcelog.h"
#include "paths.h"
#include "k8.h"
@@ -977,6 +979,37 @@ static void process_mcefd(struct pollfd *pfd, void *data)
process(pfd->fd, d->recordlen, d->loglen, d->buf);
}
+void exit_handler (int signal)
+{
+ remove(DAEMON_PID_PATH);
+}
+
+static int daemonize(int foreground)
+{
+ int ret;
+ pid_t pid;
+ FILE *fd;
+
+ fd = fopen(DAEMON_PID_PATH, "r");
+ if (!fd) {
+ if (!foreground) {
+ ret = daemon(0, need_stdout());
+ if (ret < 0)
+ return ret;
+ }
+ fd = fopen(DAEMON_PID_PATH, "w+");
+ if (!fd)
+ return -1;
+ pid = getpid();
+ fprintf(fd, "%d\n", pid);
+ fclose(fd);
+ signal(SIGKILL, exit_handler);
+ signal(SIGTERM, exit_handler);
+ return 0;
+ } else
+ return -1;
+}
+
int main(int ac, char **av)
{
struct mcefd_data d = {};
@@ -1031,8 +1064,12 @@ int main(int ac, char **av)
page_setup();
drop_cred();
register_pollcb(fd, POLLIN, process_mcefd, &d);
- if (!foreground && daemon(0, need_stdout()) < 0)
- err("daemon");
+ if (daemonize(foreground) < 0) {
+ fprintf(stderr, "Could not run in background or create"
+ " pidfile: " DAEMON_PID_PATH "\n");
+ fprintf(stderr, "Is the daemon already running?\n");
+ exit(1);
+ }
eventloop();
} else {
process(fd, d.recordlen, d.loglen, d.buf);
diff --git a/paths.h b/paths.h
index 6780552..efda627 100644
--- a/paths.h
+++ b/paths.h
@@ -5,3 +5,5 @@
#define CONFIG_FILENAME PREFIX "/etc/mcelog/mcelog.conf"
#define SOCKET_PATH "/var/run/mcelog-client"
+
+#define DAEMON_PID_PATH "/var/run/mcelog.pid"