#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, ...)
{
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)
{
exit(1);
case 0: /* child */
+ create_pid_file();
break;
default: /* parent */