Besides my_func cannot perform in a child process and the daemon can be written to a log,
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String. H>
# include & lt; An FCNTL. H>
# include & lt; Sys/types. H>
# include & lt; Unistd. H>
# include & lt; Sys/wait. H>
# include & lt; Syslog. H>
# include & lt; Signal. H>
Void my_func (int sign_no)
{
If (sign_no==SIGINT)
{
Printf (" I have a get SIGINT % d \ n ", sign_no);
exit(0);
}
The else
{
Char buf1 [100].
If (sign_no==SIGQUIT)
{
Int fd1;
If ((fd1=open ("/TMP/daemon. The log, "O_CREAT | O_WRONLY | O_APPEND, 0600)) & lt; 0)
{
Syslog (LOG_ERR, "open");
The exit (1);
}
The else
{
Snprintf (buf1, sizeof (buf1), "% s", "If you want to quit, do try SIGINT \ n");
Write (fd1 buf1, strlen (buf1));
}
}
Int ret=alarm (15);
Pause ();
}
}
Int main ()
{
Sigset_t set;
Struct sigaction action, action1, action2;
Pid_t pid, pid2;
Int I, fd, x;
Char buf [100].
Openlog (" daemon ", LOG_PID, LOG_DAEMON);
Pid=fork ();
If (pid<0)
{
Printf (" Error fork \ n ");
The exit (1);
}
Else if (pid> 0)
{
exit(0);
}
Setsid ();
The chdir ("/");
Umask (0);
for(i=0; i
Close (I);
}
If (pid==0)
{
Pid2=fork ();
If (pid2 & lt; 0)
{
Printf (" Error fork \ n ");
}
Else if (pid2==0)
{
Syslog (LOG_INFO, "the Child is running!" );
Sigemptyset (& amp; Action. Sa_mask);
Action. Sa_handler=my_func;
Action. The sa_flags=0;
Sigaction (SIGINT, & amp; The action, NULL);
The sigaction (SIGQUIT, & amp; The action, NULL);
Pause ();
exit(0);
}
If (sigemptyset (& amp; The set) & lt; 0)
{
Perror (" sigemptyset ");
The exit (1);
}
If (sigaddset (& amp; The set, SIGTSTP) & lt; 0)
{
Perror (" sigaddset ");
The exit (1);
}
If (sigaddset (& amp; The set, SIGQUIT) & lt; 0)
{
Perror (" sigaddset ");
The exit (1);
}
If (sigaddset (& amp; The set, SIGINT) & lt; 0)
{
Perror (" sigaddset ");
The exit (1);
}
If (sigismember (& amp; The set, SIGINT))
{
Sigemptyset (& amp; Action1. Sa_mask);
Action1. Sa_handler=SIG_DFL;
Action1. Sa_flags=0;
Sigaction (SIGINT, & amp; Action1, NULL);
}
If (sigismember (& amp; The set, SIGQUIT))
{
Sigemptyset (& amp; Action2. Sa_mask);
Action2. Sa_handler=SIG_DFL;
Action2. Sa_flags=0;
The sigaction (SIGQUIT, & amp; Action2, NULL);
}
If (sigprocmask (SIG_BLOCK, & amp; Set to NULL) & lt; 0)
{
Perror (" sigprocmask ");
The exit (1);
}
The else
{
Printf (" Signal set was blocked, Press any key!" );
}
Waitpid (pid2, NULL, WNOHANG);
If (sigprocmask (SIG_UNBLOCK, & amp; Set to NULL) & lt; 0)
{
Perror (" sigprocmask ");
The exit (1);
}
The else
{
Printf (" Signal set is in unblock state \ n ");
X=1;
While (1)
{
If ((fd=open ("/TMP/daemon. The log, "O_CREAT | O_WRONLY | O_APPEND, 0600)) & lt; 0)
{
Syslog (LOG_ERR, "open");
The exit (1);
}
Snprintf (buf, sizeof (buf), "% s % d % s", "Daemon message," x, "\ n");
x++;
Write (fd, buf, strlen (buf));
Close (fd);
sleep(1);
}
}
while(1);
exit(0);
}
Closelog ();
}
CodePudding user response:
What's the matter with your code format that, see more headaches,CodePudding user response:
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String. H>
# include & lt; An FCNTL. H>
# include & lt; Sys/types. H>
# include & lt; Unistd. H>
# include & lt; Sys/wait. H>
# include & lt; Syslog. H>
# include & lt; Signal. H>
Void my_func (int sign_no)
{
If (sign_no==SIGINT)
{
Printf (" I have a get SIGINT % d \ n ", sign_no);
exit(0);
}
The else
{
Char buf1 [100].
If (sign_no==SIGQUIT)
{
Int fd1;
If ((fd1=open ("/TMP/daemon. The log, "O_CREAT | O_WRONLY | O_APPEND, 0600)) & lt; 0)
{
Syslog (LOG_ERR, "open");
The exit (1);
}
The else
{
Snprintf (buf1, sizeof (buf1), "% s", "If you want to quit, do try SIGINT \ n");
Write (fd1 buf1, strlen (buf1));
}
}
Int ret=alarm (15);
Pause ();
}
}
Int main ()
{
Sigset_t set;
Struct sigaction action, action1, action2;
Pid_t pid, pid2;
Int I, fd, x;
Char buf [100].
Openlog (" daemon ", LOG_PID, LOG_DAEMON);
Pid=fork ();
If (pid<0)
{
Printf (" Error fork \ n ");
The exit (1);
}
Else if (pid> 0)
{
exit(0);
}
Setsid ();
The chdir ("/");
Umask (0);
for(i=0; i{
Close (I);
}
If (pid==0)
{
Pid2=fork ();
If (pid2 & lt; 0)
{
Printf (" Error fork \ n ");
}
Else if (pid2==0)
{
Syslog (LOG_INFO, "the Child is running!" );
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull