Rework the lifetime management of the kernel implementation of POSIX

semaphores.  Specifically, semaphores are now represented as new file
descriptor type that is set to close on exec.  This removes the need for
all of the manual process reference counting (and fork, exec, and exit
event handlers) as the normal file descriptor operations handle all of
that for us nicely.  It is also suggested as one possible implementation
in the spec and at least one other OS (OS X) uses this approach.

Some bugs that were fixed as a result include:
- References to a named semaphore whose name is removed still work after
  the sem_unlink() operation.  Prior to this patch, if a semaphore's name
  was removed, valid handles from sem_open() would get EINVAL errors from
  sem_getvalue(), sem_post(), etc.  This fixes that.
- Unnamed semaphores created with sem_init() were not cleaned up when a
  process exited or exec'd.  They were only cleaned up if the process
  did an explicit sem_destroy().  This could result in a leak of semaphore
  objects that could never be cleaned up.
- On the other hand, if another process guessed the id (kernel pointer to
  'struct ksem' of an unnamed semaphore (created via sem_init)) and had
  write access to the semaphore based on UID/GID checks, then that other
  process could manipulate the semaphore via sem_destroy(), sem_post(),
  sem_wait(), etc.
- As part of the permission check (UID/GID), the umask of the proces
  creating the semaphore was not honored.  Thus if your umask denied group
  read/write access but the explicit mode in the sem_init() call allowed
  it, the semaphore would be readable/writable by other users in the
  same group, for example.  This includes access via the previous bug.
- If the module refused to unload because there were active semaphores,
  then it might have deregistered one or more of the semaphore system
  calls before it noticed that there was a problem.  I'm not sure if
  this actually happened as the order that modules are discovered by the
  kernel linker depends on how the actual .ko file is linked.  One can
  make the order deterministic by using a single module with a mod_event
  handler that explicitly registers syscalls (and deregisters during
  unload after any checks).  This also fixes a race where even if the
  sem_module unloaded first it would have destroyed locks that the
  syscalls might be trying to access if they are still executing when
  they are unloaded.

  XXX: By the way, deregistering system calls doesn't do any blocking
  to drain any threads from the calls.
- Some minor fixes to errno values on error.  For example, sem_init()
  isn't documented to return ENFILE or EMFILE if we run out of semaphores
  the way that sem_open() can.  Instead, it should return ENOSPC in that
  case.

Other changes:
- Kernel semaphores now use a hash table to manage the namespace of
  named semaphores nearly in a similar fashion to the POSIX shared memory
  object file descriptors.  Kernel semaphores can now also have names
  longer than 14 chars (up to MAXPATHLEN) and can include subdirectories
  in their pathname.
- The UID/GID permission checks for access to a named semaphore are now
  done via vaccess() rather than a home-rolled set of checks.
- Now that kernel semaphores have an associated file object, the various
  MAC checks for POSIX semaphores accept both a file credential and an
  active credential.  There is also a new posixsem_check_stat() since it
  is possible to fstat() a semaphore file descriptor.
- A small set of regression tests (using the ksem API directly) is present
  in src/tools/regression/posixsem.

Reported by:	kris (1)
Tested by:	kris
Reviewed by:	rwatson (lightly)
MFC after:	1 month
This commit is contained in:
jhb 2008-06-27 05:39:04 +00:00
parent 46dd6e44fc
commit 411d068395
20 changed files with 2432 additions and 738 deletions

View File

@ -2633,6 +2633,10 @@ sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
kif->kf_type = KF_TYPE_SHM;
break;
case DTYPE_SEM:
kif->kf_type = KF_TYPE_SEM;
break;
default:
kif->kf_type = KF_TYPE_UNKNOWN;
break;
@ -2767,6 +2771,8 @@ file_type_to_name(short type)
return ("mque");
case DTYPE_SHM:
return ("shm");
case DTYPE_SEM:
return ("ksem");
default:
return ("unkn");
}

File diff suppressed because it is too large Load Diff

View File

@ -46,6 +46,10 @@
* worry about the bits on disk if the page is swapped out or will the
* swapper zero the parts of a page that are invalid if the page is
* swapped back in for us?
*
* (6) Add MAC support in mac_biba(4) and mac_mls(4).
*
* (7) Add a MAC check_create() hook for creating new named objects.
*/
#include <sys/cdefs.h>

View File

@ -3,6 +3,6 @@
.PATH: ${.CURDIR}/../../kern
KMOD= sem
SRCS= uipc_sem.c opt_mac.h opt_posix.h
SRCS= uipc_sem.c opt_mac.h opt_posix.h vnode_if.h
.include <bsd.kmod.mk>

