xref: /titanic_52/usr/src/lib/libc/port/rt/shm.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
1f841f6adSraf /*
2f841f6adSraf  * CDDL HEADER START
3f841f6adSraf  *
4f841f6adSraf  * The contents of this file are subject to the terms of the
5f841f6adSraf  * Common Development and Distribution License (the "License").
6f841f6adSraf  * You may not use this file except in compliance with the License.
7f841f6adSraf  *
8f841f6adSraf  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f841f6adSraf  * or http://www.opensolaris.org/os/licensing.
10f841f6adSraf  * See the License for the specific language governing permissions
11f841f6adSraf  * and limitations under the License.
12f841f6adSraf  *
13f841f6adSraf  * When distributing Covered Code, include this CDDL HEADER in each
14f841f6adSraf  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f841f6adSraf  * If applicable, add the following below this CDDL HEADER, with the
16f841f6adSraf  * fields enclosed by brackets "[]" replaced with your own identifying
17f841f6adSraf  * information: Portions Copyright [yyyy] [name of copyright owner]
18f841f6adSraf  *
19f841f6adSraf  * CDDL HEADER END
20f841f6adSraf  */
21f841f6adSraf 
22f841f6adSraf /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24f841f6adSraf  * Use is subject to license terms.
25f841f6adSraf  */
26f841f6adSraf 
27f841f6adSraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
28f841f6adSraf 
29*7257d1b4Sraf #include "lint.h"
30f841f6adSraf #include <sys/types.h>
31f841f6adSraf #include <sys/mman.h>
32f841f6adSraf #include <fcntl.h>
33f841f6adSraf #include <limits.h>
34f841f6adSraf #include <errno.h>
35f841f6adSraf #include "pos4obj.h"
36f841f6adSraf 
37f841f6adSraf int
38f841f6adSraf shm_open(const char *path, int oflag, mode_t mode)
39f841f6adSraf {
40f841f6adSraf 	int crflag;
41f841f6adSraf 	int fd;
42f841f6adSraf 	int flags;
43f841f6adSraf 
44f841f6adSraf 	if (__pos4obj_check(path) == -1)
45f841f6adSraf 		return (-1);
46f841f6adSraf 
47f841f6adSraf 	/* acquire semaphore lock to have atomic operation */
48f841f6adSraf 	if (__pos4obj_lock(path, SHM_LOCK_TYPE) < 0)
49f841f6adSraf 		return (-1);
50f841f6adSraf 
51f841f6adSraf 	fd = __pos4obj_open(path, SHM_DATA_TYPE, oflag, mode, &crflag);
52f841f6adSraf 
53f841f6adSraf 	if (fd < 0) {
54f841f6adSraf 		(void) __pos4obj_unlock(path, SHM_LOCK_TYPE);
55f841f6adSraf 		return (-1);
56f841f6adSraf 	}
57f841f6adSraf 
58f841f6adSraf 	if ((flags = fcntl(fd, F_GETFD)) < 0 ||
59f841f6adSraf 	    fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
60f841f6adSraf 		(void) __pos4obj_unlock(path, SHM_LOCK_TYPE);
61f841f6adSraf 		(void) __close_nc(fd);
62f841f6adSraf 		return (-1);
63f841f6adSraf 	}
64f841f6adSraf 
65f841f6adSraf 	/* relase semaphore lock operation */
66f841f6adSraf 	if (__pos4obj_unlock(path, SHM_LOCK_TYPE) < 0) {
67f841f6adSraf 		(void) __close_nc(fd);
68f841f6adSraf 		return (-1);
69f841f6adSraf 	}
70f841f6adSraf 
71f841f6adSraf 	return (fd);
72f841f6adSraf }
73f841f6adSraf 
74f841f6adSraf int
75f841f6adSraf shm_unlink(const char *path)
76f841f6adSraf {
77f841f6adSraf 	int	oerrno;
78f841f6adSraf 	int	err;
79f841f6adSraf 
80f841f6adSraf 	if (__pos4obj_check(path) < 0)
81f841f6adSraf 		return (-1);
82f841f6adSraf 
83f841f6adSraf 	if (__pos4obj_lock(path, SHM_LOCK_TYPE) < 0)
84f841f6adSraf 		return (-1);
85f841f6adSraf 
86f841f6adSraf 	err = __pos4obj_unlink(path, SHM_DATA_TYPE);
87f841f6adSraf 
88f841f6adSraf 	oerrno = errno;
89f841f6adSraf 
90f841f6adSraf 	(void) __pos4obj_unlock(path, SHM_LOCK_TYPE);
91f841f6adSraf 
92f841f6adSraf 	errno = oerrno;
93f841f6adSraf 	return (err);
94f841f6adSraf 
95f841f6adSraf }
96