Add PIDFile for forking type 98/204598/3 accepted/tizen/unified/20190426.054119 submit/tizen/20190425.081607
authorYoungHun Kim <yh8004.kim@samsung.com>
Thu, 25 Apr 2019 00:24:15 +0000 (09:24 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Thu, 25 Apr 2019 00:47:42 +0000 (09:47 +0900)
Change-Id: Ieb37b8c370d2dd74b1fd6a0d6417c109079448be

packaging/murphy.spec
packaging/murphyd.service
src/common/utils.c

index 0706b1cf00294f01a873cdb0de250c4157cde2ab..0c080eed365da01492c6d8cb4f357650bd7b72d8 100644 (file)
@@ -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
index 324a2eb58831daddfa1b5f1bc99e2df104bc42aa..ca91961cf4295e00cc7a4313e9a654f7e0835acd 100644 (file)
@@ -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
index 8fd1f768ad2c757192156368ff2cdabc085bd353..c9c11b3849ff898df3885723933fa4c7e3b77b29 100644 (file)
@@ -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 */