blob: 330d555b559df93c9fbf0ded957f52f9bdd67270 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
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>
Mike Lockwood4553b082010-08-16 14:14:44 -040025#include <sys/statfs.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070026#include <sys/uio.h>
27#include <dirent.h>
Jeff Brown7729d242012-05-25 15:35:28 -070028#include <limits.h>
Mike Lockwood1bedb732011-01-13 13:38:42 -050029#include <ctype.h>
Jeff Brown6249b902012-05-26 14:32:54 -070030#include <pthread.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080031#include <sys/time.h>
32#include <sys/resource.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070033#include <sys/inotify.h>
34
35#include <cutils/hashmap.h>
36#include <cutils/multiuser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070037
Brian Swetlandb14a2c62010-08-12 18:21:12 -070038#include <private/android_filesystem_config.h>
39
Brian Swetland03ee9472010-08-12 18:01:08 -070040#include "fuse.h"
41
42/* README
43 *
44 * What is this?
45 *
46 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
47 * directory permissions (all files are given fixed owner, group, and
48 * permissions at creation, owner, group, and permissions are not
49 * changeable, symlinks and hardlinks are not createable, etc.
50 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070051 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070052 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070053 * It must be run as root, but will drop to requested UID/GID as soon as it
54 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070055 *
56 * Things I believe to be true:
57 *
58 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
59 * CREAT) must bump that node's refcount
60 * - don't forget that FORGET can forget multiple references (req->nlookup)
61 * - if an op that returns a fuse_entry fails writing the reply to the
62 * kernel, you must rollback the refcount to reflect the reference the
63 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070064 *
65 * This daemon can also derive custom filesystem permissions based on directory
66 * structure when requested. These custom permissions support several features:
67 *
68 * - Apps can access their own files in /Android/data/com.example/ without
69 * requiring any additional GIDs.
70 * - Separate permissions for protecting directories like Pictures and Music.
71 * - Multi-user separation on the same physical device.
72 *
73 * The derived permissions look like this:
74 *
75 * rwxrwx--x root:sdcard_rw /
76 * rwxrwx--- root:sdcard_pics /Pictures
77 * rwxrwx--- root:sdcard_av /Music
78 *
79 * rwxrwx--x root:sdcard_rw /Android
80 * rwxrwx--x root:sdcard_rw /Android/data
81 * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example
82 * rwxrwx--x root:sdcard_rw /Android/obb/
83 * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example
84 *
85 * rwxrwx--- root:sdcard_all /Android/user
86 * rwxrwx--x root:sdcard_rw /Android/user/10
87 * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example
Brian Swetland03ee9472010-08-12 18:01:08 -070088 */
89
90#define FUSE_TRACE 0
91
92#if FUSE_TRACE
93#define TRACE(x...) fprintf(stderr,x)
94#else
95#define TRACE(x...) do {} while (0)
96#endif
97
98#define ERROR(x...) fprintf(stderr,x)
99
100#define FUSE_UNKNOWN_INO 0xffffffff
101
Jeff Brown84715842012-05-25 14:07:47 -0700102/* Maximum number of bytes to write in one request. */
103#define MAX_WRITE (256 * 1024)
104
105/* Maximum number of bytes to read in one request. */
106#define MAX_READ (128 * 1024)
107
108/* Largest possible request.
109 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
110 * the largest possible data payload. */
111#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
112
Jeff Brown6249b902012-05-26 14:32:54 -0700113/* Default number of threads. */
114#define DEFAULT_NUM_THREADS 2
115
116/* Pseudo-error constant used to indicate that no fuse status is needed
117 * or that a reply has already been written. */
118#define NO_STATUS 1
119
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700120/* Path to system-provided mapping of package name to appIds */
121static const char* const kPackagesListFile = "/data/system/packages.list";
122
123/* Supplementary groups to execute with */
124static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
125
126/* Permission mode for a specific node. Controls how file permissions
127 * are derived for children nodes. */
128typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700129 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700130 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700131 /* This node is one level above a normal root; used for legacy layouts
132 * which use the first level to represent user_id. */
133 PERM_LEGACY_PRE_ROOT,
134 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700135 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700136 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700137 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700138 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700139 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700140 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700141 PERM_ANDROID_OBB,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700142 /* This node is "/Android/user" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700143 PERM_ANDROID_USER,
144} perm_t;
145
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700146/* Permissions structure to derive */
147typedef enum {
148 DERIVE_NONE,
149 DERIVE_LEGACY,
150 DERIVE_UNIFIED,
151} derive_t;
152
Brian Swetland03ee9472010-08-12 18:01:08 -0700153struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700154 int fd;
155};
156
157struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700158 DIR *d;
159};
160
161struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700162 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700163 __u64 nid;
164 __u64 gen;
165
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700166 /* State derived based on current position in hierarchy. */
167 perm_t perm;
168 userid_t userid;
169 uid_t uid;
170 gid_t gid;
171 mode_t mode;
172
Paul Eastham11ccdb32010-10-14 11:04:26 -0700173 struct node *next; /* per-dir sibling list */
174 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700175 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700176
Jeff Brown6249b902012-05-26 14:32:54 -0700177 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700178 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800179 /* If non-null, this is the real name of the file in the underlying storage.
180 * This may differ from the field "name" only by case.
181 * strlen(actual_name) will always equal strlen(name), so it is safe to use
182 * namelen for both fields.
183 */
184 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700185
186 /* If non-null, an exact underlying path that should be grafted into this
187 * position. Used to support things like OBB. */
188 char* graft_path;
189 size_t graft_pathlen;
Brian Swetland03ee9472010-08-12 18:01:08 -0700190};
191
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700192static int str_hash(void *key) {
193 return hashmapHash(key, strlen(key));
194}
195
196static bool str_equals(void *keyA, void *keyB) {
197 return strcmp(keyA, keyB) == 0;
198}
199
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700200static int int_hash(void *key) {
201 return (int) key;
202}
203
204static bool int_equals(void *keyA, void *keyB) {
205 return keyA == keyB;
206}
207
Jeff Brown7729d242012-05-25 15:35:28 -0700208/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700209struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700210 pthread_mutex_t lock;
211
Brian Swetland03ee9472010-08-12 18:01:08 -0700212 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700213 int fd;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700214 derive_t derive;
215 bool split_perms;
Brian Swetland03ee9472010-08-12 18:01:08 -0700216 struct node root;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700217 char obbpath[PATH_MAX];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700218
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700219 Hashmap* package_to_appid;
220 Hashmap* appid_with_rw;
Brian Swetland03ee9472010-08-12 18:01:08 -0700221};
222
Jeff Brown7729d242012-05-25 15:35:28 -0700223/* Private data used by a single fuse handler. */
224struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700225 struct fuse* fuse;
226 int token;
227
Jeff Brown7729d242012-05-25 15:35:28 -0700228 /* To save memory, we never use the contents of the request buffer and the read
229 * buffer at the same time. This allows us to share the underlying storage. */
230 union {
231 __u8 request_buffer[MAX_REQUEST_SIZE];
232 __u8 read_buffer[MAX_READ];
233 };
234};
Brian Swetland03ee9472010-08-12 18:01:08 -0700235
Jeff Brown6249b902012-05-26 14:32:54 -0700236static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700237{
Jeff Brown6249b902012-05-26 14:32:54 -0700238 return (void *) (uintptr_t) nid;
239}
Brian Swetland03ee9472010-08-12 18:01:08 -0700240
Jeff Brown6249b902012-05-26 14:32:54 -0700241static inline __u64 ptr_to_id(void *ptr)
242{
243 return (__u64) (uintptr_t) ptr;
244}
Brian Swetland03ee9472010-08-12 18:01:08 -0700245
Jeff Brown6249b902012-05-26 14:32:54 -0700246static void acquire_node_locked(struct node* node)
247{
248 node->refcount++;
249 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
250}
251
252static void remove_node_from_parent_locked(struct node* node);
253
254static void release_node_locked(struct node* node)
255{
256 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
257 if (node->refcount > 0) {
258 node->refcount--;
259 if (!node->refcount) {
260 TRACE("DESTROY %p (%s)\n", node, node->name);
261 remove_node_from_parent_locked(node);
262
263 /* TODO: remove debugging - poison memory */
264 memset(node->name, 0xef, node->namelen);
265 free(node->name);
266 free(node->actual_name);
267 memset(node, 0xfc, sizeof(*node));
268 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800269 }
Jeff Brown6249b902012-05-26 14:32:54 -0700270 } else {
271 ERROR("Zero refcnt %p\n", node);
272 }
273}
274
275static void add_node_to_parent_locked(struct node *node, struct node *parent) {
276 node->parent = parent;
277 node->next = parent->child;
278 parent->child = node;
279 acquire_node_locked(parent);
280}
281
282static void remove_node_from_parent_locked(struct node* node)
283{
284 if (node->parent) {
285 if (node->parent->child == node) {
286 node->parent->child = node->parent->child->next;
287 } else {
288 struct node *node2;
289 node2 = node->parent->child;
290 while (node2->next != node)
291 node2 = node2->next;
292 node2->next = node->next;
293 }
294 release_node_locked(node->parent);
295 node->parent = NULL;
296 node->next = NULL;
297 }
298}
299
300/* Gets the absolute path to a node into the provided buffer.
301 *
302 * Populates 'buf' with the path and returns the length of the path on success,
303 * or returns -1 if the path is too long for the provided buffer.
304 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700305static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
306 const char* name;
307 size_t namelen;
308 if (node->graft_path) {
309 name = node->graft_path;
310 namelen = node->graft_pathlen;
311 } else if (node->actual_name) {
312 name = node->actual_name;
313 namelen = node->namelen;
314 } else {
315 name = node->name;
316 namelen = node->namelen;
317 }
318
Jeff Brown6249b902012-05-26 14:32:54 -0700319 if (bufsize < namelen + 1) {
320 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700321 }
322
Jeff Brown6249b902012-05-26 14:32:54 -0700323 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700324 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700325 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
326 if (pathlen < 0) {
327 return -1;
328 }
329 buf[pathlen++] = '/';
330 }
331
Jeff Brown6249b902012-05-26 14:32:54 -0700332 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
333 return pathlen + namelen;
334}
335
336/* Finds the absolute path of a file within a given directory.
337 * Performs a case-insensitive search for the file and sets the buffer to the path
338 * of the first matching file. If 'search' is zero or if no match is found, sets
339 * the buffer to the path that the file would have, assuming the name were case-sensitive.
340 *
341 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
342 * or returns NULL if the path is too long for the provided buffer.
343 */
344static char* find_file_within(const char* path, const char* name,
345 char* buf, size_t bufsize, int search)
346{
347 size_t pathlen = strlen(path);
348 size_t namelen = strlen(name);
349 size_t childlen = pathlen + namelen + 1;
350 char* actual;
351
352 if (bufsize <= childlen) {
353 return NULL;
354 }
355
356 memcpy(buf, path, pathlen);
357 buf[pathlen] = '/';
358 actual = buf + pathlen + 1;
359 memcpy(actual, name, namelen + 1);
360
361 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800362 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700363 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800364 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700365 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700366 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800367 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800368 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700369 if (!strcasecmp(entry->d_name, name)) {
370 /* we have a match - replace the name, don't need to copy the null again */
371 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800372 break;
373 }
374 }
375 closedir(dir);
376 }
Jeff Brown6249b902012-05-26 14:32:54 -0700377 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800378}
379
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700380static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800381{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700382 attr->ino = node->nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700383 attr->size = s->st_size;
384 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400385 attr->atime = s->st_atime;
386 attr->mtime = s->st_mtime;
387 attr->ctime = s->st_ctime;
388 attr->atimensec = s->st_atime_nsec;
389 attr->mtimensec = s->st_mtime_nsec;
390 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700391 attr->mode = s->st_mode;
392 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700393
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700394 attr->uid = node->uid;
395 attr->gid = node->gid;
396
397 /* Filter requested mode based on underlying file, and
398 * pass through file type. */
399 int owner_mode = s->st_mode & 0700;
400 int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
401 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
402}
403
404static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
405 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700406 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700407
408 /* By default, each node inherits from its parent */
409 node->perm = PERM_INHERIT;
410 node->userid = parent->userid;
411 node->uid = parent->uid;
412 node->gid = parent->gid;
413 node->mode = parent->mode;
414
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700415 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700416 return;
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700417 }
418
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700419 /* Derive custom permissions based on parent and current node */
420 switch (parent->perm) {
421 case PERM_INHERIT:
422 /* Already inherited above */
423 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700424 case PERM_LEGACY_PRE_ROOT:
425 /* Legacy internal layout places users at top level */
426 node->perm = PERM_ROOT;
427 node->userid = strtoul(node->name, NULL, 10);
428 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700429 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700430 /* Assume masked off by default. */
431 node->mode = 0770;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700432 if (!strcmp(node->name, "Android")) {
433 /* App-specific directories inside; let anyone traverse */
434 node->perm = PERM_ANDROID;
435 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700436 } else if (fuse->split_perms) {
437 if (!strcmp(node->name, "DCIM")
438 || !strcmp(node->name, "Pictures")) {
439 node->gid = AID_SDCARD_PICS;
440 } else if (!strcmp(node->name, "Alarms")
441 || !strcmp(node->name, "Movies")
442 || !strcmp(node->name, "Music")
443 || !strcmp(node->name, "Notifications")
444 || !strcmp(node->name, "Podcasts")
445 || !strcmp(node->name, "Ringtones")) {
446 node->gid = AID_SDCARD_AV;
447 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700448 }
449 break;
450 case PERM_ANDROID:
451 if (!strcmp(node->name, "data")) {
452 /* App-specific directories inside; let anyone traverse */
453 node->perm = PERM_ANDROID_DATA;
454 node->mode = 0771;
455 } else if (!strcmp(node->name, "obb")) {
456 /* App-specific directories inside; let anyone traverse */
457 node->perm = PERM_ANDROID_OBB;
458 node->mode = 0771;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700459 /* Single OBB directory is always shared */
460 node->graft_path = fuse->obbpath;
461 node->graft_pathlen = strlen(fuse->obbpath);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700462 } else if (!strcmp(node->name, "user")) {
463 /* User directories must only be accessible to system, protected
464 * by sdcard_all. Zygote will bind mount the appropriate user-
465 * specific path. */
466 node->perm = PERM_ANDROID_USER;
467 node->gid = AID_SDCARD_ALL;
468 node->mode = 0770;
469 }
470 break;
471 case PERM_ANDROID_DATA:
472 case PERM_ANDROID_OBB:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700473 appid = (appid_t) hashmapGet(fuse->package_to_appid, node->name);
474 if (appid != 0) {
475 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700476 }
477 node->mode = 0770;
478 break;
479 case PERM_ANDROID_USER:
480 /* Root of a secondary user */
481 node->perm = PERM_ROOT;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700482 node->userid = strtoul(node->name, NULL, 10);
483 node->gid = AID_SDCARD_R;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700484 node->mode = 0771;
485 break;
486 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700487}
488
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700489/* Return if the calling UID holds sdcard_rw. */
490static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) {
491 appid_t appid = multiuser_get_app_id(hdr->uid);
492 return hashmapContainsKey(fuse->appid_with_rw, (void*) appid);
493}
494
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700495/* Kernel has already enforced everything we returned through
496 * derive_permissions_locked(), so this is used to lock down access
497 * even further, such as enforcing that apps hold sdcard_rw. */
498static bool check_caller_access_to_name(struct fuse* fuse,
499 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700500 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700501 /* Always block security-sensitive files at root */
502 if (parent_node && parent_node->perm == PERM_ROOT) {
503 if (!strcmp(name, "autorun.inf")
504 || !strcmp(name, ".android_secure")
505 || !strcmp(name, "android_secure")) {
506 return false;
507 }
508 }
509
510 /* No additional permissions enforcement */
511 if (fuse->derive == DERIVE_NONE) {
512 return true;
513 }
514
515 /* Root or shell always have access */
516 if (hdr->uid == 0 || hdr->uid == AID_SHELL) {
517 return true;
518 }
519
520 /* If asking to write, verify that caller either owns the
521 * parent or holds sdcard_rw. */
522 if (mode & W_OK) {
523 if (parent_node && hdr->uid == parent_node->uid) {
524 return true;
525 }
526
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700527 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700528 }
529
530 /* No extra permissions to enforce */
531 return true;
532}
533
534static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700535 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
536 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700537}
538
Jeff Brown6249b902012-05-26 14:32:54 -0700539struct node *create_node_locked(struct fuse* fuse,
540 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700541{
542 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700543 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700544
Paul Eastham11ccdb32010-10-14 11:04:26 -0700545 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700546 if (!node) {
547 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700548 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700549 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700550 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700551 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700552 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700553 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700554 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700555 if (strcmp(name, actual_name)) {
556 node->actual_name = malloc(namelen + 1);
557 if (!node->actual_name) {
558 free(node->name);
559 free(node);
560 return NULL;
561 }
562 memcpy(node->actual_name, actual_name, namelen + 1);
563 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700564 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700565 node->nid = ptr_to_id(node);
566 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700567
568 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700569 acquire_node_locked(node);
570 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700571 return node;
572}
573
Jeff Brown6249b902012-05-26 14:32:54 -0700574static int rename_node_locked(struct node *node, const char *name,
575 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700576{
Jeff Brown6249b902012-05-26 14:32:54 -0700577 size_t namelen = strlen(name);
578 int need_actual_name = strcmp(name, actual_name);
579
580 /* make the storage bigger without actually changing the name
581 * in case an error occurs part way */
582 if (namelen > node->namelen) {
583 char* new_name = realloc(node->name, namelen + 1);
584 if (!new_name) {
585 return -ENOMEM;
586 }
587 node->name = new_name;
588 if (need_actual_name && node->actual_name) {
589 char* new_actual_name = realloc(node->actual_name, namelen + 1);
590 if (!new_actual_name) {
591 return -ENOMEM;
592 }
593 node->actual_name = new_actual_name;
594 }
595 }
596
597 /* update the name, taking care to allocate storage before overwriting the old name */
598 if (need_actual_name) {
599 if (!node->actual_name) {
600 node->actual_name = malloc(namelen + 1);
601 if (!node->actual_name) {
602 return -ENOMEM;
603 }
604 }
605 memcpy(node->actual_name, actual_name, namelen + 1);
606 } else {
607 free(node->actual_name);
608 node->actual_name = NULL;
609 }
610 memcpy(node->name, name, namelen + 1);
611 node->namelen = namelen;
612 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700613}
614
Jeff Brown6249b902012-05-26 14:32:54 -0700615static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700616{
Jeff Brown6249b902012-05-26 14:32:54 -0700617 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700618 return &fuse->root;
619 } else {
620 return id_to_ptr(nid);
621 }
622}
623
Jeff Brown6249b902012-05-26 14:32:54 -0700624static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
625 char* buf, size_t bufsize)
626{
627 struct node* node = lookup_node_by_id_locked(fuse, nid);
628 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
629 node = NULL;
630 }
631 return node;
632}
633
634static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700635{
636 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700637 /* use exact string comparison, nodes that differ by case
638 * must be considered distinct even if they refer to the same
639 * underlying file as otherwise operations such as "mv x x"
640 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700641 if (!strcmp(name, node->name)) {
642 return node;
643 }
644 }
645 return 0;
646}
647
Jeff Brown6249b902012-05-26 14:32:54 -0700648static struct node* acquire_or_create_child_locked(
649 struct fuse* fuse, struct node* parent,
650 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700651{
Jeff Brown6249b902012-05-26 14:32:54 -0700652 struct node* child = lookup_child_by_name_locked(parent, name);
653 if (child) {
654 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800655 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700656 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800657 }
Jeff Brown6249b902012-05-26 14:32:54 -0700658 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700659}
660
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700661static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
662 gid_t fs_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700663 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700664
Jeff Brown6249b902012-05-26 14:32:54 -0700665 fuse->fd = fd;
666 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700667 fuse->derive = derive;
668 fuse->split_perms = split_perms;
Brian Swetland03ee9472010-08-12 18:01:08 -0700669
Jeff Brown6249b902012-05-26 14:32:54 -0700670 memset(&fuse->root, 0, sizeof(fuse->root));
671 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
672 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700673 fuse->root.namelen = strlen(source_path);
674 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700675 fuse->root.userid = 0;
676 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700677
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700678 /* Set up root node for various modes of operation */
679 switch (derive) {
680 case DERIVE_NONE:
681 /* Traditional behavior that treats entire device as being accessible
682 * to sdcard_rw, and no permissions are derived. */
683 fuse->root.perm = PERM_ROOT;
684 fuse->root.mode = 0775;
685 fuse->root.gid = AID_SDCARD_RW;
686 break;
687 case DERIVE_LEGACY:
688 /* Legacy behavior used to support internal multiuser layout which
689 * places user_id at the top directory level, with the actual roots
690 * just below that. Shared OBB path is also at top level. */
691 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
692 fuse->root.mode = 0771;
693 fuse->root.gid = fs_gid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700694 fuse->package_to_appid = hashmapCreate(256, str_hash, str_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700695 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
696 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
697 break;
698 case DERIVE_UNIFIED:
699 /* Unified multiuser layout which places secondary user_id under
700 * /Android/user and shared OBB path under /Android/obb. */
701 fuse->root.perm = PERM_ROOT;
702 fuse->root.mode = 0771;
703 fuse->root.gid = fs_gid;
704 fuse->package_to_appid = hashmapCreate(256, str_hash, str_equals);
705 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
706 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
707 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700708 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700709}
710
Jeff Brown6249b902012-05-26 14:32:54 -0700711static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700712{
713 struct fuse_out_header hdr;
714 hdr.len = sizeof(hdr);
715 hdr.error = err;
716 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700717 write(fuse->fd, &hdr, sizeof(hdr));
718}
719
Jeff Brown6249b902012-05-26 14:32:54 -0700720static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700721{
722 struct fuse_out_header hdr;
723 struct iovec vec[2];
724 int res;
725
726 hdr.len = len + sizeof(hdr);
727 hdr.error = 0;
728 hdr.unique = unique;
729
730 vec[0].iov_base = &hdr;
731 vec[0].iov_len = sizeof(hdr);
732 vec[1].iov_base = data;
733 vec[1].iov_len = len;
734
735 res = writev(fuse->fd, vec, 2);
736 if (res < 0) {
737 ERROR("*** REPLY FAILED *** %d\n", errno);
738 }
739}
740
Jeff Brown6249b902012-05-26 14:32:54 -0700741static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
742 struct node* parent, const char* name, const char* actual_name,
743 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700744{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700745 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700746 struct fuse_entry_out out;
747 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700748
Jeff Brown6249b902012-05-26 14:32:54 -0700749 if (lstat(path, &s) < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700750 /* But wait! We'll automatically create a directory if its
751 * a valid package name under data or obb, since apps may not
752 * have enough permissions to create for themselves. */
753 if (errno == ENOENT && (parent->perm == PERM_ANDROID_DATA
754 || parent->perm == PERM_ANDROID_OBB)) {
755 TRACE("automatically creating %s\n", path);
756
757 pthread_mutex_lock(&fuse->lock);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700758 bool validPackage = hashmapContainsKey(fuse->package_to_appid, (char*) name);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700759 pthread_mutex_unlock(&fuse->lock);
760
761 if (!validPackage) {
762 return -ENOENT;
763 }
764 if (mkdir(path, 0775) == -1) {
765 /* We might have raced with ourselves and already created */
766 if (errno != EEXIST) {
767 ERROR("failed to mkdir(%s): %s\n", name, strerror(errno));
768 return -ENOENT;
769 }
770 }
771
772 /* It should exist this time around! */
773 if (lstat(path, &s) < 0) {
774 ERROR("failed to lstat(%s): %s\n", name, strerror(errno));
775 return -errno;
776 }
777 } else {
778 return -errno;
779 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700780 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700781
Jeff Brown6249b902012-05-26 14:32:54 -0700782 pthread_mutex_lock(&fuse->lock);
783 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
784 if (!node) {
785 pthread_mutex_unlock(&fuse->lock);
786 return -ENOMEM;
787 }
788 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700789 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700790 out.attr_valid = 10;
791 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700792 out.nodeid = node->nid;
793 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700794 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700795 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700796 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700797}
798
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700799static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700800 const char* path)
801{
802 struct fuse_attr_out out;
803 struct stat s;
804
805 if (lstat(path, &s) < 0) {
806 return -errno;
807 }
808 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700809 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700810 out.attr_valid = 10;
811 fuse_reply(fuse, unique, &out, sizeof(out));
812 return NO_STATUS;
813}
814
815static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700816 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700817{
Jeff Brown6249b902012-05-26 14:32:54 -0700818 struct node* parent_node;
819 char parent_path[PATH_MAX];
820 char child_path[PATH_MAX];
821 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700822
Jeff Brown6249b902012-05-26 14:32:54 -0700823 pthread_mutex_lock(&fuse->lock);
824 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
825 parent_path, sizeof(parent_path));
826 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
827 parent_node ? parent_node->name : "?");
828 pthread_mutex_unlock(&fuse->lock);
829
830 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
831 child_path, sizeof(child_path), 1))) {
832 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700833 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700834 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700835 return -EACCES;
836 }
837
Jeff Brown6249b902012-05-26 14:32:54 -0700838 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700839}
840
Jeff Brown6249b902012-05-26 14:32:54 -0700841static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700842 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
843{
Jeff Brown6249b902012-05-26 14:32:54 -0700844 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700845
Jeff Brown6249b902012-05-26 14:32:54 -0700846 pthread_mutex_lock(&fuse->lock);
847 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
848 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
849 hdr->nodeid, node ? node->name : "?");
850 if (node) {
851 __u64 n = req->nlookup;
852 while (n--) {
853 release_node_locked(node);
854 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700855 }
Jeff Brown6249b902012-05-26 14:32:54 -0700856 pthread_mutex_unlock(&fuse->lock);
857 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700858}
859
Jeff Brown6249b902012-05-26 14:32:54 -0700860static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
862{
Jeff Brown6249b902012-05-26 14:32:54 -0700863 struct node* node;
864 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700865
Jeff Brown6249b902012-05-26 14:32:54 -0700866 pthread_mutex_lock(&fuse->lock);
867 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
868 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
869 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
870 pthread_mutex_unlock(&fuse->lock);
871
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700872 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700873 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700874 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700875 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700876 return -EACCES;
877 }
878
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700879 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880}
881
Jeff Brown6249b902012-05-26 14:32:54 -0700882static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
884{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700885 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700886 struct node* node;
887 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700889
Jeff Brown6249b902012-05-26 14:32:54 -0700890 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700891 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700892 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
893 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
894 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
895 pthread_mutex_unlock(&fuse->lock);
896
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700897 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700898 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700899 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700900 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700901 return -EACCES;
902 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700903
Jeff Brown6249b902012-05-26 14:32:54 -0700904 /* XXX: incomplete implementation on purpose.
905 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700906
Jeff Brown6249b902012-05-26 14:32:54 -0700907 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
908 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700909 }
910
911 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
912 * are both set, then set it to the current time. Else, set it to the
913 * time specified in the request. Same goes for mtime. Use utimensat(2)
914 * as it allows ATIME and MTIME to be changed independently, and has
915 * nanosecond resolution which fuse also has.
916 */
917 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
918 times[0].tv_nsec = UTIME_OMIT;
919 times[1].tv_nsec = UTIME_OMIT;
920 if (req->valid & FATTR_ATIME) {
921 if (req->valid & FATTR_ATIME_NOW) {
922 times[0].tv_nsec = UTIME_NOW;
923 } else {
924 times[0].tv_sec = req->atime;
925 times[0].tv_nsec = req->atimensec;
926 }
927 }
928 if (req->valid & FATTR_MTIME) {
929 if (req->valid & FATTR_MTIME_NOW) {
930 times[1].tv_nsec = UTIME_NOW;
931 } else {
932 times[1].tv_sec = req->mtime;
933 times[1].tv_nsec = req->mtimensec;
934 }
935 }
Jeff Brown6249b902012-05-26 14:32:54 -0700936 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
937 handler->token, path, times[0].tv_sec, times[1].tv_sec);
938 if (utimensat(-1, path, times, 0) < 0) {
939 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700940 }
941 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700942 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700943}
944
Jeff Brown6249b902012-05-26 14:32:54 -0700945static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700946 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
947{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700948 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700949 struct node* parent_node;
950 char parent_path[PATH_MAX];
951 char child_path[PATH_MAX];
952 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700953
Jeff Brown6249b902012-05-26 14:32:54 -0700954 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700955 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700956 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
957 parent_path, sizeof(parent_path));
958 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
959 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
960 pthread_mutex_unlock(&fuse->lock);
961
962 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
963 child_path, sizeof(child_path), 1))) {
964 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700965 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700966 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700967 return -EACCES;
968 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700970 if (mknod(child_path, mode, req->rdev) < 0) {
971 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700972 }
Jeff Brown6249b902012-05-26 14:32:54 -0700973 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974}
975
Jeff Brown6249b902012-05-26 14:32:54 -0700976static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700977 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
978{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700979 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700980 struct node* parent_node;
981 char parent_path[PATH_MAX];
982 char child_path[PATH_MAX];
983 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700984
Jeff Brown6249b902012-05-26 14:32:54 -0700985 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700986 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700987 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
988 parent_path, sizeof(parent_path));
989 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
990 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
991 pthread_mutex_unlock(&fuse->lock);
992
993 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
994 child_path, sizeof(child_path), 1))) {
995 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700997 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700998 return -EACCES;
999 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001000 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001001 if (mkdir(child_path, mode) < 0) {
1002 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001003 }
Jeff Brown6249b902012-05-26 14:32:54 -07001004 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005}
1006
Jeff Brown6249b902012-05-26 14:32:54 -07001007static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001008 const struct fuse_in_header* hdr, const char* name)
1009{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001010 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001011 struct node* parent_node;
1012 char parent_path[PATH_MAX];
1013 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001014
Jeff Brown6249b902012-05-26 14:32:54 -07001015 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001016 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001017 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1018 parent_path, sizeof(parent_path));
1019 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1020 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1021 pthread_mutex_unlock(&fuse->lock);
1022
1023 if (!parent_node || !find_file_within(parent_path, name,
1024 child_path, sizeof(child_path), 1)) {
1025 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001027 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001028 return -EACCES;
1029 }
Jeff Brown6249b902012-05-26 14:32:54 -07001030 if (unlink(child_path) < 0) {
1031 return -errno;
1032 }
1033 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001034}
1035
Jeff Brown6249b902012-05-26 14:32:54 -07001036static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001037 const struct fuse_in_header* hdr, const char* name)
1038{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001039 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001040 struct node* parent_node;
1041 char parent_path[PATH_MAX];
1042 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001043
Jeff Brown6249b902012-05-26 14:32:54 -07001044 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001045 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001046 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1047 parent_path, sizeof(parent_path));
1048 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1049 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1050 pthread_mutex_unlock(&fuse->lock);
1051
1052 if (!parent_node || !find_file_within(parent_path, name,
1053 child_path, sizeof(child_path), 1)) {
1054 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001055 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001056 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001057 return -EACCES;
1058 }
Jeff Brown6249b902012-05-26 14:32:54 -07001059 if (rmdir(child_path) < 0) {
1060 return -errno;
1061 }
1062 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001063}
1064
Jeff Brown6249b902012-05-26 14:32:54 -07001065static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001066 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001067 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001069 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001070 struct node* old_parent_node;
1071 struct node* new_parent_node;
1072 struct node* child_node;
1073 char old_parent_path[PATH_MAX];
1074 char new_parent_path[PATH_MAX];
1075 char old_child_path[PATH_MAX];
1076 char new_child_path[PATH_MAX];
1077 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001078 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001079
Jeff Brown6249b902012-05-26 14:32:54 -07001080 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001081 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001082 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1083 old_parent_path, sizeof(old_parent_path));
1084 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1085 new_parent_path, sizeof(new_parent_path));
1086 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1087 old_name, new_name,
1088 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1089 req->newdir, new_parent_node ? new_parent_node->name : "?");
1090 if (!old_parent_node || !new_parent_node) {
1091 res = -ENOENT;
1092 goto lookup_error;
1093 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001094 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001095 res = -EACCES;
1096 goto lookup_error;
1097 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001098 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001099 res = -EACCES;
1100 goto lookup_error;
1101 }
Jeff Brown6249b902012-05-26 14:32:54 -07001102 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1103 if (!child_node || get_node_path_locked(child_node,
1104 old_child_path, sizeof(old_child_path)) < 0) {
1105 res = -ENOENT;
1106 goto lookup_error;
1107 }
1108 acquire_node_locked(child_node);
1109 pthread_mutex_unlock(&fuse->lock);
1110
1111 /* Special case for renaming a file where destination is same path
1112 * differing only by case. In this case we don't want to look for a case
1113 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1114 */
1115 int search = old_parent_node != new_parent_node
1116 || strcasecmp(old_name, new_name);
1117 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1118 new_child_path, sizeof(new_child_path), search))) {
1119 res = -ENOENT;
1120 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001121 }
1122
Jeff Brown6249b902012-05-26 14:32:54 -07001123 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1124 res = rename(old_child_path, new_child_path);
1125 if (res < 0) {
1126 res = -errno;
1127 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001128 }
1129
Jeff Brown6249b902012-05-26 14:32:54 -07001130 pthread_mutex_lock(&fuse->lock);
1131 res = rename_node_locked(child_node, new_name, new_actual_name);
1132 if (!res) {
1133 remove_node_from_parent_locked(child_node);
1134 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001135 }
Jeff Brown6249b902012-05-26 14:32:54 -07001136 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001137
Jeff Brown6249b902012-05-26 14:32:54 -07001138io_error:
1139 pthread_mutex_lock(&fuse->lock);
1140done:
1141 release_node_locked(child_node);
1142lookup_error:
1143 pthread_mutex_unlock(&fuse->lock);
1144 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001145}
1146
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001147static int open_flags_to_access_mode(int open_flags) {
1148 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1149 return R_OK;
1150 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1151 return W_OK;
1152 } else {
1153 /* Probably O_RDRW, but treat as default to be safe */
1154 return R_OK | W_OK;
1155 }
1156}
1157
Jeff Brown6249b902012-05-26 14:32:54 -07001158static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001159 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1160{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001161 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001162 struct node* node;
1163 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001164 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001165 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001166
Jeff Brown6249b902012-05-26 14:32:54 -07001167 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001168 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001169 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1170 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1171 req->flags, hdr->nodeid, node ? node->name : "?");
1172 pthread_mutex_unlock(&fuse->lock);
1173
1174 if (!node) {
1175 return -ENOENT;
1176 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001177 if (!check_caller_access_to_node(fuse, hdr, node,
1178 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001179 return -EACCES;
1180 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001181 h = malloc(sizeof(*h));
1182 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001183 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001184 }
Jeff Brown6249b902012-05-26 14:32:54 -07001185 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001186 h->fd = open(path, req->flags);
1187 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001188 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001189 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 }
1191 out.fh = ptr_to_id(h);
1192 out.open_flags = 0;
1193 out.padding = 0;
1194 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001195 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001196}
1197
Jeff Brown6249b902012-05-26 14:32:54 -07001198static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001199 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1200{
1201 struct handle *h = id_to_ptr(req->fh);
1202 __u64 unique = hdr->unique;
1203 __u32 size = req->size;
1204 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001205 int res;
1206
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001207 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1208 * overlaps the request buffer and will clobber data in the request. This
1209 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001210
1211 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1212 h, h->fd, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001213 if (size > sizeof(handler->read_buffer)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001214 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001215 }
1216 res = pread64(h->fd, handler->read_buffer, size, offset);
1217 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001218 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001219 }
1220 fuse_reply(fuse, unique, handler->read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001221 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001222}
1223
Jeff Brown6249b902012-05-26 14:32:54 -07001224static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001225 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1226 const void* buffer)
1227{
1228 struct fuse_write_out out;
1229 struct handle *h = id_to_ptr(req->fh);
1230 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001231
1232 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1233 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001234 res = pwrite64(h->fd, buffer, req->size, req->offset);
1235 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001236 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001237 }
1238 out.size = res;
1239 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001240 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001241}
1242
Jeff Brown6249b902012-05-26 14:32:54 -07001243static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001244 const struct fuse_in_header* hdr)
1245{
Jeff Brown6249b902012-05-26 14:32:54 -07001246 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001247 struct statfs stat;
1248 struct fuse_statfs_out out;
1249 int res;
1250
Jeff Brown6249b902012-05-26 14:32:54 -07001251 pthread_mutex_lock(&fuse->lock);
1252 TRACE("[%d] STATFS\n", handler->token);
1253 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1254 pthread_mutex_unlock(&fuse->lock);
1255 if (res < 0) {
1256 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001257 }
Jeff Brown6249b902012-05-26 14:32:54 -07001258 if (statfs(fuse->root.name, &stat) < 0) {
1259 return -errno;
1260 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001261 memset(&out, 0, sizeof(out));
1262 out.st.blocks = stat.f_blocks;
1263 out.st.bfree = stat.f_bfree;
1264 out.st.bavail = stat.f_bavail;
1265 out.st.files = stat.f_files;
1266 out.st.ffree = stat.f_ffree;
1267 out.st.bsize = stat.f_bsize;
1268 out.st.namelen = stat.f_namelen;
1269 out.st.frsize = stat.f_frsize;
1270 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001271 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001272}
1273
Jeff Brown6249b902012-05-26 14:32:54 -07001274static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001275 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1276{
1277 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001278
1279 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001280 close(h->fd);
1281 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001282 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001283}
1284
Jeff Brown6249b902012-05-26 14:32:54 -07001285static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001286 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1287{
1288 int is_data_sync = req->fsync_flags & 1;
1289 struct handle *h = id_to_ptr(req->fh);
1290 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001291
1292 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
1293 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001294 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
1295 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001296 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001297 }
Jeff Brown6249b902012-05-26 14:32:54 -07001298 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001299}
1300
Jeff Brown6249b902012-05-26 14:32:54 -07001301static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001302 const struct fuse_in_header* hdr)
1303{
Jeff Brown6249b902012-05-26 14:32:54 -07001304 TRACE("[%d] FLUSH\n", handler->token);
1305 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001306}
1307
Jeff Brown6249b902012-05-26 14:32:54 -07001308static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001309 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1310{
Jeff Brown6249b902012-05-26 14:32:54 -07001311 struct node* node;
1312 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001313 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001314 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001315
Jeff Brown6249b902012-05-26 14:32:54 -07001316 pthread_mutex_lock(&fuse->lock);
1317 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1318 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1319 hdr->nodeid, node ? node->name : "?");
1320 pthread_mutex_unlock(&fuse->lock);
1321
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001322 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001323 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001324 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001325 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001326 return -EACCES;
1327 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001328 h = malloc(sizeof(*h));
1329 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001330 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001331 }
Jeff Brown6249b902012-05-26 14:32:54 -07001332 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001333 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001334 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001335 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001336 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001337 }
1338 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001339 out.open_flags = 0;
1340 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001341 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001342 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001343}
1344
Jeff Brown6249b902012-05-26 14:32:54 -07001345static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1347{
1348 char buffer[8192];
1349 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1350 struct dirent *de;
1351 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001352
1353 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001354 if (req->offset == 0) {
1355 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001356 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001357 rewinddir(h->d);
1358 }
1359 de = readdir(h->d);
1360 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001361 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362 }
1363 fde->ino = FUSE_UNKNOWN_INO;
1364 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1365 fde->off = req->offset + 1;
1366 fde->type = de->d_type;
1367 fde->namelen = strlen(de->d_name);
1368 memcpy(fde->name, de->d_name, fde->namelen + 1);
1369 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001370 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1371 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001372}
1373
Jeff Brown6249b902012-05-26 14:32:54 -07001374static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1376{
1377 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001378
1379 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 closedir(h->d);
1381 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001382 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001383}
1384
Jeff Brown6249b902012-05-26 14:32:54 -07001385static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001386 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1387{
1388 struct fuse_init_out out;
1389
Jeff Brown6249b902012-05-26 14:32:54 -07001390 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1391 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001392 out.major = FUSE_KERNEL_VERSION;
1393 out.minor = FUSE_KERNEL_MINOR_VERSION;
1394 out.max_readahead = req->max_readahead;
1395 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1396 out.max_background = 32;
1397 out.congestion_threshold = 32;
1398 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001399 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001400 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001401}
1402
Jeff Brown6249b902012-05-26 14:32:54 -07001403static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001404 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1405{
Brian Swetland03ee9472010-08-12 18:01:08 -07001406 switch (hdr->opcode) {
1407 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001408 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001409 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001410 }
Jeff Brown84715842012-05-25 14:07:47 -07001411
Brian Swetland03ee9472010-08-12 18:01:08 -07001412 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001413 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001414 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001415 }
Jeff Brown84715842012-05-25 14:07:47 -07001416
Brian Swetland03ee9472010-08-12 18:01:08 -07001417 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001418 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001419 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001420 }
Jeff Brown84715842012-05-25 14:07:47 -07001421
Brian Swetland03ee9472010-08-12 18:01:08 -07001422 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001423 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001424 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001425 }
Jeff Brown84715842012-05-25 14:07:47 -07001426
Brian Swetland03ee9472010-08-12 18:01:08 -07001427// case FUSE_READLINK:
1428// case FUSE_SYMLINK:
1429 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001430 const struct fuse_mknod_in *req = data;
1431 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001432 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001433 }
Jeff Brown84715842012-05-25 14:07:47 -07001434
Brian Swetland03ee9472010-08-12 18:01:08 -07001435 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001436 const struct fuse_mkdir_in *req = data;
1437 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001438 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001439 }
Jeff Brown84715842012-05-25 14:07:47 -07001440
Brian Swetland03ee9472010-08-12 18:01:08 -07001441 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001442 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001443 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001444 }
Jeff Brown84715842012-05-25 14:07:47 -07001445
Brian Swetland03ee9472010-08-12 18:01:08 -07001446 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001447 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001448 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001449 }
Jeff Brown84715842012-05-25 14:07:47 -07001450
Brian Swetland03ee9472010-08-12 18:01:08 -07001451 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001452 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001453 const char *old_name = ((const char*) data) + sizeof(*req);
1454 const char *new_name = old_name + strlen(old_name) + 1;
1455 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001456 }
Jeff Brown84715842012-05-25 14:07:47 -07001457
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001458// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001459 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001460 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001461 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001462 }
Jeff Brown84715842012-05-25 14:07:47 -07001463
Brian Swetland03ee9472010-08-12 18:01:08 -07001464 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001465 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001466 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001467 }
Jeff Brown84715842012-05-25 14:07:47 -07001468
Brian Swetland03ee9472010-08-12 18:01:08 -07001469 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001470 const struct fuse_write_in *req = data;
1471 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001472 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001473 }
Jeff Brown84715842012-05-25 14:07:47 -07001474
Mike Lockwood4553b082010-08-16 14:14:44 -04001475 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001476 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001477 }
Jeff Brown84715842012-05-25 14:07:47 -07001478
Brian Swetland03ee9472010-08-12 18:01:08 -07001479 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001480 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001481 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001482 }
Jeff Brown84715842012-05-25 14:07:47 -07001483
Jeff Brown6fd921a2012-05-25 15:01:21 -07001484 case FUSE_FSYNC: {
1485 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001486 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001487 }
1488
Brian Swetland03ee9472010-08-12 18:01:08 -07001489// case FUSE_SETXATTR:
1490// case FUSE_GETXATTR:
1491// case FUSE_LISTXATTR:
1492// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001493 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001494 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001495 }
1496
Brian Swetland03ee9472010-08-12 18:01:08 -07001497 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001498 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001499 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001500 }
Jeff Brown84715842012-05-25 14:07:47 -07001501
Brian Swetland03ee9472010-08-12 18:01:08 -07001502 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001503 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001504 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001505 }
Jeff Brown84715842012-05-25 14:07:47 -07001506
Brian Swetland03ee9472010-08-12 18:01:08 -07001507 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001508 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001509 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001510 }
Jeff Brown84715842012-05-25 14:07:47 -07001511
Brian Swetland03ee9472010-08-12 18:01:08 -07001512// case FUSE_FSYNCDIR:
1513 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001514 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001515 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001516 }
Jeff Brown84715842012-05-25 14:07:47 -07001517
Brian Swetland03ee9472010-08-12 18:01:08 -07001518 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001519 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1520 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1521 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001522 }
Jeff Brown84715842012-05-25 14:07:47 -07001523 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001524}
1525
Jeff Brown6249b902012-05-26 14:32:54 -07001526static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001527{
Jeff Brown6249b902012-05-26 14:32:54 -07001528 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001529 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001530 ssize_t len = read(fuse->fd,
1531 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001532 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001533 if (errno != EINTR) {
1534 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1535 }
1536 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001537 }
Jeff Brown84715842012-05-25 14:07:47 -07001538
1539 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001540 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1541 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001542 }
1543
Jeff Brown7729d242012-05-25 15:35:28 -07001544 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001545 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001546 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1547 handler->token, (size_t)len, hdr->len);
1548 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001549 }
1550
Jeff Brown7729d242012-05-25 15:35:28 -07001551 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001552 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001553 __u64 unique = hdr->unique;
1554 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001555
1556 /* We do not access the request again after this point because the underlying
1557 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001558
1559 if (res != NO_STATUS) {
1560 if (res) {
1561 TRACE("[%d] ERROR %d\n", handler->token, res);
1562 }
1563 fuse_status(fuse, unique, res);
1564 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001565 }
1566}
1567
Jeff Brown6249b902012-05-26 14:32:54 -07001568static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001569{
Jeff Brown6249b902012-05-26 14:32:54 -07001570 struct fuse_handler* handler = data;
1571 handle_fuse_requests(handler);
1572 return NULL;
1573}
1574
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001575static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001576 Hashmap* map = context;
1577 hashmapRemove(map, key);
1578 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001579 return true;
1580}
1581
1582static bool remove_int_to_null(void *key, void *value, void *context) {
1583 Hashmap* map = context;
1584 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001585 return true;
1586}
1587
1588static int read_package_list(struct fuse *fuse) {
1589 pthread_mutex_lock(&fuse->lock);
1590
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001591 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1592 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001593
1594 FILE* file = fopen(kPackagesListFile, "r");
1595 if (!file) {
1596 ERROR("failed to open package list: %s\n", strerror(errno));
1597 pthread_mutex_unlock(&fuse->lock);
1598 return -1;
1599 }
1600
1601 char buf[512];
1602 while (fgets(buf, sizeof(buf), file) != NULL) {
1603 char package_name[512];
1604 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001605 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001606
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001607 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1608 char* package_name_dup = strdup(package_name);
1609 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) appid);
1610
1611 char* token = strtok(gids, ",");
1612 while (token != NULL) {
1613 if (strtoul(token, NULL, 10) == AID_SDCARD_RW) {
1614 hashmapPut(fuse->appid_with_rw, (void*) appid, (void*) 1);
1615 break;
1616 }
1617 token = strtok(NULL, ",");
1618 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001619 }
1620 }
1621
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001622 TRACE("read_package_list: found %d packages, %d with sdcard_rw\n",
1623 hashmapSize(fuse->package_to_appid),
1624 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001625 fclose(file);
1626 pthread_mutex_unlock(&fuse->lock);
1627 return 0;
1628}
1629
1630static void watch_package_list(struct fuse* fuse) {
1631 struct inotify_event *event;
1632 char event_buf[512];
1633
1634 int nfd = inotify_init();
1635 if (nfd < 0) {
1636 ERROR("inotify_init failed: %s\n", strerror(errno));
1637 return;
1638 }
1639
1640 bool active = false;
1641 while (1) {
1642 if (!active) {
1643 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1644 if (res == -1) {
1645 if (errno == ENOENT || errno == EACCES) {
1646 /* Framework may not have created yet, sleep and retry */
1647 ERROR("missing packages.list; retrying\n");
1648 sleep(3);
1649 continue;
1650 } else {
1651 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1652 return;
1653 }
1654 }
1655
1656 /* Watch above will tell us about any future changes, so
1657 * read the current state. */
1658 if (read_package_list(fuse) == -1) {
1659 ERROR("read_package_list failed: %s\n", strerror(errno));
1660 return;
1661 }
1662 active = true;
1663 }
1664
1665 int event_pos = 0;
1666 int res = read(nfd, event_buf, sizeof(event_buf));
1667 if (res < (int) sizeof(*event)) {
1668 if (errno == EINTR)
1669 continue;
1670 ERROR("failed to read inotify event: %s\n", strerror(errno));
1671 return;
1672 }
1673
1674 while (res >= (int) sizeof(*event)) {
1675 int event_size;
1676 event = (struct inotify_event *) (event_buf + event_pos);
1677
1678 TRACE("inotify event: %08x\n", event->mask);
1679 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1680 /* Previously watched file was deleted, probably due to move
1681 * that swapped in new data; re-arm the watch and read. */
1682 active = false;
1683 }
1684
1685 event_size = sizeof(*event) + event->len;
1686 res -= event_size;
1687 event_pos += event_size;
1688 }
1689 }
1690}
1691
Jeff Brown6249b902012-05-26 14:32:54 -07001692static int ignite_fuse(struct fuse* fuse, int num_threads)
1693{
1694 struct fuse_handler* handlers;
1695 int i;
1696
1697 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1698 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001699 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001700 return -ENOMEM;
1701 }
1702
1703 for (i = 0; i < num_threads; i++) {
1704 handlers[i].fuse = fuse;
1705 handlers[i].token = i;
1706 }
1707
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001708 /* When deriving permissions, this thread is used to process inotify events,
1709 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001710 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001711 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001712 pthread_t thread;
1713 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1714 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001715 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001716 goto quit;
1717 }
1718 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001719
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001720 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001721 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001722 } else {
1723 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001724 }
1725
1726 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001727
1728 /* don't bother killing all of the other threads or freeing anything,
1729 * should never get here anyhow */
1730quit:
1731 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001732}
1733
Mike Lockwood4f35e622011-01-12 14:39:44 -05001734static int usage()
1735{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001736 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1737 " -u: specify UID to run as\n"
1738 " -g: specify GID to run as\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001739 " -G: specify default GID for files (default sdcard_r, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001740 " -t: specify number of threads to use (default %d)\n"
1741 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001742 " -l: derive file permissions based on legacy internal layout\n"
1743 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001744 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001745 return 1;
1746}
1747
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001748static int run(const char* source_path, const char* dest_path, uid_t uid,
1749 gid_t gid, gid_t fs_gid, int num_threads, derive_t derive, bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001750 int fd;
1751 char opts[256];
1752 int res;
1753 struct fuse fuse;
1754
1755 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001756 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001757
1758 fd = open("/dev/fuse", O_RDWR);
1759 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001760 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001761 return -1;
1762 }
1763
1764 snprintf(opts, sizeof(opts),
1765 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1766 fd, uid, gid);
1767
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001768 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001769 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001770 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1771 goto error;
1772 }
1773
1774 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1775 if (res < 0) {
1776 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001777 goto error;
1778 }
1779
1780 res = setgid(gid);
1781 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001782 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001783 goto error;
1784 }
1785
1786 res = setuid(uid);
1787 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001788 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001789 goto error;
1790 }
1791
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001792 fuse_init(&fuse, fd, source_path, fs_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001793
1794 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001795 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001796
1797 /* we do not attempt to umount the file system here because we are no longer
1798 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001799
1800error:
1801 close(fd);
1802 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001803}
1804
Brian Swetland03ee9472010-08-12 18:01:08 -07001805int main(int argc, char **argv)
1806{
Brian Swetland03ee9472010-08-12 18:01:08 -07001807 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001808 const char *source_path = NULL;
1809 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001810 uid_t uid = 0;
1811 gid_t gid = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001812 gid_t fs_gid = AID_SDCARD_R;
Jeff Brown6249b902012-05-26 14:32:54 -07001813 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001814 derive_t derive = DERIVE_NONE;
1815 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001816 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001817 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001818
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001819 int opt;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001820 while ((opt = getopt(argc, argv, "u:g:G:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001821 switch (opt) {
1822 case 'u':
1823 uid = strtoul(optarg, NULL, 10);
1824 break;
1825 case 'g':
1826 gid = strtoul(optarg, NULL, 10);
1827 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001828 case 'G':
1829 fs_gid = strtoul(optarg, NULL, 10);
1830 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001831 case 't':
1832 num_threads = strtoul(optarg, NULL, 10);
1833 break;
1834 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001835 derive = DERIVE_UNIFIED;
1836 break;
1837 case 'l':
1838 derive = DERIVE_LEGACY;
1839 break;
1840 case 's':
1841 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001842 break;
1843 case '?':
1844 default:
1845 return usage();
1846 }
1847 }
1848
1849 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001850 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001851 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001852 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001853 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001854 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001855 } else if (!uid) {
1856 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001857 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001858 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001859 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001860 ERROR("too many arguments\n");
1861 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001862 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001863 }
1864
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001865 if (!source_path) {
1866 ERROR("no source path specified\n");
1867 return usage();
1868 }
1869 if (!dest_path) {
1870 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001871 return usage();
1872 }
Jeff Brown26567352012-05-25 13:27:43 -07001873 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001874 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001875 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001876 }
Jeff Brown6249b902012-05-26 14:32:54 -07001877 if (num_threads < 1) {
1878 ERROR("number of threads must be at least 1\n");
1879 return usage();
1880 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001881 if (split_perms && derive == DERIVE_NONE) {
1882 ERROR("cannot split permissions without deriving\n");
1883 return usage();
1884 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001885
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001886 rlim.rlim_cur = 8192;
1887 rlim.rlim_max = 8192;
1888 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1889 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1890 }
1891
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001892 res = run(source_path, dest_path, uid, gid, fs_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001893 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001894}