Skip to content

Commit

Permalink
pid_lock: Fix missing include under Linux & inconsistent whitespace
Browse files Browse the repository at this point in the history
* And restore default config.mk for Linux
* Should have noticed before pushing...
  • Loading branch information
updateing committed Mar 11, 2017
1 parent f63c431 commit 4bfe420
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 71 deletions.
6 changes: 3 additions & 3 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ PLUGIN_MODULES := \
packet_plugin_rjv3

# Linux
# PLUGIN_MODULES += if_impl_sockraw
PLUGIN_MODULES += if_impl_sockraw

# macOS / BSD
PLUGIN_MODULES += if_impl_bpf
# PLUGIN_MODULES += if_impl_bpf

# Other OS
# PLUGIN_MODULES += if_impl_libpcap
Expand All @@ -22,7 +22,7 @@ STATIC_BUILD := false

# If your platform has iconv_* integrated into libc, change to false
# Affects dynamic linking
LIBICONV_STANDALONE := true
LIBICONV_STANDALONE := false

CUSTOM_CFLAGS :=
CUSTOM_LDFLAGS :=
Expand Down
140 changes: 72 additions & 68 deletions util/pid_lock.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/file.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
Expand Down Expand Up @@ -36,84 +37,87 @@ RESULT pid_lock_init(const char* pidfile) {
// Return SUCCESS: We handled the incident and are ready to proceed (i.e. only when user asked)
// Return FAILURE: We could not handle, or we do not want to proceed
static RESULT pid_lock_handle_multiple_instance() {
char readbuf[PID_STRING_BUFFER_SIZE]; // 12 is big enough to hold PID number

if (read(pid_lock_fd, readbuf, PID_STRING_BUFFER_SIZE) < 0 || readbuf[0] == '\0') {
PR_ERRNO("已有另一个 MiniEAP 进程正在运行但 PID 未知,请手动结束其他 MiniEAP 进程");
return FAILURE;
} else {
int pid = atoi(readbuf);
switch (get_program_config()->kill_type) {
case KILL_NONE:
PR_ERR("已有另一个 MiniEAP 进程正在运行,PID 为 %d", pid);
return FAILURE;
case KILL_ONLY:
PR_ERR("已有另一个 MiniEAP 进程正在运行,PID 为 %d,即将发送终止信号并退出……", pid);
kill(pid, SIGTERM);
return FAILURE;
case KILL_AND_START:
PR_WARN("已有另一个 MiniEAP 进程正在运行,PID 为 %d,将在发送终止信号后继续……", pid);
kill(pid, SIGTERM);
return SUCCESS;
}
}
char readbuf[PID_STRING_BUFFER_SIZE]; // 12 is big enough to hold PID number

if (read(pid_lock_fd, readbuf, PID_STRING_BUFFER_SIZE) < 0 || readbuf[0] == '\0') {
PR_ERRNO("已有另一个 MiniEAP 进程正在运行但 PID 未知,请手动结束其他 MiniEAP 进程");
return FAILURE;
} else {
int pid = atoi(readbuf);
switch (get_program_config()->kill_type) {
case KILL_NONE:
PR_ERR("已有另一个 MiniEAP 进程正在运行,PID 为 %d", pid);
return FAILURE;
case KILL_ONLY:
PR_ERR("已有另一个 MiniEAP 进程正在运行,PID 为 %d,即将发送终止信号并退出……", pid);
kill(pid, SIGTERM);
return FAILURE;
case KILL_AND_START:
PR_WARN("已有另一个 MiniEAP 进程正在运行,PID 为 %d,将在发送终止信号后继续……", pid);
kill(pid, SIGTERM);
return SUCCESS;
default:
PR_ERR("-k 参数未知");
return FAILURE;
}
}
}

RESULT pid_lock_save_pid() {
if (pid_lock_fd == 0) {
PR_WARN("PID 文件尚未初始化");
return FAILURE;
} else if (pid_lock_fd < 0) {
// User disabled pid lock
return SUCCESS;
}
if (pid_lock_fd == 0) {
PR_WARN("PID 文件尚未初始化");
return FAILURE;
} else if (pid_lock_fd < 0) {
// User disabled pid lock
return SUCCESS;
}

char writebuf[PID_STRING_BUFFER_SIZE];
char writebuf[PID_STRING_BUFFER_SIZE];

my_itoa(getpid(), writebuf, 10);
my_itoa(getpid(), writebuf, 10);

if (write(pid_lock_fd, writebuf, strnlen(writebuf, PID_STRING_BUFFER_SIZE)) < 0) {
PR_ERRNO("无法将 PID 保存到 PID 文件");
return FAILURE;
}
if (write(pid_lock_fd, writebuf, strnlen(writebuf, PID_STRING_BUFFER_SIZE)) < 0) {
PR_ERRNO("无法将 PID 保存到 PID 文件");
return FAILURE;
}

return SUCCESS;
return SUCCESS;
}

RESULT pid_lock_lock() {
if (pid_lock_fd == 0) {
PR_WARN("PID 文件尚未初始化");
return FAILURE;
} else if (pid_lock_fd < 0) {
// User disabled pid lock
return SUCCESS;
}

int lock_result = flock(pid_lock_fd, LOCK_EX | LOCK_NB);
if (lock_result < 0) {
if (errno == EWOULDBLOCK) {
if (IS_FAIL(pid_lock_handle_multiple_instance())) {
close(pid_lock_fd);
pid_lock_fd = 0;
return FAILURE;
} // Continue if handled
} else {
PR_ERRNO("无法对 PID 文件加锁");
return FAILURE;
}
}

return SUCCESS;
if (pid_lock_fd == 0) {
PR_WARN("PID 文件尚未初始化");
return FAILURE;
} else if (pid_lock_fd < 0) {
// User disabled pid lock
return SUCCESS;
}

int lock_result = flock(pid_lock_fd, LOCK_EX | LOCK_NB);
if (lock_result < 0) {
if (errno == EWOULDBLOCK) {
if (IS_FAIL(pid_lock_handle_multiple_instance())) {
close(pid_lock_fd);
pid_lock_fd = 0;
return FAILURE;
} // Continue if handled
} else {
PR_ERRNO("无法对 PID 文件加锁");
return FAILURE;
}
}

return SUCCESS;
}

RESULT pid_lock_destroy() {
if (pid_lock_fd <= 0) {
return SUCCESS;
}

close(pid_lock_fd); // Unlocks the file simultaneously
if (unlink(get_program_config()->pidfile) < 0) {
PR_WARN("无法删除 PID 文件");
}
return SUCCESS;
}
if (pid_lock_fd <= 0) {
return SUCCESS;
}

close(pid_lock_fd); // Unlocks the file simultaneously
if (unlink(get_program_config()->pidfile) < 0) {
PR_WARN("无法删除 PID 文件");
}
return SUCCESS;
}

0 comments on commit 4bfe420

Please sign in to comment.