diff options
author | YoungHun Kim <yh8004.kim@samsung.com> | 2019-04-25 09:24:15 +0900 |
---|---|---|
committer | YoungHun Kim <yh8004.kim@samsung.com> | 2019-04-25 09:47:42 +0900 |
commit | 2e50ff6d704db0eb0a9c8e7d0a8b403790918769 (patch) | |
tree | 5afe7958a42ae7a8baeca2f2c11b8aa2318682e8 | |
parent | 34593203891047956ad10748f4eeaba7a1b28389 (diff) | |
download | murphy-2e50ff6d704db0eb0a9c8e7d0a8b403790918769.tar.gz murphy-2e50ff6d704db0eb0a9c8e7d0a8b403790918769.tar.bz2 murphy-2e50ff6d704db0eb0a9c8e7d0a8b403790918769.zip |
Add PIDFile for forking typesubmit/tizen/20190425.081607accepted/tizen/unified/20190426.054119
Change-Id: Ieb37b8c370d2dd74b1fd6a0d6417c109079448be
-rw-r--r-- | packaging/murphy.spec | 2 | ||||
-rw-r--r-- | packaging/murphyd.service | 1 | ||||
-rw-r--r-- | src/common/utils.c | 50 |
3 files changed, 52 insertions, 1 deletions
diff --git a/packaging/murphy.spec b/packaging/murphy.spec index 0706b1c..0c080ee 100644 --- a/packaging/murphy.spec +++ b/packaging/murphy.spec @@ -28,7 +28,7 @@ Summary: Resource policy framework Name: murphy -Version: 0.0.74 +Version: 0.0.75 Release: 14 License: BSD-3-Clause Group: System/Service diff --git a/packaging/murphyd.service b/packaging/murphyd.service index 324a2eb..ca91961 100644 --- a/packaging/murphyd.service +++ b/packaging/murphyd.service @@ -4,6 +4,7 @@ Description=Murphy Resource Policy Daemon [Service] ExecStart=/usr/bin/murphyd -t dlog -vvv Type=forking +PIDFile=/tmp/.murphyd.pid KillMode=process KillSignal=SIGKILL Restart=always diff --git a/src/common/utils.c b/src/common/utils.c index 8fd1f76..c9c11b3 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -41,6 +41,8 @@ #include <murphy/common/utils.h> #define MSG_OK "OK" +#define PID_FILE "/tmp/.murphyd.pid" +#define PID_MSG_LEN 16 static int notify_parent(int fd, const char *fmt, ...) { @@ -54,6 +56,53 @@ static int notify_parent(int fd, const char *fmt, ...) return (len > 0); } +static void create_pid_file(void) +{ + int fd; + struct flock lock; + char pid_buf[PID_MSG_LEN] = {'\0',}; + + fd = open(PID_FILE, O_WRONLY | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); + if (fd < 0) { + mrp_log_error("PID file cannot be created (%d)", errno); + exit(1); + } + + lock.l_type = F_WRLCK; + lock.l_start = 0; + lock.l_whence = SEEK_SET; + lock.l_len = 1000; + + if (fcntl(fd, F_SETLK, &lock) < 0) { + if (errno != EACCES && errno != EAGAIN) + mrp_log_error("Fail to lock pidfile [%d]", errno); + else + mrp_log_error("process is already running"); + goto error; + } + + if (ftruncate(fd, 0) < 0) { + mrp_log_error("Fail to truncate pidfile [%d]", errno); + goto error; + } + + memset(pid_buf, 0, sizeof(pid_buf)); + snprintf(pid_buf, sizeof(pid_buf), "%u", getpid()); + + if (write(fd, pid_buf, strlen(pid_buf)) != (int)strlen(pid_buf)) { + mrp_log_error("Fail to write pid to pidfile [%d]", errno); + goto error; + } + + close(fd); + + mrp_log_info("PID file (%s) is created", PID_FILE); + return; + +error: + close(fd); + exit(1); +} int mrp_daemonize(const char *dir, const char *new_out, const char *new_err) { @@ -137,6 +186,7 @@ int mrp_daemonize(const char *dir, const char *new_out, const char *new_err) exit(1); case 0: /* child */ + create_pid_file(); break; default: /* parent */ |