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
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 	*org_err = err;
92 }
93 
94 static int
95 err_sparse_mapit(int old, const struct error_sparse_map *map_table, int mapcnt)
96 {
97 	boolean_t found = FALSE;
98 	int ret = 0, i;
99 
100 	for (i = 0; i < mapcnt; i++) {
101 		struct error_sparse_map entry = map_table[i];
102 
103 		if (entry.start <= old && old <= entry.end) {
104 			ret += old - entry.start;
105 			found = TRUE;
106 			break;
107 		}
108 		ret += entry.end - entry.start + 1;
109 	}
110 
111 	return (found)? ret : INT_MAX;
112 }
113 
114 char *
115 mach_error_type(mach_error_t err)
116 {
117 	const struct error_system *sys_p;
118 	int sub, system;
119 
120 	do_compat( &err );
121 
122 	system = err_get_system(err);
123 	sys_p = &_mach_errors[system];
124 	sub = err_get_sub(err);
125 
126 	if (system <= err_max_system && sys_p->map_table)
127 		sub = err_sparse_mapit(sub, sys_p->map_table, sys_p->map_count);
128 
129 	if (system > err_max_system || sub >= sys_p->max_sub)
130 	    return((char *)"(?/?)");
131 	return (char *) (sys_p->subsystem[sub].subsys_name);
132 }
133 
134 boolean_t mach_error_full_diag = FALSE;
135 
136 __private_extern__ char *
137 mach_error_string_int(mach_error_t err, boolean_t *diag)
138 {
139 	const struct error_system *sys_p;
140 	const struct error_subsystem *sub_p;
141 	int sub, system, code;
142 
143 	do_compat( &err );
144 
145 	system = err_get_system(err);
146 	sys_p = &_mach_errors[system];
147 	sub = err_get_sub(err);
148 	code = err_get_code(err);
149 
150 	*diag = TRUE;
151 
152 	if (system > err_max_system)
153 	    return((char *)"(?/?) unknown error system");
154 	else if (sys_p->map_table)
155 		sub = err_sparse_mapit(sub, sys_p->map_table, sys_p->map_count);
156 
157 	if (sub >= sys_p->max_sub)
158 	    return((char *)sys_p->bad_sub);
159 
160 	sub_p = &sys_p->subsystem[sub];
161 	if (sub_p->map_table)
162 		code = err_sparse_mapit(code, sub_p->map_table, sub_p->map_count);
163 	if (code >= sub_p->max_code)
164 	    return ((char *)NO_SUCH_ERROR);
165 
166 	*diag = mach_error_full_diag;
167 	return( (char *)sub_p->codes[code] );
168 }
169 
170 char *
171 mach_error_string(mach_error_t err)
172 {
173 	boolean_t diag;
174 
175 	return mach_error_string_int( err, &diag );
176 }
177 
178 /* vim: set ts=4: */
179