blob: 9a1dd17b7ba22a9b7a00792cd9dd3927841b6520 [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) {
Jeff Sharkey39ff0ae2013-08-30 13:58:13 -0700491 /* No additional permissions enforcement */
492 if (fuse->derive == DERIVE_NONE) {
493 return true;
494 }
495
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700496 appid_t appid = multiuser_get_app_id(hdr->uid);
497 return hashmapContainsKey(fuse->appid_with_rw, (void*) appid);
498}
499
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700500/* Kernel has already enforced everything we returned through
501 * derive_permissions_locked(), so this is used to lock down access
502 * even further, such as enforcing that apps hold sdcard_rw. */
503static bool check_caller_access_to_name(struct fuse* fuse,
504 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700505 const char* name, int mode, bool has_rw) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700506 /* Always block security-sensitive files at root */
507 if (parent_node && parent_node->perm == PERM_ROOT) {
508 if (!strcmp(name, "autorun.inf")
509 || !strcmp(name, ".android_secure")
510 || !strcmp(name, "android_secure")) {
511 return false;
512 }
513 }
514
515 /* No additional permissions enforcement */
516 if (fuse->derive == DERIVE_NONE) {
517 return true;
518 }
519
520 /* Root or shell always have access */
521 if (hdr->uid == 0 || hdr->uid == AID_SHELL) {
522 return true;
523 }
524
525 /* If asking to write, verify that caller either owns the
526 * parent or holds sdcard_rw. */
527 if (mode & W_OK) {
528 if (parent_node && hdr->uid == parent_node->uid) {
529 return true;
530 }
531
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700532 return has_rw;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700533 }
534
535 /* No extra permissions to enforce */
536 return true;
537}
538
539static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700540 const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) {
541 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700542}
543
Jeff Brown6249b902012-05-26 14:32:54 -0700544struct node *create_node_locked(struct fuse* fuse,
545 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700546{
547 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700548 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700549
Paul Eastham11ccdb32010-10-14 11:04:26 -0700550 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700551 if (!node) {
552 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700553 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700554 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700555 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700556 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700557 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700558 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700559 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700560 if (strcmp(name, actual_name)) {
561 node->actual_name = malloc(namelen + 1);
562 if (!node->actual_name) {
563 free(node->name);
564 free(node);
565 return NULL;
566 }
567 memcpy(node->actual_name, actual_name, namelen + 1);
568 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700569 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700570 node->nid = ptr_to_id(node);
571 node->gen = fuse->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700572
573 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700574 acquire_node_locked(node);
575 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700576 return node;
577}
578
Jeff Brown6249b902012-05-26 14:32:54 -0700579static int rename_node_locked(struct node *node, const char *name,
580 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700581{
Jeff Brown6249b902012-05-26 14:32:54 -0700582 size_t namelen = strlen(name);
583 int need_actual_name = strcmp(name, actual_name);
584
585 /* make the storage bigger without actually changing the name
586 * in case an error occurs part way */
587 if (namelen > node->namelen) {
588 char* new_name = realloc(node->name, namelen + 1);
589 if (!new_name) {
590 return -ENOMEM;
591 }
592 node->name = new_name;
593 if (need_actual_name && node->actual_name) {
594 char* new_actual_name = realloc(node->actual_name, namelen + 1);
595 if (!new_actual_name) {
596 return -ENOMEM;
597 }
598 node->actual_name = new_actual_name;
599 }
600 }
601
602 /* update the name, taking care to allocate storage before overwriting the old name */
603 if (need_actual_name) {
604 if (!node->actual_name) {
605 node->actual_name = malloc(namelen + 1);
606 if (!node->actual_name) {
607 return -ENOMEM;
608 }
609 }
610 memcpy(node->actual_name, actual_name, namelen + 1);
611 } else {
612 free(node->actual_name);
613 node->actual_name = NULL;
614 }
615 memcpy(node->name, name, namelen + 1);
616 node->namelen = namelen;
617 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700618}
619
Jeff Brown6249b902012-05-26 14:32:54 -0700620static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700621{
Jeff Brown6249b902012-05-26 14:32:54 -0700622 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700623 return &fuse->root;
624 } else {
625 return id_to_ptr(nid);
626 }
627}
628
Jeff Brown6249b902012-05-26 14:32:54 -0700629static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
630 char* buf, size_t bufsize)
631{
632 struct node* node = lookup_node_by_id_locked(fuse, nid);
633 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
634 node = NULL;
635 }
636 return node;
637}
638
639static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700640{
641 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700642 /* use exact string comparison, nodes that differ by case
643 * must be considered distinct even if they refer to the same
644 * underlying file as otherwise operations such as "mv x x"
645 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700646 if (!strcmp(name, node->name)) {
647 return node;
648 }
649 }
650 return 0;
651}
652
Jeff Brown6249b902012-05-26 14:32:54 -0700653static struct node* acquire_or_create_child_locked(
654 struct fuse* fuse, struct node* parent,
655 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700656{
Jeff Brown6249b902012-05-26 14:32:54 -0700657 struct node* child = lookup_child_by_name_locked(parent, name);
658 if (child) {
659 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800660 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700661 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800662 }
Jeff Brown6249b902012-05-26 14:32:54 -0700663 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700664}
665
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700666static void fuse_init(struct fuse *fuse, int fd, const char *source_path,
667 gid_t fs_gid, derive_t derive, bool split_perms) {
Jeff Brown6249b902012-05-26 14:32:54 -0700668 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700669
Jeff Brown6249b902012-05-26 14:32:54 -0700670 fuse->fd = fd;
671 fuse->next_generation = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700672 fuse->derive = derive;
673 fuse->split_perms = split_perms;
Brian Swetland03ee9472010-08-12 18:01:08 -0700674
Jeff Brown6249b902012-05-26 14:32:54 -0700675 memset(&fuse->root, 0, sizeof(fuse->root));
676 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
677 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700678 fuse->root.namelen = strlen(source_path);
679 fuse->root.name = strdup(source_path);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700680 fuse->root.userid = 0;
681 fuse->root.uid = AID_ROOT;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700682
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700683 /* Set up root node for various modes of operation */
684 switch (derive) {
685 case DERIVE_NONE:
686 /* Traditional behavior that treats entire device as being accessible
687 * to sdcard_rw, and no permissions are derived. */
688 fuse->root.perm = PERM_ROOT;
689 fuse->root.mode = 0775;
690 fuse->root.gid = AID_SDCARD_RW;
691 break;
692 case DERIVE_LEGACY:
693 /* Legacy behavior used to support internal multiuser layout which
694 * places user_id at the top directory level, with the actual roots
695 * just below that. Shared OBB path is also at top level. */
696 fuse->root.perm = PERM_LEGACY_PRE_ROOT;
697 fuse->root.mode = 0771;
698 fuse->root.gid = fs_gid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700699 fuse->package_to_appid = hashmapCreate(256, str_hash, str_equals);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700700 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
701 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path);
702 break;
703 case DERIVE_UNIFIED:
704 /* Unified multiuser layout which places secondary user_id under
705 * /Android/user and shared OBB path under /Android/obb. */
706 fuse->root.perm = PERM_ROOT;
707 fuse->root.mode = 0771;
708 fuse->root.gid = fs_gid;
709 fuse->package_to_appid = hashmapCreate(256, str_hash, str_equals);
710 fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals);
711 snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path);
712 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700713 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700714}
715
Jeff Brown6249b902012-05-26 14:32:54 -0700716static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700717{
718 struct fuse_out_header hdr;
719 hdr.len = sizeof(hdr);
720 hdr.error = err;
721 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700722 write(fuse->fd, &hdr, sizeof(hdr));
723}
724
Jeff Brown6249b902012-05-26 14:32:54 -0700725static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700726{
727 struct fuse_out_header hdr;
728 struct iovec vec[2];
729 int res;
730
731 hdr.len = len + sizeof(hdr);
732 hdr.error = 0;
733 hdr.unique = unique;
734
735 vec[0].iov_base = &hdr;
736 vec[0].iov_len = sizeof(hdr);
737 vec[1].iov_base = data;
738 vec[1].iov_len = len;
739
740 res = writev(fuse->fd, vec, 2);
741 if (res < 0) {
742 ERROR("*** REPLY FAILED *** %d\n", errno);
743 }
744}
745
Jeff Brown6249b902012-05-26 14:32:54 -0700746static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
747 struct node* parent, const char* name, const char* actual_name,
748 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700749{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700750 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700751 struct fuse_entry_out out;
752 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700753
Jeff Brown6249b902012-05-26 14:32:54 -0700754 if (lstat(path, &s) < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700755 /* But wait! We'll automatically create a directory if its
756 * a valid package name under data or obb, since apps may not
757 * have enough permissions to create for themselves. */
758 if (errno == ENOENT && (parent->perm == PERM_ANDROID_DATA
759 || parent->perm == PERM_ANDROID_OBB)) {
760 TRACE("automatically creating %s\n", path);
761
762 pthread_mutex_lock(&fuse->lock);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700763 bool validPackage = hashmapContainsKey(fuse->package_to_appid, (char*) name);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700764 pthread_mutex_unlock(&fuse->lock);
765
766 if (!validPackage) {
767 return -ENOENT;
768 }
769 if (mkdir(path, 0775) == -1) {
770 /* We might have raced with ourselves and already created */
771 if (errno != EEXIST) {
772 ERROR("failed to mkdir(%s): %s\n", name, strerror(errno));
773 return -ENOENT;
774 }
775 }
776
777 /* It should exist this time around! */
778 if (lstat(path, &s) < 0) {
779 ERROR("failed to lstat(%s): %s\n", name, strerror(errno));
780 return -errno;
781 }
782 } else {
783 return -errno;
784 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700785 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700786
Jeff Brown6249b902012-05-26 14:32:54 -0700787 pthread_mutex_lock(&fuse->lock);
788 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
789 if (!node) {
790 pthread_mutex_unlock(&fuse->lock);
791 return -ENOMEM;
792 }
793 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700794 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700795 out.attr_valid = 10;
796 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700797 out.nodeid = node->nid;
798 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700799 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700800 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700801 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700802}
803
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700804static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700805 const char* path)
806{
807 struct fuse_attr_out out;
808 struct stat s;
809
810 if (lstat(path, &s) < 0) {
811 return -errno;
812 }
813 memset(&out, 0, sizeof(out));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700814 attr_from_stat(&out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700815 out.attr_valid = 10;
816 fuse_reply(fuse, unique, &out, sizeof(out));
817 return NO_STATUS;
818}
819
820static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700821 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700822{
Jeff Brown6249b902012-05-26 14:32:54 -0700823 struct node* parent_node;
824 char parent_path[PATH_MAX];
825 char child_path[PATH_MAX];
826 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700827
Jeff Brown6249b902012-05-26 14:32:54 -0700828 pthread_mutex_lock(&fuse->lock);
829 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
830 parent_path, sizeof(parent_path));
831 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
832 parent_node ? parent_node->name : "?");
833 pthread_mutex_unlock(&fuse->lock);
834
835 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
836 child_path, sizeof(child_path), 1))) {
837 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700838 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700839 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700840 return -EACCES;
841 }
842
Jeff Brown6249b902012-05-26 14:32:54 -0700843 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700844}
845
Jeff Brown6249b902012-05-26 14:32:54 -0700846static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700847 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
848{
Jeff Brown6249b902012-05-26 14:32:54 -0700849 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700850
Jeff Brown6249b902012-05-26 14:32:54 -0700851 pthread_mutex_lock(&fuse->lock);
852 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
853 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
854 hdr->nodeid, node ? node->name : "?");
855 if (node) {
856 __u64 n = req->nlookup;
857 while (n--) {
858 release_node_locked(node);
859 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860 }
Jeff Brown6249b902012-05-26 14:32:54 -0700861 pthread_mutex_unlock(&fuse->lock);
862 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700863}
864
Jeff Brown6249b902012-05-26 14:32:54 -0700865static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700866 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
867{
Jeff Brown6249b902012-05-26 14:32:54 -0700868 struct node* node;
869 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700870
Jeff Brown6249b902012-05-26 14:32:54 -0700871 pthread_mutex_lock(&fuse->lock);
872 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
873 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
874 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
875 pthread_mutex_unlock(&fuse->lock);
876
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700877 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700878 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700880 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700881 return -EACCES;
882 }
883
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700884 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700885}
886
Jeff Brown6249b902012-05-26 14:32:54 -0700887static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700888 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
889{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700890 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700891 struct node* node;
892 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700893 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894
Jeff Brown6249b902012-05-26 14:32:54 -0700895 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700896 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700897 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
898 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
899 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
900 pthread_mutex_unlock(&fuse->lock);
901
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700902 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700903 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700904 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700905 if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700906 return -EACCES;
907 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700908
Jeff Brown6249b902012-05-26 14:32:54 -0700909 /* XXX: incomplete implementation on purpose.
910 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700911
Jeff Brown6249b902012-05-26 14:32:54 -0700912 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
913 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700914 }
915
916 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
917 * are both set, then set it to the current time. Else, set it to the
918 * time specified in the request. Same goes for mtime. Use utimensat(2)
919 * as it allows ATIME and MTIME to be changed independently, and has
920 * nanosecond resolution which fuse also has.
921 */
922 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
923 times[0].tv_nsec = UTIME_OMIT;
924 times[1].tv_nsec = UTIME_OMIT;
925 if (req->valid & FATTR_ATIME) {
926 if (req->valid & FATTR_ATIME_NOW) {
927 times[0].tv_nsec = UTIME_NOW;
928 } else {
929 times[0].tv_sec = req->atime;
930 times[0].tv_nsec = req->atimensec;
931 }
932 }
933 if (req->valid & FATTR_MTIME) {
934 if (req->valid & FATTR_MTIME_NOW) {
935 times[1].tv_nsec = UTIME_NOW;
936 } else {
937 times[1].tv_sec = req->mtime;
938 times[1].tv_nsec = req->mtimensec;
939 }
940 }
Jeff Brown6249b902012-05-26 14:32:54 -0700941 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
942 handler->token, path, times[0].tv_sec, times[1].tv_sec);
943 if (utimensat(-1, path, times, 0) < 0) {
944 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945 }
946 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700947 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948}
949
Jeff Brown6249b902012-05-26 14:32:54 -0700950static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
952{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700953 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700954 struct node* parent_node;
955 char parent_path[PATH_MAX];
956 char child_path[PATH_MAX];
957 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700958
Jeff Brown6249b902012-05-26 14:32:54 -0700959 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700960 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700961 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
962 parent_path, sizeof(parent_path));
963 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
964 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
965 pthread_mutex_unlock(&fuse->lock);
966
967 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
968 child_path, sizeof(child_path), 1))) {
969 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700970 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700971 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700972 return -EACCES;
973 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700974 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700975 if (mknod(child_path, mode, req->rdev) < 0) {
976 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700977 }
Jeff Brown6249b902012-05-26 14:32:54 -0700978 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700979}
980
Jeff Brown6249b902012-05-26 14:32:54 -0700981static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700982 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
983{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700984 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -0700985 struct node* parent_node;
986 char parent_path[PATH_MAX];
987 char child_path[PATH_MAX];
988 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700989
Jeff Brown6249b902012-05-26 14:32:54 -0700990 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700991 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -0700992 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
993 parent_path, sizeof(parent_path));
994 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
995 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
996 pthread_mutex_unlock(&fuse->lock);
997
998 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
999 child_path, sizeof(child_path), 1))) {
1000 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001001 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001002 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001003 return -EACCES;
1004 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001005 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -07001006 if (mkdir(child_path, mode) < 0) {
1007 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001008 }
Jeff Brown6249b902012-05-26 14:32:54 -07001009 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001010}
1011
Jeff Brown6249b902012-05-26 14:32:54 -07001012static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001013 const struct fuse_in_header* hdr, const char* name)
1014{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001015 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001016 struct node* parent_node;
1017 char parent_path[PATH_MAX];
1018 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001019
Jeff Brown6249b902012-05-26 14:32:54 -07001020 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001021 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001022 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1023 parent_path, sizeof(parent_path));
1024 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
1025 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1026 pthread_mutex_unlock(&fuse->lock);
1027
1028 if (!parent_node || !find_file_within(parent_path, name,
1029 child_path, sizeof(child_path), 1)) {
1030 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001031 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001032 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001033 return -EACCES;
1034 }
Jeff Brown6249b902012-05-26 14:32:54 -07001035 if (unlink(child_path) < 0) {
1036 return -errno;
1037 }
1038 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001039}
1040
Jeff Brown6249b902012-05-26 14:32:54 -07001041static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001042 const struct fuse_in_header* hdr, const char* name)
1043{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001044 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001045 struct node* parent_node;
1046 char parent_path[PATH_MAX];
1047 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001048
Jeff Brown6249b902012-05-26 14:32:54 -07001049 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001050 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001051 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1052 parent_path, sizeof(parent_path));
1053 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
1054 name, hdr->nodeid, parent_node ? parent_node->name : "?");
1055 pthread_mutex_unlock(&fuse->lock);
1056
1057 if (!parent_node || !find_file_within(parent_path, name,
1058 child_path, sizeof(child_path), 1)) {
1059 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001060 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001061 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001062 return -EACCES;
1063 }
Jeff Brown6249b902012-05-26 14:32:54 -07001064 if (rmdir(child_path) < 0) {
1065 return -errno;
1066 }
1067 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001068}
1069
Jeff Brown6249b902012-05-26 14:32:54 -07001070static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001071 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001072 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001073{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001074 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001075 struct node* old_parent_node;
1076 struct node* new_parent_node;
1077 struct node* child_node;
1078 char old_parent_path[PATH_MAX];
1079 char new_parent_path[PATH_MAX];
1080 char old_child_path[PATH_MAX];
1081 char new_child_path[PATH_MAX];
1082 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001083 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001084
Jeff Brown6249b902012-05-26 14:32:54 -07001085 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001086 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001087 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1088 old_parent_path, sizeof(old_parent_path));
1089 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1090 new_parent_path, sizeof(new_parent_path));
1091 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
1092 old_name, new_name,
1093 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1094 req->newdir, new_parent_node ? new_parent_node->name : "?");
1095 if (!old_parent_node || !new_parent_node) {
1096 res = -ENOENT;
1097 goto lookup_error;
1098 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001099 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001100 res = -EACCES;
1101 goto lookup_error;
1102 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001103 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001104 res = -EACCES;
1105 goto lookup_error;
1106 }
Jeff Brown6249b902012-05-26 14:32:54 -07001107 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1108 if (!child_node || get_node_path_locked(child_node,
1109 old_child_path, sizeof(old_child_path)) < 0) {
1110 res = -ENOENT;
1111 goto lookup_error;
1112 }
1113 acquire_node_locked(child_node);
1114 pthread_mutex_unlock(&fuse->lock);
1115
1116 /* Special case for renaming a file where destination is same path
1117 * differing only by case. In this case we don't want to look for a case
1118 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1119 */
1120 int search = old_parent_node != new_parent_node
1121 || strcasecmp(old_name, new_name);
1122 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1123 new_child_path, sizeof(new_child_path), search))) {
1124 res = -ENOENT;
1125 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001126 }
1127
Jeff Brown6249b902012-05-26 14:32:54 -07001128 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1129 res = rename(old_child_path, new_child_path);
1130 if (res < 0) {
1131 res = -errno;
1132 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001133 }
1134
Jeff Brown6249b902012-05-26 14:32:54 -07001135 pthread_mutex_lock(&fuse->lock);
1136 res = rename_node_locked(child_node, new_name, new_actual_name);
1137 if (!res) {
1138 remove_node_from_parent_locked(child_node);
1139 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001140 }
Jeff Brown6249b902012-05-26 14:32:54 -07001141 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001142
Jeff Brown6249b902012-05-26 14:32:54 -07001143io_error:
1144 pthread_mutex_lock(&fuse->lock);
1145done:
1146 release_node_locked(child_node);
1147lookup_error:
1148 pthread_mutex_unlock(&fuse->lock);
1149 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001150}
1151
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001152static int open_flags_to_access_mode(int open_flags) {
1153 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1154 return R_OK;
1155 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1156 return W_OK;
1157 } else {
1158 /* Probably O_RDRW, but treat as default to be safe */
1159 return R_OK | W_OK;
1160 }
1161}
1162
Jeff Brown6249b902012-05-26 14:32:54 -07001163static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001164 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1165{
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001166 bool has_rw;
Jeff Brown6249b902012-05-26 14:32:54 -07001167 struct node* node;
1168 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001169 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001170 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001171
Jeff Brown6249b902012-05-26 14:32:54 -07001172 pthread_mutex_lock(&fuse->lock);
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001173 has_rw = get_caller_has_rw_locked(fuse, hdr);
Jeff Brown6249b902012-05-26 14:32:54 -07001174 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1175 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
1176 req->flags, hdr->nodeid, node ? node->name : "?");
1177 pthread_mutex_unlock(&fuse->lock);
1178
1179 if (!node) {
1180 return -ENOENT;
1181 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001182 if (!check_caller_access_to_node(fuse, hdr, node,
1183 open_flags_to_access_mode(req->flags), has_rw)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001184 return -EACCES;
1185 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001186 h = malloc(sizeof(*h));
1187 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001188 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 }
Jeff Brown6249b902012-05-26 14:32:54 -07001190 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001191 h->fd = open(path, req->flags);
1192 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001193 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001194 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001195 }
1196 out.fh = ptr_to_id(h);
1197 out.open_flags = 0;
1198 out.padding = 0;
1199 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001200 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001201}
1202
Jeff Brown6249b902012-05-26 14:32:54 -07001203static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001204 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1205{
1206 struct handle *h = id_to_ptr(req->fh);
1207 __u64 unique = hdr->unique;
1208 __u32 size = req->size;
1209 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001210 int res;
1211
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1213 * overlaps the request buffer and will clobber data in the request. This
1214 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001215
1216 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
1217 h, h->fd, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001218 if (size > sizeof(handler->read_buffer)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001219 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001220 }
1221 res = pread64(h->fd, handler->read_buffer, size, offset);
1222 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001223 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224 }
1225 fuse_reply(fuse, unique, handler->read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001226 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001227}
1228
Jeff Brown6249b902012-05-26 14:32:54 -07001229static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001230 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1231 const void* buffer)
1232{
1233 struct fuse_write_out out;
1234 struct handle *h = id_to_ptr(req->fh);
1235 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001236
1237 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
1238 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001239 res = pwrite64(h->fd, buffer, req->size, req->offset);
1240 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001241 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001242 }
1243 out.size = res;
1244 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001245 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001246}
1247
Jeff Brown6249b902012-05-26 14:32:54 -07001248static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001249 const struct fuse_in_header* hdr)
1250{
Jeff Brown6249b902012-05-26 14:32:54 -07001251 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001252 struct statfs stat;
1253 struct fuse_statfs_out out;
1254 int res;
1255
Jeff Brown6249b902012-05-26 14:32:54 -07001256 pthread_mutex_lock(&fuse->lock);
1257 TRACE("[%d] STATFS\n", handler->token);
1258 res = get_node_path_locked(&fuse->root, path, sizeof(path));
1259 pthread_mutex_unlock(&fuse->lock);
1260 if (res < 0) {
1261 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001262 }
Jeff Brown6249b902012-05-26 14:32:54 -07001263 if (statfs(fuse->root.name, &stat) < 0) {
1264 return -errno;
1265 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001266 memset(&out, 0, sizeof(out));
1267 out.st.blocks = stat.f_blocks;
1268 out.st.bfree = stat.f_bfree;
1269 out.st.bavail = stat.f_bavail;
1270 out.st.files = stat.f_files;
1271 out.st.ffree = stat.f_ffree;
1272 out.st.bsize = stat.f_bsize;
1273 out.st.namelen = stat.f_namelen;
1274 out.st.frsize = stat.f_frsize;
1275 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001276 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001277}
1278
Jeff Brown6249b902012-05-26 14:32:54 -07001279static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001280 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1281{
1282 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001283
1284 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001285 close(h->fd);
1286 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001287 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001288}
1289
Jeff Brown6249b902012-05-26 14:32:54 -07001290static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001291 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1292{
1293 int is_data_sync = req->fsync_flags & 1;
1294 struct handle *h = id_to_ptr(req->fh);
1295 int res;
Jeff Brown6249b902012-05-26 14:32:54 -07001296
1297 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
1298 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001299 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
1300 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001301 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001302 }
Jeff Brown6249b902012-05-26 14:32:54 -07001303 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001304}
1305
Jeff Brown6249b902012-05-26 14:32:54 -07001306static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001307 const struct fuse_in_header* hdr)
1308{
Jeff Brown6249b902012-05-26 14:32:54 -07001309 TRACE("[%d] FLUSH\n", handler->token);
1310 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001311}
1312
Jeff Brown6249b902012-05-26 14:32:54 -07001313static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001314 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1315{
Jeff Brown6249b902012-05-26 14:32:54 -07001316 struct node* node;
1317 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001318 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001319 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001320
Jeff Brown6249b902012-05-26 14:32:54 -07001321 pthread_mutex_lock(&fuse->lock);
1322 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
1323 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
1324 hdr->nodeid, node ? node->name : "?");
1325 pthread_mutex_unlock(&fuse->lock);
1326
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001327 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001328 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001329 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001330 if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001331 return -EACCES;
1332 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001333 h = malloc(sizeof(*h));
1334 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001335 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001336 }
Jeff Brown6249b902012-05-26 14:32:54 -07001337 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001338 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001339 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001340 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001341 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001342 }
1343 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001344 out.open_flags = 0;
1345 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001347 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001348}
1349
Jeff Brown6249b902012-05-26 14:32:54 -07001350static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001351 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1352{
1353 char buffer[8192];
1354 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1355 struct dirent *de;
1356 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001357
1358 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001359 if (req->offset == 0) {
1360 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001361 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362 rewinddir(h->d);
1363 }
1364 de = readdir(h->d);
1365 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001366 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001367 }
1368 fde->ino = FUSE_UNKNOWN_INO;
1369 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1370 fde->off = req->offset + 1;
1371 fde->type = de->d_type;
1372 fde->namelen = strlen(de->d_name);
1373 memcpy(fde->name, de->d_name, fde->namelen + 1);
1374 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001375 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1376 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001377}
1378
Jeff Brown6249b902012-05-26 14:32:54 -07001379static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1381{
1382 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001383
1384 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001385 closedir(h->d);
1386 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001387 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001388}
1389
Jeff Brown6249b902012-05-26 14:32:54 -07001390static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001391 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1392{
1393 struct fuse_init_out out;
1394
Jeff Brown6249b902012-05-26 14:32:54 -07001395 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1396 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001397 out.major = FUSE_KERNEL_VERSION;
1398 out.minor = FUSE_KERNEL_MINOR_VERSION;
1399 out.max_readahead = req->max_readahead;
1400 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1401 out.max_background = 32;
1402 out.congestion_threshold = 32;
1403 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001404 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001405 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001406}
1407
Jeff Brown6249b902012-05-26 14:32:54 -07001408static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001409 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1410{
Brian Swetland03ee9472010-08-12 18:01:08 -07001411 switch (hdr->opcode) {
1412 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001413 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001414 return handle_lookup(fuse, handler, hdr, name);
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_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001418 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001419 return handle_forget(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_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001423 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001424 return handle_getattr(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_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001428 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001429 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001430 }
Jeff Brown84715842012-05-25 14:07:47 -07001431
Brian Swetland03ee9472010-08-12 18:01:08 -07001432// case FUSE_READLINK:
1433// case FUSE_SYMLINK:
1434 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001435 const struct fuse_mknod_in *req = data;
1436 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001437 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001438 }
Jeff Brown84715842012-05-25 14:07:47 -07001439
Brian Swetland03ee9472010-08-12 18:01:08 -07001440 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001441 const struct fuse_mkdir_in *req = data;
1442 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001443 return handle_mkdir(fuse, handler, hdr, req, 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_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001447 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001448 return handle_unlink(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_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001452 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001453 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001454 }
Jeff Brown84715842012-05-25 14:07:47 -07001455
Brian Swetland03ee9472010-08-12 18:01:08 -07001456 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001457 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001458 const char *old_name = ((const char*) data) + sizeof(*req);
1459 const char *new_name = old_name + strlen(old_name) + 1;
1460 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001461 }
Jeff Brown84715842012-05-25 14:07:47 -07001462
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001463// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001464 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001465 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001466 return handle_open(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_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001470 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001471 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001472 }
Jeff Brown84715842012-05-25 14:07:47 -07001473
Brian Swetland03ee9472010-08-12 18:01:08 -07001474 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001475 const struct fuse_write_in *req = data;
1476 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001477 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001478 }
Jeff Brown84715842012-05-25 14:07:47 -07001479
Mike Lockwood4553b082010-08-16 14:14:44 -04001480 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001481 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001482 }
Jeff Brown84715842012-05-25 14:07:47 -07001483
Brian Swetland03ee9472010-08-12 18:01:08 -07001484 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001485 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001486 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001487 }
Jeff Brown84715842012-05-25 14:07:47 -07001488
Jeff Brown6fd921a2012-05-25 15:01:21 -07001489 case FUSE_FSYNC: {
1490 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001491 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001492 }
1493
Brian Swetland03ee9472010-08-12 18:01:08 -07001494// case FUSE_SETXATTR:
1495// case FUSE_GETXATTR:
1496// case FUSE_LISTXATTR:
1497// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001498 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001499 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001500 }
1501
Brian Swetland03ee9472010-08-12 18:01:08 -07001502 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001503 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001504 return handle_opendir(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_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001508 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001509 return handle_readdir(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_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001513 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001514 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001515 }
Jeff Brown84715842012-05-25 14:07:47 -07001516
Brian Swetland03ee9472010-08-12 18:01:08 -07001517// case FUSE_FSYNCDIR:
1518 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001519 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001520 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001521 }
Jeff Brown84715842012-05-25 14:07:47 -07001522
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001524 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1525 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1526 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001527 }
Jeff Brown84715842012-05-25 14:07:47 -07001528 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001529}
1530
Jeff Brown6249b902012-05-26 14:32:54 -07001531static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001532{
Jeff Brown6249b902012-05-26 14:32:54 -07001533 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001534 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001535 ssize_t len = read(fuse->fd,
1536 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001537 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001538 if (errno != EINTR) {
1539 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1540 }
1541 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001542 }
Jeff Brown84715842012-05-25 14:07:47 -07001543
1544 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001545 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1546 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001547 }
1548
Jeff Brown7729d242012-05-25 15:35:28 -07001549 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001550 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001551 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1552 handler->token, (size_t)len, hdr->len);
1553 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001554 }
1555
Jeff Brown7729d242012-05-25 15:35:28 -07001556 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001557 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001558 __u64 unique = hdr->unique;
1559 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001560
1561 /* We do not access the request again after this point because the underlying
1562 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001563
1564 if (res != NO_STATUS) {
1565 if (res) {
1566 TRACE("[%d] ERROR %d\n", handler->token, res);
1567 }
1568 fuse_status(fuse, unique, res);
1569 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001570 }
1571}
1572
Jeff Brown6249b902012-05-26 14:32:54 -07001573static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001574{
Jeff Brown6249b902012-05-26 14:32:54 -07001575 struct fuse_handler* handler = data;
1576 handle_fuse_requests(handler);
1577 return NULL;
1578}
1579
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001580static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001581 Hashmap* map = context;
1582 hashmapRemove(map, key);
1583 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001584 return true;
1585}
1586
1587static bool remove_int_to_null(void *key, void *value, void *context) {
1588 Hashmap* map = context;
1589 hashmapRemove(map, key);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001590 return true;
1591}
1592
1593static int read_package_list(struct fuse *fuse) {
1594 pthread_mutex_lock(&fuse->lock);
1595
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001596 hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid);
1597 hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001598
1599 FILE* file = fopen(kPackagesListFile, "r");
1600 if (!file) {
1601 ERROR("failed to open package list: %s\n", strerror(errno));
1602 pthread_mutex_unlock(&fuse->lock);
1603 return -1;
1604 }
1605
1606 char buf[512];
1607 while (fgets(buf, sizeof(buf), file) != NULL) {
1608 char package_name[512];
1609 int appid;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001610 char gids[512];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001611
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001612 if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) {
1613 char* package_name_dup = strdup(package_name);
1614 hashmapPut(fuse->package_to_appid, package_name_dup, (void*) appid);
1615
1616 char* token = strtok(gids, ",");
1617 while (token != NULL) {
1618 if (strtoul(token, NULL, 10) == AID_SDCARD_RW) {
1619 hashmapPut(fuse->appid_with_rw, (void*) appid, (void*) 1);
1620 break;
1621 }
1622 token = strtok(NULL, ",");
1623 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001624 }
1625 }
1626
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001627 TRACE("read_package_list: found %d packages, %d with sdcard_rw\n",
1628 hashmapSize(fuse->package_to_appid),
1629 hashmapSize(fuse->appid_with_rw));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001630 fclose(file);
1631 pthread_mutex_unlock(&fuse->lock);
1632 return 0;
1633}
1634
1635static void watch_package_list(struct fuse* fuse) {
1636 struct inotify_event *event;
1637 char event_buf[512];
1638
1639 int nfd = inotify_init();
1640 if (nfd < 0) {
1641 ERROR("inotify_init failed: %s\n", strerror(errno));
1642 return;
1643 }
1644
1645 bool active = false;
1646 while (1) {
1647 if (!active) {
1648 int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF);
1649 if (res == -1) {
1650 if (errno == ENOENT || errno == EACCES) {
1651 /* Framework may not have created yet, sleep and retry */
1652 ERROR("missing packages.list; retrying\n");
1653 sleep(3);
1654 continue;
1655 } else {
1656 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1657 return;
1658 }
1659 }
1660
1661 /* Watch above will tell us about any future changes, so
1662 * read the current state. */
1663 if (read_package_list(fuse) == -1) {
1664 ERROR("read_package_list failed: %s\n", strerror(errno));
1665 return;
1666 }
1667 active = true;
1668 }
1669
1670 int event_pos = 0;
1671 int res = read(nfd, event_buf, sizeof(event_buf));
1672 if (res < (int) sizeof(*event)) {
1673 if (errno == EINTR)
1674 continue;
1675 ERROR("failed to read inotify event: %s\n", strerror(errno));
1676 return;
1677 }
1678
1679 while (res >= (int) sizeof(*event)) {
1680 int event_size;
1681 event = (struct inotify_event *) (event_buf + event_pos);
1682
1683 TRACE("inotify event: %08x\n", event->mask);
1684 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1685 /* Previously watched file was deleted, probably due to move
1686 * that swapped in new data; re-arm the watch and read. */
1687 active = false;
1688 }
1689
1690 event_size = sizeof(*event) + event->len;
1691 res -= event_size;
1692 event_pos += event_size;
1693 }
1694 }
1695}
1696
Jeff Brown6249b902012-05-26 14:32:54 -07001697static int ignite_fuse(struct fuse* fuse, int num_threads)
1698{
1699 struct fuse_handler* handlers;
1700 int i;
1701
1702 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1703 if (!handlers) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001704 ERROR("cannot allocate storage for threads\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001705 return -ENOMEM;
1706 }
1707
1708 for (i = 0; i < num_threads; i++) {
1709 handlers[i].fuse = fuse;
1710 handlers[i].token = i;
1711 }
1712
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001713 /* When deriving permissions, this thread is used to process inotify events,
1714 * otherwise it becomes one of the FUSE handlers. */
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001715 i = (fuse->derive == DERIVE_NONE) ? 1 : 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001716 for (; i < num_threads; i++) {
Jeff Brown6249b902012-05-26 14:32:54 -07001717 pthread_t thread;
1718 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1719 if (res) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001720 ERROR("failed to start thread #%d, error=%d\n", i, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001721 goto quit;
1722 }
1723 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001724
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001725 if (fuse->derive == DERIVE_NONE) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001726 handle_fuse_requests(&handlers[0]);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001727 } else {
1728 watch_package_list(fuse);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001729 }
1730
1731 ERROR("terminated prematurely\n");
Jeff Brown6249b902012-05-26 14:32:54 -07001732
1733 /* don't bother killing all of the other threads or freeing anything,
1734 * should never get here anyhow */
1735quit:
1736 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001737}
1738
Mike Lockwood4f35e622011-01-12 14:39:44 -05001739static int usage()
1740{
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001741 ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n"
1742 " -u: specify UID to run as\n"
1743 " -g: specify GID to run as\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001744 " -G: specify default GID for files (default sdcard_r, requires -d or -l)\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001745 " -t: specify number of threads to use (default %d)\n"
1746 " -d: derive file permissions based on path\n"
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001747 " -l: derive file permissions based on legacy internal layout\n"
1748 " -s: split derived permissions for pics, av\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001749 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001750 return 1;
1751}
1752
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001753static int run(const char* source_path, const char* dest_path, uid_t uid,
1754 gid_t gid, gid_t fs_gid, int num_threads, derive_t derive, bool split_perms) {
Jeff Brown26567352012-05-25 13:27:43 -07001755 int fd;
1756 char opts[256];
1757 int res;
1758 struct fuse fuse;
1759
1760 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001761 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001762
1763 fd = open("/dev/fuse", O_RDWR);
1764 if (fd < 0){
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001765 ERROR("cannot open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001766 return -1;
1767 }
1768
1769 snprintf(opts, sizeof(opts),
1770 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1771 fd, uid, gid);
1772
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001773 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001774 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001775 ERROR("cannot mount fuse filesystem: %s\n", strerror(errno));
1776 goto error;
1777 }
1778
1779 res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups);
1780 if (res < 0) {
1781 ERROR("cannot setgroups: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001782 goto error;
1783 }
1784
1785 res = setgid(gid);
1786 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001787 ERROR("cannot setgid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001788 goto error;
1789 }
1790
1791 res = setuid(uid);
1792 if (res < 0) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001793 ERROR("cannot setuid: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001794 goto error;
1795 }
1796
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001797 fuse_init(&fuse, fd, source_path, fs_gid, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001798
1799 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001800 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001801
1802 /* we do not attempt to umount the file system here because we are no longer
1803 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001804
1805error:
1806 close(fd);
1807 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001808}
1809
Brian Swetland03ee9472010-08-12 18:01:08 -07001810int main(int argc, char **argv)
1811{
Brian Swetland03ee9472010-08-12 18:01:08 -07001812 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001813 const char *source_path = NULL;
1814 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001815 uid_t uid = 0;
1816 gid_t gid = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001817 gid_t fs_gid = AID_SDCARD_R;
Jeff Brown6249b902012-05-26 14:32:54 -07001818 int num_threads = DEFAULT_NUM_THREADS;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001819 derive_t derive = DERIVE_NONE;
1820 bool split_perms = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001821 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001822 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001823
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001824 int opt;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001825 while ((opt = getopt(argc, argv, "u:g:G:t:dls")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001826 switch (opt) {
1827 case 'u':
1828 uid = strtoul(optarg, NULL, 10);
1829 break;
1830 case 'g':
1831 gid = strtoul(optarg, NULL, 10);
1832 break;
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001833 case 'G':
1834 fs_gid = strtoul(optarg, NULL, 10);
1835 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001836 case 't':
1837 num_threads = strtoul(optarg, NULL, 10);
1838 break;
1839 case 'd':
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001840 derive = DERIVE_UNIFIED;
1841 break;
1842 case 'l':
1843 derive = DERIVE_LEGACY;
1844 break;
1845 case 's':
1846 split_perms = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001847 break;
1848 case '?':
1849 default:
1850 return usage();
1851 }
1852 }
1853
1854 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001855 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001856 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001857 source_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001858 } else if (!dest_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001859 dest_path = arg;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001860 } else if (!uid) {
1861 uid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001862 } else if (!gid) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001863 gid = strtoul(arg, NULL, 10);
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001864 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001865 ERROR("too many arguments\n");
1866 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001867 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001868 }
1869
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001870 if (!source_path) {
1871 ERROR("no source path specified\n");
1872 return usage();
1873 }
1874 if (!dest_path) {
1875 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001876 return usage();
1877 }
Jeff Brown26567352012-05-25 13:27:43 -07001878 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001879 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001880 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001881 }
Jeff Brown6249b902012-05-26 14:32:54 -07001882 if (num_threads < 1) {
1883 ERROR("number of threads must be at least 1\n");
1884 return usage();
1885 }
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001886 if (split_perms && derive == DERIVE_NONE) {
1887 ERROR("cannot split permissions without deriving\n");
1888 return usage();
1889 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001890
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001891 rlim.rlim_cur = 8192;
1892 rlim.rlim_max = 8192;
1893 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1894 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1895 }
1896
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001897 res = run(source_path, dest_path, uid, gid, fs_gid, num_threads, derive, split_perms);
Jeff Brown26567352012-05-25 13:27:43 -07001898 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001899}