Add kernel and user space parts of a protocol for sending property lists

to / from the kernel using ioctls.
This commit is contained in:
thorpej 2006-07-05 21:46:10 +00:00
parent 2f3beab89e
commit 434b7a76fc
10 changed files with 474 additions and 11 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: prop_dictionary.h,v 1.2 2006/05/18 03:05:19 thorpej Exp $ */
/* $NetBSD: prop_dictionary.h,v 1.3 2006/07/05 21:46:10 thorpej Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
@ -83,6 +83,22 @@ const char * prop_dictionary_keysym_cstring_nocopy(prop_dictionary_keysym_t);
boolean_t prop_dictionary_keysym_equals(prop_dictionary_keysym_t,
prop_dictionary_keysym_t);
#if defined(__NetBSD__)
#if !defined(_KERNEL) && !defined(_STANDALONE)
int prop_dictionary_send_ioctl(prop_dictionary_t, int,
unsigned long);
int prop_dictionary_recv_ioctl(int, unsigned long,
prop_dictionary_t *);
#elif defined(_KERNEL)
struct plistref;
int prop_dictionary_copyin_ioctl(const struct plistref *, int,
prop_dictionary_t *);
int prop_dictionary_copyout_ioctl(struct plistref *,
prop_dictionary_t, int);
#endif
#endif /* __NetBSD__ */
__END_DECLS
#endif /* _PROPLIB_PROP_DICTIONARY_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: proplib.h,v 1.1 2006/04/27 20:11:27 thorpej Exp $ */
/* $NetBSD: proplib.h,v 1.2 2006/07/05 21:46:10 thorpej Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
@ -46,4 +46,14 @@
#include <prop/prop_number.h>
#include <prop/prop_string.h>
/*
* Property List Reference --
* Used to pass externalized property lists across protection
* boundaries (ioctls, syscalls, etc.).
*/
struct plistref {
const void *pref_plist; /* plist data */
size_t pref_len; /* total length of plist data */
};
#endif /* _PROPLIB_PROPLIB_H_ */

View File

@ -1,6 +1,6 @@
# $NetBSD: Makefile.inc,v 1.1 2006/04/27 20:11:27 thorpej Exp $
# $NetBSD: Makefile.inc,v 1.2 2006/07/05 21:46:10 thorpej Exp $
.PATH: ${.PARSEDIR}
SRCS+= prop_array.c prop_bool.c prop_data.c prop_dictionary.c \
prop_number.c prop_object.c prop_string.c
prop_kern.c prop_number.c prop_object.c prop_string.c

View File

@ -0,0 +1,115 @@
.\" $NetBSD: prop_dictionary_copyin_ioctl.9,v 1.1 2006/07/05 21:46:10 thorpej Exp $
.\"
.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jason R. Thorpe.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd July 3, 2006
.Dt PROP_DICTIONARY_COPYIN_IOCTL 9
.Os
.Sh NAME
.Nm prop_dictionary_copyin_ioctl ,
.Nm prop_dictionary_copyout_ioctl
.Nd Copy property lists to and from kernel space
.Sh SYNOPSIS
.In prop/proplib.h
.Ft int
.Fn prop_dictionary_copyin_ioctl "const struct plistref *pref" \
"int ioctlflags" "prop_dictionary_t *dictp"
.Ft int
.Fn prop_dictionary_copyout_ioctl "struct plistref *pref" \
"prop_dictionary_t dict" "int ioctlflags"
.Sh DESCRIPTION
The
.Nm prop_dictionary_copyin_ioctl
and
.Nm prop_dictionary_copyout_ioctl
functions implement the kernel side of a protocol for sending property lists
to and from the kernel using
.Xr ioctl 2 .
.Pp
A kernel ioctl routine receivig or returning a property list will be passed a
pointer to a
.Vt struct plistref .
This structure encapsulates the reference to the property list in externalized
form.
.Pp
The following
.Pq simplified
example demonstrates using
.Fn prop_dictionary_copyin_ioctl
and
.Fn prop_dictionary_copyout_ioctl
in an ioctl routine:
.Bd -literal
extern prop_dictionary_t fooprops;
int
fooioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
{
prop_dictionary_t dict, odict;
int error;
switch (cmd) {
case FOOSETPROPS: {
const struct plistref *pref = (const struct plistref *) data;
error = prop_dictionary_copyin_ioctl(pref, flag, &dict);
if (error)
return (error);
odict = fooprops;
fooprops = dict;
prop_object_release(odict);
break;
}
case FOOGETPROPS: {
struct plistref *pref = (struct plistref *) data;
error = prop_dictionary_copyout_ioctl(pref, fooprops, flag);
break;
}
default:
return (EPASSTHROUGH);
}
return (error);
}
.Ed
.Sh SEE ALSO
.Xr prop_dictionary 3 ,
.Xr prop_dictionary_send_ioctl 3 ,
.Xr proplib 3
.Sh HISTORY
The
.Nm proplib
property container object library first appeared in
.Nx 4.0 .

