6-1.c revision 8d9c084e1770f2719ef99a79416584c945cc775f
1/* 2* Copyright (c) 2005, Bull S.A.. All rights reserved. 3* Created by: Sebastien Decugis 4 5* This program is free software; you can redistribute it and/or modify it 6* under the terms of version 2 of the GNU General Public License as 7* published by the Free Software Foundation. 8* 9* This program is distributed in the hope that it would be useful, but 10* WITHOUT ANY WARRANTY; without even the implied warranty of 11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12* 13* You should have received a copy of the GNU General Public License along 14* with this program; if not, write the Free Software Foundation, Inc., 59 15* Temple Place - Suite 330, Boston MA 02111-1307, USA. 16 17* This sample test aims to check the following assertion: 18* 19* A call to sem_open with the same name refers to a new semaphore, once 20* sem_unlink has been called. 21 22* The steps are: 23* -> open a semaphore 24* -> unlink 25* -> try to reconnect (should fail) 26* -> open with O_CREATE 27* -> check the semaphore are different 28 29*/ 30 31/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */ 32#define _POSIX_C_SOURCE 200112L 33 34/******************************************************************************/ 35/*************************** standard includes ********************************/ 36/******************************************************************************/ 37#include <pthread.h> 38#include <stdarg.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <unistd.h> 43 44#include <semaphore.h> 45#include <errno.h> 46#include <fcntl.h> 47 48/******************************************************************************/ 49/*************************** Test framework *******************************/ 50/******************************************************************************/ 51#include "../testfrmw/testfrmw.h" 52#include "../testfrmw/testfrmw.c" 53/* This header is responsible for defining the following macros: 54 * UNRESOLVED(ret, descr); 55 * where descr is a description of the error and ret is an int 56 * (error code for example) 57 * FAILED(descr); 58 * where descr is a short text saying why the test has failed. 59 * PASSED(); 60 * No parameter. 61 * 62 * Both three macros shall terminate the calling process. 63 * The testcase shall not terminate in any other maneer. 64 * 65 * The other file defines the functions 66 * void output_init() 67 * void output(char * string, ...) 68 * 69 * Those may be used to output information. 70 */ 71 72/******************************************************************************/ 73/**************************** Configuration ***********************************/ 74/******************************************************************************/ 75#ifndef VERBOSE 76#define VERBOSE 1 77#endif 78 79#define SEM_NAME "/sem_unlink_6_1" 80 81/******************************************************************************/ 82/*************************** Test case ***********************************/ 83/******************************************************************************/ 84 85/* The main test function. */ 86int main(int argc, char * argv[]) 87{ 88 int ret, value; 89 90 sem_t * sem1, * sem2; 91 92 /* Initialize output */ 93 output_init(); 94 95 /* Create the semaphore */ 96 sem1 = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 1); 97 98 if ((sem1 == SEM_FAILED) && (errno == EEXIST)) 99 { 100 sem_unlink(SEM_NAME); 101 sem1 = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 1); 102 } 103 104 if (sem1 == SEM_FAILED) 105 { 106 UNRESOLVED(errno, "Failed to create the semaphore"); 107 } 108 109 /* Unlink */ 110 ret = sem_unlink(SEM_NAME); 111 112 if (ret != 0) 113 { 114 UNRESOLVED(errno, "Failed to unlink the semaphore"); 115 } 116 117 /* Try reconnect */ 118 sem2 = sem_open(SEM_NAME, 0); 119 120 if (sem2 != SEM_FAILED) 121 { 122 FAILED("Reconnecting the unlinked semaphore did not failed"); 123 } 124 125 if (errno != ENOENT) 126 { 127 output("Error %d: %s\n", errno, strerror(errno)); 128 FAILED("Reconnecting the unlinked semaphore failed with a wrong error"); 129 } 130 131 /* Reopen the semaphore */ 132 sem2 = sem_open(SEM_NAME, O_CREAT | O_EXCL, 0777, 3); 133 134 if (sem2 == SEM_FAILED) 135 { 136 output("Gor error %d: %s\n", errno, strerror(errno)); 137 FAILED("Failed to recreate the semaphore"); 138 } 139 140 /* Check the semaphore have different values */ 141 ret = sem_getvalue(sem1, &value); 142 143 if (ret != 0) 144 { 145 UNRESOLVED(errno, "Failed to read sem1 value"); 146 } 147 148 if (value != 1) 149 { 150 output("Read: %d\n", value); 151 FAILED("Semaphore value is not as expected"); 152 } 153 154 ret = sem_getvalue(sem2, &value); 155 156 if (ret != 0) 157 { 158 UNRESOLVED(errno, "Failed to read sem1 value"); 159 } 160 161 if (value != 3) 162 { 163 output("Read: %d\n", value); 164 FAILED("Semaphore value is not as expected"); 165 } 166 167 /* Unlink */ 168 ret = sem_unlink(SEM_NAME); 169 170 if (ret != 0) 171 { 172 UNRESOLVED(errno, "Failed to unlink the semaphore"); 173 } 174 175 /* close both */ 176 ret = sem_close(sem1); 177 178 if (ret != 0) 179 { 180 UNRESOLVED(errno, "Failed to close the semaphore"); 181 } 182 183 ret = sem_close(sem2); 184 185 if (ret != 0) 186 { 187 UNRESOLVED(errno, "Failed to close the semaphore"); 188 } 189 190 /* Test passed */ 191#if VERBOSE > 0 192 output("Test passed\n"); 193 194#endif 195 PASSED; 196}