chiark / gitweb /
debian/control: Include `prlimit' and `x86-model' in the metapackage.
[misc] / sema.1
CommitLineData
300a556d
MW
1.TH "sema" 1 "6 March 2016" "Mark Wooding" "Toys"
2.SH NAME
3sema \- operations on (SysV-style) semaphores
4.
5.SH SYNOPSIS
6.B sema
7.RB [ \-w
8.IR when ]
9.I subcmd
10.RI [ args ...]
11.PP
12Subcommands:
13'RS
14.br
15.B mkfile
16.RB [ \-x ]
17.RB [ \-m
18.IR mode ]
19.I name
20.br
21.B new
22.RB [ \-x ]
23.RB [ \-m
24.IR mode ]
25.I name
26.I value
27.br
28.B rm
29.RB [ \-f ]
30.I name
31.br
32.B get
33.I name
34.br
35.B set
36.I name
37.I value
38.br
39.B post
40.RB [ \-n
41.IR count ]
42.I name
43.br
44.B wait
45.RB [ \-n
46.IR count ]
47.I name
48.RI [ cmd
49.IR args ...]
50.RE
51.
52.SH DESCRIPTION
53The
54.B sema
55program performs simple operations
56on (System V-style) semaphores.
57It's not intended to be a utility for
58general-purpose hacking on
59existing semaphores,
60but rather a tool for doing synchronization
61between shell scripts or other simple programs.
62The two biggest limitations of
63.B sema
64are that it only ever acts on the first semaphore of a set,
65and it has no way to change the project-id
66(see
67.BR ftok (3)).
68.SS Common features
69Semaphores are identified by a
70.IR name .
71Currently, this is always a pathname,
72though additional ways of designating semaphore sets
73may be added in later versions,
74so an open-ended syntax is used:
75a
76.I name
77has the form
78.IP
79.RI [ type \c
80.BR : ] \c
81.I value
82.PP
83Currently defined
84.IR type s
85and the required
86.IR value s
87are as follows.
88.TP
89.B file
90The
91.I value
92is a pathname to a file in the filesystem.
93The file's inode number and other information
94are converted to an IPC key using
95.BR ftok (3)
96which is used to fetch a semaphore id using
97.BR semget (2).
98.PP
99The
100.I type
101may be omitted if it is
102.B file
103and the
104.I value
105does not contain a
106.RB ` : '
107before its first
108.RB ` / '
109(if any);
110as a result,
111most plain pathnames can be used directly.
112.PP
113The command-line options accepted before the subcommand name
114are as follows.
115.TP
116.B "\-h, \-\-help"
117Write a help message to standard output
118and exit successfully.
119.TP
120.B "\-v, \-\-version"
121Write a version number to standard output
122and exit successfully.
123.TP
124.BI "\-w, \-\-wait " duration
125Nearly all operations on SysV semaphores may need to block
126for an extended period of time.
127This is obvious for waiting, but, alas,
128semaphore initialization is poorly designed,
129and
130.I every
131operation which expects a properly initialized semaphore
132has little choice but to wait until the semaphore has been set up.
133.IP
134If
135.I duration
136is
137.B forever
138(the default)
139then
140.B sema
141will wait indefinitely until the thing it's waiting for happens.
142If
143.I duration
144is
145.B none
146or
147.B never
148then
149.B sema
150will exit immediately,
151with status 251,
152instead of waiting.
153Otherwise,
154.I
155duration
156should be a
157(possibly fractional \(en i.e., with a decimal point)
158number followed by an optional unit suffix:
159.RB ` s '
160for seconds (the default),
161.RB ` m '
162for minutes,
163.RB ` h '
164for hours, and
165.RB ` d '
166for days.
167.SS Exit status
168.IP 0
169Success.
170.IP 251
171The semaphore did not become available
172within the period
173.B sema
174was told to wait.
175.IP 252
176The semaphore was not properly initialized
177within the period
178.B sema
179was told to wait.
180.IP 253
181An error was detected in
182.BR sema 's
183command-line arguments.
184.IP 254
185A system error occurred.
186.IP Other
187Exit status from the program executed by the
188.B wait
189subcommand.
190.PP
191If
192.B sema
193detects an error,
194it writes a human-readable description of the problem
195to standard error and exits with one of the above status codes.
196The codes have been chosen so as not to conflict
197with those commonly used by other programs,
198so that errors from
199.B sema
200itself can be distinguished from problems encountered
201by the program executed by the
202.B wait
203subcommand,
204but the available space for error codes is small
205and conflicts are inevitable.
206.
207.SH SUBCOMMANDS
208.SS mkfile
209Create the file designated by the semaphore
210.I name
211argument.
212(An error is reported if the
213.I name
214does not designate a pathname.)
215.PP
216Options are as follows.
217.TP
218.BI "\-m, \-\-mode " mode
219Set the permissions for the new file.
220The
221.I mode
222should be a numeric file permission specification, in octal,
223as for
224.BR chmod (1).
225The default is to allow read and write according to the process umask.
226.TP
227.B "\-x, \-\-exclusive"
228Fail if the file already exists.
229.SS new
230Create a new semaphore set containing a single semaphore
231with the given
232.I name
233and initial
234.IR value .
235Options are as follows.
236.TP
237.BI "\-m, \-\-mode " mode
238Set the permissions for the new file.
239The
240.I mode
241should be a numeric file permission specification, in octal;
242the read and write bits determine whether the owner, group and others
243can read and change the semaphore;
244the execute bits are ignored.
245The default is to allow read and write according to the process umask.
246.TP
247.B "\-x, \-\-exclusive"
248Fail if a semaphore set with the given
249.I name
250already exists.
251.SS rm
252Delete the semaphore set with the given
253.IR name .
254.PP
255Options are as follows.
256.TP
257.B "\-f, \-\-force"
258Don't report an error if no semaphore set exists with the given name.
259.SS get
260Fetch the current value of the semaphore with the given
261.I name
262and
263write it, in decimal, to standard outout, followed by a newline.
264.PP
265Because this captures a snapshot of the state
266of an asynchronously changing value,
267this command is only really useful for diagnostic purposes
268or when the system is known to be quiescent.
269.PP
270There are no options.
271.SS set
272Set the value of the semaphore with the given
273.I name
274to
275.IR value .
276.PP
277Because this modifies a the state
278of an asynchronously changing value,
279this command is only really useful
280when the system is known to be quiescent.
281.PP
282There are no options.
283.SS post
284Atomically increment the value of the semaphore with the given
285.IR name .
286This operation can't block
287(though it may still be necessary to wait
288until the semaphore set is initialized).
289.PP
290Options are as follows.
291.TP
292.BI "\-n, \-\-count " count
293Adjust the semaphore value by
294.IR count ,
295which must be a positive integer,
296rather than 1.
297.SS wait
298Wait until the value of the semaphore with the given
299.I name
300is positive and then atomically decrement it.
301.PP
302If a
303.I command
304is provided
305(possibly with some
306.IR arguments )
307then execute it and then increment the semaphore value again
308when it finishes.
309The program is executed directly by
310.BR execvp (3).
311Restoring the semaphore value is reliable:
312it is done by the kernel,
313so there's no risk of some program crashing and
314leaving the semaphore in an inconsistent state;
315though obviously if the program gets stuck
316it will continue to hold the semaphore until it's killed.
317The semaphore is released as soon as the
318.I command
319exits;
320if it forked child processed,
321the semaphore will be released
322and the children will continue to run.
323.PP
324Options are as follows.
325.TP
326.BI "\-n, \-\-count " count
327Adjust the semaphore value by
328.IR count ,
329which must be a positive integer,
330rather than 1.
331.B sema
332will wait until semaphore value is at least
333.IR count ,
334and atomically decrease it by
335.IR count .
336If there is a
337.I command
338then
339.B sema
340arranges for the semaphore value to be increased by
341.I count
342when it exits.
343.
344.SH BUGS
345System V semaphores are remarkably awful.
346POSIX semaphores are superficially much better,
347but actually deficient in a number of ways.
348Most significantly for our purposes,
349there's no analogue of the
350.B SEM_UNDO
351feature,
352so to implement the feature of
353.B wait
354which holds the semaphore during the execution of a command
355.B sema
356would have to wait for it to finish;
357and if
358.B sema
359is killed in the meantime then nobody will fix the semaphore.
360Another important deficiency is that
361POSIX semaphores can only be adjusted a single step at a time,
362so the
363.B \-n
364feature of the
365.B wait
366and
367.B post
368commands can't be implemented satisfactorily.
369.SH AUTHOR
370Mark Wooding, <mdw@distorted.org.uk>