View File

@ -0,0 +1,109 @@
.\" $NetBSD: prop_dictionary_send_ioctl.3,v 1.1 2006/07/05 21:46:10 thorpej Exp $
.\"
.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jason R. Thorpe.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd July 3, 2006
.Dt PROP_DICTIONARY_SEND_IOCTL 9
.Os
.Sh NAME
.Nm prop_dictionary_send_ioctl ,
.Nm prop_dictionary_recv_ioctl
.Nd Send and receive propertly lists to and from the kernel using ioctl
.Sh SYNOPSIS
.In prop/proplib.h
.Ft int
.Fn prop_dictionary_send_ioctl "prop_dictionary_t dict" "int fd" \
"unsigned long cmd"
.Ft int
.Fn prop_dictionary_recv_ioctl "int fd" "unsigned long cmd" \
"prop_dictionary_t *dictp"
.Sh DESCRIPTION
The
.Nm prop_dictionary_send_ioctl
and
.Nm prop_dictionary_recv_ioctl
functions implement the user space side of a protocol for sending property
lists to and from the kernel using
.Xr ioctl 2 .
.Pp
The following
.Pq simplified
example demonstrates using
.Fn prop_dictionary_send_ioctl
and
.Fn prop_dictionary_receive_ioctl
in an application:
.Bd -literal
void
foo_setprops(prop_dictionary_t dict)
{
int fd;
fd = open("/dev/foo", O_RDWR, 0640);
if (fd == -1)
return;
(void) prop_dictionary_send_ioctl(dict, fd, FOOSETPROPS);
(void) close(fd);
}
prop_dictionary_t
foo_getprops(void)
{
prop_dictionary_t dict;
int fd;
fd = open("/dev/foo", O_RDONLY, 0640);
if (fd == -1)
return (NULL);
if (prop_dictionary_recv_ioctl(fd, FOOGETPROPS, &dict) != 0)
return (NULL);
(void) close(fd);
return (dict);
}
.Ed
.Sh SEE ALSO
.Xr prop_dictionary 3 ,
.Xr prop_dictionary_copyin_ioctl 9 ,
.Xr proplib 3
.Sh HISTORY
The
.Nm proplib
property container object library first appeared in
.Nx 4.0 .

View File

