xref: /titanic_52/usr/src/lib/libc/port/rt/shm.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include <sys/types.h>
31 #include <sys/mman.h>
32 #include <fcntl.h>
33 #include <limits.h>
34 #include <errno.h>
35 #include "pos4obj.h"
36 
37 int
38 shm_open(const char *path, int oflag, mode_t mode)
39 {
40 	int crflag;
41 	int fd;
42 	int flags;
43 
44 	if (__pos4obj_check(path) == -1)
45 		return (-1);
46 
47 	/* acquire semaphore lock to have atomic operation */
48 	if (__pos4obj_lock(path, SHM_LOCK_TYPE) < 0)
49 		return (-1);
50 
51 	fd = __pos4obj_open(path, SHM_DATA_TYPE, oflag, mode, &crflag);
52 
53 	if (fd < 0) {
54 		(void) __pos4obj_unlock(path, SHM_LOCK_TYPE);
55 		return (-1);
56 	}
57 
58 	if ((flags = fcntl(fd, F_GETFD)) < 0 ||
59 	    fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
60 		(void) __pos4obj_unlock(path, SHM_LOCK_TYPE);
61 		(void) __close_nc(fd);
62 		return (-1);
63 	}
64 
65 	/* relase semaphore lock operation */
66 	if (__pos4obj_unlock(path, SHM_LOCK_TYPE) < 0) {
67 		(void) __close_nc(fd);
68 		return (-1);
69 	}
70 
71 	return (fd);
72 }
73 
74 int
75 shm_unlink(const char *path)
76 {
77 	int	oerrno;
78 	int	err;
79 
80 	if (__pos4obj_check(path) < 0)
81 		return (-1);
82 
83 	if (__pos4obj_lock(path, SHM_LOCK_TYPE) < 0)
84 		return (-1);
85 
86 	err = __pos4obj_unlink(path, SHM_DATA_TYPE);
87 
88 	oerrno = errno;
89 
90 	(void) __pos4obj_unlock(path, SHM_LOCK_TYPE);
91 
92 	errno = oerrno;
93 	return (err);
94 
95 }
96