Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpuffs Add convenience routine puffs_unmountonsignal()...



details:   https://anonhg.NetBSD.org/src/rev/d2b2bf258413
branches:  trunk
changeset: 750737:d2b2bf258413
user:      pooka <pooka%NetBSD.org@localhost>
date:      Tue Jan 12 18:42:38 2010 +0000

description:
Add convenience routine puffs_unmountonsignal(), which does exactly that.

diffstat:

 lib/libpuffs/framebuf.c   |  14 +++++-----
 lib/libpuffs/puffs.3      |  22 +++++++++++++++-
 lib/libpuffs/puffs.c      |  61 ++++++++++++++++++++++++++++++++++++++++------
 lib/libpuffs/puffs.h      |   4 ++-
 lib/libpuffs/puffs_priv.h |   5 ++-
 5 files changed, 85 insertions(+), 21 deletions(-)

diffs (288 lines):

diff -r 23f43c13a836 -r d2b2bf258413 lib/libpuffs/framebuf.c
--- a/lib/libpuffs/framebuf.c   Tue Jan 12 17:39:21 2010 +0000
+++ b/lib/libpuffs/framebuf.c   Tue Jan 12 18:42:38 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: framebuf.c,v 1.29 2008/09/04 15:30:36 pooka Exp $      */
+/*     $NetBSD: framebuf.c,v 1.30 2010/01/12 18:42:38 pooka Exp $      */
 
 /*
  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: framebuf.c,v 1.29 2008/09/04 15:30:36 pooka Exp $");
+__RCSID("$NetBSD: framebuf.c,v 1.30 2010/01/12 18:42:38 pooka Exp $");
 #endif /* !lint */
 
 #include <sys/types.h>
@@ -783,11 +783,11 @@
        struct puffs_fctrl_io *fio;
        struct kevent *newevs;
        struct kevent kev[2];
-       size_t nfds;
+       size_t nevs;
        int rv, readenable;
 
-       nfds = pu->pu_nfds+1;
-       newevs = realloc(pu->pu_evs, (2*nfds) * sizeof(struct kevent));
+       nevs = pu->pu_nevs+2;
+       newevs = realloc(pu->pu_evs, nevs*sizeof(struct kevent));
        if (newevs == NULL)
                return -1;
        pu->pu_evs = newevs;
@@ -824,7 +824,7 @@
                fio->stat |= FIO_ENABLE_W;
 
        LIST_INSERT_HEAD(&pu->pu_ios, fio, fio_entries);
-       pu->pu_nfds = nfds;
+       pu->pu_nevs = nevs;
 
        return 0;
 }
@@ -999,7 +999,7 @@
        }
 
        /* don't bother with realloc */
-       pu->pu_nfds--;
+       pu->pu_nevs -= 2;
 
        /* don't free us yet, might have some references in event arrays */
        fio->stat |= FIO_DEAD;
diff -r 23f43c13a836 -r d2b2bf258413 lib/libpuffs/puffs.3
--- a/lib/libpuffs/puffs.3      Tue Jan 12 17:39:21 2010 +0000
+++ b/lib/libpuffs/puffs.3      Tue Jan 12 18:42:38 2010 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: puffs.3,v 1.46 2009/02/20 14:26:56 pooka Exp $
+.\"    $NetBSD: puffs.3,v 1.47 2010/01/12 18:42:38 pooka Exp $
 .\"
 .\" Copyright (c) 2006, 2007, 2008 Antti Kantee.  All rights reserved.
 .\"
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 12, 2008
+.Dd January 8, 2010
 .Dt PUFFS 3
 .Os
 .Sh NAME
@@ -82,6 +82,8 @@
 .Ft int
 .Fn puffs_mainloop "struct puffs_usermount *pu"
 .Ft int
+.Fn puffs_unmountonsignal "int sig" "bool ignoresig"
+.Ft int
 .Fo puffs_dispatch_create
 .Fa "struct puffs_usermount *pu" "struct puffs_framebuf *pb"
 .Fa "struct puffs_cc **pccp"
