From: Ken Raeburn Date: Thu, 6 May 2004 02:28:25 +0000 (+0000) Subject: Start using our first bit of per-thread storage X-Git-Tag: krb5-1.4-beta1~427 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=2e0cd9c3947738ed35e6d672ee4cdda5359163a0;p=krb5.git Start using our first bit of per-thread storage * error_message.c (buffer): Static variable deleted. (com_err_initialize): Register cleanup support for com_err thread-specific data key. (error_message): Use a per-thread dynamically-allocated buffer instead of static storage, for the case where an unknown error code is given. If any errors occur allocating or tracking the buffer, return a fixed message. * t_com_err.c: Include stdlib.h. If TEST_THREADS is defined, include pthread.h. (run): Renamed from main, changed signature. (main): New function. Just call run, or if TEST_THREADS is defined, create a thread to call it. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@16318 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/util/et/ChangeLog b/src/util/et/ChangeLog index 1209177fa..4e99e206f 100644 --- a/src/util/et/ChangeLog +++ b/src/util/et/ChangeLog @@ -1,3 +1,19 @@ +2004-05-05 Ken Raeburn + + * error_message.c (buffer): Static variable deleted. + (com_err_initialize): Register cleanup support for com_err + thread-specific data key. + (error_message): Use a per-thread dynamically-allocated buffer + instead of static storage, for the case where an unknown error + code is given. If any errors occur allocating or tracking the + buffer, return a fixed message. + + * t_com_err.c: Include stdlib.h. If TEST_THREADS is defined, + include pthread.h. + (run): Renamed from main, changed signature. + (main): New function. Just call run, or if TEST_THREADS is + defined, create a thread to call it. + 2004-05-04 Ken Raeburn * configure.in: Invoke KRB5_BUILD_PROGRAM and KRB5_RUN_FLAGS. diff --git a/src/util/et/error_message.c b/src/util/et/error_message.c index 44a73b6c6..dd3c36fd0 100644 --- a/src/util/et/error_message.c +++ b/src/util/et/error_message.c @@ -38,8 +38,6 @@ extern char const * const sys_errlist[]; extern const int sys_nerr; #endif -static char buffer[ET_EBUFSIZ]; - /*@null@*/ static struct et_list * _et_list = (struct et_list *) NULL; /*@null@*//*@only@*/static struct dynamic_et_list * et_list_dynamic; static k5_mutex_t et_list_lock = K5_MUTEX_PARTIAL_INITIALIZER; @@ -49,7 +47,14 @@ MAKE_FINI_FUNCTION(com_err_terminate); int com_err_initialize(void) { - return k5_mutex_finish_init(&et_list_lock); + int err; + err = k5_mutex_finish_init(&et_list_lock); + if (err) + return err; + err = k5_key_register(K5_KEY_COM_ERR, free); + if (err) + return err; + return 0; } void com_err_terminate(void) @@ -81,7 +86,7 @@ error_message(long code) unsigned long table_num; int started = 0; unsigned int divisor = 100; - char *cp; + char *cp, *cp1; const struct error_table *table; int merr; @@ -221,8 +226,20 @@ oops: return (strerror (code)); } #endif - - cp = buffer; + + cp = k5_getspecific(K5_KEY_COM_ERR); + if (cp == NULL) { + cp = malloc(ET_EBUFSIZ); + if (cp == NULL) { + no_specific: + return "Unknown error code"; + } + if (k5_setspecific(K5_KEY_COM_ERR, cp) != 0) { + free(cp); + goto no_specific; + } + } + cp1 = cp; strcpy(cp, "Unknown code "); cp += sizeof("Unknown code ") - 1; if (table_num != 0L) { @@ -241,7 +258,7 @@ oops: } *cp++ = '0' + offset; *cp = '\0'; - return(buffer); + return(cp1); } /*@-incondefs@*/ /* _et_list is global on unix but not in header annotations */ diff --git a/src/util/et/t_com_err.c b/src/util/et/t_com_err.c index c6095cfe1..2cba3cfdc 100644 --- a/src/util/et/t_com_err.c +++ b/src/util/et/t_com_err.c @@ -1,5 +1,6 @@ #include #include +#include #include "com_err.h" #include "et1.h" #include "et2.h" @@ -59,7 +60,7 @@ try_em_1 (int t1_known, int t2_known, int lineno) } #define try_em(A,B) try_em_1(A,B,__LINE__) -int main (/*@unused@*/ int argc, /*@unused@*/ char *argv[]) +static void *run(/*@unused@*/ void *x) { try_em (0, 0); (void) add_error_table (&et_et1_error_table); @@ -110,5 +111,33 @@ int main (/*@unused@*/ int argc, /*@unused@*/ char *argv[]) (void) remove_error_table (&et_et2_error_table); try_em (0, 0); + return 0; +} + +#ifdef TEST_THREADS +#include +#endif + +int main (/*@unused@*/ int argc, /*@unused@*/ char *argv[]) +{ +#ifdef TEST_THREADS + pthread_t t; + int err; + void *t_retval; + + err = pthread_create(&t, 0, run, 0); + if (err) { + fprintf(stderr, "pthread_create error: %s\n", strerror(err)); + exit(1); + } + err = pthread_join(t, &t_retval); + if (err) { + fprintf(stderr, "pthread_join error: %s\n", strerror(err)); + exit(1); + } + return fail; +#else + run(0); return fail; +#endif }