]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/et/error_message.c
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 const char * error_message (errcode_t code
)
125 offset
= (int) (code
& ((1<<ERRCODE_RANGE
)-1));
126 table_num
= code
- offset
;
128 #ifdef HAS_SYS_ERRLIST
129 if (offset
< sys_nerr
)
130 return(sys_errlist
[offset
]);
134 cp
= strerror(offset
);
142 for (et
= _et_list
; et
; et
= et
->next
) {
143 if ((et
->table
->base
& 0xffffffL
) == (table_num
& 0xffffffL
)) {
144 /* This is the right table */
145 if (et
->table
->n_msgs
<= offset
) {
148 const char *msg
= et
->table
->msgs
[offset
];
151 return (*com_err_gettext
)(msg
);
157 for (et
= _et_dynamic_list
; et
; et
= et
->next
) {
158 if ((et
->table
->base
& 0xffffffL
) == (table_num
& 0xffffffL
)) {
159 /* This is the right table */
160 if (et
->table
->n_msgs
<= offset
) {
163 const char *msg
= et
->table
->msgs
[offset
];
166 return (*com_err_gettext
)(msg
);
174 strcpy (buffer
, "Unknown code ");
176 strcat (buffer
, error_table_name (table_num
));
177 strcat (buffer
, " ");
179 for (cp
= buffer
; *cp
; cp
++)
182 *cp
++ = '0' + offset
/ 100;
186 if (started
|| offset
>= 10) {
187 *cp
++ = '0' + offset
/ 10;
190 *cp
++ = '0' + offset
;
196 * This routine will only return a value if the we are not running as
197 * a privileged process.
199 static char *safe_getenv(const char *arg
)
202 if ((getuid() != geteuid()) || (getgid() != getegid()))
206 if (prctl(PR_GET_DUMPABLE
, 0, 0, 0, 0) == 0)
209 #if (defined(linux) && defined(SYS_prctl))
210 if (syscall(SYS_prctl
, PR_GET_DUMPABLE
, 0, 0, 0, 0) == 0)
215 #if defined(HAVE_SECURE_GETENV)
216 return secure_getenv(arg
);
217 #elif defined(HAVE___SECURE_GETENV)
218 return __secure_getenv(arg
);
224 #define DEBUG_INIT 0x8000
225 #define DEBUG_ADDREMOVE 0x0001
227 static int debug_mask
= 0;
228 static FILE *debug_f
= 0;
230 static void init_debug(void)
232 char *dstr
, *fn
, *tmp
;
235 if (debug_mask
& DEBUG_INIT
)
238 dstr
= getenv("COMERR_DEBUG");
240 debug_mask
= strtoul(dstr
, &tmp
, 0);
245 debug_mask
|= DEBUG_INIT
;
246 if (debug_mask
== DEBUG_INIT
)
249 fn
= safe_getenv("COMERR_DEBUG_FILE");
251 debug_f
= fopen(fn
, "a");
253 debug_f
= fopen("/dev/tty", "a");
255 fd
= fileno(debug_f
);
256 #if defined(HAVE_FCNTL)
258 flags
= fcntl(fd
, F_GETFD
);
260 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
264 debug_mask
= DEBUG_INIT
;
269 * New interface provided by krb5's com_err library
271 errcode_t
add_error_table(const struct error_table
* et
)
275 if (!(el
= (struct et_list
*) malloc(sizeof(struct et_list
))))
278 if (et_list_lock() != 0) {
284 el
->next
= _et_dynamic_list
;
285 _et_dynamic_list
= el
;
288 if (debug_mask
& DEBUG_ADDREMOVE
)
289 fprintf(debug_f
, "add_error_table: %s (0x%p)\n",
290 error_table_name(et
->base
),
298 * New interface provided by krb5's com_err library
300 errcode_t
remove_error_table(const struct error_table
* et
)
303 struct et_list
*el2
= 0;
305 if (et_list_lock() != 0)
308 el
= _et_dynamic_list
;
311 if (el
->table
->base
== et
->base
) {
312 if (el2
) /* Not the beginning of the list */
313 el2
->next
= el
->next
;
315 _et_dynamic_list
= el
->next
;
317 if (debug_mask
& DEBUG_ADDREMOVE
)
319 "remove_error_table: %s (0x%p)\n",
320 error_table_name(et
->base
),
328 if (debug_mask
& DEBUG_ADDREMOVE
)
329 fprintf(debug_f
, "remove_error_table FAILED: %s (0x%p)\n",
330 error_table_name(et
->base
),
337 * Variant of the interface provided by Heimdal's com_err library
340 add_to_error_table(struct et_list
*new_table
)
342 add_error_table(new_table
->table
);