Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include<unistd.h>
  2. #include<syslog.h>
  3. #include<signal.h>
  4. #include<sys/types.h>
  5. #include<sys/stat.h>
  6.  
  7. void handle_alarm( int sig ) {
  8.  
  9.   syslog( LOG_INFO, "ALARM FROM MY DAEMON");
  10. }
  11.  
  12. int main( int argc, char* argv[] ) {
  13.         pid_t pid, sid;
  14.        
  15.         // Forkataan prosessi daemonille
  16.         pid = fork();
  17.         if (pid < 0) {
  18.                return -1;
  19.         }
  20.         // Prosessi luotu, voidaan poistua parent -prosessista
  21.         if (pid > 0) {
  22.                 return 0;
  23.         }
  24.  
  25.         umask(0);      
  26.        
  27.         // Luodaan uusi sessio
  28.         sid = setsid();
  29.         if (sid < 0) {
  30.                 return -1;
  31.         }
  32.        
  33.         // Vaihdetaan hakemisto root:iin
  34.         if ((chdir("/")) < 0) {
  35.                 return -1;
  36.         }
  37.        
  38.         // Suljetaan standardi streamit
  39.         close(STDIN_FILENO);
  40.         close(STDOUT_FILENO);
  41.         close(STDERR_FILENO);
  42.        
  43.        
  44.         // Itse daemonin koodi...
  45.         signal( SIGALRM, handle_alarm );
  46.         openlog( "MYDAEMON",  LOG_PID, LOG_DAEMON);
  47.     alarm( 10 );
  48.         while (true) {
  49.       syslog( LOG_INFO, "DAEMON WOKE UP TO DO SOME THINGS...");
  50.       sleep( 10 );
  51.         }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement