1 /* $NetBSD: shm.c,v 1.3 2015/07/08 07:14:38 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Mindaugas Rasiukevicius. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Interface for POSIX shared memory objects. 34 */ 35 36 #include <sys/cdefs.h> 37 __RCSID("$NetBSD: shm.c,v 1.3 2015/07/08 07:14:38 martin Exp $"); 38 39 #include <sys/mman.h> 40 #include <sys/mount.h> 41 #include <sys/stat.h> 42 43 #include <stdio.h> 44 #include <stdbool.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <fcntl.h> 48 #include <errno.h> 49 #include <limits.h> 50 51 /* 52 * Shared memory objects are supported using tmpfs. 53 */ 54 #define SHMFS_DIR_PATH "/var/shm" 55 #define SHMFS_DIR_MODE (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 56 #define SHMFS_OBJ_PREFIX ".shmobj_" 57 58 #define MOUNT_SHMFS MOUNT_TMPFS 59 60 static bool shm_ok = false; 61 62 static bool 63 _shm_check_fs(void) 64 { 65 int fd; 66 struct statvfs sv; 67 struct stat st; 68 69 fd = open(SHMFS_DIR_PATH, O_DIRECTORY|O_RDONLY); 70 if (fd == -1) 71 return false; 72 73 if (fstatvfs1(fd, &sv, ST_NOWAIT) == -1) 74 goto out; 75 76 if (strncmp(sv.f_fstypename, MOUNT_SHMFS, sizeof(sv.f_fstypename))) 77 goto out; 78 79 if (fstat(fd, &st) == -1) 80 goto out; 81 82 if ((st.st_mode & SHMFS_DIR_MODE) != SHMFS_DIR_MODE) 83 goto out; 84 85 shm_ok = true; 86 87 out: 88 close(fd); 89 return shm_ok; 90 } 91 92 static bool 93 _shm_get_path(char *buf, size_t len, const char *name) 94 { 95 int ret; 96 97 if (__predict_false(!shm_ok) && !_shm_check_fs()) { 98 errno = ENOTSUP; 99 return false; 100 } 101 102 /* 103 * As per POSIX: the name should begin with a slash character. 104 * We may disallow other slashes (implementation-defined behaviour). 105 */ 106 if (*name++ != '/' || strchr(name, '/') != NULL) { 107 errno = EINVAL; 108 return false; 109 } 110 111 ret = snprintf(buf, len, SHMFS_DIR_PATH "/" SHMFS_OBJ_PREFIX "%s", 112 name); 113 114 if ((size_t)ret >= len) { 115 errno = ENAMETOOLONG; 116 return false; 117 } 118 return ret != -1; 119 } 120 121 int 122 shm_open(const char *name, int oflag, mode_t mode) 123 { 124 char path[PATH_MAX]; 125 126 if (!_shm_get_path(path, sizeof(path), name)) { 127 return -1; 128 } 129 return open(path, oflag | O_CLOEXEC | O_NOFOLLOW, mode); 130 } 131 132 int 133 shm_unlink(const char *name) 134 { 135 char path[PATH_MAX]; 136 137 if (!_shm_get_path(path, sizeof(path), name)) { 138 return -1; 139 } 140 return unlink(path); 141 } 142