blob: f8625619500e57d40edf4f9569bbe53c7740e597 [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
Elliott Hughes300d5642014-07-08 13:53:26 -070017#define LOG_TAG "sdcard"
18
Elliott Hughes60281d52014-05-07 14:39:58 -070019#include <ctype.h>
20#include <dirent.h>
21#include <errno.h>
22#include <fcntl.h>
Marcus Oaklande43b99a2014-07-23 13:04:59 +010023#include <inttypes.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070024#include <limits.h>
25#include <linux/fuse.h>
26#include <pthread.h>
William Robertse5099802015-07-31 13:11:19 -070027#include <stdbool.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070031#include <sys/inotify.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070032#include <sys/mount.h>
Christopher Ferrisff649ea2014-09-13 13:53:08 -070033#include <sys/param.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070034#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070035#include <sys/stat.h>
Mike Lockwood4553b082010-08-16 14:14:44 -040036#include <sys/statfs.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080037#include <sys/time.h>
William Robertse5099802015-07-31 13:11:19 -070038#include <sys/types.h>
Elliott Hughes60281d52014-05-07 14:39:58 -070039#include <sys/uio.h>
40#include <unistd.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070041
Jeff Sharkey44d63422013-09-12 09:44:48 -070042#include <cutils/fs.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070043#include <cutils/hashmap.h>
Elliott Hughes300d5642014-07-08 13:53:26 -070044#include <cutils/log.h>
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070045#include <cutils/multiuser.h>
William Robertse5099802015-07-31 13:11:19 -070046#include <packagelistparser/packagelistparser.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070047
Brian Swetlandb14a2c62010-08-12 18:21:12 -070048#include <private/android_filesystem_config.h>
49
Brian Swetland03ee9472010-08-12 18:01:08 -070050/* README
51 *
52 * What is this?
Elliott Hughes60281d52014-05-07 14:39:58 -070053 *
Brian Swetland03ee9472010-08-12 18:01:08 -070054 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
Elliott Hughes60281d52014-05-07 14:39:58 -070055 * directory permissions (all files are given fixed owner, group, and
56 * permissions at creation, owner, group, and permissions are not
Brian Swetland03ee9472010-08-12 18:01:08 -070057 * changeable, symlinks and hardlinks are not createable, etc.
58 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070059 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070060 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070061 * It must be run as root, but will drop to requested UID/GID as soon as it
62 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070063 *
64 * Things I believe to be true:
65 *
66 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
67 * CREAT) must bump that node's refcount
68 * - don't forget that FORGET can forget multiple references (req->nlookup)
69 * - if an op that returns a fuse_entry fails writing the reply to the
70 * kernel, you must rollback the refcount to reflect the reference the
71 * kernel did not actually acquire
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -070072 *
73 * This daemon can also derive custom filesystem permissions based on directory
74 * structure when requested. These custom permissions support several features:
75 *
76 * - Apps can access their own files in /Android/data/com.example/ without
77 * requiring any additional GIDs.
78 * - Separate permissions for protecting directories like Pictures and Music.
79 * - Multi-user separation on the same physical device.
Brian Swetland03ee9472010-08-12 18:01:08 -070080 */
81
82#define FUSE_TRACE 0
83
84#if FUSE_TRACE
Elliott Hughes300d5642014-07-08 13:53:26 -070085#define TRACE(x...) ALOGD(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070086#else
87#define TRACE(x...) do {} while (0)
88#endif
89
Elliott Hughes300d5642014-07-08 13:53:26 -070090#define ERROR(x...) ALOGE(x)
Brian Swetland03ee9472010-08-12 18:01:08 -070091
92#define FUSE_UNKNOWN_INO 0xffffffff
93
Jeff Brown84715842012-05-25 14:07:47 -070094/* Maximum number of bytes to write in one request. */
95#define MAX_WRITE (256 * 1024)
96
97/* Maximum number of bytes to read in one request. */
98#define MAX_READ (128 * 1024)
99
100/* Largest possible request.
101 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
102 * the largest possible data payload. */
103#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
104
Jeff Brown6249b902012-05-26 14:32:54 -0700105/* Pseudo-error constant used to indicate that no fuse status is needed
106 * or that a reply has already been written. */
107#define NO_STATUS 1
108
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700109/* Supplementary groups to execute with */
110static const gid_t kGroups[1] = { AID_PACKAGE_INFO };
111
112/* Permission mode for a specific node. Controls how file permissions
113 * are derived for children nodes. */
114typedef enum {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700115 /* Nothing special; this node should just inherit from its parent. */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700116 PERM_INHERIT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700117 /* This node is one level above a normal root; used for legacy layouts
118 * which use the first level to represent user_id. */
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700119 PERM_PRE_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700120 /* This node is "/" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700121 PERM_ROOT,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700122 /* This node is "/Android" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700123 PERM_ANDROID,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700124 /* This node is "/Android/data" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700125 PERM_ANDROID_DATA,
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700126 /* This node is "/Android/obb" */
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700127 PERM_ANDROID_OBB,
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700128 /* This node is "/Android/media" */
129 PERM_ANDROID_MEDIA,
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700130} perm_t;
131
Brian Swetland03ee9472010-08-12 18:01:08 -0700132struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700133 int fd;
134};
135
136struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -0700137 DIR *d;
138};
139
140struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700141 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700142 __u64 nid;
143 __u64 gen;
Narayan Kamathfaa09352015-01-13 18:21:10 +0000144 /*
145 * The inode number for this FUSE node. Note that this isn't stable across
146 * multiple invocations of the FUSE daemon.
147 */
148 __u32 ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700149
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700150 /* State derived based on current position in hierarchy. */
151 perm_t perm;
152 userid_t userid;
153 uid_t uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700154 bool under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700155
Paul Eastham11ccdb32010-10-14 11:04:26 -0700156 struct node *next; /* per-dir sibling list */
157 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700158 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700159
Jeff Brown6249b902012-05-26 14:32:54 -0700160 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700161 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800162 /* If non-null, this is the real name of the file in the underlying storage.
163 * This may differ from the field "name" only by case.
164 * strlen(actual_name) will always equal strlen(name), so it is safe to use
165 * namelen for both fields.
166 */
167 char *actual_name;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700168
169 /* If non-null, an exact underlying path that should be grafted into this
170 * position. Used to support things like OBB. */
171 char* graft_path;
172 size_t graft_pathlen;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200173
174 bool deleted;
Brian Swetland03ee9472010-08-12 18:01:08 -0700175};
176
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700177static int str_hash(void *key) {
178 return hashmapHash(key, strlen(key));
179}
180
Jeff Sharkey44d63422013-09-12 09:44:48 -0700181/** Test if two string keys are equal ignoring case */
182static bool str_icase_equals(void *keyA, void *keyB) {
183 return strcasecmp(keyA, keyB) == 0;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700184}
185
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700186/* Global data for all FUSE mounts */
187struct fuse_global {
Jeff Brown6249b902012-05-26 14:32:54 -0700188 pthread_mutex_t lock;
189
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700190 uid_t uid;
191 gid_t gid;
192 bool multi_user;
193
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700194 char source_path[PATH_MAX];
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700195 char obb_path[PATH_MAX];
196
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700197 Hashmap* package_to_appid;
198
Brian Swetland03ee9472010-08-12 18:01:08 -0700199 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700200 struct node root;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700201
Narayan Kamathfaa09352015-01-13 18:21:10 +0000202 /* Used to allocate unique inode numbers for fuse nodes. We use
203 * a simple counter based scheme where inode numbers from deleted
204 * nodes aren't reused. Note that inode allocations are not stable
205 * across multiple invocation of the sdcard daemon, but that shouldn't
206 * be a huge problem in practice.
207 *
208 * Note that we restrict inodes to 32 bit unsigned integers to prevent
209 * truncation on 32 bit processes when unsigned long long stat.st_ino is
210 * assigned to an unsigned long ino_t type in an LP32 process.
211 *
212 * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
213 * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
214 * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
215 * in fs/fuse/inode.c).
216 *
217 * Accesses must be guarded by |lock|.
218 */
219 __u32 inode_ctr;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700220
221 struct fuse* fuse_default;
222 struct fuse* fuse_read;
223 struct fuse* fuse_write;
224};
225
226/* Single FUSE mount */
227struct fuse {
228 struct fuse_global* global;
229
230 char dest_path[PATH_MAX];
231
232 int fd;
233
234 gid_t gid;
235 mode_t mask;
Brian Swetland03ee9472010-08-12 18:01:08 -0700236};
237
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700238/* Private data used by a single FUSE handler */
Jeff Brown7729d242012-05-25 15:35:28 -0700239struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700240 struct fuse* fuse;
241 int token;
242
Jeff Brown7729d242012-05-25 15:35:28 -0700243 /* To save memory, we never use the contents of the request buffer and the read
244 * buffer at the same time. This allows us to share the underlying storage. */
245 union {
246 __u8 request_buffer[MAX_REQUEST_SIZE];
Elliott Hughese24e9a52015-07-28 16:36:47 -0700247 __u8 read_buffer[MAX_READ + PAGE_SIZE];
Jeff Brown7729d242012-05-25 15:35:28 -0700248 };
249};
Brian Swetland03ee9472010-08-12 18:01:08 -0700250
Jeff Brown6249b902012-05-26 14:32:54 -0700251static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700252{
Jeff Brown6249b902012-05-26 14:32:54 -0700253 return (void *) (uintptr_t) nid;
254}
Brian Swetland03ee9472010-08-12 18:01:08 -0700255
Jeff Brown6249b902012-05-26 14:32:54 -0700256static inline __u64 ptr_to_id(void *ptr)
257{
258 return (__u64) (uintptr_t) ptr;
259}
Brian Swetland03ee9472010-08-12 18:01:08 -0700260
Jeff Brown6249b902012-05-26 14:32:54 -0700261static void acquire_node_locked(struct node* node)
262{
263 node->refcount++;
264 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
265}
266
267static void remove_node_from_parent_locked(struct node* node);
268
269static void release_node_locked(struct node* node)
270{
271 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
272 if (node->refcount > 0) {
273 node->refcount--;
274 if (!node->refcount) {
275 TRACE("DESTROY %p (%s)\n", node, node->name);
276 remove_node_from_parent_locked(node);
277
278 /* TODO: remove debugging - poison memory */
279 memset(node->name, 0xef, node->namelen);
280 free(node->name);
281 free(node->actual_name);
282 memset(node, 0xfc, sizeof(*node));
283 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800284 }
Jeff Brown6249b902012-05-26 14:32:54 -0700285 } else {
286 ERROR("Zero refcnt %p\n", node);
287 }
288}
289
290static void add_node_to_parent_locked(struct node *node, struct node *parent) {
291 node->parent = parent;
292 node->next = parent->child;
293 parent->child = node;
294 acquire_node_locked(parent);
295}
296
297static void remove_node_from_parent_locked(struct node* node)
298{
299 if (node->parent) {
300 if (node->parent->child == node) {
301 node->parent->child = node->parent->child->next;
302 } else {
303 struct node *node2;
304 node2 = node->parent->child;
305 while (node2->next != node)
306 node2 = node2->next;
307 node2->next = node->next;
308 }
309 release_node_locked(node->parent);
310 node->parent = NULL;
311 node->next = NULL;
312 }
313}
314
315/* Gets the absolute path to a node into the provided buffer.
316 *
317 * Populates 'buf' with the path and returns the length of the path on success,
318 * or returns -1 if the path is too long for the provided buffer.
319 */
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700320static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) {
321 const char* name;
322 size_t namelen;
323 if (node->graft_path) {
324 name = node->graft_path;
325 namelen = node->graft_pathlen;
326 } else if (node->actual_name) {
327 name = node->actual_name;
328 namelen = node->namelen;
329 } else {
330 name = node->name;
331 namelen = node->namelen;
332 }
333
Jeff Brown6249b902012-05-26 14:32:54 -0700334 if (bufsize < namelen + 1) {
335 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700336 }
337
Jeff Brown6249b902012-05-26 14:32:54 -0700338 ssize_t pathlen = 0;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700339 if (node->parent && node->graft_path == NULL) {
Jeff Brown6249b902012-05-26 14:32:54 -0700340 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
341 if (pathlen < 0) {
342 return -1;
343 }
344 buf[pathlen++] = '/';
345 }
346
Jeff Brown6249b902012-05-26 14:32:54 -0700347 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
348 return pathlen + namelen;
349}
350
351/* Finds the absolute path of a file within a given directory.
352 * Performs a case-insensitive search for the file and sets the buffer to the path
353 * of the first matching file. If 'search' is zero or if no match is found, sets
354 * the buffer to the path that the file would have, assuming the name were case-sensitive.
355 *
356 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
357 * or returns NULL if the path is too long for the provided buffer.
358 */
359static char* find_file_within(const char* path, const char* name,
360 char* buf, size_t bufsize, int search)
361{
362 size_t pathlen = strlen(path);
363 size_t namelen = strlen(name);
364 size_t childlen = pathlen + namelen + 1;
365 char* actual;
366
367 if (bufsize <= childlen) {
368 return NULL;
369 }
370
371 memcpy(buf, path, pathlen);
372 buf[pathlen] = '/';
373 actual = buf + pathlen + 1;
374 memcpy(actual, name, namelen + 1);
375
376 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800377 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700378 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800379 if (!dir) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700380 ERROR("opendir %s failed: %s\n", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700381 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800382 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800383 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700384 if (!strcasecmp(entry->d_name, name)) {
385 /* we have a match - replace the name, don't need to copy the null again */
386 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800387 break;
388 }
389 }
390 closedir(dir);
391 }
Jeff Brown6249b902012-05-26 14:32:54 -0700392 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800393}
394
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700395static void attr_from_stat(struct fuse* fuse, struct fuse_attr *attr,
396 const struct stat *s, const struct node* node) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000397 attr->ino = node->ino;
Brian Swetland03ee9472010-08-12 18:01:08 -0700398 attr->size = s->st_size;
399 attr->blocks = s->st_blocks;
Elliott Hughesf1df8542014-11-10 11:03:38 -0800400 attr->atime = s->st_atim.tv_sec;
401 attr->mtime = s->st_mtim.tv_sec;
402 attr->ctime = s->st_ctim.tv_sec;
403 attr->atimensec = s->st_atim.tv_nsec;
404 attr->mtimensec = s->st_mtim.tv_nsec;
405 attr->ctimensec = s->st_ctim.tv_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700406 attr->mode = s->st_mode;
407 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700408
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700409 attr->uid = node->uid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700410
411 if (fuse->gid == AID_SDCARD_RW) {
412 /* As an optimization, certain trusted system components only run
413 * as owner but operate across all users. Since we're now handing
414 * out the sdcard_rw GID only to trusted apps, we're okay relaxing
415 * the user boundary enforcement for the default view. The UIDs
416 * assigned to app directories are still multiuser aware. */
417 attr->gid = AID_SDCARD_RW;
418 } else {
419 attr->gid = multiuser_get_uid(node->userid, fuse->gid);
420 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700421
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700422 int visible_mode = 0775 & ~fuse->mask;
423 if (node->perm == PERM_PRE_ROOT) {
424 /* Top of multi-user view should always be visible to ensure
425 * secondary users can traverse inside. */
426 visible_mode = 0711;
427 } else if (node->under_android) {
428 /* Block "other" access to Android directories, since only apps
429 * belonging to a specific user should be in there; we still
430 * leave +x open for the default view. */
431 if (fuse->gid == AID_SDCARD_RW) {
432 visible_mode = visible_mode & ~0006;
433 } else {
434 visible_mode = visible_mode & ~0007;
435 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700436 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700437 int owner_mode = s->st_mode & 0700;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700438 int filtered_mode = visible_mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6));
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700439 attr->mode = (attr->mode & S_IFMT) | filtered_mode;
440}
441
Jeff Sharkey44d63422013-09-12 09:44:48 -0700442static int touch(char* path, mode_t mode) {
443 int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode);
444 if (fd == -1) {
445 if (errno == EEXIST) {
446 return 0;
447 } else {
448 ERROR("Failed to open(%s): %s\n", path, strerror(errno));
449 return -1;
450 }
451 }
452 close(fd);
453 return 0;
454}
455
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700456static void derive_permissions_locked(struct fuse* fuse, struct node *parent,
457 struct node *node) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700458 appid_t appid;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700459
460 /* By default, each node inherits from its parent */
461 node->perm = PERM_INHERIT;
462 node->userid = parent->userid;
463 node->uid = parent->uid;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700464 node->under_android = parent->under_android;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700465
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700466 /* Derive custom permissions based on parent and current node */
467 switch (parent->perm) {
468 case PERM_INHERIT:
469 /* Already inherited above */
470 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700471 case PERM_PRE_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700472 /* Legacy internal layout places users at top level */
473 node->perm = PERM_ROOT;
474 node->userid = strtoul(node->name, NULL, 10);
475 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700476 case PERM_ROOT:
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700477 /* Assume masked off by default. */
Jeff Sharkey44d63422013-09-12 09:44:48 -0700478 if (!strcasecmp(node->name, "Android")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700479 /* App-specific directories inside; let anyone traverse */
480 node->perm = PERM_ANDROID;
Jeff Sharkey10a239b2015-07-28 10:49:41 -0700481 node->under_android = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700482 }
483 break;
484 case PERM_ANDROID:
Jeff Sharkey44d63422013-09-12 09:44:48 -0700485 if (!strcasecmp(node->name, "data")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700486 /* App-specific directories inside; let anyone traverse */
487 node->perm = PERM_ANDROID_DATA;
Jeff Sharkey44d63422013-09-12 09:44:48 -0700488 } else if (!strcasecmp(node->name, "obb")) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700489 /* App-specific directories inside; let anyone traverse */
490 node->perm = PERM_ANDROID_OBB;
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700491 /* Single OBB directory is always shared */
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700492 node->graft_path = fuse->global->obb_path;
493 node->graft_pathlen = strlen(fuse->global->obb_path);
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700494 } else if (!strcasecmp(node->name, "media")) {
495 /* App-specific directories inside; let anyone traverse */
496 node->perm = PERM_ANDROID_MEDIA;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700497 }
498 break;
499 case PERM_ANDROID_DATA:
500 case PERM_ANDROID_OBB:
Jeff Sharkey2e7d80d2014-05-30 15:38:31 -0700501 case PERM_ANDROID_MEDIA:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700502 appid = (appid_t) (uintptr_t) hashmapGet(fuse->global->package_to_appid, node->name);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700503 if (appid != 0) {
504 node->uid = multiuser_get_uid(parent->userid, appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700505 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700506 break;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700507 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -0700508}
509
Jeff Sharkey22b91262015-12-14 11:02:01 -0700510static void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent) {
511 struct node *node;
512 for (node = parent->child; node; node = node->next) {
513 derive_permissions_locked(fuse, parent, node);
514 if (node->child) {
515 derive_permissions_recursive_locked(fuse, node);
516 }
517 }
518}
519
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700520/* Kernel has already enforced everything we returned through
521 * derive_permissions_locked(), so this is used to lock down access
522 * even further, such as enforcing that apps hold sdcard_rw. */
523static bool check_caller_access_to_name(struct fuse* fuse,
524 const struct fuse_in_header *hdr, const struct node* parent_node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700525 const char* name, int mode) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700526 /* Always block security-sensitive files at root */
527 if (parent_node && parent_node->perm == PERM_ROOT) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700528 if (!strcasecmp(name, "autorun.inf")
529 || !strcasecmp(name, ".android_secure")
530 || !strcasecmp(name, "android_secure")) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700531 return false;
532 }
533 }
534
Jeff Sharkey44d63422013-09-12 09:44:48 -0700535 /* Root always has access; access for any other UIDs should always
536 * be controlled through packages.list. */
537 if (hdr->uid == 0) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700538 return true;
539 }
540
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700541 /* No extra permissions to enforce */
542 return true;
543}
544
545static bool check_caller_access_to_node(struct fuse* fuse,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700546 const struct fuse_in_header *hdr, const struct node* node, int mode) {
547 return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode);
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700548}
549
Jeff Brown6249b902012-05-26 14:32:54 -0700550struct node *create_node_locked(struct fuse* fuse,
551 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700552{
553 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700554 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700555
Narayan Kamathfaa09352015-01-13 18:21:10 +0000556 // Detect overflows in the inode counter. "4 billion nodes should be enough
557 // for everybody".
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700558 if (fuse->global->inode_ctr == 0) {
Narayan Kamathfaa09352015-01-13 18:21:10 +0000559 ERROR("No more inode numbers available");
560 return NULL;
561 }
562
Paul Eastham11ccdb32010-10-14 11:04:26 -0700563 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700564 if (!node) {
565 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700566 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700567 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700568 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700569 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700570 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700571 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700572 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700573 if (strcmp(name, actual_name)) {
574 node->actual_name = malloc(namelen + 1);
575 if (!node->actual_name) {
576 free(node->name);
577 free(node);
578 return NULL;
579 }
580 memcpy(node->actual_name, actual_name, namelen + 1);
581 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700582 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700583 node->nid = ptr_to_id(node);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700584 node->ino = fuse->global->inode_ctr++;
585 node->gen = fuse->global->next_generation++;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700586
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200587 node->deleted = false;
588
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700589 derive_permissions_locked(fuse, parent, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700590 acquire_node_locked(node);
591 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700592 return node;
593}
594
Jeff Brown6249b902012-05-26 14:32:54 -0700595static int rename_node_locked(struct node *node, const char *name,
596 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700597{
Jeff Brown6249b902012-05-26 14:32:54 -0700598 size_t namelen = strlen(name);
599 int need_actual_name = strcmp(name, actual_name);
600
601 /* make the storage bigger without actually changing the name
602 * in case an error occurs part way */
603 if (namelen > node->namelen) {
604 char* new_name = realloc(node->name, namelen + 1);
605 if (!new_name) {
606 return -ENOMEM;
607 }
608 node->name = new_name;
609 if (need_actual_name && node->actual_name) {
610 char* new_actual_name = realloc(node->actual_name, namelen + 1);
611 if (!new_actual_name) {
612 return -ENOMEM;
613 }
614 node->actual_name = new_actual_name;
615 }
616 }
617
618 /* update the name, taking care to allocate storage before overwriting the old name */
619 if (need_actual_name) {
620 if (!node->actual_name) {
621 node->actual_name = malloc(namelen + 1);
622 if (!node->actual_name) {
623 return -ENOMEM;
624 }
625 }
626 memcpy(node->actual_name, actual_name, namelen + 1);
627 } else {
628 free(node->actual_name);
629 node->actual_name = NULL;
630 }
631 memcpy(node->name, name, namelen + 1);
632 node->namelen = namelen;
633 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700634}
635
Jeff Brown6249b902012-05-26 14:32:54 -0700636static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700637{
Jeff Brown6249b902012-05-26 14:32:54 -0700638 if (nid == FUSE_ROOT_ID) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700639 return &fuse->global->root;
Brian Swetland03ee9472010-08-12 18:01:08 -0700640 } else {
641 return id_to_ptr(nid);
642 }
643}
644
Jeff Brown6249b902012-05-26 14:32:54 -0700645static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
646 char* buf, size_t bufsize)
647{
648 struct node* node = lookup_node_by_id_locked(fuse, nid);
649 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
650 node = NULL;
651 }
652 return node;
653}
654
655static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700656{
657 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700658 /* use exact string comparison, nodes that differ by case
659 * must be considered distinct even if they refer to the same
660 * underlying file as otherwise operations such as "mv x x"
661 * will not work because the source and target nodes are the same. */
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200662 if (!strcmp(name, node->name) && !node->deleted) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700663 return node;
664 }
665 }
666 return 0;
667}
668
Jeff Brown6249b902012-05-26 14:32:54 -0700669static struct node* acquire_or_create_child_locked(
670 struct fuse* fuse, struct node* parent,
671 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700672{
Jeff Brown6249b902012-05-26 14:32:54 -0700673 struct node* child = lookup_child_by_name_locked(parent, name);
674 if (child) {
675 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800676 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700677 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800678 }
Jeff Brown6249b902012-05-26 14:32:54 -0700679 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700680}
681
Jeff Brown6249b902012-05-26 14:32:54 -0700682static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700683{
684 struct fuse_out_header hdr;
685 hdr.len = sizeof(hdr);
686 hdr.error = err;
687 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700688 write(fuse->fd, &hdr, sizeof(hdr));
689}
690
Jeff Brown6249b902012-05-26 14:32:54 -0700691static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700692{
693 struct fuse_out_header hdr;
694 struct iovec vec[2];
695 int res;
696
697 hdr.len = len + sizeof(hdr);
698 hdr.error = 0;
699 hdr.unique = unique;
700
701 vec[0].iov_base = &hdr;
702 vec[0].iov_len = sizeof(hdr);
703 vec[1].iov_base = data;
704 vec[1].iov_len = len;
705
706 res = writev(fuse->fd, vec, 2);
707 if (res < 0) {
708 ERROR("*** REPLY FAILED *** %d\n", errno);
709 }
710}
711
Jeff Brown6249b902012-05-26 14:32:54 -0700712static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
713 struct node* parent, const char* name, const char* actual_name,
714 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700715{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700716 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700717 struct fuse_entry_out out;
718 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700719
Jeff Brown6249b902012-05-26 14:32:54 -0700720 if (lstat(path, &s) < 0) {
Jeff Sharkey44d63422013-09-12 09:44:48 -0700721 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700722 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700723
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700724 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700725 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
726 if (!node) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700727 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700728 return -ENOMEM;
729 }
730 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700731 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700732 out.attr_valid = 10;
733 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700734 out.nodeid = node->nid;
735 out.generation = node->gen;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700736 pthread_mutex_unlock(&fuse->global->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700737 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700738 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700739}
740
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700741static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node,
Jeff Brown6249b902012-05-26 14:32:54 -0700742 const char* path)
743{
744 struct fuse_attr_out out;
745 struct stat s;
746
747 if (lstat(path, &s) < 0) {
748 return -errno;
749 }
750 memset(&out, 0, sizeof(out));
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700751 attr_from_stat(fuse, &out.attr, &s, node);
Jeff Brown6249b902012-05-26 14:32:54 -0700752 out.attr_valid = 10;
753 fuse_reply(fuse, unique, &out, sizeof(out));
754 return NO_STATUS;
755}
756
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700757static void fuse_notify_delete(struct fuse* fuse, const __u64 parent,
758 const __u64 child, const char* name) {
759 struct fuse_out_header hdr;
760 struct fuse_notify_delete_out data;
761 struct iovec vec[3];
762 size_t namelen = strlen(name);
763 int res;
764
765 hdr.len = sizeof(hdr) + sizeof(data) + namelen + 1;
766 hdr.error = FUSE_NOTIFY_DELETE;
767 hdr.unique = 0;
768
769 data.parent = parent;
770 data.child = child;
771 data.namelen = namelen;
772 data.padding = 0;
773
774 vec[0].iov_base = &hdr;
775 vec[0].iov_len = sizeof(hdr);
776 vec[1].iov_base = &data;
777 vec[1].iov_len = sizeof(data);
778 vec[2].iov_base = (void*) name;
779 vec[2].iov_len = namelen + 1;
780
781 res = writev(fuse->fd, vec, 3);
782 /* Ignore ENOENT, since other views may not have seen the entry */
783 if (res < 0 && errno != ENOENT) {
784 ERROR("*** NOTIFY FAILED *** %d\n", errno);
785 }
786}
787
Jeff Brown6249b902012-05-26 14:32:54 -0700788static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700789 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700790{
Jeff Brown6249b902012-05-26 14:32:54 -0700791 struct node* parent_node;
792 char parent_path[PATH_MAX];
793 char child_path[PATH_MAX];
794 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700795
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700796 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700797 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
798 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100799 TRACE("[%d] LOOKUP %s @ %"PRIx64" (%s)\n", handler->token, name, hdr->nodeid,
Jeff Brown6249b902012-05-26 14:32:54 -0700800 parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700801 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700802
803 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
804 child_path, sizeof(child_path), 1))) {
805 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700806 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700807 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700808 return -EACCES;
809 }
810
Jeff Brown6249b902012-05-26 14:32:54 -0700811 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700812}
813
Jeff Brown6249b902012-05-26 14:32:54 -0700814static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700815 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
816{
Jeff Brown6249b902012-05-26 14:32:54 -0700817 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700819 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700820 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100821 TRACE("[%d] FORGET #%"PRIu64" @ %"PRIx64" (%s)\n", handler->token, req->nlookup,
Jeff Brown6249b902012-05-26 14:32:54 -0700822 hdr->nodeid, node ? node->name : "?");
823 if (node) {
824 __u64 n = req->nlookup;
825 while (n--) {
826 release_node_locked(node);
827 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700828 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700829 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700830 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700831}
832
Jeff Brown6249b902012-05-26 14:32:54 -0700833static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700834 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
835{
Jeff Brown6249b902012-05-26 14:32:54 -0700836 struct node* node;
837 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700838
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700839 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700840 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100841 TRACE("[%d] GETATTR flags=%x fh=%"PRIx64" @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700842 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700843 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700844
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700845 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700846 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700847 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700848 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700849 return -EACCES;
850 }
851
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700852 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700853}
854
Jeff Brown6249b902012-05-26 14:32:54 -0700855static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700856 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
857{
Jeff Brown6249b902012-05-26 14:32:54 -0700858 struct node* node;
859 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700860 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700862 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700863 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100864 TRACE("[%d] SETATTR fh=%"PRIx64" valid=%x @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700865 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700866 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700867
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700868 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700869 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700870 }
Marco Nelissena80f0982014-12-10 10:44:20 -0800871
872 if (!(req->valid & FATTR_FH) &&
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700873 !check_caller_access_to_node(fuse, hdr, node, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700874 return -EACCES;
875 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876
Jeff Brown6249b902012-05-26 14:32:54 -0700877 /* XXX: incomplete implementation on purpose.
878 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700879
Elliott Hughes853574d2014-07-31 12:03:03 -0700880 if ((req->valid & FATTR_SIZE) && truncate64(path, req->size) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700881 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700882 }
883
884 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
885 * are both set, then set it to the current time. Else, set it to the
886 * time specified in the request. Same goes for mtime. Use utimensat(2)
887 * as it allows ATIME and MTIME to be changed independently, and has
888 * nanosecond resolution which fuse also has.
889 */
890 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
891 times[0].tv_nsec = UTIME_OMIT;
892 times[1].tv_nsec = UTIME_OMIT;
893 if (req->valid & FATTR_ATIME) {
894 if (req->valid & FATTR_ATIME_NOW) {
895 times[0].tv_nsec = UTIME_NOW;
896 } else {
897 times[0].tv_sec = req->atime;
898 times[0].tv_nsec = req->atimensec;
899 }
900 }
901 if (req->valid & FATTR_MTIME) {
902 if (req->valid & FATTR_MTIME_NOW) {
903 times[1].tv_nsec = UTIME_NOW;
904 } else {
905 times[1].tv_sec = req->mtime;
906 times[1].tv_nsec = req->mtimensec;
907 }
908 }
Jeff Brown6249b902012-05-26 14:32:54 -0700909 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
910 handler->token, path, times[0].tv_sec, times[1].tv_sec);
911 if (utimensat(-1, path, times, 0) < 0) {
912 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700913 }
914 }
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -0700915 return fuse_reply_attr(fuse, hdr->unique, node, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700916}
917
Jeff Brown6249b902012-05-26 14:32:54 -0700918static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700919 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
920{
Jeff Brown6249b902012-05-26 14:32:54 -0700921 struct node* parent_node;
922 char parent_path[PATH_MAX];
923 char child_path[PATH_MAX];
924 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700925
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700926 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700927 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
928 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100929 TRACE("[%d] MKNOD %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700930 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700931 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700932
933 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
934 child_path, sizeof(child_path), 1))) {
935 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700936 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700937 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700938 return -EACCES;
939 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700940 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700941 if (mknod(child_path, mode, req->rdev) < 0) {
942 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700943 }
Jeff Brown6249b902012-05-26 14:32:54 -0700944 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945}
946
Jeff Brown6249b902012-05-26 14:32:54 -0700947static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
949{
Jeff Brown6249b902012-05-26 14:32:54 -0700950 struct node* parent_node;
951 char parent_path[PATH_MAX];
952 char child_path[PATH_MAX];
953 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700954
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700955 pthread_mutex_lock(&fuse->global->lock);
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));
Marcus Oaklande43b99a2014-07-23 13:04:59 +0100958 TRACE("[%d] MKDIR %s 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -0700959 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -0700960 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -0700961
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 Sharkeyf38f29c2015-06-23 14:30:37 -0700966 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -0700967 return -EACCES;
968 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700970 if (mkdir(child_path, mode) < 0) {
971 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700972 }
Jeff Sharkey44d63422013-09-12 09:44:48 -0700973
974 /* When creating /Android/data and /Android/obb, mark them as .nomedia */
975 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) {
976 char nomedia[PATH_MAX];
977 snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path);
978 if (touch(nomedia, 0664) != 0) {
979 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
980 return -ENOENT;
981 }
982 }
983 if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) {
984 char nomedia[PATH_MAX];
Jeff Sharkeyed2fe572015-07-16 09:13:52 -0700985 snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->global->obb_path);
Jeff Sharkey44d63422013-09-12 09:44:48 -0700986 if (touch(nomedia, 0664) != 0) {
987 ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno));
988 return -ENOENT;
989 }
990 }
991
Jeff Brown6249b902012-05-26 14:32:54 -0700992 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700993}
994
Jeff Brown6249b902012-05-26 14:32:54 -0700995static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 const struct fuse_in_header* hdr, const char* name)
997{
Jeff Brown6249b902012-05-26 14:32:54 -0700998 struct node* parent_node;
Krzysztof Adamskic5353122014-07-16 08:34:30 +0200999 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001000 char parent_path[PATH_MAX];
1001 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001002
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001003 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001004 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1005 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001006 TRACE("[%d] UNLINK %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001007 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001008 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001009
1010 if (!parent_node || !find_file_within(parent_path, name,
1011 child_path, sizeof(child_path), 1)) {
1012 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001013 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001014 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001015 return -EACCES;
1016 }
Jeff Brown6249b902012-05-26 14:32:54 -07001017 if (unlink(child_path) < 0) {
1018 return -errno;
1019 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001020 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001021 child_node = lookup_child_by_name_locked(parent_node, name);
1022 if (child_node) {
1023 child_node->deleted = true;
1024 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001025 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001026 if (parent_node && child_node) {
1027 /* Tell all other views that node is gone */
1028 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1029 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1030 if (fuse != fuse->global->fuse_default) {
1031 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1032 }
1033 if (fuse != fuse->global->fuse_read) {
1034 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1035 }
1036 if (fuse != fuse->global->fuse_write) {
1037 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1038 }
1039 }
Jeff Brown6249b902012-05-26 14:32:54 -07001040 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001041}
1042
Jeff Brown6249b902012-05-26 14:32:54 -07001043static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001044 const struct fuse_in_header* hdr, const char* name)
1045{
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001046 struct node* child_node;
Jeff Brown6249b902012-05-26 14:32:54 -07001047 struct node* parent_node;
1048 char parent_path[PATH_MAX];
1049 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001050
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001051 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001052 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1053 parent_path, sizeof(parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001054 TRACE("[%d] RMDIR %s @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001055 name, hdr->nodeid, parent_node ? parent_node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001056 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001057
1058 if (!parent_node || !find_file_within(parent_path, name,
1059 child_path, sizeof(child_path), 1)) {
1060 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001061 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001062 if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001063 return -EACCES;
1064 }
Jeff Brown6249b902012-05-26 14:32:54 -07001065 if (rmdir(child_path) < 0) {
1066 return -errno;
1067 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001068 pthread_mutex_lock(&fuse->global->lock);
Krzysztof Adamskic5353122014-07-16 08:34:30 +02001069 child_node = lookup_child_by_name_locked(parent_node, name);
1070 if (child_node) {
1071 child_node->deleted = true;
1072 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001073 pthread_mutex_unlock(&fuse->global->lock);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001074 if (parent_node && child_node) {
1075 /* Tell all other views that node is gone */
1076 TRACE("[%d] fuse_notify_delete parent=%"PRIx64", child=%"PRIx64", name=%s\n",
1077 handler->token, (uint64_t) parent_node->nid, (uint64_t) child_node->nid, name);
1078 if (fuse != fuse->global->fuse_default) {
1079 fuse_notify_delete(fuse->global->fuse_default, parent_node->nid, child_node->nid, name);
1080 }
1081 if (fuse != fuse->global->fuse_read) {
1082 fuse_notify_delete(fuse->global->fuse_read, parent_node->nid, child_node->nid, name);
1083 }
1084 if (fuse != fuse->global->fuse_write) {
1085 fuse_notify_delete(fuse->global->fuse_write, parent_node->nid, child_node->nid, name);
1086 }
1087 }
Jeff Brown6249b902012-05-26 14:32:54 -07001088 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001089}
1090
Jeff Brown6249b902012-05-26 14:32:54 -07001091static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -07001093 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001094{
Jeff Brown6249b902012-05-26 14:32:54 -07001095 struct node* old_parent_node;
1096 struct node* new_parent_node;
1097 struct node* child_node;
1098 char old_parent_path[PATH_MAX];
1099 char new_parent_path[PATH_MAX];
1100 char old_child_path[PATH_MAX];
1101 char new_child_path[PATH_MAX];
1102 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001103 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001104
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001105 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001106 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
1107 old_parent_path, sizeof(old_parent_path));
1108 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
1109 new_parent_path, sizeof(new_parent_path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001110 TRACE("[%d] RENAME %s->%s @ %"PRIx64" (%s) -> %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001111 old_name, new_name,
1112 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
1113 req->newdir, new_parent_node ? new_parent_node->name : "?");
1114 if (!old_parent_node || !new_parent_node) {
1115 res = -ENOENT;
1116 goto lookup_error;
1117 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001118 if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001119 res = -EACCES;
1120 goto lookup_error;
1121 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001122 if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001123 res = -EACCES;
1124 goto lookup_error;
1125 }
Jeff Brown6249b902012-05-26 14:32:54 -07001126 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
1127 if (!child_node || get_node_path_locked(child_node,
1128 old_child_path, sizeof(old_child_path)) < 0) {
1129 res = -ENOENT;
1130 goto lookup_error;
1131 }
1132 acquire_node_locked(child_node);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001133 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001134
1135 /* Special case for renaming a file where destination is same path
1136 * differing only by case. In this case we don't want to look for a case
1137 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
1138 */
1139 int search = old_parent_node != new_parent_node
1140 || strcasecmp(old_name, new_name);
1141 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
1142 new_child_path, sizeof(new_child_path), search))) {
1143 res = -ENOENT;
1144 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001145 }
1146
Jeff Brown6249b902012-05-26 14:32:54 -07001147 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
1148 res = rename(old_child_path, new_child_path);
1149 if (res < 0) {
1150 res = -errno;
1151 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001152 }
1153
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001154 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001155 res = rename_node_locked(child_node, new_name, new_actual_name);
1156 if (!res) {
1157 remove_node_from_parent_locked(child_node);
Jeff Sharkey22b91262015-12-14 11:02:01 -07001158 derive_permissions_locked(fuse, new_parent_node, child_node);
1159 derive_permissions_recursive_locked(fuse, child_node);
Jeff Brown6249b902012-05-26 14:32:54 -07001160 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001161 }
Jeff Brown6249b902012-05-26 14:32:54 -07001162 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001163
Jeff Brown6249b902012-05-26 14:32:54 -07001164io_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001165 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001166done:
1167 release_node_locked(child_node);
1168lookup_error:
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001169 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001170 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001171}
1172
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001173static int open_flags_to_access_mode(int open_flags) {
1174 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1175 return R_OK;
1176 } else if ((open_flags & O_ACCMODE) == O_WRONLY) {
1177 return W_OK;
1178 } else {
1179 /* Probably O_RDRW, but treat as default to be safe */
1180 return R_OK | W_OK;
1181 }
1182}
1183
Jeff Brown6249b902012-05-26 14:32:54 -07001184static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001185 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1186{
Jeff Brown6249b902012-05-26 14:32:54 -07001187 struct node* node;
1188 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001189 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001190 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001191
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001192 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001193 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001194 TRACE("[%d] OPEN 0%o @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001195 req->flags, hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001196 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001197
1198 if (!node) {
1199 return -ENOENT;
1200 }
Jeff Sharkeyaa04e812013-08-30 10:26:15 -07001201 if (!check_caller_access_to_node(fuse, hdr, node,
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001202 open_flags_to_access_mode(req->flags))) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001203 return -EACCES;
1204 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001205 h = malloc(sizeof(*h));
1206 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001207 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001208 }
Jeff Brown6249b902012-05-26 14:32:54 -07001209 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001210 h->fd = open(path, req->flags);
1211 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001212 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001213 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001214 }
1215 out.fh = ptr_to_id(h);
1216 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001217
1218#ifdef FUSE_STACKED_IO
1219 out.lower_fd = h->fd;
1220#else
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001221 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001222#endif
1223
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001224 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001225 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001226}
1227
Jeff Brown6249b902012-05-26 14:32:54 -07001228static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001229 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1230{
1231 struct handle *h = id_to_ptr(req->fh);
1232 __u64 unique = hdr->unique;
1233 __u32 size = req->size;
1234 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -07001235 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001236 __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGE_SIZE) & ~((uintptr_t)PAGE_SIZE-1));
Jeff Brown6249b902012-05-26 14:32:54 -07001237
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001238 /* Don't access any other fields of hdr or req beyond this point, the read buffer
1239 * overlaps the request buffer and will clobber data in the request. This
1240 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -07001241
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001242 TRACE("[%d] READ %p(%d) %u@%"PRIu64"\n", handler->token,
1243 h, h->fd, size, (uint64_t) offset);
Arpad Horvath80b435a2014-02-14 16:42:27 -08001244 if (size > MAX_READ) {
Jeff Brown6249b902012-05-26 14:32:54 -07001245 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001246 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001247 res = pread64(h->fd, read_buffer, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001248 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001249 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001250 }
Arpad Horvath80b435a2014-02-14 16:42:27 -08001251 fuse_reply(fuse, unique, read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -07001252 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001253}
1254
Jeff Brown6249b902012-05-26 14:32:54 -07001255static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001256 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
1257 const void* buffer)
1258{
1259 struct fuse_write_out out;
1260 struct handle *h = id_to_ptr(req->fh);
1261 int res;
Elliott Hughese24e9a52015-07-28 16:36:47 -07001262 __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGE_SIZE)));
Elliott Hughes60281d52014-05-07 14:39:58 -07001263
Arpad Horvath49e93442014-02-18 10:18:25 +01001264 if (req->flags & O_DIRECT) {
1265 memcpy(aligned_buffer, buffer, req->size);
1266 buffer = (const __u8*) aligned_buffer;
1267 }
Jeff Brown6249b902012-05-26 14:32:54 -07001268
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001269 TRACE("[%d] WRITE %p(%d) %u@%"PRIu64"\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001270 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001271 res = pwrite64(h->fd, buffer, req->size, req->offset);
1272 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001273 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001274 }
1275 out.size = res;
Daisuke Okitsu19ec8862013-08-05 12:18:15 +09001276 out.padding = 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001277 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001278 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001279}
1280
Jeff Brown6249b902012-05-26 14:32:54 -07001281static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001282 const struct fuse_in_header* hdr)
1283{
Jeff Brown6249b902012-05-26 14:32:54 -07001284 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001285 struct statfs stat;
1286 struct fuse_statfs_out out;
1287 int res;
1288
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001289 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001290 TRACE("[%d] STATFS\n", handler->token);
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001291 res = get_node_path_locked(&fuse->global->root, path, sizeof(path));
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001292 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001293 if (res < 0) {
1294 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001295 }
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001296 if (statfs(fuse->global->root.name, &stat) < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001297 return -errno;
1298 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001299 memset(&out, 0, sizeof(out));
1300 out.st.blocks = stat.f_blocks;
1301 out.st.bfree = stat.f_bfree;
1302 out.st.bavail = stat.f_bavail;
1303 out.st.files = stat.f_files;
1304 out.st.ffree = stat.f_ffree;
1305 out.st.bsize = stat.f_bsize;
1306 out.st.namelen = stat.f_namelen;
1307 out.st.frsize = stat.f_frsize;
1308 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001309 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001310}
1311
Jeff Brown6249b902012-05-26 14:32:54 -07001312static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001313 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1314{
1315 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001316
1317 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001318 close(h->fd);
1319 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001320 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001321}
1322
Jeff Brown6249b902012-05-26 14:32:54 -07001323static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001324 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
1325{
Elliott Hughesf6d67372014-07-08 14:38:26 -07001326 bool is_dir = (hdr->opcode == FUSE_FSYNCDIR);
1327 bool is_data_sync = req->fsync_flags & 1;
Jeff Brown6249b902012-05-26 14:32:54 -07001328
Elliott Hughesf6d67372014-07-08 14:38:26 -07001329 int fd = -1;
1330 if (is_dir) {
1331 struct dirhandle *dh = id_to_ptr(req->fh);
1332 fd = dirfd(dh->d);
1333 } else {
1334 struct handle *h = id_to_ptr(req->fh);
1335 fd = h->fd;
1336 }
1337
1338 TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token,
1339 is_dir ? "FSYNCDIR" : "FSYNC",
1340 id_to_ptr(req->fh), fd, is_data_sync);
1341 int res = is_data_sync ? fdatasync(fd) : fsync(fd);
1342 if (res == -1) {
Jeff Brown6249b902012-05-26 14:32:54 -07001343 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001344 }
Jeff Brown6249b902012-05-26 14:32:54 -07001345 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001346}
1347
Jeff Brown6249b902012-05-26 14:32:54 -07001348static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001349 const struct fuse_in_header* hdr)
1350{
Jeff Brown6249b902012-05-26 14:32:54 -07001351 TRACE("[%d] FLUSH\n", handler->token);
1352 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001353}
1354
Jeff Brown6249b902012-05-26 14:32:54 -07001355static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001356 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
1357{
Jeff Brown6249b902012-05-26 14:32:54 -07001358 struct node* node;
1359 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001360 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001361 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001362
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001363 pthread_mutex_lock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001364 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001365 TRACE("[%d] OPENDIR @ %"PRIx64" (%s)\n", handler->token,
Jeff Brown6249b902012-05-26 14:32:54 -07001366 hdr->nodeid, node ? node->name : "?");
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001367 pthread_mutex_unlock(&fuse->global->lock);
Jeff Brown6249b902012-05-26 14:32:54 -07001368
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001369 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -07001370 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001371 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001372 if (!check_caller_access_to_node(fuse, hdr, node, R_OK)) {
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001373 return -EACCES;
1374 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001375 h = malloc(sizeof(*h));
1376 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -07001377 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001378 }
Jeff Brown6249b902012-05-26 14:32:54 -07001379 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001380 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -07001381 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001382 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001383 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001384 }
1385 out.fh = ptr_to_id(h);
Ken Sumrall3a876882013-08-14 20:02:13 -07001386 out.open_flags = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001387
1388#ifdef FUSE_STACKED_IO
1389 out.lower_fd = -1;
1390#else
Ken Sumrall3a876882013-08-14 20:02:13 -07001391 out.padding = 0;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001392#endif
1393
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001394 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001395 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001396}
1397
Jeff Brown6249b902012-05-26 14:32:54 -07001398static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001399 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
1400{
1401 char buffer[8192];
1402 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
1403 struct dirent *de;
1404 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001405
1406 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001407 if (req->offset == 0) {
1408 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -07001409 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001410 rewinddir(h->d);
1411 }
1412 de = readdir(h->d);
1413 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -07001414 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001415 }
1416 fde->ino = FUSE_UNKNOWN_INO;
1417 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
1418 fde->off = req->offset + 1;
1419 fde->type = de->d_type;
1420 fde->namelen = strlen(de->d_name);
1421 memcpy(fde->name, de->d_name, fde->namelen + 1);
1422 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001423 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1424 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001425}
1426
Jeff Brown6249b902012-05-26 14:32:54 -07001427static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001428 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1429{
1430 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001431
1432 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001433 closedir(h->d);
1434 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001435 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001436}
1437
Jeff Brown6249b902012-05-26 14:32:54 -07001438static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001439 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1440{
1441 struct fuse_init_out out;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001442 size_t fuse_struct_size;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001443
Jeff Brown6249b902012-05-26 14:32:54 -07001444 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1445 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001446
1447 /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
1448 * defined (fuse version 7.6). The structure is the same from 7.6 through
1449 * 7.22. Beginning with 7.23, the structure increased in size and added
1450 * new parameters.
1451 */
1452 if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
1453 ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
1454 req->major, req->minor, FUSE_KERNEL_VERSION);
1455 return -1;
1456 }
1457
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001458 /* We limit ourselves to 15 because we don't handle BATCH_FORGET yet */
1459 out.minor = MIN(req->minor, 15);
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001460 fuse_struct_size = sizeof(out);
1461#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
1462 /* FUSE_KERNEL_VERSION >= 23. */
1463
1464 /* If the kernel only works on minor revs older than or equal to 22,
1465 * then use the older structure size since this code only uses the 7.22
1466 * version of the structure. */
1467 if (req->minor <= 22) {
1468 fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
1469 }
1470#endif
1471
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001472 out.major = FUSE_KERNEL_VERSION;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001473 out.max_readahead = req->max_readahead;
1474 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Thierry Strudelac5175f2016-01-13 15:11:35 -08001475
1476#ifdef FUSE_STACKED_IO
1477 out.flags |= FUSE_STACKED_IO;
1478#endif
1479
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001480 out.max_background = 32;
1481 out.congestion_threshold = 32;
1482 out.max_write = MAX_WRITE;
Christopher Ferrisff649ea2014-09-13 13:53:08 -07001483 fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
Jeff Brown6249b902012-05-26 14:32:54 -07001484 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001485}
1486
Jeff Brown6249b902012-05-26 14:32:54 -07001487static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001488 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1489{
Brian Swetland03ee9472010-08-12 18:01:08 -07001490 switch (hdr->opcode) {
1491 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001492 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001493 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001494 }
Jeff Brown84715842012-05-25 14:07:47 -07001495
Brian Swetland03ee9472010-08-12 18:01:08 -07001496 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001497 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001498 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001499 }
Jeff Brown84715842012-05-25 14:07:47 -07001500
Brian Swetland03ee9472010-08-12 18:01:08 -07001501 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001502 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001503 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001504 }
Jeff Brown84715842012-05-25 14:07:47 -07001505
Brian Swetland03ee9472010-08-12 18:01:08 -07001506 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001507 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001508 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001509 }
Jeff Brown84715842012-05-25 14:07:47 -07001510
Brian Swetland03ee9472010-08-12 18:01:08 -07001511// case FUSE_READLINK:
1512// case FUSE_SYMLINK:
1513 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001514 const struct fuse_mknod_in *req = data;
1515 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001516 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001517 }
Jeff Brown84715842012-05-25 14:07:47 -07001518
Brian Swetland03ee9472010-08-12 18:01:08 -07001519 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001520 const struct fuse_mkdir_in *req = data;
1521 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001522 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001523 }
Jeff Brown84715842012-05-25 14:07:47 -07001524
Brian Swetland03ee9472010-08-12 18:01:08 -07001525 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001526 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001527 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001528 }
Jeff Brown84715842012-05-25 14:07:47 -07001529
Brian Swetland03ee9472010-08-12 18:01:08 -07001530 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001531 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001532 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001533 }
Jeff Brown84715842012-05-25 14:07:47 -07001534
Brian Swetland03ee9472010-08-12 18:01:08 -07001535 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001536 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001537 const char *old_name = ((const char*) data) + sizeof(*req);
1538 const char *new_name = old_name + strlen(old_name) + 1;
1539 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001540 }
Jeff Brown84715842012-05-25 14:07:47 -07001541
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001542// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001543 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001544 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001545 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001546 }
Jeff Brown84715842012-05-25 14:07:47 -07001547
Brian Swetland03ee9472010-08-12 18:01:08 -07001548 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001549 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001550 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001551 }
Jeff Brown84715842012-05-25 14:07:47 -07001552
Brian Swetland03ee9472010-08-12 18:01:08 -07001553 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001554 const struct fuse_write_in *req = data;
1555 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001556 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001557 }
Jeff Brown84715842012-05-25 14:07:47 -07001558
Mike Lockwood4553b082010-08-16 14:14:44 -04001559 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001560 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001561 }
Jeff Brown84715842012-05-25 14:07:47 -07001562
Brian Swetland03ee9472010-08-12 18:01:08 -07001563 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001564 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001565 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001566 }
Jeff Brown84715842012-05-25 14:07:47 -07001567
Daisuke Okitsub2831a22014-02-17 10:33:11 +01001568 case FUSE_FSYNC:
1569 case FUSE_FSYNCDIR: {
Jeff Brown6fd921a2012-05-25 15:01:21 -07001570 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001571 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001572 }
1573
Brian Swetland03ee9472010-08-12 18:01:08 -07001574// case FUSE_SETXATTR:
1575// case FUSE_GETXATTR:
1576// case FUSE_LISTXATTR:
1577// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001578 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001579 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001580 }
1581
Brian Swetland03ee9472010-08-12 18:01:08 -07001582 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001583 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001584 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001585 }
Jeff Brown84715842012-05-25 14:07:47 -07001586
Brian Swetland03ee9472010-08-12 18:01:08 -07001587 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001588 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001589 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001590 }
Jeff Brown84715842012-05-25 14:07:47 -07001591
Brian Swetland03ee9472010-08-12 18:01:08 -07001592 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001593 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001594 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001595 }
Jeff Brown84715842012-05-25 14:07:47 -07001596
Brian Swetland03ee9472010-08-12 18:01:08 -07001597 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001598 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001599 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001600 }
Jeff Brown84715842012-05-25 14:07:47 -07001601
Brian Swetland03ee9472010-08-12 18:01:08 -07001602 default: {
Marcus Oaklande43b99a2014-07-23 13:04:59 +01001603 TRACE("[%d] NOTIMPL op=%d uniq=%"PRIx64" nid=%"PRIx64"\n",
Jeff Brown6249b902012-05-26 14:32:54 -07001604 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1605 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001606 }
Jeff Brown84715842012-05-25 14:07:47 -07001607 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001608}
1609
Jeff Brown6249b902012-05-26 14:32:54 -07001610static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001611{
Jeff Brown6249b902012-05-26 14:32:54 -07001612 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001613 for (;;) {
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001614 ssize_t len = TEMP_FAILURE_RETRY(read(fuse->fd,
1615 handler->request_buffer, sizeof(handler->request_buffer)));
Brian Swetland03ee9472010-08-12 18:01:08 -07001616 if (len < 0) {
Jeff Sharkey4a485812015-06-30 16:02:40 -07001617 if (errno == ENODEV) {
1618 ERROR("[%d] someone stole our marbles!\n", handler->token);
1619 exit(2);
1620 }
Mark Salyzyn6b6c1bd2015-07-06 10:00:36 -07001621 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
Jeff Brown6249b902012-05-26 14:32:54 -07001622 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001623 }
Jeff Brown84715842012-05-25 14:07:47 -07001624
1625 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001626 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1627 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001628 }
1629
Jeff Brown7729d242012-05-25 15:35:28 -07001630 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001631 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001632 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1633 handler->token, (size_t)len, hdr->len);
1634 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001635 }
1636
Jeff Brown7729d242012-05-25 15:35:28 -07001637 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001638 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001639 __u64 unique = hdr->unique;
1640 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001641
1642 /* We do not access the request again after this point because the underlying
1643 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001644
1645 if (res != NO_STATUS) {
1646 if (res) {
1647 TRACE("[%d] ERROR %d\n", handler->token, res);
1648 }
1649 fuse_status(fuse, unique, res);
1650 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001651 }
1652}
1653
Jeff Brown6249b902012-05-26 14:32:54 -07001654static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001655{
Jeff Brown6249b902012-05-26 14:32:54 -07001656 struct fuse_handler* handler = data;
1657 handle_fuse_requests(handler);
1658 return NULL;
1659}
1660
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001661static bool remove_str_to_int(void *key, void *value, void *context) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001662 Hashmap* map = context;
1663 hashmapRemove(map, key);
1664 free(key);
Jeff Sharkey977a9f32013-08-12 20:23:49 -07001665 return true;
1666}
1667
William Robertse5099802015-07-31 13:11:19 -07001668static bool package_parse_callback(pkg_info *info, void *userdata) {
1669 struct fuse_global *global = (struct fuse_global *)userdata;
1670
1671 char* name = strdup(info->name);
1672 hashmapPut(global->package_to_appid, name, (void*) (uintptr_t) info->uid);
1673 packagelist_free(info);
1674 return true;
1675}
1676
1677static bool read_package_list(struct fuse_global* global) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001678 pthread_mutex_lock(&global->lock);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001679
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001680 hashmapForEach(global->package_to_appid, remove_str_to_int, global->package_to_appid);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001681
William Robertse5099802015-07-31 13:11:19 -07001682 bool rc = packagelist_parse(package_parse_callback, global);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001683 TRACE("read_package_list: found %zu packages\n",
1684 hashmapSize(global->package_to_appid));
William Robertse5099802015-07-31 13:11:19 -07001685
Jeff Sharkey22b91262015-12-14 11:02:01 -07001686 /* Regenerate ownership details using newly loaded mapping */
1687 derive_permissions_recursive_locked(global->fuse_default, &global->root);
1688
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001689 pthread_mutex_unlock(&global->lock);
William Robertse5099802015-07-31 13:11:19 -07001690
1691 return rc;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001692}
1693
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001694static void watch_package_list(struct fuse_global* global) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001695 struct inotify_event *event;
1696 char event_buf[512];
1697
1698 int nfd = inotify_init();
1699 if (nfd < 0) {
1700 ERROR("inotify_init failed: %s\n", strerror(errno));
1701 return;
1702 }
1703
1704 bool active = false;
1705 while (1) {
1706 if (!active) {
William Robertse5099802015-07-31 13:11:19 -07001707 int res = inotify_add_watch(nfd, PACKAGES_LIST_FILE, IN_DELETE_SELF);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001708 if (res == -1) {
1709 if (errno == ENOENT || errno == EACCES) {
1710 /* Framework may not have created yet, sleep and retry */
William Robertse5099802015-07-31 13:11:19 -07001711 ERROR("missing \"%s\"; retrying\n", PACKAGES_LIST_FILE);
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001712 sleep(3);
1713 continue;
1714 } else {
1715 ERROR("inotify_add_watch failed: %s\n", strerror(errno));
1716 return;
1717 }
1718 }
1719
1720 /* Watch above will tell us about any future changes, so
1721 * read the current state. */
William Robertse5099802015-07-31 13:11:19 -07001722 if (read_package_list(global) == false) {
1723 ERROR("read_package_list failed\n");
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001724 return;
1725 }
1726 active = true;
1727 }
1728
1729 int event_pos = 0;
1730 int res = read(nfd, event_buf, sizeof(event_buf));
1731 if (res < (int) sizeof(*event)) {
1732 if (errno == EINTR)
1733 continue;
1734 ERROR("failed to read inotify event: %s\n", strerror(errno));
1735 return;
1736 }
1737
1738 while (res >= (int) sizeof(*event)) {
1739 int event_size;
1740 event = (struct inotify_event *) (event_buf + event_pos);
1741
1742 TRACE("inotify event: %08x\n", event->mask);
1743 if ((event->mask & IN_IGNORED) == IN_IGNORED) {
1744 /* Previously watched file was deleted, probably due to move
1745 * that swapped in new data; re-arm the watch and read. */
1746 active = false;
1747 }
1748
1749 event_size = sizeof(*event) + event->len;
1750 res -= event_size;
1751 event_pos += event_size;
1752 }
1753 }
1754}
1755
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001756static int usage() {
1757 ERROR("usage: sdcard [OPTIONS] <source_path> <label>\n"
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001758 " -u: specify UID to run as\n"
1759 " -g: specify GID to run as\n"
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001760 " -U: specify user ID that owns device\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001761 " -m: source_path is multi-user\n"
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001762 " -w: runtime write mount has full write access\n"
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001763 "\n");
Jeff Brown26567352012-05-25 13:27:43 -07001764 return 1;
1765}
1766
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001767static int fuse_setup(struct fuse* fuse, gid_t gid, mode_t mask) {
Jeff Brown26567352012-05-25 13:27:43 -07001768 char opts[256];
Jeff Brown26567352012-05-25 13:27:43 -07001769
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001770 fuse->fd = open("/dev/fuse", O_RDWR);
1771 if (fuse->fd == -1) {
1772 ERROR("failed to open fuse device: %s\n", strerror(errno));
Jeff Brown26567352012-05-25 13:27:43 -07001773 return -1;
1774 }
1775
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001776 umount2(fuse->dest_path, MNT_DETACH);
1777
Jeff Brown26567352012-05-25 13:27:43 -07001778 snprintf(opts, sizeof(opts),
1779 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001780 fuse->fd, fuse->global->uid, fuse->global->gid);
1781 if (mount("/dev/fuse", fuse->dest_path, "fuse", MS_NOSUID | MS_NODEV | MS_NOEXEC |
1782 MS_NOATIME, opts) != 0) {
1783 ERROR("failed to mount fuse filesystem: %s\n", strerror(errno));
1784 return -1;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001785 }
1786
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001787 fuse->gid = gid;
1788 fuse->mask = mask;
1789
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001790 return 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001791}
1792
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001793static void run(const char* source_path, const char* label, uid_t uid,
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001794 gid_t gid, userid_t userid, bool multi_user, bool full_write) {
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001795 struct fuse_global global;
1796 struct fuse fuse_default;
1797 struct fuse fuse_read;
1798 struct fuse fuse_write;
1799 struct fuse_handler handler_default;
1800 struct fuse_handler handler_read;
1801 struct fuse_handler handler_write;
1802 pthread_t thread_default;
1803 pthread_t thread_read;
1804 pthread_t thread_write;
1805
1806 memset(&global, 0, sizeof(global));
1807 memset(&fuse_default, 0, sizeof(fuse_default));
1808 memset(&fuse_read, 0, sizeof(fuse_read));
1809 memset(&fuse_write, 0, sizeof(fuse_write));
1810 memset(&handler_default, 0, sizeof(handler_default));
1811 memset(&handler_read, 0, sizeof(handler_read));
1812 memset(&handler_write, 0, sizeof(handler_write));
1813
1814 pthread_mutex_init(&global.lock, NULL);
1815 global.package_to_appid = hashmapCreate(256, str_hash, str_icase_equals);
1816 global.uid = uid;
1817 global.gid = gid;
1818 global.multi_user = multi_user;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001819 global.next_generation = 0;
1820 global.inode_ctr = 1;
1821
1822 memset(&global.root, 0, sizeof(global.root));
1823 global.root.nid = FUSE_ROOT_ID; /* 1 */
1824 global.root.refcount = 2;
1825 global.root.namelen = strlen(source_path);
1826 global.root.name = strdup(source_path);
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001827 global.root.userid = userid;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001828 global.root.uid = AID_ROOT;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001829 global.root.under_android = false;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001830
1831 strcpy(global.source_path, source_path);
1832
1833 if (multi_user) {
1834 global.root.perm = PERM_PRE_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001835 snprintf(global.obb_path, sizeof(global.obb_path), "%s/obb", source_path);
1836 } else {
1837 global.root.perm = PERM_ROOT;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001838 snprintf(global.obb_path, sizeof(global.obb_path), "%s/Android/obb", source_path);
1839 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001840
1841 fuse_default.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001842 fuse_read.global = &global;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001843 fuse_write.global = &global;
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001844
1845 global.fuse_default = &fuse_default;
1846 global.fuse_read = &fuse_read;
1847 global.fuse_write = &fuse_write;
1848
Jeff Sharkeyb9f438f2015-08-06 11:39:44 -07001849 snprintf(fuse_default.dest_path, PATH_MAX, "/mnt/runtime/default/%s", label);
1850 snprintf(fuse_read.dest_path, PATH_MAX, "/mnt/runtime/read/%s", label);
1851 snprintf(fuse_write.dest_path, PATH_MAX, "/mnt/runtime/write/%s", label);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001852
1853 handler_default.fuse = &fuse_default;
1854 handler_read.fuse = &fuse_read;
1855 handler_write.fuse = &fuse_write;
1856
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001857 handler_default.token = 0;
1858 handler_read.token = 1;
1859 handler_write.token = 2;
1860
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001861 umask(0);
1862
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001863 if (multi_user) {
1864 /* Multi-user storage is fully isolated per user, so "other"
1865 * permissions are completely masked off. */
1866 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1867 || fuse_setup(&fuse_read, AID_EVERYBODY, 0027)
1868 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0027)) {
1869 ERROR("failed to fuse_setup\n");
1870 exit(1);
1871 }
1872 } else {
1873 /* Physical storage is readable by all users on device, but
1874 * the Android directories are masked off to a single user
1875 * deep inside attr_from_stat(). */
1876 if (fuse_setup(&fuse_default, AID_SDCARD_RW, 0006)
1877 || fuse_setup(&fuse_read, AID_EVERYBODY, full_write ? 0027 : 0022)
1878 || fuse_setup(&fuse_write, AID_EVERYBODY, full_write ? 0007 : 0022)) {
1879 ERROR("failed to fuse_setup\n");
1880 exit(1);
1881 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001882 }
1883
1884 /* Drop privs */
1885 if (setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups) < 0) {
1886 ERROR("cannot setgroups: %s\n", strerror(errno));
1887 exit(1);
1888 }
1889 if (setgid(gid) < 0) {
1890 ERROR("cannot setgid: %s\n", strerror(errno));
1891 exit(1);
1892 }
1893 if (setuid(uid) < 0) {
1894 ERROR("cannot setuid: %s\n", strerror(errno));
1895 exit(1);
1896 }
1897
1898 if (multi_user) {
Jeff Sharkeyed2fe572015-07-16 09:13:52 -07001899 fs_prepare_dir(global.obb_path, 0775, uid, gid);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001900 }
1901
1902 if (pthread_create(&thread_default, NULL, start_handler, &handler_default)
1903 || pthread_create(&thread_read, NULL, start_handler, &handler_read)
1904 || pthread_create(&thread_write, NULL, start_handler, &handler_write)) {
1905 ERROR("failed to pthread_create\n");
1906 exit(1);
1907 }
1908
1909 watch_package_list(&global);
1910 ERROR("terminated prematurely\n");
1911 exit(1);
1912}
1913
1914int main(int argc, char **argv) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001915 const char *source_path = NULL;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001916 const char *label = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001917 uid_t uid = 0;
1918 gid_t gid = 0;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001919 userid_t userid = 0;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001920 bool multi_user = false;
1921 bool full_write = false;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001922 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001923 struct rlimit rlim;
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001924 int fs_version;
Brian Swetland03ee9472010-08-12 18:01:08 -07001925
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001926 int opt;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001927 while ((opt = getopt(argc, argv, "u:g:U:mw")) != -1) {
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001928 switch (opt) {
1929 case 'u':
1930 uid = strtoul(optarg, NULL, 10);
1931 break;
1932 case 'g':
1933 gid = strtoul(optarg, NULL, 10);
1934 break;
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001935 case 'U':
1936 userid = strtoul(optarg, NULL, 10);
1937 break;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001938 case 'm':
1939 multi_user = true;
1940 break;
Jeff Sharkeye93a0512013-10-08 10:14:24 -07001941 case 'w':
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001942 full_write = true;
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001943 break;
1944 case '?':
1945 default:
1946 return usage();
1947 }
1948 }
1949
1950 for (i = optind; i < argc; i++) {
Mike Lockwood4f35e622011-01-12 14:39:44 -05001951 char* arg = argv[i];
Jeff Sharkeydfe0cba2013-07-03 17:08:29 -07001952 if (!source_path) {
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001953 source_path = arg;
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001954 } else if (!label) {
1955 label = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001956 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001957 ERROR("too many arguments\n");
1958 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001959 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001960 }
1961
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001962 if (!source_path) {
1963 ERROR("no source path specified\n");
1964 return usage();
1965 }
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001966 if (!label) {
1967 ERROR("no label specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001968 return usage();
1969 }
Jeff Brown26567352012-05-25 13:27:43 -07001970 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001971 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001972 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001973 }
1974
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001975 rlim.rlim_cur = 8192;
1976 rlim.rlim_max = 8192;
1977 if (setrlimit(RLIMIT_NOFILE, &rlim)) {
1978 ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno);
1979 }
1980
Nick Kralevich8d28fa72014-07-24 17:05:59 -07001981 while ((fs_read_atomic_int("/data/.layout_version", &fs_version) == -1) || (fs_version < 3)) {
1982 ERROR("installd fs upgrade not yet complete. Waiting...\n");
1983 sleep(1);
1984 }
1985
Jeff Sharkey10a239b2015-07-28 10:49:41 -07001986 run(source_path, label, uid, gid, userid, multi_user, full_write);
Jeff Sharkeyf38f29c2015-06-23 14:30:37 -07001987 return 1;
Brian Swetland03ee9472010-08-12 18:01:08 -07001988}