1/*
2    Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
3    Created by:  majid.awad REMOVE-THIS AT intel DOT com
4    This file is licensed under the GPL license.  For the full content
5    of this license, see the COPYING file at the top level of this
6    source tree.
7 */
8
9/*
10   sem_open test case that attempts to open a new semaphore,
11   close the semaphore and then open and existing semaphore, which
12   should fail when both O_CREAT and O_EXCL name exist during the opening
13   of a Semaphore.  Fail to open is a Pass for this test.
14 */
15
16#include <sys/types.h>
17#include <stdio.h>
18#include <errno.h>
19#include <unistd.h>
20#include <semaphore.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include "posixtest.h"
24
25#define TEST "2-1"
26#define FUNCTION "sem_open"
27#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
28
29
30int main()
31{
32	sem_t   *mysemp;
33	char semname[50];
34
35	sprintf(semname, "/" FUNCTION "_" TEST "_%ld", (long)getpid());
36
37	mysemp = sem_open(semname, O_CREAT, 0777, 0);
38	if ( mysemp  == SEM_FAILED ) {
39  		perror(ERROR_PREFIX "sem_open");
40		return PTS_UNRESOLVED;
41	}
42
43	if (sem_close(mysemp) == -1 ) {
44		perror(ERROR_PREFIX "sem_close");
45		return PTS_UNRESOLVED;
46	}
47
48	mysemp = sem_open(semname, O_CREAT|O_EXCL, 0777, 1);
49	if (( mysemp  == SEM_FAILED ) && ( errno == EEXIST) ) {
50		puts("TEST PASSED");
51		sem_unlink(semname);
52		return  PTS_PASS;
53	}
54	else
55	{
56		puts("TEST FAILED");
57		return PTS_FAIL;
58	}
59}
60