Date: Tue, 28 Jan 1997 11:33:24 +0200 (EET) From: Jukka Ukkonen <jau@jau.thunderbolt.fi> To: FreeBSD-gnats-submit@freebsd.org Subject: bin/2604: Added POSIX.4/POSIX.1b shm_open()/shm_unlink() Message-ID: <199701280933.LAA13781@jau.thunderbolt.fi> Resent-Message-ID: <199701281000.CAA03574@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
>Number: 2604
>Category: bin
>Synopsis: Added POSIX.4/POSIX.1b shm_open()/shm_unlink()
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Class: change-request
>Submitter-Id: current-users
>Arrival-Date: Tue Jan 28 02:00:04 PST 1997
>Last-Modified:
>Originator: Jukka Ukkonen
>Organization:
Private person
>Release: FreeBSD 2.1-STABLE i386
>Environment:
This is a new feature to the system library. The exact
version or release of the operating system as well as
the hardware environment are meaningless in this context.
>Description:
This is a minimal implementation of the shared memory objects
library interface required by POSIX.4.
Using the normal files as the objects to mmap() has been
supported for quite a long time in BSD environments, and I saw
no practical reason to change the file descriptor used by shm_*
interface to refer to something else.
POSIX requires only mmap(), ftruncate(), and close() to work
with the descriptors returned by shm_open(), though.
>How-To-Repeat:
Share and enjoy.
>Fix:
See the attached shar package below...
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# shm_open.c
# shm_unlink.c
#
echo x - shm_open.c
sed 's/^X//' >shm_open.c << 'END-of-shm_open.c'
X/*
X * Copyright (c) 1995-1997 Jukka Ukkonen <jau@iki.fi>
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X * notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X * notice, this list of conditions and the following disclaimer in the
X * documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X * must display the following acknowledgement:
X * This product includes software developed by Jukka Antero Ukkonen.
X * 4. Neither the names of the authors nor the names of contributors
X * may be used to endorse or promote products derived from this software
X * without specific prior written permission.
X * 5. The source code must be available for anyone who wishes to have it.
X *
X * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X * SUCH DAMAGE.
X *
X * %W% (Jukka Ukkonen) %E%
X */
X
X#ifndef lint
Xstatic const char sccsid[] = "%W%\t(Jukka Ukkonen)\t%E%";
X#endif
X
X/*
X * usage/example:
X *
X * fd = shm_open (name, O_RDWR|O_CREAT|O_EXCL, 0600);
X *
X * shm = mmap (NULL, pagesize, PROT_READ | PROT_WRITE,
X * MAP_INHERIT | MAP_SHARED, fd, (off_t) 0);
X */
X
X#include <sys/types.h>
X#include <sys/mman.h>
X#include <sys/stat.h>
X#include <fcntl.h>
X#include <errno.h>
X
Xint
Xshm_open (name, flags, mode)
X char *name;
X int flags;
X mode_t mode;
X{
X struct stat st;
X register int fd;
X
X if (! name) {
X errno = EINVAL;
X
X return (-1);
X }
X
X if ((fd = open (name, flags, mode)) < 0)
X return (-1);
X
X if (fstat (fd, &st) < 0)
X return (-1);
X
X /*
X * The whole difference to the plain open(2) is here.
X *
X * We check that the file descriptor we just opened
X * refers to a regular file. We also report invalid
X * parameter, if the file name did not give us just
X * a plain regular file.
X * This is to guarantee that the opened descriptor
X * really can be mmap()ed later on.
X */
X
X if (! S_ISREG (st.st_mode)) {
X close (fd);
X
X errno = EINVAL;
X
X return (-1);
X }
X
X return (fd);
X}
END-of-shm_open.c
echo x - shm_unlink.c
sed 's/^X//' >shm_unlink.c << 'END-of-shm_unlink.c'
X/*
X * Copyright (c) 1995-1997 Jukka Ukkonen <jau@iki.fi>
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X * notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X * notice, this list of conditions and the following disclaimer in the
X * documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X * must display the following acknowledgement:
X * This product includes software developed by Jukka Antero Ukkonen.
X * 4. Neither the names of the authors nor the names of contributors
X * may be used to endorse or promote products derived from this software
X * without specific prior written permission.
X * 5. The source code must be available for anyone who wishes to have it.
X *
X * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X * SUCH DAMAGE.
X *
X * %W% (Jukka Ukkonen) %E%
X */
X
X#ifndef lint
Xstatic const char sccsid[] = "%W%\t(Jukka Ukkonen)\t%E%";
X#endif
X
X
X#include <sys/types.h>
X#include <sys/mman.h>
X#include <sys/stat.h>
X#include <unistd.h>
X#include <errno.h>
X
Xint
Xshm_unlink (name)
X char *name;
X{
X struct stat st;
X
X if (! name) {
X errno = EINVAL;
X return (-1);
X }
X
X if (stat (name, &st) < 0)
X return (-1);
X
X /*
X * Try to protect against unlinking something that
X * never was a potential candidate for being mmap()'ed
X * for shared memory access.
X */
X
X if (! S_ISREG (st.st_mode)) {
X errno = EINVAL;
X
X return (-1);
X }
X
X return (unlink (name));
X}
X
END-of-shm_unlink.c
exit
>Audit-Trail:
>Unformatted:
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199701280933.LAA13781>
