Skip to content

Commit

Permalink
daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
pgq committed Jun 10, 2017
1 parent 9e2b169 commit cc3d8cc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
25 changes: 15 additions & 10 deletions base/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const static char *log_level_str[] = {

static int log_fd = -1;

static int timestamp(char *);
static int timestamp(char *, size_t len);

int log_init(fcy_str *file_name)
{
Expand Down Expand Up @@ -49,7 +49,7 @@ void log_base(const char *file,
size_t i = 0;
va_list ap;

i += timestamp(data);
i += timestamp(data, 32);
i += sprintf(data + i, " [%d]", getpid());
i += sprintf(data + i, " %s ", log_level_str[level]);

Expand All @@ -76,7 +76,7 @@ void log_sys(const char *file,
size_t i = 0;
va_list ap;

i += timestamp(data);
i += timestamp(data, 32);
i += sprintf(data + i, " [%d]", getpid());
i += sprintf(data + i, " %s ", to_abort ? "[SYSFA]":"[SYSER]");

Expand All @@ -92,14 +92,19 @@ void log_sys(const char *file,
}
}

static int timestamp(char *data)
static int timestamp(char *data, size_t len)
{
struct tm *tm;
time_t rawtime;
struct timeval tv;

time(&rawtime);
tm = localtime(&rawtime);
asctime_r(tm, data);
gettimeofday(&tv, NULL);
time_t seconds = tv.tv_sec;

struct tm tm_time;

gmtime_r(&seconds, &tm_time);

return snprintf(data, len, "%4d%02d%02d %02d:%02d:%02d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);

return 24;
}
1 change: 0 additions & 1 deletion cycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void run_master_process()
exit(EXIT_FAILURE);

case 0:
CHECK(close(pid_fd));
run_single_process();
exit(EXIT_SUCCESS);

Expand Down
2 changes: 0 additions & 2 deletions cycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#ifndef FANCY_CYCLE_H
#define FANCY_CYCLE_H

extern int pid_fd;

void run_master_process();
void run_single_process();
void run_signal_process(int sig_no);
Expand Down
22 changes: 14 additions & 8 deletions fancy.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
#include "config.h"
#include "cycle.h"

int pid_fd;
static int open_and_lock_pid_file(const char *path);
static int write_and_lock_pid_file();
static int open_and_test_file(const char *path);
static int write_and_lock_file(const char *path);

int main(int argc, char **argv)
{
Expand Down Expand Up @@ -54,7 +53,7 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS);
}

if (open_and_lock_pid_file(FANCY_PID_FILE) == FCY_ERROR) {
if (open_and_test_file(FANCY_PID_FILE) == FCY_ERROR) {
fprintf(stderr, "fancy already running");
exit(EXIT_FAILURE);
}
Expand All @@ -73,7 +72,7 @@ int main(int argc, char **argv)
}
}

if (write_and_lock_pid_file() == FCY_ERROR) {
if (write_and_lock_file(FANCY_PID_FILE) == FCY_ERROR) {
LOG_ERROR("create pid file error");
exit(EXIT_FAILURE);
}
Expand All @@ -89,9 +88,9 @@ int main(int argc, char **argv)
}
}

static int open_and_lock_pid_file(const char *path)
static int open_and_test_file(const char *path)
{
pid_fd = open(path, O_RDWR | O_CREAT ,
int pid_fd = open(path, O_RDWR | O_CREAT ,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (pid_fd == -1) {
perror("open pid file error");
Expand All @@ -106,11 +105,18 @@ static int open_and_lock_pid_file(const char *path)
fprintf(stderr, "can not lock file %s", strerror(errno));
exit(EXIT_FAILURE);
}

CHECK(lockf(pid_fd, F_ULOCK, 0));
CHECK(close(pid_fd));

return FCY_OK;
}

static int write_and_lock_pid_file()
static int write_and_lock_file(const char *path)
{
int pid_fd = open(path, O_RDWR | O_CREAT ,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

if (lockf(pid_fd, F_TLOCK, 0) == -1) {
if (errno == EAGAIN || errno == EACCES) {
CHECK(close(pid_fd));
Expand Down
2 changes: 1 addition & 1 deletion fancy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ daemonize off;
master_process off;
worker_processes 3;

log_level off;
log_level debug;
log_path stdout; #/home/frank/ClionProjects/fancy/html/fancy.log;

events {
Expand Down

0 comments on commit cc3d8cc

Please sign in to comment.