-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.c
196 lines (171 loc) · 3.6 KB
/
mux.c
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
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
enum {
Readmax = 8,
Muxmax = 8,
Pathmax = 32,
};
struct muxpoint {
char path[Pathmax];
int rh[Readmax];
int wh[Readmax];
int c;
} muxs[Muxmax];
pthread_mutex_t openlock = PTHREAD_MUTEX_INITIALIZER;
static int
muxfind(const char *path){
int i;
pthread_mutex_lock(&openlock);
for(i=0;i<Muxmax;i++){
if(strcmp(muxs[i].path, path)==0) break;
}
if(i==Muxmax) for(i=0;i<Muxmax;i++){
if(*muxs[i].path == 0){
strncpy(muxs[i].path, path, Pathmax);
break;
}
}
muxs[i].c++;
pthread_mutex_unlock(&openlock);
if(i==Muxmax) return -ENOMEM;
return i;
}
static int
mux_openr(const char *path){
int i, m;
int fds[2];
m = muxfind(path);
if(m < 0) return m;
if(pipe(fds) < 0) return 0-errno;
for(i=0;i<Readmax;i++){
if(__sync_bool_compare_and_swap(&muxs[m].wh[i], 0, fds[1])) break;
}
if(i==Readmax){
close(fds[0]);
close(fds[1]);
return -ENOMEM;
}
muxs[m].rh[i]=fds[0];
return i*Muxmax+m;
}
static int
mux_open(const char *path, struct fuse_file_info *fi){
if(strlen(path) > Pathmax-1) return -ENAMETOOLONG;
fi->direct_io=1;
fi->nonseekable=1;
if ((fi->flags & 3) == O_RDONLY){
fi->fh = mux_openr(path);
return 0;
}
if ((fi->flags & 3) == O_WRONLY){
fi->fh = muxfind(path);
/* race? */
return 0;
}
return -EACCES;
}
static int
mux_release(const char *path, struct fuse_file_info *fi){
int i, m;
m = fi->fh % Muxmax;
pthread_mutex_lock(&openlock);
if ((fi->flags & 3) == O_RDONLY){
i = fi->fh / Muxmax;
close(muxs[m].rh[i]);
close(muxs[m].wh[i]);
muxs[m].wh[i] = 0;
muxs[m].rh[i] = 0;
}
muxs[m].c--;
if(muxs[m].c==0) muxs[m].path[0]=0;
pthread_mutex_unlock(&openlock);
return 0;
}
static int mux_getattr(const char *path, struct stat *stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0770;
stbuf->st_nlink = 2;
} else {
stbuf->st_mode = S_IFREG | 0660;
stbuf->st_nlink = 1;
stbuf->st_size = 0;
}
return 0;
}
static int
mux_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi){
int i;
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
for(i=0;i<Muxmax;i++)
if(*muxs[i].path == '/')
filler(buf, muxs[i].path+1, NULL, 0);
return 0;
}
static int
mux_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
int m,i,r;
m = fi->fh % Muxmax;
i = fi->fh / Muxmax;
r = read(muxs[m].rh[i], buf, size);
if(r<0) return 0-errno;
return r;
}
static int
writeall(int fd, const char *buf, size_t size){
int r, o;
r = o = 0;
while(o < size){
r = write(fd, buf+o, size-o);
if(r<0) return 0-errno;
o+=r;
}
return o;
}
static int
mux_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
int i, r;
for(i=0;i<Readmax;i++){
if(muxs[fi->fh].wh[i] != 0){
r = writeall(muxs[fi->fh].wh[i], buf, size);
if(r<0) return r;
}
}
return size;
}
int
mux_truncate(const char *path, off_t off){
return 0;
}
static struct fuse_operations mux_oper = {
.getattr = mux_getattr,
.readdir = mux_readdir,
.open = mux_open,
.read = mux_read,
.write = mux_write,
.release = mux_release,
.truncate = mux_truncate,
};
void
mux_interrupt(int sig){
}
int
main(int argc, char **argv){
struct sigaction sa;
sa.sa_handler = mux_interrupt;
sa.sa_flags = 0;
sigfillset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, NULL);
return fuse_main(argc, argv, &mux_oper, NULL);
}