Skip to content

Commit

Permalink
write working number into pid file
Browse files Browse the repository at this point in the history
fix: fifo file cannot open in non-blocking, use block write-only open
to be fix: sometimes the fifo still open fail
  • Loading branch information
yodalee committed Jan 2, 2014
1 parent c64c9d7 commit cd45999
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion sp_hw2r01942008/config/server.cfg
@@ -1,4 +1,4 @@
path=/home/yodalee/git/SP/sp_hw2r01942008/sdir
account_path=/home/yodalee/git/SP/sp_hw2r01942008/config/account
thread=3
run_path=/home/yodalee/git/SP/sp_hw2r01942008
run_path=/tmp
1 change: 1 addition & 0 deletions sp_hw2r01942008/include/csiebox_server.h
Expand Up @@ -33,6 +33,7 @@ typedef struct {
char account_path[PATH_MAX];
char run_path[PATH_MAX];
char fifofile[PATH_MAX];
char pidfile[PATH_MAX];
int thread_num;
int isDaemonize;
} arg;
Expand Down
2 changes: 1 addition & 1 deletion sp_hw2r01942008/include/thread.h
Expand Up @@ -60,7 +60,7 @@ void init_thread_pool(thread_pool** pool, int num);
/* dispatch a task to a thread */
int run_task(thread_pool* pool, task_thread_arg* arg);
/* check number of working thread */
int check_working(thread_pool *pool);
uint32_t check_working(thread_pool *pool);
void destroy_thread_pool(thread_pool** pool);

#ifdef __cplusplus
Expand Down
48 changes: 35 additions & 13 deletions sp_hw2r01942008/src/csiebox_server.c
Expand Up @@ -25,7 +25,7 @@ static csiebox_server *global_server;
static int parse_arg(csiebox_server* server, int argc, char** argv);
static void daemonize(csiebox_server *server);
static void writeFIFO(int signum);
static void deleteFIFO();
static void deleteFile();
static void prepare_arg(csiebox_server *server, int client_id);
static void handle_request(void *inarg, void *outarg);
static int get_account_info(
Expand Down Expand Up @@ -117,7 +117,7 @@ int csiebox_server_run(csiebox_server* server) {
//catch signal
struct sigaction intaction;
struct sigaction usraction;
intaction.sa_handler = &deleteFIFO;
intaction.sa_handler = &deleteFile;
sigaction(SIGINT, &intaction, NULL);
sigaction(SIGTERM, &intaction, NULL);
//create fifo
Expand Down Expand Up @@ -154,8 +154,12 @@ int csiebox_server_run(csiebox_server* server) {

switch (select(maxfd+1, &readset, NULL, NULL, &tv)) {
case -1:
fprintf(stderr, "select error\n");
return 1;
if (errno == EINTR) {
fprintf(stderr, "select get interrupt\n");
continue;
}
fprintf(stderr, "select error\n");
return 1;
case 0:
continue;
}
Expand Down Expand Up @@ -213,10 +217,10 @@ int csiebox_server_run(csiebox_server* server) {
usraction.sa_handler = SIG_DFL;
sigaction(SIGUSR1, &usraction, NULL);

//deleteFIFO
deleteFIFO();
//deleteFile
deleteFile();

return 1;
return 0;
}

void csiebox_server_destroy(csiebox_server** server) {
Expand Down Expand Up @@ -305,7 +309,6 @@ daemonize(csiebox_server *server)
int fd0, fd1, fd2;
FILE *pidfile;
pid_t pid;
char pidfilepath[PATH_MAX];
//clear file creation mask
umask(0);
//become session leader
Expand All @@ -331,11 +334,11 @@ daemonize(csiebox_server *server)
fd2 = dup(0);
//open syslog, write pid file, check file descriptor
openlog("csiebox_server", LOG_CONS, LOG_DAEMON);
strncpy(pidfilepath, server->arg.run_path, PATH_MAX);
strcat(pidfilepath, PIDFILE);
pidfile = fopen(pidfilepath, "w");
strncpy(server->arg.pidfile, server->arg.run_path, PATH_MAX);
strcat(server->arg.pidfile, PIDFILE);
pidfile = fopen(server->arg.pidfile, "w");
if (pidfile == NULL) {
syslog(LOG_ERR, "cannot open %s", pidfilepath);
syslog(LOG_ERR, "cannot open %s", server->arg.pidfile);
exit(1);
}
fprintf(pidfile, "%d\n", getpid());
Expand All @@ -350,16 +353,35 @@ daemonize(csiebox_server *server)
static void
writeFIFO(int signum)
{
int fd;
uint32_t working_thread = 0;
working_thread = htonl(check_working(global_server->pool));
//fprintf(stderr, "%s\n", global_server->arg.fifofile);
fd = open(global_server->arg.fifofile, O_WRONLY);
if (fd == -1) {
fprintf(stderr, "open fifo file fail\n");
return;
}
fprintf(stderr, "working thread num = %u\n", working_thread);
write(fd, &working_thread, sizeof(uint32_t));
close(fd);
}

static void
deleteFIFO()
deleteFile()
{
int ret;
ret = unlink(global_server->arg.fifofile);
if (ret == -1) {
fprintf(stderr, "delete fifo file error\n");
}
if (global_server->arg.isDaemonize) {
ret = unlink(global_server->arg.pidfile);
if (ret == -1) {
fprintf(stderr, "delete pid file error\n");
}
}
exit(0);
}

static void
Expand Down
5 changes: 3 additions & 2 deletions sp_hw2r01942008/src/thread.c
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "thread.h"

void
Expand Down Expand Up @@ -91,10 +92,10 @@ run_task(thread_pool *pool, task_thread_arg* arg)
return found;
}

int
uint32_t
check_working(thread_pool *pool)
{
int workingNum = 0;
uint32_t workingNum = 0;
int i;
pthread_mutex_lock(&(pool->dispatch_lock));
for (i = 0; i < pool->thread_num; ++i) {
Expand Down

0 comments on commit cd45999

Please sign in to comment.