@@ -438,6 +440,22 @@
 .Xr puffs_framebuf 3
 has been initialized, I/O from the relevant descriptors is processed
 automatically by the eventloop.
+.It Fn puffs_unmountonsignal signum ignoresig
+Cause all file servers within the process to initiate unmount upon
+receipt of signal
+.Ar signum .
+This works only for servers which call
+.Fn puffs_mainloop
+and must be called before any server within the process enters the mainloop.
+The process signal handler is still called before starting the unmount
+procedure.
+The parameter
+.Ar ignoresig
+is provided as a convenience and tells if to install a signal handler
+to ignore
+.Ar sig
+so that the process will not e.g. terminate based on the default action
+before the file system unmount can be initiated.
 .It Fn puffs_dispatch_create pu pb pccp
 .It Fn puffs_dispatch_exec pcc pbp
 In case the use of
diff -r 23f43c13a836 -r d2b2bf258413 lib/libpuffs/puffs.c
--- a/lib/libpuffs/puffs.c      Tue Jan 12 17:39:21 2010 +0000
+++ b/lib/libpuffs/puffs.c      Tue Jan 12 18:42:38 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: puffs.c,v 1.104 2010/01/07 23:03:26 pooka Exp $        */
+/*     $NetBSD: puffs.c,v 1.105 2010/01/12 18:42:38 pooka Exp $        */
 
 /*
  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: puffs.c,v 1.104 2010/01/07 23:03:26 pooka Exp $");
+__RCSID("$NetBSD: puffs.c,v 1.105 2010/01/12 18:42:38 pooka Exp $");
 #endif /* !lint */
 
 #include <sys/param.h>
@@ -731,6 +731,29 @@
        return 0;
 }
 
+/* no sigset_t static intializer */
+static int sigs[NSIG] = { 0, };
+static int sigcatch = 0;
+
+int
+puffs_unmountonsignal(int sig, bool sigignore)
+{
+
+       if (sig < 0 || sig >= (int)NSIG) {
+               errno = EINVAL;
+               return -1;
+       }
+       if (sigignore)
+               if (signal(sig, SIG_IGN) == SIG_ERR)
+                       return -1;
+
+       if (!sigs[sig])
+               sigcatch++;
+       sigs[sig] = 1;
+
+       return 0;
+}
+
 /*
  * Actual mainloop.  This is called from a context which can block.
  * It is called either from puffs_mainloop (indirectly, via
@@ -747,6 +770,7 @@
        int ndone;
 
        while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
+
                /*
                 * Schedule existing requests.
                 */
@@ -819,11 +843,10 @@
                                fio->stat &= ~FIO_WR;
                                nchanges++;
                        }
-                       assert(nchanges <= pu->pu_nfds);
                }
 
                ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
-                   pu->pu_evs, 2*pu->pu_nfds, pu->pu_ml_timep);
+                   pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
 
                if (ndone == -1) {
                        if (errno != EINTR)
@@ -850,7 +873,10 @@
 #endif
 
                        fio = (void *)curev->udata;
-                       pfctrl = fio->fctrl;
+                       if (__predict_true(fio))
+                               pfctrl = fio->fctrl;
+                       else
+                               pfctrl = NULL;
                        if (curev->flags & EV_ERROR) {
                                assert(curev->filter == EVFILT_WRITE);
                                fio->stat &= ~FIO_WR;
@@ -872,6 +898,13 @@
                                puffs__framev_output(pu, pfctrl, fio);
                                what |= PUFFS_FBIO_WRITE;
                        }
+
+                       else if (__predict_false(curev->filter==EVFILT_SIGNAL)){
+                               if ((pu->pu_state & PU_DONEXIT) == 0) {
+                                       PU_SETSFLAG(pu, PU_DONEXIT);
+                                       puffs_exit(pu, 0);
+                               }
+                       }
                        if (what)
                                puffs__framev_notify(fio, what);
                }
