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