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.