• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/mount.h>
24 #include <sys/stat.h>
25 #include <sys/statfs.h>
26 #include <sys/uio.h>
27 #include <dirent.h>
28 
29 #include <private/android_filesystem_config.h>
30 
31 #include "fuse.h"
32 
33 /* README
34  *
35  * What is this?
36  *
37  * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
38  * directory permissions (all files are given fixed owner, group, and
39  * permissions at creation, owner, group, and permissions are not
40  * changeable, symlinks and hardlinks are not createable, etc.
41  *
42  * usage:  sdcard <path> <uid> <gid>
43  *
44  * It must be run as root, but will change to uid/gid as soon as it
45  * mounts a filesystem on /mnt/sdcard.  It will refuse to run if uid or
46  * gid are zero.
47  *
48  *
49  * Things I believe to be true:
50  *
51  * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
52  * CREAT) must bump that node's refcount
53  * - don't forget that FORGET can forget multiple references (req->nlookup)
54  * - if an op that returns a fuse_entry fails writing the reply to the
55  * kernel, you must rollback the refcount to reflect the reference the
56  * kernel did not actually acquire
57  *
58  *
59  * Bugs:
60  *
61  * - need to move/rename node on RENAME
62  */
63 
64 #define FUSE_TRACE 0
65 
66 #if FUSE_TRACE
67 #define TRACE(x...) fprintf(stderr,x)
68 #else
69 #define TRACE(x...) do {} while (0)
70 #endif
71 
72 #define ERROR(x...) fprintf(stderr,x)
73 
74 #define FUSE_UNKNOWN_INO 0xffffffff
75 
76 #define MOUNT_POINT "/mnt/sdcard"
77 
78 struct handle {
79     struct node *node;
80     int fd;
81 };
82 
83 struct dirhandle {
84     struct node *node;
85     DIR *d;
86 };
87 
88 struct node {
89     __u64 nid;
90     __u64 gen;
91 
92     struct node *next;
93     struct node *child;
94     struct node *all;
95     struct node *parent;
96 
97     __u32 refcount;
98     __u32 namelen;
99 
100     char name[1];
101 };
102 
103 struct fuse {
104     __u64 next_generation;
105     __u64 next_node_id;
106 
107     int fd;
108 
109     struct node *all;
110 
111     struct node root;
112     char rootpath[1024];
113 };
114 
115 #define PATH_BUFFER_SIZE 1024
116 
node_get_path(struct node * node,char * buf,const char * name)117 char *node_get_path(struct node *node, char *buf, const char *name)
118 {
119     char *out = buf + PATH_BUFFER_SIZE - 1;
120     int len;
121     out[0] = 0;
122 
123     if (name) {
124         len = strlen(name);
125         goto start;
126     }
127 
128     while (node) {
129         name = node->name;
130         len = node->namelen;
131         node = node->parent;
132     start:
133         if ((len + 1) > (out - buf))
134             return 0;
135         out -= len;
136         memcpy(out, name, len);
137         out --;
138         out[0] = '/';
139     }
140 
141     return out;
142 }
143 
attr_from_stat(struct fuse_attr * attr,struct stat * s)144 void attr_from_stat(struct fuse_attr *attr, struct stat *s)
145 {
146     attr->ino = s->st_ino;
147     attr->size = s->st_size;
148     attr->blocks = s->st_blocks;
149     attr->atime = s->st_atime;
150     attr->mtime = s->st_mtime;
151     attr->ctime = s->st_ctime;
152     attr->atimensec = s->st_atime_nsec;
153     attr->mtimensec = s->st_mtime_nsec;
154     attr->ctimensec = s->st_ctime_nsec;
155     attr->mode = s->st_mode;
156     attr->nlink = s->st_nlink;
157 
158         /* force permissions to something reasonable:
159          * world readable
160          * writable by the sdcard group
161          */
162     if (attr->mode & 0100) {
163         attr->mode = (attr->mode & (~0777)) | 0775;
164     } else {
165         attr->mode = (attr->mode & (~0777)) | 0664;
166     }
167 
168         /* all files owned by root.sdcard */
169     attr->uid = 0;
170     attr->gid = AID_SDCARD_RW;
171 }
172 
node_get_attr(struct node * node,struct fuse_attr * attr)173 int node_get_attr(struct node *node, struct fuse_attr *attr)
174 {
175     int res;
176     struct stat s;
177     char *path, buffer[PATH_BUFFER_SIZE];
178 
179     path = node_get_path(node, buffer, 0);
180     res = lstat(path, &s);
181     if (res < 0) {
182         ERROR("lstat('%s') errno %d\n", path, errno);
183         return -1;
184     }
185 
186     attr_from_stat(attr, &s);
187     attr->ino = node->nid;
188 
189     return 0;
190 }
191 
node_create(struct node * parent,const char * name,__u64 nid,__u64 gen)192 struct node *node_create(struct node *parent, const char *name, __u64 nid, __u64 gen)
193 {
194     struct node *node;
195     int namelen = strlen(name);
196 
197     node = calloc(1, sizeof(struct node) + namelen);
198     if (node == 0) {
199         return 0;
200     }
201 
202     node->nid = nid;
203     node->gen = gen;
204     node->parent = parent;
205     node->next = parent->child;
206     parent->child = node;
207     memcpy(node->name, name, namelen + 1);
208     node->namelen = namelen;
209     parent->refcount++;
210 
211     return node;
212 }
213 
fuse_init(struct fuse * fuse,int fd,const char * path)214 void fuse_init(struct fuse *fuse, int fd, const char *path)
215 {
216     fuse->fd = fd;
217     fuse->next_node_id = 2;
218     fuse->next_generation = 0;
219 
220     fuse->all = &fuse->root;
221 
222     fuse->root.nid = FUSE_ROOT_ID; /* 1 */
223     fuse->root.next = 0;
224     fuse->root.child = 0;
225     fuse->root.parent = 0;
226 
227     fuse->root.all = 0;
228     fuse->root.refcount = 2;
229 
230     strcpy(fuse->root.name, path);
231     fuse->root.namelen = strlen(fuse->root.name);
232 }
233 
id_to_ptr(__u64 nid)234 static inline void *id_to_ptr(__u64 nid)
235 {
236     return (void *) nid;
237 }
238 
ptr_to_id(void * ptr)239 static inline __u64 ptr_to_id(void *ptr)
240 {
241     return (__u64) ptr;
242 }
243 
244 
lookup_by_inode(struct fuse * fuse,__u64 nid)245 struct node *lookup_by_inode(struct fuse *fuse, __u64 nid)
246 {
247     if (nid == FUSE_ROOT_ID) {
248         return &fuse->root;
249     } else {
250         return id_to_ptr(nid);
251     }
252 }
253 
lookup_child_by_name(struct node * node,const char * name)254 struct node *lookup_child_by_name(struct node *node, const char *name)
255 {
256     for (node = node->child; node; node = node->next) {
257         if (!strcmp(name, node->name)) {
258             return node;
259         }
260     }
261     return 0;
262 }
263 
lookup_child_by_inode(struct node * node,__u64 nid)264 struct node *lookup_child_by_inode(struct node *node, __u64 nid)
265 {
266     for (node = node->child; node; node = node->next) {
267         if (node->nid == nid) {
268             return node;
269         }
270     }
271     return 0;
272 }
273 
node_lookup(struct fuse * fuse,struct node * parent,const char * name,struct fuse_attr * attr)274 struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name,
275                          struct fuse_attr *attr)
276 {
277     int res;
278     struct stat s;
279     char *path, buffer[PATH_BUFFER_SIZE];
280     struct node *node;
281 
282     path = node_get_path(parent, buffer, name);
283         /* XXX error? */
284 
285     res = lstat(path, &s);
286     if (res < 0)
287         return 0;
288 
289     node = lookup_child_by_name(parent, name);
290     if (!node) {
291         node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++);
292         if (!node)
293             return 0;
294         node->nid = ptr_to_id(node);
295         node->all = fuse->all;
296         fuse->all = node;
297     }
298 
299     attr_from_stat(attr, &s);
300     attr->ino = node->nid;
301 
302     return node;
303 }
304 
node_release(struct node * node)305 void node_release(struct node *node)
306 {
307     TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
308     node->refcount--;
309     if (node->refcount == 0) {
310         if (node->parent->child == node) {
311             node->parent->child = node->parent->child->next;
312         } else {
313             struct node *node2;
314 
315             node2 = node->parent->child;
316             while (node2->next != node)
317                 node2 = node2->next;
318             node2->next = node->next;
319         }
320 
321         TRACE("DESTROY %p (%s)\n", node, node->name);
322 
323         node_release(node->parent);
324 
325         node->parent = 0;
326         node->next = 0;
327 
328             /* TODO: remove debugging - poison memory */
329         memset(node, 0xef, sizeof(*node) + strlen(node->name));
330 
331         free(node);
332     }
333 }
334 
fuse_status(struct fuse * fuse,__u64 unique,int err)335 void fuse_status(struct fuse *fuse, __u64 unique, int err)
336 {
337     struct fuse_out_header hdr;
338     hdr.len = sizeof(hdr);
339     hdr.error = err;
340     hdr.unique = unique;
341     if (err) {
342 //        ERROR("*** %d ***\n", err);
343     }
344     write(fuse->fd, &hdr, sizeof(hdr));
345 }
346 
fuse_reply(struct fuse * fuse,__u64 unique,void * data,int len)347 void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
348 {
349     struct fuse_out_header hdr;
350     struct iovec vec[2];
351     int res;
352 
353     hdr.len = len + sizeof(hdr);
354     hdr.error = 0;
355     hdr.unique = unique;
356 
357     vec[0].iov_base = &hdr;
358     vec[0].iov_len = sizeof(hdr);
359     vec[1].iov_base = data;
360     vec[1].iov_len = len;
361 
362     res = writev(fuse->fd, vec, 2);
363     if (res < 0) {
364         ERROR("*** REPLY FAILED *** %d\n", errno);
365     }
366 }
367 
lookup_entry(struct fuse * fuse,struct node * node,const char * name,__u64 unique)368 void lookup_entry(struct fuse *fuse, struct node *node,
369                   const char *name, __u64 unique)
370 {
371     struct fuse_entry_out out;
372 
373     memset(&out, 0, sizeof(out));
374 
375     node = node_lookup(fuse, node, name, &out.attr);
376     if (!node) {
377         fuse_status(fuse, unique, -ENOENT);
378         return;
379     }
380 
381     node->refcount++;
382 //    fprintf(stderr,"ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
383     out.nodeid = node->nid;
384     out.generation = node->gen;
385     out.entry_valid = 10;
386     out.attr_valid = 10;
387 
388     fuse_reply(fuse, unique, &out, sizeof(out));
389 }
390 
handle_fuse_request(struct fuse * fuse,struct fuse_in_header * hdr,void * data,unsigned len)391 void handle_fuse_request(struct fuse *fuse, struct fuse_in_header *hdr, void *data, unsigned len)
392 {
393     struct node *node;
394 
395     if ((len < sizeof(*hdr)) || (hdr->len != len)) {
396         ERROR("malformed header\n");
397         return;
398     }
399 
400     len -= hdr->len;
401 
402     if (hdr->nodeid) {
403         node = lookup_by_inode(fuse, hdr->nodeid);
404         if (!node) {
405             fuse_status(fuse, hdr->unique, -ENOENT);
406             return;
407         }
408     } else {
409         node = 0;
410     }
411 
412     switch (hdr->opcode) {
413     case FUSE_LOOKUP: { /* bytez[] -> entry_out */
414         TRACE("LOOKUP %llx %s\n", hdr->nodeid, (char*) data);
415         lookup_entry(fuse, node, (char*) data, hdr->unique);
416         return;
417     }
418     case FUSE_FORGET: {
419         struct fuse_forget_in *req = data;
420         TRACE("FORGET %llx (%s) #%lld\n", hdr->nodeid, node->name, req->nlookup);
421             /* no reply */
422         while (req->nlookup--)
423             node_release(node);
424         return;
425     }
426     case FUSE_GETATTR: { /* getattr_in -> attr_out */
427         struct fuse_getattr_in *req = data;
428         struct fuse_attr_out out;
429 
430         TRACE("GETATTR flags=%x fh=%llx\n",req->getattr_flags, req->fh);
431 
432         memset(&out, 0, sizeof(out));
433         node_get_attr(node, &out.attr);
434         out.attr_valid = 10;
435 
436         fuse_reply(fuse, hdr->unique, &out, sizeof(out));
437         return;
438     }
439     case FUSE_SETATTR: { /* setattr_in -> attr_out */
440         struct fuse_setattr_in *req = data;
441         struct fuse_attr_out out;
442         TRACE("SETATTR fh=%llx id=%llx valid=%x\n",
443               req->fh, hdr->nodeid, req->valid);
444 
445             /* XXX */
446 
447         memset(&out, 0, sizeof(out));
448         node_get_attr(node, &out.attr);
449         out.attr_valid = 10;
450         fuse_reply(fuse, hdr->unique, &out, sizeof(out));
451         return;
452     }
453 //    case FUSE_READLINK:
454 //    case FUSE_SYMLINK:
455     case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
456         struct fuse_mknod_in *req = data;
457         char *path, buffer[PATH_BUFFER_SIZE];
458         char *name = ((char*) data) + sizeof(*req);
459         int res;
460         TRACE("MKNOD %s @ %llx\n", name, hdr->nodeid);
461         path = node_get_path(node, buffer, name);
462 
463         req->mode = (req->mode & (~0777)) | 0664;
464         res = mknod(path, req->mode, req->rdev); /* XXX perm?*/
465         if (res < 0) {
466             fuse_status(fuse, hdr->unique, -errno);
467         } else {
468             lookup_entry(fuse, node, name, hdr->unique);
469         }
470         return;
471     }
472     case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
473         struct fuse_mkdir_in *req = data;
474         struct fuse_entry_out out;
475         char *path, buffer[PATH_BUFFER_SIZE];
476         char *name = ((char*) data) + sizeof(*req);
477         int res;
478         TRACE("MKDIR %s @ %llx 0%o\n", name, hdr->nodeid, req->mode);
479         path = node_get_path(node, buffer, name);
480 
481         req->mode = (req->mode & (~0777)) | 0775;
482         res = mkdir(path, req->mode);
483         if (res < 0) {
484             fuse_status(fuse, hdr->unique, -errno);
485         } else {
486             lookup_entry(fuse, node, name, hdr->unique);
487         }
488         return;
489     }
490     case FUSE_UNLINK: { /* bytez[] -> */
491         char *path, buffer[PATH_BUFFER_SIZE];
492         int res;
493         TRACE("UNLINK %s @ %llx\n", (char*) data, hdr->nodeid);
494         path = node_get_path(node, buffer, (char*) data);
495         res = unlink(path);
496         fuse_status(fuse, hdr->unique, res ? -errno : 0);
497         return;
498     }
499     case FUSE_RMDIR: { /* bytez[] -> */
500         char *path, buffer[PATH_BUFFER_SIZE];
501         int res;
502         TRACE("RMDIR %s @ %llx\n", (char*) data, hdr->nodeid);
503         path = node_get_path(node, buffer, (char*) data);
504         res = rmdir(path);
505         fuse_status(fuse, hdr->unique, res ? -errno : 0);
506         return;
507     }
508     case FUSE_RENAME: { /* rename_in, oldname, newname ->  */
509         struct fuse_rename_in *req = data;
510         char *oldname = ((char*) data) + sizeof(*req);
511         char *newname = oldname + strlen(oldname) + 1;
512         char *oldpath, oldbuffer[PATH_BUFFER_SIZE];
513         char *newpath, newbuffer[PATH_BUFFER_SIZE];
514         struct node *newnode;
515         int res;
516 
517         newnode = lookup_by_inode(fuse, req->newdir);
518         if (!newnode) {
519             fuse_status(fuse, hdr->unique, -ENOENT);
520             return;
521         }
522 
523         oldpath = node_get_path(node, oldbuffer, oldname);
524         newpath = node_get_path(newnode, newbuffer, newname);
525 
526         res = rename(oldpath, newpath);
527         fuse_status(fuse, hdr->unique, res ? -errno : 0);
528         return;
529     }
530 //    case FUSE_LINK:
531     case FUSE_OPEN: { /* open_in -> open_out */
532         struct fuse_open_in *req = data;
533         struct fuse_open_out out;
534         char *path, buffer[PATH_BUFFER_SIZE];
535         struct handle *h;
536 
537         h = malloc(sizeof(*h));
538         if (!h) {
539             fuse_status(fuse, hdr->unique, -ENOMEM);
540             return;
541         }
542 
543         path = node_get_path(node, buffer, 0);
544         TRACE("OPEN %llx '%s' 0%o fh=%p\n", hdr->nodeid, path, req->flags, h);
545         h->fd = open(path, req->flags);
546         if (h->fd < 0) {
547             ERROR("ERROR\n");
548             fuse_status(fuse, hdr->unique, errno);
549             free(h);
550             return;
551         }
552         out.fh = ptr_to_id(h);
553         out.open_flags = 0;
554         out.padding = 0;
555         fuse_reply(fuse, hdr->unique, &out, sizeof(out));
556         return;
557     }
558     case FUSE_READ: { /* read_in -> byte[] */
559         char buffer[128 * 1024];
560         struct fuse_read_in *req = data;
561         struct handle *h = id_to_ptr(req->fh);
562         int res;
563         TRACE("READ %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
564         if (req->size > sizeof(buffer)) {
565             fuse_status(fuse, hdr->unique, -EINVAL);
566             return;
567         }
568         res = pread(h->fd, buffer, req->size, req->offset);
569         if (res < 0) {
570             fuse_status(fuse, hdr->unique, errno);
571             return;
572         }
573         fuse_reply(fuse, hdr->unique, buffer, res);
574         return;
575     }
576     case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
577         struct fuse_write_in *req = data;
578         struct fuse_write_out out;
579         struct handle *h = id_to_ptr(req->fh);
580         int res;
581         TRACE("WRITE %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
582         res = pwrite(h->fd, ((char*) data) + sizeof(*req), req->size, req->offset);
583         if (res < 0) {
584             fuse_status(fuse, hdr->unique, errno);
585             return;
586         }
587         out.size = res;
588         fuse_reply(fuse, hdr->unique, &out, sizeof(out));
589         goto oops;
590     }
591     case FUSE_STATFS: { /* getattr_in -> attr_out */
592         struct statfs stat;
593         struct fuse_statfs_out out;
594         int res;
595 
596         TRACE("STATFS\n");
597 
598         if (statfs(fuse->root.name, &stat)) {
599             fuse_status(fuse, hdr->unique, -errno);
600             return;
601         }
602 
603         memset(&out, 0, sizeof(out));
604         out.st.blocks = stat.f_blocks;
605         out.st.bfree = stat.f_bfree;
606         out.st.bavail = stat.f_bavail;
607         out.st.files = stat.f_files;
608         out.st.ffree = stat.f_ffree;
609         out.st.bsize = stat.f_bsize;
610         out.st.namelen = stat.f_namelen;
611         out.st.frsize = stat.f_frsize;
612         fuse_reply(fuse, hdr->unique, &out, sizeof(out));
613         return;
614     }
615     case FUSE_RELEASE: { /* release_in -> */
616         struct fuse_release_in *req = data;
617         struct handle *h = id_to_ptr(req->fh);
618         TRACE("RELEASE %p(%d)\n", h, h->fd);
619         close(h->fd);
620         free(h);
621         fuse_status(fuse, hdr->unique, 0);
622         return;
623     }
624 //    case FUSE_FSYNC:
625 //    case FUSE_SETXATTR:
626 //    case FUSE_GETXATTR:
627 //    case FUSE_LISTXATTR:
628 //    case FUSE_REMOVEXATTR:
629     case FUSE_FLUSH:
630         fuse_status(fuse, hdr->unique, 0);
631         return;
632     case FUSE_OPENDIR: { /* open_in -> open_out */
633         struct fuse_open_in *req = data;
634         struct fuse_open_out out;
635         char *path, buffer[PATH_BUFFER_SIZE];
636         struct dirhandle *h;
637 
638         h = malloc(sizeof(*h));
639         if (!h) {
640             fuse_status(fuse, hdr->unique, -ENOMEM);
641             return;
642         }
643 
644         path = node_get_path(node, buffer, 0);
645         TRACE("OPENDIR %llx '%s'\n", hdr->nodeid, path);
646         h->d = opendir(path);
647         if (h->d == 0) {
648             ERROR("ERROR\n");
649             fuse_status(fuse, hdr->unique, -errno);
650             free(h);
651             return;
652         }
653         out.fh = ptr_to_id(h);
654         fuse_reply(fuse, hdr->unique, &out, sizeof(out));
655         return;
656     }
657     case FUSE_READDIR: {
658         struct fuse_read_in *req = data;
659         char buffer[8192];
660         struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
661         struct dirent *de;
662         struct dirhandle *h = id_to_ptr(req->fh);
663         TRACE("READDIR %p\n", h);
664         de = readdir(h->d);
665         if (!de) {
666             fuse_status(fuse, hdr->unique, 0);
667             return;
668         }
669         fde->ino = FUSE_UNKNOWN_INO;
670         fde->off = 0;
671         fde->type = de->d_type;
672         fde->namelen = strlen(de->d_name);
673         memcpy(fde->name, de->d_name, fde->namelen + 1);
674         fuse_reply(fuse, hdr->unique, fde,
675                    FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
676         return;
677     }
678     case FUSE_RELEASEDIR: { /* release_in -> */
679         struct fuse_release_in *req = data;
680         struct dirhandle *h = id_to_ptr(req->fh);
681         TRACE("RELEASEDIR %p\n",h);
682         closedir(h->d);
683         free(h);
684         fuse_status(fuse, hdr->unique, 0);
685         return;
686     }
687 //    case FUSE_FSYNCDIR:
688     case FUSE_INIT: { /* init_in -> init_out */
689         struct fuse_init_in *req = data;
690         struct fuse_init_out out;
691 
692         TRACE("INIT ver=%d.%d maxread=%d flags=%x\n",
693                 req->major, req->minor, req->max_readahead, req->flags);
694 
695         out.major = FUSE_KERNEL_VERSION;
696         out.minor = FUSE_KERNEL_MINOR_VERSION;
697         out.max_readahead = req->max_readahead;
698         out.flags = 0;
699         out.max_background = 32;
700         out.congestion_threshold = 32;
701         out.max_write = 256 * 1024;
702 
703         fuse_reply(fuse, hdr->unique, &out, sizeof(out));
704         return;
705     }
706     default: {
707         struct fuse_out_header h;
708         ERROR("NOTIMPL op=%d uniq=%llx nid=%llx\n",
709                 hdr->opcode, hdr->unique, hdr->nodeid);
710 
711         oops:
712         h.len = sizeof(h);
713         h.error = -ENOSYS;
714         h.unique = hdr->unique;
715         write(fuse->fd, &h, sizeof(h));
716         break;
717     }
718     }
719 }
720 
handle_fuse_requests(struct fuse * fuse)721 void handle_fuse_requests(struct fuse *fuse)
722 {
723     unsigned char req[256 * 1024 + 128];
724     int len;
725 
726     for (;;) {
727         len = read(fuse->fd, req, 8192);
728         if (len < 0) {
729             if (errno == EINTR)
730                 continue;
731             ERROR("handle_fuse_requests: errno=%d\n", errno);
732             return;
733         }
734         handle_fuse_request(fuse, (void*) req, (void*) (req + sizeof(struct fuse_in_header)), len);
735     }
736 }
737 
main(int argc,char ** argv)738 int main(int argc, char **argv)
739 {
740     struct fuse fuse;
741     char opts[256];
742     int fd;
743     int res;
744     unsigned uid;
745     unsigned gid;
746     const char *path;
747 
748     if (argc != 4) {
749         ERROR("usage: sdcard <path> <uid> <gid>\n");
750         return -1;
751     }
752 
753     uid = strtoul(argv[2], 0, 10);
754     gid = strtoul(argv[3], 0, 10);
755     if (!uid || !gid) {
756         ERROR("uid and gid must be nonzero\n");
757         return -1;
758     }
759 
760     path = argv[1];
761 
762         /* cleanup from previous instance, if necessary */
763     umount2(MOUNT_POINT, 2);
764 
765     fd = open("/dev/fuse", O_RDWR);
766     if (fd < 0){
767         ERROR("cannot open fuse device (%d)\n", errno);
768         return -1;
769     }
770 
771     sprintf(opts, "fd=%i,rootmode=40000,default_permissions,allow_other,"
772             "user_id=%d,group_id=%d", fd, uid, gid);
773 
774     res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts);
775     if (res < 0) {
776         ERROR("cannot mount fuse filesystem (%d)\n", errno);
777         return -1;
778     }
779 
780     if (setgid(gid) < 0) {
781         ERROR("cannot setgid!\n");
782         return -1;
783     }
784     if (setuid(uid) < 0) {
785         ERROR("cannot setuid!\n");
786         return -1;
787     }
788 
789     fuse_init(&fuse, fd, path);
790 
791     umask(0);
792     handle_fuse_requests(&fuse);
793 
794     return 0;
795 }
796