Skip to content

Commit

Permalink
bugfix: linitctl start hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
niaow committed Dec 31, 2017
1 parent d5e4beb commit e01784c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/linitctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void cmd_start(int argc, char **argv, FILE *stream) {
nspace++;
}
}
stat++;
if(nspace < 2) {
fprintf(stderr, "[FATAL] Bad response: \"%s\"\n", resp);
exit(65);
Expand Down
23 changes: 20 additions & 3 deletions src/linitd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ void addSock(int fd);
void epoll_cycle();
int backlog = 64;

//epoll fd
int epollfd;

//forward declare structs
typedef uint32_t refcnt;
struct conn;
Expand Down Expand Up @@ -76,6 +79,8 @@ typedef enum{
service_status_stopped //service stopped
} servicestate;

void close_conn(struct conn *c);

//get a string representing a state
char *statestr(servicestate state) {
switch(state) {
Expand Down Expand Up @@ -130,6 +135,16 @@ enum{ notify_fail, notify_success, notify_gone } notify(struct conn *c, struct s
memcpy(ndat + c->tx.len, msg.dat, msg.len);
c->tx.dat = ndat;
c->tx.len += msg.len;
if(c->txoff && (c->tx.len > 0)) { //tx buffer was just populated
//enable write in epoll
struct epoll_event ev = {.events = EPOLLIN | EPOLLOUT, .data = {.ptr = &c->e}};
if(epoll_ctl(epollfd, EPOLL_CTL_MOD, c->fd, &ev) == -1) { //failed to enable - close and forget
fprintf(stderr, "[ERROR] Failed to enable writing on conn %d\n", c->fd);
close_conn(c);
} else {
c->txoff = false;
}
}
return notify_success;
}
//run on a state change to notify all associated conns of the change
Expand Down Expand Up @@ -218,8 +233,10 @@ void rmService(char* name) {
bool readBuf(struct buf *b, int fd) { //read data from fd into the buffer
byte dat[1024];
size_t n = read(fd, dat, 1024);
if(n < 1) {
if(n < 0) {
return false;
} else if(n == 0) {
return true;
}
byte* nd = arralloc(byte, n + b->len);
if(nd == NULL) {
Expand Down Expand Up @@ -264,8 +281,7 @@ void set_noblock(int fd) {
exit(65);
}

//epoll fd
int epollfd;

//epoll-associated data
void addSock(int fd) { //add a socket to epoll
printf("Sock: %d\n", fd);
Expand Down Expand Up @@ -631,6 +647,7 @@ void cmd_state(struct conn *c, char *args) {
servicestate s = service_status_null;
if(streq(states, "running")) {
s = service_status_running;
printf("[INFO] Finished %s\n", sname);
} else if(streq(states, "stopped")) {
s = service_status_stopped;
} else {
Expand Down

0 comments on commit e01784c

Please sign in to comment.