6 * Copyright 1987 by the Student Information Processing Board
7 * of the Massachusetts Institute of Technology
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose is hereby granted, provided that
11 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
12 * advertising or publicity pertaining to distribution of the software
13 * without specific, written prior permission. M.I.T. and the
14 * M.I.T. S.I.P.B. make no representations about the suitability of
15 * this software for any purpose. It is provided "as is" without
16 * express or implied warranty.
23 #ifdef HAVE_SYS_PRCTL_H
24 #include <sys/prctl.h>
26 #define PR_GET_DUMPABLE 3
28 #if (!defined(HAVE_PRCTL) && defined(linux))
29 #include <sys/syscall.h>
35 #include <sys/types.h>
38 #include "error_table.h"
41 static char buffer[25];
43 struct et_list * _et_list = (struct et_list *) NULL;
44 struct et_list * _et_dynamic_list = (struct et_list *) NULL;
47 const char * error_message (errcode_t code)
55 offset = (int) (code & ((1<<ERRCODE_RANGE)-1));
56 table_num = code - offset;
58 #ifdef HAS_SYS_ERRLIST
59 if (offset < sys_nerr)
60 return(sys_errlist[offset]);
64 cp = strerror(offset);
71 for (et = _et_list; et; et = et->next) {
72 if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) {
73 /* This is the right table */
74 if (et->table->n_msgs <= offset)
76 return(et->table->msgs[offset]);
79 for (et = _et_dynamic_list; et; et = et->next) {
80 if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) {
81 /* This is the right table */
82 if (et->table->n_msgs <= offset)
84 return(et->table->msgs[offset]);
88 strcpy (buffer, "Unknown code ");
90 strcat (buffer, error_table_name (table_num));
93 for (cp = buffer; *cp; cp++)
96 *cp++ = '0' + offset / 100;
100 if (started || offset >= 10) {
101 *cp++ = '0' + offset / 10;
104 *cp++ = '0' + offset;
110 * This routine will only return a value if the we are not running as
111 * a privileged process.
113 static char *safe_getenv(const char *arg)
115 if ((getuid() != geteuid()) || (getgid() != getegid()))
118 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
121 #if (defined(linux) && defined(SYS_prctl))
122 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
127 #ifdef HAVE___SECURE_GETENV
128 return __secure_getenv(arg);
134 #define DEBUG_INIT 0x8000
135 #define DEBUG_ADDREMOVE 0x0001
137 static int debug_mask = 0;
138 static FILE *debug_f = 0;
140 static void init_debug(void)
145 if (debug_mask & DEBUG_INIT)
148 dstr = getenv("COMERR_DEBUG");
150 debug_mask = strtoul(dstr, 0, 0);
152 fn = safe_getenv("COMERR_DEBUG_FILE");
154 debug_f = fopen(fn, "a");
156 debug_f = fopen("/dev/tty", "a");
160 debug_mask |= DEBUG_INIT;
164 * New interface provided by krb5's com_err library
166 errcode_t add_error_table(const struct error_table * et)
170 if (!(el = (struct et_list *) malloc(sizeof(struct et_list))))
174 el->next = _et_dynamic_list;
175 _et_dynamic_list = el;
178 if (debug_mask & DEBUG_ADDREMOVE)
179 fprintf(debug_f, "add_error_table: %s (0x%p)\n",
180 error_table_name(et->base),
187 * New interface provided by krb5's com_err library
189 errcode_t remove_error_table(const struct error_table * et)
191 struct et_list *el = _et_dynamic_list;
192 struct et_list *el2 = 0;
196 if (el->table->base == et->base) {
197 if (el2) /* Not the beginning of the list */
198 el2->next = el->next;
200 _et_dynamic_list = el->next;
202 if (debug_mask & DEBUG_ADDREMOVE)
204 "remove_error_table: %s (0x%p)\n",
205 error_table_name(et->base),
212 if (debug_mask & DEBUG_ADDREMOVE)
213 fprintf(debug_f, "remove_error_table FAILED: %s (0x%p)\n",
214 error_table_name(et->base),
220 * Variant of the interface provided by Heimdal's com_err library
223 add_to_error_table(struct et_list *new_table)
225 add_error_table(new_table->table);