test with poll
This commit is contained in:
76
src/chored.c
76
src/chored.c
@@ -26,6 +26,8 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#include <poll.h>
|
||||
#include "chore.h"
|
||||
|
||||
int chored_command(int);
|
||||
@@ -154,7 +156,7 @@ int chored_init() {
|
||||
int chored_loop(jobs *global_jobs) {
|
||||
struct sockaddr_un sock_unix;
|
||||
|
||||
if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == 1) {
|
||||
if((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == 1) {
|
||||
printf("can't open socket\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -178,16 +180,18 @@ int chored_loop(jobs *global_jobs) {
|
||||
int acc = 0;
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
|
||||
for(;;) {
|
||||
if ((acc = accept(sock, (struct sockaddr *)&addr, &addr_len)) < 0) {
|
||||
printf("lost connection\n");
|
||||
return 1;
|
||||
};
|
||||
printf("received connection\n");
|
||||
chored_command(acc);
|
||||
close(acc);
|
||||
int status;
|
||||
acc = accept4(sock, (struct sockaddr *)&addr, &addr_len, SOCK_NONBLOCK);
|
||||
if (acc>=0) {
|
||||
printf("received connection %d\n", acc);
|
||||
chored_command(acc);
|
||||
close(acc);
|
||||
}
|
||||
waitpid(WAIT_MYPGRP, &status, WNOHANG);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -196,18 +200,26 @@ int chored_terminate(jobs *global_jobs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int chored_spawn() {
|
||||
switch(fork()) {
|
||||
case 0:
|
||||
printf("child \n");
|
||||
break;
|
||||
int tjob(char *command) {
|
||||
printf("child: %s\n", command);
|
||||
_exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case -1:
|
||||
int chored_spawn(char *command) {
|
||||
pid_t pid;
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
printf("can't fork \n");
|
||||
|
||||
default:
|
||||
printf("parent\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
tjob(command);
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("parent\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -222,8 +234,34 @@ int check_command(char *command, ssize_t len) {
|
||||
int chored_command(int acc) {
|
||||
char buffer[255];
|
||||
ssize_t received;
|
||||
|
||||
struct pollfd pfd[1];
|
||||
int nready;
|
||||
pfd[0].fd = acc;
|
||||
pfd[0].events = POLLIN;
|
||||
nready = poll(pfd, 1, 60*1000);
|
||||
if (nready == -1) {
|
||||
printf("poll error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nready == 0) {
|
||||
printf("poll timeout\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
received = read(acc, &buffer, 255);
|
||||
if (received<0) {
|
||||
printf("read error %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("received (%zd): %s", received, buffer);
|
||||
check_command(buffer, received);
|
||||
if ((check_command(buffer, received)) < 0) {
|
||||
printf("bad command\n");
|
||||
return 1;
|
||||
}
|
||||
chored_spawn(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user