home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / notebook / locking next >
Text File  |  1994-08-16  |  7KB  |  226 lines

  1. .DA "16 Aug 1994"
  2. .TL
  3. Locking in C News
  4. .Ix locking
  5. .SH
  6. Introduction
  7. .LP
  8. Several parts of
  9. C News
  10. need some way of locking parts of the news
  11. subsystem against concurrent execution.
  12. Various system-specific locking system calls exist,
  13. but none of them
  14. is truly portable,
  15. and most of them provide far more functionality than
  16. we need.
  17. .LP
  18. C News
  19. .Ix link
  20. locking uses the \fIlink\fR(2) system call and pre-agreed names.
  21. \fILink\fR has the necessary characteristic for safe locking:
  22. it is an atomic test-and-set operation.
  23. Furthermore,
  24. it exists in all Unixes.
  25. .SH
  26. Mechanism
  27. .LP
  28. .Ix lock names
  29. All locks are created in the NEWSCTL directory
  30. (see \fIConfiguration Mechanisms in C News\fR for where this directory
  31. is to be found and how programs can determine this)
  32. and have names starting with `LOCK'.
  33. To acquire a lock,
  34. .Ix lock acquiring
  35. .Ix LOCK
  36. .Ix files LOCK
  37. first create a temporary file in NEWSCTL with a name
  38. of the form `L.\fIn\fR', where \fIn\fR is your process id.
  39. Write your process id,
  40. in decimal ASCII,
  41. into this file.
  42. Then attempt to link the temporary file to `LOCK\fIx\fR',
  43. where \fIx\fR is chosen based
  44. on what sort of locking you wish to do.
  45. Existing lock names are:
  46. .IP LOCK \w'LOCKexplode'u+2n
  47. additions to article tree, changes to control files.
  48. Normally held mostly by \fIrelaynews\fR and
  49. administrative programs;
  50. grabbed briefly by \fIexpire\fR at the end of its run.
  51. .IP LOCKinput
  52. processing of spooled input.
  53. Held by \fInewsrun\fR to prevent multiple simultaneous \fInewsrun\fRs.
  54. .IP LOCKexplode
  55. changes to batch files.
  56. Held by the exploder when writing them,
  57. and grabbed briefly by the batcher when moving them aside.
  58. .IP LOCKoverview
  59. changes to overview files.
  60. .IP LOCKbatch
  61. preparation of batches.
  62. Held by the batcher (in the normal configuration)
  63. to prevent multiple simultaneous batching runs.
  64. .IP LOCKexpire
  65. rebuilding of history file, removal/archiving of articles.
  66. Held by \fIdoexpire\fR to prevent multiple simultaneous \fIexpire\fRs.
  67. .LP
  68. If the link fails,
  69. sleep briefly and try again.
  70. If it succeeds,
  71. remove the temporary file and proceed
  72. (some old code may defer removal of the temporary file
  73. until the lock itself is removed).
  74. Programs are expected to make a determined effort to remove lock files
  75. when they terminate,
  76. normally or as a result of signals
  77. (however, caution is indicated:
  78. race conditions should be resolved in favor of
  79. a small probability of leaving a dead lock around,
  80. \fInot\fR in favor of a small probability of removing
  81. a lock owned by someone else!).
  82. .LP
  83. Although there are various thorny questions associated with breaking
  84. locks by dead programs,
  85. .Ix lock clearing
  86. reboot is a time when surviving locks are
  87. definitely invalid.
  88. (Although there are problems even here if a networked group of systems
  89. are not rebooted as a unit.)
  90. For this and other reasons,
  91. a system running C News should execute
  92. .Ix newsboot
  93. .Ix /etc/rc
  94. NEWSCTL/bin/newsboot at reboot time (e.g. from \fI/etc/rc\fR).
  95. .SH
  96. C Interface
  97. .LP
  98. .Ix locking in C
  99. It's obviously desirable to package this mechanism up in a convenient form,
  100. to avoid having to reinvent it.
  101. It happens that the main lock,
  102. LOCK,
  103. is the only one that current C code needs\(emeverything else is done
  104. only from shell files.
  105. The C interface consists of three functions:
  106. .IP newslock \w'newsunlock'u+2n
  107. .Ix newslock
  108. acquire the main lock, and remember the fact.
  109. .IP newsunlock
  110. .Ix newsunlock
  111. release the main lock \fIif\fR I acquired it.
  112. .IP nolock
  113. .Ix nolock
  114. remember that I did \fInot\fR acquire the main lock
  115. (used by children of lock-holding processes).
  116. .LP
  117. In addition, there are two commonly-used utility functions
  118. that tie into the locking subsystem:
  119. .IP errunlock \w'errunlock'u+2n
  120. .Ix errunlock
  121. .Ix error
  122. .Ix warning
  123. invoke the \fIwarning\fR function
  124. to print an error message,
  125. call \fInewsunlock\fR,
  126. and do \fIexit(1)\fR.
  127. .IP nemalloc
  128. .Ix nemalloc
  129. .Ix malloc
  130. like \fImalloc\fR, except on failure, call \fIerrunlock\fR
  131. instead of returning NULL.
  132. .SH
  133. Shell Interface
  134. .LP
  135. .Ix locking in shell scripts
  136. The shell interface is a bit more problematic.
  137. Ideally, one would like to hide the locking machinery inside a pair
  138. of shell commands,
  139. analogous to the C functions,
  140. so that the locking mechanism could be changed easily.
  141. The trouble is that some kinds of mechanism require a single process
  142. to acquire the lock and then release it, and
  143. death of such a process automatically releases its lock(s).
  144. It's much more natural for a shell program to invoke separate ``lock''
  145. and ``unlock'' commands;
  146. an attempt at a single-process interface for locking in the C News
  147. Performance Release produced a rather odd shell programming style
  148. that did not lend itself well to complex cases.
  149. .LP
  150. The bottom line is:
  151. we're using a two-command model.
  152. If a single-process locking mechanism is used, it will be necessary to
  153. create a lock-holder background process as part of the locking operation,
  154. and kill it as part of the unlocking operation.
  155. The standard mechanism, using links, doesn't have this problem.
  156. .LP
  157. To acquire a lock, use the \fIlock\fR command,
  158. .Ix lock command
  159. found in NEWSBIN.
  160. The syntax is:
  161. .DS
  162. \fBlock\fR [\fB\-o\fR] [\fB\-v\fR] lockname pid [sleeptime]
  163. .DE
  164. \fILockname\fR is the name of the lock, e.g. ``LOCKinput''.
  165. \fIPid\fR is the process ID of the lock-holding process,
  166. normally written ``$$'' to have the shell supply it.
  167. .LP
  168. Normally, \fIlock\fR persists until it gets the lock,
  169. sleeping \fIsleeptime\fR (default 30) seconds after each failure.
  170. If the \fB\-o\fR option is used,
  171. \fIlock\fR tries only once, and returns exit status 0 for success
  172. and 1 for failure.
  173. If the \fB\-v\fR option is used,
  174. \fIlock\fR prints
  175. ``news system locked, waiting..''
  176. to standard output just before sleeping after a failure.
  177. .LP
  178. To release a lock, invoke \fIunlock\fR
  179. .Ix unlock command
  180. (found in NEWSBIN)
  181. with the lock name(s) as arguments.
  182. You must be the lock holder\(em\fIunlock\fR does not check this.
  183. .LP
  184. A lock that has been sitting around for some time is naturally an
  185. object of suspicion.
  186. It can be useful for a program to indicate that it is \fIdoing\fR
  187. something by updating the date on the lock.
  188. To update a lock, invoke \fItouchlock\fR
  189. .Ix touchlock command
  190. (found in NEWSBIN)
  191. with the lock name(s) as arguments.
  192. You must be the lock holder\(em\fItouchlock\fR does not check this.
  193. .LP
  194. As an additional feature, potentially of use in special situations,
  195. if the
  196. \fIlockname\fR given to \fIlock\fR, \fItouchlock\fR, or \fIunlock\fR
  197. contains ``/'',
  198. it is understood to be the pathname of the lock,
  199. and \fIlockname\fB.\fIpid\fR is used as the pathname of the temporary.
  200. This permits use of locks that do not reside in NEWSCTL.
  201. .LP
  202. .Ix Unix "System V"
  203. .Ix "System V" breakage
  204. .Ix ln
  205. The underlying implementation of our standard locking mechanism
  206. in shell has an additional problem in that System V has broken
  207. \fIln\fR(1) so that it removes a pre-existing destination file.
  208. C News
  209. therefore provides a pure,
  210. simple locking program under
  211. the name NEWSBIN/newslock
  212. .Ix newslock
  213. (if the recommendations in
  214. \fIDirectory Layout and PATH in C News\fR are followed,
  215. this will
  216. automatically be in the search path of shell programs).
  217. Syntax is:
  218. .DS
  219. \fBnewslock\fR tempfile lockfile
  220. .DE
  221. Exit status is 0 for success,
  222. 1 for failure,
  223. 2 for wrong number of arguments.
  224. No messages are printed for normal failure,
  225. so no redirection of output is needed.
  226.