summaryrefslogtreecommitdiff
path: root/usr/src/man/man3c/err.3c
blob: c81427c3dde72ef6c707d4bec3a40f24f4892bf8 (plain)
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
'\" te
.\" Copyright 2014 Nexenta Systems, Inc.  All Rights Reserved.
.\" 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.
.\" 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]
.TH ERR 3C "Nov 24, 2014"
.SH NAME
err, verr, errx, verrx, warn, vwarn, warnx, vwarnx \- formatted error messages
.SH SYNOPSIS
.LP
.nf
#include <err.h>

\fBvoid\fR \fBerr\fR(\fBint\fR \fIeval\fR, \fBconst char *\fR\fIfmt\fR, ...);
.fi

.LP
.nf
\fBvoid\fR \fBverr\fR(\fBint\fR \fIeval\fR, \fBconst char *\fR\fIfmt\fR, \fBva_list\fR \fIargs\fR);
.fi

.LP
.nf
\fBvoid\fR \fBerrx\fR(\fBint\fR \fIeval\fR, \fBconst char *\fR\fIfmt\fR, ...);
.fi

.LP
.nf
\fBvoid\fR \fBverrx\fR\fB(int\fR \fIeval\fR, \fBconst char *\fR\fIfmt\fR, \fBva_list\fR \fIargs\fR);
.fi

.LP
.nf
\fBvoid\fR \fBwarn\fR(\fBconst char *\fR\fIfmt\fR, ...);
.fi

.LP
.nf
\fBvoid\fR \fBvwarn\fR(\fBconst char *\fR\fIfmt\fR, \fBva_list\fR \fIargs\fR);
.fi

.LP
.nf
\fBvoid\fR \fBwarnx\fR(\fBconst char *\fR\fIfmt\fR, ...);
.fi

.LP
.nf
\fBvoid\fR \fBvwarnx\fR(\fBconst char *\fR\fIfmt\fR, \fBva_list\fR \fIargs\fR);
.fi

.SH DESCRIPTION
.LP
The \fBerr()\fR and \fBwarn()\fR family of functions display a formatted error
message on the standard error output. In all cases, the last component of the
program name, followed by a colon character and a space, are output. If the
\fIfmt\fR argument is not \fINULL\fR, the formatted error message is output. In
the case of the \fBerr()\fR, \fBverr()\fR, \fBwarn()\fR, and \fBvwarn()\fR
functions, the error message string affiliated with the current value of the
global variable \fBerrno\fR is output next, preceded by a colon character and a
space if \fIfmt\fR is not \fINULL\fR. In all cases, the output is followed by a
newline character.  The \fBerrx()\fR, \fBverrx()\fR, \fBwarnx()\fR, and
\fBvwarnx()\fR functions will not output this error message string.
.sp
.LP
The \fBerr()\fR, \fBverr()\fR, \fBerrx()\fR, and \fBverrx()\fR functions do not
return, but instead cause the program to terminate with the status value given
by the argument \fIeval\fR.
.SH EXAMPLES
.LP
\fBExample 1 \fRDisplay the current \fBerrno\fR information string and
terminate with status indicating failure.
.sp
.in +2
.nf
if ((p = malloc(size)) == NULL)
    err(EXIT_FAILURE, NULL);
if ((fd = open(file_name, O_RDONLY, 0)) == -1)
    err(EXIT_FAILURE, "%s", file_name);
.fi
.in -2

.LP
\fBExample 2 \fRDisplay an error message and terminate with status indicating
failure.
.sp
.in +2
.nf
if (tm.tm_hour < START_TIME)
    errx(EXIT_FAILURE, "too early, wait until %s", start_time_string);
.fi
.in -2

.LP
\fBExample 3 \fRWarn of an error.
.sp
.in +2
.nf
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);
.fi
.in -2

.SH WARNINGS
.LP
It is important never to pass a string with user-supplied data as a format
without using `%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 \fBsnprintf\fR(3C),
as the resulting string can still contain user-supplied conversion specifiers
for later interpolation by the \fBerr()\fR and \fBwarn()\fR functions.
.sp
.LP
Always be sure to use the proper secure idiom:
.sp
.in +2
.nf
err(1, "%s", string);
.fi
.in -2

.SH ATTRIBUTES
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	Safe with Exceptions
.TE

.sp
.LP
These functions are safe to use in multithreaded applications as long as
\fBsetlocale\fR(3C) is not being called to change the locale.
.SH SEE ALSO
.LP
.BR exit (3C),
.BR getexecname (3C),
.BR setlocale (3C),
.BR strerror (3C),
.BR attributes (7)