0

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!

12
  • Why do you need a timeout? Are you sure the client is sending something before the timeout? Commented Dec 31, 2021 at 17:06
  • Note that read() doesn't add a null terminator. %s format requires a null-terminated string. Commented Dec 31, 2021 at 17:07
  • Someone made the same comment about %s in your previous question. Commented Dec 31, 2021 at 17:09
  • 2
    You're closing stdout. How do you expect to see the output of printf? Commented Dec 31, 2021 at 17:48
  • 1
    BTW, there's no need to close stdin, stdout, and stderr explicitly, since they'll be closed in the for loop. Commented Dec 31, 2021 at 17:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.