Lock file operations fixed in listener
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 3 Nov 2014 09:20:33 +0000 (10:20 +0100)
committerMaciej J. Karpiuk <m.karpiuk2@samsung.com>
Tue, 17 Feb 2015 10:00:04 +0000 (11:00 +0100)
Change-Id: If94b0167477306d1af4ea5c60a8b17d83fe13b39

src/listener/listener-daemon.cpp

index e5f4839..e283269 100644 (file)
 
 namespace {
 const char* const CKM_LOCK = "/var/run/key-manager.pid";
+const char* const LISTENER_LOCK = "/var/run/key-manager-listener.pid";
 };
 
 void daemonize()
 {
     // Let's operate in background
+    int fd;
     int result = fork();
     if (result < 0){
         SLOG(LOG_ERROR, CKM_LISTENER_TAG, "%s", "Error in fork!");
@@ -66,27 +68,33 @@ void daemonize()
 
     // Let's change current directory
     if (-1 == chdir("/")) {
-        SLOG(LOG_ERROR, CKM_LISTENER_TAG, "%s", "Error in chdir!");
+        SLOG(LOG_ERROR, CKM_LISTENER_TAG, "Error in chdir!");
         exit(1);
     }
 
     // Let's create lock file
-    result = open("/tmp/ckm-listener.lock", O_RDWR | O_CREAT, 0640);
-    if (result < 0) {
-        SLOG(LOG_ERROR, CKM_LISTENER_TAG, "%s", "Error in opening lock file!");
+    fd = TEMP_FAILURE_RETRY(creat(LISTENER_LOCK, 0640));
+    if (fd < 0) {
+        SLOG(LOG_ERROR, CKM_LISTENER_TAG, "Error in opening lock file!");
         exit(1);
     }
 
-    if (lockf(result, F_TLOCK, 0) < 0) {
-        SLOG(LOG_ERROR, CKM_LISTENER_TAG, "%s", "Daemon already working!");
-        exit(0);
+    if (lockf(fd, F_TLOCK, 0) < 0) {
+        if (errno == EACCES || errno == EAGAIN) {
+            SLOG(LOG_ERROR, CKM_LISTENER_TAG, "Daemon already working!");
+            exit(0);
+        }
+        SLOG(LOG_ERROR, CKM_LISTENER_TAG, "lockf failed with error: %s" , strerror(errno));
+        exit(1);
     }
 
-    char str[100];
-    sprintf(str, "%d\n", getpid());
-    result = write(result, str, strlen(str));
+    std::string pid = std::to_string(getpid());
+    if (TEMP_FAILURE_RETRY(write(fd, pid.c_str(), pid.size())) <= 0) {
+        SLOG(LOG_ERROR, CKM_LISTENER_TAG, "Failed to write lock file. Error: %s", strerror(errno));
+        exit(1);
+    }
 
-    SLOG(LOG_DEBUG, CKM_LISTENER_TAG, "%s", str);
+    SLOG(LOG_DEBUG, CKM_LISTENER_TAG, "%s", pid.c_str());
 }
 
 bool isCkmRunning()