@ -0,0 +1,199 @@
/* $NetBSD: prop_kern.c,v 1.1 2006/07/05 21:46:10 thorpej Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(__NetBSD__)
#include <sys/types.h>
#include <sys/ioctl.h>
#include <prop/proplib.h>
#if !defined(_KERNEL) && !defined(_STANDALONE)
#include <errno.h>
#include <string.h>
#include <stdlib.h>
/*
* prop_dictionary_send_ioctl --
* Send a dictionary to the kernel using the specified ioctl.
*/
int
prop_dictionary_send_ioctl(prop_dictionary_t dict, int fd, unsigned long cmd)
{
struct plistref pref;
char *buf;
int error;
buf = prop_dictionary_externalize(dict);
if (buf == NULL) {
/* Assume we ran out of memory. */
return (ENOMEM);
}
pref.pref_plist = buf;
pref.pref_len = strlen(buf) + 1;
if (ioctl(fd, cmd, &pref) == -1)
error = errno;
else
error = 0;
free(buf);
return (error);
}
/*
* prop_dictionary_recv_ioctl --
* Receive a dictionary from the kernel using the specified ioctl.
*/
int
prop_dictionary_recv_ioctl(int fd, unsigned long cmd, prop_dictionary_t *dictp)
{
struct plistref pref;
prop_dictionary_t dict;
char *buf;
size_t len;
int error;
pref.pref_len = 0;
for (;;) {
len = pref.pref_len;
if (len != 0) {
buf = malloc(len);
if (buf == NULL)
return (ENOMEM);
} else
buf = NULL;
pref.pref_plist = buf;
if (ioctl(fd, cmd, &pref) == -1) {
error = errno;
free(buf);
return (error);
}
/*
* Kernel only copies out the plist if the buffer
* we provided is big enough to hold the entire
* thing. Kernel provides us the needed size in
* the argument structure.
*/
if (len >= pref.pref_len)
break;
free(buf);
}
dict = prop_dictionary_internalize(buf);
free(buf);
if (dict == NULL)
return (EIO);
*dictp = dict;
return (0);
}
#endif /* !_KERNEL && !_STANDALONE */
#if defined(_KERNEL)
#include <sys/errno.h>
#include <sys/malloc.h>
#include <sys/systm.h>
/*
* prop_dictionary_copyin_ioctl --
* Copy in a dictionary sent with an ioctl.
*/
int
prop_dictionary_copyin_ioctl(const struct plistref *pref, int ioctlflags,
prop_dictionary_t *dictp)
{
prop_dictionary_t dict;
char *buf;
int error;
/*
* Allocate an extra byte so we can guarantee NUL-termination.
* XXX Some sanity check on the size?
*/
buf = malloc(pref->pref_len + 1, M_TEMP, M_WAITOK);
error = ioctl_copyin(ioctlflags, pref->pref_plist, buf, pref->pref_len);
if (error) {
free(buf, M_TEMP);
return (error);
}
buf[pref->pref_len] = '\0';
dict = prop_dictionary_internalize(buf);
free(buf, M_TEMP);
if (dict == NULL)
return (EIO);
*dictp = dict;
return (0);
}
/*
* prop_dictionary_copyout_ioctl --
* Copy out a dictionary being received with an ioctl.
*/
int
prop_dictionary_copyout_ioctl(struct plistref *pref, prop_dictionary_t dict,
int ioctlflags)
{
char *buf;
size_t len;
int error = 0;
buf = prop_dictionary_externalize(dict);
if (buf == NULL)
return (ENOMEM);
len = strlen(buf) + 1;
if (len <= pref->pref_len)
error = ioctl_copyout(ioctlflags, buf,
__UNCONST(pref->pref_plist), len);
if (error == 0)
pref->pref_len = len;
free(buf, M_TEMP);
return (error);
}
#endif /* _KERNEL */
#endif /* __NetBSD__ */

View File

