xref: /xnu-10002.81.5/libsyscall/mach/mach_error_string.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions /*
29*5e3eaea3SApple OSS Distributions  * @OSF_COPYRIGHT@
30*5e3eaea3SApple OSS Distributions  */
31*5e3eaea3SApple OSS Distributions /*
32*5e3eaea3SApple OSS Distributions  * Mach Operating System
33*5e3eaea3SApple OSS Distributions  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34*5e3eaea3SApple OSS Distributions  * All Rights Reserved.
35*5e3eaea3SApple OSS Distributions  *
36*5e3eaea3SApple OSS Distributions  * Permission to use, copy, modify and distribute this software and its
37*5e3eaea3SApple OSS Distributions  * documentation is hereby granted, provided that both the copyright
38*5e3eaea3SApple OSS Distributions  * notice and this permission notice appear in all copies of the
39*5e3eaea3SApple OSS Distributions  * software, derivative works or modified versions, and any portions
40*5e3eaea3SApple OSS Distributions  * thereof, and that both notices appear in supporting documentation.
41*5e3eaea3SApple OSS Distributions  *
42*5e3eaea3SApple OSS Distributions  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43*5e3eaea3SApple OSS Distributions  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44*5e3eaea3SApple OSS Distributions  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45*5e3eaea3SApple OSS Distributions  *
46*5e3eaea3SApple OSS Distributions  * Carnegie Mellon requests users of this software to return to
47*5e3eaea3SApple OSS Distributions  *
48*5e3eaea3SApple OSS Distributions  *  Software Distribution Coordinator  or  [email protected]
49*5e3eaea3SApple OSS Distributions  *  School of Computer Science
50*5e3eaea3SApple OSS Distributions  *  Carnegie Mellon University
51*5e3eaea3SApple OSS Distributions  *  Pittsburgh PA 15213-3890
52*5e3eaea3SApple OSS Distributions  *
53*5e3eaea3SApple OSS Distributions  * any improvements or extensions that they make and grant Carnegie Mellon
54*5e3eaea3SApple OSS Distributions  * the rights to redistribute these changes.
55*5e3eaea3SApple OSS Distributions  */
56*5e3eaea3SApple OSS Distributions 
57*5e3eaea3SApple OSS Distributions #include <limits.h>
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions #include <mach/boolean.h>
60*5e3eaea3SApple OSS Distributions #include <mach/error.h>
61*5e3eaea3SApple OSS Distributions #include <mach/mach_error.h>
62*5e3eaea3SApple OSS Distributions 
63*5e3eaea3SApple OSS Distributions #include "errorlib.h"
64*5e3eaea3SApple OSS Distributions #include "externs.h"
65*5e3eaea3SApple OSS Distributions 
66*5e3eaea3SApple OSS Distributions static void do_compat(mach_error_t *);
67*5e3eaea3SApple OSS Distributions 
68*5e3eaea3SApple OSS Distributions static void
do_compat(mach_error_t * org_err)69*5e3eaea3SApple OSS Distributions do_compat(mach_error_t *org_err)
70*5e3eaea3SApple OSS Distributions {
71*5e3eaea3SApple OSS Distributions 	mach_error_t            err = *org_err;
72*5e3eaea3SApple OSS Distributions 
73*5e3eaea3SApple OSS Distributions 	/*
74*5e3eaea3SApple OSS Distributions 	 * map old error numbers to
75*5e3eaea3SApple OSS Distributions 	 * to new error sys & subsystem
76*5e3eaea3SApple OSS Distributions 	 */
77*5e3eaea3SApple OSS Distributions 
78*5e3eaea3SApple OSS Distributions 	if ((-200 < err) && (err <= -100)) {
79*5e3eaea3SApple OSS Distributions 		err = -(err + 100) | IPC_SEND_MOD;
80*5e3eaea3SApple OSS Distributions 	} else if ((-300 < err) && (err <= -200)) {
81*5e3eaea3SApple OSS Distributions 		err = -(err + 200) | IPC_RCV_MOD;
82*5e3eaea3SApple OSS Distributions 	} else if ((-400 < err) && (err <= -300)) {
83*5e3eaea3SApple OSS Distributions 		err = -(err + 300) | MACH_IPC_MIG_MOD;
84*5e3eaea3SApple OSS Distributions 	} else if ((1000 <= err) && (err < 1100)) {
85*5e3eaea3SApple OSS Distributions 		err = (err - 1000) | SERV_NETNAME_MOD;
86*5e3eaea3SApple OSS Distributions 	} else if ((1600 <= err) && (err < 1700)) {
87*5e3eaea3SApple OSS Distributions 		err = (err - 1600) | SERV_ENV_MOD;
88*5e3eaea3SApple OSS Distributions 	} else if ((27600 <= err) && (err < 27700)) {
89*5e3eaea3SApple OSS Distributions 		err = (err - 27600) | SERV_EXECD_MOD;
90*5e3eaea3SApple OSS Distributions 	}
91*5e3eaea3SApple OSS Distributions 
92*5e3eaea3SApple OSS Distributions 	*org_err = err;
93*5e3eaea3SApple OSS Distributions }
94*5e3eaea3SApple OSS Distributions 
95*5e3eaea3SApple OSS Distributions static int
err_sparse_mapit(int old,const struct error_sparse_map * map_table,int mapcnt)96*5e3eaea3SApple OSS Distributions err_sparse_mapit(int old, const struct error_sparse_map *map_table, int mapcnt)
97*5e3eaea3SApple OSS Distributions {
98*5e3eaea3SApple OSS Distributions 	boolean_t found = FALSE;
99*5e3eaea3SApple OSS Distributions 	int ret = 0, i;
100*5e3eaea3SApple OSS Distributions 
101*5e3eaea3SApple OSS Distributions 	for (i = 0; i < mapcnt; i++) {
102*5e3eaea3SApple OSS Distributions 		struct error_sparse_map entry = map_table[i];
103*5e3eaea3SApple OSS Distributions 
104*5e3eaea3SApple OSS Distributions 		if (entry.start <= old && old <= entry.end) {
105*5e3eaea3SApple OSS Distributions 			ret += old - entry.start;
106*5e3eaea3SApple OSS Distributions 			found = TRUE;
107*5e3eaea3SApple OSS Distributions 			break;
108*5e3eaea3SApple OSS Distributions 		}
109*5e3eaea3SApple OSS Distributions 		ret += entry.end - entry.start + 1;
110*5e3eaea3SApple OSS Distributions 	}
111*5e3eaea3SApple OSS Distributions 
112*5e3eaea3SApple OSS Distributions 	return (found)? ret : INT_MAX;
113*5e3eaea3SApple OSS Distributions }
114*5e3eaea3SApple OSS Distributions 
115*5e3eaea3SApple OSS Distributions char *
mach_error_type(mach_error_t err)116*5e3eaea3SApple OSS Distributions mach_error_type(mach_error_t err)
117*5e3eaea3SApple OSS Distributions {
118*5e3eaea3SApple OSS Distributions 	const struct error_system *sys_p;
119*5e3eaea3SApple OSS Distributions 	int sub, system;
120*5e3eaea3SApple OSS Distributions 
121*5e3eaea3SApple OSS Distributions 	do_compat( &err );
122*5e3eaea3SApple OSS Distributions 
123*5e3eaea3SApple OSS Distributions 	system = err_get_system(err);
124*5e3eaea3SApple OSS Distributions 	sys_p = &_mach_errors[system];
125*5e3eaea3SApple OSS Distributions 	sub = err_get_sub(err);
126*5e3eaea3SApple OSS Distributions 
127*5e3eaea3SApple OSS Distributions 	if (system <= err_max_system && sys_p->map_table) {
128*5e3eaea3SApple OSS Distributions 		sub = err_sparse_mapit(sub, sys_p->map_table, sys_p->map_count);
129*5e3eaea3SApple OSS Distributions 	}
130*5e3eaea3SApple OSS Distributions 
131*5e3eaea3SApple OSS Distributions 	if (system > err_max_system || sub >= sys_p->max_sub) {
132*5e3eaea3SApple OSS Distributions 		return (char *)"(?/?)";
133*5e3eaea3SApple OSS Distributions 	}
134*5e3eaea3SApple OSS Distributions 	return (char *) (sys_p->subsystem[sub].subsys_name);
135*5e3eaea3SApple OSS Distributions }
136*5e3eaea3SApple OSS Distributions 
137*5e3eaea3SApple OSS Distributions boolean_t mach_error_full_diag = FALSE;
138*5e3eaea3SApple OSS Distributions 
139*5e3eaea3SApple OSS Distributions __private_extern__ char *
mach_error_string_int(mach_error_t err,boolean_t * diag)140*5e3eaea3SApple OSS Distributions mach_error_string_int(mach_error_t err, boolean_t *diag)
141*5e3eaea3SApple OSS Distributions {
142*5e3eaea3SApple OSS Distributions 	const struct error_system *sys_p;
143*5e3eaea3SApple OSS Distributions 	const struct error_subsystem *sub_p;
144*5e3eaea3SApple OSS Distributions 	int sub, system, code;
145*5e3eaea3SApple OSS Distributions 
146*5e3eaea3SApple OSS Distributions 	do_compat( &err );
147*5e3eaea3SApple OSS Distributions 
148*5e3eaea3SApple OSS Distributions 	system = err_get_system(err);
149*5e3eaea3SApple OSS Distributions 	sys_p = &_mach_errors[system];
150*5e3eaea3SApple OSS Distributions 	sub = err_get_sub(err);
151*5e3eaea3SApple OSS Distributions 	code = err_get_code(err);
152*5e3eaea3SApple OSS Distributions 
153*5e3eaea3SApple OSS Distributions 	*diag = TRUE;
154*5e3eaea3SApple OSS Distributions 
155*5e3eaea3SApple OSS Distributions 	if (system > err_max_system) {
156*5e3eaea3SApple OSS Distributions 		return (char *)"(?/?) unknown error system";
157*5e3eaea3SApple OSS Distributions 	} else if (sys_p->map_table) {
158*5e3eaea3SApple OSS Distributions 		sub = err_sparse_mapit(sub, sys_p->map_table, sys_p->map_count);
159*5e3eaea3SApple OSS Distributions 	}
160*5e3eaea3SApple OSS Distributions 
161*5e3eaea3SApple OSS Distributions 	if (sub >= sys_p->max_sub) {
162*5e3eaea3SApple OSS Distributions 		return (char *)sys_p->bad_sub;
163*5e3eaea3SApple OSS Distributions 	}
164*5e3eaea3SApple OSS Distributions 
165*5e3eaea3SApple OSS Distributions 	sub_p = &sys_p->subsystem[sub];
166*5e3eaea3SApple OSS Distributions 	if (sub_p->map_table) {
167*5e3eaea3SApple OSS Distributions 		code = err_sparse_mapit(code, sub_p->map_table, sub_p->map_count);
168*5e3eaea3SApple OSS Distributions 	}
169*5e3eaea3SApple OSS Distributions 	if (code >= sub_p->max_code) {
170*5e3eaea3SApple OSS Distributions 		return (char *)NO_SUCH_ERROR;
171*5e3eaea3SApple OSS Distributions 	}
172*5e3eaea3SApple OSS Distributions 
173*5e3eaea3SApple OSS Distributions 	*diag = mach_error_full_diag;
174*5e3eaea3SApple OSS Distributions 	return (char *)sub_p->codes[code];
175*5e3eaea3SApple OSS Distributions }
176*5e3eaea3SApple OSS Distributions 
177*5e3eaea3SApple OSS Distributions char *
mach_error_string(mach_error_t err)178*5e3eaea3SApple OSS Distributions mach_error_string(mach_error_t err)
179*5e3eaea3SApple OSS Distributions {
180*5e3eaea3SApple OSS Distributions 	boolean_t diag;
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions 	return mach_error_string_int( err, &diag );
183*5e3eaea3SApple OSS Distributions }
184*5e3eaea3SApple OSS Distributions 
185*5e3eaea3SApple OSS Distributions /* vim: set ts=4: */
186