Home | History | Annotate | Line # | Download | only in refuse
      1  1.1  pho /* $NetBSD: chan.c,v 1.1 2022/01/22 08:09:40 pho Exp $ */
      2  1.1  pho 
      3  1.1  pho /*
      4  1.1  pho  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  1.1  pho  * All rights reserved.
      6  1.1  pho  *
      7  1.1  pho  * Redistribution and use in source and binary forms, with or without
      8  1.1  pho  * modification, are permitted provided that the following conditions
      9  1.1  pho  * are met:
     10  1.1  pho  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pho  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pho  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pho  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pho  *    documentation and/or other materials provided with the distribution.
     15  1.1  pho  * 3. The name of the author may not be used to endorse or promote
     16  1.1  pho  *    products derived from this software without specific prior written
     17  1.1  pho  *    permission.
     18  1.1  pho  *
     19  1.1  pho  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  1.1  pho  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  1.1  pho  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  pho  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     23  1.1  pho  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  pho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     25  1.1  pho  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  pho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27  1.1  pho  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     28  1.1  pho  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     29  1.1  pho  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1  pho  */
     31  1.1  pho 
     32  1.1  pho #include <sys/cdefs.h>
     33  1.1  pho #if !defined(lint)
     34  1.1  pho __RCSID("$NetBSD: chan.c,v 1.1 2022/01/22 08:09:40 pho Exp $");
     35  1.1  pho #endif /* !lint */
     36  1.1  pho 
     37  1.1  pho #include <assert.h>
     38  1.1  pho #include <err.h>
     39  1.1  pho #include <fuse_internal.h>
     40  1.1  pho #if defined(MULTITHREADED_REFUSE)
     41  1.1  pho #  include <pthread.h>
     42  1.1  pho #endif
     43  1.1  pho #include <stdlib.h>
     44  1.1  pho #include <string.h>
     45  1.1  pho 
     46  1.1  pho /* The communication channel API is a part of the public interface of
     47  1.1  pho  * FUSE. However, it is actually an implementation detail and we can't
     48  1.1  pho  * really emulate its semantics. We only use it for implementing
     49  1.1  pho  * pre-3.0 fuse_mount(), i.e. the mount-before-new thingy.
     50  1.1  pho  */
     51  1.1  pho 
     52  1.1  pho struct fuse_chan {
     53  1.1  pho     char* mountpoint;
     54  1.1  pho     struct fuse_args* args;
     55  1.1  pho     struct fuse* fuse;
     56  1.1  pho     bool is_to_be_destroyed;
     57  1.1  pho };
     58  1.1  pho 
     59  1.1  pho struct refuse_chan_storage {
     60  1.1  pho     size_t n_alloc;
     61  1.1  pho     struct fuse_chan** vec;
     62  1.1  pho };
     63  1.1  pho 
     64  1.1  pho #if defined(MULTITHREADED_REFUSE)
     65  1.1  pho static pthread_mutex_t storage_mutex = PTHREAD_MUTEX_INITIALIZER;
     66  1.1  pho #endif
     67  1.1  pho static struct refuse_chan_storage storage;
     68  1.1  pho 
     69  1.1  pho 
     70  1.1  pho struct fuse_chan* fuse_chan_new(const char* mountpoint, const struct fuse_args* args) {
     71  1.1  pho     struct fuse_chan* chan;
     72  1.1  pho 
     73  1.1  pho     chan = calloc(1, sizeof(*chan));
     74  1.1  pho     if (!chan) {
     75  1.1  pho         warn("%s", __func__);
     76  1.1  pho         return NULL;
     77  1.1  pho     }
     78  1.1  pho 
     79  1.1  pho     chan->mountpoint = strdup(mountpoint);
     80  1.1  pho     if (!chan->mountpoint) {
     81  1.1  pho         warn("%s", __func__);
     82  1.1  pho         free(chan);
     83  1.1  pho         return NULL;
     84  1.1  pho     }
     85  1.1  pho 
     86  1.1  pho     chan->args = fuse_opt_deep_copy_args(args->argc, args->argv);
     87  1.1  pho     if (!chan->args) {
     88  1.1  pho         warn("%s", __func__);
     89  1.1  pho         free(chan->mountpoint);
     90  1.1  pho         free(chan);
     91  1.1  pho         return NULL;
     92  1.1  pho     }
     93  1.1  pho 
     94  1.1  pho     return chan;
     95  1.1  pho }
     96  1.1  pho 
     97  1.1  pho void
     98  1.1  pho fuse_chan_destroy(struct fuse_chan* chan) {
     99  1.1  pho     free(chan->mountpoint);
    100  1.1  pho     fuse_opt_free_args(chan->args);
    101  1.1  pho     free(chan->args);
    102  1.1  pho     free(chan);
    103  1.1  pho }
    104  1.1  pho 
    105  1.1  pho int
    106  1.1  pho fuse_chan_stash(struct fuse_chan* chan) {
    107  1.1  pho     int idx;
    108  1.1  pho #if defined(MULTITHREADED_REFUSE)
    109  1.1  pho     int rv;
    110  1.1  pho 
    111  1.1  pho     rv = pthread_mutex_lock(&storage_mutex);
    112  1.1  pho     assert(rv == 0);
    113  1.1  pho #endif
    114  1.1  pho 
    115  1.1  pho     /* Find the first empty slot in the storage. */
    116  1.1  pho     for (idx = 0; idx < (int)storage.n_alloc; idx++) {
    117  1.1  pho         if (storage.vec[idx] == NULL) {
    118  1.1  pho             storage.vec[idx] = chan;
    119  1.1  pho             goto done;
    120  1.1  pho         }
    121  1.1  pho     }
    122  1.1  pho 
    123  1.1  pho     /* Allocate more space */
    124  1.1  pho     storage.n_alloc = (storage.n_alloc + 8) * 2;
    125  1.1  pho     storage.vec     = realloc(storage.vec, sizeof(struct fuse_chan*) * storage.n_alloc);
    126  1.1  pho     if (!storage.vec) {
    127  1.1  pho         warn("%s", __func__);
    128  1.1  pho         idx = -1;
    129  1.1  pho         goto done;
    130  1.1  pho     }
    131  1.1  pho 
    132  1.1  pho     storage.vec[idx] = chan;
    133  1.1  pho     memset(&storage.vec[idx+1], 0, sizeof(struct fuse_chan*) * (storage.n_alloc - (size_t)idx - 1));
    134  1.1  pho 
    135  1.1  pho   done:
    136  1.1  pho #if defined(MULTITHREADED_REFUSE)
    137  1.1  pho     rv = pthread_mutex_unlock(&storage_mutex);
    138  1.1  pho     assert(rv == 0);
    139  1.1  pho #endif
    140  1.1  pho     return idx;
    141  1.1  pho }
    142  1.1  pho 
    143  1.1  pho /* Acquire a pointer to a stashed channel with a given index. */
    144  1.1  pho struct fuse_chan* fuse_chan_peek(int idx) {
    145  1.1  pho     struct fuse_chan* chan = NULL;
    146  1.1  pho #if defined(MULTITHREADED_REFUSE)
    147  1.1  pho     int rv;
    148  1.1  pho 
    149  1.1  pho     rv = pthread_mutex_lock(&storage_mutex);
    150  1.1  pho     assert(rv == 0);
    151  1.1  pho #endif
    152  1.1  pho 
    153  1.1  pho     if (idx >= 0 && idx < (int)storage.n_alloc) {
    154  1.1  pho         chan = storage.vec[idx];
    155  1.1  pho     }
    156  1.1  pho 
    157  1.1  pho #if defined(MULTITHREADED_REFUSE)
    158  1.1  pho     rv = pthread_mutex_unlock(&storage_mutex);
    159  1.1  pho     assert(rv == 0);
    160  1.1  pho #endif
    161  1.1  pho     return chan;
    162  1.1  pho }
    163  1.1  pho 
    164  1.1  pho /* Like fuse_chan_peek() but also removes the channel from the
    165  1.1  pho  * storage. */
    166  1.1  pho struct fuse_chan* fuse_chan_take(int idx) {
    167  1.1  pho     struct fuse_chan* chan = NULL;
    168  1.1  pho #if defined(MULTITHREADED_REFUSE)
    169  1.1  pho     int rv;
    170  1.1  pho 
    171  1.1  pho     rv = pthread_mutex_lock(&storage_mutex);
    172  1.1  pho     assert(rv == 0);
    173  1.1  pho #endif
    174  1.1  pho 
    175  1.1  pho     if (idx >= 0 && idx < (int)storage.n_alloc) {
    176  1.1  pho         chan = storage.vec[idx];
    177  1.1  pho         storage.vec[idx] = NULL;
    178  1.1  pho     }
    179  1.1  pho 
    180  1.1  pho #if defined(MULTITHREADED_REFUSE)
    181  1.1  pho     rv = pthread_mutex_unlock(&storage_mutex);
    182  1.1  pho     assert(rv == 0);
    183  1.1  pho #endif
    184  1.1  pho     return chan;
    185  1.1  pho }
    186  1.1  pho 
    187  1.1  pho /* Find the first stashed channel satisfying a given predicate in the
    188  1.1  pho  * storage, or NULL if no channels satisfy it. */
    189  1.1  pho struct fuse_chan*
    190  1.1  pho fuse_chan_find(bool (*pred)(struct fuse_chan*, void*),
    191  1.1  pho                int* found_idx, void* priv) {
    192  1.1  pho     int idx;
    193  1.1  pho     struct fuse_chan* chan = NULL;
    194  1.1  pho #if defined(MULTITHREADED_REFUSE)
    195  1.1  pho     int rv;
    196  1.1  pho 
    197  1.1  pho     rv = pthread_mutex_lock(&storage_mutex);
    198  1.1  pho     assert(rv == 0);
    199  1.1  pho #endif
    200  1.1  pho 
    201  1.1  pho     for (idx = 0; idx < (int)storage.n_alloc; idx++) {
    202  1.1  pho         if (storage.vec[idx] != NULL) {
    203  1.1  pho             if (pred(storage.vec[idx], priv)) {
    204  1.1  pho                 chan = storage.vec[idx];
    205  1.1  pho                 if (found_idx)
    206  1.1  pho                     *found_idx = idx;
    207  1.1  pho                 goto done;
    208  1.1  pho             }
    209  1.1  pho         }
    210  1.1  pho     }
    211  1.1  pho 
    212  1.1  pho   done:
    213  1.1  pho #if defined(MULTITHREADED_REFUSE)
    214  1.1  pho     rv = pthread_mutex_unlock(&storage_mutex);
    215  1.1  pho     assert(rv == 0);
    216  1.1  pho #endif
    217  1.1  pho     return chan;
    218  1.1  pho }
    219  1.1  pho 
    220  1.1  pho void
    221  1.1  pho fuse_chan_set_fuse(struct fuse_chan* chan, struct fuse* fuse) {
    222  1.1  pho     chan->fuse = fuse;
    223  1.1  pho }
    224  1.1  pho 
    225  1.1  pho void
    226  1.1  pho fuse_chan_set_to_be_destroyed(struct fuse_chan* chan, bool is_to_be_destroyed) {
    227  1.1  pho     chan->is_to_be_destroyed = is_to_be_destroyed;
    228  1.1  pho }
    229  1.1  pho 
    230  1.1  pho const char*
    231  1.1  pho fuse_chan_mountpoint(const struct fuse_chan* chan) {
    232  1.1  pho     return chan->mountpoint;
    233  1.1  pho }
    234  1.1  pho 
    235  1.1  pho struct fuse_args*
    236  1.1  pho fuse_chan_args(struct fuse_chan* chan) {
    237  1.1  pho     return chan->args;
    238  1.1  pho }
    239  1.1  pho 
    240  1.1  pho struct fuse*
    241  1.1  pho fuse_chan_fuse(struct fuse_chan* chan) {
    242  1.1  pho     return chan->fuse;
    243  1.1  pho }
    244  1.1  pho 
    245  1.1  pho bool
    246  1.1  pho fuse_chan_is_to_be_destroyed(const struct fuse_chan* chan) {
    247  1.1  pho     return chan->is_to_be_destroyed;
    248  1.1  pho }
    249