Thread: Daemon Process

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Daemon Process

    Hey guys,
    I'm currently working on a project (a file monitorizer).
    I'm supposed to be reading files over and over and detect if anything has changed (and if anything has changed the daemon's should report it to my server).
    Right now I'm just trying to receive a comand through my daemon to my server through a named pipe in which the daemon process should be WR_ONLY and my server is RD_ONLY.

    My server code is :

    Code:
    int main(int argc, char *argv[])
    {
     int pid,erro,erro2,status,apipe,pipe,pipe2,flag=1,n=1,i=0;
     char **cmds;
     char *comand;
     char *buff;
     const char *comando;
     
     buff=malloc(100*sizeof(char));
     printf("\n\n*** Bem vindo ao FileMonitorizer ***\n\n"); 
     comand = buff; 
     while(n != -1 && strcmp(comand,"exit")!=0)
     {
      printf("\n\n*** A ler do pipe! ***\n\n");
      mkfifo("daemonfifo",0646);
      pipe2=open("daemonfifo",O_RDONLY); 
      n = read(pipe2,comand,BUF_TAM_MAX);
      if(n)
      {
       close(pipe2);
       comand[n] = '\0';
       comando = comand;
       printf("\n\n*** Comando recebido pelo daemon -> %s ***\n\n",comando);
       n=-1;
      }
     } 
       printf("\n\n*** A sair do FileMonitorizer! ***\n\n"); 
       return 1;
    }
    While my daemon code is :
    Code:
    int main(void) {
            
            /* Our process ID and Session ID */
            pid_t pid, sid;
            
            /* Fork off the parent process */
            pid = fork();
            if (pid < 0) {
                    exit(EXIT_FAILURE);
            }
            /* If we got a good PID, then
               we can exit the parent process. */
            if (pid > 0) {
                    exit(EXIT_SUCCESS);
            }
    
    
            /* Change the file mode mask */
            umask(0);
                    
            /* Open any logs here */        
                    
            /* Create a new SID for the child process */
            sid = setsid();
            if (sid < 0) {
                    /* Log the failure */
                    exit(EXIT_FAILURE);
            }
            
    
    
            
            /* Change the current working directory */
            if ((chdir("/")) < 0) {
                    /* Log the failure */
                    exit(EXIT_FAILURE);
            }
            
            /* Close out the standard file descriptors */
    
    
    	close(STDIN_FILENO);
            close(STDOUT_FILENO);
            close(STDERR_FILENO);
            
            /* Daemon-specific initialization goes here */
            
            /* The Big Loop */
            while (1) {
              int pipe2 = open("daemonfifo",O_WRONLY); 
    	  char *comando = "ola";
    	  write(pipe2,comando,strlen(comando));
    	  close(pipe2);
              sleep(30); /* wait 30 seconds */
            }
       exit(EXIT_SUCCESS);
    }
    Thing is, my server ain't receiving a thing while daemonfifo already exists because I created it on my server using mkfifo

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    How about you check the return value of all those functions you're calling, particularly open, read, write and malloc. Print out a useful error message if the function encounters any type of error (look into perror or strerror(errno)). For your daemon, you will need to log to a file, since you closed stdout and stderr. Also, read the man pages for those functions. Your sever code has a bug here:
    Code:
    n = read(pipe2,comand,BUF_TAM_MAX);
    if (n)
    What is n if read() encounters an error? Is that value of n logically true or false? Do you want to run your code in that case? There may be other bugs related to you not using a function or it's return value properly. I will let you read all the documentation and find any such cases yourself.

    You should have a loop for your read and write calls. They aren't guaranteed to write all the bytes requested in one go. They may only write one byte. Currently your code would lose some of the information.

    Why does your daemon open and close the fifo every time through the while loop? Why not just open it once before the loop and close it when you exit the program?

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Quote Originally Posted by anduril462 View Post
    How about you check the return value of all those functions you're calling, particularly open, read, write and malloc. Print out a useful error message if the function encounters any type of error (look into perror or strerror(errno)). For your daemon, you will need to log to a file, since you closed stdout and stderr. Also, read the man pages for those functions. Your sever code has a bug here:
    Code:
    n = read(pipe2,comand,BUF_TAM_MAX);
    if (n)
    What is n if read() encounters an error? Is that value of n logically true or false? Do you want to run your code in that case? There may be other bugs related to you not using a function or it's return value properly. I will let you read all the documentation and find any such cases yourself.

    You should have a loop for your read and write calls. They aren't guaranteed to write all the bytes requested in one go. They may only write one byte. Currently your code would lose some of the information.

    Why does your daemon open and close the fifo every time through the while loop? Why not just open it once before the loop and close it when you exit the program?
    Thanks for all the help.
    I managed to get my daemon working fine and sending the commands through a pipe to my server.c

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Daemon Process
    By DeanWinchester in forum C Programming
    Replies: 1
    Last Post: 05-25-2012, 03:54 AM
  2. ssh daemon question
    By Overworked_PhD in forum Linux Programming
    Replies: 4
    Last Post: 07-07-2009, 11:44 AM
  3. Daemon process debugger
    By karthigayan in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 12:28 AM
  4. Replies: 3
    Last Post: 02-20-2009, 12:46 AM
  5. Writting a daemon...
    By Unregistered in forum Linux Programming
    Replies: 2
    Last Post: 08-20-2002, 10:13 AM