I've already asked a similar question a few days ago. Basically I have to make a daemon-client communicate via named tubes. The client part is done and sends messages correctly, but the daemon part seems to not be receiving anything... Here's the code:
void makeDaemon(){
int i,fd0,fd1,fd2;
pid_t pid;
struct sigaction sa;
struct rlimit rl;
umask(0);
if(getrlimit(RLIMIT_NOFILE, &rl)<0) exit(EXIT_FAILURE);
if((pid=fork())<0) exit(EXIT_FAILURE);
if(pid>0) exit(EXIT_SUCCESS);
setsid();
sa.sa_handler=SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags=0;
if(sigaction(SIGHUP, &sa, NULL)<0) exit(EXIT_FAILURE);
if((pid=fork())<0) exit(EXIT_FAILURE);
if(pid>0) exit(EXIT_SUCCESS);
//if(chdir("/")<0) exit(EXIT_FAILURE);
if(rl.rlim_max=RLIM_INFINITY) rl.rlim_max=1024;
for(i=0;i< rl.rlim_max; i++) close(i);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
//sigaction(SIGUSR1,&sac,NULL);
}
int main(){
makeDaemon();
char* path="./run/pipes/saturnd-request-pipe";
char* path2="./run/pipes/saturnd-reply-pipe";
//Creation d'un pipe au cas où il n'existe pas
//mkfireply_pipe(path,0666);
int timeout=5000;
struct pollfd pf[1];
int request_pipe;
while(1){
request_pipe=open(path,O_RDONLY);
if(request_pipe==-1){
perror("Erreur d'ouverture du pipe");
exit(EXIT_FAILURE);
}
pf[0].fd=request_pipe;
pf[0].events=POLLIN;
int p=poll(pf,1,timeout);
if(pf[0].revents & POLLIN){
char tmp1[2];
ssize_t s=read(request_pipe,tmp1,2);
if(s==-1) exit(EXIT_FAILURE);
printf("%s\n",tmp1);
int i=1;
}else{
printf("Timed out\n");
break;
}
return 0;
}
}
My question is, should I maybe make a handler containing this while loop so that the daemon can receive a signal? As of right now, it just times out and dies. We cannot use sockets, and everything is done via these two names pipes. Any suggestion would be appreciated. Thanks!
read()doesn't add a null terminator.%sformat requires a null-terminated string.%sin your previous question.forloop.