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