daemon.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. {---------------------------------------------------------------------------
  2. CncWare
  3. (c) Copyright 2000
  4. ---------------------------------------------------------------------------
  5. Filename..: daemon.pp
  6. Programmer: Ken J. Wright
  7. Date......: 03/21/2000
  8. Purpose - Program to demonstrate construction of a Linux daemon.
  9. Usage:
  10. 1) Compile this program.
  11. 2) Run it. You will be immediately returned to a command prompt.
  12. 3) Issue the command: ps ax|grep daemon. This will show you the process
  13. id of the program "daemon" that you just ran.
  14. 4) Issue the command: tail -f daemon.log. This let's you watch the log file
  15. being filled with the message in the code below. Press Ctrl/c to break
  16. out of the tail command.
  17. 5) Issue the command: kill -HUP pid. pid is the process number you saw with
  18. the ps command above. You will see that a new log file has been created.
  19. 6) Issue the command: kill -TERM pid. This will stop the daemon. Issuing the
  20. ps command above, you will see that the daemon is no longer running.
  21. -------------------------------<< REVISIONS >>--------------------------------
  22. Ver | Date | Prog | Decription
  23. -------+------------+------+--------------------------------------------------
  24. 1.00 | 03/21/2000 | kjw | Initial release.
  25. 1.01 | 03/21/2000 | kjw | Forgot to close input, output, & stderr.
  26. ------------------------------------------------------------------------------
  27. }
  28. Program Daemon;
  29. uses SysUtils,BaseUnix;
  30. Var
  31. { vars for daemonizing }
  32. bHup,
  33. bTerm : boolean;
  34. fLog : text;
  35. logname : string;
  36. aOld,
  37. aTerm,
  38. aHup : pSigActionRec;
  39. ps1 : psigset;
  40. sSet : cardinal;
  41. pid : pid_t;
  42. secs : longint;
  43. zerosigs : sigset_t;
  44. hr,mn,sc,sc100 : word;
  45. { handle SIGHUP & SIGTERM }
  46. procedure DoSig(sig : longint);cdecl;
  47. begin
  48. case sig of
  49. SIGHUP : bHup := true;
  50. SIGTERM : bTerm := true;
  51. end;
  52. end;
  53. { open the log file }
  54. Procedure NewLog;
  55. Begin
  56. Assign(fLog,logname);
  57. Rewrite(fLog);
  58. Writeln(flog,'Log created at ',formatdatetime('hh:nn:ss',now));
  59. Close(fLog);
  60. End;
  61. Begin
  62. logname := 'daemon.log';
  63. secs := 10;
  64. fpsigemptyset(zerosigs);
  65. { set global daemon booleans }
  66. bHup := true; { to open log file }
  67. bTerm := false;
  68. { block all signals except -HUP & -TERM }
  69. sSet := $ffffbffe;
  70. ps1 := @sSet;
  71. fpsigprocmask(sig_block,ps1,nil);
  72. { setup the signal handlers }
  73. new(aOld);
  74. new(aHup);
  75. new(aTerm);
  76. aTerm^.sa_handler{.sh} := SigactionHandler(@DoSig);
  77. aTerm^.sa_mask := zerosigs;
  78. aTerm^.sa_flags := 0;
  79. {$ifndef BSD} {Linux'ism}
  80. aTerm^.sa_restorer := nil;
  81. {$endif}
  82. aHup^.sa_handler := SigactionHandler(@DoSig);
  83. aHup^.sa_mask := zerosigs;
  84. aHup^.sa_flags := 0;
  85. {$ifndef BSD} {Linux'ism}
  86. aHup^.sa_restorer := nil;
  87. {$endif}
  88. fpSigAction(SIGTERM,aTerm,aOld);
  89. fpSigAction(SIGHUP,aHup,aOld);
  90. { daemonize }
  91. pid := fpFork;
  92. Case pid of
  93. 0 : Begin { we are in the child }
  94. Close(input); { close standard in }
  95. Close(output); { close standard out }
  96. Assign(output,'/dev/null');
  97. ReWrite(output);
  98. Close(stderr); { close standard error }
  99. Assign(stderr,'/dev/null');
  100. ReWrite(stderr);
  101. End;
  102. -1 : secs := 0; { forking error, so run as non-daemon }
  103. Else Halt; { successful fork, so parent dies }
  104. End;
  105. { begin processing loop }
  106. Repeat
  107. If bHup Then Begin
  108. {$I-}
  109. Close(fLog);
  110. {$I+}
  111. IOResult;
  112. NewLog;
  113. bHup := false;
  114. End;
  115. {----------------------}
  116. { Do your daemon stuff }
  117. Append(flog);
  118. Writeln(flog,'daemon code activated at ',formatdatetime('hh:nn:ss',now));
  119. Close(fLog);
  120. { the following output goes to the bit bucket }
  121. Writeln('daemon code activated at ',hr:0,':',mn:0,':',sc:0);
  122. {----------------------}
  123. If bTerm Then
  124. BREAK
  125. Else
  126. { wait a while }
  127. fpSelect(0,nil,nil,nil,secs*1000);
  128. Until bTerm;
  129. End.