--- rpl/src/rpl.c 2011/07/25 07:45:00 1.75 +++ rpl/src/rpl.c 2011/09/20 15:45:53 1.92 @@ -1,6 +1,6 @@ /* ================================================================================ - RPL/2 (R) version 4.1.2 + RPL/2 (R) version 4.1.3 Copyright (C) 1989-2011 Dr. BERTRAND Joël This file is part of RPL/2. @@ -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; @@ -182,9 +171,12 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).langue = 'E'; } + initialisation_contexte_cas(s_etat_processus); + (*s_etat_processus).exception = d_ep; (*s_etat_processus).erreur_systeme = d_es; (*s_etat_processus).erreur_execution = d_ex; + (*s_etat_processus).compteur_violation_d_acces = 0; (*s_etat_processus).rpl_home = rpl_home; @@ -200,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); @@ -239,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); @@ -335,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 @@ -413,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) { @@ -437,7 +376,10 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - if (sigaction(SIGTERM, &action, NULL) != 0) + signal_test = SIGTEST; + kill(getpid(), SIGINT); + + if (signal_test != SIGINT) { erreur = d_es_signal; @@ -454,53 +396,10 @@ 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; - - if (sigaction(SIGTSTP, &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); - } - -# 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 (sigaction(SIGTERM, &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); - } + erreur = d_es_signal; - if (sigaction(SIGCONT, &action, NULL) != 0) - { if ((*s_etat_processus).langue == 'F') { printf("+++Système : Initialisation des signaux POSIX " @@ -514,14 +413,10 @@ rplinit(int argc, char *argv[], unsigned 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; + signal_test = SIGTEST; + kill(getpid(), SIGTERM); - if (sigaction(SIGFSTOP, &action, NULL) != 0) + if (signal_test != SIGTERM) { erreur = d_es_signal; @@ -538,17 +433,11 @@ rplinit(int argc, char *argv[], unsigned 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; + action.sa_handler = interruption2; + action.sa_flags = SA_NODEFER | SA_ONSTACK; - if (sigaction(SIGFABORT, &action, NULL) != 0) + if (sigaction(SIGTSTP, &action, NULL) != 0) { - erreur = d_es_signal; - if ((*s_etat_processus).langue == 'F') { printf("+++Système : Initialisation des signaux POSIX " @@ -562,14 +451,10 @@ rplinit(int argc, char *argv[], unsigned 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; + signal_test = SIGTEST; + kill(getpid(), SIGTSTP); - if (sigaction(SIGURG, &action, NULL) != 0) + if (signal_test != SIGTSTP) { erreur = d_es_signal; @@ -586,12 +471,8 @@ rplinit(int argc, char *argv[], unsigned 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_handler = interruption5; + action.sa_flags = SA_NODEFER | SA_ONSTACK; if (sigaction(SIGPIPE, &action, NULL) != 0) { @@ -610,14 +491,10 @@ 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; + signal_test = SIGTEST; + kill(getpid(), SIGPIPE); - if (sigaction(SIGINJECT, &action, NULL) != 0) + if (signal_test != SIGPIPE) { erreur = d_es_signal; @@ -634,14 +511,10 @@ rplinit(int argc, char *argv[], unsigned 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; + action.sa_handler = interruption1; + action.sa_flags = SA_NODEFER | SA_ONSTACK; - if (sigaction(SIGABORT, &action, NULL) != 0) + if (sigaction(SIGALRM, &action, NULL) != 0) { erreur = d_es_signal; @@ -658,14 +531,10 @@ rplinit(int argc, char *argv[], unsigned 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; + signal_test = SIGTEST; + kill(getpid(), SIGALRM); - if (sigaction(SIGALRM, &action, NULL) != 0) + if (signal_test != SIGALRM) { erreur = d_es_signal; @@ -682,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; @@ -735,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] == '-') @@ -1530,6 +1403,12 @@ rplinit(int argc, char *argv[], unsigned break; } + case '-': + case ' ': + { + break; + } + default : { if ((*s_etat_processus).langue == 'F') @@ -1576,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') @@ -1611,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) { @@ -2010,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); @@ -2763,6 +2643,20 @@ rplinit(int argc, char *argv[], unsigned if (setjmp(contexte_initial) == 0) { erreur = sequenceur(s_etat_processus); + + if (erreur == d_absence_erreur) + { + if (((*s_etat_processus).var_volatile_alarme + == 0) && ((*s_etat_processus) + .arret_depuis_abort == 0) && + ((*s_etat_processus).at_exit + != NULL)) + { + erreur = evaluation(s_etat_processus, + (*s_etat_processus).at_exit, + 'E'); + } + } } } else @@ -2770,18 +2664,20 @@ rplinit(int argc, char *argv[], unsigned if (setjmp(contexte_initial) == 0) { erreur = sequenceur_optimise(s_etat_processus); - } - } - if (erreur == d_absence_erreur) - { - if (((*s_etat_processus).var_volatile_alarme == 0) - && ((*s_etat_processus).arret_depuis_abort - == 0) && ((*s_etat_processus).at_exit - != NULL)) - { - erreur = evaluation(s_etat_processus, - (*s_etat_processus).at_exit, 'E'); + if (erreur == d_absence_erreur) + { + if (((*s_etat_processus).var_volatile_alarme + == 0) && ((*s_etat_processus) + .arret_depuis_abort == 0) && + ((*s_etat_processus).at_exit + != NULL)) + { + erreur = evaluation(s_etat_processus, + (*s_etat_processus).at_exit, + 'E'); + } + } } } } @@ -2898,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); } } } @@ -2937,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); } } } @@ -3060,6 +2960,7 @@ rplinit(int argc, char *argv[], unsigned } nanosleep(&attente, NULL); + scrutation_interruptions(s_etat_processus); pthread_mutex_lock(&((*s_etat_processus).mutex)); } @@ -3586,35 +3487,29 @@ 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); + sem_destroy2(semaphore_gestionnaires_signaux, getpid(), SEM_SIGNAUX); # endif -# ifdef _BROKEN_SIGINFO - destruction_fifos_signaux(s_etat_processus); -# undef return -# endif + destruction_queue_signaux(s_etat_processus); + liberation_contexte_cas(s_etat_processus); free((*s_etat_processus).chemin_fichiers_temporaires); free(s_etat_processus);