View File

@ -189,11 +189,16 @@ void mac_pipe_init(struct pipepair *);
int mac_pipe_label_set(struct ucred *cred, struct pipepair *pp,
struct label *label);
int mac_posixsem_check_getvalue(struct ucred *cred,struct ksem *ks);
int mac_posixsem_check_getvalue(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks);
int mac_posixsem_check_open(struct ucred *cred, struct ksem *ks);
int mac_posixsem_check_post(struct ucred *cred, struct ksem *ks);
int mac_posixsem_check_post(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks);
int mac_posixsem_check_stat(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks);
int mac_posixsem_check_unlink(struct ucred *cred, struct ksem *ks);
int mac_posixsem_check_wait(struct ucred *cred, struct ksem *ks);
int mac_posixsem_check_wait(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks);
void mac_posixsem_create(struct ucred *cred, struct ksem *ks);
void mac_posixsem_destroy(struct ksem *);
void mac_posixsem_init(struct ksem *);

View File

@ -288,16 +288,22 @@ typedef int (*mpo_pipe_internalize_label_t)(struct label *label,
typedef void (*mpo_pipe_relabel_t)(struct ucred *cred, struct pipepair *pp,
struct label *oldlabel, struct label *newlabel);
typedef int (*mpo_posixsem_check_getvalue_t)(struct ucred *cred,
struct ksem *ks, struct label *kslabel);
typedef int (*mpo_posixsem_check_getvalue_t)(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks,
struct label *kslabel);
typedef int (*mpo_posixsem_check_open_t)(struct ucred *cred,
struct ksem *ks, struct label *kslabel);
typedef int (*mpo_posixsem_check_post_t)(struct ucred *cred,
struct ksem *ks, struct label *kslabel);
typedef int (*mpo_posixsem_check_post_t)(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks,
struct label *kslabel);
typedef int (*mpo_posixsem_check_stat_t)(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks,
struct label *kslabel);
typedef int (*mpo_posixsem_check_unlink_t)(struct ucred *cred,
struct ksem *ks, struct label *kslabel);
typedef int (*mpo_posixsem_check_wait_t)(struct ucred *cred,
struct ksem *ks, struct label *kslabel);
typedef int (*mpo_posixsem_check_wait_t)(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks,
struct label *kslabel);
typedef void (*mpo_posixsem_create_t)(struct ucred *cred,
struct ksem *ks, struct label *kslabel);
typedef void (*mpo_posixsem_destroy_label_t)(struct label *label);
@ -742,6 +748,7 @@ struct mac_policy_ops {
mpo_posixsem_check_getvalue_t mpo_posixsem_check_getvalue;
mpo_posixsem_check_open_t mpo_posixsem_check_open;
mpo_posixsem_check_post_t mpo_posixsem_check_post;
mpo_posixsem_check_stat_t mpo_posixsem_check_stat;
mpo_posixsem_check_unlink_t mpo_posixsem_check_unlink;
mpo_posixsem_check_wait_t mpo_posixsem_check_wait;
mpo_posixsem_create_t mpo_posixsem_create;

View File

@ -101,21 +101,37 @@ mac_posixsem_check_open(struct ucred *cred, struct ksem *ks)
}
int
mac_posixsem_check_getvalue(struct ucred *cred, struct ksem *ks)
mac_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks)
{
int error;
MAC_CHECK(posixsem_check_getvalue, cred, ks, ks->ks_label);
MAC_CHECK(posixsem_check_getvalue, active_cred, file_cred, ks,
ks->ks_label);
return (error);
}
int
mac_posixsem_check_post(struct ucred *cred, struct ksem *ks)
mac_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks)
{
int error;
MAC_CHECK(posixsem_check_post, cred, ks, ks->ks_label);
MAC_CHECK(posixsem_check_post, active_cred, file_cred, ks,
ks->ks_label);
return (error);
}
int
mac_posixsem_check_stat(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks)
{
int error;
MAC_CHECK(posixsem_check_stat, active_cred, file_cred, ks,
ks->ks_label);
return (error);
}
@ -131,11 +147,13 @@ mac_posixsem_check_unlink(struct ucred *cred, struct ksem *ks)
}
int
mac_posixsem_check_wait(struct ucred *cred, struct ksem *ks)
mac_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks)
{
int error;
MAC_CHECK(posixsem_check_wait, cred, ks, ks->ks_label);
MAC_CHECK(posixsem_check_wait, active_cred, file_cred, ks,
ks->ks_label);
return (error);
}

