1 .\" Copyright (c) 2005 Kungliga Tekniska Högskolan
2 .\" (Royal Institute of Technology, Stockholm, Sweden).
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the Institute nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" This manpage was contributed by Gregory McGarry.
43 .Nm error_table_name ,
44 .Nm init_error_table ,
45 .Nm set_com_err_hook ,
46 .Nm reset_com_err_hook ,
47 .Nm add_to_error_table ,
48 .Nm initialize_error_table_r
49 .Nm free_error_table ,
51 .Nd common error display library
53 Common Error Library (libcom_err, -lcom_err)
55 .Fd #include <stdio.h>
56 .Fd #include <stdarg.h>
57 .Fd #include <krb5/com_err.h>
58 .Fd #include \&"XXX_err.h\&"
60 typedef void (*errf)(const char *, long, const char *, ...);
62 .Fn com_err "const char *whoami" "long code" "const char *format" "..."
64 .Fn com_err_va "const char *whoami" "long code" "const char *format" "..."
66 .Fn error_message "long code"
68 .Fn error_table_name "int num"
70 .Fn init_error_table "const char **msgs" "long base" "int count"
72 .Fn set_com_err_hook "errf func"
74 .Fn reset_com_err_hook ""
76 .Fn add_to_error_table "struct et_list *new_table"
78 .Fn initialize_error_table_r "struct et_list **et_list" "const char **msgs" "int base" "long count"
80 .Fn free_error_table "struct et_list *"
82 .Fn com_right "struct et_list *list" long code"
86 library provides a common error-reporting mechanism for defining and
87 accessing error codes and descriptions for application software
88 packages. Error descriptions are defined in a table and error codes
89 are used to index the table. The error table, the descriptions and
90 the error codes are generated using
93 The error table is registered with the
95 library by calling its initialisation function defined in its header
96 file. The initialisation function is generally defined as
97 .Fn initialize_<name>_error_table ,
100 is the name of the error table.
102 If a thread-safe version of the library is needed
103 .Fn initialize_<name>_error_table_r
104 that internally calls
105 .Fn initialize_error_table_r
108 Any variable which is to contain an error code should be declared
109 .Em <name>_error_number
112 is the name of the error table.
114 The following functions are available to the application developer:
115 .Bl -tag -width compact
116 .It Fn com_err "whoami" "code" "format" "..."
117 Displays an error message on standard error composed of the
119 string, which should specify the program name, followed by an error
120 message generated from
122 and a string produced using the
125 string and any following arguments. If
127 is NULL, the formatted message will not be
128 printed. The argument
131 .It Fn com_err_va "whoami" "code" "format" "va_list args"
132 This routine provides an interface, equivalent to
134 which may be used by higher-level variadic functions (functions which
135 accept variable numbers of arguments).
136 .It Fn error_message "code"
137 Returns the character string error message associated with
141 is associated with an unknown error table, or if
143 is associated with a known error table but is not in the table, a
144 string of the form `Unknown code XXXX NN' is returned, where XXXX is
145 the error table name produced by reversing the compaction performed on
146 the error table number implied by that error code, and NN is the
147 offset from that base value.
149 Although this routine is available for use when needed, its use should
150 be left to circumstances which render
155 returns the error string just like
157 but in a thread-safe way.
159 .It Fn error_table_name "num"
160 Convert a machine-independent error table number
162 into an error table name.
163 .It Fn init_error_table "msgs" "base" "count"
164 Initialise the internal error table with the array of character string
169 The error codes are assigned incrementally from
171 This function is useful for using the error-reporting mechanism with
172 custom error tables that have not been generated with
174 Although this routine is available for use when needed, its use should
177 .Fn initialize_error_table_r
181 .Fn init_error_table ,
182 but in a thread-safe way.
184 .It Fn set_com_err_hook "func"
185 Provides a hook into the
187 library to allow the routine
189 to be dynamically substituted for
193 has been called, calls to
195 will turn into calls to the new hook routine. This function is
196 intended to be used in daemons to use a routine which calls
198 or in a window system application to pop up a dialogue box.
199 .It Fn reset_com_err_hook ""
200 Turns off the hook set in
201 .Fn set_com_err_hook .
202 .It Fn add_to_error_table "new_table"
203 Add the error table, its messages strings and error codes in
205 to the internal error table.
208 The following is an example using the table defined in
216 #include "test_err.h"
219 hook(const char *whoami, long code,
220 const char *format, va_list args)
223 static int initialized = 0;
226 openlog(whoami, LOG_NOWAIT, LOG_DAEMON);
229 vsprintf(buffer, format, args);
230 syslog(LOG_ERR, "%s %s", error_message(code), buffer);
234 main(int argc, char *argv[])
236 char *whoami = argv[0];
238 initialize_test_error_table();
239 com_err(whoami, TEST_INVAL, "before hook");
240 set_com_err_hook(hook);
241 com_err(whoami, TEST_IO, "after hook");