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