@ -1,4 +1,4 @@
# $NetBSD: shl.mi,v 1.357 2006/06/26 21:23:56 mrg Exp $
# $NetBSD: shl.mi,v 1.358 2006/07/05 21:46:10 thorpej Exp $
# Note: libtermcap and libtermlib are hardlinked and share the same version.
./lib/libc.so.12.142 base-sys-shlib
./lib/libcrypt.so.0.2 base-sys-shlib
@ -8,7 +8,7 @@
./lib/libipsec.so.2.1 base-net-shlib
./lib/libkvm.so.5.2 base-sys-shlib
./lib/libm.so.0.5 base-sys-shlib
./lib/libprop.so.0.1 base-sys-shlib
./lib/libprop.so.0.2 base-sys-shlib
./lib/libradius.so.1.0 base-sys-shlib
./lib/libtermcap.so.0.6 base-sys-shlib
./lib/libtermlib.so.0.6 base-sys-shlib
@ -72,7 +72,7 @@
./usr/lib/libpcap.so.2.0 base-net-shlib
./usr/lib/libpci.so.1.0 base-sys-shlib
./usr/lib/libposix.so.0.1 base-sys-shlib
./usr/lib/libprop.so.0.1 base-sys-shlib
./usr/lib/libprop.so.0.2 base-sys-shlib
./usr/lib/libpthread.so.0.6 base-sys-shlib
./usr/lib/libpthread_dbg.so.0.0 base-sys-shlib
./usr/lib/libresolv.so.1.1 base-net-shlib

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.894 2006/06/25 21:49:14 yamt Exp $
# $NetBSD: mi,v 1.895 2006/07/05 21:46:10 thorpej Exp $
./etc/mtree/set.comp comp-sys-root
./usr/bin/addr2line comp-debug-bin bfd
./usr/bin/ar comp-util-bin bfd
@ -4196,8 +4196,10 @@
./usr/share/man/cat3/prop_dictionary_make_immutable.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_make_immutable.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_mutable.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_recv_ioctl.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_remove.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_remove_keysym.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_send_ioctl.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_set.0 comp-c-catman .cat
./usr/share/man/cat3/prop_dictionary_set_keysym.0 comp-c-catman .cat
./usr/share/man/cat3/prop_number.0 comp-c-catman .cat
@ -5752,6 +5754,8 @@
./usr/share/man/cat9/proc_trampoline.0 comp-sys-catman .cat
./usr/share/man/cat9/prop_copy.0 comp-obsolete obsolete
./usr/share/man/cat9/prop_delete.0 comp-obsolete obsolete
./usr/share/man/cat9/prop_dictionary_copyin_ioctl.0 comp-sys-catman .cat
./usr/share/man/cat9/prop_dictionary_copyout_ioctl.0 comp-sys-catman .cat
./usr/share/man/cat9/prop_get.0 comp-obsolete obsolete
./usr/share/man/cat9/prop_list.0 comp-obsolete obsolete
./usr/share/man/cat9/prop_objs.0 comp-obsolete obsolete
@ -8319,8 +8323,10 @@
./usr/share/man/man3/prop_dictionary_keysym_equals.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_make_immutable.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_mutable.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_recv_ioctl.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_remove.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_remove_keysym.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_send_ioctl.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_set.3 comp-c-man .man
./usr/share/man/man3/prop_dictionary_set_keysym.3 comp-c-man .man
./usr/share/man/man3/prop_number.3 comp-c-man .man
@ -9870,6 +9876,8 @@
./usr/share/man/man9/proc_trampoline.9 comp-sys-man .man
./usr/share/man/man9/prop_copy.9 comp-obsolete obsolete
./usr/share/man/man9/prop_delete.9 comp-obsolete obsolete
./usr/share/man/man9/prop_dictionary_copyin_ioctl.9 comp-sys-man .man
./usr/share/man/man9/prop_dictionary_copyout_ioctl.9 comp-sys-man .man
./usr/share/man/man9/prop_get.9 comp-obsolete obsolete
./usr/share/man/man9/prop_list.9 comp-obsolete obsolete
./usr/share/man/man9/prop_objs.9 comp-obsolete obsolete

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.4 2006/05/18 03:05:19 thorpej Exp $
# $NetBSD: Makefile,v 1.5 2006/07/05 21:46:11 thorpej Exp $
.include <bsd.own.mk>
@ -16,6 +16,12 @@ LIB= prop
MAN= prop_array.3 prop_bool.3 prop_data.3 prop_dictionary.3 \
prop_number.3 prop_object.3 prop_string.3 proplib.3
MAN+= prop_dictionary_copyin_ioctl.9
MLINKS+= prop_dictionary_copyin_ioctl.9 prop_dictionary_copyout_ioctl.9
MAN+= prop_dictionary_send_ioctl.3
MLINKS+= prop_dictionary_send_ioctl.3 prop_dictionary_recv_ioctl.3
MLINKS+= prop_array.3 prop_array_add.3
MLINKS+= prop_array.3 prop_array_capacity.3
MLINKS+= prop_array.3 prop_array_copy.3

View File

@ -1,4 +1,4 @@
# $NetBSD: shlib_version,v 1.2 2006/05/18 03:05:19 thorpej Exp $
# $NetBSD: shlib_version,v 1.3 2006/07/05 21:46:11 thorpej Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing
major=0
minor=1
minor=2