Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * $Header$ |
| 3 | * $Source$ |
| 4 | * $Locker$ |
| 5 | * |
| 6 | * Copyright 1987 by the Student Information Processing Board |
| 7 | * of the Massachusetts Institute of Technology |
| 8 | * |
Theodore Ts'o | 06cefee | 1999-10-23 01:16:22 +0000 | [diff] [blame] | 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. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 19 | #include "config.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 21 | #include <stdlib.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 22 | #include <string.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 23 | #include <errno.h> |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 24 | #ifdef HAVE_SYS_PRCTL_H |
| 25 | #include <sys/prctl.h> |
| 26 | #else |
| 27 | #define PR_GET_DUMPABLE 3 |
| 28 | #endif |
| 29 | #if (!defined(HAVE_PRCTL) && defined(linux)) |
| 30 | #include <sys/syscall.h> |
| 31 | #endif |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 32 | #ifdef HAVE_SEMAPHORE_H |
| 33 | #include <semaphore.h> |
| 34 | #endif |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 35 | #if HAVE_UNISTD_H |
| 36 | #include <unistd.h> |
| 37 | #endif |
Theodore Ts'o | 902be4a | 2008-10-01 20:45:26 -0400 | [diff] [blame] | 38 | #include <fcntl.h> |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 39 | #if HAVE_SYS_TYPES_H |
| 40 | #include <sys/types.h> |
| 41 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 42 | #include "com_err.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | #include "error_table.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 44 | #include "internal.h" |
| 45 | |
Theodore Ts'o | 47526e3 | 2008-02-09 23:39:27 -0500 | [diff] [blame] | 46 | #ifdef TLS |
| 47 | #define THREAD_LOCAL static TLS |
| 48 | #else |
| 49 | #define THREAD_LOCAL static |
| 50 | #endif |
| 51 | |
| 52 | THREAD_LOCAL char buffer[25]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 53 | |
| 54 | struct et_list * _et_list = (struct et_list *) NULL; |
Theodore Ts'o | d51b819 | 2005-06-20 17:59:06 -0400 | [diff] [blame] | 55 | struct et_list * _et_dynamic_list = (struct et_list *) NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 56 | |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 57 | #ifdef __GNUC__ |
| 58 | #define COMERR_ATTR(x) __attribute__(x) |
| 59 | #else |
| 60 | #define COMERR_ATTR(x) |
| 61 | #endif |
| 62 | |
| 63 | #ifdef HAVE_SEM_INIT |
| 64 | static sem_t _et_lock; |
Theodore Ts'o | 98e9fb9 | 2008-10-02 08:51:27 -0400 | [diff] [blame] | 65 | static int _et_lock_initialized; |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 66 | |
Theodore Ts'o | 98e9fb9 | 2008-10-02 08:51:27 -0400 | [diff] [blame] | 67 | static void COMERR_ATTR((constructor)) setup_et_lock(void) |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 68 | { |
| 69 | sem_init(&_et_lock, 0, 1); |
| 70 | _et_lock_initialized = 1; |
| 71 | } |
| 72 | |
Theodore Ts'o | 98e9fb9 | 2008-10-02 08:51:27 -0400 | [diff] [blame] | 73 | static void COMERR_ATTR((destructor)) fini_et_lock(void) |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 74 | { |
| 75 | sem_destroy(&_et_lock); |
| 76 | _et_lock_initialized = 0; |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | |
Theodore Ts'o | 98e9fb9 | 2008-10-02 08:51:27 -0400 | [diff] [blame] | 81 | int et_list_lock(void) |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 82 | { |
| 83 | #ifdef HAVE_SEM_INIT |
| 84 | if (!_et_lock_initialized) |
| 85 | setup_et_lock(); |
| 86 | return sem_wait(&_et_lock); |
| 87 | #else |
| 88 | return 0; |
| 89 | #endif |
| 90 | } |
| 91 | |
Theodore Ts'o | 98e9fb9 | 2008-10-02 08:51:27 -0400 | [diff] [blame] | 92 | int et_list_unlock(void) |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 93 | { |
| 94 | #ifdef HAVE_SEM_INIT |
| 95 | if (_et_lock_initialized) |
| 96 | return sem_post(&_et_lock); |
| 97 | #endif |
| 98 | return 0; |
| 99 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 100 | |
Theodore Ts'o | 4259052 | 2011-10-05 00:58:40 -0400 | [diff] [blame] | 101 | typedef char *(*gettextf) (const char *); |
| 102 | |
Theodore Ts'o | 299a1e8 | 2011-10-05 03:03:44 -0400 | [diff] [blame^] | 103 | static gettextf com_err_gettext = NULL; |
Theodore Ts'o | 4259052 | 2011-10-05 00:58:40 -0400 | [diff] [blame] | 104 | |
| 105 | gettextf set_com_err_gettext (new_proc) |
| 106 | gettextf new_proc; |
| 107 | { |
| 108 | gettextf x = com_err_gettext; |
| 109 | |
| 110 | com_err_gettext = new_proc; |
| 111 | |
| 112 | return x; |
| 113 | } |
| 114 | |
| 115 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 116 | const char * error_message (errcode_t code) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 117 | { |
| 118 | int offset; |
| 119 | struct et_list *et; |
Theodore Ts'o | a47b66e | 1997-08-10 23:02:21 +0000 | [diff] [blame] | 120 | errcode_t table_num; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 121 | int started = 0; |
| 122 | char *cp; |
| 123 | |
Theodore Ts'o | a47b66e | 1997-08-10 23:02:21 +0000 | [diff] [blame] | 124 | offset = (int) (code & ((1<<ERRCODE_RANGE)-1)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 125 | table_num = code - offset; |
| 126 | if (!table_num) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 127 | #ifdef HAS_SYS_ERRLIST |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 128 | if (offset < sys_nerr) |
| 129 | return(sys_errlist[offset]); |
| 130 | else |
| 131 | goto oops; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 132 | #else |
| 133 | cp = strerror(offset); |
| 134 | if (cp) |
| 135 | return(cp); |
| 136 | else |
| 137 | goto oops; |
| 138 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 139 | } |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 140 | et_list_lock(); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 141 | for (et = _et_list; et; et = et->next) { |
Theodore Ts'o | 6b6c27f | 2007-12-15 22:31:03 -0500 | [diff] [blame] | 142 | if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 143 | /* This is the right table */ |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 144 | if (et->table->n_msgs <= offset) { |
| 145 | break; |
| 146 | } else { |
| 147 | const char *msg = et->table->msgs[offset]; |
| 148 | et_list_unlock(); |
Theodore Ts'o | 4259052 | 2011-10-05 00:58:40 -0400 | [diff] [blame] | 149 | if (com_err_gettext) |
| 150 | return (*com_err_gettext)(msg); |
| 151 | else |
| 152 | return msg; |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 153 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Theodore Ts'o | d51b819 | 2005-06-20 17:59:06 -0400 | [diff] [blame] | 156 | for (et = _et_dynamic_list; et; et = et->next) { |
Theodore Ts'o | 6b6c27f | 2007-12-15 22:31:03 -0500 | [diff] [blame] | 157 | if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) { |
Theodore Ts'o | d51b819 | 2005-06-20 17:59:06 -0400 | [diff] [blame] | 158 | /* This is the right table */ |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 159 | if (et->table->n_msgs <= offset) { |
| 160 | break; |
| 161 | } else { |
| 162 | const char *msg = et->table->msgs[offset]; |
| 163 | et_list_unlock(); |
Theodore Ts'o | 4259052 | 2011-10-05 00:58:40 -0400 | [diff] [blame] | 164 | if (com_err_gettext) |
| 165 | return (*com_err_gettext)(msg); |
| 166 | else |
| 167 | return msg; |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 168 | } |
Theodore Ts'o | d51b819 | 2005-06-20 17:59:06 -0400 | [diff] [blame] | 169 | } |
| 170 | } |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 171 | et_list_unlock(); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 172 | oops: |
| 173 | strcpy (buffer, "Unknown code "); |
| 174 | if (table_num) { |
| 175 | strcat (buffer, error_table_name (table_num)); |
| 176 | strcat (buffer, " "); |
| 177 | } |
| 178 | for (cp = buffer; *cp; cp++) |
| 179 | ; |
| 180 | if (offset >= 100) { |
| 181 | *cp++ = '0' + offset / 100; |
| 182 | offset %= 100; |
| 183 | started++; |
| 184 | } |
| 185 | if (started || offset >= 10) { |
| 186 | *cp++ = '0' + offset / 10; |
| 187 | offset %= 10; |
| 188 | } |
| 189 | *cp++ = '0' + offset; |
| 190 | *cp = '\0'; |
| 191 | return(buffer); |
| 192 | } |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 193 | |
| 194 | /* |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 195 | * This routine will only return a value if the we are not running as |
| 196 | * a privileged process. |
| 197 | */ |
| 198 | static char *safe_getenv(const char *arg) |
| 199 | { |
| 200 | if ((getuid() != geteuid()) || (getgid() != getegid())) |
| 201 | return NULL; |
| 202 | #if HAVE_PRCTL |
| 203 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) |
| 204 | return NULL; |
| 205 | #else |
| 206 | #if (defined(linux) && defined(SYS_prctl)) |
| 207 | if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) |
| 208 | return NULL; |
| 209 | #endif |
| 210 | #endif |
| 211 | |
| 212 | #ifdef HAVE___SECURE_GETENV |
| 213 | return __secure_getenv(arg); |
| 214 | #else |
| 215 | return getenv(arg); |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | #define DEBUG_INIT 0x8000 |
| 220 | #define DEBUG_ADDREMOVE 0x0001 |
| 221 | |
| 222 | static int debug_mask = 0; |
| 223 | static FILE *debug_f = 0; |
| 224 | |
| 225 | static void init_debug(void) |
| 226 | { |
Theodore Ts'o | 902be4a | 2008-10-01 20:45:26 -0400 | [diff] [blame] | 227 | char *dstr, *fn, *tmp; |
| 228 | int fd, flags; |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 229 | |
| 230 | if (debug_mask & DEBUG_INIT) |
| 231 | return; |
| 232 | |
| 233 | dstr = getenv("COMERR_DEBUG"); |
Theodore Ts'o | 902be4a | 2008-10-01 20:45:26 -0400 | [diff] [blame] | 234 | if (dstr) { |
| 235 | debug_mask = strtoul(dstr, &tmp, 0); |
| 236 | if (*tmp || errno) |
| 237 | debug_mask = 0; |
| 238 | } |
| 239 | |
| 240 | debug_mask |= DEBUG_INIT; |
| 241 | if (debug_mask == DEBUG_INIT) |
| 242 | return; |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 243 | |
| 244 | fn = safe_getenv("COMERR_DEBUG_FILE"); |
| 245 | if (fn) |
| 246 | debug_f = fopen(fn, "a"); |
| 247 | if (!debug_f) |
| 248 | debug_f = fopen("/dev/tty", "a"); |
Theodore Ts'o | 902be4a | 2008-10-01 20:45:26 -0400 | [diff] [blame] | 249 | if (debug_f) { |
| 250 | fd = fileno(debug_f); |
| 251 | if (fd >= 0) { |
| 252 | flags = fcntl(fd, F_GETFD); |
| 253 | if (flags >= 0) |
| 254 | fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 255 | } |
| 256 | } else |
| 257 | debug_mask = DEBUG_INIT; |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 258 | |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 262 | * New interface provided by krb5's com_err library |
| 263 | */ |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 264 | errcode_t add_error_table(const struct error_table * et) |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 265 | { |
Theodore Ts'o | d51b819 | 2005-06-20 17:59:06 -0400 | [diff] [blame] | 266 | struct et_list *el; |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 267 | |
| 268 | if (!(el = (struct et_list *) malloc(sizeof(struct et_list)))) |
| 269 | return ENOMEM; |
| 270 | |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 271 | if (et_list_lock() != 0) { |
| 272 | free(el); |
| 273 | return errno; |
| 274 | } |
| 275 | |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 276 | el->table = et; |
Theodore Ts'o | d51b819 | 2005-06-20 17:59:06 -0400 | [diff] [blame] | 277 | el->next = _et_dynamic_list; |
| 278 | _et_dynamic_list = el; |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 279 | |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 280 | init_debug(); |
| 281 | if (debug_mask & DEBUG_ADDREMOVE) |
| 282 | fprintf(debug_f, "add_error_table: %s (0x%p)\n", |
| 283 | error_table_name(et->base), |
| 284 | (const void *) et); |
| 285 | |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 286 | et_list_unlock(); |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * New interface provided by krb5's com_err library |
| 292 | */ |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 293 | errcode_t remove_error_table(const struct error_table * et) |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 294 | { |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 295 | struct et_list *el; |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 296 | struct et_list *el2 = 0; |
| 297 | |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 298 | if (et_list_lock() != 0) |
| 299 | return ENOENT; |
| 300 | |
| 301 | el = _et_dynamic_list; |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 302 | init_debug(); |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 303 | while (el) { |
| 304 | if (el->table->base == et->base) { |
| 305 | if (el2) /* Not the beginning of the list */ |
| 306 | el2->next = el->next; |
| 307 | else |
Theodore Ts'o | d51b819 | 2005-06-20 17:59:06 -0400 | [diff] [blame] | 308 | _et_dynamic_list = el->next; |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 309 | (void) free(el); |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 310 | if (debug_mask & DEBUG_ADDREMOVE) |
| 311 | fprintf(debug_f, |
| 312 | "remove_error_table: %s (0x%p)\n", |
| 313 | error_table_name(et->base), |
| 314 | (const void *) et); |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 315 | et_list_unlock(); |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | el2 = el; |
| 319 | el = el->next; |
| 320 | } |
Theodore Ts'o | ec84b74 | 2006-12-22 13:38:38 -0500 | [diff] [blame] | 321 | if (debug_mask & DEBUG_ADDREMOVE) |
| 322 | fprintf(debug_f, "remove_error_table FAILED: %s (0x%p)\n", |
| 323 | error_table_name(et->base), |
| 324 | (const void *) et); |
Theodore Ts'o | d7f45af | 2008-09-12 10:15:26 -0400 | [diff] [blame] | 325 | et_list_unlock(); |
Theodore Ts'o | 00aba96 | 2003-03-19 19:46:02 -0500 | [diff] [blame] | 326 | return ENOENT; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Variant of the interface provided by Heimdal's com_err library |
| 331 | */ |
| 332 | void |
| 333 | add_to_error_table(struct et_list *new_table) |
| 334 | { |
| 335 | add_error_table(new_table->table); |
| 336 | } |