0

I'm trying to implement a server and client in C using a daemon process and fifo (named pipes). My client and server successfully creates a connection between each other. However, server only makes the first read (which is reading client's pid) and then kills itself.

First I run the server program and then check with the command ps -xj | grep myserver if it successfully creates a daemon process. Everything looks good at this point. Then I debug the client program and check the server's daemon process with the command ps -xj | grep myserver after each write of client. Also I check the server's log file after each write of client.

After client connects to the fifo, I can see DAEMON STARTED in the server logs file. Then I check the server's log file after each write of client and I don't see anything until client finishes all the writes. After client finishes all the writes again I check the server's log file and I can only see the pid of client but I can't see the other read results of servers in its log file.

For example, I would expect syslog(LOG_NOTICE, "Directory name: %s\n", dirName); to write the dirName to log file of the server. Also after client makes all the writes I check the server's daemon process with the command ps -xj | grep myserver and I see that daemon process is killed. I can't find a reason for why server kills the daemon process after client makes all its writes and doesn't read the client's writes after client's first write. I would appreciate any help. Thanks!

This is the code I wrote so far:

Server:

createDaemon(argv[1]);

umask(0);           
mkfifo(serverFifoPath, 0666)  
int serverFd = open(serverFifoPath, O_RDONLY);
int dummyFd = open(serverFifoPath, O_WRONLY);
signal(SIGPIPE, SIG_IGN)

for(;;)
{
    syslog(LOG_NOTICE, "DAEMON STARTED");

    //READ
    pid_t clientPID = 0;
    read(serverFd, &clientPID, sizeof(clientPID))

    int dirNameSize;
    read(serverFd, &dirNameSize, sizeof(dirNameSize))
    char *dirName = (char *)malloc(dirNameSize);
    read(serverFd, &dirName, dirNameSize)

    //Another integer and string read here
    syslog(LOG_NOTICE, "Pid: %d\n", clientPID);
    syslog(LOG_NOTICE, "Directory name: %s\n", dirName);

    int clientFd = open(clientFifo, O_WRONLY);
    //Server writes to fifo here
    close(clientFd);                
}

Client:

mkfifo(clientFifo, 0666);
int serverFd = open(serverFifoPath, O_WRONLY);

write(serverFd, &clientPID, sizeof(clientPID));

write(serverFd, &dirNameSize, sizeof(dirNameSize));
write(serverFd, &(argv[1]), dirNameSize);
//Another integer and string write here

int clientFd = open(clientFifo, O_RDONLY);
unlink(clientFifo);

//Client reads here

And I got my createDaemon function from this link: Creating a Daemon in Linux

Community
  • 1
  • 1
vontarro
  • 319
  • 1
  • 2
  • 11
  • I figured it out I was passing wrong string parameters to read and write functions. Instead of `&dirName`, it should be `dirName` and instead of `&(argv[1])`, it should be `(argv[1])`. Now I got `Bad file descriptor` error from the function `close(clientFd)` in the server program. Firstly I was exiting the program but now I continue the execution after writing the error to the log file of server and daemon isn't killed. – vontarro Mar 30 '16 at 07:26
  • Hi vontarro can you please share code of daemon? thanks – MD05 Mar 25 '17 at 01:10

0 Answers0