--- rpl/src/rpl.c 2010/03/04 17:42:47 1.11 +++ rpl/src/rpl.c 2012/10/07 08:18:36 1.121 @@ -1,7 +1,7 @@ /* ================================================================================ - RPL/2 (R) version 4.0.12 - Copyright (C) 1989-2010 Dr. BERTRAND Joël + RPL/2 (R) version 4.1.11 + Copyright (C) 1989-2012 Dr. BERTRAND Joël This file is part of RPL/2. @@ -21,7 +21,7 @@ #define MAIN_RPL -#include "rpl.conv.h" +#include "rpl-conv.h" /* @@ -31,16 +31,25 @@ */ int -rplinit(int argc, char *argv[], unsigned char ***resultats, char *rpl_home) +rplinit(int argc, char *argv[], char *envp[], + unsigned char ***resultats, char *rpl_home) { -# include "copyright.conv.h" -# include "licence.conv.h" +# include "copyright-conv.h" +# include "licence-conv.h" + + char **arg_exec; + +# ifdef HAVE_STACK_OVERFLOW_RECOVERY + char pile_signaux[SIGSTKSZ]; +# endif + +# define RPL_PATH_MAX 1024 + char repertoire_initial[RPL_PATH_MAX]; file *f_source; int erreur_historique; int option_P; - int status; logical1 core; logical1 debug; @@ -71,6 +80,9 @@ rplinit(int argc, char *argv[], unsigned struct_processus *s_etat_processus; + struct_liste_variables_statiques *l_element_statique_courant; + struct_liste_variables_statiques *l_element_statique_suivant; + struct_table_variables_partagees s_variables_partagees; struct sigaction action; @@ -99,43 +111,172 @@ rplinit(int argc, char *argv[], unsigned volatile int erreur; volatile unsigned char traitement_fichier_temporaire; + errno = 0; + s_queue_signaux = NULL; + routine_recursive = 0; + pid_processus_pere = getpid(); + +# ifdef DEBUG_MEMOIRE + debug_memoire_initialisation(); +# endif + setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); - sem_init(&semaphore_liste_threads, 0, 1); +# ifndef SEMAPHORES_NOMMES sem_init(&semaphore_gestionnaires_signaux, 0, 0); - sem_init(&semaphore_gestionnaires_signaux_atomique, 0, 1); +# else + semaphore_gestionnaires_signaux = sem_init2(0, getpid(), SEM_SIGNAUX); + + if (semaphore_gestionnaires_signaux == SEM_FAILED) + { + erreur = d_es_allocation_memoire; + + if ((langue = getenv("LANG")) != NULL) + { + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Système : Mémoire insuffisante\n"); + } + else + { + uprintf("+++System : Not enough memory\n"); + } + } + else + { + uprintf("+++System : Not enough memory\n"); + } + + return(EXIT_FAILURE); + } +# endif if ((s_etat_processus = malloc(sizeof(struct_processus))) == NULL) { erreur = d_es_allocation_memoire; - if ((*s_etat_processus).langue == 'F') + if ((langue = getenv("LANG")) != NULL) + { + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Système : Mémoire insuffisante\n"); + } + else + { + uprintf("+++System : Not enough memory\n"); + } + } + else + { + uprintf("+++System : Not enough memory\n"); + } + + return(EXIT_FAILURE); + } + + if ((langue = getenv("LANG")) != NULL) + { + (*s_etat_processus).langue = (strncmp(langue, "fr", 2) == 0) + ? 'F' : 'E'; + } + else + { + (*s_etat_processus).langue = 'E'; + } + + if (getcwd(repertoire_initial, RPL_PATH_MAX) == NULL) + { + if ((langue = getenv("LANG")) != NULL) + { + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Système : Mémoire insuffisante\n"); + } + else + { + uprintf("+++System : Not enough memory\n"); + } + } + else + { + uprintf("+++System : Not enough memory\n"); + } + + return(EXIT_FAILURE); + } + + if ((arg_exec = malloc((argc + 1) * sizeof(char *))) == NULL) + { + if ((langue = getenv("LANG")) != NULL) { - printf("+++Système : Mémoire insuffisante\n"); + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Système : Mémoire insuffisante\n"); + } + else + { + uprintf("+++System : Not enough memory\n"); + } } else { - printf("+++System : Not enough memory\n"); + uprintf("+++System : Not enough memory\n"); } return(EXIT_FAILURE); } + for(i = 0; i < (unsigned long) argc; i++) + { + arg_exec[i] = argv[i]; + } + + argv[argc] = NULL; + + 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).requete_redemarrage = d_faux; (*s_etat_processus).rpl_home = rpl_home; - insertion_thread(s_etat_processus, d_vrai); - pthread_mutexattr_init(&attributs_mutex); pthread_mutexattr_settype(&attributs_mutex, PTHREAD_MUTEX_NORMAL); pthread_mutex_init(&((*s_etat_processus).mutex), &attributs_mutex); pthread_mutexattr_destroy(&attributs_mutex); - sem_init(&((*s_etat_processus).semaphore_fork), 0, 0); + pthread_mutexattr_init(&attributs_mutex); + pthread_mutexattr_settype(&attributs_mutex, PTHREAD_MUTEX_NORMAL); + pthread_mutex_init(&((*s_etat_processus).mutex_allocation), + &attributs_mutex); + pthread_mutexattr_destroy(&attributs_mutex); + + pthread_mutexattr_init(&attributs_mutex); + pthread_mutexattr_settype(&attributs_mutex, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&mutex_sections_critiques, &attributs_mutex); + pthread_mutexattr_destroy(&attributs_mutex); + +# ifndef SEMAPHORES_NOMMES + sem_init(&((*s_etat_processus).semaphore_fork), 0, 0); +# else + if (((*s_etat_processus).semaphore_fork = sem_init3(0, getpid(), + pthread_self(), SEM_FORK)) == SEM_FAILED) + { + if ((*s_etat_processus).langue == 'F') + { + uprintf("+++Système : Mémoire insuffisante\n"); + } + else + { + uprintf("+++System : Not enough memory\n"); + } + + return(EXIT_FAILURE); + } +# endif pthread_mutexattr_init(&attributs_mutex); pthread_mutexattr_settype(&attributs_mutex, PTHREAD_MUTEX_NORMAL); @@ -155,30 +296,56 @@ rplinit(int argc, char *argv[], unsigned .mutex), &attributs_mutex); pthread_mutexattr_destroy(&attributs_mutex); - if ((langue = getenv("LANG")) != NULL) - { - (*s_etat_processus).langue = (strncmp(langue, "fr", 2) == 0) - ? 'F' : 'E'; - } - else - { - (*s_etat_processus).langue = 'E'; - } + (*s_etat_processus).chemin_fichiers_temporaires = + recherche_chemin_fichiers_temporaires(s_etat_processus); - localisation_courante(s_etat_processus); + insertion_thread(s_etat_processus, d_vrai); + creation_queue_signaux(s_etat_processus); +# ifndef OS2 + localisation_courante(s_etat_processus); +# else if ((*s_etat_processus).erreur_systeme != d_es) { - if ((*s_etat_processus).langue == 'F') + if (((*s_etat_processus).localisation = malloc((strlen(d_locale) + 1) * + sizeof(unsigned char))) == NULL) { - uprintf("+++Système : Mémoire insuffisante\n"); + if ((*s_etat_processus).langue == 'F') + { + uprintf("+++Système : Mémoire insuffisante\n"); + } + else + { + uprintf("+++System : Not enough memory\n"); + } + + return(EXIT_FAILURE); } - else + + strcpy((*s_etat_processus).localisation, d_locale); + } +# endif + + (*s_etat_processus).erreur_systeme = d_es; + + if ((*s_etat_processus).localisation == NULL) + { + if (((*s_etat_processus).localisation = malloc((strlen(d_locale) + 1) * + sizeof(unsigned char))) == NULL) { - 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); + strcpy((*s_etat_processus).localisation, d_locale); } printf("+++RPL/2 (R) version %s (%s)\n", d_version_rpl, @@ -186,11 +353,11 @@ rplinit(int argc, char *argv[], unsigned if ((*s_etat_processus).langue == 'F') { - printf("+++Copyright (C) 1989 à 2009, 2010 BERTRAND Joël\n"); + printf("+++Copyright (C) 1989 à 2011, 2012 BERTRAND Joël\n"); } else { - printf("+++Copyright (C) 1989 to 2009, 2010 BERTRAND Joel\n"); + printf("+++Copyright (C) 1989 to 2011, 2012 BERTRAND Joel\n"); } if (getenv("HOME") != NULL) @@ -215,83 +382,87 @@ 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 +# ifdef HAVE_STACK_OVERFLOW_RECOVERY + if (stackoverflow_install_handler(interruption_depassement_pile, + pile_signaux, sizeof(pile_signaux)) != 0) { - printf("+++System : Not enough memory\n"); - } + erreur = d_es_signal; - return(EXIT_FAILURE); - } + if ((*s_etat_processus).langue == 'F') + { + printf("+++Système : Initialisation de la pile alternative " + "impossible\n"); + } + else + { + printf("+++System : Initialization of alternate " + "stack failed\n"); + } - if (pthread_setspecific(semaphore_fork_processus_courant, - &((*s_etat_processus).semaphore_fork)) != 0) - { + return(EXIT_FAILURE); + } +# else if ((*s_etat_processus).langue == 'F') { - printf("+++Système : Mémoire insuffisante\n"); + printf("+++Attention : Le système ne supporte pas de pile " + "alternative\n"); } else { - printf("+++System : Not enough memory\n"); + printf("+++Warning : Operating system does not support alternate " + "stack\n"); } +# endif - return(EXIT_FAILURE); - } - - // Initialisation d'une pile de signal pour récupérer les - // débordement de pile + action.sa_handler = interruption1; + action.sa_flags = 0; -# ifndef Cygwin - if (((*s_etat_processus).pile_signal.ss_sp = - malloc((*s_etat_processus).pile_signal.ss_size = - SIGSTKSZ)) == NULL) + if (sigaction(SIGINT, &action, NULL) != 0) { - erreur = d_es_allocation_memoire; + erreur = d_es_signal; if ((*s_etat_processus).langue == 'F') { - printf("+++Système : Mémoire insuffisante\n"); + printf("+++Système : Initialisation des signaux POSIX " + "impossible\n"); } else { - printf("+++System : Not enough memory\n"); + printf("+++System : Initialization of POSIX signals failed\n"); } return(EXIT_FAILURE); } - (*s_etat_processus).pile_signal.ss_flags = 0; + signal_test = SIGTEST; + kill(getpid(), SIGINT); + + attente.tv_sec = 0; + attente.tv_nsec = 1000000; + + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) + { + nanosleep(&attente, NULL); + } - if (sigaltstack(&((*s_etat_processus).pile_signal), NULL) != 0) + if (signal_test != SIGINT) { erreur = d_es_signal; if ((*s_etat_processus).langue == 'F') { - printf("+++Système : Initialisation de la pile spécifique de signal" - " impossible\n"); + printf("+++Système : Initialisation des signaux POSIX " + "impossible\n"); } else { - printf("+++System : Initialization of signal stack failed\n"); + printf("+++System : Initialization of POSIX signals failed\n"); } return(EXIT_FAILURE); } -# endif - action.sa_sigaction = interruption1; - action.sa_flags = SA_ONSTACK | SA_SIGINFO; - - if (sigaction(SIGINT, &action, NULL) != 0) + if (sigaction(SIGTERM, &action, NULL) != 0) { erreur = d_es_signal; @@ -308,11 +479,21 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption2; - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + signal_test = SIGTEST; + kill(getpid(), SIGTERM); - if (sigaction(SIGTSTP, &action, NULL) != 0) + attente.tv_sec = 0; + attente.tv_nsec = 1000000; + + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) + { + nanosleep(&attente, NULL); + } + + if (signal_test != SIGTERM) { + erreur = d_es_signal; + if ((*s_etat_processus).langue == 'F') { printf("+++Système : Initialisation des signaux POSIX " @@ -326,11 +507,10 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption4; - action.sa_flags = SA_ONSTACK | SA_SIGINFO; - - if (sigaction(SIGSTART, &action, NULL) != 0) + if (sigaction(SIGALRM, &action, NULL) != 0) { + erreur = d_es_signal; + if ((*s_etat_processus).langue == 'F') { printf("+++Système : Initialisation des signaux POSIX " @@ -344,8 +524,21 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - if (sigaction(SIGCONT, &action, NULL) != 0) + signal_test = SIGTEST; + kill(getpid(), SIGALRM); + + attente.tv_sec = 0; + attente.tv_nsec = 1000000; + + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) { + nanosleep(&attente, NULL); + } + + if (signal_test != SIGALRM) + { + erreur = d_es_signal; + if ((*s_etat_processus).langue == 'F') { printf("+++Système : Initialisation des signaux POSIX " @@ -359,13 +552,11 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption5; - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + action.sa_handler = interruption2; + action.sa_flags = 0; - if (sigaction(SIGFSTOP, &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 " @@ -379,10 +570,18 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption8; - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + signal_test = SIGTEST; + kill(getpid(), SIGTSTP); + + attente.tv_sec = 0; + attente.tv_nsec = 1000000; - if (sigaction(SIGURG, &action, NULL) != 0) + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) + { + nanosleep(&attente, NULL); + } + + if (signal_test != SIGTSTP) { erreur = d_es_signal; @@ -399,8 +598,8 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption7; - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + action.sa_handler = interruption5; + action.sa_flags = 0; if (sigaction(SIGPIPE, &action, NULL) != 0) { @@ -419,10 +618,18 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption6; - action.sa_flags = SA_ONSTACK | SA_SIGINFO; + signal_test = SIGTEST; + kill(getpid(), SIGPIPE); + + attente.tv_sec = 0; + attente.tv_nsec = 1000000; + + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) + { + nanosleep(&attente, NULL); + } - if (sigaction(SIGINJECT, &action, NULL) != 0) + if (signal_test != SIGPIPE) { erreur = d_es_signal; @@ -439,10 +646,10 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption9; - action.sa_flags = SA_ONSTACK | SA_SIGINFO; + action.sa_handler = interruption1; + action.sa_flags = 0; - if (sigaction(SIGABORT, &action, NULL) != 0) + if (sigaction(SIGUSR1, &action, NULL) != 0) { erreur = d_es_signal; @@ -459,10 +666,18 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption1; - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + signal_test = SIGTEST; + kill(getpid(), SIGUSR1); - if (sigaction(SIGALRM, &action, NULL) != 0) + attente.tv_sec = 0; + attente.tv_nsec = 1000000; + + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) + { + nanosleep(&attente, NULL); + } + + if (signal_test != SIGUSR1) { erreur = d_es_signal; @@ -479,13 +694,9 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - action.sa_sigaction = interruption3; - action.sa_flags = SA_NODEFER | SA_ONSTACK | SA_SIGINFO; + signal_test = SIGTEST + 1; - (*s_etat_processus).chemin_fichiers_temporaires = - recherche_chemin_fichiers_temporaires(s_etat_processus); - - erreur = d_os; + erreur = d_absence_erreur; core = d_faux; mode_interactif = d_faux; (*s_etat_processus).nom_fichier_source = NULL; @@ -506,7 +717,7 @@ rplinit(int argc, char *argv[], unsigned if (argc == 1) { - erreur = d_os_ligne_de_commande; + erreur = d_erreur; informations(s_etat_processus); } else @@ -531,6 +742,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] == '-') @@ -558,31 +778,6 @@ rplinit(int argc, char *argv[], unsigned } option_a = d_vrai; - printf("\n"); - - if ((*s_etat_processus).langue == 'F') - { - printf(" Auteur: Joël BERTRAND,\n"); - printf(" Email : " - "joel.bertrand@systella.fr\n"); - printf(" Liste de diffusion : " - "rpl2@systella.fr\n"); - printf(" Page : " - "http://www.rpl2.fr"); - printf("\n"); - } - else - { - printf(" Author: Joël BERTRAND,\n"); - printf(" Email : " - "joel.bertrand@systella.fr\n"); - printf(" Mailing list : " - "rpl2@systella.fr\n"); - printf(" Web page : " - "http://www.rpl2/net"); - printf("\n"); - } - break; } @@ -1297,6 +1492,7 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } + free(type_debug); break; } @@ -1350,6 +1546,12 @@ rplinit(int argc, char *argv[], unsigned break; } + case '-': + case ' ': + { + break; + } + default : { if ((*s_etat_processus).langue == 'F') @@ -1384,7 +1586,7 @@ rplinit(int argc, char *argv[], unsigned printf("+++Error : More than one definition\n"); } - erreur = d_os_ligne_de_commande; + erreur = d_erreur; } else { @@ -1394,8 +1596,37 @@ rplinit(int argc, char *argv[], unsigned } } + /* + * Dans le cas où le programme est appelé avec l'option -d, + * on ne récupère par les signaux de violation d'accès. On + * tente simplement la récupération des dépassements de pile. + */ + if (debug == d_faux) { + +# ifdef HAVE_SIGSEGV_RECOVERY + if (sigsegv_install_handler(interruption_violation_access) != 0) + { + erreur = d_es_signal; + + if ((*s_etat_processus).langue == 'F') + { + printf("+++Système : Initialisation de la pile alternative " + "impossible\n"); + } + else + { + printf("+++System : Initialization of alternate " + "stack failed\n"); + } + + return(EXIT_FAILURE); + } +# else + action.sa_handler = interruption3; + action.sa_flags = 0; + if (sigaction(SIGSEGV, &action, NULL) != 0) { if ((*s_etat_processus).langue == 'F') @@ -1412,6 +1643,39 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } + signal_test = SIGTEST; + kill(getpid(), SIGSEGV); + + attente.tv_sec = 0; + attente.tv_nsec = 1000000; + + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) + { + nanosleep(&attente, NULL); + } + + if (signal_test != SIGSEGV) + { + 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); + } +# endif + + action.sa_handler = interruption3; + action.sa_flags = 0; + if (sigaction(SIGBUS, &action, NULL) != 0) { if ((*s_etat_processus).langue == 'F') @@ -1427,12 +1691,42 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } + + signal_test = SIGTEST; + kill(getpid(), SIGBUS); + + attente.tv_sec = 0; + attente.tv_nsec = 1000000; + + for(i = 0; (i < 1000) && (signal_test == SIGTEST); i++) + { + nanosleep(&attente, NULL); + } + + if (signal_test != SIGBUS) + { + 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 (option_n == d_vrai) { - action.sa_sigaction = interruption10; - action.sa_flags = SA_ONSTACK | SA_SIGINFO; + action.sa_handler = interruption4; + action.sa_flags = 0; if (sigaction(SIGHUP, &action, NULL) != 0) { @@ -1586,7 +1880,7 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).nom_fichier_source); } - erreur = d_os_fichier_introuvable; + erreur = d_erreur; } else { @@ -1633,7 +1927,7 @@ rplinit(int argc, char *argv[], unsigned } } - if ((erreur == d_os) && (presence_definition == 'O')) + if ((erreur == d_absence_erreur) && (presence_definition == 'O')) { (*s_etat_processus).profilage = (option_P != 0) ? d_vrai : d_faux; (*s_etat_processus).niveau_profilage = option_P; @@ -1642,15 +1936,27 @@ rplinit(int argc, char *argv[], unsigned gettimeofday(&((*s_etat_processus).horodatage_profilage), NULL); (*s_etat_processus).liste_mutexes = NULL; + (*s_etat_processus).sections_critiques = 0; (*s_etat_processus).test_instruction = 'N'; (*s_etat_processus).nombre_arguments = 0; (*s_etat_processus).affichage_arguments = 'N'; (*s_etat_processus).autorisation_conversion_chaine = 'Y'; (*s_etat_processus).autorisation_evaluation_nom = 'Y'; + + if (mode_interactif == d_vrai) + { + (*s_etat_processus).autorisation_nom_implicite = 'Y'; + } + else + { + (*s_etat_processus).autorisation_nom_implicite = 'N'; + } + (*s_etat_processus).autorisation_empilement_programme = 'N'; (*s_etat_processus).requete_arret = 'N'; (*s_etat_processus).evaluation_forcee = 'N'; + (*s_etat_processus).recherche_type = 'N'; (*s_etat_processus).constante_symbolique = 'N'; (*s_etat_processus).traitement_symbolique = 'N'; @@ -1662,19 +1968,16 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).l_base_pile = NULL; (*s_etat_processus).l_base_pile_last = NULL; - (*s_etat_processus).s_liste_variables = NULL; + (*s_etat_processus).s_arbre_variables = NULL; + (*s_etat_processus).l_liste_variables_par_niveau = NULL; (*s_etat_processus).gel_liste_variables = d_faux; - (*s_etat_processus).nombre_variables = 0; - (*s_etat_processus).nombre_variables_allouees = 0; - (*s_etat_processus).s_liste_variables_statiques = NULL; - (*s_etat_processus).nombre_variables_statiques = 0; - (*s_etat_processus).nombre_variables_statiques_allouees = 0; + (*s_etat_processus).pointeur_variable_courante = NULL; + (*s_etat_processus).pointeur_variable_statique_courante = NULL; + (*s_etat_processus).l_liste_variables_statiques = NULL; (*s_etat_processus).niveau_courant = 0; (*s_etat_processus).niveau_initial = 0; (*s_etat_processus).creation_variables_statiques = d_faux; (*s_etat_processus).creation_variables_partagees = d_faux; - (*s_etat_processus).position_variable_courante = 0; - (*s_etat_processus).position_variable_statique_courante = 0; (*s_etat_processus).s_bibliotheques = NULL; (*s_etat_processus).s_instructions_externes = NULL; @@ -1795,13 +2098,19 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).pile_origine_interruptions[i] = NULL; } + (*s_etat_processus).at_exit = NULL; + (*s_etat_processus).at_poke = NULL; + (*s_etat_processus).traitement_at_poke = 'N'; + (*s_etat_processus).pointeurs_caracteres = NULL; (*s_etat_processus).arbre_instructions = NULL; (*s_etat_processus).tid_processus_pere = pthread_self(); + (*s_etat_processus).tid = pthread_self(); (*s_etat_processus).pid_processus_pere = getpid(); (*s_etat_processus).processus_detache = d_vrai; (*s_etat_processus).var_volatile_processus_pere = -1; + (*s_etat_processus).var_volatile_processus_racine = -1; (*s_etat_processus).var_volatile_traitement_retarde_stop = 0; (*s_etat_processus).var_volatile_alarme = 0; (*s_etat_processus).var_volatile_requete_arret = 0; @@ -1810,9 +2119,14 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).var_volatile_traitement_sigint = 0; (*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); + initialisation_variables(s_etat_processus); + initialisation_instructions(s_etat_processus); if ((*s_etat_processus).erreur_systeme != d_es) { @@ -1829,7 +2143,7 @@ rplinit(int argc, char *argv[], unsigned } if (((*s_etat_processus).instruction_derniere_erreur = - malloc(2 * sizeof(unsigned char))) == NULL) + malloc(sizeof(unsigned char))) == NULL) { erreur = d_es_allocation_memoire; @@ -1880,7 +2194,7 @@ rplinit(int argc, char *argv[], unsigned free((*s_etat_processus).instruction_courante); - if ((*s_etat_processus).erreur_systeme == d_es_allocation_memoire) + if ((*s_etat_processus).erreur_systeme != d_es) { erreur = d_es_allocation_memoire; } @@ -1889,12 +2203,10 @@ rplinit(int argc, char *argv[], unsigned (*((*s_etat_processus).l_base_pile_systeme)) .retour_definition = 'Y'; - (*s_etat_processus).indep = (struct_objet *) malloc( - sizeof(struct_objet)); - (*s_etat_processus).depend = (struct_objet *) malloc( - sizeof(struct_objet)); + (*s_etat_processus).indep = allocation(s_etat_processus, NON); + (*s_etat_processus).depend = allocation(s_etat_processus, NON); (*s_etat_processus).parametres_courbes_de_niveau = - (struct_objet *) malloc(sizeof(struct_objet)); + allocation(s_etat_processus, NON); if (((*s_etat_processus).indep != NULL) && ((*s_etat_processus).depend != NULL) && @@ -2198,7 +2510,7 @@ rplinit(int argc, char *argv[], unsigned (*s_etat_processus).position_courante = 0; (*s_etat_processus).traitement_cycle_exit = 'N'; - if ((*s_etat_processus).nombre_variables == 0) + if ((*s_etat_processus).s_arbre_variables == NULL) { if ((*s_etat_processus).langue == 'F') { @@ -2258,9 +2570,8 @@ rplinit(int argc, char *argv[], unsigned return(EXIT_FAILURE); } - if ((*s_etat_processus).s_liste_variables - [(*s_etat_processus) - .position_variable_courante].niveau != 0) + if ((*(*s_etat_processus).pointeur_variable_courante) + .niveau != 0) { if ((*s_etat_processus).langue == 'F') { @@ -2305,6 +2616,11 @@ rplinit(int argc, char *argv[], unsigned home, ds_fichier_historique); using_history(); + + // Pour pouvoir utiliser le keymap avant le premier + // appel à readline(). + rl_initialize(); + erreur_historique = read_history( (*s_etat_processus).nom_fichier_historique); @@ -2346,8 +2662,6 @@ rplinit(int argc, char *argv[], unsigned fflush(stdout); - initialisation_instructions(s_etat_processus); - if (arguments != NULL) { tampon = (*s_etat_processus).definitions_chainees; @@ -2547,26 +2861,70 @@ rplinit(int argc, char *argv[], unsigned free(arguments); } - if (option_D == d_vrai) + if (option_a == d_vrai) { - lancement_daemon(s_etat_processus); + fprintf(stdout, "%s\n", (*s_etat_processus) + .definitions_chainees); } - - if (option_p == d_faux) + else { - if (setjmp(contexte_initial) == 0) + if (option_D == d_vrai) { - erreur = sequenceur(s_etat_processus); + lancement_daemon(s_etat_processus); } - } - else - { - if (setjmp(contexte_initial) == 0) + + if (option_p == d_faux) + { + 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 { - erreur = sequenceur_optimise(s_etat_processus); + 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'); + } + } + } } } + for(i = 0; i < (*s_etat_processus).sections_critiques; i++) + { + pthread_mutex_unlock(&mutex_sections_critiques); + } + + liberation(s_etat_processus, (*s_etat_processus).at_exit); + liberation(s_etat_processus, (*s_etat_processus).at_poke); + if ((*s_etat_processus).generateur_aleatoire != NULL) { liberation_generateur_aleatoire(s_etat_processus); @@ -2575,16 +2933,6 @@ rplinit(int argc, char *argv[], unsigned l_element_courant = (*s_etat_processus).liste_mutexes; while(l_element_courant != NULL) { - pthread_mutex_trylock(&((*((struct_mutex *) - (*(*((struct_liste_chainee *) - l_element_courant)).donnee).objet)).mutex)); - pthread_mutex_unlock(&((*((struct_mutex *) - (*(*((struct_liste_chainee *) - l_element_courant)).donnee).objet)).mutex)); - pthread_mutex_destroy(&((*((struct_mutex *) - (*(*((struct_liste_chainee *) - l_element_courant)).donnee).objet)).mutex)); - liberation(s_etat_processus, (*((struct_liste_chainee *) l_element_courant)).donnee); @@ -2676,17 +3024,31 @@ 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 { - kill((*(*((struct_processus_fils *) - (*(*((struct_liste_chainee *) - l_element_courant)).donnee).objet)) - .thread).pid, SIGFSTOP); + if ((*s_etat_processus).arret_depuis_abort + == -1) + { + envoi_signal_processus( + (*(*((struct_processus_fils *) + (*(*((struct_liste_chainee *) + l_element_courant)).donnee).objet)) + .thread).pid, rpl_sigabort); + } + else + { + envoi_signal_processus( + (*(*((struct_processus_fils *) + (*(*((struct_liste_chainee *) + l_element_courant)).donnee).objet)) + .thread).pid, rpl_sigstop); + } } } else @@ -2704,17 +3066,33 @@ 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 { - pthread_kill((*(*((struct_processus_fils *) - (*(*((struct_liste_chainee *) - l_element_courant)).donnee).objet)) - .thread).tid, SIGFSTOP); + if ((*s_etat_processus).arret_depuis_abort + == -1) + { + envoi_signal_thread( + (*(*((struct_processus_fils *) + (*(*((struct_liste_chainee *) + l_element_courant)).donnee) + .objet)).thread).tid, + rpl_sigabort); + } + else + { + envoi_signal_thread( + (*(*((struct_processus_fils *) + (*(*((struct_liste_chainee *) + l_element_courant)).donnee) + .objet)).thread).tid, + rpl_sigstop); + } } } @@ -2742,18 +3120,9 @@ rplinit(int argc, char *argv[], unsigned while((*s_etat_processus).l_base_pile_processus != NULL) { - status = 0; - l_element_courant = (void *) (*s_etat_processus).l_base_pile_processus; - if ((*s_etat_processus) - .nombre_interruptions_non_affectees != 0) - { - affectation_interruptions_logicielles( - s_etat_processus); - } - for(i = 0; i < (unsigned long) (*(*((struct_processus_fils *) (*(*((struct_liste_chainee *) @@ -2775,7 +3144,7 @@ rplinit(int argc, char *argv[], unsigned .thread).nombre_objets_dans_pipe--; action.sa_handler = SIG_IGN; - action.sa_flags = SA_ONSTACK; + action.sa_flags = 0; if (sigaction(SIGPIPE, &action, ®istre) != 0) @@ -2812,7 +3181,16 @@ rplinit(int argc, char *argv[], unsigned } pthread_mutex_unlock(&((*s_etat_processus).mutex)); + + if ((*s_etat_processus) + .nombre_interruptions_non_affectees != 0) + { + affectation_interruptions_logicielles( + s_etat_processus); + } + nanosleep(&attente, NULL); + scrutation_interruptions(s_etat_processus); pthread_mutex_lock(&((*s_etat_processus).mutex)); } @@ -2966,26 +3344,21 @@ rplinit(int argc, char *argv[], unsigned * le libérer... */ - for(i = 0; i < (*s_etat_processus).nombre_variables; i++) - { - liberation(s_etat_processus, - (*s_etat_processus).s_liste_variables[i].objet); - free((*s_etat_processus).s_liste_variables[i].nom); - } - - free((*s_etat_processus).s_liste_variables); - - for(i = 0; i < (*s_etat_processus) - .nombre_variables_statiques; i++) - { - liberation(s_etat_processus, (*s_etat_processus) - .s_liste_variables_statiques[i].objet); - free((*s_etat_processus) - .s_liste_variables_statiques[i].nom); + liberation_arbre_variables(s_etat_processus, + (*s_etat_processus).s_arbre_variables, d_vrai); + free((*s_etat_processus).pointeurs_caracteres_variables); + + l_element_statique_courant = (*s_etat_processus) + .l_liste_variables_statiques; + + while(l_element_statique_courant != NULL) + { + l_element_statique_suivant = + (*l_element_statique_courant).suivant; + free(l_element_statique_courant); + l_element_statique_courant = l_element_statique_suivant; } - free((*s_etat_processus).s_liste_variables_statiques); - for(i = 0; i < (*((*s_etat_processus) .s_liste_variables_partagees)).nombre_variables; i++) @@ -3190,7 +3563,17 @@ rplinit(int argc, char *argv[], unsigned fclose((*((struct_descripteur_fichier *) (*((struct_liste_chainee *) l_element_courant)).donnee)) - .descripteur); + .descripteur_c); + + if ((*((struct_descripteur_fichier *) + (*((struct_liste_chainee *) + l_element_courant)).donnee)).type != 'C') + { + sqlite3_close((*((struct_descripteur_fichier *) + (*((struct_liste_chainee *) + l_element_courant)).donnee)) + .descripteur_sqlite); + } if ((*((struct_descripteur_fichier *) (*((struct_liste_chainee *) @@ -3271,8 +3654,6 @@ rplinit(int argc, char *argv[], unsigned l_element_courant = l_element_suivant; } - free((*s_etat_processus).chemin_fichiers_temporaires); - l_element_courant = (*s_etat_processus).s_marques; while(l_element_courant != NULL) { @@ -3324,10 +3705,6 @@ rplinit(int argc, char *argv[], unsigned } } -# ifndef Cygwin - free((*s_etat_processus).pile_signal.ss_sp); -# endif - closelog(); pthread_mutex_destroy(&((*s_etat_processus).protection_liste_mutexes)); @@ -3337,22 +3714,60 @@ rplinit(int argc, char *argv[], unsigned retrait_thread(s_etat_processus); pthread_mutex_destroy(&((*s_etat_processus).mutex)); + pthread_mutex_destroy(&((*s_etat_processus).mutex_allocation)); + pthread_mutex_destroy(&mutex_sections_critiques); + +# ifndef SEMAPHORES_NOMMES sem_post(&((*s_etat_processus).semaphore_fork)); sem_destroy(&((*s_etat_processus).semaphore_fork)); +# else + sem_post((*s_etat_processus).semaphore_fork); + sem_destroy3((*s_etat_processus).semaphore_fork, getpid(), pthread_self(), + SEM_FORK); +# endif free((*s_etat_processus).localisation); - free(s_etat_processus); - sem_destroy(&semaphore_liste_threads); +# 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, getpid(), SEM_SIGNAUX); +# endif + + destruction_queue_signaux(s_etat_processus); + liberation_contexte_cas(s_etat_processus); + + free((*s_etat_processus).chemin_fichiers_temporaires); + + if ((*s_etat_processus).requete_redemarrage == d_vrai) + { + chdir(repertoire_initial); + execvp(arg_exec[0], &(arg_exec[0])); + erreur = d_erreur; + } + + free(arg_exec); + free(s_etat_processus); # ifdef DEBUG_MEMOIRE - debug_memoire_verification(s_etat_processus); + debug_memoire_verification(); + analyse_post_mortem(); +# endif + +# ifdef HAVE_STACK_OVERFLOW_RECOVERY + stackoverflow_deinstall_handler(); +# endif + +# ifdef HAVE_SIGSEGV_RECOVERY + if (debug == d_faux) + { + sigsegv_deinstall_handler(); + } # endif - return((erreur == d_os) ? EXIT_SUCCESS : EXIT_FAILURE); + return((erreur == d_absence_erreur) ? EXIT_SUCCESS : EXIT_FAILURE); } @@ -3364,7 +3779,7 @@ informations(struct_processus *s_etat_pr if ((*s_etat_processus).langue == 'F') { printf(" rpl [-options] [programme]\n"); - printf(" -a : informations sur l'auteur\n"); + printf(" -a : analyse du code\n"); printf(" -A : paramètres passés au programme principal\n"); printf(" -c : génération de fichier de débogage (rpl-core)\n"); printf(" -d : option de déverminage interne\n"); @@ -3383,7 +3798,7 @@ informations(struct_processus *s_etat_pr else { printf(" rpl [-options] [program]\n"); - printf(" -a : displays informations about the author\n"); + printf(" -a : analyzes program\n"); printf(" -A : sends parameters to main program\n"); printf(" -c : allows creation of a rpl-core file, providing a way" "\n" @@ -3407,4 +3822,72 @@ informations(struct_processus *s_etat_pr return; } + +logical1 +controle_integrite(struct_processus *s_etat_processus, + unsigned char *executable_candidat, unsigned char *executable) +{ + unsigned char *md5; + unsigned char *sha1; + + if (strcmp(executable, "rplpp") == 0) + { + md5 = rplpp_md5; + sha1 = rplpp_sha1; + } + else if (strcmp(executable, "rplfile") == 0) + { + md5 = rplfile_md5; + sha1 = rplfile_sha1; + } + else if (strcmp(executable, "rpliconv") == 0) + { + md5 = rpliconv_md5; + sha1 = rpliconv_sha1; + } + else if (strcmp(executable, "rplawk") == 0) + { + md5 = rplawk_md5; + sha1 = rplawk_sha1; + } + else if (strcmp(executable, "rplconvert") == 0) + { + md5 = rplconvert_md5; + sha1 = rplconvert_sha1; + } + else + { + return(d_faux); + } + + if (controle(s_etat_processus, executable_candidat, "md5", md5) != d_vrai) + { + return(d_faux); + } + + if (controle(s_etat_processus, executable_candidat, "sha1", sha1) != d_vrai) + { + return(d_faux); + } + + return(d_vrai); +} + + +unsigned char * +date_compilation() +{ + unsigned char *date; + + if ((date = malloc((strlen(d_date_en_rpl) + 1) * sizeof(unsigned char))) + == NULL) + { + return(NULL); + } + + strcpy(date, d_date_en_rpl); + + return(date); +} + // vim: ts=4