xref: /onnv-gate/usr/src/uts/common/fs/nfs/nfs_strerror.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This file contains code to support better NFS error messages.  Death to
31*0Sstevel@tonic-gate  * integer codes in user error messages!
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * XXX Ideally this code should be more general and available to the entire
34*0Sstevel@tonic-gate  * kernel (see RFE 1101936).  When this happens, this file can go away.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <nfs/nfs.h>
38*0Sstevel@tonic-gate #include <sys/systm.h>
39*0Sstevel@tonic-gate #include <sys/cmn_err.h>
40*0Sstevel@tonic-gate #include <sys/errno.h>
41*0Sstevel@tonic-gate #include <sys/varargs.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #ifndef NULL
44*0Sstevel@tonic-gate #define	NULL	0
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /* size of a temporary printf format buffer. */
48*0Sstevel@tonic-gate #define	FMT_BUF_SIZE	1024
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static void expand_format_string(int, const char *, char *, int);
51*0Sstevel@tonic-gate static char *nfs_strerror(int);
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * nfs_perror: Works like printf (format string and variable args) except
55*0Sstevel@tonic-gate  * that it will substitute an error message for a "%m" string (like
56*0Sstevel@tonic-gate  * syslog), using the given errno value.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate void
nfs_perror(int error,char * fmt,...)60*0Sstevel@tonic-gate nfs_perror(int error, char *fmt, ...)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	va_list ap;
63*0Sstevel@tonic-gate 	char buf[FMT_BUF_SIZE];		/* massaged version of fmt */
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	/* Expand %m */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	expand_format_string(error, fmt, buf, FMT_BUF_SIZE);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	/*
70*0Sstevel@tonic-gate 	 * Now pass the massaged format string and its arguments off to
71*0Sstevel@tonic-gate 	 * printf.
72*0Sstevel@tonic-gate 	 */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	va_start(ap, fmt);
75*0Sstevel@tonic-gate 	(void) vzprintf(getzoneid(), buf, ap);
76*0Sstevel@tonic-gate 	va_end(ap);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * nfs_cmn_err: Works like cmn_err (error level, format string, and
81*0Sstevel@tonic-gate  * variable args) except that it will substitute an error message for a
82*0Sstevel@tonic-gate  * "%m" string (like syslog), using the given errno value.
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate void
nfs_cmn_err(int error,int level,char * fmt,...)86*0Sstevel@tonic-gate nfs_cmn_err(int error, int level, char *fmt, ...)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	va_list ap;
89*0Sstevel@tonic-gate 	char buf[FMT_BUF_SIZE];		/* massaged version of fmt */
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/* Expand %m */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	expand_format_string(error, fmt, buf, FMT_BUF_SIZE);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	/*
96*0Sstevel@tonic-gate 	 * Now pass the massaged format string and its arguments off to
97*0Sstevel@tonic-gate 	 * cmn_err.
98*0Sstevel@tonic-gate 	 */
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	va_start(ap, fmt);
101*0Sstevel@tonic-gate 	(void) vzcmn_err(getzoneid(), level, buf, ap);
102*0Sstevel@tonic-gate 	va_end(ap);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * expand_format_string: copy the printf format string from "fmt" to "buf",
107*0Sstevel@tonic-gate  * expanding %m to the error string for "error".
108*0Sstevel@tonic-gate  */
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate static void
expand_format_string(int error,const char * fmt,char * buf,int buf_chars)111*0Sstevel@tonic-gate expand_format_string(int error, const char *fmt, char *buf, int buf_chars)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	const char *from;		/* pointer into fmt */
114*0Sstevel@tonic-gate 	char *to;			/* pointer into buf */
115*0Sstevel@tonic-gate 	char *errmsg;			/* expansion for %m */
116*0Sstevel@tonic-gate 	char *trunc_msg = "Truncated NFS error message: ";
117*0Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/*
120*0Sstevel@tonic-gate 	 * Copy the given format string into the result buffer, expanding
121*0Sstevel@tonic-gate 	 * %m as we go.  If the result buffer is too short, complain and
122*0Sstevel@tonic-gate 	 * truncate the message.  (We don't expect this to ever happen,
123*0Sstevel@tonic-gate 	 * though.)
124*0Sstevel@tonic-gate 	 */
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	for (from = fmt, to = buf; *from; from++) {
127*0Sstevel@tonic-gate 		if (to >= buf + buf_chars - 1) {
128*0Sstevel@tonic-gate 			zprintf(zoneid, trunc_msg);
129*0Sstevel@tonic-gate 			break;
130*0Sstevel@tonic-gate 		}
131*0Sstevel@tonic-gate 		if (*from == '%' && *(from+1) == 'm') {
132*0Sstevel@tonic-gate 			errmsg = nfs_strerror(error);
133*0Sstevel@tonic-gate 			/*
134*0Sstevel@tonic-gate 			 * If there's an error message and room to display
135*0Sstevel@tonic-gate 			 * it, copy it in.  If there's no message or not
136*0Sstevel@tonic-gate 			 * enough room, try just printing an error number.
137*0Sstevel@tonic-gate 			 * (We assume that the error value is in a
138*0Sstevel@tonic-gate 			 * reasonable range.)  If there's no room for
139*0Sstevel@tonic-gate 			 * anything, bail out.
140*0Sstevel@tonic-gate 			 */
141*0Sstevel@tonic-gate 			if (errmsg != NULL &&
142*0Sstevel@tonic-gate 			    strlen(buf) + strlen(errmsg) < buf_chars) {
143*0Sstevel@tonic-gate 				(void) strcpy(to, errmsg);
144*0Sstevel@tonic-gate 				to += strlen(errmsg);
145*0Sstevel@tonic-gate 			} else if (strlen(buf) + strlen("error XXX") <
146*0Sstevel@tonic-gate 			    buf_chars) {
147*0Sstevel@tonic-gate 				(void) sprintf(to, "error %d", error);
148*0Sstevel@tonic-gate 				/*
149*0Sstevel@tonic-gate 				 * Don't try to guess how many characters
150*0Sstevel@tonic-gate 				 * were laid down.
151*0Sstevel@tonic-gate 				 */
152*0Sstevel@tonic-gate 				to = buf + strlen(buf);
153*0Sstevel@tonic-gate 			} else {
154*0Sstevel@tonic-gate 				zprintf(zoneid, trunc_msg);
155*0Sstevel@tonic-gate 				break;
156*0Sstevel@tonic-gate 			}
157*0Sstevel@tonic-gate 			from++;
158*0Sstevel@tonic-gate 		} else {
159*0Sstevel@tonic-gate 			*to++ = *from;
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 	*to = '\0';
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate /*
166*0Sstevel@tonic-gate  * nfs_strerror: map an errno value to a string.  Not all possible errno
167*0Sstevel@tonic-gate  * values are supported.
168*0Sstevel@tonic-gate  *
169*0Sstevel@tonic-gate  * If there is no string for the given errno value, return NULL.
170*0Sstevel@tonic-gate  */
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static char *
nfs_strerror(int errcode)173*0Sstevel@tonic-gate nfs_strerror(int errcode)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	char *result;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	switch (errcode) {
178*0Sstevel@tonic-gate 	case EPERM:
179*0Sstevel@tonic-gate 		result = "Not owner";
180*0Sstevel@tonic-gate 		break;
181*0Sstevel@tonic-gate 	case ENOENT:
182*0Sstevel@tonic-gate 		result = "No such file or directory";
183*0Sstevel@tonic-gate 		break;
184*0Sstevel@tonic-gate 	case EIO:
185*0Sstevel@tonic-gate 		result = "I/O error";
186*0Sstevel@tonic-gate 		break;
187*0Sstevel@tonic-gate 	case EACCES:
188*0Sstevel@tonic-gate 		result = "Permission denied";
189*0Sstevel@tonic-gate 		break;
190*0Sstevel@tonic-gate 	case EEXIST:
191*0Sstevel@tonic-gate 		result = "File exists";
192*0Sstevel@tonic-gate 		break;
193*0Sstevel@tonic-gate 	case ENOTDIR:
194*0Sstevel@tonic-gate 		result = "Not a directory";
195*0Sstevel@tonic-gate 		break;
196*0Sstevel@tonic-gate 	case EISDIR:
197*0Sstevel@tonic-gate 		result = "Is a directory";
198*0Sstevel@tonic-gate 		break;
199*0Sstevel@tonic-gate 	case EINVAL:
200*0Sstevel@tonic-gate 		result = "Invalid argument";
201*0Sstevel@tonic-gate 		break;
202*0Sstevel@tonic-gate 	case EFBIG:
203*0Sstevel@tonic-gate 		result = "File too large";
204*0Sstevel@tonic-gate 		break;
205*0Sstevel@tonic-gate 	case ENOSPC:
206*0Sstevel@tonic-gate 		result = "No space left on device";
207*0Sstevel@tonic-gate 		break;
208*0Sstevel@tonic-gate 	case EROFS:
209*0Sstevel@tonic-gate 		result = "Read-only file system";
210*0Sstevel@tonic-gate 		break;
211*0Sstevel@tonic-gate 	case EDQUOT:
212*0Sstevel@tonic-gate 		result = "Disc quota exceeded";
213*0Sstevel@tonic-gate 		break;
214*0Sstevel@tonic-gate 	case ENOTEMPTY:
215*0Sstevel@tonic-gate 		result = "Directory not empty";
216*0Sstevel@tonic-gate 		break;
217*0Sstevel@tonic-gate 	case ESTALE:
218*0Sstevel@tonic-gate 		result = "Stale NFS file handle";
219*0Sstevel@tonic-gate 		break;
220*0Sstevel@tonic-gate 	case ENOMEM:
221*0Sstevel@tonic-gate 		result = "Not enough memory";
222*0Sstevel@tonic-gate 		break;
223*0Sstevel@tonic-gate 	default:
224*0Sstevel@tonic-gate 		result = NULL;
225*0Sstevel@tonic-gate 		break;
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	return (result);
229*0Sstevel@tonic-gate }
230