--- rpl/src/semaphores.c 2016/03/18 12:43:48 1.75 +++ rpl/src/semaphores.c 2016/03/22 09:22:23 1.76 @@ -76,14 +76,40 @@ sem_init2(unsigned int valeur, pid_t pid sem_t *semaphore; unsigned char *chemin; + unsigned char *langue; if ((chemin = nom_segment_semaphore(pid, ordre)) == NULL) { return(SEM_FAILED); } - semaphore = sem_open(chemin, O_RDWR | O_CREAT /*| O_EXCL*/, - S_IRUSR | S_IWUSR, valeur); + if ((semaphore = sem_open(chemin, O_RDWR | O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR, valeur)) == SEM_FAILED) + { + if (errno == EEXIST) + { + if ((langue = getenv("LANG")) != NULL) + { + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Attention : Le sémaphore %s préexiste !\n", + chemin); + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", chemin); + } + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", chemin); + } + + semaphore = sem_open(chemin, O_RDWR | O_CREAT, + S_IRUSR | S_IWUSR, valeur); + } + } + sys_free(chemin); return(semaphore); @@ -96,14 +122,40 @@ sem_init3(unsigned int valeur, pid_t pid sem_t *semaphore; unsigned char *chemin; + unsigned char *langue; if ((chemin = nom_segment_semaphore_thread(pid, tid, ordre)) == NULL) { return(SEM_FAILED); } - semaphore = sem_open(chemin, O_CREAT | /*O_EXCL | */ O_RDWR, - S_IRUSR | S_IWUSR, valeur); + if ((semaphore = sem_open(chemin, O_RDWR | O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR, valeur)) == SEM_FAILED) + { + if (errno == EEXIST) + { + if ((langue = getenv("LANG")) != NULL) + { + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Attention : Le sémaphore %s préexiste !\n", + chemin); + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", chemin); + } + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", chemin); + } + + semaphore = sem_open(chemin, O_RDWR | O_CREAT, + S_IRUSR | S_IWUSR, valeur); + } + } + sys_free(chemin); return(semaphore); @@ -277,6 +329,8 @@ sem_init_SysV(sem_t *semaphore, int shar union semun argument; + unsigned char *langue; + if (shared == 0) { // Sémaphore privé @@ -301,11 +355,40 @@ sem_init_SysV(sem_t *semaphore, int shar (long long unsigned) pthread_self(), (long long unsigned) semaphore); - if ((desc = open((*semaphore).path, O_RDWR | O_CREAT /*| O_EXCL*/, + if ((desc = open((*semaphore).path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1) { - sys_free((*semaphore).path); - return(-1); + if (errno != EEXIST) + { + sys_free((*semaphore).path); + return(-1); + } + + if ((langue = getenv("LANG")) != NULL) + { + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Attention : Le sémaphore %s préexiste !\n", + (*semaphore).path); + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", + (*semaphore).path); + } + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", + (*semaphore).path); + } + + if ((desc = open((*semaphore).path, O_RDWR | O_CREAT, + S_IRUSR | S_IWUSR)) == -1) + { + sys_free((*semaphore).path); + return(-1); + } } (*semaphore).pid = getpid(); @@ -701,6 +784,8 @@ sem_t key_t clef; union semun argument; + + unsigned char *langue; # endif unsigned char *nom_absolu; @@ -888,13 +973,42 @@ sem_t va_end(liste); # ifndef OS2 // IPCS_SYSV - if ((desc = open(nom_absolu, O_CREAT | /*O_EXCL |*/ O_RDWR, + if ((desc = open(nom_absolu, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR)) == -1) { - sys_free(semaphore); - sys_free(nom_absolu); + if (errno != EEXIST) + { + sys_free(semaphore); + sys_free(nom_absolu); + return(SEM_FAILED); + } - return(SEM_FAILED); + if ((langue = getenv("LANG")) != NULL) + { + if (strncmp(langue, "fr", 2) == 0) + { + uprintf("+++Attention : Le sémaphore %s préexiste !\n", + nom_absolu); + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", + nom_absolu); + } + } + else + { + uprintf("+++Warning: %s semaphore preexists!\n", + nom_absolu); + } + + if ((desc = open(nom_absolu, O_CREAT | O_RDWR, + S_IRUSR | S_IWUSR)) == -1) + { + sys_free(semaphore); + sys_free(nom_absolu); + return(SEM_FAILED); + } } if ((clef = ftok(nom_absolu, 1)) == -1)