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