[PATCH 2/2] Filesystem: Add kernel event filter handler

Gedare Bloom gedare at rtems.org
Thu Oct 31 15:03:43 UTC 2013


On Thu, Oct 31, 2013 at 10:49 AM, Sebastian Huber
<sebastian.huber at embedded-brains.de> wrote:
> This handler is necessary to implement the KQUEUE(2) system calls.
>
> Add <sys/event.h> from FreeBSD 8.3.
> ---
>  cpukit/Makefile.am                           |    1 +
>  cpukit/libcsupport/include/rtems/libio.h     |   31 +++
>  cpukit/libcsupport/include/sys/event.h       |  279 ++++++++++++++++++++++++++
>  cpukit/libfs/Makefile.am                     |    1 +
>  cpukit/libfs/src/defaults/default_kqfilter.c |   38 ++++
>  cpukit/preinstall.am                         |    4 +
>  6 files changed, 354 insertions(+), 0 deletions(-)
>  create mode 100644 cpukit/libcsupport/include/sys/event.h
>  create mode 100644 cpukit/libfs/src/defaults/default_kqfilter.c
>
> diff --git a/cpukit/Makefile.am b/cpukit/Makefile.am
> index 443ed35..3e26cac 100644
> --- a/cpukit/Makefile.am
> +++ b/cpukit/Makefile.am
> @@ -50,6 +50,7 @@ include_HEADERS += libmd/md4.h
>  include_HEADERS += libmd/md5.h
>
>  include_sys_HEADERS += libcsupport/include/sys/ioccom.h
> +include_sys_HEADERS += libcsupport/include/sys/event.h
>  include_sys_HEADERS += libcsupport/include/sys/filio.h
>  include_sys_HEADERS += libcsupport/include/sys/ioctl.h
>  include_sys_HEADERS += libcsupport/include/sys/poll.h
> diff --git a/cpukit/libcsupport/include/rtems/libio.h b/cpukit/libcsupport/include/rtems/libio.h
> index a642062..21d57eb 100644
> --- a/cpukit/libcsupport/include/rtems/libio.h
> +++ b/cpukit/libcsupport/include/rtems/libio.h
> @@ -41,6 +41,8 @@
>  extern "C" {
>  #endif
>
> +struct knote;
> +
>  /**
>   * @defgroup LibIOFSOps File System Operations
>   *
> @@ -958,6 +960,22 @@ typedef int (*rtems_filesystem_poll_t)(
>  );
>
>  /**
> + * @brief Kernel event filter support.
> + *
> + * @param[in, out] iop The IO pointer.
> + * @param[in] kn The kernel event note.
> + *
> + * @retval 0 Successful operation.
> + * @retval error An error occurred.  This is usually EINVAL.
> + *
> + * @see rtems_filesystem_default_kqfilter().
> + */
> +typedef int (*rtems_filesystem_kqfilter_t)(
> +  rtems_libio_t *iop,
> +  struct knote *kn
> +);
> +
> +/**
>   * @brief File system node operations table.
>   */
>  struct _rtems_filesystem_file_handlers_r {
> @@ -973,6 +991,7 @@ struct _rtems_filesystem_file_handlers_r {
>    rtems_filesystem_fdatasync_t fdatasync_h;
>    rtems_filesystem_fcntl_t fcntl_h;
>    rtems_filesystem_poll_t poll_h;
> +  rtems_filesystem_kqfilter_t kqfilter_h;
>  };
>
>  /**
> @@ -1158,6 +1177,18 @@ int rtems_filesystem_default_poll(
>    int events
>  );
>
> +/**
> + * @brief Default kernel event filter handler.
> + *
> + * @retval EINVAL Always.
> + *
> + * @see rtems_filesystem_kqfilter_t.
> + */
> +int rtems_filesystem_default_kqfilter(
> +  rtems_libio_t *iop,
> +  struct knote *kn
> +);
> +
>  /** @} */
>
>  /**
> diff --git a/cpukit/libcsupport/include/sys/event.h b/cpukit/libcsupport/include/sys/event.h
> new file mode 100644
> index 0000000..d92eb55
> --- /dev/null
> +++ b/cpukit/libcsupport/include/sys/event.h
> @@ -0,0 +1,279 @@
> +/*-
> + * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon at FreeBSD.org>
> + * All rights reserved.
> + *
> + * 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.
> + *
> + * 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 _SYS_EVENT_H_
> +#define _SYS_EVENT_H_
> +
> +#include <sys/queue.h>
I don't think we have this include file in RTEMS?

> +
> +#define EVFILT_READ            (-1)
> +#define EVFILT_WRITE           (-2)
> +#define EVFILT_AIO             (-3)    /* attached to aio requests */
> +#define EVFILT_VNODE           (-4)    /* attached to vnodes */
> +#define EVFILT_PROC            (-5)    /* attached to struct proc */
> +#define EVFILT_SIGNAL          (-6)    /* attached to struct proc */
> +#define EVFILT_TIMER           (-7)    /* timers */
> +/*     EVFILT_NETDEV           (-8)       no longer supported */
> +#define EVFILT_FS              (-9)    /* filesystem events */
> +#define EVFILT_LIO             (-10)   /* attached to lio requests */
> +#define EVFILT_USER            (-11)   /* User events */
> +#define EVFILT_SYSCOUNT                11
> +
> +#define EV_SET(kevp_, a, b, c, d, e, f) do {   \
> +       struct kevent *kevp = (kevp_);          \
> +       (kevp)->ident = (a);                    \
> +       (kevp)->filter = (b);                   \
> +       (kevp)->flags = (c);                    \
> +       (kevp)->fflags = (d);                   \
> +       (kevp)->data = (e);                     \
> +       (kevp)->udata = (f);                    \
> +} while(0)
> +
> +struct kevent {
> +       uintptr_t       ident;          /* identifier for this event */
> +       short           filter;         /* filter for event */
> +       u_short         flags;
> +       u_int           fflags;
> +       intptr_t        data;
> +       void            *udata;         /* opaque user data identifier */
> +};
> +
> +/* actions */
> +#define EV_ADD         0x0001          /* add event to kq (implies enable) */
> +#define EV_DELETE      0x0002          /* delete event from kq */
> +#define EV_ENABLE      0x0004          /* enable event */
> +#define EV_DISABLE     0x0008          /* disable event (not reported) */
> +
> +/* flags */
> +#define EV_ONESHOT     0x0010          /* only report one occurrence */
> +#define EV_CLEAR       0x0020          /* clear event state after reporting */
> +#define EV_RECEIPT     0x0040          /* force EV_ERROR on success, data=0 */
> +#define EV_DISPATCH    0x0080          /* disable event after reporting */
> +
> +#define EV_SYSFLAGS    0xF000          /* reserved by system */
> +#define EV_FLAG1       0x2000          /* filter-specific flag */
> +
> +/* returned values */
> +#define EV_EOF         0x8000          /* EOF detected */
> +#define EV_ERROR       0x4000          /* error, data contains errno */
> +
> + /*
> +  * data/hint flags/masks for EVFILT_USER, shared with userspace
> +  *
> +  * On input, the top two bits of fflags specifies how the lower twenty four
> +  * bits should be applied to the stored value of fflags.
> +  *
> +  * On output, the top two bits will always be set to NOTE_FFNOP and the
> +  * remaining twenty four bits will contain the stored fflags value.
> +  */
> +#define NOTE_FFNOP     0x00000000              /* ignore input fflags */
> +#define NOTE_FFAND     0x40000000              /* AND fflags */
> +#define NOTE_FFOR      0x80000000              /* OR fflags */
> +#define NOTE_FFCOPY    0xc0000000              /* copy fflags */
> +#define NOTE_FFCTRLMASK        0xc0000000              /* masks for operations */
> +#define NOTE_FFLAGSMASK        0x00ffffff
> +
> +#define NOTE_TRIGGER   0x01000000              /* Cause the event to be
> +                                                  triggered for output. */
> +
> +/*
> + * data/hint flags for EVFILT_{READ|WRITE}, shared with userspace
> + */
> +#define NOTE_LOWAT     0x0001                  /* low water mark */
> +
> +/*
> + * data/hint flags for EVFILT_VNODE, shared with userspace
> + */
> +#define        NOTE_DELETE     0x0001                  /* vnode was removed */
> +#define        NOTE_WRITE      0x0002                  /* data contents changed */
> +#define        NOTE_EXTEND     0x0004                  /* size increased */
> +#define        NOTE_ATTRIB     0x0008                  /* attributes changed */
> +#define        NOTE_LINK       0x0010                  /* link count changed */
> +#define        NOTE_RENAME     0x0020                  /* vnode was renamed */
> +#define        NOTE_REVOKE     0x0040                  /* vnode access was revoked */
> +
> +/*
> + * data/hint flags for EVFILT_PROC, shared with userspace
> + */
> +#define        NOTE_EXIT       0x80000000              /* process exited */
> +#define        NOTE_FORK       0x40000000              /* process forked */
> +#define        NOTE_EXEC       0x20000000              /* process exec'd */
> +#define        NOTE_PCTRLMASK  0xf0000000              /* mask for hint bits */
> +#define        NOTE_PDATAMASK  0x000fffff              /* mask for pid */
> +
> +/* additional flags for EVFILT_PROC */
> +#define        NOTE_TRACK      0x00000001              /* follow across forks */
> +#define        NOTE_TRACKERR   0x00000002              /* could not track child */
> +#define        NOTE_CHILD      0x00000004              /* am a child process */
> +
> +struct knote;
> +SLIST_HEAD(klist, knote);
> +struct kqueue;
> +SLIST_HEAD(kqlist, kqueue);
> +struct knlist {
> +       struct  klist   kl_list;
> +       void    (*kl_lock)(void *);     /* lock function */
> +       void    (*kl_unlock)(void *);
> +       void    (*kl_assert_locked)(void *);
> +       void    (*kl_assert_unlocked)(void *);
> +       void *kl_lockarg;               /* argument passed to kl_lockf() */
> +};
> +
> +
> +#ifdef _KERNEL
> +
> +#ifdef MALLOC_DECLARE
> +MALLOC_DECLARE(M_KQUEUE);
> +#endif
> +
> +/*
> + * Flags for knote call
> + */
> +#define        KNF_LISTLOCKED  0x0001                  /* knlist is locked */
> +#define        KNF_NOKQLOCK    0x0002                  /* do not keep KQ_LOCK */
> +
> +#define KNOTE(list, hist, flags)       knote(list, hist, flags)
> +#define KNOTE_LOCKED(list, hint)       knote(list, hint, KNF_LISTLOCKED)
> +#define KNOTE_UNLOCKED(list, hint)     knote(list, hint, 0)
> +
> +#define        KNLIST_EMPTY(list)              SLIST_EMPTY(&(list)->kl_list)
> +
> +/*
> + * Flag indicating hint is a signal.  Used by EVFILT_SIGNAL, and also
> + * shared by EVFILT_PROC  (all knotes attached to p->p_klist)
> + */
> +#define NOTE_SIGNAL    0x08000000
> +
> +/*
> + * Hint values for the optional f_touch event filter.  If f_touch is not set
> + * to NULL and f_isfd is zero the f_touch filter will be called with the type
> + * argument set to EVENT_REGISTER during a kevent() system call.  It is also
> + * called under the same conditions with the type argument set to EVENT_PROCESS
> + * when the event has been triggered.
> + */
> +#define EVENT_REGISTER 1
> +#define EVENT_PROCESS  2
> +
> +struct filterops {
> +       int     f_isfd;         /* true if ident == filedescriptor */
> +       int     (*f_attach)(struct knote *kn);
> +       void    (*f_detach)(struct knote *kn);
> +       int     (*f_event)(struct knote *kn, long hint);
> +       void    (*f_touch)(struct knote *kn, struct kevent *kev, u_long type);
> +};
> +
> +/*
> + * Setting the KN_INFLUX flag enables you to unlock the kq that this knote
> + * is on, and modify kn_status as if you had the KQ lock.
> + *
> + * kn_sfflags, kn_sdata, and kn_kevent are protected by the knlist lock.
> + */
> +struct knote {
> +       SLIST_ENTRY(knote)      kn_link;        /* for kq */
> +       SLIST_ENTRY(knote)      kn_selnext;     /* for struct selinfo */
> +       struct                  knlist *kn_knlist;      /* f_attach populated */
> +       TAILQ_ENTRY(knote)      kn_tqe;
> +       struct                  kqueue *kn_kq;  /* which queue we are on */
> +       struct                  kevent kn_kevent;
> +       int                     kn_status;      /* protected by kq lock */
> +#define KN_ACTIVE      0x01                    /* event has been triggered */
> +#define KN_QUEUED      0x02                    /* event is on queue */
> +#define KN_DISABLED    0x04                    /* event is disabled */
> +#define KN_DETACHED    0x08                    /* knote is detached */
> +#define KN_INFLUX      0x10                    /* knote is in flux */
> +#define KN_MARKER      0x20                    /* ignore this knote */
> +#define KN_KQUEUE      0x40                    /* this knote belongs to a kq */
> +#define KN_HASKQLOCK   0x80                    /* for _inevent */
> +       int                     kn_sfflags;     /* saved filter flags */
> +       intptr_t                kn_sdata;       /* saved data field */
> +       union {
> +               struct          file *p_fp;     /* file data pointer */
> +               struct          proc *p_proc;   /* proc pointer */
> +               struct          aiocblist *p_aio;       /* AIO job pointer */
> +               struct          aioliojob *p_lio;       /* LIO job pointer */
> +       } kn_ptr;
> +       struct                  filterops *kn_fop;
> +       void                    *kn_hook;
> +       int                     kn_hookid;
> +
> +#define kn_id          kn_kevent.ident
> +#define kn_filter      kn_kevent.filter
> +#define kn_flags       kn_kevent.flags
> +#define kn_fflags      kn_kevent.fflags
> +#define kn_data                kn_kevent.data
> +#define kn_fp          kn_ptr.p_fp
> +};
> +struct kevent_copyops {
> +       void    *arg;
> +       int     (*k_copyout)(void *arg, struct kevent *kevp, int count);
> +       int     (*k_copyin)(void *arg, struct kevent *kevp, int count);
> +};
> +
> +struct thread;
> +struct proc;
> +struct knlist;
> +struct mtx;
> +
> +extern void    knote(struct knlist *list, long hint, int lockflags);
> +extern void    knote_fork(struct knlist *list, int pid);
> +extern void    knlist_add(struct knlist *knl, struct knote *kn, int islocked);
> +extern void    knlist_remove(struct knlist *knl, struct knote *kn, int islocked);
> +extern void    knlist_remove_inevent(struct knlist *knl, struct knote *kn);
> +extern int     knlist_empty(struct knlist *knl);
> +extern void    knlist_init(struct knlist *knl, void *lock,
> +    void (*kl_lock)(void *), void (*kl_unlock)(void *),
> +    void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *));
> +extern void    knlist_init_mtx(struct knlist *knl, struct mtx *lock);
> +extern void    knlist_destroy(struct knlist *knl);
> +extern void    knlist_cleardel(struct knlist *knl, struct thread *td,
> +       int islocked, int killkn);
> +#define knlist_clear(knl, islocked)                            \
> +               knlist_cleardel((knl), NULL, (islocked), 0)
> +#define knlist_delete(knl, td, islocked)                       \
> +               knlist_cleardel((knl), (td), (islocked), 1)
> +extern void    knote_fdclose(struct thread *p, int fd);
> +extern int     kqfd_register(int fd, struct kevent *kev, struct thread *p,
> +                   int waitok);
> +extern int     kqueue_add_filteropts(int filt, struct filterops *filtops);
> +extern int     kqueue_del_filteropts(int filt);
> +
> +#else  /* !_KERNEL */
> +
> +#include <sys/cdefs.h>
> +struct timespec;
> +
> +__BEGIN_DECLS
> +int     kqueue(void);
> +int     kevent(int kq, const struct kevent *changelist, int nchanges,
> +           struct kevent *eventlist, int nevents,
> +           const struct timespec *timeout);
> +__END_DECLS
> +
> +#endif /* !_KERNEL */
> +
> +#endif /* !_SYS_EVENT_H_ */
> diff --git a/cpukit/libfs/Makefile.am b/cpukit/libfs/Makefile.am
> index b8496d5..21bb7ef 100644
> --- a/cpukit/libfs/Makefile.am
> +++ b/cpukit/libfs/Makefile.am
> @@ -33,6 +33,7 @@ libdefaultfs_a_SOURCES = \
>      src/defaults/default_node_type.c \
>      src/defaults/default_ftruncate_directory.c \
>      src/defaults/default_handlers.c src/defaults/default_ops.c
> +libdefaultfs_a_SOURCES += src/defaults/default_kqfilter.c
>  libdefaultfs_a_SOURCES += src/defaults/default_poll.c
>
>  noinst_LIBRARIES += libimfs.a
> diff --git a/cpukit/libfs/src/defaults/default_kqfilter.c b/cpukit/libfs/src/defaults/default_kqfilter.c
> new file mode 100644
> index 0000000..6c84539
> --- /dev/null
> +++ b/cpukit/libfs/src/defaults/default_kqfilter.c
> @@ -0,0 +1,38 @@
> +/**
> + * @file
> + *
> + * @brief Default Kernel Event Filter Handler
> + *
> + * @ingroup LibIOFSHandler
> + */
> +
> +/*
> + * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
> + *
> + *  embedded brains GmbH
> + *  Dornierstr. 4
> + *  82178 Puchheim
> + *  Germany
> + *  <rtems at embedded-brains.de>
> + *
> + * The license and distribution terms for this file may be
> + * found in the file LICENSE in this distribution or at
> + * http://www.rtems.com/license/LICENSE.
> + */
> +
> +#if HAVE_CONFIG_H
> +  #include "config.h"
> +#endif
> +
> +#include <rtems/libio_.h>
> +
> +#include <sys/types.h>
> +#include <sys/event.h>
> +
> +int rtems_filesystem_default_kqfilter(
> +  rtems_libio_t *iop,
> +  struct knote *kn
> +)
> +{
> +  return EINVAL;
> +}
> diff --git a/cpukit/preinstall.am b/cpukit/preinstall.am
> index bdcc359..4e80a3f 100644
> --- a/cpukit/preinstall.am
> +++ b/cpukit/preinstall.am
> @@ -68,6 +68,10 @@ $(PROJECT_INCLUDE)/sys/ioccom.h: libcsupport/include/sys/ioccom.h $(PROJECT_INCL
>         $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/sys/ioccom.h
>  PREINSTALL_FILES += $(PROJECT_INCLUDE)/sys/ioccom.h
>
> +$(PROJECT_INCLUDE)/sys/event.h: libcsupport/include/sys/event.h $(PROJECT_INCLUDE)/sys/$(dirstamp)
> +       $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/sys/event.h
> +PREINSTALL_FILES += $(PROJECT_INCLUDE)/sys/event.h
> +
>  $(PROJECT_INCLUDE)/sys/filio.h: libcsupport/include/sys/filio.h $(PROJECT_INCLUDE)/sys/$(dirstamp)
>         $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/sys/filio.h
>  PREINSTALL_FILES += $(PROJECT_INCLUDE)/sys/filio.h
> --
> 1.7.7
>
> _______________________________________________
> rtems-devel mailing list
> rtems-devel at rtems.org
> http://www.rtems.org/mailman/listinfo/rtems-devel



More information about the devel mailing list