//Daemon-Read file#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<signal.h>#include"mylog.h"//Monitoring PipelinevoidListenfifo () {//File Size intlen=0; intFd2=0; Charbuf[ -]={0}; //Open Pipe file (pipe file is generally read with system functions) intFd=open ("/home/test/1/fifo1", o_rdonly); if(fd==-1) {Writelog ("/home/test/1/mylog.txt","Open the Fifio1 failed!"); //printf ("Open the Fifio1 failed! Error message:%s\n ", Strerror (errno)); return; } Len=read (Fd,buf,sizeof(BUF)); if(len<=0) {Writelog ("/home/test/1/mylog.txt","Read the FIFO1 is failed!"); //printf ("Read the FIFO1 is failed! Error code is%d\n ", Len); return; } if(Buf[strlen (BUF)-1]=='\ n') {Buf[strlen (BUF)-1]=0; } writelog ("/home/test/1/mylog.txt", BUF); //Close the Fifo1Close (FD); //turn off standard outputClose (Stdout_fileno); //specify file as standard output-output file can only be writtenFd2=open (buf,o_wronly); memset (BUF,0,sizeof(BUF)); sprintf (BUF,"fd2=%d\n", FD); Writelog ("/home/test/1/mylog.txt", buf);}//Message processing mechanismvoidCatch_signal (intSign ) { Switch(sign) { CaseSigint:listenfifo (); Break; }}//Read the fileintReadmyfile (Const Char*path) { if(path==NULL) {printf ("Param is isn't allow NULL! \ n"); return-1; } //pipe files do not use C library functions Charbuf[ -]={0}; intFd=open (path,o_rdonly); if(fd==-1) {printf ("Open the FIFO failed! Error message:%s\n", Strerror (errno)); return-1; } /*The read () function blocks the process when it reads the pipe file, and when the other end of the pipe shuts down normally, the Read function returns 0, and an abnormal shutdown returns 1*/ while(Read (Fd,buf,sizeof(BUF)) >0) {printf ("%s", BUF); memset (BUF,0,sizeof(BUF)); } close (FD); return 0;}//Creating DaemonsintSetdaemon () {pid_t PID=Fork (); if(pid==-1) {printf ("Fork Error! Error message:%s\n", Strerror (errno)); Exit (0); } if(pid==0) { //Child ProcessSetsid (); ChDir ("/"); Umask (0); /*Close (Stdin_fileno); Close (Stderr_fileno); */ } if(pid>0) {exit (0); } return 0;}//Capturing MessagesintMysignnal (intSignvoid(*func) (int)){ structsigaction act,oact; Act.sa_handler=func; Sigemptyset (&act.sa_mask); Act.sa_flags=0; returnSigaction (sign,&act,&oact);}intMainintArgChar*args[]) { if(arg<2) {printf ("Please enter a parameter! \ n"); return-1; } Setdaemon (); //Register Messagemysignnal (sigint,catch_signal); //Readmyfile (args[1]); while(1) {printf ("I ' m living!\n"); Sleep (1); } return 0;}
. suffixes:.c. OCC=Gccsrcs=tec01.cobjs=$ (srcs:.c=. O) EXEC=tecdstart:$ (OBJS) -L.-lmylog-o $ (EXEC) $ (objs )"------OK----------". C.O: -wall-g-o [email protected]-C $< clean: -F $ ( Objs)F $ (EXEC)
Summary:
This program write code 20 minutes to complete, but debugging took 40 minutes, I send a signal to the daemon, the daemon does not output content, where I made two errors
--the first error; When I created the daemon, I turned the standard output, standard input, standard error, all three file descriptors closed, so that the file descriptor I opened in Listenfifo is actually a standard input (i.e. fd=0)
I observed the fd=0 through the log, which reminds me that my file descriptor may have all been closed, and in this program I opened 2 file descriptors, one for the pipeline file, one for the screen output file.
--A second error
The standard output should be a write-only file, but I use o_rdonly in the open () function, so there is no result.
Linux Daemon II (Activation daemon)