@@ -889,14 +922,14 @@
        if (puffs__cc_restoremain(pu) == -1)
                warn("cannot restore main context.  impending doom");
 }
-
 int
 puffs_mainloop(struct puffs_usermount *pu)
 {
        struct puffs_fctrl_io *fio;
        struct puffs_cc *pcc;
        struct kevent *curev;
-       int sverrno;
+       size_t nevs;
+       int sverrno, i;
 
        assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
 
@@ -911,10 +944,12 @@
            &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
                goto out;
 
-       curev = realloc(pu->pu_evs, (2*pu->pu_nfds)*sizeof(struct kevent));
+       nevs = pu->pu_nevs + sigcatch;
+       curev = realloc(pu->pu_evs, nevs * sizeof(struct kevent));
        if (curev == NULL)
                goto out;
        pu->pu_evs = curev;
+       pu->pu_nevs = nevs;
 
        LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
                EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
@@ -924,7 +959,15 @@
                    0, 0, (uintptr_t)fio);
                curev++;
        }
-       if (kevent(pu->pu_kq, pu->pu_evs, 2*pu->pu_nfds, NULL, 0, NULL) == -1)
+       for (i = 0; i < NSIG; i++) {
+               if (sigs[i]) {
+                       EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
+                           0, 0, 0);
+                       curev++;
+               }
+       }
+       assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
+       if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
                goto out;
 
        pu->pu_state |= PU_INLOOP;
diff -r 23f43c13a836 -r d2b2bf258413 lib/libpuffs/puffs.h
--- a/lib/libpuffs/puffs.h      Tue Jan 12 17:39:21 2010 +0000
+++ b/lib/libpuffs/puffs.h      Tue Jan 12 18:42:38 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: puffs.h,v 1.113 2009/12/05 20:54:10 pooka Exp $        */
+/*     $NetBSD: puffs.h,v 1.114 2010/01/12 18:42:39 pooka Exp $        */
 
 /*
  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
@@ -427,6 +427,8 @@
 int            puffs_mainloop(struct puffs_usermount *);
 int            puffs_daemon(struct puffs_usermount *, int, int);
 
+int            puffs_unmountonsignal(int, bool);
+
 
 int    puffs_getselectable(struct puffs_usermount *);
 int    puffs_setblockingmode(struct puffs_usermount *, int);
diff -r 23f43c13a836 -r d2b2bf258413 lib/libpuffs/puffs_priv.h
--- a/lib/libpuffs/puffs_priv.h Tue Jan 12 17:39:21 2010 +0000
+++ b/lib/libpuffs/puffs_priv.h Tue Jan 12 18:42:38 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: puffs_priv.h,v 1.41 2008/08/11 16:23:37 pooka Exp $    */
+/*     $NetBSD: puffs_priv.h,v 1.42 2010/01/12 18:42:39 pooka Exp $    */
 
 /*
  * Copyright (c) 2006, 2007, 2008 Antti Kantee.  All Rights Reserved.
@@ -116,6 +116,7 @@
 #define PU_HASKQ       0x0400
 #define PU_PUFFSDAEMON 0x0800
 #define PU_MAINRESTORE 0x1000
+#define PU_DONEXIT     0x2000
 #define PU_SETSTATE(pu, s) (pu->pu_state = (s) | (pu->pu_state & ~PU_STATEMASK))
 #define PU_SETSFLAG(pu, s) (pu->pu_state |= (s))
 #define PU_CLRSFLAG(pu, s) \
@@ -149,7 +150,7 @@
        LIST_HEAD(, puffs_fctrl_io) pu_ios;
        LIST_HEAD(, puffs_fctrl_io) pu_ios_rmlist;
        struct kevent           *pu_evs;
-       size_t                  pu_nfds;
+       size_t                  pu_nevs;
 
        puffs_ml_loop_fn        pu_ml_lfn;
        struct timespec         pu_ml_timeout;



Home | Main Index | Thread Index | Old Index