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   open_sem test case that attempts to open a new semaphore,
10   close the semaphore and then open and existing semaphore, which
11   should fail when both O_CREAT and O_EXCL name exist during the opening
12   of a Semaphore.  Fail to open is a Pass for this test.
13 */
14
15#include <sys/types.h>
16#include <stdio.h>
17#include <errno.h>
18#include <unistd.h>
19#include <semaphore.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include "posixtest.h"
23
24#define TEST "4-1"
25#define FUNCTION "sem_open"
26#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
27
28
29int main()
30{
31	sem_t   *mysemp;
32	char semname[50];
33
34	sprintf(semname, "/" FUNCTION "_" TEST "_%ld", (long)getpid());
35
36	/*Trying to open the first Sem */
37	mysemp = sem_open(semname, O_CREAT, 0444, 1);
38	sem_close(mysemp);
39
40	/* Opening the same existance SEM */
41	mysemp = sem_open(semname, O_CREAT|O_EXCL, 0444, 1);
42
43	if ((mysemp  == SEM_FAILED ) && ( errno == EEXIST)) {
44		puts("TEST PASSED");
45		sem_unlink(semname);
46		return PTS_PASS;
47	} else {
48		puts("TEST FAILED");
49		return PTS_FAIL;
50	}
51}
52