Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Markus Elfring Newsgroups: comp.programming.threads,comp.unix.programmer Subject: Safe accesses of global arrays in signal handlers? Date: Tue, 25 Sep 2012 08:01:47 +0200 Lines: 58 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Trace: individual.net RHsq+aC5xcr/VtT94c6K9ATLJGon3alYsgR7iqs7a+UFoGQtAFvxT7Msc5grW+XUFb Cancel-Lock: sha1:rATYqArz6gLmIS0wWku+zJKYDzY= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120825 Thunderbird/15.0 Xref: csiph.com comp.programming.threads:1089 comp.unix.programmer:3311 Hello, A secure programming guideline contains the following information. https://www.securecoding.cert.org/confluence/display/seccode/SIG31-C.+Do+not+access+or+modify+shared+objects+in+signal+handlers : 'Accessing or modifying shared objects in signal handlers can result in race conditions that can leave data in an inconsistent state. The exception to this rule is the ability to read and write to variables of volatile sig_atomic_t. ... It is important to note that the behavior of a program that accesses an object of any other type from a signal handler is undefined.' I interpret this description in the way that there are risks for the portable use of a shared variable which has got a data type like "array" within a signal handler. http://en.wikipedia.org/wiki/Array_data_type Source code example: pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER; struct my_element my_list[MY_ARRAY_SIZE]; void my_status_log(int number) { int result = pthread_mutex_lock(&my_lock); if (result) exit(result); if ( my_list[number - 1].catched && printf("Signal %d was received.", number) < 22) perror("printf"); result = pthread_mutex_unlock(&my_lock); if (result) exit(result); } static void my_signal_handler(int number) { struct my_data * pointer = my_list[number - 1].context; if (!pointer) return; my_list[number - 1].catched = 1; /* Call more functions eventually ... */ } I would appreciate your advices for the clarification of the involved implementation details. Would you like to suggest any solutions for such an use case? Regards, Markus