xref: /freebsd/contrib/com_err/com_err.3 (revision b652778e426d00b6a1df29bbd86869db86f36e25)
1ae771770SStanislav Sedov.\" Copyright (c) 2005 Kungliga Tekniska Högskolan
2ae771770SStanislav Sedov.\" (Royal Institute of Technology, Stockholm, Sweden).
3ae771770SStanislav Sedov.\" All rights reserved.
4f9ce010aSMark Murray.\"
5ae771770SStanislav Sedov.\" Redistribution and use in source and binary forms, with or without
6ae771770SStanislav Sedov.\" modification, are permitted provided that the following conditions
7ae771770SStanislav Sedov.\" are met:
8f9ce010aSMark Murray.\"
9ae771770SStanislav Sedov.\" 1. Redistributions of source code must retain the above copyright
10ae771770SStanislav Sedov.\"    notice, this list of conditions and the following disclaimer.
11ae771770SStanislav Sedov.\"
12ae771770SStanislav Sedov.\" 2. Redistributions in binary form must reproduce the above copyright
13ae771770SStanislav Sedov.\"    notice, this list of conditions and the following disclaimer in the
14ae771770SStanislav Sedov.\"    documentation and/or other materials provided with the distribution.
15ae771770SStanislav Sedov.\"
16ae771770SStanislav Sedov.\" 3. Neither the name of the Institute nor the names of its contributors
17ae771770SStanislav Sedov.\"    may be used to endorse or promote products derived from this software
18ae771770SStanislav Sedov.\"    without specific prior written permission.
19ae771770SStanislav Sedov.\"
20ae771770SStanislav Sedov.\" THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
21ae771770SStanislav Sedov.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22ae771770SStanislav Sedov.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ae771770SStanislav Sedov.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
24ae771770SStanislav Sedov.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25ae771770SStanislav Sedov.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26ae771770SStanislav Sedov.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27ae771770SStanislav Sedov.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28ae771770SStanislav Sedov.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29ae771770SStanislav Sedov.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30ae771770SStanislav Sedov.\" SUCH DAMAGE.
31ae771770SStanislav Sedov.\"
32ae771770SStanislav Sedov.\" $Id$
33ae771770SStanislav Sedov.\"
34ae771770SStanislav Sedov.\" This manpage was contributed by Gregory McGarry.
35ae771770SStanislav Sedov.\"
36ae771770SStanislav Sedov.Dd July  7, 2005
37ae771770SStanislav Sedov.Dt COM_ERR 3
38ae771770SStanislav Sedov.Os
39ae771770SStanislav Sedov.Sh NAME
40ae771770SStanislav Sedov.Nm com_err ,
41ae771770SStanislav Sedov.Nm com_err_va ,
42ae771770SStanislav Sedov.Nm error_message ,
43ae771770SStanislav Sedov.Nm error_table_name ,
44ae771770SStanislav Sedov.Nm init_error_table ,
45ae771770SStanislav Sedov.Nm set_com_err_hook ,
46ae771770SStanislav Sedov.Nm reset_com_err_hook ,
47ae771770SStanislav Sedov.Nm add_to_error_table ,
48ae771770SStanislav Sedov.Nm initialize_error_table_r
49ae771770SStanislav Sedov.Nm free_error_table ,
50ae771770SStanislav Sedov.Nm com_right
51ae771770SStanislav Sedov.Nd common error display library
52ae771770SStanislav Sedov.Sh LIBRARY
53ae771770SStanislav SedovCommon Error Library (libcom_err, -lcom_err)
54ae771770SStanislav Sedov.Sh SYNOPSIS
55ae771770SStanislav Sedov.Fd #include <stdio.h>
56ae771770SStanislav Sedov.Fd #include <stdarg.h>
57*50c9ba2cSStanislav Sedov.Fd #include <com_err.h>
58ae771770SStanislav Sedov.Fd #include \&"XXX_err.h\&"
59ae771770SStanislav Sedov.Pp
60ae771770SStanislav Sedovtypedef void (*errf)(const char *, long, const char *, ...);
61ae771770SStanislav Sedov.Ft void
62ae771770SStanislav Sedov.Fn com_err "const char *whoami" "long code" "const char *format" "..."
63ae771770SStanislav Sedov.Ft void
64ae771770SStanislav Sedov.Fn com_err_va "const char *whoami" "long code" "const char *format" "..."
65ae771770SStanislav Sedov.Ft const char *
66ae771770SStanislav Sedov.Fn error_message "long code"
67ae771770SStanislav Sedov.Ft const char *
68ae771770SStanislav Sedov.Fn error_table_name "int num"
69ae771770SStanislav Sedov.Ft int
70ae771770SStanislav Sedov.Fn init_error_table "const char **msgs" "long base" "int count"
71ae771770SStanislav Sedov.Ft errf
72ae771770SStanislav Sedov.Fn set_com_err_hook "errf func"
73ae771770SStanislav Sedov.Ft errf
74ae771770SStanislav Sedov.Fn reset_com_err_hook ""
75ae771770SStanislav Sedov.Ft void
76ae771770SStanislav Sedov.Fn add_to_error_table "struct et_list *new_table"
77ae771770SStanislav Sedov.Ft void
78ae771770SStanislav Sedov.Fn initialize_error_table_r "struct et_list **et_list" "const char **msgs" "int base" "long count"
79ae771770SStanislav Sedov.Ft void
80ae771770SStanislav Sedov.Fn free_error_table "struct et_list *"
81ae771770SStanislav Sedov.Ft const char *
82ae771770SStanislav Sedov.Fn com_right "struct et_list *list" long code"
83ae771770SStanislav Sedov.Sh DESCRIPTION
84ae771770SStanislav SedovThe
85ae771770SStanislav Sedov.Nm
86ae771770SStanislav Sedovlibrary provides a common error-reporting mechanism for defining and
87ae771770SStanislav Sedovaccessing error codes and descriptions for application software
88ae771770SStanislav Sedovpackages.  Error descriptions are defined in a table and error codes
89ae771770SStanislav Sedovare used to index the table.  The error table, the descriptions and
90ae771770SStanislav Sedovthe error codes are generated using
91ae771770SStanislav Sedov.Xr compile_et 1 .
92ae771770SStanislav Sedov.Pp
93ae771770SStanislav SedovThe error table is registered with the
94ae771770SStanislav Sedov.Nm
95ae771770SStanislav Sedovlibrary by calling its initialisation function defined in its header
96ae771770SStanislav Sedovfile.  The initialisation function is generally defined as
97ae771770SStanislav Sedov.Fn initialize_<name>_error_table ,
98ae771770SStanislav Sedovwhere
99ae771770SStanislav Sedov.Em name
100ae771770SStanislav Sedovis the name of the error table.
101ae771770SStanislav Sedov.Pp
102ae771770SStanislav SedovIf a thread-safe version of the library is needed
103ae771770SStanislav Sedov.Fn initialize_<name>_error_table_r
104ae771770SStanislav Sedovthat internally calls
105ae771770SStanislav Sedov.Fn initialize_error_table_r
106ae771770SStanislav Sedovinstead be used.
107ae771770SStanislav Sedov.Pp
108ae771770SStanislav SedovAny variable which is to contain an error code should be declared
109ae771770SStanislav Sedov.Em <name>_error_number
110ae771770SStanislav Sedovwhere
111ae771770SStanislav Sedov.Em name
112ae771770SStanislav Sedovis the name of the error table.
113ae771770SStanislav Sedov.Sh FUNCTIONS
114ae771770SStanislav SedovThe following functions are available to the application developer:
115ae771770SStanislav Sedov.Bl -tag -width compact
116ae771770SStanislav Sedov.It Fn com_err "whoami" "code" "format" "..."
117ae771770SStanislav SedovDisplays an error message on standard error composed of the
118ae771770SStanislav Sedov.Fa whoami
119ae771770SStanislav Sedovstring, which should specify the program name, followed by an error
120ae771770SStanislav Sedovmessage generated from
121ae771770SStanislav Sedov.Fa code ,
122f9ce010aSMark Murrayand a string produced using the
123ae771770SStanislav Sedov.Xr printf 3
124ae771770SStanislav Sedov.Fa format
125ae771770SStanislav Sedovstring and any following arguments.  If
126ae771770SStanislav Sedov.Fa format
127ae771770SStanislav Sedovis NULL, the formatted message will not be
128ae771770SStanislav Sedovprinted.  The argument
129ae771770SStanislav Sedov.Fa format
130ae771770SStanislav Sedovmay not be omitted.
131ae771770SStanislav Sedov.It Fn com_err_va "whoami" "code" "format" "va_list args"
132ae771770SStanislav SedovThis routine provides an interface, equivalent to
133ae771770SStanislav Sedov.Fn com_err ,
134ae771770SStanislav Sedovwhich may be used by higher-level variadic functions (functions which
135ae771770SStanislav Sedovaccept variable numbers of arguments).
136ae771770SStanislav Sedov.It Fn error_message "code"
137ae771770SStanislav SedovReturns the character string error message associate with
138ae771770SStanislav Sedov.Fa code .
139ae771770SStanislav SedovIf
140ae771770SStanislav Sedov.Fa code is associated with an unknown error table, or if
141ae771770SStanislav Sedov.Fa code
142ae771770SStanislav Sedovis associated with a known error table but is not in the table, a
143ae771770SStanislav Sedovstring of the form `Unknown code XXXX NN' is returned, where XXXX is
144ae771770SStanislav Sedovthe error table name produced by reversing the compaction performed on
145ae771770SStanislav Sedovthe error table number implied by that error code, and NN is the
146ae771770SStanislav Sedovoffset from that base value.
147ae771770SStanislav Sedov.Pp
148ae771770SStanislav SedovAlthough this routine is available for use when needed, its use should
149ae771770SStanislav Sedovbe left to circumstances which render
150ae771770SStanislav Sedov.Fn com_err
151ae771770SStanislav Sedovunusable.
152ae771770SStanislav Sedov.Pp
153ae771770SStanislav Sedov.Fn com_right
154ae771770SStanislav Sedovreturns the error string just like
155ae771770SStanislav Sedov.Fa com_err
156ae771770SStanislav Sedovbut in a thread-safe way.
157ae771770SStanislav Sedov.Pp
158ae771770SStanislav Sedov.It Fn error_table_name "num"
159ae771770SStanislav SedovConvert a machine-independent error table number
160ae771770SStanislav Sedov.Fa num
161ae771770SStanislav Sedovinto an error table name.
162ae771770SStanislav Sedov.It Fn init_error_table "msgs" "base" "count"
163ae771770SStanislav SedovInitialise the internal error table with the array of character string
164ae771770SStanislav Sedoverror messages in
165ae771770SStanislav Sedov.Fa msgs
166ae771770SStanislav Sedovof length
167ae771770SStanislav Sedov.Fa count .
168ae771770SStanislav SedovThe error codes are assigned incrementally from
169ae771770SStanislav Sedov.Fa base .
170ae771770SStanislav SedovThis function is useful for using the error-reporting mechanism with
171ae771770SStanislav Sedovcustom error tables that have not been generated with
172ae771770SStanislav Sedov.Xr compile_et 1 .
173ae771770SStanislav SedovAlthough this routine is available for use when needed, its use should
174ae771770SStanislav Sedovbe restricted.
175ae771770SStanislav Sedov.Pp
176ae771770SStanislav Sedov.Fn initialize_error_table_r
177ae771770SStanislav Sedovinitialize the
178ae771770SStanislav Sedov.Fa et_list
179ae771770SStanislav Sedovin the same way as
180ae771770SStanislav Sedov.Fn init_error_table ,
181ae771770SStanislav Sedovbut in a thread-safe way.
182ae771770SStanislav Sedov.Pp
183ae771770SStanislav Sedov.It Fn set_com_err_hook "func"
184ae771770SStanislav SedovProvides a hook into the
185ae771770SStanislav Sedov.Nm
186ae771770SStanislav Sedovlibrary to allow the routine
187ae771770SStanislav Sedov.Fa func
188ae771770SStanislav Sedovto be dynamically substituted for
189ae771770SStanislav Sedov.Fn com_err .
190ae771770SStanislav SedovAfter
191ae771770SStanislav Sedov.Fn set_com_err_hook
192ae771770SStanislav Sedov has been called, calls to
193ae771770SStanislav Sedov.Fn com_err
194ae771770SStanislav Sedovwill turn into calls to the new hook routine.  This function is
195ae771770SStanislav Sedovintended to be used in daemons to use a routine which calls
196ae771770SStanislav Sedov.Xr syslog 3 ,
197ae771770SStanislav Sedovor in a window system application to pop up a dialogue box.
198ae771770SStanislav Sedov.It Fn reset_com_err_hook ""
199ae771770SStanislav SedovTurns off the hook set in
200ae771770SStanislav Sedov.Fn set_com_err_hook .
201ae771770SStanislav Sedov.It Fn add_to_error_table "new_table"
202ae771770SStanislav SedovAdd the error table, its messages strings and error codes in
203ae771770SStanislav Sedov.Fa new_table
204ae771770SStanislav Sedovto the internal error table.
205ae771770SStanislav Sedov.El
206ae771770SStanislav Sedov.Sh EXAMPLES
207ae771770SStanislav SedovThe following is an example using the table defined in
208ae771770SStanislav Sedov.Xr compile_et 1 :
209ae771770SStanislav Sedov.Pp
210ae771770SStanislav Sedov.Bd -literal
211ae771770SStanislav Sedov	#include <stdio.h>
212ae771770SStanislav Sedov	#include <stdarg.h>
213ae771770SStanislav Sedov	#include <syslog.h>
214f9ce010aSMark Murray
215ae771770SStanislav Sedov	#include "test_err.h"
216f9ce010aSMark Murray
217ae771770SStanislav Sedov	void
218ae771770SStanislav Sedov	hook(const char *whoami, long code,
219ae771770SStanislav Sedov		const char *format, va_list args)
220ae771770SStanislav Sedov	{
221ae771770SStanislav Sedov		char buffer[BUFSIZ];
222ae771770SStanislav Sedov		static int initialized = 0;
223f9ce010aSMark Murray
224ae771770SStanislav Sedov		if (!initialized) {
225ae771770SStanislav Sedov			openlog(whoami, LOG_NOWAIT, LOG_DAEMON);
226ae771770SStanislav Sedov			initialized = 1;
227ae771770SStanislav Sedov		}
228ae771770SStanislav Sedov		vsprintf(buffer, format, args);
229ae771770SStanislav Sedov		syslog(LOG_ERR, "%s %s", error_message(code), buffer);
230ae771770SStanislav Sedov	}
231f9ce010aSMark Murray
232ae771770SStanislav Sedov	int
233ae771770SStanislav Sedov	main(int argc, char *argv[])
234ae771770SStanislav Sedov	{
235ae771770SStanislav Sedov		char *whoami = argv[0];
236f9ce010aSMark Murray
237ae771770SStanislav Sedov		initialize_test_error_table();
238ae771770SStanislav Sedov		com_err(whoami, TEST_INVAL, "before hook");
239ae771770SStanislav Sedov		set_com_err_hook(hook);
240ae771770SStanislav Sedov		com_err(whoami, TEST_IO, "after hook");
241ae771770SStanislav Sedov		return (0);
242ae771770SStanislav Sedov	}
243ae771770SStanislav Sedov.Ed
244ae771770SStanislav Sedov.Sh SEE ALSO
245ae771770SStanislav Sedov.Xr compile_et 1
246