17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This file contains code to support better NFS error messages.  Death to
297c478bd9Sstevel@tonic-gate  * integer codes in user error messages!
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * XXX Ideally this code should be more general and available to the entire
327c478bd9Sstevel@tonic-gate  * kernel (see RFE 1101936).  When this happens, this file can go away.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
36*4870e0a7SRichard PALO #include <sys/null.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* size of a temporary printf format buffer. */
437c478bd9Sstevel@tonic-gate #define	FMT_BUF_SIZE	1024
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static void expand_format_string(int, const char *, char *, int);
467c478bd9Sstevel@tonic-gate static char *nfs_strerror(int);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * nfs_perror: Works like printf (format string and variable args) except
507c478bd9Sstevel@tonic-gate  * that it will substitute an error message for a "%m" string (like
517c478bd9Sstevel@tonic-gate  * syslog), using the given errno value.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate void
nfs_perror(int error,char * fmt,...)557c478bd9Sstevel@tonic-gate nfs_perror(int error, char *fmt, ...)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	va_list ap;
587c478bd9Sstevel@tonic-gate 	char buf[FMT_BUF_SIZE];		/* massaged version of fmt */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	/* Expand %m */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	expand_format_string(error, fmt, buf, FMT_BUF_SIZE);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * Now pass the massaged format string and its arguments off to
667c478bd9Sstevel@tonic-gate 	 * printf.
677c478bd9Sstevel@tonic-gate 	 */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
707c478bd9Sstevel@tonic-gate 	(void) vzprintf(getzoneid(), buf, ap);
717c478bd9Sstevel@tonic-gate 	va_end(ap);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * nfs_cmn_err: Works like cmn_err (error level, format string, and
767c478bd9Sstevel@tonic-gate  * variable args) except that it will substitute an error message for a
777c478bd9Sstevel@tonic-gate  * "%m" string (like syslog), using the given errno value.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate void
nfs_cmn_err(int error,int level,char * fmt,...)817c478bd9Sstevel@tonic-gate nfs_cmn_err(int error, int level, char *fmt, ...)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	va_list ap;
847c478bd9Sstevel@tonic-gate 	char buf[FMT_BUF_SIZE];		/* massaged version of fmt */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/* Expand %m */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	expand_format_string(error, fmt, buf, FMT_BUF_SIZE);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * Now pass the massaged format string and its arguments off to
927c478bd9Sstevel@tonic-gate 	 * cmn_err.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
967c478bd9Sstevel@tonic-gate 	(void) vzcmn_err(getzoneid(), level, buf, ap);
977c478bd9Sstevel@tonic-gate 	va_end(ap);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * expand_format_string: copy the printf format string from "fmt" to "buf",
1027c478bd9Sstevel@tonic-gate  * expanding %m to the error string for "error".
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static void
expand_format_string(int error,const char * fmt,char * buf,int buf_chars)1067c478bd9Sstevel@tonic-gate expand_format_string(int error, const char *fmt, char *buf, int buf_chars)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	const char *from;		/* pointer into fmt */
1097c478bd9Sstevel@tonic-gate 	char *to;			/* pointer into buf */
1107c478bd9Sstevel@tonic-gate 	char *errmsg;			/* expansion for %m */
1117c478bd9Sstevel@tonic-gate 	char *trunc_msg = "Truncated NFS error message: ";
1127c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * Copy the given format string into the result buffer, expanding
1167c478bd9Sstevel@tonic-gate 	 * %m as we go.  If the result buffer is too short, complain and
1177c478bd9Sstevel@tonic-gate 	 * truncate the message.  (We don't expect this to ever happen,
1187c478bd9Sstevel@tonic-gate 	 * though.)
1197c478bd9Sstevel@tonic-gate 	 */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	for (from = fmt, to = buf; *from; from++) {
1227c478bd9Sstevel@tonic-gate 		if (to >= buf + buf_chars - 1) {
1237c478bd9Sstevel@tonic-gate 			zprintf(zoneid, trunc_msg);
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		}
1267c478bd9Sstevel@tonic-gate 		if (*from == '%' && *(from+1) == 'm') {
1277c478bd9Sstevel@tonic-gate 			errmsg = nfs_strerror(error);
1287c478bd9Sstevel@tonic-gate 			/*
1297c478bd9Sstevel@tonic-gate 			 * If there's an error message and room to display
1307c478bd9Sstevel@tonic-gate 			 * it, copy it in.  If there's no message or not
1317c478bd9Sstevel@tonic-gate 			 * enough room, try just printing an error number.
1327c478bd9Sstevel@tonic-gate 			 * (We assume that the error value is in a
1337c478bd9Sstevel@tonic-gate 			 * reasonable range.)  If there's no room for
1347c478bd9Sstevel@tonic-gate 			 * anything, bail out.
1357c478bd9Sstevel@tonic-gate 			 */
1367c478bd9Sstevel@tonic-gate 			if (errmsg != NULL &&
1377c478bd9Sstevel@tonic-gate 			    strlen(buf) + strlen(errmsg) < buf_chars) {
1387c478bd9Sstevel@tonic-gate 				(void) strcpy(to, errmsg);
1397c478bd9Sstevel@tonic-gate 				to += strlen(errmsg);
1407c478bd9Sstevel@tonic-gate 			} else if (strlen(buf) + strlen("error XXX") <
1417c478bd9Sstevel@tonic-gate 			    buf_chars) {
1427c478bd9Sstevel@tonic-gate 				(void) sprintf(to, "error %d", error);
1437c478bd9Sstevel@tonic-gate 				/*
1447c478bd9Sstevel@tonic-gate 				 * Don't try to guess how many characters
1457c478bd9Sstevel@tonic-gate 				 * were laid down.
1467c478bd9Sstevel@tonic-gate 				 */
1477c478bd9Sstevel@tonic-gate 				to = buf + strlen(buf);
1487c478bd9Sstevel@tonic-gate 			} else {
1497c478bd9Sstevel@tonic-gate 				zprintf(zoneid, trunc_msg);
1507c478bd9Sstevel@tonic-gate 				break;
1517c478bd9Sstevel@tonic-gate 			}
1527c478bd9Sstevel@tonic-gate 			from++;
1537c478bd9Sstevel@tonic-gate 		} else {
1547c478bd9Sstevel@tonic-gate 			*to++ = *from;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	*to = '\0';
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * nfs_strerror: map an errno value to a string.  Not all possible errno
1627c478bd9Sstevel@tonic-gate  * values are supported.
1637c478bd9Sstevel@tonic-gate  *
1647c478bd9Sstevel@tonic-gate  * If there is no string for the given errno value, return NULL.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static char *
nfs_strerror(int errcode)1687c478bd9Sstevel@tonic-gate nfs_strerror(int errcode)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	char *result;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	switch (errcode) {
1737c478bd9Sstevel@tonic-gate 	case EPERM:
1747c478bd9Sstevel@tonic-gate 		result = "Not owner";
1757c478bd9Sstevel@tonic-gate 		break;
1767c478bd9Sstevel@tonic-gate 	case ENOENT:
1777c478bd9Sstevel@tonic-gate 		result = "No such file or directory";
1787c478bd9Sstevel@tonic-gate 		break;
1797c478bd9Sstevel@tonic-gate 	case EIO:
1807c478bd9Sstevel@tonic-gate 		result = "I/O error";
1817c478bd9Sstevel@tonic-gate 		break;
1827c478bd9Sstevel@tonic-gate 	case EACCES:
1837c478bd9Sstevel@tonic-gate 		result = "Permission denied";
1847c478bd9Sstevel@tonic-gate 		break;
1857c478bd9Sstevel@tonic-gate 	case EEXIST:
1867c478bd9Sstevel@tonic-gate 		result = "File exists";
1877c478bd9Sstevel@tonic-gate 		break;
1887c478bd9Sstevel@tonic-gate 	case ENOTDIR:
1897c478bd9Sstevel@tonic-gate 		result = "Not a directory";
1907c478bd9Sstevel@tonic-gate 		break;
1917c478bd9Sstevel@tonic-gate 	case EISDIR:
1927c478bd9Sstevel@tonic-gate 		result = "Is a directory";
1937c478bd9Sstevel@tonic-gate 		break;
1947c478bd9Sstevel@tonic-gate 	case EINVAL:
1957c478bd9Sstevel@tonic-gate 		result = "Invalid argument";
1967c478bd9Sstevel@tonic-gate 		break;
1977c478bd9Sstevel@tonic-gate 	case EFBIG:
1987c478bd9Sstevel@tonic-gate 		result = "File too large";
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 	case ENOSPC:
2017c478bd9Sstevel@tonic-gate 		result = "No space left on device";
2027c478bd9Sstevel@tonic-gate 		break;
2037c478bd9Sstevel@tonic-gate 	case EROFS:
2047c478bd9Sstevel@tonic-gate 		result = "Read-only file system";
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 	case EDQUOT:
2077c478bd9Sstevel@tonic-gate 		result = "Disc quota exceeded";
2087c478bd9Sstevel@tonic-gate 		break;
2097c478bd9Sstevel@tonic-gate 	case ENOTEMPTY:
2107c478bd9Sstevel@tonic-gate 		result = "Directory not empty";
2117c478bd9Sstevel@tonic-gate 		break;
2127c478bd9Sstevel@tonic-gate 	case ESTALE:
2137c478bd9Sstevel@tonic-gate 		result = "Stale NFS file handle";
2147c478bd9Sstevel@tonic-gate 		break;
2157c478bd9Sstevel@tonic-gate 	case ENOMEM:
2167c478bd9Sstevel@tonic-gate 		result = "Not enough memory";
2177c478bd9Sstevel@tonic-gate 		break;
2187c478bd9Sstevel@tonic-gate 	default:
2197c478bd9Sstevel@tonic-gate 		result = NULL;
2207c478bd9Sstevel@tonic-gate 		break;
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	return (result);
2247c478bd9Sstevel@tonic-gate }
225