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