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