/* * linux/kernel/signal.c * * Copyright (C) 1991, 1992 Linus Torvalds */
#include <linux/sched.h> #include <linux/kernel.h> #include <linux/signal.h> #include <linux/errno.h> #include <linux/wait.h> #include <linux/ptrace.h> #include <linux/unistd.h> #include <linux/mm.h> #include <asm/segment.h> #define _S(nr) (1<<((nr)-1)) #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP))) #ifndef __alpha__
/* * This call isn't used by all ports, in particular, the Alpha * uses osf_sigprocmask instead. Maybe it should be moved into * arch-dependent dir? */
/* Z tej funkcji (systemowej) korzysta się by ustawić sygnały blokowane. Parametry to:
asmlinkage int sys_sigprocmask(int how, sigset_t *set, sigset_t *oset)
{
sigset_t new_set, old_set = current->blocked;
int error;
if (set) {
/* Sprawdzamy za pomocą verify_area, czy można czytać z miejsca, na które wskazuje set (tzn. czy wskaźnik jest dobry) */
error = verify_area(VERIFY_READ, set, sizeof(sigset_t));
if (error)
return error;
/* Nie można blokować sygnałów SIGKILL i SIGSTOP. W zależności od how: SIG_BLOCK - dodatkowo blokujemy sygnały z new_set, SIG_UNBLOCK - odblokowujemy sygnały z new_set, SIG_SETMASK - ustawiamy sygnały blokowane na new_set */
new_set = get_user(set) & _BLOCKABLE;
switch (how) {
case SIG_BLOCK:
current->blocked |= new_set;
break;
case SIG_UNBLOCK:
current->blocked &= ~new_set;
break;
case SIG_SETMASK:
current->blocked = new_set;
break;
default:
return -EINVAL;
}
}
if (oset) {
/* Ew. wpisanie starej maski (po sprawdzeniu czy adres oset jest dobry) */
error = verify_area(VERIFY_WRITE, oset, sizeof(sigset_t));
if (error)
return error;
put_user(old_set, oset);
}
return 0;
}
/* * For backwards compatibility? Functionality superseded by sigprocmask. */
/* Dwie proste funkcje. Pierwsza równoważna: sigprocmask(0,(sigset_t*)0,os) i późniejszemu sprawdzeniu os. Druga równoważna: Sigprocmask(SIG_SET,s1,os), gdzie s1 to adres newmask. */
asmlinkage int sys_sgetmask(void)
{
return current->blocked;
}
asmlinkage int sys_ssetmask(int newmask)
{
int old=current->blocked;
current->blocked = newmask & _BLOCKABLE;
return old;
}
#endif
/* Funkcja wpisuje pod adres set te sygnały, które bieżący proces odebrał, ale nie obsłużył, bo je blokuje */
asmlinkage int sys_sigpending(sigset_t *set)
{
int error;
error = verify_area(VERIFY_WRITE, set, sizeof(sigset_t));
if (!error)
put_user(current->blocked & current->signal, set);
return error;
}
/* * POSIX 3.3.1.3: * "Setting a signal action to SIG_IGN for a signal that is pending * shall cause the pending signal to be discarded, whether or not * it is blocked." * * "Setting a signal action to SIG_DFL for a signal that is pending * and whose default action is to ignore the signal (for example, * SIGCHLD), shall cause the pending signal to be discarded, whether * or not it is blocked" * * Note the silly behaviour of SIGCHLD: SIG_IGN means that the signal * isn't actually ignored, but does automatic child reaping, while * SIG_DFL is explicitly said by POSIX to force the signal to be ignored.. */
/* Jeśli zmieniana jest obsługa sygnału na SIG_IGN, albo na SIG_DFL, kiedy działaniem domyślnym jest ignorowanie sygnału, to bit odpowiadający temu sygnałowi jest czyszczony (przez poniższą procedurę). Wywołują ją obie funkcje zmieniające obsługę sygnałów: sys_signal i sys_sigaction.
UWAGA: Sygnał SIGCHLD zachowuje się odwrotnie niż normalny sygnał: jest on obsługiwany przy SIG_IGN (automatyczne zbieranie dzieci, p. do_signal), zaś przy SIG_DFL jest on ignorowany*/
static inline void check_pending(int signum)
{
struct sigaction *p;
/* p to obsługa sygnału signum */
p = signum - 1 + current->sig->action;
/* _S to makro, które biorąc signum (liczbę), zwraca słowo o ustawionym bicie signum. Ten właśnie bit jest następnie (już nie przez makro, tylko poniższy kod) czyszczony z pola signal */
if (p->sa_handler == SIG_IGN) {
current->signal &= ~_S(signum);
return;
}
/* Czyszczenie przy SIG_DFL odbywa się tylko dla następujących trzech sygnałów - ich obsługą domyślną jest ignorowanie*/
if (p->sa_handler == SIG_DFL) {
if (signum != SIGCONT && signum != SIGCHLD && signum != SIGWINCH)
return;
current->signal &= ~_S(signum);
return;
}
}
#ifndef __alpha__
/* * For backwards compatibility? Functionality superseded by sigaction. */
/* Stara funkcja do zmieniania obsługi sygnału */
asmlinkage unsigned long sys_signal(int signum, __sighandler_t handler)
{
int err;
struct sigaction tmp;
if (signum<1 || signum>32)
return -EINVAL;
/* Nie można zmieniać obsługi SIGKILL i SIGSTOP. Inaczej można by stworzyć niezabijalne procesy */
if (signum==SIGKILL || signum==SIGSTOP)
return -EINVAL;
/* Jeśli dajemy prawdziwy adres, trzeba sprawdzić jego ważność (funkcja verify_area) */
if (handler != SIG_DFL && handler != SIG_IGN) {
err = verify_area(VERIFY_READ, handler, 1);
if (err)
return err;
}
/* Podmieniamy procedurę obsługującą sygnał na handler. Starą procedurę zwracamy jako wynik funkcji. Flaga SA_ONESHOT oznacza, że po obsłudze sygnału z nasza nowa funkcja handler, pole sa_handler zostanie z powrotem ustawione na SIG_DFL. Ustawienie flagi SA_NOMASK powstrzymuje jądro od zmieniania pola blocked w task_struct odbierającego sygnał (p. opis handle_signal) */
memset(&tmp, 0, sizeof(tmp));
tmp.sa_handler = handler;
tmp.sa_flags = SA_ONESHOT | SA_NOMASK;
handler = current->sig->action[signum-1].sa_handler;
current->sig->action[signum-1] = tmp;
check_pending(signum);
return (unsigned long) handler;
}
#endif
/* To jest prawdziwa funkcja systemowa służąca do zmiany obsługi sygnału. Parametry:
asmlinkage int sys_sigaction(int signum, const struct sigaction * action,
struct sigaction * oldaction)
{
struct sigaction new_sa, *p;
if (signum<1 || signum>32)
return -EINVAL;
p = signum - 1 + current->sig->action;
if (action) {
/* Najpierw sprawdzamy za pomocą verify_area, czy adres jest poprawny*/
int err = verify_area(VERIFY_READ, action, sizeof(*action));
if (err)
return err;
/* Nie można zmienić obsługi tych sygnałów (ze zrozumiałych przyczyn)*/
if (signum==SIGKILL || signum==SIGSTOP)
return -EINVAL;
/* Kopiujemy samą strukturę action, ale nie procedurę handlera. Jeśli sa_handler nie jest jedną z predefiniowanych stałych liczbowych musimy sprawdzić, czy adres jest dobry*/
memcpy_fromfs(&new_sa, action, sizeof(struct sigaction));
if (new_sa.sa_handler != SIG_DFL && new_sa.sa_handler != SIG_IGN) {
err = verify_area(VERIFY_READ, new_sa.sa_handler, 1);
if (err)
return err;
}
}
/* Zapamiętywanie starej obsługi */
if (oldaction) {
int err = verify_area(VERIFY_WRITE, oldaction, sizeof(*oldaction));
if (err)
return err;
memcpy_tofs(oldaction, p, sizeof(struct sigaction));
}
/* Samo ustawianie obslugi. Za pomocą check_pending ew. usuwamy te sygnały, które postanowilimy ignorować (p. check_pending)*/
if (action) {
*p = new_sa;
check_pending(signum);
}
return 0;
}
Komentarze: Mikolaj Bojanczyk