Mon, 28 Jan 2013 17:37:18 +0100
Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.
1 ##
2 ## rc.pod -- RPM Auxiliary Tool (Manual Page)
3 ## Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
4 ##
5 ## This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
6 ## All rights reserved. Licenses which grant limited permission to use,
7 ## copy, modify and distribute this software are available from the
8 ## OpenPKG GmbH.
9 ##
10 ## THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
11 ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
13 ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
14 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
15 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
16 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
17 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
18 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
20 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 ## SUCH DAMAGE.
22 ##
24 =pod
26 =head1 NAME
28 B<@l_prefix@/bin/openpkg rc> - OpenPKG Run-Command Processor
30 =head1 SYNOPSIS
32 B<@l_prefix@/bin/openpkg rc> [B<-s>|B<--silent>] [B<-v>|B<--verbose>] [B<-d>|B<--debug>] [B<-k>|B<--keep>] [B<-D>|B<--define> I<name>=I<value>] I<package> I<command> [I<command> ...]
34 B<@l_prefix@/bin/openpkg rc> B<-p>|B<--print> I<package> I<command> [I<command> ...]
36 eval `B<@l_prefix@/bin/openpkg rc> B<-e>|B<--eval> I<package> I<command> [I<command> ...]`
38 B<@l_prefix@/bin/openpkg rc> B<-q>|B<--query> I<variable> [I<variable> ...]
40 B<@l_prefix@/bin/openpkg rc> B<-c>|B<--config>
42 B<@l_prefix@/bin/openpkg rc> B<-h>|B<--help>
44 =head1 DESCRIPTION
46 The B<@l_prefix@/bin/openpkg rc> program is the run-command (rc) processor
47 of the B<OpenPKG> instance C<@l_prefix@>. It allows querying the rc
48 configuration variables and the execution of rc command scripts of one
49 or more installed B<OpenPKG> packages. The implemented run-command
50 facility is partly modeled after the classical UNIX(tm) System V
51 run-command facility plus ideas taken from the FreeBSD and NetBSD
52 run-command facilities. It mainly merges the classical startup/shutdown
53 procedures with periodical procedures into a single approach.
55 =head1 USAGE
57 =over 4
59 =item B<@l_prefix@/bin/openpkg rc> [B<-s>|B<--silent>] [B<-v>|B<--verbose>] [B<-d>|B<--debug>] [B<-k>|B<--keep>] [B<-D>|B<--define> I<name>=I<value>] I<package> I<command> [I<command> ...]
61 B<Run-Command Execution.> This executes one or more specified
62 I<command>s in a particular I<package> or in all installed packages if
63 I<package> is "C<all>". Option B<--silent> can be used to explicitly
64 disable progress messages on C<stderr>. Option B<--verbose> can be used
65 to explicitly enable progress messages on C<stderr>. By default, B<rc>
66 automatically determines whether progress messages should be displayed
67 or not depending on whether C<stderr> is connected to a terminal device.
68 Option B<--keep> can be used for debugging purposes to keep the
69 temporary files were generated during internal processing. Option
70 B<--define> can be used to on-the-fly re-define a configuration variable
71 I<name> with I<value>.
73 =item B<@l_prefix@/bin/openpkg rc> B<-p>|B<--print> I<package> I<command> [I<command> ...]
75 B<Run-Command Printing.> This is like the run-command execution (see
76 above), but instead of immediately executing all involved individual
77 run-command scripts, they are concatenated (but with all configuration
78 parts reduced to a single configuration part) and printed to C<stdout>.
79 Use this for debugging or post-processing purposes.
81 =item eval `B<@l_prefix@/bin/openpkg rc> B<-e>|B<--eval> I<package> I<command> [I<command> ...]`
83 B<Run-Command Evaluation.> This is like the run-command execution
84 (see above), but the resulting exported shell environment variables
85 are output to a temporary file as a (Bourne-Shell or C-Shell syntax)
86 shell script, suitable for evaluation within the shell environment
87 of the caller. A one-line script is printed to C<stdout> which then
88 "sources" (and immediately removes) this temporary file. This is
89 slightly different from printing the temporary script directly to
90 C<stdout>, because not all shell implementations like to "B<eval>"
91 large multi-line scripts. Hence, use this for executing the "C<env>"
92 run-commands within the current shell.
94 =item B<@l_prefix@/bin/openpkg rc> B<-q>|B<--query> I<variable> [I<variable> ...]
96 B<Configuration Variable Querying.> This queries the effective values
97 (see B<RUN-COMMAND CONFIGURATION> section below) of one or more
98 run-command configuration I<variable>s. Use this within a shell script
99 to selectively query a particular variable.
101 =item B<@l_prefix@/bin/openpkg rc> B<-c>|B<--config>
103 B<Configuration Variable Summary.> This displays on C<stdout> a
104 three-column table showing the name, default and effective values of
105 all run-command configuration variables. If C<stdout> is connected to
106 terminal device, variables where the default and effective values differ
107 are shown in bold mode.
109 =item B<@l_prefix@/bin/openpkg rc> B<-h>|B<--help>
111 B<Requesting Help.> This just displays a short summary of
112 the usage for this program.
114 =back
116 =head1 FILES
118 =head2 RUN-COMMAND FILES (C<@l_prefix@/bin/openpkg rc.d/rc.*>)
120 The foundation of the B<OpenPKG> run-command facility are the individual
121 run-command files C<rc.*> of the installed packages. They are all
122 located in the directory C<@l_prefix@/bin/openpkg rc.d/> and are named
123 "C<rc.>I<package>". They consist of one or more sections, each starting
124 with a section header "C<%>I<name> [I<options>]" and following a GNU
125 Bash compatible shell script. There are three classes of sections:
127 =over 4
129 =item B<Special Sections>
131 There are 2 sections which have a special meaning to the B<rc> program
132 and cannot be used for different purposes.
134 =over 4
136 =item C<%config>
138 This section has to consist of run-command configuration
139 variable default settings in Bourne-Shell syntax only, i.e.,
140 it has to contain one or more lines, each of exactly the form
141 "I<package>C<_>I<variable>C<=>I<value>". Notice that in section
142 "C<%config>" of run-command file "C<rc.foo>" the variables all have
143 to be prefixed (by convention) with "C<foo_>". It is allowed that the
144 I<value>s reference other variables defined before in the same section
145 or from the "C<%config>" section of the bootstrap package "C<openpkg>".
146 Additionally, by convention all lines have to be indented by 4 spaces.
148 NOTICE: All sections of all run-command scripts see all C<%config>
149 sections of all packages.
151 =item C<%common>
153 This section can consist of an arbitrary shell script
154 which is automatically prepended to the shell scripts
155 of all other sections on execution. It is usually used
156 to define common and local variables and functions.
158 =back
160 =item B<Conventional Sections>
162 There are 9 sections which have no special meaning to the B<rc> program,
163 but are used in B<OpenPKG> by convention for standard purposes. Hence,
164 do not use them for arbitrary things instead.
166 Throughout these sections it is possible to specify the user to be
167 switched to before the code is executed.
169 A priority can be given for each section to control execution sequence
170 if "C<all>" packages are given. Higher priority leads to earlier
171 starting and later stopping. The priority is a number where low numbers
172 mean high priority (numerically sorted for starting). If omitted, the
173 priority defaults to 500.
175 Output generated on F<stdout> and F<stderr> is captured and normally
176 suppressed. In case the sections script exits with a non-zero return
177 value, the intercepted messages are consolidated, reformatted with a
178 surrounding ASCII-art border and printed to F<stderr>. If the section
179 is tagged with option B<-o>, F<stdout> messages are passed through
180 verbatim, even if the section script exists with a zero return value.
182 =over 4
184 =item C<%start> [B<-u> I<user>] [B<-p> I<priority>]
186 This section should start daemons or initialize components.
187 It is especially executed by B<OpenPKG> during system startup.
189 =item C<%stop> [B<-u> I<user>] [B<-p> I<priority>]
191 This section should stop daemons or cleaning up components. It is
192 especially executed by B<OpenPKG> during system shutdown and package
193 deinstallation.
195 =item C<%restart> [B<-u> I<user>] [B<-p> I<priority>]
197 This section should restart daemons.
198 It is especially executed by B<OpenPKG> during package upgrades.
200 =item C<%status -o> [B<-u> I<user>] [B<-p> I<priority>]
202 This section has to provide status information for a package
203 by printing to C<stdout> (hence the B<-o> option is always required),
204 in Bourne-Shell syntax, the definition of three variables:
206 =over 4
208 =item I<package>C<_enable>
210 Whether package is enabled, i.e., whether it accepts run-commands. This
211 variable just has to be printed, because is already set in current
212 script environment.
214 =item I<package>C<_usable>
216 Whether package is usable, i.e., whether it is already correctly
217 configured, etc. This variable has to be individually determined.
219 =item I<package>C<_active>
221 Whether package is active, i.e., whether it is already running.
222 This variable has to be individually determined.
224 =back
226 =item C<%monthly> [B<-u> I<user>] [B<-p> I<priority>]
228 =item C<%weekly> [B<-u> I<user>] [B<-p> I<priority>]
230 =item C<%hourly> [B<-u> I<user>] [B<-p> I<priority>]
232 =item C<%quarterly> [B<-u> I<user>] [B<-p> I<priority>]
234 These sections should perform periodical tasks for a package and
235 are executed on a monthly, weekly, hourly or quarterly basis.
237 =item C<%env>
239 This section is intended to export one or more environment variables
240 which are imported into the shell environment of the caller through the
241 B<--eval> command line option.
243 =back
245 =item B<Custom Sections>
247 All other sections are custom ones and can be fully individual to each
248 package. Some often seen sections are C<%reload> (just reload the
249 configuration without full stop and start procedure), C<%info> (output
250 arbitrary information about package), etc.
252 =back
254 =head2 RUN-COMMAND CONFIGURATION (C<@l_prefix@/bin/openpkg rc.conf>)
256 The run-command configuration variables defined in the "C<%config>"
257 sections of all installed packages can be overridden by the
258 administrator in the global file C<@l_prefix@/bin/openpkg rc.conf>. This file is
259 usually empty, but can be filled with Bourne-Shell compatible variable
260 assignment statements like "I<package>C<_>I<variable>=I<value>".
262 =head2 RUN-COMMAND FUNCTIONS (C<@l_prefix@/bin/openpkg rc.func>)
264 The file C<@l_prefix@/bin/openpkg rc.func> is prepended to the scripts of all
265 executed run-command sections and provides reusable functions.
266 Currently the following functions are defined:
268 =over 4
270 =item B<rcMsg> [B<-e>] [B<-w>] I<message>
272 Print a message to the output device of the run-command caller. The
273 message is printed to the captured F<stderr> (but are visible to
274 the caller of the run-command script only if the script fails). All
275 messages are prefixed with "C<rc:>". If option B<-e> is given, the
276 prefix is "C<rc:ERROR:>". If option B<-w> is given, the prefix is
277 "C<rc:WARNING:>".
279 =item B<rcPath> [B<-a>] [B<-r>] [B<-p>] [B<-e>] I<variable> I<dir> [I<dir> ...]
281 Add (option B<-a>) or remove (option B<-r>) one or more I<dir>ectories
282 to/from the colon-separated value of a I<var>able. By default, on
283 addition, the directory is appended to the end, if option B<-p> is used
284 the directory is prepended to the start. If option B<-e> is used, the
285 directories are added only if they exist on the underlying filesystem.
286 Use this function for conveniently manipulating C<PATH>, C<MANPATH>,
287 C<INFOPATH>, C<LD_LIBRARY_PATH> and similar variables.
289 =item B<rcTmp> [B<-i>] [B<-f>] [B<-n> I<name>] [B<-k>]
291 Convenience interface to secure temporary file handling. Option B<-i>
292 first has to be used to initialize a secure temporary directory (option
293 B<-k> later will kill this again). Under option B<-f> you then can get
294 reasonable temporary filenames under the created temporary directory. By
295 default, the temporary filename is just "C<tmp>", but can be specified
296 with option B<-n> to be I<name>.
298 =item B<rcService> I<package> I<status> I<value>
300 Convenience interface to checking the C<%status> variables
301 I<package>C<_>I<status> which have to be provided by the C<%config> (if
302 I<status> is "C<enable>" this is enough) and C<%status> (if I<status> is
303 "C<enable>", "C<usable>" and "C<active>"). The B<rcService> especially
304 caches the resolving of the variable values. Returns 0 if the resolved
305 value is I<value>, else returns 1.
307 =item B<rcVarIsYes> I<name>
309 Checks whether variable I<name> has a value of "C<yes>", "C<true>",
310 "C<on>", or "C<1>" (with arbitrary lower or upper case letters). Returns
311 0 if value is one of these positive ones, else returns 1.
313 =back
315 =head1 EXAMPLES
317 First, an example run-command script C<@l_prefix@/bin/openpkg rc.d/rc.foo> for a
318 fictional daemon package "C<foo>":
320 %config
321 foo_enable="${openpkg_rc_def}"
322 foo_listen="127.0.0.1"
323 foo_log_prolog="true"
324 foo_log_epilog="true"
325 foo_log_numfiles="10"
326 foo_log_minsize="1M"
327 foo_log_complevel="9"
329 %common
330 foo_homedir="@l_prefix@/share/foo"
331 foo_cfgfile="@l_prefix@/etc/foo/foo.cfg"
332 foo_pidfile="@l_prefix@/var/foo/foo.pid"
333 foo_logfile="@l_prefix@/var/foo/foo.log"
334 foo_signal () {
335 if [ -f ${foo_pidfile} ]; then
336 kill -$1 `cat ${foo_pidfile}`
337 return $?
338 else
339 return 1
340 fi
341 }
343 %status
344 foo_usable="no"
345 if [ ".`grep '<<PASSWORD>>' ${foo_cfgfile}`" = . ]; then
346 foo_usable="yes"
347 fi
348 foo_active="no"
349 if [ -f ${foo_pidfile} ]; then
350 foo_signal 0
351 if [ $? -eq 0 ]; then
352 foo_active="yes"
353 fi
354 fi
355 echo "foo_enable=${foo_enable}"
356 echo "foo_usable=${foo_usable}"
357 echo "foo_active=${foo_active}"
359 %start
360 rcService foo enable yes || exit 0
361 rcService foo active yes && exit 0
362 @l_prefix@/sbin/foo --listen ${foo_listen}
364 %stop
365 rcService foo enable yes || exit 0
366 rcService foo active yes || exit 0
367 foo_signal TERM
369 %restart
370 rcService foo enable yes || exit 0
371 rcService foo active yes || exit 0
372 rc foo stop start
374 %daily
375 rcService foo enable yes || exit 0
376 shtool rotate -f \
377 -n ${foo_log_numfiles} -s ${foo_log_minsize} -d \
378 -z ${foo_log_complevel} -o @l_rusr@ -g @l_rgrp@ -m 644 \
379 -P "${foo_log_prolog}" -E "${foo_log_epilog}" \
380 ${foo_logfile}
382 %env
383 if rcService foo enable yes; then
384 FOO_HOME="$foo_homedir"
385 export FOO_HOME
386 fi
388 Now, show all run-command configuration variables, their
389 default values and their effective values of package "C<foo>":
391 $ @l_prefix@/bin/openpkg rc --config | grep "^foo_"
393 Override the default value of a run-command configuration
394 variable C<foo_listen>:
396 $ echo 'foo_listen="192.168.0.1"' >>@l_prefix@/bin/openpkg rc.conf
398 Stop and start from scratch the package "C<foo>":
400 $ @l_prefix@/bin/openpkg rc foo stop start
402 Query the effective value of run-command configuration
403 variable C<foo_enable> in a script:
405 if [ ".`@l_prefix@/bin/openpkg rc -q foo_enable`" = .yes ]; then ...
407 Import all environment settings from all (including "C<foo>") installed
408 packages into the current Bourne-Shell environment:
410 $ eval `@l_prefix@/bin/openpkg rc --eval all env`
411 $ echo $FOO_HOME
413 =head1 SEE ALSO
415 B<OpenPKG> http://www.openpkg.org/
417 =head1 HISTORY
419 The B<OpenPKG> run-command facility consisting of the scripts C<rc>
420 and C<rc.func> were originally invented in November 2000 by Ralf S.
421 Engelschall for B<OpenPKG>. They were completely worked off from scratch
422 in July 2003 for B<OpenPKG 1.3>.
424 =head1 AUTHOR
426 Ralf S. Engelschall
427 rse@engelschall.com
428 www.engelschall.com
430 =cut