#include #include #include #include #include #include #include #include #include #ifndef VERBOSE #define VERBOSE 1 #endif #define FAILED(s) \ { \ printf("Test FAILED: %s\n", s); \ exit(1); \ } #define SEM_NAME "/sem_unlink_5_1" #ifdef NAME_MAX #undef NAME_MAX #endif /******************************************************************************/ /*************************** Test case ***********************************/ /******************************************************************************/ /* The main test function. */ int main(void) { int ret, error; sem_t *sem; long NAME_MAX; char *sem_name; /* Get NAME_MAX value */ NAME_MAX = pathconf("/", _PC_NAME_MAX); #if VERBOSE > 0 printf("NAME_MAX: %ld\n", NAME_MAX); #endif if (NAME_MAX > 0) { /* create a semaphore with a name longer than NAME_MAX */ sem_name = calloc(NAME_MAX + 2, sizeof(char)); if (sem_name == NULL) { perror("Failed to allocate space for the semaphore name"); } /* the space was allocated */ sem_name[0] = '/'; sem_name[NAME_MAX + 1] = '\0'; memset(sem_name + 1, 'N', NAME_MAX); /* Create the semaphore */ sem = sem_open(sem_name, O_CREAT, 0777, 1); if (sem != SEM_FAILED) { ret = sem_unlink(sem_name); error = errno; free(sem_name); if (ret == 0) { FAILED ("The function did not return ENAMETOOLONG as expected"); } else { printf("Error was %d: %s\n", error, strerror(error)); FAILED ("Unable to unlink a semaphore which we just created"); } } #if VERBOSE > 0 else { printf ("Creation of the semaphore failed with error %d: %s\n", errno, strerror(errno)); } #endif } /* Test passed */ #if VERBOSE > 0 printf("Test passed\n"); #endif exit(0); }