/*! Process pending signals for thread (called from kthreads_schedule ()) */ int ksignal_process_pending ( kthread_t *kthread ) { ksignal_handling_t *sh; int retval = EXIT_SUCCESS; ksiginfo_t *ksig, *next; ASSERT ( kthread ); sh = kthread_get_sigparams ( kthread ); ksig = list_get ( &sh->pending_signals, FIRST ); while ( ksig ) { next = list_get_next ( &ksig->list ); if ( !sigtestset ( sh->mask, ksig->siginfo.si_signo ) ) { list_remove ( &sh->pending_signals, 0, &ksig->list ); retval = ksignal_queue ( kthread, &ksig->siginfo ); kfree ( ksig ); /* handle only first signal? * no, all of them - they will mask ... */ /*if ( retval == EXIT_SUCCESS ) break;*/ } ksig = next; } return retval; }
/*! * Wait for signal * \param set Signals thread is waiting for * \param info Where to save caught signal * \return signal number if signal is caught, * -1 otherwise and appropriate error number is set */ int sys__sigwaitinfo ( void *p ) { sigset_t *set; siginfo_t *info; kthread_t *kthread; ksignal_handling_t *sh; ksiginfo_t *ksig, *next; int retval; set = *( (sigset_t **) p ); p += sizeof (sigset_t *); info = *( (siginfo_t **) p ); ASSERT_ERRNO_AND_EXIT ( set, EINVAL ); set = U2K_GET_ADR ( set, kthread_get_process (NULL) ); ASSERT_ERRNO_AND_EXIT ( set, EINVAL ); if ( info ) info = U2K_GET_ADR ( info, kthread_get_process (NULL) ); kthread = kthread_get_active (); sh = kthread_get_sigparams ( kthread ); /* first, search for such signal in pending signals */ ksig = list_get ( &sh->pending_signals, FIRST ); while ( ksig ) { next = list_get_next ( &ksig->list ); if ( sigtestset ( set, ksig->siginfo.si_signo ) ) { retval = ksig->siginfo.si_signo; if ( info ) *info = ksig->siginfo; list_remove ( &sh->pending_signals, 0, &ksig->list ); kfree ( ksig ); EXIT2 ( EXIT_SUCCESS, retval ); } ksig = next; } /* * if no pending signal found that matches given mask * suspend thread until signal is received */ kthread_suspend ( kthread, ksignal_received_signal, NULL ); kthreads_schedule (); EXIT ( EINTR ); /* if other events wake thread */ }
/*! Send signal to target thread */ int ksignal_queue ( kthread_t *kthread, siginfo_t *sig ) { int enqueue = FALSE; int retval = EXIT_SUCCESS; int schedule = FALSE; ksignal_handling_t *sh; sigaction_t *act; void (*func) (kthread_t *, void *), *param; kprocess_t *proc; siginfo_t *us; param_t param1, param2, param3; ASSERT ( kthread ); ASSERT ( kthread_check_kthread ( kthread ) ); ASSERT ( sig->si_signo > 0 && sig->si_signo <= SIGMAX ); if ( !kthread_is_alive ( kthread ) ) return ESRCH; sh = kthread_get_sigparams ( kthread ); /* is thread suspended and waits for this signal? */ if ( kthread_is_suspended ( kthread, (void **) &func, ¶m ) && ((void *) func) == ((void *) ksignal_received_signal) ) { /* thread is waiting for signal */ if ( !ksignal_received_signal ( kthread, sig ) ) { /* waited for this signal */ /* do not process this signal further */ return EXIT_SUCCESS; } } /* * If thread is in interruptable state and signal is not masked: * - process signal * - otherwise queue it */ if ( kthread_get_interruptable ( kthread ) && !sigtestset ( sh->mask, sig->si_signo ) ) { act = &sh->act[sig->si_signo]; if ( act->sa_flags != SA_SIGINFO ) return ENOTSUP; /* not supported without SA_SIGINFO! */ if ( act->sa_sigaction == SIG_ERR || act->sa_sigaction == SIG_DFL || act->sa_sigaction == SIG_IGN || act->sa_sigaction == SIG_HOLD ) { return ENOTSUP; /* not yet supported */ } if ( !kthread_is_ready ( kthread ) ) { /* * thread is suspended/blocked on something else * * 1. handle interruption: * a) we break suspend/wait state * (call cancel function) * b) set errno = EINTR * c) set return value = FAILURE * 2. create new thread state * a) process signal in this state */ void (*func) (kthread_t *, void *), *param; kthread_is_suspended (kthread, (void **) &func, ¶m); if ( func ) func ( kthread, param ); kthread_move_to_ready ( kthread, LAST ); kthread_set_errno ( kthread, EINTR ); kthread_set_syscall_retval ( kthread, EXIT_FAILURE ); /* thread is unsuspended, but signal * handler will be added first */ schedule = TRUE; } /* copy sig to user space */ proc = kthread_get_process ( kthread ); us = kprocess_stack_alloc ( proc ); ASSERT (us); /*if ( !us ) return ENOMEM;*/ *us = *sig; kthread_create_new_state ( kthread, act->sa_sigaction, K2U_GET_ADR ( us, proc ), NULL, HANDLER_STACK_SIZE, TRUE ); param1.p_ptr = proc; param2.p_ptr = us; param3.p_ptr = NULL; kthread_add_cleanup ( kthread, kprocess_stack_free, param1, param2, param3 ); /* mask signal in thread mask */ sigaddset ( sh->mask, sig->si_signo ); /* mask additional signals in thread mask */ sigaddsets ( sh->mask, &act->sa_mask ); } else { enqueue = TRUE; } if ( enqueue ) { ksignal_add_to_pending ( sh, sig ); retval = EAGAIN; } if ( schedule ) kthreads_schedule (); return retval; }
/*! Callback function called when a signal is delivered to suspended thread */ static int ksignal_received_signal ( kthread_t *kthread, void *param ) { siginfo_t *sig; context_t *context; uint sysid; void *p; sigset_t *set; siginfo_t *info; int retval = EXIT_SUCCESS; ASSERT ( kthread ); /* thread waked by signal or other event? */ if ( param == NULL ) { kthread_set_errno ( kthread, EINTR ); kthread_set_syscall_retval ( kthread, EXIT_FAILURE ); return EXIT_FAILURE; /* other event interrupted thread */ } /* signal interrupted, but did thread waited for this signal? */ sig = param; /* get syscall which caused thread to be suspend */ context = kthread_get_context ( kthread ); sysid = arch_syscall_get_id ( context ); switch ( sysid ) { case SIGWAITINFO: /* sigwaitinfo */ p = arch_syscall_get_params ( context ); set = *( (sigset_t **) p ); p += sizeof (sigset_t *); info = *( (siginfo_t **) p ); p += sizeof (siginfo_t *); ASSERT ( set ); set = U2K_GET_ADR ( set, kthread_get_process (NULL) ); ASSERT ( set ); if ( info ) info = U2K_GET_ADR ( info, kthread_get_process (NULL) ); retval = EXIT_FAILURE; if ( sigtestset ( set, sig->si_signo ) ) { retval = sig->si_signo; kthread_set_syscall_retval ( kthread, retval ); if ( info ) *info = *sig; kthread_set_errno ( kthread, EXIT_SUCCESS ); /* resume with thread */ kthread_move_to_ready ( kthread, LAST ); kthreads_schedule (); return EXIT_SUCCESS; } else { /* not waiting for this signal */ return EXIT_FAILURE; } default: return EXIT_FAILURE; } }
/*! Send signal to target thread */ int ksignal_queue ( kthread_t *kthread, siginfo_t *sig ) { int enqueue = FALSE; int retval = EXIT_SUCCESS; int schedule = FALSE; ksignal_handling_t *sh; sigaction_t *act; void (*func) (kthread_t *, void *), *param; kprocess_t *proc; siginfo_t *us; param_t param1, param2, param3; ksiginfo_t *ksig; ASSERT ( kthread ); ASSERT ( kthread_check_kthread ( kthread ) ); ASSERT ( sig->si_signo > 0 && sig->si_signo <= SIGMAX ); if ( !kthread_is_alive ( kthread ) ) return ESRCH; sh = kthread_get_sigparams ( kthread ); /* is thread suspended and waits for this signal? */ if ( kthread_is_suspended ( kthread, (void **) &func, ¶m ) ) { if ( ((void *) func) == ((void *) ksignal_received_signal) ) { /* thread is waiting for signal */ if ( !ksignal_received_signal ( kthread, sig ) ) { /* waited for this signal */ /* should continue with signal handler also? */ /* do not process this signal further */ return EXIT_SUCCESS; } } /* else { * thread is waiting for something else; * deal with this later (in next "if") * } */ } /* if signal is not masked in thread signal mask, deliver signal */ if ( !sigtestset ( sh->mask, sig->si_signo ) ) { act = &sh->act[sig->si_signo]; if ( act->sa_flags != SA_SIGINFO ) return ENOTSUP; /* not supported without SA_SIGINFO! */ if ( act->sa_sigaction == SIG_ERR || act->sa_sigaction == SIG_DFL || act->sa_sigaction == SIG_IGN || act->sa_sigaction == SIG_HOLD ) { return ENOTSUP; /* not yet supported */ } /* current implementation * - if thread is active or ready * -- save old context in list * -- create new context * - else * -- enqueue signal or cancel wait state (todo) * * on sys__exit check if its handler or thread!!! */ if ( !kthread_is_ready ( kthread ) ) { void (*func) (kthread_t *, void *), *param; if ( kthread_is_suspended ( kthread, (void **) &func, ¶m ) ) { /* * thread is suspended on something * else; interrupt it or not? * * -handle interruption (kernel part) * -interrupt it (resume) * -process signal * * to do above just move thread to * ready, set errno & retval */ if ( func ) func ( kthread, param ); kthread_move_to_ready ( kthread, LAST ); kthread_set_errno ( kthread, EINTR ); kthread_set_syscall_retval(kthread,EXIT_FAILURE); /* thread is unsuspended, but signal * handler will be added first */ schedule = TRUE; } else { /* what else? this is error */ ASSERT ( FALSE ); } } /* copy sig to user space */ proc = kthread_get_process ( kthread ); us = ffs_alloc(proc->stack_pool, sizeof (siginfo_t)); ASSERT (us); /*if ( !us ) return ENOMEM;*/ *us = *sig; kthread_create_new_state ( kthread, act->sa_sigaction, K2U_GET_ADR ( us, proc ), NULL, HANDLER_STACK_SIZE, TRUE ); param1.p_ptr = proc->stack_pool; param2.p_ptr = us; param3.p_ptr = NULL; kthread_add_cleanup ( kthread, kthread_param_free, param1, param2, param3 ); /* mask signal in thread mask */ sigaddset ( sh->mask, sig->si_signo ); /* mask additional signals in thread mask */ sigaddsets ( sh->mask, &act->sa_mask ); } else { enqueue = TRUE; } if ( enqueue ) { /* mask signal in thread mask */ sigaddset ( sh->mask, sig->si_signo ); /* add signal to list of pending signals */ ksig = kmalloc ( sizeof (ksiginfo_t) ); ksig->siginfo = *sig; list_append ( &sh->pending_signals, ksig, &ksig->list ); /* list_sort_add ( &sh->pending_signals, ksig, &ksig->list, ksignal_compare ); */ retval = EAGAIN; } if ( schedule ) kthreads_schedule (); return retval; }