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