--- rpl/src/interruptions.c 2010/08/22 16:38:36 1.33 +++ rpl/src/interruptions.c 2011/09/14 14:34:28 1.67 @@ -1,7 +1,7 @@ /* ================================================================================ - RPL/2 (R) version 4.0.18 - Copyright (C) 1989-2010 Dr. BERTRAND Joël + RPL/2 (R) version 4.1.3 + Copyright (C) 1989-2011 Dr. BERTRAND Joël This file is part of RPL/2. @@ -183,9 +183,9 @@ insertion_thread_surveillance(struct_pro } } - pthread_mutex_lock(&((*s_argument_thread).mutex)); + pthread_mutex_lock(&((*s_argument_thread).mutex_nombre_references)); (*s_argument_thread).nombre_references++; - pthread_mutex_unlock(&((*s_argument_thread).mutex)); + pthread_mutex_unlock(&((*s_argument_thread).mutex_nombre_references)); (*l_nouvel_objet).suivant = liste_threads_surveillance; (*l_nouvel_objet).donnee = (void *) s_argument_thread; @@ -378,7 +378,8 @@ retrait_thread_surveillance(struct_proce (*l_element_precedent).suivant = (*l_element_courant).suivant; } - if (pthread_mutex_lock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_lock(&((*s_argument_thread).mutex_nombre_references)) + != 0) { # ifndef SEMAPHORES_NOMMES sem_post(&semaphore_liste_threads); @@ -400,7 +401,8 @@ retrait_thread_surveillance(struct_proce if ((*s_argument_thread).nombre_references == 0) { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { # ifndef SEMAPHORES_NOMMES sem_post(&semaphore_liste_threads); @@ -415,11 +417,13 @@ retrait_thread_surveillance(struct_proce } pthread_mutex_destroy(&((*s_argument_thread).mutex)); + pthread_mutex_destroy(&((*s_argument_thread).mutex_nombre_references)); free(s_argument_thread); } else { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { # ifndef SEMAPHORES_NOMMES sem_post(&semaphore_liste_threads); @@ -676,7 +680,8 @@ liberation_threads(struct_processus *s_e s_argument_thread = (struct_descripteur_thread *) (*((struct_liste_chainee *) element_courant)).donnee; - if (pthread_mutex_lock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_lock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; sem_post(&semaphore_liste_threads); @@ -700,8 +705,8 @@ liberation_threads(struct_processus *s_e close((*s_argument_thread) .pipe_nombre_interruptions_attente[0]); - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) - != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; sem_post(&semaphore_liste_threads); @@ -709,6 +714,8 @@ liberation_threads(struct_processus *s_e } pthread_mutex_destroy(&((*s_argument_thread).mutex)); + pthread_mutex_destroy(&((*s_argument_thread) + .mutex_nombre_references)); if ((*s_argument_thread).processus_detache == d_faux) { @@ -723,8 +730,8 @@ liberation_threads(struct_processus *s_e } else { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) - != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; sem_post(&semaphore_liste_threads); @@ -796,25 +803,8 @@ liberation_threads(struct_processus *s_e } } - for(i = 0; i < (*s_etat_processus).nombre_variables; i++) - { - pthread_mutex_trylock(&((*(*s_etat_processus) - .s_liste_variables[i].objet).mutex)); - pthread_mutex_unlock(&((*(*s_etat_processus) - .s_liste_variables[i].objet).mutex)); - - // Les variables de niveau 0 sont des définitions qui - // ne sont pas copiées entre threads. - if ((*s_etat_processus).s_liste_variables[i].niveau > 0) - { - 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); + liberation_arbre_variables(s_etat_processus, + (*s_etat_processus).s_arbre_variables, d_faux); for(i = 0; i < (*s_etat_processus).nombre_variables_statiques; i++) { @@ -1328,6 +1318,7 @@ liberation_threads(struct_processus *s_e sem_destroy2((*s_etat_processus).semaphore_fork, sem_fork); # endif + liberation_contexte_cas(s_etat_processus); free(s_etat_processus); s_etat_processus = candidat; @@ -1350,7 +1341,8 @@ liberation_threads(struct_processus *s_e s_argument_thread = (struct_descripteur_thread *) (*l_element_courant).donnee; - if (pthread_mutex_lock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_lock(&((*s_argument_thread).mutex_nombre_references)) + != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; sem_post(&semaphore_liste_threads); @@ -1373,7 +1365,8 @@ liberation_threads(struct_processus *s_e close((*s_argument_thread).pipe_interruptions[0]); close((*s_argument_thread).pipe_nombre_interruptions_attente[0]); - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; sem_post(&semaphore_liste_threads); @@ -1381,6 +1374,8 @@ liberation_threads(struct_processus *s_e } pthread_mutex_destroy(&((*s_argument_thread).mutex)); + pthread_mutex_destroy(&((*s_argument_thread) + .mutex_nombre_references)); if ((*s_argument_thread).processus_detache == d_faux) { @@ -1394,7 +1389,8 @@ liberation_threads(struct_processus *s_e } else { - if (pthread_mutex_unlock(&((*s_argument_thread).mutex)) != 0) + if (pthread_mutex_unlock(&((*s_argument_thread) + .mutex_nombre_references)) != 0) { (*s_etat_processus).erreur_systeme = d_es_processus; sem_post(&semaphore_liste_threads); @@ -1461,8 +1457,8 @@ recherche_thread(pid_t pid, pthread_t ti return(s_etat_processus); } -static logical1 -recherche_thread_principal(pid_t pid, pthread_t *thread) +static struct_processus * +recherche_thread_principal(pid_t pid) { volatile struct_liste_chainee_volatile *l_element_courant; @@ -1486,12 +1482,11 @@ recherche_thread_principal(pid_t pid, pt * Le processus n'existe plus. On ne distribue aucun signal. */ - return(d_faux); + return(NULL); } - (*thread) = (*((struct_thread *) (*l_element_courant).donnee)).tid; - - return(d_vrai); + return((*((struct_thread *) (*l_element_courant).donnee)) + .s_etat_processus); } @@ -1592,12 +1587,12 @@ verrouillage_gestionnaire_signaux() // ce que ce soit possible. # ifndef SEMAPHORES_NOMMES - while(sem_trywait(&semaphore_liste_threads) == -1) + while(sem_wait(&semaphore_liste_threads) == -1) # else - while(sem_trywait(semaphore_liste_threads) == -1) + while(sem_wait(semaphore_liste_threads) == -1) # endif { - if ((errno != EINTR) && (errno != EAGAIN)) + if (errno != EINTR) { pthread_sigmask(SIG_SETMASK, &oldset, NULL); @@ -1613,8 +1608,6 @@ verrouillage_gestionnaire_signaux() BUG(1, uprintf("Lock error !\n")); return; } - - sched_yield(); } } @@ -1724,665 +1717,199 @@ deverrouillage_gestionnaire_signaux() return; } -#ifdef _BROKEN_SIGINFO - -// Remplacer les mutexes par des sémaphores SysV - -#define longueur_queue 256 -#define nombre_queues 13 - -static int *fifos; -static int segment; -static sem_t *semaphores[nombre_queues]; -static sem_t *semaphore_global; - -#ifdef IPCS_SYSV -static unsigned char *chemin = NULL; -#endif - -unsigned char * -nom_segment(unsigned char *chemin, pid_t pid) -{ - unsigned char *fichier; - -# ifdef IPCS_SYSV - if ((fichier = malloc((strlen(chemin) + 1 + 256 + 1) * - sizeof(unsigned char))) == NULL) - { - return(NULL); - } - - sprintf(fichier, "%s/RPL-SIGQUEUES-%d", chemin, (int) pid); -# else - if ((fichier = malloc((1 + 256 + 1) * - sizeof(unsigned char))) == NULL) - { - return(NULL); - } +#define test_signal(signal) \ + if (signal_test == SIGTEST) { signal_test = signal; return; } - sprintf(fichier, "/RPL-SIGQUEUES-%d", (int) pid); -# endif - - return(fichier); -} +// Récupération des signaux +// - SIGINT (arrêt au clavier) +// - SIGTERM (signal d'arrêt en provenance du système) -unsigned char * -nom_semaphore(pid_t pid, int queue) +void +interruption1(int signal) { - unsigned char *fichier; - - if ((fichier = malloc((256 + 1) * sizeof(unsigned char))) == NULL) - { - return(NULL); - } - - sprintf(fichier, "/RPL-SIGESMAPHORES-%d-%d", (int) pid, queue); + test_signal(signal); - return(fichier); -} - -inline int -queue_de_signal(int signal) -{ switch(signal) { case SIGINT: - return(0); - case SIGTSTP: - return(1); - case SIGCONT: - return(2); - case SIGURG: - return(3); - case SIGPIPE: - return(4); + envoi_signal_processus(getpid(), rpl_sigint); + break; + + case SIGTERM: + envoi_signal_processus(getpid(), rpl_sigterm); + break; + case SIGALRM: - return(5); - case SIGFSTOP: - return(6); - case SIGSTART: - return(7); - case SIGINJECT: - return(8); - case SIGABORT: - return(9); - case SIGFABORT: - return(10); - case SIGSEGV: - return(11); - case SIGBUS: - return(12); + envoi_signal_processus(getpid(), rpl_sigalrm); + break; } - return(-1); + return; } -void -creation_fifos_signaux(struct_processus *s_etat_processus) +inline static void +signal_alrm(struct_processus *s_etat_processus, pid_t pid) { - /* - * Signaux utilisés - * SIGINT, SIGTSTP, SIGCONT, SIGURG, SIGPIPE, SIGALRM, SIGFSTOP, - * SIGSTART, SIGINJECT, SIGABORT, SIGFABORT - */ - -# ifndef IPCS_SYSV // POSIX -# else // SystemV + struct_processus *s_thread_principal; - file *desc; - - int i; - - key_t clef; - - unsigned char *nom; - - // Création d'un segment de données associé au PID du processus courant - - chemin = (*s_etat_processus).chemin_fichiers_temporaires; - - if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, - getpid())) == NULL) - { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } - - if ((desc = fopen(nom, "w")) == NULL) - { - (*s_etat_processus).erreur_systeme = d_es_erreur_fichier; - return; - } - - fclose(desc); - - if ((clef = ftok(nom, 1)) == -1) - { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } - - free(nom); + verrouillage_gestionnaire_signaux(); - if ((segment = shmget(clef, - nombre_queues * (longueur_queue + 4) * sizeof(int), - IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)) == -1) + if (pid == getpid()) { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } + // Si pid est égal à getpid(), le signal à traiter est issu + // du même processus que celui qui va le traiter, mais d'un thread + // différent. - fifos = shmat(segment, NULL, 0); - - if (((void *) fifos) == ((void *) -1)) - { - if (shmctl(segment, IPC_RMID, 0) == -1) + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; + printf("[%d] RPL/SIGALRM (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); } - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } - -# endif - - /* - * Structure d'une queue - * 0 : pointeur en lecture sur le premier emplacement libre (int) - * 1 : pointeur en écriture sur le premier emplacement à lire (int) - * 2 : longueur de la queue (int) - * 3 : éléments restants (int) - * 4 à 4 + (2) : queue (int) - */ - - for(i = 0; i < nombre_queues; i++) - { - fifos[(i * (longueur_queue + 4))] = 0; - fifos[(i * (longueur_queue + 4)) + 1] = 0; - fifos[(i * (longueur_queue + 4)) + 2] = longueur_queue; - fifos[(i * (longueur_queue + 4)) + 3] = longueur_queue; - } - - // Création des sémaphores : un sémaphore par signal et par queue - // plus un sémaphore global pour tous les threads. - - for(i = 0; i < nombre_queues; i++) - { - if ((nom = nom_semaphore(getpid(), i)) == NULL) + if ((*s_etat_processus).pid_processus_pere != getpid()) { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; + // On n'est pas dans le processus père, on remonte le signal. + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigalrm); } - - // Le sémaphore est créé en écrasant si nécessaire un sémaphore - // préexistant. Comme le nom du sémaphore contient l'identifiant du - // processus, il est anormal d'avoir un sémaphore de même nom - // préexistant. - - if ((semaphores[i] = sem_open(nom, O_CREAT, S_IRUSR | S_IWUSR, - 1)) == SEM_FAILED) + else { - (*s_etat_processus).erreur_systeme = d_es_semaphore; - return; + // On est dans le processus père, on effectue un arrêt d'urgence. + (*s_etat_processus).var_volatile_alarme = -1; + (*s_etat_processus).var_volatile_requete_arret = -1; } - - free(nom); } - - - if ((nom = nom_semaphore(getpid(), nombre_queues)) == NULL) - { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } - - if ((semaphore_global = sem_open(nom, O_CREAT, S_IRUSR | S_IWUSR, - 1)) == SEM_FAILED) - { - (*s_etat_processus).erreur_systeme = d_es_semaphore; - return; - } - - free(nom); - - return; -} - -void -liberation_fifos_signaux(struct_processus *s_etat_processus) -{ - int i; - - if (shmdt(fifos) == -1) + else { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } + // Le signal est issu d'un processus différent. On recherche le + // thread principal pour remonter le signal. - for(i = 0; i < nombre_queues; i++) - { - if (sem_close(semaphores[i]) != 0) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - (*s_etat_processus).erreur_systeme = d_es_semaphore; - return; + envoi_signal_contexte(s_thread_principal, rpl_sigalrm); } } - if (sem_close(semaphore_global) != 0) - { - (*s_etat_processus).erreur_systeme = d_es_semaphore; - return; - } - + deverrouillage_gestionnaire_signaux(); return; } -void -destruction_fifos_signaux(struct_processus *s_etat_processus) +inline static void +signal_term(struct_processus *s_etat_processus, pid_t pid) { - int i; - - unsigned char *nom; - - if (shmdt(fifos) == -1) - { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } - - if (shmctl(segment, IPC_RMID, 0) == -1) - { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } - - if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, - getpid())) == NULL) - { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } + struct_processus *s_thread_principal; + volatile sig_atomic_t exclusion = 0; - unlink(nom); - free(nom); + verrouillage_gestionnaire_signaux(); - for(i = 0; i < nombre_queues; i++) + if (pid == getpid()) { - if ((nom = nom_semaphore(getpid(), i)) == NULL) + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; + printf("[%d] RPL/SIGTERM (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); } - if (sem_unlink(nom) != 0) + if ((*s_etat_processus).pid_processus_pere != getpid()) { - (*s_etat_processus).erreur_systeme = d_es_semaphore; - return; + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigterm); } + else + { + (*s_etat_processus).var_volatile_traitement_sigint = -1; - free(nom); - } - - if ((nom = nom_semaphore(getpid(), nombre_queues)) == NULL) - { - (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; - return; - } - - if (sem_unlink(nom) != 0) - { - (*s_etat_processus).erreur_systeme = d_es_semaphore; - return; - } - - free(nom); - - return; -} - -int -queue_in(pid_t pid, int signal) -{ -#undef printf -// Transformer ce truc en POSIX ! On ne fait du SysV que si on n'a pas le choix - -# ifndef IPCS_SYSV -# else // Traitement à l'aide d'IPCS SystemV - - int *base; - int *buffer; - int *projection_fifos; - int queue; - int identifiant; - - key_t clef; - - sem_t *semaphore; - - struct stat s_stat; - - unsigned char *nom; - - queue = queue_de_signal(signal); - - // Ouverture des projections - - if ((nom = nom_segment(chemin, pid)) == NULL) - { - return(-1); - } - - // Dans le cas de SIGSTART, premier signal envoyé à un processus fils, - // il convient d'attendre que le fichier support soit effectivement - // accessible. Dans tous les autres cas, ce fichier doit exister. S'il - // n'existe plus, le processus associé n'existe plus. - - if (signal == SIGSTART) - { - // On attend que le fichier sois présent + while(exclusion == 1); + exclusion = 1; - while(stat(nom, &s_stat) != 0); - } - - if ((clef = ftok(nom, 1)) == -1) - { - return(-1); - } + if ((*s_etat_processus).var_volatile_requete_arret == -1) + { + deverrouillage_gestionnaire_signaux(); + exclusion = 0; + return; + } - free(nom); + (*s_etat_processus).var_volatile_requete_arret = -1; + (*s_etat_processus).var_volatile_alarme = -1; - if (signal == SIGSTART) - { - while((identifiant = shmget(clef, - nombre_queues * (longueur_queue + 4) * sizeof(int), - S_IRUSR | S_IWUSR)) == -1); + exclusion = 0; + } } else { - if ((identifiant = shmget(clef, - nombre_queues * (longueur_queue + 4) * sizeof(int), - S_IRUSR | S_IWUSR)) == -1) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - return(-1); + envoi_signal_contexte(s_thread_principal, rpl_sigterm); } } - projection_fifos = shmat(identifiant, NULL, 0); - - if (((void *) projection_fifos) == ((void *) -1)) - { - return(-1); - } - - if ((nom = nom_semaphore(pid, queue)) == NULL) - { - shmdt(projection_fifos); - return(-1); - } - - while((semaphore = sem_open(nom, 0)) == SEM_FAILED); - - if (sem_wait(semaphore) != 0) - { - shmdt(projection_fifos); - return(-1); - } - - // Il ne faut pas empiler plusieurs SIGSTART car SIGSTART peut provenir - // de l'instruction SWI. Plusieurs threads peuvent interrompre de façon - // asynchrone le processus père durant une phase de signaux masqués. - - base = &(projection_fifos[(longueur_queue + 4) * queue]); - buffer = &(base[4]); - - // base[3] contient le nombre d'éléments restants - - if (base[3] <= 0) - { - sem_post(semaphore); - sem_close(semaphore); - shmdt(projection_fifos); - return(-1); - } - - base[3]--; - - // base[1] contient le prochain élément à écrire - buffer[base[1]++] = (int) pid; - base[1] %= base[2]; - - if (sem_post(semaphore) != 0) - { - shmdt(projection_fifos); - sem_close(semaphore); - return(-1); - } - - sem_close(semaphore); - - // Fermeture des projections - shmdt(projection_fifos); - -# endif - - return(0); -} - -pid_t -origine_signal(int signal) -{ - int *base; - int *buffer; - int pid; - int queue; - - queue = queue_de_signal(signal); - - BUG(queue == -1, uprintf("[%d] Unknown signal %d in this context\n", - (int) getpid(), signal)); - - if (sem_wait(semaphores[queue]) != 0) - { - return(-1); - } - - // Le signal SIGCONT peut être envoyé de façon totalement asynchrone. - // Il peut y avoir plus de signaux envoyés que d'interruptions traitées. - // Il convient donc de rectifier la queue lors du traitement de - // l'interruption correspondante. Le gestionnaire étant installé sans - // l'option NODEFER, la queue reste cohérente. - - if (signal == SIGCONT) - { - base = &(fifos[(longueur_queue + 4) * queue]); - buffer = &(base[4]); - base[0] = (base[1] - 1) % base[2]; - pid = buffer[base[0]++]; - base[3] = base[2]; - } - else - { - base = &(fifos[(longueur_queue + 4) * queue]); - buffer = &(base[4]); - pid = buffer[base[0]++]; - base[0] %= base[2]; - base[3]++; - } - - if (base[3] > base[2]) - { - sem_post(semaphores[queue]); - return(-1); - } - - if (sem_post(semaphores[queue]) != 0) - { - return(-1); - } - - return((pid_t) pid); + deverrouillage_gestionnaire_signaux(); + return; } -#endif - -#ifdef printf -# undef printf -#endif - -void -interruption1(SIGHANDLER_ARGS) +inline static void +signal_int(struct_processus *s_etat_processus, pid_t pid) { - pid_t pid; - - pthread_t thread; - - struct_processus *s_etat_processus; - + struct_processus *s_thread_principal; volatile sig_atomic_t exclusion = 0; verrouillage_gestionnaire_signaux(); -# ifdef _BROKEN_SIGINFO - if (signal == SIGINT) - { - // Si l'interruption provient du clavier, il n'y a pas eu d'appel - // à queue_in(). - - pid = getpid(); - } - else + if (pid == getpid()) { - pid = origine_signal(signal); - } -# else - pid = (*siginfo).si_pid; -# endif + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) + { + printf("[%d] RPL/SIGINT (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); + } - switch(signal) - { - case SIGALRM : + if ((*s_etat_processus).pid_processus_pere != getpid()) { - if (pid == getpid()) - { - if ((s_etat_processus = recherche_thread(getpid(), - pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigint); + } + else + { + (*s_etat_processus).var_volatile_traitement_sigint = -1; - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) - { - printf("[%d] SIGALRM (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); - } + while(exclusion == 1); + exclusion = 1; - if ((*s_etat_processus).pid_processus_pere != getpid()) - { - kill((*s_etat_processus).pid_processus_pere, signal); - } - else - { - (*s_etat_processus).var_volatile_alarme = -1; - (*s_etat_processus).var_volatile_requete_arret = -1; - } - } - else + if ((*s_etat_processus).var_volatile_requete_arret == -1) { - if (recherche_thread_principal(getpid(), &thread) == d_vrai) - { - pthread_kill(thread, signal); - } + deverrouillage_gestionnaire_signaux(); + exclusion = 0; + return; } - break; - } - - case SIGINT : - { - /* - * Une vieille spécification POSIX permet au pointeur siginfo - * d'être nul dans le cas d'un ^C envoyé depuis le clavier. - * Solaris suit en particulier cette spécification. - */ - -# ifndef _BROKEN_SIGINFO - if (siginfo == NULL) + if ((*s_etat_processus).langue == 'F') { - kill(getpid(), signal); + printf("+++Interruption\n"); } else -# endif - if (pid == getpid()) { - if ((s_etat_processus = recherche_thread(getpid(), - pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } - - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) - { - printf("[%d] SIGINT (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); - } - - if ((*s_etat_processus).pid_processus_pere != getpid()) - { - kill((*s_etat_processus).pid_processus_pere, signal); - } - else - { - (*s_etat_processus).var_volatile_traitement_sigint = -1; - - while(exclusion == 1); - exclusion = 1; - - if ((*s_etat_processus).var_volatile_requete_arret == -1) - { - deverrouillage_gestionnaire_signaux(); - exclusion = 0; - return; - } - - if (strncmp(getenv("LANG"), "fr", 2) == 0) - { - printf("+++Interruption\n"); - } - else - { - printf("+++Interrupt\n"); - } - - fflush(stdout); + printf("+++Interrupt\n"); + } - (*s_etat_processus).var_volatile_requete_arret = -1; - (*s_etat_processus).var_volatile_alarme = -1; + fflush(stdout); - exclusion = 0; - } - } - else - { - if (recherche_thread_principal(getpid(), &thread) == d_vrai) - { - pthread_kill(thread, signal); - } - } + (*s_etat_processus).var_volatile_requete_arret = -1; + (*s_etat_processus).var_volatile_alarme = -1; - break; + exclusion = 0; } - - default : + } + else + { + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - BUG(1, uprintf("[%d] Unknown signal %d in this context\n", - (int) getpid(), signal)); - break; + envoi_signal_contexte(s_thread_principal, rpl_sigint); } } @@ -2390,52 +1917,33 @@ interruption1(SIGHANDLER_ARGS) return; } +// Récupération des signaux +// - SIGFSTP +// +// ATTENTION : +// Le signal SIGFSTP provient de la mort du processus de contrôle. +// Sous certains systèmes (Linux...), la mort du terminal de contrôle +// se traduit par l'envoi d'un SIGHUP au processus. Sur d'autres +// (SunOS), le processus reçoit un SIGFSTP avec une structure siginfo +// non initialisée (pointeur NULL) issue de TERMIO. + void -interruption2(SIGHANDLER_ARGS) +interruption2(int signal) { - pid_t pid; - - pthread_t thread; + test_signal(signal); + envoi_signal_processus(getpid(), rpl_sigtstp); + return; +} - struct_processus *s_etat_processus; +static inline void +signal_tstp(struct_processus *s_etat_processus, pid_t pid) +{ + struct_processus *s_thread_principal; verrouillage_gestionnaire_signaux(); -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif - -# ifndef _BROKEN_SIGINFO - if (siginfo == NULL) - { - /* - * Le signal SIGFSTP provient de la mort du processus de contrôle. - * Sous certains systèmes (Linux...), la mort du terminal de contrôle - * se traduit par l'envoi d'un SIGHUP au processus. Sur d'autres - * (SunOS), le processus reçoit un SIGFSTP avec une structure siginfo - * non initialisée (pointeur NULL) issue de TERMIO. - */ - - if (recherche_thread_principal(getpid(), &thread) == d_vrai) - { - pthread_kill(thread, SIGHUP); - deverrouillage_gestionnaire_signaux(); - return; - } - } - else -# endif if (pid == getpid()) { - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) - == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } - /* * 0 => fonctionnement normal * -1 => requête @@ -2444,14 +1952,15 @@ interruption2(SIGHANDLER_ARGS) if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGTSTP (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGTSTP (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } if ((*s_etat_processus).var_volatile_processus_pere == 0) { - kill((*s_etat_processus).pid_processus_pere, signal); + envoi_signal_processus((*s_etat_processus).pid_processus_pere, + rpl_sigtstp); } else { @@ -2462,11 +1971,10 @@ interruption2(SIGHANDLER_ARGS) { // Envoi d'un signal au thread maître du groupe. - if (recherche_thread_principal(getpid(), &thread) == d_vrai) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - pthread_kill(thread, SIGTSTP); - deverrouillage_gestionnaire_signaux(); - return; + envoi_signal_contexte(s_thread_principal, rpl_sigtstp); } } @@ -2475,21 +1983,36 @@ interruption2(SIGHANDLER_ARGS) } void -interruption3(SIGHANDLER_ARGS) +interruption3(int signal) { - pid_t pid; + // Si on passe par ici, c'est qu'il est impossible de récupérer + // l'erreur d'accès à la mémoire. On sort donc du programme quitte à + // ce qu'il reste des processus orphelins. - struct_processus *s_etat_processus; + unsigned char message[] = "+++System : Uncaught access violation\n" + "+++System : Aborting !\n"; - static int compteur = 0; + test_signal(signal); - verrouillage_gestionnaire_signaux(); + if (pid_processus_pere == getpid()) + { + kill(pid_processus_pere, SIGALRM); + } -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif + write(STDERR_FILENO, message, strlen(message)); + _exit(EXIT_FAILURE); +} + +#if 0 +// Utiliser libsigsegv +void INTERRUPTION3_A_FIXER() +{ + pthread_t thread; + + struct_processus *s_etat_processus; + + test_signal(signal); + verrouillage_gestionnaire_signaux(); if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { @@ -2515,85 +2038,115 @@ interruption3(SIGHANDLER_ARGS) // Segfault dans une routine interne if (strncmp(getenv("LANG"), "fr", 2) == 0) { - printf("+++Système : Violation d'accès (dépassement de pile)\n"); + printf("+++Système : Violation d'accès\n"); } else { - printf("+++System : Access violation (stack overflow)\n"); + printf("+++System : Access violation\n"); } fflush(stdout); - compteur++; + (*s_etat_processus).compteur_violation_d_acces++; - if (compteur > 1) + if ((*s_etat_processus).compteur_violation_d_acces > 1) { + // On vient de récupérer plus d'une erreur de segmentation + // dans le même processus ou le même thread. L'erreur n'est pas + // récupérable et on sort autoritairement du programme. Il peut + // rester des processus orphelins en attente ! + + if (strncmp(getenv("LANG"), "fr", 2) == 0) + { + printf("+++Système : Violation d'accès, tentative de " + "terminaison de la tâche\n"); + printf(" (defauts multiples)\n"); + } + else + { + printf("+++System : Access violation, trying to kill task " + "(multiple defaults)\n"); + } + + fflush(stdout); + deverrouillage_gestionnaire_signaux(); exit(EXIT_FAILURE); } else { - deverrouillage_gestionnaire_signaux(); - longjmp(contexte_initial, -1); - } - } - - deverrouillage_gestionnaire_signaux(); - return; -} + // Première erreur de segmentation. On essaie de terminer + // proprement le thread ou le processus. Le signal ne peut être + // envoyé que depuis le même processus. -void -interruption4(SIGHANDLER_ARGS) -{ - pid_t pid; + if (recherche_thread_principal(getpid(), &thread) == d_vrai) + { + if (pthread_equal(thread, pthread_self()) != 0) + { + deverrouillage_gestionnaire_signaux(); - struct_processus *s_etat_processus; + if ((*s_etat_processus).pid_processus_pere != getpid()) + { + // On est dans le thread principal d'un processus. - verrouillage_gestionnaire_signaux(); + longjmp(contexte_processus, -1); + } + else + { + // On est dans le thread principal du processus + // père. -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif + longjmp(contexte_initial, -1); + } + } + else + { + // On est dans un thread fils d'un thread principal. - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } + deverrouillage_gestionnaire_signaux(); + longjmp(contexte_thread, -1); + } + } - /* - * Démarrage d'un processus fils ou gestion de SIGCONT (SUSPEND) - */ + // Là, on ramasse les miettes puisque le thread n'existe plus + // dans la base (corruption de la mémoire). - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) - { - printf("[%d] SIGSTART/SIGCONT (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); + deverrouillage_gestionnaire_signaux(); + longjmp(contexte_initial, -1); + } } deverrouillage_gestionnaire_signaux(); return; } +#endif -void -interruption5(SIGHANDLER_ARGS) +// Traitement de rpl_sigstart + +static inline void +signal_start(struct_processus *s_etat_processus, pid_t pid) { - pid_t pid; + (*s_etat_processus).demarrage_fils = d_vrai; + return; +} - pthread_t thread; +// Traitement de rpl_sigcont - struct_processus *s_etat_processus; +static inline void +signal_cont(struct_processus *s_etat_processus, pid_t pid) +{ + (*s_etat_processus).redemarrage_processus = d_vrai; + return; +} - verrouillage_gestionnaire_signaux(); +// Traitement de rpl_sigstop -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif +static inline void +signal_stop(struct_processus *s_etat_processus, pid_t pid) +{ + struct_processus *s_thread_principal; + + verrouillage_gestionnaire_signaux(); if (pid == getpid()) { @@ -2606,7 +2159,7 @@ interruption5(SIGHANDLER_ARGS) if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGFSTOP (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGFSTOP (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } @@ -2638,11 +2191,10 @@ interruption5(SIGHANDLER_ARGS) // Envoi d'un signal au thread maître du groupe. - if (recherche_thread_principal(getpid(), &thread) == d_vrai) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - pthread_kill(thread, signal); - deverrouillage_gestionnaire_signaux(); - return; + envoi_signal_contexte(s_thread_principal, rpl_sigstop); } } @@ -2650,21 +2202,13 @@ interruption5(SIGHANDLER_ARGS) return; } -void -interruption6(SIGHANDLER_ARGS) -{ - pid_t pid; - - struct_processus *s_etat_processus; +// Traitement de rpl_siginject +static inline void +signal_inject(struct_processus *s_etat_processus, pid_t pid) +{ verrouillage_gestionnaire_signaux(); -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); @@ -2673,7 +2217,7 @@ interruption6(SIGHANDLER_ARGS) if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGINJECT/SIGQUIT (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGINJECT (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } @@ -2682,58 +2226,33 @@ interruption6(SIGHANDLER_ARGS) return; } +// Récupération des signaux +// - SIGPIPE + void -interruption7(SIGHANDLER_ARGS) +interruption5(int signal) { - pid_t pid; - - struct_processus *s_etat_processus; - - verrouillage_gestionnaire_signaux(); + unsigned char message[] = "+++System : SIGPIPE\n" + "+++System : Aborting !\n"; -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif - - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } + test_signal(signal); - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) + if (pid_processus_pere == getpid()) { - printf("[%d] SIGPIPE (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); + envoi_signal_processus(pid_processus_pere, rpl_sigalrm); } - (*s_etat_processus).var_volatile_requete_arret = -1; - deverrouillage_gestionnaire_signaux(); - - BUG(1, printf("[%d] SIGPIPE\n", (int) getpid())); + write(STDERR_FILENO, message, strlen(message)); return; } -void -interruption8(SIGHANDLER_ARGS) +static inline void +signal_urg(struct_processus *s_etat_processus, pid_t pid) { - pid_t pid; - - pthread_t thread; - - struct_processus *s_etat_processus; + struct_processus *s_thread_principal; verrouillage_gestionnaire_signaux(); -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif - if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) @@ -2745,7 +2264,7 @@ interruption8(SIGHANDLER_ARGS) if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGURG (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGURG (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } @@ -2757,11 +2276,10 @@ interruption8(SIGHANDLER_ARGS) { // Envoi d'un signal au thread maître du groupe. - if (recherche_thread_principal(getpid(), &thread) == d_vrai) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - pthread_kill(thread, SIGURG); - deverrouillage_gestionnaire_signaux(); - return; + envoi_signal_contexte(s_thread_principal, rpl_sigurg); } } @@ -2769,115 +2287,28 @@ interruption8(SIGHANDLER_ARGS) return; } -void -interruption9(SIGHANDLER_ARGS) -{ - pid_t pid; - - struct_processus *s_etat_processus; - - verrouillage_gestionnaire_signaux(); - -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif +// Traitement de rpl_sigabort - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) - { - deverrouillage_gestionnaire_signaux(); - return; - } - - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) - { - printf("[%d] SIGABORT/SIGPROF (thread %llu)\n", (int) getpid(), - (unsigned long long) pthread_self()); - fflush(stdout); - } - -# ifdef _BROKEN_SIGINFO - if (queue_in(getpid(), signal) != 0) - { - return; - } - - deverrouillage_gestionnaire_signaux(); - interruption11(signal); -# else - deverrouillage_gestionnaire_signaux(); - interruption11(signal, siginfo, context); -# endif - return; -} - -void -interruption10(SIGHANDLER_ARGS) +static inline void +signal_abort(struct_processus *s_etat_processus, pid_t pid) { - file *fichier; - - pid_t pid; - - struct_processus *s_etat_processus; - - unsigned char nom[8 + 64 + 1]; + struct_processus *s_thread_principal; verrouillage_gestionnaire_signaux(); -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif - if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) { deverrouillage_gestionnaire_signaux(); return; } - snprintf(nom, 8 + 64 + 1, "rpl-out-%lu-%lu", (unsigned long) getpid(), - (unsigned long) pthread_self()); - - if ((fichier = fopen(nom, "w+")) != NULL) - { - fclose(fichier); - - freopen(nom, "w", stdout); - freopen(nom, "w", stderr); - } - - freopen("/dev/null", "r", stdin); - if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) { - printf("[%d] SIGHUP (thread %llu)\n", (int) getpid(), + printf("[%d] RPL/SIGABORT (thread %llu)\n", (int) getpid(), (unsigned long long) pthread_self()); fflush(stdout); } - deverrouillage_gestionnaire_signaux(); - return; -} - -void -interruption11(SIGHANDLER_ARGS) -{ - pid_t pid; - - pthread_t thread; - - struct_processus *s_etat_processus; - - verrouillage_gestionnaire_signaux(); - -# ifdef _BROKEN_SIGINFO - pid = origine_signal(signal); -# else - pid = (*siginfo).si_pid; -# endif - if (pid == getpid()) { if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) @@ -2925,11 +2356,10 @@ interruption11(SIGHANDLER_ARGS) // Envoi d'un signal au thread maître du groupe. - if (recherche_thread_principal(getpid(), &thread) == d_vrai) + if ((s_thread_principal = recherche_thread_principal(getpid())) + != NULL) { - pthread_kill(thread, signal); - deverrouillage_gestionnaire_signaux(); - return; + envoi_signal_contexte(s_thread_principal, rpl_sigabort); } } @@ -2937,6 +2367,56 @@ interruption11(SIGHANDLER_ARGS) return; } +// Récupération des signaux +// - SIGHUP + +void +interruption4(int signal) +{ + test_signal(signal); + envoi_signal_processus(getpid(), rpl_sighup); + return; +} + +static inline void +signal_hup(struct_processus *s_etat_processus, pid_t pid) +{ + file *fichier; + + unsigned char nom[8 + 64 + 1]; + + verrouillage_gestionnaire_signaux(); + + if ((s_etat_processus = recherche_thread(getpid(), pthread_self())) == NULL) + { + deverrouillage_gestionnaire_signaux(); + return; + } + + snprintf(nom, 8 + 64 + 1, "rpl-out-%lu-%lu", (unsigned long) getpid(), + (unsigned long) pthread_self()); + + if ((fichier = fopen(nom, "w+")) != NULL) + { + fclose(fichier); + + freopen(nom, "w", stdout); + freopen(nom, "w", stderr); + } + + freopen("/dev/null", "r", stdin); + + if (((*s_etat_processus).type_debug & d_debug_signaux) != 0) + { + printf("[%d] SIGHUP (thread %llu)\n", (int) getpid(), + (unsigned long long) pthread_self()); + fflush(stdout); + } + + deverrouillage_gestionnaire_signaux(); + return; +} + void traitement_exceptions_gsl(const char *reason, const char *file, int line, int gsl_errno) @@ -2956,115 +2436,434 @@ traitement_exceptions_gsl(const char *re return; } -#ifdef _BROKEN_SIGINFO +static inline void +envoi_interruptions(struct_processus *s_etat_processus, enum signaux_rpl signal, + pid_t pid_source) +{ + unsigned char message[] = "+++System : Spurious signa !\n"; + + switch(signal) + { + case rpl_sigint: + signal_int(s_etat_processus, pid_source); + break; + + case rpl_sigterm: + signal_term(s_etat_processus, pid_source); + break; + + case rpl_sigstart: + signal_start(s_etat_processus, pid_source); + break; + + case rpl_sigcont: + signal_cont(s_etat_processus, pid_source); + break; + + case rpl_sigstop: + signal_stop(s_etat_processus, pid_source); + break; + + case rpl_sigabort: + signal_abort(s_etat_processus, pid_source); + break; + + case rpl_sigurg: + signal_urg(s_etat_processus, pid_source); + break; + + case rpl_siginject: + signal_inject(s_etat_processus, pid_source); + break; + + case rpl_sigalrm: + signal_alrm(s_etat_processus, pid_source); + break; + + case rpl_sighup: + signal_hup(s_etat_processus, pid_source); + break; + + case rpl_sigtstp: + signal_tstp(s_etat_processus, pid_source); + break; + + default: + write(STDERR_FILENO, message, strlen(message)); + break; + } + + return; +} + +void +scrutation_interruptions(struct_processus *s_etat_processus) +{ + // Interruptions qui arrivent sur le processus depuis un + // processus externe. + + // Interruptions qui arrivent depuis le groupe courant de threads. -#undef kill -#undef pthread_kill + if ((*s_etat_processus).pointeur_signal_lecture != + (*s_etat_processus).pointeur_signal_ecriture) + { + // Il y a un signal dans la queue du thread courant. On le traite. + } + + return; +} int -kill_broken_siginfo(pid_t pid, int signal) +envoi_signal_processus(pid_t pid, enum signaux_rpl signal) { - int ios; + // Il s'agit d'ouvrir le segment de mémoire partagée, de le projeter en + // mémoire puis d'y inscrire le signal à traiter. - sem_t *semaphore; + return(0); +} - unsigned char *nom; +int +envoi_signal_thread(pthread_t tid, enum signaux_rpl signal) +{ + // Un signal est envoyé d'un thread à un autre thread du même processus. + + return(0); +} + +int +envoi_signal_contexte(struct_processus *s_etat_processus_a_signaler, + enum signaux_rpl signal) +{ + (*s_etat_processus_a_signaler).signaux_en_queue + [(*s_etat_processus_a_signaler).pointeur_signal_ecriture + 1] = + signal; + + // On valide l'écriture. Cela évite l'utilisation d'un mutex. + + (*s_etat_processus_a_signaler).pointeur_signal_ecriture++; + return(0); +} + + +/* +================================================================================ + Fonction renvoyant le nom du segment de mémoire partagée en fonction + du pid du processus. +================================================================================ + Entrée : Chemin absolue servant de racine, pid du processus +-------------------------------------------------------------------------------- + Sortie : NULL ou nom du segment +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ + +static unsigned char * +nom_segment(unsigned char *chemin, pid_t pid) +{ + unsigned char *fichier; + +# ifdef IPCS_SYSV // !POSIX +# ifndef OS2 // !OS2 + + if ((fichier = malloc((strlen(chemin) + 1 + 256 + 1) * + sizeof(unsigned char))) == NULL) + { + return(NULL); + } + + sprintf(fichier, "%s/RPL-SIGQUEUES-%d", chemin, (int) pid); +# else // OS2 + if ((fichier = malloc((10 + 256 + 1) * sizeof(unsigned char))) + == NULL) + { + return(NULL); + } + + sprintf(fichier, "\\SHAREMEM\\RPL-SIGQUEUES-%d", (int) pid); +# endif // OS2 +# else // POSIX + + if ((fichier = malloc((1 + 256 + 1) * + sizeof(unsigned char))) == NULL) + { + return(NULL); + } + + sprintf(fichier, "/RPL-SIGQUEUES-%d", (int) pid); +# endif + + return(fichier); +} + + +/* +================================================================================ + Fonction créant un segment de mémoire partagée destiné à contenir + la queue des signaux. +================================================================================ + Entrée : structure de description du processus +-------------------------------------------------------------------------------- + Sortie : Néant +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ - /* - * Lorsqu'on veut interrompre le processus pid, on ouvre le segment - * correspondant au processus en question et ou ajoute le pid dans la - * queue. - * - * Le sémaphore global à tous les threads d'un même processus sert - * à garantir que les signaux seront traités dans l'ordre de ce qui est - * effectivement mis dans la queue. - */ +void +creation_queue_signaux(struct_processus *s_etat_processus) +{ + int segment; - // Sémaphore acquis + pthread_mutexattr_t attributs_mutex; - if ((nom = nom_semaphore(getpid(), nombre_queues)) == NULL) + unsigned char *nom; + +# ifndef IPCS_SYSV // POSIX + + if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, + getpid())) == NULL) { - return(-1); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } - if ((semaphore = sem_open(nom, 0)) == SEM_FAILED) + if ((segment = shm_open(nom, O_RDWR | O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR)) == -1) { free(nom); - return(-1); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (ftruncate(segment, sizeof(struct_queue_signaux)) == -1) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + s_queue_signaux = mmap(NULL, sizeof(struct_queue_signaux), + PROT_READ | PROT_WRITE, MAP_SHARED, segment, 0); + close(segment); + + if (((void *) s_queue_signaux) == ((void *) -1)) + { + if (shm_unlink(nom) == -1) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + free(nom); + + pthread_mutexattr_init(&attributs_mutex); + pthread_mutexattr_settype(&attributs_mutex, PTHREAD_MUTEX_NORMAL); + pthread_mutex_init(&((*s_queue_signaux).mutex), &attributs_mutex); + pthread_mutexattr_destroy(&attributs_mutex); + + (*s_queue_signaux).pointeur_lecture = 0; + (*s_queue_signaux).pointeur_ecriture = 0; + +# else // SystemV +# ifndef OS2 + + file *desc; + + key_t clef; + + // Création d'un segment de données associé au PID du processus courant + + chemin = (*s_etat_processus).chemin_fichiers_temporaires; + + if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, + getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if ((desc = fopen(nom, "w")) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_erreur_fichier; + return; + } + + fclose(desc); + + if ((clef = ftok(nom, 1)) == -1) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } free(nom); - if (sem_wait(semaphore) == -1) + if ((segment = shmget(clef, + nombre_queues * ((2 * longueur_queue) + 4) * sizeof(int), + IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)) == -1) { - return(-1); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } - if ((signal != 0) && (signal != SIGINT)) + fifos = shmat(segment, NULL, 0); + + if (((void *) fifos) == ((void *) -1)) { - if (queue_in(pid, signal) != 0) + if (shmctl(segment, IPC_RMID, 0) == -1) { - sem_post(semaphore); - sem_close(semaphore); - return(-1); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } + + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } - ios = kill(pid, signal); +# else - // Sémaphore relâché + if ((nom = nom_segment(NULL, getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (DosAllocSharedMem(&ptr_os2, nom, nombre_queues * + ((2 * longueur_queue) + 4) * sizeof(int), + PAG_WRITE | PAG_READ | PAG_COMMIT) != 0) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } - sem_post(semaphore); - sem_close(semaphore); + free(nom); + fifos = ptr_os2; - return(ios); +# endif +# endif + + return; } -int -pthread_kill_broken_siginfo(pthread_t tid, int signal) + +/* +================================================================================ + Fonction libérant le segment de mémoire partagée destiné à contenir + la queue des signaux. +================================================================================ + Entrée : structure de description du processus +-------------------------------------------------------------------------------- + Sortie : Néant +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ + +void +liberation_queue_signaux(struct_processus *s_etat_processus) { - int ios; +# ifdef IPCS_SYSV // SystemV +# ifndef OS2 +# else // OS/2 +# endif +# else // POSIX + if (munmap(s_queue_signaux, sizeof(struct_queue_signaux)) != 0) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } +# endif + + return; +} - sem_t *semaphore; +/* +================================================================================ + Fonction détruisant le segment de mémoire partagée destiné à contenir + la queue des signaux. +================================================================================ + Entrée : structure de description du processus +-------------------------------------------------------------------------------- + Sortie : Néant +-------------------------------------------------------------------------------- + Effet de bord : Néant +================================================================================ +*/ + +void +destruction_queue_signaux(struct_processus *s_etat_processus) +{ unsigned char *nom; - if ((nom = nom_semaphore(getpid(), nombre_queues)) == NULL) +# ifdef IPCS_SYSV // SystemV +# ifndef OS2 + + if (shmdt(fifos) == -1) { - return(-1); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } - if ((semaphore = sem_open(nom, 0)) == SEM_FAILED) + if (shmctl(segment, IPC_RMID, 0) == -1) { - free(nom); - return(-1); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } + if ((nom = nom_segment((*s_etat_processus).chemin_fichiers_temporaires, + getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + unlink(nom); free(nom); - if (sem_wait(semaphore) == -1) +# else + + if (DosFreeMem(fifos) != 0) { - return(-1); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } - if ((signal != 0) && (signal != SIGINT)) +# endif +# else // POSIX + + if (munmap(s_queue_signaux, sizeof(struct_queue_signaux)) != 0) { - if (queue_in(getpid(), signal) != 0) - { - sem_post(semaphore); - sem_close(semaphore); - return(-1); - } + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if ((nom = nom_segment(NULL, getpid())) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (shm_unlink(nom) != 0) + { + free(nom); + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; } - ios = pthread_kill(tid, signal); + free(nom); - sem_post(semaphore); - sem_close(semaphore); +# endif - return(ios); + return; } -#endif - // vim: ts=4