Permalink
Browse files

Add "starting" and "started" to better reflect daemon state to the

caller.
  • Loading branch information...
xiali authored and jjzhang committed Jun 18, 2013
1 parent 3fd797e commit 569eda9587ecf00ea4a7ca49a6323b37a34112f5
Showing with 80 additions and 37 deletions.
  1. +80 −37 src/main.c
View
@@ -63,6 +63,12 @@ static int client_size = 0;
struct client *client = NULL;
struct pollfd *pollfd = NULL;
+typedef enum
+{
+ BOOTHD_STARTED=0,
+ BOOTHD_STARTING
+} BOOTH_DAEMON_STATE;
+
int poll_timeout = -1;
typedef enum {
@@ -450,7 +456,34 @@ static int setup_timer(void)
return timerlist_init();
}
-static int loop(void)
+static int write_daemon_state(int fd, int state)
+{
+ char buf[16];
+ int rv=0;
+
+ memset(buf, 0, sizeof(buf));
+ snprintf(buf, sizeof(buf), "%d %d", getpid(), state);
+
+ rv = lseek(fd, 0, SEEK_SET);
+ if (rv < 0) {
+ log_error("lseek set fd(%d) offset to 0 error, return(%d), message(%s)",
+ fd, rv, strerror(errno));
+ rv = -1;
+ return rv;
+ }
+
+ rv = write(fd, buf, strlen(buf));
+ if (rv <= 0) {
+ log_error("write to fd(%d) error, return(%d), message(%s)",
+ fd, rv, strerror(errno));
+ rv = -1;
+ return rv;
+ }
+
+ rv = 0;
+ return rv;
+}
+static int loop(int fd)
{
void (*workfn) (int ci);
void (*deadfn) (int ci);
@@ -473,6 +506,18 @@ static int loop(void)
goto fail;
client_add(rv, process_listener, NULL);
+ rv = write_daemon_state(fd, BOOTHD_STARTED);
+ if (rv != 0) {
+ log_error("write daemon state %d to lockfile error %s: %s",
+ BOOTHD_STARTED, cl.lockfile, strerror(errno));
+ goto fail;
+ }
+
+ if (cl.type == ARBITRATOR)
+ log_info("BOOTH arbitrator daemon started");
+ else if (cl.type == SITE)
+ log_info("BOOTH cluster site daemon started");
+
while (1) {
rv = poll(pollfd, client_maxi + 1, poll_timeout);
if (rv == -1 && errno == EINTR)
@@ -681,49 +726,47 @@ static int do_revoke(void)
return do_command(BOOTHC_CMD_REVOKE);
}
+
+
static int lockfile(void)
{
- char buf[16];
struct flock lock;
int fd, rv;
fd = open(cl.lockfile, O_CREAT|O_WRONLY, 0666);
if (fd < 0) {
log_error("lockfile open error %s: %s",
cl.lockfile, strerror(errno));
- return -1;
- }
-
- lock.l_type = F_WRLCK;
- lock.l_start = 0;
- lock.l_whence = SEEK_SET;
- lock.l_len = 0;
-
- rv = fcntl(fd, F_SETLK, &lock);
- if (rv < 0) {
- log_error("lockfile setlk error %s: %s",
- cl.lockfile, strerror(errno));
- goto fail;
- }
-
- rv = ftruncate(fd, 0);
- if (rv < 0) {
- log_error("lockfile truncate error %s: %s",
- cl.lockfile, strerror(errno));
- goto fail;
- }
-
- memset(buf, 0, sizeof(buf));
- snprintf(buf, sizeof(buf), "%d\n", getpid());
-
- rv = write(fd, buf, strlen(buf));
- if (rv <= 0) {
- log_error("lockfile write error %s: %s",
- cl.lockfile, strerror(errno));
- goto fail;
- }
+ return -1;
+ }
+
+ lock.l_type = F_WRLCK;
+ lock.l_start = 0;
+ lock.l_whence = SEEK_SET;
+ lock.l_len = 0;
+
+ rv = fcntl(fd, F_SETLK, &lock);
+ if (rv < 0) {
+ log_error("lockfile setlk error %s: %s",
+ cl.lockfile, strerror(errno));
+ goto fail;
+ }
+
+ rv = ftruncate(fd, 0);
+ if (rv < 0) {
+ log_error("lockfile truncate error %s: %s",
+ cl.lockfile, strerror(errno));
+ goto fail;
+ }
+
+ rv = write_daemon_state(fd, BOOTHD_STARTING);
+ if (rv != 0) {
+ log_error("write daemon state %d to lockfile error %s: %s",
+ BOOTHD_STARTING, cl.lockfile, strerror(errno));
+ goto fail;
+ }
- return fd;
+ return fd;
fail:
close(fd);
return -1;
@@ -986,14 +1029,14 @@ static int do_server(int type)
return fd;
if (type == ARBITRATOR)
- log_info("BOOTH arbitrator daemon started");
+ log_info("BOOTH arbitrator daemon is starting.");
else if (type == SITE)
- log_info("BOOTH cluster site daemon started");
+ log_info("BOOTH cluster site daemon is starting.");
set_scheduler();
set_oom_adj(-16);
- rv = loop();
+ rv = loop(fd);
out:
if (fd >= 0)

0 comments on commit 569eda9

Please sign in to comment.