--- rpl/src/rpl.c 2011/09/09 12:23:25 1.81 +++ rpl/src/rpl.c 2011/09/20 15:45:53 1.92 @@ -23,12 +23,6 @@ #define MAIN_RPL #include "rpl-conv.h" -#ifdef IPCS_SYSV -#ifndef OS2 - unsigned char *chemin_semaphores_SysV; -#endif -#endif - /* ================================================================================ @@ -105,6 +99,8 @@ rplinit(int argc, char *argv[], unsigned volatile unsigned char traitement_fichier_temporaire; errno = 0; + s_queue_signaux = NULL; + pid_processus_pere = getpid(); # ifdef DEBUG_MEMOIRE debug_memoire_initialisation(); @@ -114,18 +110,11 @@ rplinit(int argc, char *argv[], unsigned setvbuf(stderr, NULL, _IOLBF, 0); # ifndef SEMAPHORES_NOMMES - sem_init(&semaphore_liste_threads, 0, 1); sem_init(&semaphore_gestionnaires_signaux, 0, 0); - sem_init(&semaphore_gestionnaires_signaux_atomique, 0, 1); # else - semaphore_liste_threads = sem_init2(1, sem_liste_threads); - semaphore_gestionnaires_signaux = sem_init2(0, sem_gestionnaires_signaux); - semaphore_gestionnaires_signaux_atomique = sem_init2(1, - sem_gestionnaires_signaux_atomique); - - if ((semaphore_liste_threads == SEM_FAILED) || - (semaphore_gestionnaires_signaux == SEM_FAILED) || - (semaphore_gestionnaires_signaux_atomique == SEM_FAILED)) + semaphore_gestionnaires_signaux = sem_init2(0, getpid(), SEM_SIGNAUX); + + if (semaphore_gestionnaires_signaux == SEM_FAILED) { erreur = d_es_allocation_memoire; @@ -203,22 +192,22 @@ rplinit(int argc, char *argv[], unsigned pthread_mutexattr_destroy(&attributs_mutex); # ifndef SEMAPHORES_NOMMES - sem_init(&((*s_etat_processus).semaphore_fork), 0, 0); + sem_init(&((*s_etat_processus).semaphore_fork), 0, 0); # else - if (((*s_etat_processus).semaphore_fork = sem_init2(0, sem_fork)) == - SEM_FAILED) - { - if ((*s_etat_processus).langue == 'F') - { - uprintf("+++Système : Mémoire insuffisante\n"); - } - else + if (((*s_etat_processus).semaphore_fork = sem_init3(0, getpid(), + pthread_self(), SEM_FORK)) == SEM_FAILED) { - uprintf("+++System : Not enough memory\n"); - } + if ((*s_etat_processus).langue == 'F') + { + uprintf("+++Système : Mémoire insuffisante\n"); + } + else + { + uprintf("+++System : Not enough memory\n"); + } - return(EXIT_FAILURE); - } + return(EXIT_FAILURE); + } # endif pthread_mutexattr_init(&attributs_mutex); @@ -242,21 +231,8 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).chemin_fichiers_temporaires = recherche_chemin_fichiers_temporaires(s_etat_processus); -# ifdef IPCS_SYSV -# ifndef OS2 - chemin_semaphores_SysV = (*s_etat_processus).chemin_fichiers_temporaires; -# endif -# endif - -# ifdef _BROKEN_SIGINFO - creation_fifos_signaux(s_etat_processus); -# ifdef return -# undef return -# endif -# define return destruction_fifos_signaux(s_etat_processus); return -# endif - insertion_thread(s_etat_processus, d_vrai); + creation_queue_signaux(s_etat_processus); # ifndef OS2 localisation_courante(s_etat_processus); @@ -338,42 +314,6 @@ rplinit(int argc, char *argv[], unsigned home = ""; } - // Initialisation d'une clef - - if (pthread_key_create(&semaphore_fork_processus_courant, NULL) != 0) - { - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Mémoire insuffisante\n"); - } - else - { - printf("+++System : Not enough memory\n"); - } - - return(EXIT_FAILURE); - } - -# ifndef SEMAPHORES_NOMMES - if (pthread_setspecific(semaphore_fork_processus_courant, - &((*s_etat_processus).semaphore_fork)) != 0) -# else - if (pthread_setspecific(semaphore_fork_processus_courant, - (*s_etat_processus).semaphore_fork) != 0) -# endif - { - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Mémoire insuffisante\n"); - } - else - { - printf("+++System : Not enough memory\n"); - } - - return(EXIT_FAILURE); - } - // Initialisation d'une pile de signal pour récupérer les // débordement de pile @@ -416,12 +356,8 @@ rplinit(int argc, char *argv[], unsigned } # endif -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption1; -# else action.sa_handler = interruption1; -# endif - action.sa_flags = SA_ONSTACK | SA_SIGINFO; + action.sa_flags = SA_ONSTACK; if (sigaction(SIGINT, &action, NULL) != 0) { @@ -497,12 +433,8 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption2; -# else action.sa_handler = interruption2; -# endif - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + action.sa_flags = SA_NODEFER | SA_ONSTACK; if (sigaction(SIGTSTP, &action, NULL) != 0) { @@ -539,222 +471,8 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption4; -# else - action.sa_handler = interruption4; -# endif - // SIGCONT ne doit pas pouvoir être appelé depuis son gestionnaire. - action.sa_flags = SA_ONSTACK | SA_SIGINFO; - - if (sigaction(SIGSTART, &action, NULL) != 0) - { - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - signal_test = SIGTEST; - kill(getpid(), SIGSTART); - - if (signal_test != SIGSTART) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - if (sigaction(SIGCONT, &action, NULL) != 0) - { - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - signal_test = SIGTEST; - kill(getpid(), SIGCONT); - - if (signal_test != SIGCONT) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption5; -# else action.sa_handler = interruption5; -# endif - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; - - if (sigaction(SIGFSTOP, &action, NULL) != 0) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - signal_test = SIGTEST; - kill(getpid(), SIGFSTOP); - - if (signal_test != SIGFSTOP) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption11; -# else - action.sa_handler = interruption11; -# endif - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; - - if (sigaction(SIGFABORT, &action, NULL) != 0) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - signal_test = SIGTEST; - kill(getpid(), SIGFABORT); - - if (signal_test != SIGFABORT) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption8; -# else - action.sa_handler = interruption8; -# endif - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; - - if (sigaction(SIGURG, &action, NULL) != 0) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - signal_test = SIGTEST; - kill(getpid(), SIGURG); - - if (signal_test != SIGURG) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption7; -# else - action.sa_handler = interruption7; -# endif - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + action.sa_flags = SA_NODEFER | SA_ONSTACK; if (sigaction(SIGPIPE, &action, NULL) != 0) { @@ -793,100 +511,8 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption6; -# else - action.sa_handler = interruption6; -# endif - action.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_NODEFER; - - if (sigaction(SIGINJECT, &action, NULL) != 0) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - signal_test = SIGTEST; - kill(getpid(), SIGINJECT); - - if (signal_test != SIGINJECT) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption9; -# else - action.sa_handler = interruption9; -# endif - action.sa_flags = SA_ONSTACK | SA_SIGINFO | SA_NODEFER; - - if (sigaction(SIGABORT, &action, NULL) != 0) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - - signal_test = SIGTEST; - kill(getpid(), SIGABORT); - - if (signal_test != SIGABORT) - { - erreur = d_es_signal; - - if ((*s_etat_processus).langue == 'F') - { - printf("+++Système : Initialisation des signaux POSIX " - "impossible\n"); - } - else - { - printf("+++System : Initialization of POSIX signals failed\n"); - } - - return(EXIT_FAILURE); - } - -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption1; -# else action.sa_handler = interruption1; -# endif - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + action.sa_flags = SA_NODEFER | SA_ONSTACK; if (sigaction(SIGALRM, &action, NULL) != 0) { @@ -925,12 +551,7 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption3; -# else - action.sa_handler = interruption3; -# endif - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + signal_test = SIGTEST + 1; erreur = d_absence_erreur; core = d_faux; @@ -978,6 +599,15 @@ rplinit(int argc, char *argv[], unsigned option_t = d_faux; option_v = d_faux; + // Lorsque le programme est appelé depuis un shebang, argv[0] contient + // le chemin du programme et argv[1] tous les arguments. + // argv[2] contient quant à lui le nom du script RPL/2. + // + // Exemple : + // argv[0] : /usr/local/bin/rpl + // argv[1] : -csdp -t 800 + // argv[2] : ./on_exit.rpl + while((--argc) > 0) { if ((*(++argv))[0] == '-') @@ -1773,6 +1403,12 @@ rplinit(int argc, char *argv[], unsigned break; } + case '-': + case ' ': + { + break; + } + default : { if ((*s_etat_processus).langue == 'F') @@ -1819,6 +1455,9 @@ rplinit(int argc, char *argv[], unsigned if (debug == d_faux) { + action.sa_handler = interruption3; + action.sa_flags = SA_NODEFER | SA_ONSTACK; + if (sigaction(SIGSEGV, &action, NULL) != 0) { if ((*s_etat_processus).langue == 'F') @@ -1854,12 +1493,8 @@ rplinit(int argc, char *argv[], unsigned if (option_n == d_vrai) { -# ifndef _BROKEN_SIGINFO - action.sa_sigaction = interruption10; -# else - action.sa_handler = interruption10; -# endif - action.sa_flags = SA_ONSTACK | SA_SIGINFO; + action.sa_handler = interruption4; + action.sa_flags = SA_ONSTACK; if (sigaction(SIGHUP, &action, NULL) != 0) { @@ -2253,6 +1888,8 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).var_volatile_recursivite = 0; (*s_etat_processus).var_volatile_exception_gsl = 0; (*s_etat_processus).arret_depuis_abort = 0; + (*s_etat_processus).pointeur_signal_lecture = 0; + (*s_etat_processus).pointeur_signal_ecriture = 0; initialisation_allocateur(s_etat_processus); initialisation_drapeaux(s_etat_processus); @@ -3157,27 +2794,30 @@ rplinit(int argc, char *argv[], unsigned { if ((*s_etat_processus).var_volatile_alarme != 0) { - kill((*(*((struct_processus_fils *) + envoi_signal_processus( + (*(*((struct_processus_fils *) (*(*((struct_liste_chainee *) l_element_courant)).donnee).objet)) - .thread).pid, SIGURG); + .thread).pid, rpl_sigurg); } else { if ((*s_etat_processus).arret_depuis_abort == -1) { - kill((*(*((struct_processus_fils *) + envoi_signal_processus( + (*(*((struct_processus_fils *) (*(*((struct_liste_chainee *) l_element_courant)).donnee).objet)) - .thread).pid, SIGFABORT); + .thread).pid, rpl_sigabort); } else { - kill((*(*((struct_processus_fils *) + envoi_signal_processus( + (*(*((struct_processus_fils *) (*(*((struct_liste_chainee *) l_element_courant)).donnee).objet)) - .thread).pid, SIGFSTOP); + .thread).pid, rpl_sigstop); } } } @@ -3196,31 +2836,32 @@ rplinit(int argc, char *argv[], unsigned if ((*s_etat_processus).var_volatile_alarme != 0) { - pthread_kill((*(*((struct_processus_fils *) + envoi_signal_thread( + (*(*((struct_processus_fils *) (*(*((struct_liste_chainee *) l_element_courant)).donnee).objet)) - .thread).tid, SIGURG); + .thread).tid, rpl_sigurg); } else { if ((*s_etat_processus).arret_depuis_abort == -1) { - pthread_kill( + envoi_signal_thread( (*(*((struct_processus_fils *) (*(*((struct_liste_chainee *) l_element_courant)).donnee) .objet)).thread).tid, - SIGFABORT); + rpl_sigabort); } else { - pthread_kill( + envoi_signal_thread( (*(*((struct_processus_fils *) (*(*((struct_liste_chainee *) l_element_courant)).donnee) .objet)).thread).tid, - SIGFSTOP); + rpl_sigstop); } } } @@ -3319,6 +2960,7 @@ rplinit(int argc, char *argv[], unsigned } nanosleep(&attente, NULL); + scrutation_interruptions(s_etat_processus); pthread_mutex_lock(&((*s_etat_processus).mutex)); } @@ -3845,36 +3487,28 @@ rplinit(int argc, char *argv[], unsigned pthread_mutex_destroy(&((*s_etat_processus).mutex_allocation)); # ifndef SEMAPHORES_NOMMES - sem_post(&((*s_etat_processus).semaphore_fork)); - sem_destroy(&((*s_etat_processus).semaphore_fork)); + sem_post(&((*s_etat_processus).semaphore_fork); + sem_destroy(&((*s_etat_processus).semaphore_fork); # else sem_post((*s_etat_processus).semaphore_fork); - sem_destroy2((*s_etat_processus).semaphore_fork, sem_fork); + sem_destroy3((*s_etat_processus).semaphore_fork, getpid(), pthread_self(), + SEM_FORK); # endif free((*s_etat_processus).localisation); -# ifndef SEMAPHORES_NOMMES - sem_destroy(&semaphore_liste_threads); -# else - sem_destroy2(semaphore_liste_threads, sem_liste_threads); -# endif + pthread_mutex_destroy(&mutex_liste_threads); + pthread_mutex_destroy(&mutex_gestionnaires_signaux_atomique); + # ifndef SEMAPHORES_NOMMES sem_post(&semaphore_gestionnaires_signaux); sem_destroy(&semaphore_gestionnaires_signaux); - sem_destroy(&semaphore_gestionnaires_signaux_atomique); # else sem_post(semaphore_gestionnaires_signaux); - sem_destroy2(semaphore_gestionnaires_signaux, sem_gestionnaires_signaux); - sem_destroy2(semaphore_gestionnaires_signaux_atomique, - sem_gestionnaires_signaux_atomique); -# endif - -# ifdef _BROKEN_SIGINFO - destruction_fifos_signaux(s_etat_processus); -# undef return + sem_destroy2(semaphore_gestionnaires_signaux, getpid(), SEM_SIGNAUX); # endif + destruction_queue_signaux(s_etat_processus); liberation_contexte_cas(s_etat_processus); free((*s_etat_processus).chemin_fichiers_temporaires);