1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
.\" The contents of this file are subject to the terms of the Common
.\" Development and Distribution License (the "License"). You may not use
.\" this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or
.\" http://www.opensolaris.org/os/licensing. See the License for the
.\" specific language governing permissions and limitations under the
.\" License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each file
.\" and include the License file at usr/src/OPENSOLARIS.LICENSE. If
.\" applicable, add the following below this CDDL HEADER, with the fields
.\" enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\" Copyright (c) 1996-2001 Wolfram Schneider. Berlin.
.\" Copyright (c) 1993-1995 Berkeley Software Design, Inc.
.\" Portions Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 2014 Nexenta Systems, Inc. All Rights Reserved.
.\" Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
.\"
.Dd November 15, 2022
.Dt ERR 3C
.Os
.Sh NAME
.Nm err ,
.Nm errc ,
.Nm errx ,
.Nm warn ,
.Nm warnc ,
.Nm warnx ,
.Nm verr ,
.Nm verrc ,
.Nm verrx ,
.Nm vwarn ,
.Nm vwarnc ,
.Nm vwarnx
.Nd formatted error messages
.Sh SYNOPSIS
.In err.h
.Ft void
.Fo err
.Fa "int eval"
.Fa "const char *fmt"
.Fa "..."
.Fc
.Ft void
.Fo errc
.Fa "int eval"
.Fa "int code"
.Fa "const char *fmt"
.Fa "..."
.Fc
.Ft void
.Fo errx
.Fa "int eval"
.Fa "const char *fmt"
.Fa "..."
.Fc
.Ft void
.Fo warn
.Fa "const char *fmt"
.Fa "..."
.Fc
.Ft void
.Fo warnc
.Fa "int code"
.Fa "const char *fmt"
.Fa "..."
.Fc
.Ft void
.Fo warnx
.Fa "const char *fmt"
.Fa "..."
.Fc
.Ft void
.Fo verr
.Fa "int eval"
.Fa "const char *fmt"
.Fa "va_list args"
.Fc
.Ft void
.Fo verrc
.Fa "int eval"
.Fa "int code"
.Fa "const char *fmt"
.Fa "va_list args"
.Fc
.Ft void
.Fo verrx
.Fa "int eval"
.Fa "const char *fmt"
.Fa "va_list args"
.Fc
.Ft void
.Fo vwarn
.Fa "const char *fmt"
.Fa "va_list args"
.Fc
.Ft void
.Fo vwarnc
.Fa "int code"
.Fa "const char *fmt"
.Fa "va_list args"
.Fc
.Ft void
.Fo vwarnx
.Fa "const char *fmt"
.Fa "va_list args"
.Fc
.Sh DESCRIPTION
The
.Fn err
and
.Fn warn
family of functions display a formatted error message to standard error.
In all cases, the last component of the program name, followed by a colon
character and a space, are output.
If the
.Ar fmt
argument is not
.Dv NULL ,
the formatted error message is output.
.Pp
In the case of the
.Fn err ,
.Fn errc ,
.Fn warn ,
.Fn warnc ,
.Fn verr ,
.Fn verrc ,
.Fn vwarn
and
.Fn vwarnc
functions, an error message obtained from
.Xr strerror 3C
is output next, preceded by a colon character and a space if
.Ar fmt
is not
.Dv NULL .
The
.Fn err ,
.Fn warn ,
.Fn verr
and
.Fn vwarn
functions produce the error string affiliated with the current value of the
global variable
.Va errno .
The
.Fn errc ,
.Fn warnc ,
.Fn verrc
and
.Fn vwarnc
functions use the provided
.Ar code
value to look up the error message.
.Pp
The
.Fn errx ,
.Fn verrx ,
.Fn warnx
and
.Fn vwarnx
functions will not output this error message string.
.Pp
In all cases, the output is followed by a newline character.
.Pp
The
.Fn err ,
.Fn errc ,
.Fn errx ,
.Fn verr ,
.Fn verrc
and
.Fn verrx
functions do not return, but instead cause the program to terminate with the
status value given by the
.Ar eval
argument.
.Sh EXAMPLES
.Sy Example 1
Display the current
.Va errno
information string and terminate with status indicating failure.
.Bd -literal -offset indent
#include <err.h>
\&...
if ((p = malloc(size)) == NULL)
err(EXIT_FAILURE, NULL);
if ((fd = open(file_name, O_RDONLY, 0)) == -1)
err(EXIT_FAILURE, "%s", file_name);
.Ed
.Pp
.Sy Example 2
Display an error message and terminate with status indicating failure.
.Bd -literal -offset indent
if (tm.tm_hour < START_TIME)
errx(EXIT_FAILURE, "wait until %s", start_time_string);
.Ed
.Pp
.Sy Example 3
Warn of an error.
.Bd -literal -offset indent
if ((fd = open(raw_device, O_RDONLY, 0)) == -1) {
warnx("%s: %s: trying the block device",
raw_device, strerror(errno));
}
if ((fd = open(block_device, O_RDONLY, 0)) == -1)
warn("%s", block_device);
.Ed
.Pp
.Sy Example 4
Warn of an error using a custom error code
.Bd -literal -offset indent
int error = function_returning_error_code();
if (error != 0)
warnc(error, "%s", "function did not succeed");
.Ed
.Sh WARNINGS
It is important never to pass a string with user-supplied data as a format
without using
.Sq %s .
An attacker can put format specifiers in the string to mangle the stack,
leading to a possible security hole.
This holds true even if the string has been built by hand using a function
like
.Xr snprintf 3C ,
as the resulting string can still contain user-supplied conversion specifiers
for later interpolation by the
.Fn err
and
.Fn warn
functions.
.Pp
Always be sure to use the proper secure idiom:
.Bd -literal -offset indent
err(1, "%s", string);
.Ed
.Sh INTERFACE STABILITY
.Sy Committed
.Sh MT-LEVEL
.Sy MT-Safe with Exceptions
.Pp
These functions are safe to use in multithreaded applications as long as
.Xr setlocale 3C
is not being called to change the locale.
.Sh SEE ALSO
.Xr exit 3C ,
.Xr getexecname 3C ,
.Xr setlocale 3C ,
.Xr strerror 3C ,
.Xr attributes 7
.Sh STANDARDS
The functions described in this man page are
.Bx
extensions and should not be used in portable code.
|