Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libcom_err / doc / com_err.texinfo
1 \input texinfo @c -*-texinfo-*-
2
3 @c $FreeBSD: src/lib/libcom_err/doc/com_err.texinfo,v 1.4 1999/09/06 07:41:34 peter Exp $
4 @c $DragonFly: src/lib/libcom_err/doc/com_err.texinfo,v 1.2 2003/06/17 04:26:48 dillon Exp $
5
6 @c Note that although this source file is in texinfo format (more
7 @c or less), it is not yet suitable for turning into an ``info''
8 @c file.  Sorry, maybe next time.
9 @c
10 @c In order to produce hardcopy documentation from a texinfo file,
11 @c run ``tex com_err.texinfo'' which will load in texinfo.tex,
12 @c provided in this distribution.  (texinfo.tex is from the Free
13 @c Software Foundation, and is under different copyright restrictions
14 @c from the rest of this package.)
15
16 @ifinfo
17 @barfo
18 @end ifinfo
19
20 @iftex
21 @tolerance 10000
22
23 @c Mutate section headers...
24 @begingroup
25   @catcode\11#=6
26   @gdef@secheading#1#2#3{@secheadingi {#3@enspace #1}}
27 @endgroup
28 @end iftex
29
30 @c %**start of header (This is for running Texinfo on a region.)
31 @setfilename com_err
32 @settitle A Common Error Description Library for UNIX
33 @c %**end of header (This is for running Texinfo on a region.)
34
35 @ifinfo
36 This file documents the use of the Common Error Description library.
37
38 Copyright (C) 1987, 1988 Student Information Processing Board of the
39 Massachusetts Institute of Technology.
40
41 Permission to use, copy, modify, and distribute this software and its
42 documentation for any purpose and without fee is hereby granted, provided
43 that the above copyright notice appear in all copies and that both that
44 copyright notice and this permission notice appear in supporting
45 documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
46 used in advertising or publicity pertaining to distribution of the software
47 without specific, written prior permission.  M.I.T. and the M.I.T. S.I.P.B.
48 make no representations about the suitability of this software for any
49 purpose.  It is provided "as is" without express or implied warranty.
50
51 Note that the file texinfo.tex, provided with this distribution, is from
52 the Free Software Foundation, and is under different copyright restrictions
53 from the remainder of this package.
54
55 @end ifinfo
56
57 @ignore
58 Permission is granted to process this file through Tex and print the
59 results, provided the printed document carries copying permission
60 notice identical to this one except for the removal of this paragraph
61 (this paragraph not being relevant to the printed manual).
62
63 @end ignore
64
65 @setchapternewpage odd
66
67 @titlepage
68 @center @titlefont{A Common Error Description}
69 @center @titlefont{Library for UNIX}
70 @sp 2
71 @center Ken Raeburn
72 @center Bill Sommerfeld
73 @sp 1
74 @center MIT Student Information Processing Board
75 @sp 3
76 @center last updated 1 January 1989
77 @center for version 1.2
78 @center ***DRAFT COPY ONLY***
79
80 @vskip 2in
81
82 @center @b{Abstract}
83
84 UNIX has always had a clean and simple system call interface, with a
85 standard set of error codes passed between the kernel and user
86 programs.  Unfortunately, the same cannot be said of many of the
87 libraries layered on top of the primitives provided by the kernel.
88 Typically, each one has used a different style of indicating errors to
89 their callers, leading to a total hodgepodge of error handling, and
90 considerable amounts of work for the programmer.  This paper describes
91 a library and associated utilities which allows a more uniform way for
92 libraries to return errors to their callers, and for programs to
93 describe errors and exceptional conditions to their users.
94
95 @page
96 @vskip 0pt plus 1filll
97
98 Copyright @copyright{} 1987, 1988 by the Student Information Processing
99 Board of the Massachusetts Institute of Technology.
100
101 Permission to use, copy, modify, and distribute this software and its
102 documentation for any purpose and without fee is hereby granted, provided
103 that the above copyright notice appear in all copies and that both that
104 copyright notice and this permission notice appear in supporting
105 documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
106 used in advertising or publicity pertaining to distribution of the software
107 without specific, written prior permission.  M.I.T. and the M.I.T. S.I.P.B.
108 make no representations about the suitability of this software for any
109 purpose.  It is provided "as is" without express or implied warranty.
110
111 Note that the file texinfo.tex, provided with this distribution, is from
112 the Free Software Foundation, and is under different copyright restrictions
113 from the remainder of this package.
114
115 @end titlepage
116
117 @ifinfo
118 @node Top, Why com_err?, (dir), (dir)
119 @comment  node-name,  next,  previous,  up
120 @top General Introduction
121
122 @menu
123 * Why com_err?::                What is all this for?
124 * Error codes::                 What's an error code, anyway?
125 * Error table source file::     How to describe an error table.
126 * The error-table compiler::    How to compile the table.
127 * Run-time support routines::   How to use from within your program.
128 * Coding Conventions::          Stylistic issues.
129 * Building and Installation::   How to build and install.
130 * Bug Reports::                 You have found a bug?  Report it.
131 * Acknowledgements::            Whom to thank...
132
133 @end menu
134
135 @end ifinfo
136
137 @page
138
139 @ifinfo
140 @node Why com_err?, Error codes, Top, (dir)
141 @comment  node-name,  next,  previous,  up
142 @end ifinfo
143
144 @section Why com_err?
145
146 In building application software packages, a programmer often has to
147 deal with a number of libraries, each of which can use a different
148 error-reporting mechanism.  Sometimes one of two values is returned,
149 indicating simply SUCCESS or FAILURE, with no description of errors
150 encountered.  Sometimes it is an index into a table of text strings,
151 where the name of the table used is dependent on the library being
152 used when the error is generated; since each table starts numbering at
153 0 or 1, additional information as to the source of the error code is
154 needed to determine which table to look at.  Sometimes no text messages are
155 supplied at all, and the programmer must supply them at any point at which
156 he may wish to report error conditions.
157 Often, a global variable is assigned some value describing the error, but
158 the programmer has to know in each case whether to look at @code{errno},
159 @code{h_errno}, the return value from @code{hes_err()}, or whatever other
160 variables or routines are specified.
161 And what happens if something
162 in the procedure of
163 examining or reporting the error changes the same variable?
164
165 The package we have developed is an attempt to present a common
166 error-handling mechanism to manipulate the most common form of error code
167 in a fashion that does not have the problems listed above.
168
169 A list of up to 256 text messages is supplied to a translator we have
170 written, along with the three- to four-character ``name'' of the error
171 table.  The library using this error table need only call a routine
172 generated from this error-table source to make the table ``known'' to the
173 com_err library, and any error code the library generates can be converted
174 to the corresponding error message.  There is also a default format for
175 error codes accidentally returned before making the table known, which is
176 of the form @samp{unknown code foo 32}, where @samp{foo} would be the name
177 of the table.
178
179 @ifinfo
180 @node Error codes, Error table source file, Why com_err?, (dir)
181 @comment  node-name,  next,  previous,  up
182 @end ifinfo
183
184 @section Error codes
185
186 Error codes themselves are 32 bit (signed) integers, of which the high
187 order 24 bits are an identifier of which error table the error code is
188 from, and the low order 8 bits are a sequential error number within
189 the table.  An error code may thus be easily decomposed into its component
190 parts.  Only the lowest 32 bits of an error code are considered significant
191 on systems which support wider values.
192
193 Error table 0 is defined to match the UNIX system call error table
194 (@code{sys_errlist}); this allows @code{errno} values to be used directly
195 in the library (assuming that @code{errno} is of a type with the same width
196 as @t{long}).  Other error table numbers are formed by compacting together
197 the first four characters of the error table name.  The mapping between
198 characters in the name and numeric values in the error code are defined in
199 a system-independent fashion, so that two systems that can pass integral
200 values between them can reliably pass error codes without loss of meaning;
201 this should work even if the character sets used are not the same.
202 (However, if this is to be done, error table 0 should be avoided, since the
203 local system call error tables may differ.)
204
205 Any variable which is to contain an error code should be declared @t{long}.
206 The draft proposed American National Standard for C (as of May, 1988)
207 requires that @t{long} variables be at least 32 bits; any system which does
208 not support 32-bit @t{long} values cannot make use of this package (nor
209 much other software that assumes an ANSI-C environment base) without
210 significant effort.
211
212 @ifinfo
213 @node Error table source file, The error-table compiler, Error codes, (dir)
214 @comment  node-name,  next,  previous,  up
215 @end ifinfo
216
217 @section Error table source file
218
219 The error table source file begins with the declaration of the table name,
220 as
221
222 @example
223 error_table @var{tablename}
224 @end example
225
226 Individual error codes are
227 specified with
228
229 @example
230 error_code @var{ERROR_NAME}, @var{"text message"}
231 @end example
232
233 where @samp{ec} can also be used as a short form of @samp{error_code}.  To
234 indicate the end of the table, use @samp{end}.  Thus, a (short) sample
235 error table might be:
236
237 @example
238
239         error_table     dsc
240
241         error_code      DSC_DUP_MTG_NAME,
242                         "Meeting already exists"
243
244         ec              DSC_BAD_PATH,
245                         "A bad meeting pathname was given"
246
247         ec              DSC_BAD_MODES,
248                         "Invalid mode for this access control list"
249
250         end
251
252 @end example
253
254 @ifinfo
255 @node The error-table compiler, Run-time support routines, Error table source file, (dir)
256 @comment  node-name,  next,  previous,  up
257 @end ifinfo
258
259 @section The error-table compiler
260
261 The error table compiler is named @code{compile_et}.  It takes one
262 argument, the pathname of a file (ending in @samp{.et}, e.g.,
263 @samp{dsc_err.et}) containing an error table source file.  It parses the
264 error table, and generates two output files -- a C header file
265 (@samp{discuss_err.h}) which contains definitions of the numerical values
266 of the error codes defined in the error table, and a C source file which
267 should be compiled and linked with the executable.  The header file must be
268 included in the source of a module which wishes to reference the error
269 codes defined; the object module generated from the C code may be linked in
270 to a program which wishes to use the printed forms of the error codes.
271
272 This translator accepts a @kbd{-language @var{lang}} argument, which
273 determines for which language (or language variant) the output should be
274 written.  At the moment, @var{lang} is currently limited to @kbd{ANSI-C}
275 and @kbd{K&R-C}, and some abbreviated forms of each.  Eventually, this will
276 be extended to include some support for C++.  The default is currently
277 @kbd{K&R-C}, though the generated sources will have ANSI-C code
278 conditionalized on the symbol @t{__STDC__}.
279
280 @ifinfo
281 @node Run-time support routines, Coding Conventions, The error-table compiler, (dir)
282 @comment  node-name,  next,  previous,  up
283 @end ifinfo
284
285 @section Run-time support routines
286
287 Any source file which uses the routines supplied with or produced by the
288 com_err package should include the header file @file{<com_err.h>}.  It
289 contains declarations and definitions which may be needed on some systems.
290 (Some functions cannot be referenced properly without the return type
291 declarations in this file.  Some functions may work properly on most
292 architectures even without the header file, but relying on this is not
293 recommended.)
294
295 The run-time support routines and variables provided via this package
296 include the following:
297
298 @example
299 void initialize_@var{xxxx}_error_table (void);
300 @end example
301
302 One of these routines is built by the error compiler for each error table.
303 It makes the @var{xxxx} error table ``known'' to the error reporting
304 system.  By convention, this routine should be called in the initialization
305 routine of the @var{xxxx} library.  If the library has no initialization
306 routine, some combination of routines which form the core of the library
307 should ensure that this routine is called.  It is not advised to leave it
308 the caller to make this call.
309
310 There is no harm in calling this routine more than once.
311
312 @example
313 #define ERROR_TABLE_BASE_@var{xxxx} @var{nnnnn}L
314 @end example
315
316 This symbol contains the value of the first error code entry in the
317 specified table.
318 This rarely needs be used by the
319 programmer.
320
321 @example
322 const char *error_message (long code);
323 @end example
324
325 This routine returns the character string error message associated
326 with @code{code}; if this is associated with an unknown error table, or
327 if the code is associated with a known error table but the code is not
328 in the table, a string of the form @samp{Unknown code @var{xxxx nn}} is
329 returned, where @var{xxxx} is the error table name produced by
330 reversing the compaction performed on the error table number implied
331 by that error code, and @var{nn} is the offset from that base value.
332
333 Although this routine is available for use when needed, its use should be
334 left to circumstances which render @code{com_err} (below) unusable.
335
336 @example
337 void com_err (const char *whoami,  /* module reporting error */
338               long code,           /* error code */
339               const char *format,  /* format for additional detail */
340               ...);                /*  (extra parameters) */
341 @end example
342
343 This routine provides an alternate way to print error messages to
344 standard error; it allows the error message to be passed in as a
345 parameter, rather than in an external variable.  @emph{Provide grammatical
346 context for ``message.''}
347
348 If @var{format} is @code{(char *)NULL}, the formatted message will not be
349 printed.  @var{format} may not be omitted.
350
351 @example
352 #include <stdarg.h>
353
354 void com_err_va (const char *whoami,
355                  long code,
356                  const char *format,
357                  va_list args);
358 @end example
359
360 This routine provides an interface, equivalent to @code{com_err} above,
361 which may be used by higher-level variadic functions (functions which
362 accept variable numbers of arguments).
363
364 @example
365 #include <stdarg.h>
366
367 void (*set_com_err_hook (void (*proc) ())) ();
368
369 void (*@var{proc}) (const char *whoami, long code, va_list args);
370
371 void reset_com_err_hook ();
372 @end example
373
374 These two routines allow a routine to be dynamically substituted for
375 @samp{com_err}.  After @samp{set_com_err_hook} has been called,
376 calls to @samp{com_err} will turn into calls to the new hook routine.
377 @samp{reset_com_err_hook} turns off this hook.  This may intended to
378 be used in daemons (to use a routine which calls @var{syslog(3)}), or
379 in a window system application (which could pop up a dialogue box).
380
381 If a program is to be used in an environment in which simply printing
382 messages to the @code{stderr} stream would be inappropriate (such as in a
383 daemon program which runs without a terminal attached),
384 @code{set_com_err_hook} may be used to redirect output from @code{com_err}.
385 The following is an example of an error handler which uses @var{syslog(3)}
386 as supplied in BSD 4.3:
387
388 @example
389 #include <stdio.h>
390 #include <stdarg.h>
391 #include <syslog.h>
392
393 /* extern openlog (const char * name, int logopt, int facility); */
394 /* extern syslog (int priority, char * message, ...); */
395
396 void hook (const char * whoami, long code,
397            const char * format, va_list args)
398 @{
399     char buffer[BUFSIZ];
400     static int initialized = 0;
401     if (!initialized) @{
402         openlog (whoami,
403                  LOG_NOWAIT|LOG_CONS|LOG_PID|LOG_NDELAY,
404                  LOG_DAEMON);
405         initialized = 1;
406     @}
407     vsprintf (buffer, format, args);
408     syslog (LOG_ERR, "%s %s", error_message (code), buffer);
409 @}
410 @end example
411
412 After making the call
413 @code{set_com_err_hook (hook);},
414 any calls to @code{com_err} will result in messages being sent to the
415 @var{syslogd} daemon for logging.
416 The name of the program, @samp{whoami}, is supplied to the
417 @samp{openlog()} call, and the message is formatted into a buffer and
418 passed to @code{syslog}.
419
420 Note that since the extra arguments to @code{com_err} are passed by
421 reference via the @code{va_list} value @code{args}, the hook routine may
422 place any form of interpretation on them, including ignoring them.  For
423 consistency, @code{printf}-style interpretation is suggested, via
424 @code{vsprintf} (or @code{_doprnt} on BSD systems without full support for
425 the ANSI C library).
426
427 @ifinfo
428 @node Coding Conventions, Building and Installation, Run-time support routines, (dir)
429 @comment  node-name,  next,  previous,  up
430 @end ifinfo
431
432 @section Coding Conventions
433
434 The following conventions are just some general stylistic conventions
435 to follow when writing robust libraries and programs.  Conventions
436 similar to this are generally followed inside the UNIX kernel and most
437 routines in the Multics operating system.  In general, a routine
438 either succeeds (returning a zero error code, and doing some side
439 effects in the process), or it fails, doing minimal side effects; in
440 any event, any invariant which the library assumes must be maintained.
441
442 In general, it is not in the domain of non user-interface library
443 routines to write error messages to the user's terminal, or halt the
444 process.  Such forms of ``error handling'' should be reserved for
445 failures of internal invariants and consistancy checks only, as it
446 provides the user of the library no way to clean up for himself in the
447 event of total failure.
448
449 Library routines which can fail should be set up to return an error
450 code.  This should usually be done as the return value of the
451 function; if this is not acceptable, the routine should return a
452 ``null'' value, and put the error code into a parameter passed by
453 reference.
454
455 Routines which use the first style of interface can be used from
456 user-interface levels of a program as follows:
457
458 @example
459 @{
460     if ((code = initialize_world(getuid(), random())) != 0) @{
461         com_err("demo", code,
462                 "when trying to initialize world");
463         exit(1);
464     @}
465     if ((database = open_database("my_secrets", &code))==NULL) @{
466         com_err("demo", code,
467                 "while opening my_secrets");
468         exit(1);
469     @}
470 @}
471 @end example
472
473 A caller which fails to check the return status is in error.  It is
474 possible to look for code which ignores error returns by using lint;
475 look for error messages of the form ``foobar returns value which is
476 sometimes ignored'' or ``foobar returns value which is always
477 ignored.''
478
479 Since libraries may be built out of other libraries, it is often necessary
480 for the success of one routine to depend on another.  When a lower level
481 routine returns an error code, the middle level routine has a few possible
482 options.  It can simply return the error code to its caller after doing
483 some form of cleanup, it can substitute one of its own, or it can take
484 corrective action of its own and continue normally.  For instance, a
485 library routine which makes a ``connect'' system call to make a network
486 connection may reflect the system error code @code{ECONNREFUSED}
487 (Connection refused) to its caller, or it may return a ``server not
488 available, try again later,'' or it may try a different server.
489
490 Cleanup which is typically necessary may include, but not be limited
491 to, freeing allocated memory which will not be needed any more,
492 unlocking concurrancy locks, dropping reference counts, closing file
493 descriptors, or otherwise undoing anything which the procedure did up
494 to this point.  When there are a lot of things which can go wrong, it
495 is generally good to write one block of error-handling code which is
496 branched to, using a goto, in the event of failure.  A common source
497 of errors in UNIX programs is failing to close file descriptors on
498 error returns; this leaves a number of ``zombied'' file descriptors
499 open, which eventually causes the process to run out of file
500 descriptors and fall over.
501
502 @example
503 @{
504     FILE *f1=NULL, *f2=NULL, *f3=NULL;
505     int status = 0;
506
507     if ( (f1 = fopen(FILE1, "r")) == NULL) @{
508         status = errno;
509         goto error;
510     @}
511
512     /*
513      * Crunch for a while
514      */
515
516     if ( (f2 = fopen(FILE2, "w")) == NULL) @{
517         status = errno;
518         goto error;
519     @}
520
521     if ( (f3 = fopen(FILE3, "a+")) == NULL) @{
522         status = errno;
523             goto error;
524     @}
525
526     /*
527      * Do more processing.
528      */
529     fclose(f1);
530     fclose(f2);
531     fclose(f3);
532     return 0;
533
534 error:
535     if (f1) fclose(f1);
536     if (f2) fclose(f2);
537     if (f3) fclose(f3);
538     return status;
539 @}
540 @end example
541
542 @ifinfo
543 @node Building and Installation, Bug Reports, Coding Conventions, (dir)
544 @comment  node-name,  next,  previous,  up
545 @end ifinfo
546
547 @section Building and Installation
548
549 The distribution of this package will probably be done as a compressed
550 ``tar''-format file available via anonymous FTP from SIPB.MIT.EDU.
551 Retrieve @samp{pub/com_err.tar.Z} and extract the contents.  A subdirectory
552 @t{profiled} should be created to hold objects compiled for profiling.
553 Running ``make all'' should then be sufficient to build the library and
554 error-table compiler.  The files @samp{libcom_err.a},
555 @samp{libcom_err_p.a}, @samp{com_err.h}, and @samp{compile_et} should be
556 installed for use; @samp{com_err.3} and @samp{compile_et.1} can also be
557 installed as manual pages.
558
559 Potential problems:
560
561 @itemize @bullet
562
563 @item Use of @code{strcasecmp}, a routine provided in BSD for
564 case-insensitive string comparisons.  If an equivalent routine is
565 available, you can modify @code{CFLAGS} in the makefile to define
566 @code{strcasecmp} to the name of that routine.
567
568 @item Compilers that defined @code{__STDC__} without providing the header
569 file @code{<stdarg.h>}.  One such example is Metaware's High ``C''
570 compiler, as provided at Project Athena on the IBM RT/PC workstation; if
571 @code{__HIGHC__} is defined, it is assumed that @code{<stdarg.h>} is not
572 available, and therefore @code{<varargs.h>} must be used.  If the symbol
573 @code{VARARGS} is defined (e.g., in the makefile), @code{<varargs.h>} will
574 be used.
575
576 @item If your linker rejects symbols that are simultaneously defined in two
577 library files, edit @samp{Makefile} to remove @samp{perror.c} from the
578 library.  This file contains a version of @var{perror(3)} which calls
579 @code{com_err} instead of calling @code{write} directly.
580
581 @end itemize
582
583 As I do not have access to non-BSD systems, there are probably
584 bugs present that may interfere with building or using this package on
585 other systems.  If they are reported to me, they can probably be fixed for
586 the next version.
587
588 @ifinfo
589 @node Bug Reports, Acknowledgements, Building and Installation, (dir)
590 @comment  node-name,  next,  previous,  up
591 @end ifinfo
592
593 @section Bug Reports
594
595 Please send any comments or bug reports to the principal author: Ken
596 Raeburn, @t{Raeburn@@Athena.MIT.EDU}.
597
598 @ifinfo
599 @node Acknowledgements, , Bug Reports, (dir)
600 @comment  node-name,  next,  previous,  up
601 @end ifinfo
602
603 @section Acknowledgements
604
605 I would like to thank: Bill Sommerfeld, for his help with some of this
606 documentation, and catching some of the bugs the first time around;
607 Honeywell Information Systems, for not killing off the @emph{Multics}
608 operating system before I had an opportunity to use it; Honeywell's
609 customers, who persuaded them not to do so, for a while; Ted Anderson of
610 CMU, for catching some problems before version 1.2 left the nest; Stan
611 Zanarotti and several others of MIT's Student Information Processing Board,
612 for getting us started with ``discuss,'' for which this package was
613 originally written; and everyone I've talked into --- I mean, asked to read
614 this document and the ``man'' pages.
615
616 @bye