]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/mach/mach_error_string.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libsyscall / mach / mach_error_string.c
CommitLineData
2d21ac55
A
1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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.
0a7de745 25 *
2d21ac55
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0a7de745 31/*
2d21ac55
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
0a7de745 35 *
2d21ac55
A
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.
0a7de745 41 *
2d21ac55
A
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.
0a7de745 45 *
2d21ac55 46 * Carnegie Mellon requests users of this software to return to
0a7de745 47 *
2d21ac55
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
0a7de745 52 *
2d21ac55
A
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
66static void do_compat(mach_error_t *);
67
68static void
69do_compat(mach_error_t *org_err)
70{
0a7de745 71 mach_error_t err = *org_err;
2d21ac55 72
0a7de745
A
73 /*
74 * map old error numbers to
75 * to new error sys & subsystem
2d21ac55
A
76 */
77
0a7de745 78 if ((-200 < err) && (err <= -100)) {
2d21ac55 79 err = -(err + 100) | IPC_SEND_MOD;
0a7de745 80 } else if ((-300 < err) && (err <= -200)) {
2d21ac55 81 err = -(err + 200) | IPC_RCV_MOD;
0a7de745 82 } else if ((-400 < err) && (err <= -300)) {
2d21ac55 83 err = -(err + 300) | MACH_IPC_MIG_MOD;
0a7de745 84 } else if ((1000 <= err) && (err < 1100)) {
2d21ac55 85 err = (err - 1000) | SERV_NETNAME_MOD;
0a7de745 86 } else if ((1600 <= err) && (err < 1700)) {
2d21ac55 87 err = (err - 1600) | SERV_ENV_MOD;
0a7de745 88 } else if ((27600 <= err) && (err < 27700)) {
2d21ac55 89 err = (err - 27600) | SERV_EXECD_MOD;
0a7de745 90 }
2d21ac55
A
91
92 *org_err = err;
93}
94
95static int
96err_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
115char *
116mach_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
0a7de745 127 if (system <= err_max_system && sys_p->map_table) {
2d21ac55 128 sub = err_sparse_mapit(sub, sys_p->map_table, sys_p->map_count);
0a7de745 129 }
2d21ac55 130
0a7de745
A
131 if (system > err_max_system || sub >= sys_p->max_sub) {
132 return (char *)"(?/?)";
133 }
2d21ac55
A
134 return (char *) (sys_p->subsystem[sub].subsys_name);
135}
136
137boolean_t mach_error_full_diag = FALSE;
138
139__private_extern__ char *
140mach_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
0a7de745
A
155 if (system > err_max_system) {
156 return (char *)"(?/?) unknown error system";
157 } else if (sys_p->map_table) {
2d21ac55 158 sub = err_sparse_mapit(sub, sys_p->map_table, sys_p->map_count);
0a7de745 159 }
2d21ac55 160
0a7de745
A
161 if (sub >= sys_p->max_sub) {
162 return (char *)sys_p->bad_sub;
163 }
2d21ac55
A
164
165 sub_p = &sys_p->subsystem[sub];
0a7de745 166 if (sub_p->map_table) {
2d21ac55 167 code = err_sparse_mapit(code, sub_p->map_table, sub_p->map_count);
0a7de745
A
168 }
169 if (code >= sub_p->max_code) {
170 return (char *)NO_SUCH_ERROR;
171 }
2d21ac55
A
172
173 *diag = mach_error_full_diag;
0a7de745 174 return (char *)sub_p->codes[code];
2d21ac55
A
175}
176
177char *
178mach_error_string(mach_error_t err)
179{
180 boolean_t diag;
181
182 return mach_error_string_int( err, &diag );
2d21ac55
A
183}
184
185/* vim: set ts=4: */