View File

@ -1504,7 +1504,7 @@ biba_pipe_relabel(struct ucred *cred, struct pipepair *pp,
}
static int
biba_posixsem_check_write(struct ucred *cred, struct ksem *ks,
biba_posixsem_check_openunlink(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
{
struct mac_biba *subj, *obj;
@ -1522,15 +1522,33 @@ biba_posixsem_check_write(struct ucred *cred, struct ksem *ks,
}
static int
biba_posixsem_check_rdonly(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
biba_posixsem_check_write(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
struct mac_biba *subj, *obj;
if (!biba_enabled)
return (0);
subj = SLOT(cred->cr_label);
subj = SLOT(active_cred->cr_label);
obj = SLOT(kslabel);
if (!biba_dominate_effective(subj, obj))
return (EACCES);
return (0);
}
static int
biba_posixsem_check_rdonly(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
struct mac_biba *subj, *obj;
if (!biba_enabled)
return (0);
subj = SLOT(active_cred->cr_label);
obj = SLOT(kslabel);
if (!biba_dominate_effective(obj, subj))
@ -3335,9 +3353,10 @@ static struct mac_policy_ops mac_biba_ops =
.mpo_pipe_relabel = biba_pipe_relabel,
.mpo_posixsem_check_getvalue = biba_posixsem_check_rdonly,
.mpo_posixsem_check_open = biba_posixsem_check_write,
.mpo_posixsem_check_open = biba_posixsem_check_openunlink,
.mpo_posixsem_check_post = biba_posixsem_check_write,
.mpo_posixsem_check_unlink = biba_posixsem_check_write,
.mpo_posixsem_check_stat = biba_posixsem_check_rdonly,
.mpo_posixsem_check_unlink = biba_posixsem_check_openunlink,
.mpo_posixsem_check_wait = biba_posixsem_check_write,
.mpo_posixsem_create = biba_posixsem_create,
.mpo_posixsem_destroy_label = biba_destroy_label,

View File

@ -1400,7 +1400,7 @@ mls_pipe_relabel(struct ucred *cred, struct pipepair *pp,
}
static int
mls_posixsem_check_rdonly(struct ucred *cred, struct ksem *ks,
mls_posixsem_check_openunlink(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
{
struct mac_mls *subj, *obj;
@ -1411,6 +1411,24 @@ mls_posixsem_check_rdonly(struct ucred *cred, struct ksem *ks,
subj = SLOT(cred->cr_label);
obj = SLOT(kslabel);
if (!mls_dominate_effective(obj, subj))
return (EACCES);
return (0);
}
static int
mls_posixsem_check_rdonly(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
struct mac_mls *subj, *obj;
if (!mls_enabled)
return (0);
subj = SLOT(active_cred->cr_label);
obj = SLOT(kslabel);
if (!mls_dominate_effective(subj, obj))
return (EACCES);
@ -1418,15 +1436,15 @@ mls_posixsem_check_rdonly(struct ucred *cred, struct ksem *ks,
}
static int
mls_posixsem_check_write(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
mls_posixsem_check_write(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
struct mac_mls *subj, *obj;
if (!mls_enabled)
return (0);
subj = SLOT(cred->cr_label);
subj = SLOT(active_cred->cr_label);
obj = SLOT(kslabel);
if (!mls_dominate_effective(obj, subj))
@ -2958,9 +2976,10 @@ static struct mac_policy_ops mls_ops =
.mpo_pipe_relabel = mls_pipe_relabel,
.mpo_posixsem_check_getvalue = mls_posixsem_check_rdonly,
.mpo_posixsem_check_open = mls_posixsem_check_write,
.mpo_posixsem_check_open = mls_posixsem_check_openunlink,
.mpo_posixsem_check_post = mls_posixsem_check_write,
.mpo_posixsem_check_unlink = mls_posixsem_check_write,
.mpo_posixsem_check_stat = mls_posixsem_check_rdonly,
.mpo_posixsem_check_unlink = mls_posixsem_check_openunlink,
.mpo_posixsem_check_wait = mls_posixsem_check_write,
.mpo_posixsem_create = mls_posixsem_create,
.mpo_posixsem_destroy_label = mls_destroy_label,

View File

@ -523,8 +523,8 @@ stub_pipe_relabel(struct ucred *cred, struct pipepair *pp,
}
static int
stub_posixsem_check_getvalue(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
stub_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
return (0);
@ -539,8 +539,16 @@ stub_posixsem_check_open(struct ucred *cred, struct ksem *ks,
}
static int
stub_posixsem_check_post(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
stub_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
return (0);
}
static int
stub_posixsem_check_stat(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
return (0);
@ -555,8 +563,8 @@ stub_posixsem_check_unlink(struct ucred *cred, struct ksem *ks,
}
static int
stub_posixsem_check_wait(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
stub_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
return (0);
@ -1582,6 +1590,7 @@ static struct mac_policy_ops stub_ops =
.mpo_posixsem_check_getvalue = stub_posixsem_check_getvalue,
.mpo_posixsem_check_open = stub_posixsem_check_open,
.mpo_posixsem_check_post = stub_posixsem_check_post,
.mpo_posixsem_check_stat = stub_posixsem_check_stat,
.mpo_posixsem_check_unlink = stub_posixsem_check_unlink,
.mpo_posixsem_check_wait = stub_posixsem_check_wait,
.mpo_posixsem_create = stub_posixsem_create,

View File

@ -1012,11 +1012,12 @@ test_pipe_relabel(struct ucred *cred, struct pipepair *pp,
COUNTER_DECL(posixsem_check_getvalue);
static int
test_posixsem_check_getvalue(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
test_posixsem_check_getvalue(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
LABEL_CHECK(cred->cr_label, MAGIC_CRED);
LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
COUNTER_INC(posixsem_check_getvalue);
@ -1038,17 +1039,31 @@ test_posixsem_check_open(struct ucred *cred, struct ksem *ks,
COUNTER_DECL(posixsem_check_post);
static int
test_posixsem_check_post(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
test_posixsem_check_post(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
LABEL_CHECK(cred->cr_label, MAGIC_CRED);
LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
COUNTER_INC(posixsem_check_post);
return (0);
}
COUNTER_DECL(posixsem_check_stat);
static int
test_posixsem_check_stat(struct ucred *active_cred,
struct ucred *file_cred, struct ksem *ks, struct label *kslabel)
{
LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
COUNTER_INC(posixsem_check_stat);
return (0);
}
COUNTER_DECL(posixsem_check_unlink);
static int
test_posixsem_check_unlink(struct ucred *cred, struct ksem *ks,
@ -1064,11 +1079,12 @@ test_posixsem_check_unlink(struct ucred *cred, struct ksem *ks,
COUNTER_DECL(posixsem_check_wait);
static int
test_posixsem_check_wait(struct ucred *cred, struct ksem *ks,
struct label *kslabel)
test_posixsem_check_wait(struct ucred *active_cred, struct ucred *file_cred,
struct ksem *ks, struct label *kslabel)
{
LABEL_CHECK(cred->cr_label, MAGIC_CRED);
LABEL_CHECK(active_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(file_cred->cr_label, MAGIC_CRED);
LABEL_CHECK(kslabel, MAGIC_POSIX_SEM);
COUNTER_INC(posixsem_check_wait);
@ -2881,6 +2897,7 @@ static struct mac_policy_ops test_ops =
.mpo_posixsem_check_getvalue = test_posixsem_check_getvalue,
.mpo_posixsem_check_open = test_posixsem_check_open,
.mpo_posixsem_check_post = test_posixsem_check_post,
.mpo_posixsem_check_stat = test_posixsem_check_stat,
.mpo_posixsem_check_unlink = test_posixsem_check_unlink,
.mpo_posixsem_check_wait = test_posixsem_check_wait,
.mpo_posixsem_create = test_posixsem_create,

View File

@ -61,6 +61,7 @@ struct socket;
#define DTYPE_CRYPTO 6 /* crypto */
#define DTYPE_MQUEUE 7 /* posix message queue */
#define DTYPE_SHM 8 /* swap-backed shared memory */
#define DTYPE_SEM 9 /* posix semaphore */
#ifdef _KERNEL

View File

@ -34,26 +34,32 @@
#endif
#include <sys/condvar.h>
#include <sys/queue.h>
struct kuser {
pid_t ku_pid;
LIST_ENTRY(kuser) ku_next;
};
struct ksem {
LIST_ENTRY(ksem) ks_entry; /* global list entry */
int ks_onlist; /* boolean if on a list (ks_entry) */
char *ks_name; /* if named, this is the name */
int ks_ref; /* number of references */
mode_t ks_mode; /* protection bits */
uid_t ks_uid; /* creator uid */
gid_t ks_gid; /* creator gid */
unsigned int ks_value; /* current value */
struct cv ks_cv; /* waiters sleep here */
int ks_waiters; /* number of waiters */
LIST_HEAD(, kuser) ks_users; /* pids using this sem */
struct label *ks_label; /* MAC label */
int ks_ref; /* number of references */
mode_t ks_mode; /* protection bits */
uid_t ks_uid; /* creator uid */
gid_t ks_gid; /* creator gid */
unsigned int ks_value; /* current value */
struct cv ks_cv; /* waiters sleep here */
int ks_waiters; /* number of waiters */
int ks_flags;
/*
* Values maintained solely to make this a better-behaved file
* descriptor for fstat() to run on.
*
* XXX: dubious
*/
struct timespec ks_atime;
struct timespec ks_mtime;
struct timespec ks_ctime;
struct timespec ks_birthtime;
struct label *ks_label; /* MAC label */
};
#define KS_ANONYMOUS 0x0001 /* Anonymous (unnamed) semaphore. */
#define KS_DEAD 0x0002 /* No new waiters allowed. */
#endif /* !_POSIX4_KSEM_H_ */

View File

@ -249,6 +249,7 @@ struct user {
#define KF_TYPE_CRYPTO 6
#define KF_TYPE_MQUEUE 7
#define KF_TYPE_SHM 8
#define KF_TYPE_SEM 9
#define KF_TYPE_UNKNOWN 255
#define KF_VTYPE_VNON 0

View File

@ -0,0 +1,11 @@
# $FreeBSD$
PROG= posixsem
SRCS= posixsem.c test.c
DPADD= ${LIBKVM}
LDADD= -lkvm
NO_MAN=
WARNS?= 3
.include <bsd.prog.mk>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
#!/bin/sh
#
# $FreeBSD$
./posixsem

View File

@ -0,0 +1,128 @@
/*-
* Copyright (c) 2008 Yahoo!, Inc.
* All rights reserved.
* Written by: John Baldwin <jhb@FreeBSD.org>
*
* 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. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <stdarg.h>
#include <stdio.h>
#include "test.h"
static int test_index;
static struct regression_test *test;
static int test_acknowleged;
SET_DECLARE(regression_tests_set, struct regression_test);
/*
* Outputs a test summary of the following:
*
* <status> <test #> [name] [# <fmt> [fmt args]]
*/
static void
vprint_status(const char *status, const char *fmt, va_list ap)
{
printf("%s %d", status, test_index);
if (test->rt_name)
printf(" - %s", test->rt_name);
if (fmt) {
printf(" # ");
vprintf(fmt, ap);
}
printf("\n");
test_acknowleged = 1;
}
static void
print_status(const char *status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprint_status(status, fmt, ap);
va_end(ap);
}
void
pass(void)
{
print_status("ok", NULL);
}
void
fail(void)
{
print_status("not ok", NULL);
}
void
fail_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprint_status("not ok", fmt, ap);
va_end(ap);
}
void
skip(const char *reason)
{
print_status("ok", "skip %s", reason);
}
void
todo(const char *reason)
{
print_status("not ok", "TODO %s", reason);
}
void
run_tests(void)
{
struct regression_test **testp;
printf("1..%td\n", SET_COUNT(regression_tests_set));
test_index = 1;
SET_FOREACH(testp, regression_tests_set) {
test_acknowleged = 0;
test = *testp;
test->rt_function();
if (!test_acknowleged)
print_status("not ok", "unknown status");
test_index++;
}
}

View File

@ -0,0 +1,59 @@
/*-
* Copyright (c) 2008 Yahoo!, Inc.
* All rights reserved.
* Written by: John Baldwin <jhb@FreeBSD.org>
*
* 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. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD$
*/
#ifndef __TEST_H__
#define __TEST_H__
#include <sys/linker_set.h>
struct regression_test {
void (*rt_function)(void);
const char *rt_name;
};
#define TEST(function, name) \
static struct regression_test _regtest_##function = { \
(function), \
(name) \
}; \
DATA_SET(regression_tests_set, _regtest_##function)
void fail(void);
void fail_err(const char *fmt, ...);
void pass(void);
void run_tests(void);
void skip(const char *reason);
void todo(const char *reason);
#define fail_errno(tag) fail_err("%s: %s", (tag), strerror(errno))
#endif /* !__TEST_H__ */

View File

@ -222,6 +222,10 @@ procstat_files(pid_t pid, struct kinfo_proc *kipp)
str = "h";
break;
case KF_TYPE_SEM:
str = "e";
break;
case KF_TYPE_NONE:
case KF_TYPE_UNKNOWN:
default: