Add app file lock API.

Change-Id: If69093cc3f0ba482acfdc916b6d9f41981ade3d6
diff --git a/mbtk/libmbtk_lib/src/mbtk_utils.c b/mbtk/libmbtk_lib/src/mbtk_utils.c
index 3d61daf..fe4d716 100755
--- a/mbtk/libmbtk_lib/src/mbtk_utils.c
+++ b/mbtk/libmbtk_lib/src/mbtk_utils.c
@@ -12,6 +12,10 @@
 #include <string.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/types.h>
 
 #include "mbtk_type.h"
 #include "mbtk_utils.h"
@@ -536,4 +540,30 @@
     }
 }
 
+int app_already_running(const char *pid_file)
+{
+    int fd;
+    char buff[16];
+
+    fd = open(pid_file, O_RDWR | O_CREAT, 0644);
+    if(fd < 0) {
+        LOGE("Open %s fail:%d", pid_file, errno);
+        exit(1);
+    }
+
+    if(flock(fd, LOCK_EX | LOCK_NB)) {
+        if(EWOULDBLOCK == errno) {
+            LOGE("%s is running.", pid_file);
+            exit(1);
+        } else {
+            close(fd);
+            return 1;
+        }
+    }
+
+    ftruncate(fd, 0);
+    sprintf(buff, "%ld", (long)getpid());
+    write(fd, buff, strlen(buff) + 1);
+    return 0;
+}