daemon.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 linux;
  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 : longint;
  43. secs : longint;
  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. GetTime(hr,mn,sc,sc100);
  59. Writeln(flog,'Log created at ',hr:0,':',mn:0,':',sc:0);
  60. Close(fLog);
  61. End;
  62. Begin
  63. logname := 'daemon.log';
  64. secs := 10;
  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. sigprocmask(sig_block,ps1,nil);
  72. { setup the signal handlers }
  73. new(aOld);
  74. new(aHup);
  75. new(aTerm);
  76. aTerm^.handler.sh := @DoSig;
  77. aTerm^.sa_mask := 0;
  78. aTerm^.sa_flags := 0;
  79. {$ifndef BSD} {Linux'ism}
  80. aTerm^.sa_restorer := nil;
  81. {$endif}
  82. aHup^.handler.sh := @DoSig;
  83. aHup^.sa_mask := 0;
  84. aHup^.sa_flags := 0;
  85. {$ifndef BSD} {Linux'ism}
  86. aHup^.sa_restorer := nil;
  87. {$endif}
  88. SigAction(SIGTERM,aTerm,aOld);
  89. SigAction(SIGHUP,aHup,aOld);
  90. { daemonize }
  91. pid := Fork;
  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. GetTime(hr,mn,sc,sc100);
  118. Append(flog);
  119. Writeln(flog,'daemon code activated at ',hr:0,':',mn:0,':',sc:0);
  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. Select(0,nil,nil,nil,secs*1000);
  129. Until bTerm;
  130. End.
  131. {
  132. $Log$
  133. Revision 1.3 2002-09-07 15:06:35 peter
  134. * old logs removed and tabs fixed
  135. Revision 1.2 2002/02/25 12:56:43 marco
  136. * Fixed two linux'isms, and commited Jonas fix for the RTE 103
  137. }