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.
24 #ifdef HAVE_SYS_PRCTL_H
25 #include <sys/prctl.h>
27 #define PR_GET_DUMPABLE 3
29 #if (!defined(HAVE_PRCTL) && defined(linux))
30 #include <sys/syscall.h>
32 #ifdef HAVE_SEMAPHORE_H
33 #include <semaphore.h>
42 #include <sys/types.h>
45 #include "error_table.h"
49 #define THREAD_LOCAL static TLS
51 #define THREAD_LOCAL static
54 THREAD_LOCAL char buffer[25];
56 struct et_list * _et_list = (struct et_list *) NULL;
57 struct et_list * _et_dynamic_list = (struct et_list *) NULL;
60 #define COMERR_ATTR(x) __attribute__(x)
62 #define COMERR_ATTR(x)
66 static sem_t _et_lock;
67 static int _et_lock_initialized;
69 static void COMERR_ATTR((constructor)) setup_et_lock(void)
71 sem_init(&_et_lock, 0, 1);
72 _et_lock_initialized = 1;
75 static void COMERR_ATTR((destructor)) fini_et_lock(void)
77 sem_destroy(&_et_lock);
78 _et_lock_initialized = 0;
83 int et_list_lock(void)
86 if (!_et_lock_initialized)
88 return sem_wait(&_et_lock);
94 int et_list_unlock(void)
97 if (_et_lock_initialized)
98 return sem_post(&_et_lock);
103 typedef char *(*gettextf) (const char *);
105 static gettextf com_err_gettext = NULL;
107 gettextf set_com_err_gettext(gettextf new_proc)
109 gettextf x = com_err_gettext;
111 com_err_gettext = new_proc;
117 #define SYS_ERR_BASE 0x40000000
119 #define SYS_ERR_BASE 0
122 const char * error_message (errcode_t code)
130 offset = (int) (code & ((1<<ERRCODE_RANGE)-1));
131 table_num = code - offset;
132 if (table_num == SYS_ERR_BASE) {
133 #ifdef HAS_SYS_ERRLIST
135 return(sys_errlist[code]);
147 for (et = _et_list; et; et = et->next) {
148 if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) {
149 /* This is the right table */
150 if (et->table->n_msgs <= offset) {
153 const char *msg = et->table->msgs[offset];
156 return (*com_err_gettext)(msg);
162 for (et = _et_dynamic_list; et; et = et->next) {
163 if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) {
164 /* This is the right table */
165 if (et->table->n_msgs <= offset) {
168 const char *msg = et->table->msgs[offset];
171 return (*com_err_gettext)(msg);
179 strcpy (buffer, "Unknown code ");
181 strcat (buffer, error_table_name (table_num));
182 strcat (buffer, " ");
184 for (cp = buffer; *cp; cp++)
187 *cp++ = '0' + offset / 100;
191 if (started || offset >= 10) {
192 *cp++ = '0' + offset / 10;
195 *cp++ = '0' + offset;
201 * This routine will only return a value if the we are not running as
202 * a privileged process.
204 static char *safe_getenv(const char *arg)
207 if ((getuid() != geteuid()) || (getgid() != getegid()))
211 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
214 #if (defined(linux) && defined(SYS_prctl))
215 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
220 #if defined(HAVE_SECURE_GETENV)
221 return secure_getenv(arg);
222 #elif defined(HAVE___SECURE_GETENV)
223 return __secure_getenv(arg);
229 #define DEBUG_INIT 0x8000
230 #define DEBUG_ADDREMOVE 0x0001
232 static int debug_mask = 0;
233 static FILE *debug_f = 0;
235 static void init_debug(void)
237 char *dstr, *fn, *tmp;
239 if (debug_mask & DEBUG_INIT)
242 dstr = getenv("COMERR_DEBUG");
244 debug_mask = strtoul(dstr, &tmp, 0);
249 debug_mask |= DEBUG_INIT;
250 if (debug_mask == DEBUG_INIT)
253 fn = safe_getenv("COMERR_DEBUG_FILE");
255 debug_f = fopen(fn, "a");
257 debug_f = fopen("/dev/tty", "a");
260 int fd = fileno(debug_f);
263 int flags = fcntl(fd, F_GETFD);
266 flags = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
268 fprintf(debug_f, "Couldn't set FD_CLOEXEC "
269 "on debug FILE: %s\n", strerror(errno));
272 debug_mask = DEBUG_INIT;
277 debug_mask = DEBUG_INIT;
281 * New interface provided by krb5's com_err library
283 errcode_t add_error_table(const struct error_table * et)
287 if (!(el = (struct et_list *) malloc(sizeof(struct et_list))))
290 if (et_list_lock() != 0) {
296 el->next = _et_dynamic_list;
297 _et_dynamic_list = el;
300 if (debug_mask & DEBUG_ADDREMOVE)
301 fprintf(debug_f, "add_error_table: %s (0x%p)\n",
302 error_table_name(et->base),
310 * New interface provided by krb5's com_err library
312 errcode_t remove_error_table(const struct error_table * et)
315 struct et_list *el2 = 0;
317 if (et_list_lock() != 0)
320 el = _et_dynamic_list;
323 if (el->table->base == et->base) {
324 if (el2) /* Not the beginning of the list */
325 el2->next = el->next;
327 _et_dynamic_list = el->next;
329 if (debug_mask & DEBUG_ADDREMOVE)
331 "remove_error_table: %s (0x%p)\n",
332 error_table_name(et->base),
340 if (debug_mask & DEBUG_ADDREMOVE)
341 fprintf(debug_f, "remove_error_table FAILED: %s (0x%p)\n",
342 error_table_name(et->base),
349 * Variant of the interface provided by Heimdal's com_err library
352 add_to_error_table(struct et_list *new_table)
354 add_error_table(new_table->table);