Implement request type ReqExit

This commit is contained in:
Cheer Xiao 2013-06-18 18:39:28 +08:00
parent a6b867455f
commit 1a1b4e4593
2 changed files with 55 additions and 36 deletions

View File

@ -22,43 +22,49 @@ void external(ReqCmd *cmd) {
void worker() {
char *err;
ReqCmd *cmd = (ReqCmd*)RecvReq(&err);
if (!cmd) {
Req *req = RecvReq(&err);
if (!req) {
fprintf(res, "%s\n", err);
return;
}
pid_t pid;
check_1("fork", pid = fork());
if (pid == 0) {
fclose(res);
external(cmd);
} else {
fprintf(res, "spawned external: pid = %d\n", pid);
while (1) {
int status;
pid_t ret = waitpid(pid, &status, 0);
if (ret == -1 && errno == ECHILD) {
break;
}
check_1("wait", ret);
printf("external %d ", pid);
if (WIFEXITED(status)) {
printf("terminated: %d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("terminated by signal: %d\n", WTERMSIG(status));
} else if (WCOREDUMP(status)) {
printf("core dumped\n");
} else if (WIFSTOPPED(status)) {
printf("stopped by signal: %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
} else {
printf("changed to some state das doesn't know\n");
ReqType type = req->type;
if (type == REQ_TYPE_COMMAND) {
pid_t pid;
check_1("fork", pid = fork());
if (pid == 0) {
fclose(res);
external((ReqCmd*)req);
} else {
fprintf(res, "spawned external: pid = %d\n", pid);
while (1) {
int status;
pid_t ret = waitpid(pid, &status, 0);
if (ret == -1 && errno == ECHILD) {
break;
}
check_1("wait", ret);
printf("external %d ", pid);
if (WIFEXITED(status)) {
printf("terminated: %d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("terminated by signal: %d\n", WTERMSIG(status));
} else if (WCOREDUMP(status)) {
printf("core dumped\n");
} else if (WIFSTOPPED(status)) {
printf("stopped by signal: %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
} else {
printf("changed to some state das doesn't know\n");
}
}
}
} else if (type == REQ_TYPE_EXIT) {
exiting = 1;
}
FreeReq((Req*)cmd);
FreeReq(req);
}
int main(int argc, char **argv) {

View File

@ -32,8 +32,19 @@ void freeReqCmd(ReqCmd *p) {
free(p);
}
void freeReqExit(ReqExit *p) {
free(p);
}
void FreeReq(Req *p) {
freeReqCmd((ReqCmd*)p);
switch (p->type) {
case REQ_TYPE_COMMAND:
freeReqCmd((ReqCmd*)p);
break;
case REQ_TYPE_EXIT:
freeReqExit((ReqExit*)p);
break;
}
}
void printReqCmd(ReqCmd *cmd) {
@ -129,6 +140,12 @@ ReqCmd *loadReqCmd(json_t *root) {
}
}
ReqExit *newReqExit() {
ReqExit *r = alloc(ReqExit, 1);
r->type = REQ_TYPE_EXIT;
return r;
}
Req *loadReq(json_t *root) {
char *type;
json_t *data;
@ -153,14 +170,10 @@ char *readReq() {
return buf;
}
extern int exiting;
Req *RecvReq(char **err) {
char *buf = readReq();
if (!buf) {
exiting = 1;
*err = strdup("exiting");
return 0;
return (Req*)newReqExit();
}
json_t *root;