daemon.pp 4.4 KB

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