// however invalidate any other reasons why the executable file might be
// covered by the GNU Public License.
// -----------------------------------------------------------------------
-//fosa_long_jump.h
+//fosa_long_jump.c
//==============================================
// ******** ****** ******** **********
// **///// /** ** **////// /** /**
#ifdef CONFIG_LONGJUMP
#include <fosa_long_jump.h>
+#include <stdio.h>
-enum {
- LONGJMP_MAGIC = 0x01234567,
- LONGJMP_NSIG = 3,
- LONGJMP_FIRSTSIG = SIGRTMIN + 1,
-};
-
+#define LONGJMP_MAGIC 0x01234567
int jmp_used_signals [LONGJMP_NSIG] = {[0 ... (LONGJMP_NSIG - 1)] = 0};
+pthread_mutex_t signal_pool_m = PTHREAD_MUTEX_INITIALIZER;
-
-int fosa_long_jump_save_context
- (fosa_long_jump_context_t * context)
-{
- if (!context)
- return FOSA_EINVAL;
-
- // Save the actual signal mask & mark the jump as still not performed
- pthread_sigmask (SIG_SETMASK, NULL, &(context -> jmp_sigmask));
- context -> jmp_hasexecuted = false;
-
- if (setjmp (context -> jmp_context) == LONGJMP_MAGIC) {
-#ifndef CONFIG_LONGJUMP_FREE_SIGNALS
- pthread_sigmask (SIG_SETMASK, &(context -> jmp_sigmask), NULL);
-#endif
- context -> jmp_hasexecuted = true;
- }
- return 0;
-}
-
+extern void fosa_longjmp (fosa_long_jump_context_t ctx, unsigned long magic);
int fosa_long_jump_was_performed
(const fosa_long_jump_context_t * context, int * jumped)
if (!context && !jumped)
return FOSA_EINVAL;
- *jumped = context -> jmp_hasexecuted? 1:0;
+// printf ("context [6]=%ld\n", (*(context))[6]);
+ *jumped = ((*(context))[6] == LONGJMP_MAGIC);
return 0;
}
void jmp_handler (int signo, siginfo_t *info, void *context)
{
fosa_long_jump_context_t *jmp_info = (fosa_long_jump_context_t *) info -> si_value.sival_ptr;
+
+#ifdef CONFIG_LONGJUMP_FREE_SIGNAL
sigset_t set;
-#ifdef CONFIG_LONGJUMP_FREE_SIGNALS
- // Restore the signal mask beofre the context save
- pthread_sigmask (SIG_SETMASK, &(context -> jmp_sigmask), NULL);
+ // Restore the signal mask
+// pthread_sigmask (SIG_SETMASK, &(jmp_info -> jmp_sigmask), NULL);
// Free this signal
sigemptyset (&set);
sigaddset (&set, signo);
pthread_sigmask (SIG_BLOCK, &sigmask, NULL);
- pthread_mutex_lock ();
+ pthread_mutex_lock (&signal_pool_m);
jmp_used_signals [signo] = 0;
- pthread_mutex_unlock ();
+ pthread_mutex_unlock (&signal_pool_m);
#endif
+// printf ("\t\t\t\t>>> RESTORE CONTEXT <<<\n\n");
// Restore the saved context
- longjmp (jmp_info -> jmp_context, LONGJMP_MAGIC);
+ fosa_longjmp (*jmp_info, LONGJMP_MAGIC);
return;
}
sigset_t sigmask;
// Find the first usable signal
- // FIXME: Should be atomical
+ pthread_mutex_lock (&signal_pool_m);
for (i = 0; i < LONGJMP_NSIG && jmp_used_signals [i]; i++);
- if (i >= LONGJMP_NSIG)
+ if (i >= LONGJMP_NSIG) {
+ pthread_mutex_unlock (&signal_pool_m);
return FOSA_ENOMEM;
+ }
jmp_used_signals [i] = 1;
+ pthread_mutex_unlock (&signal_pool_m);
*signal = LONGJMP_FIRSTSIG + i;
*handler = pthread_self ();