Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 300d564 | 2014-07-08 13:53:26 -0700 | [diff] [blame] | 17 | #define LOG_TAG "sdcard" |
| 18 | |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | #include <dirent.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <limits.h> |
| 24 | #include <linux/fuse.h> |
| 25 | #include <pthread.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 29 | #include <sys/inotify.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 30 | #include <sys/mount.h> |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 31 | #include <sys/resource.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 32 | #include <sys/stat.h> |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 33 | #include <sys/statfs.h> |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 34 | #include <sys/time.h> |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 35 | #include <sys/uio.h> |
| 36 | #include <unistd.h> |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 37 | |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 38 | #include <cutils/fs.h> |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 39 | #include <cutils/hashmap.h> |
Elliott Hughes | 300d564 | 2014-07-08 13:53:26 -0700 | [diff] [blame] | 40 | #include <cutils/log.h> |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 41 | #include <cutils/multiuser.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 42 | |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 43 | #include <private/android_filesystem_config.h> |
| 44 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 45 | /* README |
| 46 | * |
| 47 | * What is this? |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 48 | * |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 49 | * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 50 | * directory permissions (all files are given fixed owner, group, and |
| 51 | * permissions at creation, owner, group, and permissions are not |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 52 | * changeable, symlinks and hardlinks are not createable, etc. |
| 53 | * |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 54 | * See usage() for command line options. |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 55 | * |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 56 | * It must be run as root, but will drop to requested UID/GID as soon as it |
| 57 | * mounts a filesystem. It will refuse to run if requested UID/GID are zero. |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 58 | * |
| 59 | * Things I believe to be true: |
| 60 | * |
| 61 | * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK, |
| 62 | * CREAT) must bump that node's refcount |
| 63 | * - don't forget that FORGET can forget multiple references (req->nlookup) |
| 64 | * - if an op that returns a fuse_entry fails writing the reply to the |
| 65 | * kernel, you must rollback the refcount to reflect the reference the |
| 66 | * kernel did not actually acquire |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 67 | * |
| 68 | * This daemon can also derive custom filesystem permissions based on directory |
| 69 | * structure when requested. These custom permissions support several features: |
| 70 | * |
| 71 | * - Apps can access their own files in /Android/data/com.example/ without |
| 72 | * requiring any additional GIDs. |
| 73 | * - Separate permissions for protecting directories like Pictures and Music. |
| 74 | * - Multi-user separation on the same physical device. |
| 75 | * |
| 76 | * The derived permissions look like this: |
| 77 | * |
| 78 | * rwxrwx--x root:sdcard_rw / |
| 79 | * rwxrwx--- root:sdcard_pics /Pictures |
| 80 | * rwxrwx--- root:sdcard_av /Music |
| 81 | * |
| 82 | * rwxrwx--x root:sdcard_rw /Android |
| 83 | * rwxrwx--x root:sdcard_rw /Android/data |
| 84 | * rwxrwx--- u0_a12:sdcard_rw /Android/data/com.example |
| 85 | * rwxrwx--x root:sdcard_rw /Android/obb/ |
| 86 | * rwxrwx--- u0_a12:sdcard_rw /Android/obb/com.example |
| 87 | * |
| 88 | * rwxrwx--- root:sdcard_all /Android/user |
| 89 | * rwxrwx--x root:sdcard_rw /Android/user/10 |
| 90 | * rwxrwx--- u10_a12:sdcard_rw /Android/user/10/Android/data/com.example |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 91 | */ |
| 92 | |
| 93 | #define FUSE_TRACE 0 |
| 94 | |
| 95 | #if FUSE_TRACE |
Elliott Hughes | 300d564 | 2014-07-08 13:53:26 -0700 | [diff] [blame] | 96 | #define TRACE(x...) ALOGD(x) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 97 | #else |
| 98 | #define TRACE(x...) do {} while (0) |
| 99 | #endif |
| 100 | |
Elliott Hughes | 300d564 | 2014-07-08 13:53:26 -0700 | [diff] [blame] | 101 | #define ERROR(x...) ALOGE(x) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 102 | |
| 103 | #define FUSE_UNKNOWN_INO 0xffffffff |
| 104 | |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 105 | /* Maximum number of bytes to write in one request. */ |
| 106 | #define MAX_WRITE (256 * 1024) |
| 107 | |
| 108 | /* Maximum number of bytes to read in one request. */ |
| 109 | #define MAX_READ (128 * 1024) |
| 110 | |
| 111 | /* Largest possible request. |
| 112 | * The request size is bounded by the maximum size of a FUSE_WRITE request because it has |
| 113 | * the largest possible data payload. */ |
| 114 | #define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE) |
| 115 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 116 | /* Default number of threads. */ |
| 117 | #define DEFAULT_NUM_THREADS 2 |
| 118 | |
| 119 | /* Pseudo-error constant used to indicate that no fuse status is needed |
| 120 | * or that a reply has already been written. */ |
| 121 | #define NO_STATUS 1 |
| 122 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 123 | /* Path to system-provided mapping of package name to appIds */ |
| 124 | static const char* const kPackagesListFile = "/data/system/packages.list"; |
| 125 | |
| 126 | /* Supplementary groups to execute with */ |
| 127 | static const gid_t kGroups[1] = { AID_PACKAGE_INFO }; |
| 128 | |
| 129 | /* Permission mode for a specific node. Controls how file permissions |
| 130 | * are derived for children nodes. */ |
| 131 | typedef enum { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 132 | /* Nothing special; this node should just inherit from its parent. */ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 133 | PERM_INHERIT, |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 134 | /* This node is one level above a normal root; used for legacy layouts |
| 135 | * which use the first level to represent user_id. */ |
| 136 | PERM_LEGACY_PRE_ROOT, |
| 137 | /* This node is "/" */ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 138 | PERM_ROOT, |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 139 | /* This node is "/Android" */ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 140 | PERM_ANDROID, |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 141 | /* This node is "/Android/data" */ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 142 | PERM_ANDROID_DATA, |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 143 | /* This node is "/Android/obb" */ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 144 | PERM_ANDROID_OBB, |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 145 | /* This node is "/Android/user" */ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 146 | PERM_ANDROID_USER, |
| 147 | } perm_t; |
| 148 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 149 | /* Permissions structure to derive */ |
| 150 | typedef enum { |
| 151 | DERIVE_NONE, |
| 152 | DERIVE_LEGACY, |
| 153 | DERIVE_UNIFIED, |
| 154 | } derive_t; |
| 155 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 156 | struct handle { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 157 | int fd; |
| 158 | }; |
| 159 | |
| 160 | struct dirhandle { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 161 | DIR *d; |
| 162 | }; |
| 163 | |
| 164 | struct node { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 165 | __u32 refcount; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 166 | __u64 nid; |
| 167 | __u64 gen; |
| 168 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 169 | /* State derived based on current position in hierarchy. */ |
| 170 | perm_t perm; |
| 171 | userid_t userid; |
| 172 | uid_t uid; |
| 173 | gid_t gid; |
| 174 | mode_t mode; |
| 175 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 176 | struct node *next; /* per-dir sibling list */ |
| 177 | struct node *child; /* first contained file by this dir */ |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 178 | struct node *parent; /* containing directory */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 179 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 180 | size_t namelen; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 181 | char *name; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 182 | /* If non-null, this is the real name of the file in the underlying storage. |
| 183 | * This may differ from the field "name" only by case. |
| 184 | * strlen(actual_name) will always equal strlen(name), so it is safe to use |
| 185 | * namelen for both fields. |
| 186 | */ |
| 187 | char *actual_name; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 188 | |
| 189 | /* If non-null, an exact underlying path that should be grafted into this |
| 190 | * position. Used to support things like OBB. */ |
| 191 | char* graft_path; |
| 192 | size_t graft_pathlen; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 195 | static int str_hash(void *key) { |
| 196 | return hashmapHash(key, strlen(key)); |
| 197 | } |
| 198 | |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 199 | /** Test if two string keys are equal ignoring case */ |
| 200 | static bool str_icase_equals(void *keyA, void *keyB) { |
| 201 | return strcasecmp(keyA, keyB) == 0; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 204 | static int int_hash(void *key) { |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 205 | return (int) (uintptr_t) key; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static bool int_equals(void *keyA, void *keyB) { |
| 209 | return keyA == keyB; |
| 210 | } |
| 211 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 212 | /* Global data structure shared by all fuse handlers. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 213 | struct fuse { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 214 | pthread_mutex_t lock; |
| 215 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 216 | __u64 next_generation; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 217 | int fd; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 218 | derive_t derive; |
| 219 | bool split_perms; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 220 | gid_t write_gid; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 221 | struct node root; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 222 | char obbpath[PATH_MAX]; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 223 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 224 | Hashmap* package_to_appid; |
| 225 | Hashmap* appid_with_rw; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 228 | /* Private data used by a single fuse handler. */ |
| 229 | struct fuse_handler { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 230 | struct fuse* fuse; |
| 231 | int token; |
| 232 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 233 | /* To save memory, we never use the contents of the request buffer and the read |
| 234 | * buffer at the same time. This allows us to share the underlying storage. */ |
| 235 | union { |
| 236 | __u8 request_buffer[MAX_REQUEST_SIZE]; |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 237 | __u8 read_buffer[MAX_READ + PAGESIZE]; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 238 | }; |
| 239 | }; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 240 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 241 | static inline void *id_to_ptr(__u64 nid) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 242 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 243 | return (void *) (uintptr_t) nid; |
| 244 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 245 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 246 | static inline __u64 ptr_to_id(void *ptr) |
| 247 | { |
| 248 | return (__u64) (uintptr_t) ptr; |
| 249 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 250 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 251 | static void acquire_node_locked(struct node* node) |
| 252 | { |
| 253 | node->refcount++; |
| 254 | TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount); |
| 255 | } |
| 256 | |
| 257 | static void remove_node_from_parent_locked(struct node* node); |
| 258 | |
| 259 | static void release_node_locked(struct node* node) |
| 260 | { |
| 261 | TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount); |
| 262 | if (node->refcount > 0) { |
| 263 | node->refcount--; |
| 264 | if (!node->refcount) { |
| 265 | TRACE("DESTROY %p (%s)\n", node, node->name); |
| 266 | remove_node_from_parent_locked(node); |
| 267 | |
| 268 | /* TODO: remove debugging - poison memory */ |
| 269 | memset(node->name, 0xef, node->namelen); |
| 270 | free(node->name); |
| 271 | free(node->actual_name); |
| 272 | memset(node, 0xfc, sizeof(*node)); |
| 273 | free(node); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 274 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 275 | } else { |
| 276 | ERROR("Zero refcnt %p\n", node); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static void add_node_to_parent_locked(struct node *node, struct node *parent) { |
| 281 | node->parent = parent; |
| 282 | node->next = parent->child; |
| 283 | parent->child = node; |
| 284 | acquire_node_locked(parent); |
| 285 | } |
| 286 | |
| 287 | static void remove_node_from_parent_locked(struct node* node) |
| 288 | { |
| 289 | if (node->parent) { |
| 290 | if (node->parent->child == node) { |
| 291 | node->parent->child = node->parent->child->next; |
| 292 | } else { |
| 293 | struct node *node2; |
| 294 | node2 = node->parent->child; |
| 295 | while (node2->next != node) |
| 296 | node2 = node2->next; |
| 297 | node2->next = node->next; |
| 298 | } |
| 299 | release_node_locked(node->parent); |
| 300 | node->parent = NULL; |
| 301 | node->next = NULL; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* Gets the absolute path to a node into the provided buffer. |
| 306 | * |
| 307 | * Populates 'buf' with the path and returns the length of the path on success, |
| 308 | * or returns -1 if the path is too long for the provided buffer. |
| 309 | */ |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 310 | static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) { |
| 311 | const char* name; |
| 312 | size_t namelen; |
| 313 | if (node->graft_path) { |
| 314 | name = node->graft_path; |
| 315 | namelen = node->graft_pathlen; |
| 316 | } else if (node->actual_name) { |
| 317 | name = node->actual_name; |
| 318 | namelen = node->namelen; |
| 319 | } else { |
| 320 | name = node->name; |
| 321 | namelen = node->namelen; |
| 322 | } |
| 323 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 324 | if (bufsize < namelen + 1) { |
| 325 | return -1; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 328 | ssize_t pathlen = 0; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 329 | if (node->parent && node->graft_path == NULL) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 330 | pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2); |
| 331 | if (pathlen < 0) { |
| 332 | return -1; |
| 333 | } |
| 334 | buf[pathlen++] = '/'; |
| 335 | } |
| 336 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 337 | memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */ |
| 338 | return pathlen + namelen; |
| 339 | } |
| 340 | |
| 341 | /* Finds the absolute path of a file within a given directory. |
| 342 | * Performs a case-insensitive search for the file and sets the buffer to the path |
| 343 | * of the first matching file. If 'search' is zero or if no match is found, sets |
| 344 | * the buffer to the path that the file would have, assuming the name were case-sensitive. |
| 345 | * |
| 346 | * Populates 'buf' with the path and returns the actual name (within 'buf') on success, |
| 347 | * or returns NULL if the path is too long for the provided buffer. |
| 348 | */ |
| 349 | static char* find_file_within(const char* path, const char* name, |
| 350 | char* buf, size_t bufsize, int search) |
| 351 | { |
| 352 | size_t pathlen = strlen(path); |
| 353 | size_t namelen = strlen(name); |
| 354 | size_t childlen = pathlen + namelen + 1; |
| 355 | char* actual; |
| 356 | |
| 357 | if (bufsize <= childlen) { |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | memcpy(buf, path, pathlen); |
| 362 | buf[pathlen] = '/'; |
| 363 | actual = buf + pathlen + 1; |
| 364 | memcpy(actual, name, namelen + 1); |
| 365 | |
| 366 | if (search && access(buf, F_OK)) { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 367 | struct dirent* entry; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 368 | DIR* dir = opendir(path); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 369 | if (!dir) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 370 | ERROR("opendir %s failed: %s\n", path, strerror(errno)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 371 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 372 | } |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 373 | while ((entry = readdir(dir))) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 374 | if (!strcasecmp(entry->d_name, name)) { |
| 375 | /* we have a match - replace the name, don't need to copy the null again */ |
| 376 | memcpy(actual, entry->d_name, namelen); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | } |
| 380 | closedir(dir); |
| 381 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 382 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 385 | static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, const struct node* node) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 386 | { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 387 | attr->ino = node->nid; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 388 | attr->size = s->st_size; |
| 389 | attr->blocks = s->st_blocks; |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 390 | attr->atime = s->st_atime; |
| 391 | attr->mtime = s->st_mtime; |
| 392 | attr->ctime = s->st_ctime; |
| 393 | attr->atimensec = s->st_atime_nsec; |
| 394 | attr->mtimensec = s->st_mtime_nsec; |
| 395 | attr->ctimensec = s->st_ctime_nsec; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 396 | attr->mode = s->st_mode; |
| 397 | attr->nlink = s->st_nlink; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 398 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 399 | attr->uid = node->uid; |
| 400 | attr->gid = node->gid; |
| 401 | |
| 402 | /* Filter requested mode based on underlying file, and |
| 403 | * pass through file type. */ |
| 404 | int owner_mode = s->st_mode & 0700; |
| 405 | int filtered_mode = node->mode & (owner_mode | (owner_mode >> 3) | (owner_mode >> 6)); |
| 406 | attr->mode = (attr->mode & S_IFMT) | filtered_mode; |
| 407 | } |
| 408 | |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 409 | static int touch(char* path, mode_t mode) { |
| 410 | int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, mode); |
| 411 | if (fd == -1) { |
| 412 | if (errno == EEXIST) { |
| 413 | return 0; |
| 414 | } else { |
| 415 | ERROR("Failed to open(%s): %s\n", path, strerror(errno)); |
| 416 | return -1; |
| 417 | } |
| 418 | } |
| 419 | close(fd); |
| 420 | return 0; |
| 421 | } |
| 422 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 423 | static void derive_permissions_locked(struct fuse* fuse, struct node *parent, |
| 424 | struct node *node) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 425 | appid_t appid; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 426 | |
| 427 | /* By default, each node inherits from its parent */ |
| 428 | node->perm = PERM_INHERIT; |
| 429 | node->userid = parent->userid; |
| 430 | node->uid = parent->uid; |
| 431 | node->gid = parent->gid; |
| 432 | node->mode = parent->mode; |
| 433 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 434 | if (fuse->derive == DERIVE_NONE) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 435 | return; |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 438 | /* Derive custom permissions based on parent and current node */ |
| 439 | switch (parent->perm) { |
| 440 | case PERM_INHERIT: |
| 441 | /* Already inherited above */ |
| 442 | break; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 443 | case PERM_LEGACY_PRE_ROOT: |
| 444 | /* Legacy internal layout places users at top level */ |
| 445 | node->perm = PERM_ROOT; |
| 446 | node->userid = strtoul(node->name, NULL, 10); |
| 447 | break; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 448 | case PERM_ROOT: |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 449 | /* Assume masked off by default. */ |
| 450 | node->mode = 0770; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 451 | if (!strcasecmp(node->name, "Android")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 452 | /* App-specific directories inside; let anyone traverse */ |
| 453 | node->perm = PERM_ANDROID; |
| 454 | node->mode = 0771; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 455 | } else if (fuse->split_perms) { |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 456 | if (!strcasecmp(node->name, "DCIM") |
| 457 | || !strcasecmp(node->name, "Pictures")) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 458 | node->gid = AID_SDCARD_PICS; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 459 | } else if (!strcasecmp(node->name, "Alarms") |
| 460 | || !strcasecmp(node->name, "Movies") |
| 461 | || !strcasecmp(node->name, "Music") |
| 462 | || !strcasecmp(node->name, "Notifications") |
| 463 | || !strcasecmp(node->name, "Podcasts") |
| 464 | || !strcasecmp(node->name, "Ringtones")) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 465 | node->gid = AID_SDCARD_AV; |
| 466 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 467 | } |
| 468 | break; |
| 469 | case PERM_ANDROID: |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 470 | if (!strcasecmp(node->name, "data")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 471 | /* App-specific directories inside; let anyone traverse */ |
| 472 | node->perm = PERM_ANDROID_DATA; |
| 473 | node->mode = 0771; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 474 | } else if (!strcasecmp(node->name, "obb")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 475 | /* App-specific directories inside; let anyone traverse */ |
| 476 | node->perm = PERM_ANDROID_OBB; |
| 477 | node->mode = 0771; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 478 | /* Single OBB directory is always shared */ |
| 479 | node->graft_path = fuse->obbpath; |
| 480 | node->graft_pathlen = strlen(fuse->obbpath); |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 481 | } else if (!strcasecmp(node->name, "user")) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 482 | /* User directories must only be accessible to system, protected |
| 483 | * by sdcard_all. Zygote will bind mount the appropriate user- |
| 484 | * specific path. */ |
| 485 | node->perm = PERM_ANDROID_USER; |
| 486 | node->gid = AID_SDCARD_ALL; |
| 487 | node->mode = 0770; |
| 488 | } |
| 489 | break; |
| 490 | case PERM_ANDROID_DATA: |
| 491 | case PERM_ANDROID_OBB: |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 492 | appid = (appid_t) (uintptr_t) hashmapGet(fuse->package_to_appid, node->name); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 493 | if (appid != 0) { |
| 494 | node->uid = multiuser_get_uid(parent->userid, appid); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 495 | } |
| 496 | node->mode = 0770; |
| 497 | break; |
| 498 | case PERM_ANDROID_USER: |
| 499 | /* Root of a secondary user */ |
| 500 | node->perm = PERM_ROOT; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 501 | node->userid = strtoul(node->name, NULL, 10); |
| 502 | node->gid = AID_SDCARD_R; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 503 | node->mode = 0771; |
| 504 | break; |
| 505 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 508 | /* Return if the calling UID holds sdcard_rw. */ |
| 509 | static bool get_caller_has_rw_locked(struct fuse* fuse, const struct fuse_in_header *hdr) { |
Jeff Sharkey | 39ff0ae | 2013-08-30 13:58:13 -0700 | [diff] [blame] | 510 | /* No additional permissions enforcement */ |
| 511 | if (fuse->derive == DERIVE_NONE) { |
| 512 | return true; |
| 513 | } |
| 514 | |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 515 | appid_t appid = multiuser_get_app_id(hdr->uid); |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 516 | return hashmapContainsKey(fuse->appid_with_rw, (void*) (uintptr_t) appid); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 519 | /* Kernel has already enforced everything we returned through |
| 520 | * derive_permissions_locked(), so this is used to lock down access |
| 521 | * even further, such as enforcing that apps hold sdcard_rw. */ |
| 522 | static bool check_caller_access_to_name(struct fuse* fuse, |
| 523 | const struct fuse_in_header *hdr, const struct node* parent_node, |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 524 | const char* name, int mode, bool has_rw) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 525 | /* Always block security-sensitive files at root */ |
| 526 | if (parent_node && parent_node->perm == PERM_ROOT) { |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 527 | if (!strcasecmp(name, "autorun.inf") |
| 528 | || !strcasecmp(name, ".android_secure") |
| 529 | || !strcasecmp(name, "android_secure")) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /* No additional permissions enforcement */ |
| 535 | if (fuse->derive == DERIVE_NONE) { |
| 536 | return true; |
| 537 | } |
| 538 | |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 539 | /* Root always has access; access for any other UIDs should always |
| 540 | * be controlled through packages.list. */ |
| 541 | if (hdr->uid == 0) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 542 | return true; |
| 543 | } |
| 544 | |
| 545 | /* If asking to write, verify that caller either owns the |
| 546 | * parent or holds sdcard_rw. */ |
| 547 | if (mode & W_OK) { |
| 548 | if (parent_node && hdr->uid == parent_node->uid) { |
| 549 | return true; |
| 550 | } |
| 551 | |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 552 | return has_rw; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* No extra permissions to enforce */ |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | static bool check_caller_access_to_node(struct fuse* fuse, |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 560 | const struct fuse_in_header *hdr, const struct node* node, int mode, bool has_rw) { |
| 561 | return check_caller_access_to_name(fuse, hdr, node->parent, node->name, mode, has_rw); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 564 | struct node *create_node_locked(struct fuse* fuse, |
| 565 | struct node *parent, const char *name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 566 | { |
| 567 | struct node *node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 568 | size_t namelen = strlen(name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 569 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 570 | node = calloc(1, sizeof(struct node)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 571 | if (!node) { |
| 572 | return NULL; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 573 | } |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 574 | node->name = malloc(namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 575 | if (!node->name) { |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 576 | free(node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 577 | return NULL; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 578 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 579 | memcpy(node->name, name, namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 580 | if (strcmp(name, actual_name)) { |
| 581 | node->actual_name = malloc(namelen + 1); |
| 582 | if (!node->actual_name) { |
| 583 | free(node->name); |
| 584 | free(node); |
| 585 | return NULL; |
| 586 | } |
| 587 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 588 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 589 | node->namelen = namelen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 590 | node->nid = ptr_to_id(node); |
| 591 | node->gen = fuse->next_generation++; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 592 | |
| 593 | derive_permissions_locked(fuse, parent, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 594 | acquire_node_locked(node); |
| 595 | add_node_to_parent_locked(node, parent); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 596 | return node; |
| 597 | } |
| 598 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 599 | static int rename_node_locked(struct node *node, const char *name, |
| 600 | const char* actual_name) |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 601 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 602 | size_t namelen = strlen(name); |
| 603 | int need_actual_name = strcmp(name, actual_name); |
| 604 | |
| 605 | /* make the storage bigger without actually changing the name |
| 606 | * in case an error occurs part way */ |
| 607 | if (namelen > node->namelen) { |
| 608 | char* new_name = realloc(node->name, namelen + 1); |
| 609 | if (!new_name) { |
| 610 | return -ENOMEM; |
| 611 | } |
| 612 | node->name = new_name; |
| 613 | if (need_actual_name && node->actual_name) { |
| 614 | char* new_actual_name = realloc(node->actual_name, namelen + 1); |
| 615 | if (!new_actual_name) { |
| 616 | return -ENOMEM; |
| 617 | } |
| 618 | node->actual_name = new_actual_name; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /* update the name, taking care to allocate storage before overwriting the old name */ |
| 623 | if (need_actual_name) { |
| 624 | if (!node->actual_name) { |
| 625 | node->actual_name = malloc(namelen + 1); |
| 626 | if (!node->actual_name) { |
| 627 | return -ENOMEM; |
| 628 | } |
| 629 | } |
| 630 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 631 | } else { |
| 632 | free(node->actual_name); |
| 633 | node->actual_name = NULL; |
| 634 | } |
| 635 | memcpy(node->name, name, namelen + 1); |
| 636 | node->namelen = namelen; |
| 637 | return 0; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 640 | static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 641 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 642 | if (nid == FUSE_ROOT_ID) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 643 | return &fuse->root; |
| 644 | } else { |
| 645 | return id_to_ptr(nid); |
| 646 | } |
| 647 | } |
| 648 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 649 | static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid, |
| 650 | char* buf, size_t bufsize) |
| 651 | { |
| 652 | struct node* node = lookup_node_by_id_locked(fuse, nid); |
| 653 | if (node && get_node_path_locked(node, buf, bufsize) < 0) { |
| 654 | node = NULL; |
| 655 | } |
| 656 | return node; |
| 657 | } |
| 658 | |
| 659 | static struct node *lookup_child_by_name_locked(struct node *node, const char *name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 660 | { |
| 661 | for (node = node->child; node; node = node->next) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 662 | /* use exact string comparison, nodes that differ by case |
| 663 | * must be considered distinct even if they refer to the same |
| 664 | * underlying file as otherwise operations such as "mv x x" |
| 665 | * will not work because the source and target nodes are the same. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 666 | if (!strcmp(name, node->name)) { |
| 667 | return node; |
| 668 | } |
| 669 | } |
| 670 | return 0; |
| 671 | } |
| 672 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 673 | static struct node* acquire_or_create_child_locked( |
| 674 | struct fuse* fuse, struct node* parent, |
| 675 | const char* name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 676 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 677 | struct node* child = lookup_child_by_name_locked(parent, name); |
| 678 | if (child) { |
| 679 | acquire_node_locked(child); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 680 | } else { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 681 | child = create_node_locked(fuse, parent, name, actual_name); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 682 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 683 | return child; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 686 | static void fuse_init(struct fuse *fuse, int fd, const char *source_path, |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 687 | gid_t write_gid, derive_t derive, bool split_perms) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 688 | pthread_mutex_init(&fuse->lock, NULL); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 689 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 690 | fuse->fd = fd; |
| 691 | fuse->next_generation = 0; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 692 | fuse->derive = derive; |
| 693 | fuse->split_perms = split_perms; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 694 | fuse->write_gid = write_gid; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 695 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 696 | memset(&fuse->root, 0, sizeof(fuse->root)); |
| 697 | fuse->root.nid = FUSE_ROOT_ID; /* 1 */ |
| 698 | fuse->root.refcount = 2; |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 699 | fuse->root.namelen = strlen(source_path); |
| 700 | fuse->root.name = strdup(source_path); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 701 | fuse->root.userid = 0; |
| 702 | fuse->root.uid = AID_ROOT; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 703 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 704 | /* Set up root node for various modes of operation */ |
| 705 | switch (derive) { |
| 706 | case DERIVE_NONE: |
| 707 | /* Traditional behavior that treats entire device as being accessible |
| 708 | * to sdcard_rw, and no permissions are derived. */ |
| 709 | fuse->root.perm = PERM_ROOT; |
| 710 | fuse->root.mode = 0775; |
| 711 | fuse->root.gid = AID_SDCARD_RW; |
| 712 | break; |
| 713 | case DERIVE_LEGACY: |
| 714 | /* Legacy behavior used to support internal multiuser layout which |
| 715 | * places user_id at the top directory level, with the actual roots |
| 716 | * just below that. Shared OBB path is also at top level. */ |
| 717 | fuse->root.perm = PERM_LEGACY_PRE_ROOT; |
| 718 | fuse->root.mode = 0771; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 719 | fuse->root.gid = AID_SDCARD_R; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 720 | fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 721 | fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals); |
| 722 | snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/obb", source_path); |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 723 | fs_prepare_dir(fuse->obbpath, 0775, getuid(), getgid()); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 724 | break; |
| 725 | case DERIVE_UNIFIED: |
| 726 | /* Unified multiuser layout which places secondary user_id under |
| 727 | * /Android/user and shared OBB path under /Android/obb. */ |
| 728 | fuse->root.perm = PERM_ROOT; |
| 729 | fuse->root.mode = 0771; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 730 | fuse->root.gid = AID_SDCARD_R; |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 731 | fuse->package_to_appid = hashmapCreate(256, str_hash, str_icase_equals); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 732 | fuse->appid_with_rw = hashmapCreate(128, int_hash, int_equals); |
| 733 | snprintf(fuse->obbpath, sizeof(fuse->obbpath), "%s/Android/obb", source_path); |
| 734 | break; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 735 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 738 | static void fuse_status(struct fuse *fuse, __u64 unique, int err) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 739 | { |
| 740 | struct fuse_out_header hdr; |
| 741 | hdr.len = sizeof(hdr); |
| 742 | hdr.error = err; |
| 743 | hdr.unique = unique; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 744 | write(fuse->fd, &hdr, sizeof(hdr)); |
| 745 | } |
| 746 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 747 | static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 748 | { |
| 749 | struct fuse_out_header hdr; |
| 750 | struct iovec vec[2]; |
| 751 | int res; |
| 752 | |
| 753 | hdr.len = len + sizeof(hdr); |
| 754 | hdr.error = 0; |
| 755 | hdr.unique = unique; |
| 756 | |
| 757 | vec[0].iov_base = &hdr; |
| 758 | vec[0].iov_len = sizeof(hdr); |
| 759 | vec[1].iov_base = data; |
| 760 | vec[1].iov_len = len; |
| 761 | |
| 762 | res = writev(fuse->fd, vec, 2); |
| 763 | if (res < 0) { |
| 764 | ERROR("*** REPLY FAILED *** %d\n", errno); |
| 765 | } |
| 766 | } |
| 767 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 768 | static int fuse_reply_entry(struct fuse* fuse, __u64 unique, |
| 769 | struct node* parent, const char* name, const char* actual_name, |
| 770 | const char* path) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 771 | { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 772 | struct node* node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 773 | struct fuse_entry_out out; |
| 774 | struct stat s; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 775 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 776 | if (lstat(path, &s) < 0) { |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 777 | return -errno; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 778 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 779 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 780 | pthread_mutex_lock(&fuse->lock); |
| 781 | node = acquire_or_create_child_locked(fuse, parent, name, actual_name); |
| 782 | if (!node) { |
| 783 | pthread_mutex_unlock(&fuse->lock); |
| 784 | return -ENOMEM; |
| 785 | } |
| 786 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 787 | attr_from_stat(&out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 788 | out.attr_valid = 10; |
| 789 | out.entry_valid = 10; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 790 | out.nodeid = node->nid; |
| 791 | out.generation = node->gen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 792 | pthread_mutex_unlock(&fuse->lock); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 793 | fuse_reply(fuse, unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 794 | return NO_STATUS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 797 | static int fuse_reply_attr(struct fuse* fuse, __u64 unique, const struct node* node, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 798 | const char* path) |
| 799 | { |
| 800 | struct fuse_attr_out out; |
| 801 | struct stat s; |
| 802 | |
| 803 | if (lstat(path, &s) < 0) { |
| 804 | return -errno; |
| 805 | } |
| 806 | memset(&out, 0, sizeof(out)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 807 | attr_from_stat(&out.attr, &s, node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 808 | out.attr_valid = 10; |
| 809 | fuse_reply(fuse, unique, &out, sizeof(out)); |
| 810 | return NO_STATUS; |
| 811 | } |
| 812 | |
| 813 | static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 814 | const struct fuse_in_header *hdr, const char* name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 815 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 816 | struct node* parent_node; |
| 817 | char parent_path[PATH_MAX]; |
| 818 | char child_path[PATH_MAX]; |
| 819 | const char* actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 820 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 821 | pthread_mutex_lock(&fuse->lock); |
| 822 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 823 | parent_path, sizeof(parent_path)); |
| 824 | TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid, |
| 825 | parent_node ? parent_node->name : "?"); |
| 826 | pthread_mutex_unlock(&fuse->lock); |
| 827 | |
| 828 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 829 | child_path, sizeof(child_path), 1))) { |
| 830 | return -ENOENT; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 831 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 832 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, R_OK, false)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 833 | return -EACCES; |
| 834 | } |
| 835 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 836 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 839 | static int handle_forget(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 840 | const struct fuse_in_header *hdr, const struct fuse_forget_in *req) |
| 841 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 842 | struct node* node; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 843 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 844 | pthread_mutex_lock(&fuse->lock); |
| 845 | node = lookup_node_by_id_locked(fuse, hdr->nodeid); |
| 846 | TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup, |
| 847 | hdr->nodeid, node ? node->name : "?"); |
| 848 | if (node) { |
| 849 | __u64 n = req->nlookup; |
| 850 | while (n--) { |
| 851 | release_node_locked(node); |
| 852 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 853 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 854 | pthread_mutex_unlock(&fuse->lock); |
| 855 | return NO_STATUS; /* no reply */ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 858 | static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 859 | const struct fuse_in_header *hdr, const struct fuse_getattr_in *req) |
| 860 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 861 | struct node* node; |
| 862 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 863 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 864 | pthread_mutex_lock(&fuse->lock); |
| 865 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 866 | TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token, |
| 867 | req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?"); |
| 868 | pthread_mutex_unlock(&fuse->lock); |
| 869 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 870 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 871 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 872 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 873 | if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 874 | return -EACCES; |
| 875 | } |
| 876 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 877 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 880 | static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 881 | const struct fuse_in_header *hdr, const struct fuse_setattr_in *req) |
| 882 | { |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 883 | bool has_rw; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 884 | struct node* node; |
| 885 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 886 | struct timespec times[2]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 887 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 888 | pthread_mutex_lock(&fuse->lock); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 889 | has_rw = get_caller_has_rw_locked(fuse, hdr); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 890 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 891 | TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token, |
| 892 | req->fh, req->valid, hdr->nodeid, node ? node->name : "?"); |
| 893 | pthread_mutex_unlock(&fuse->lock); |
| 894 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 895 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 896 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 897 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 898 | if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 899 | return -EACCES; |
| 900 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 901 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 902 | /* XXX: incomplete implementation on purpose. |
| 903 | * chmod/chown should NEVER be implemented.*/ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 904 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 905 | if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) { |
| 906 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 910 | * are both set, then set it to the current time. Else, set it to the |
| 911 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 912 | * as it allows ATIME and MTIME to be changed independently, and has |
| 913 | * nanosecond resolution which fuse also has. |
| 914 | */ |
| 915 | if (req->valid & (FATTR_ATIME | FATTR_MTIME)) { |
| 916 | times[0].tv_nsec = UTIME_OMIT; |
| 917 | times[1].tv_nsec = UTIME_OMIT; |
| 918 | if (req->valid & FATTR_ATIME) { |
| 919 | if (req->valid & FATTR_ATIME_NOW) { |
| 920 | times[0].tv_nsec = UTIME_NOW; |
| 921 | } else { |
| 922 | times[0].tv_sec = req->atime; |
| 923 | times[0].tv_nsec = req->atimensec; |
| 924 | } |
| 925 | } |
| 926 | if (req->valid & FATTR_MTIME) { |
| 927 | if (req->valid & FATTR_MTIME_NOW) { |
| 928 | times[1].tv_nsec = UTIME_NOW; |
| 929 | } else { |
| 930 | times[1].tv_sec = req->mtime; |
| 931 | times[1].tv_nsec = req->mtimensec; |
| 932 | } |
| 933 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 934 | TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n", |
| 935 | handler->token, path, times[0].tv_sec, times[1].tv_sec); |
| 936 | if (utimensat(-1, path, times, 0) < 0) { |
| 937 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 938 | } |
| 939 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 940 | return fuse_reply_attr(fuse, hdr->unique, node, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 943 | static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 944 | const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name) |
| 945 | { |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 946 | bool has_rw; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 947 | struct node* parent_node; |
| 948 | char parent_path[PATH_MAX]; |
| 949 | char child_path[PATH_MAX]; |
| 950 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 951 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 952 | pthread_mutex_lock(&fuse->lock); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 953 | has_rw = get_caller_has_rw_locked(fuse, hdr); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 954 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 955 | parent_path, sizeof(parent_path)); |
| 956 | TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token, |
| 957 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 958 | pthread_mutex_unlock(&fuse->lock); |
| 959 | |
| 960 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 961 | child_path, sizeof(child_path), 1))) { |
| 962 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 963 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 964 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 965 | return -EACCES; |
| 966 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 967 | __u32 mode = (req->mode & (~0777)) | 0664; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 968 | if (mknod(child_path, mode, req->rdev) < 0) { |
| 969 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 970 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 971 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 974 | static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 975 | const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name) |
| 976 | { |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 977 | bool has_rw; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 978 | struct node* parent_node; |
| 979 | char parent_path[PATH_MAX]; |
| 980 | char child_path[PATH_MAX]; |
| 981 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 982 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 983 | pthread_mutex_lock(&fuse->lock); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 984 | has_rw = get_caller_has_rw_locked(fuse, hdr); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 985 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 986 | parent_path, sizeof(parent_path)); |
| 987 | TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token, |
| 988 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 989 | pthread_mutex_unlock(&fuse->lock); |
| 990 | |
| 991 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 992 | child_path, sizeof(child_path), 1))) { |
| 993 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 994 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 995 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 996 | return -EACCES; |
| 997 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 998 | __u32 mode = (req->mode & (~0777)) | 0775; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 999 | if (mkdir(child_path, mode) < 0) { |
| 1000 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1001 | } |
Jeff Sharkey | 44d6342 | 2013-09-12 09:44:48 -0700 | [diff] [blame] | 1002 | |
| 1003 | /* When creating /Android/data and /Android/obb, mark them as .nomedia */ |
| 1004 | if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "data")) { |
| 1005 | char nomedia[PATH_MAX]; |
| 1006 | snprintf(nomedia, PATH_MAX, "%s/.nomedia", child_path); |
| 1007 | if (touch(nomedia, 0664) != 0) { |
| 1008 | ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno)); |
| 1009 | return -ENOENT; |
| 1010 | } |
| 1011 | } |
| 1012 | if (parent_node->perm == PERM_ANDROID && !strcasecmp(name, "obb")) { |
| 1013 | char nomedia[PATH_MAX]; |
| 1014 | snprintf(nomedia, PATH_MAX, "%s/.nomedia", fuse->obbpath); |
| 1015 | if (touch(nomedia, 0664) != 0) { |
| 1016 | ERROR("Failed to touch(%s): %s\n", nomedia, strerror(errno)); |
| 1017 | return -ENOENT; |
| 1018 | } |
| 1019 | } |
| 1020 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1021 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1024 | static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1025 | const struct fuse_in_header* hdr, const char* name) |
| 1026 | { |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1027 | bool has_rw; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1028 | struct node* parent_node; |
| 1029 | char parent_path[PATH_MAX]; |
| 1030 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1031 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1032 | pthread_mutex_lock(&fuse->lock); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1033 | has_rw = get_caller_has_rw_locked(fuse, hdr); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1034 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 1035 | parent_path, sizeof(parent_path)); |
| 1036 | TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token, |
| 1037 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 1038 | pthread_mutex_unlock(&fuse->lock); |
| 1039 | |
| 1040 | if (!parent_node || !find_file_within(parent_path, name, |
| 1041 | child_path, sizeof(child_path), 1)) { |
| 1042 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1043 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1044 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1045 | return -EACCES; |
| 1046 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1047 | if (unlink(child_path) < 0) { |
| 1048 | return -errno; |
| 1049 | } |
| 1050 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1053 | static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1054 | const struct fuse_in_header* hdr, const char* name) |
| 1055 | { |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1056 | bool has_rw; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1057 | struct node* parent_node; |
| 1058 | char parent_path[PATH_MAX]; |
| 1059 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1060 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1061 | pthread_mutex_lock(&fuse->lock); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1062 | has_rw = get_caller_has_rw_locked(fuse, hdr); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1063 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 1064 | parent_path, sizeof(parent_path)); |
| 1065 | TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token, |
| 1066 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 1067 | pthread_mutex_unlock(&fuse->lock); |
| 1068 | |
| 1069 | if (!parent_node || !find_file_within(parent_path, name, |
| 1070 | child_path, sizeof(child_path), 1)) { |
| 1071 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1072 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1073 | if (!check_caller_access_to_name(fuse, hdr, parent_node, name, W_OK, has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1074 | return -EACCES; |
| 1075 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1076 | if (rmdir(child_path) < 0) { |
| 1077 | return -errno; |
| 1078 | } |
| 1079 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1082 | static int handle_rename(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1083 | const struct fuse_in_header* hdr, const struct fuse_rename_in* req, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1084 | const char* old_name, const char* new_name) |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1085 | { |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1086 | bool has_rw; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1087 | struct node* old_parent_node; |
| 1088 | struct node* new_parent_node; |
| 1089 | struct node* child_node; |
| 1090 | char old_parent_path[PATH_MAX]; |
| 1091 | char new_parent_path[PATH_MAX]; |
| 1092 | char old_child_path[PATH_MAX]; |
| 1093 | char new_child_path[PATH_MAX]; |
| 1094 | const char* new_actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1095 | int res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1096 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1097 | pthread_mutex_lock(&fuse->lock); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1098 | has_rw = get_caller_has_rw_locked(fuse, hdr); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1099 | old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 1100 | old_parent_path, sizeof(old_parent_path)); |
| 1101 | new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir, |
| 1102 | new_parent_path, sizeof(new_parent_path)); |
| 1103 | TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token, |
| 1104 | old_name, new_name, |
| 1105 | hdr->nodeid, old_parent_node ? old_parent_node->name : "?", |
| 1106 | req->newdir, new_parent_node ? new_parent_node->name : "?"); |
| 1107 | if (!old_parent_node || !new_parent_node) { |
| 1108 | res = -ENOENT; |
| 1109 | goto lookup_error; |
| 1110 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1111 | if (!check_caller_access_to_name(fuse, hdr, old_parent_node, old_name, W_OK, has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1112 | res = -EACCES; |
| 1113 | goto lookup_error; |
| 1114 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1115 | if (!check_caller_access_to_name(fuse, hdr, new_parent_node, new_name, W_OK, has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1116 | res = -EACCES; |
| 1117 | goto lookup_error; |
| 1118 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1119 | child_node = lookup_child_by_name_locked(old_parent_node, old_name); |
| 1120 | if (!child_node || get_node_path_locked(child_node, |
| 1121 | old_child_path, sizeof(old_child_path)) < 0) { |
| 1122 | res = -ENOENT; |
| 1123 | goto lookup_error; |
| 1124 | } |
| 1125 | acquire_node_locked(child_node); |
| 1126 | pthread_mutex_unlock(&fuse->lock); |
| 1127 | |
| 1128 | /* Special case for renaming a file where destination is same path |
| 1129 | * differing only by case. In this case we don't want to look for a case |
| 1130 | * insensitive match. This allows commands like "mv foo FOO" to work as expected. |
| 1131 | */ |
| 1132 | int search = old_parent_node != new_parent_node |
| 1133 | || strcasecmp(old_name, new_name); |
| 1134 | if (!(new_actual_name = find_file_within(new_parent_path, new_name, |
| 1135 | new_child_path, sizeof(new_child_path), search))) { |
| 1136 | res = -ENOENT; |
| 1137 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1140 | TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path); |
| 1141 | res = rename(old_child_path, new_child_path); |
| 1142 | if (res < 0) { |
| 1143 | res = -errno; |
| 1144 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1147 | pthread_mutex_lock(&fuse->lock); |
| 1148 | res = rename_node_locked(child_node, new_name, new_actual_name); |
| 1149 | if (!res) { |
| 1150 | remove_node_from_parent_locked(child_node); |
| 1151 | add_node_to_parent_locked(child_node, new_parent_node); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1152 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1153 | goto done; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1154 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1155 | io_error: |
| 1156 | pthread_mutex_lock(&fuse->lock); |
| 1157 | done: |
| 1158 | release_node_locked(child_node); |
| 1159 | lookup_error: |
| 1160 | pthread_mutex_unlock(&fuse->lock); |
| 1161 | return res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1164 | static int open_flags_to_access_mode(int open_flags) { |
| 1165 | if ((open_flags & O_ACCMODE) == O_RDONLY) { |
| 1166 | return R_OK; |
| 1167 | } else if ((open_flags & O_ACCMODE) == O_WRONLY) { |
| 1168 | return W_OK; |
| 1169 | } else { |
| 1170 | /* Probably O_RDRW, but treat as default to be safe */ |
| 1171 | return R_OK | W_OK; |
| 1172 | } |
| 1173 | } |
| 1174 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1175 | static int handle_open(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1176 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 1177 | { |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1178 | bool has_rw; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1179 | struct node* node; |
| 1180 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1181 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1182 | struct handle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1183 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1184 | pthread_mutex_lock(&fuse->lock); |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1185 | has_rw = get_caller_has_rw_locked(fuse, hdr); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1186 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 1187 | TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token, |
| 1188 | req->flags, hdr->nodeid, node ? node->name : "?"); |
| 1189 | pthread_mutex_unlock(&fuse->lock); |
| 1190 | |
| 1191 | if (!node) { |
| 1192 | return -ENOENT; |
| 1193 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1194 | if (!check_caller_access_to_node(fuse, hdr, node, |
| 1195 | open_flags_to_access_mode(req->flags), has_rw)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1196 | return -EACCES; |
| 1197 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1198 | h = malloc(sizeof(*h)); |
| 1199 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1200 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1201 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1202 | TRACE("[%d] OPEN %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1203 | h->fd = open(path, req->flags); |
| 1204 | if (h->fd < 0) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1205 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1206 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1207 | } |
| 1208 | out.fh = ptr_to_id(h); |
| 1209 | out.open_flags = 0; |
| 1210 | out.padding = 0; |
| 1211 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1212 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1215 | static int handle_read(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1216 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1217 | { |
| 1218 | struct handle *h = id_to_ptr(req->fh); |
| 1219 | __u64 unique = hdr->unique; |
| 1220 | __u32 size = req->size; |
| 1221 | __u64 offset = req->offset; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1222 | int res; |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1223 | __u8 *read_buffer = (__u8 *) ((uintptr_t)(handler->read_buffer + PAGESIZE) & ~((uintptr_t)PAGESIZE-1)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1224 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1225 | /* Don't access any other fields of hdr or req beyond this point, the read buffer |
| 1226 | * overlaps the request buffer and will clobber data in the request. This |
| 1227 | * saves us 128KB per request handler thread at the cost of this scary comment. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1228 | |
| 1229 | TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token, |
| 1230 | h, h->fd, size, offset); |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1231 | if (size > MAX_READ) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1232 | return -EINVAL; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1233 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1234 | res = pread64(h->fd, read_buffer, size, offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1235 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1236 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1237 | } |
Arpad Horvath | 80b435a | 2014-02-14 16:42:27 -0800 | [diff] [blame] | 1238 | fuse_reply(fuse, unique, read_buffer, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1239 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1242 | static int handle_write(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1243 | const struct fuse_in_header* hdr, const struct fuse_write_in* req, |
| 1244 | const void* buffer) |
| 1245 | { |
| 1246 | struct fuse_write_out out; |
| 1247 | struct handle *h = id_to_ptr(req->fh); |
| 1248 | int res; |
Arpad Horvath | 49e9344 | 2014-02-18 10:18:25 +0100 | [diff] [blame] | 1249 | __u8 aligned_buffer[req->size] __attribute__((__aligned__(PAGESIZE))); |
Elliott Hughes | 60281d5 | 2014-05-07 14:39:58 -0700 | [diff] [blame] | 1250 | |
Arpad Horvath | 49e9344 | 2014-02-18 10:18:25 +0100 | [diff] [blame] | 1251 | if (req->flags & O_DIRECT) { |
| 1252 | memcpy(aligned_buffer, buffer, req->size); |
| 1253 | buffer = (const __u8*) aligned_buffer; |
| 1254 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1255 | |
| 1256 | TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token, |
| 1257 | h, h->fd, req->size, req->offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1258 | res = pwrite64(h->fd, buffer, req->size, req->offset); |
| 1259 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1260 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1261 | } |
| 1262 | out.size = res; |
| 1263 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1264 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1267 | static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1268 | const struct fuse_in_header* hdr) |
| 1269 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1270 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1271 | struct statfs stat; |
| 1272 | struct fuse_statfs_out out; |
| 1273 | int res; |
| 1274 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1275 | pthread_mutex_lock(&fuse->lock); |
| 1276 | TRACE("[%d] STATFS\n", handler->token); |
| 1277 | res = get_node_path_locked(&fuse->root, path, sizeof(path)); |
| 1278 | pthread_mutex_unlock(&fuse->lock); |
| 1279 | if (res < 0) { |
| 1280 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1281 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1282 | if (statfs(fuse->root.name, &stat) < 0) { |
| 1283 | return -errno; |
| 1284 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1285 | memset(&out, 0, sizeof(out)); |
| 1286 | out.st.blocks = stat.f_blocks; |
| 1287 | out.st.bfree = stat.f_bfree; |
| 1288 | out.st.bavail = stat.f_bavail; |
| 1289 | out.st.files = stat.f_files; |
| 1290 | out.st.ffree = stat.f_ffree; |
| 1291 | out.st.bsize = stat.f_bsize; |
| 1292 | out.st.namelen = stat.f_namelen; |
| 1293 | out.st.frsize = stat.f_frsize; |
| 1294 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1295 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1298 | static int handle_release(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1299 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1300 | { |
| 1301 | struct handle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1302 | |
| 1303 | TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1304 | close(h->fd); |
| 1305 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1306 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1309 | static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1310 | const struct fuse_in_header* hdr, const struct fuse_fsync_in* req) |
| 1311 | { |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame^] | 1312 | bool is_dir = (hdr->opcode == FUSE_FSYNCDIR); |
| 1313 | bool is_data_sync = req->fsync_flags & 1; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1314 | |
Elliott Hughes | f6d6737 | 2014-07-08 14:38:26 -0700 | [diff] [blame^] | 1315 | int fd = -1; |
| 1316 | if (is_dir) { |
| 1317 | struct dirhandle *dh = id_to_ptr(req->fh); |
| 1318 | fd = dirfd(dh->d); |
| 1319 | } else { |
| 1320 | struct handle *h = id_to_ptr(req->fh); |
| 1321 | fd = h->fd; |
| 1322 | } |
| 1323 | |
| 1324 | TRACE("[%d] %s %p(%d) is_data_sync=%d\n", handler->token, |
| 1325 | is_dir ? "FSYNCDIR" : "FSYNC", |
| 1326 | id_to_ptr(req->fh), fd, is_data_sync); |
| 1327 | int res = is_data_sync ? fdatasync(fd) : fsync(fd); |
| 1328 | if (res == -1) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1329 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1330 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1331 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1334 | static int handle_flush(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1335 | const struct fuse_in_header* hdr) |
| 1336 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1337 | TRACE("[%d] FLUSH\n", handler->token); |
| 1338 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1341 | static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1342 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 1343 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1344 | struct node* node; |
| 1345 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1346 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1347 | struct dirhandle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1348 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1349 | pthread_mutex_lock(&fuse->lock); |
| 1350 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 1351 | TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token, |
| 1352 | hdr->nodeid, node ? node->name : "?"); |
| 1353 | pthread_mutex_unlock(&fuse->lock); |
| 1354 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1355 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1356 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1357 | } |
Jeff Sharkey | aa04e81 | 2013-08-30 10:26:15 -0700 | [diff] [blame] | 1358 | if (!check_caller_access_to_node(fuse, hdr, node, R_OK, false)) { |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1359 | return -EACCES; |
| 1360 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1361 | h = malloc(sizeof(*h)); |
| 1362 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1363 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1364 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1365 | TRACE("[%d] OPENDIR %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1366 | h->d = opendir(path); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1367 | if (!h->d) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1368 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1369 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1370 | } |
| 1371 | out.fh = ptr_to_id(h); |
Ken Sumrall | 3a87688 | 2013-08-14 20:02:13 -0700 | [diff] [blame] | 1372 | out.open_flags = 0; |
| 1373 | out.padding = 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1374 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1375 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1378 | static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1379 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 1380 | { |
| 1381 | char buffer[8192]; |
| 1382 | struct fuse_dirent *fde = (struct fuse_dirent*) buffer; |
| 1383 | struct dirent *de; |
| 1384 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1385 | |
| 1386 | TRACE("[%d] READDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1387 | if (req->offset == 0) { |
| 1388 | /* rewinddir() might have been called above us, so rewind here too */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1389 | TRACE("[%d] calling rewinddir()\n", handler->token); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1390 | rewinddir(h->d); |
| 1391 | } |
| 1392 | de = readdir(h->d); |
| 1393 | if (!de) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1394 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1395 | } |
| 1396 | fde->ino = FUSE_UNKNOWN_INO; |
| 1397 | /* increment the offset so we can detect when rewinddir() seeks back to the beginning */ |
| 1398 | fde->off = req->offset + 1; |
| 1399 | fde->type = de->d_type; |
| 1400 | fde->namelen = strlen(de->d_name); |
| 1401 | memcpy(fde->name, de->d_name, fde->namelen + 1); |
| 1402 | fuse_reply(fuse, hdr->unique, fde, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1403 | FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); |
| 1404 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1407 | static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1408 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1409 | { |
| 1410 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1411 | |
| 1412 | TRACE("[%d] RELEASEDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1413 | closedir(h->d); |
| 1414 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1415 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1418 | static int handle_init(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1419 | const struct fuse_in_header* hdr, const struct fuse_init_in* req) |
| 1420 | { |
| 1421 | struct fuse_init_out out; |
| 1422 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1423 | TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n", |
| 1424 | handler->token, req->major, req->minor, req->max_readahead, req->flags); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1425 | out.major = FUSE_KERNEL_VERSION; |
| 1426 | out.minor = FUSE_KERNEL_MINOR_VERSION; |
| 1427 | out.max_readahead = req->max_readahead; |
| 1428 | out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
| 1429 | out.max_background = 32; |
| 1430 | out.congestion_threshold = 32; |
| 1431 | out.max_write = MAX_WRITE; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1432 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1433 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1436 | static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1437 | const struct fuse_in_header *hdr, const void *data, size_t data_len) |
| 1438 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1439 | switch (hdr->opcode) { |
| 1440 | case FUSE_LOOKUP: { /* bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1441 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1442 | return handle_lookup(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1443 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1444 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1445 | case FUSE_FORGET: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1446 | const struct fuse_forget_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1447 | return handle_forget(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1448 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1449 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1450 | case FUSE_GETATTR: { /* getattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1451 | const struct fuse_getattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1452 | return handle_getattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1453 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1454 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1455 | case FUSE_SETATTR: { /* setattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1456 | const struct fuse_setattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1457 | return handle_setattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1458 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1459 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1460 | // case FUSE_READLINK: |
| 1461 | // case FUSE_SYMLINK: |
| 1462 | case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1463 | const struct fuse_mknod_in *req = data; |
| 1464 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1465 | return handle_mknod(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1466 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1467 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1468 | case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1469 | const struct fuse_mkdir_in *req = data; |
| 1470 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1471 | return handle_mkdir(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1472 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1473 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1474 | case FUSE_UNLINK: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1475 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1476 | return handle_unlink(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1477 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1478 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1479 | case FUSE_RMDIR: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1480 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1481 | return handle_rmdir(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1482 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1483 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1484 | case FUSE_RENAME: { /* rename_in, oldname, newname -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1485 | const struct fuse_rename_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1486 | const char *old_name = ((const char*) data) + sizeof(*req); |
| 1487 | const char *new_name = old_name + strlen(old_name) + 1; |
| 1488 | return handle_rename(fuse, handler, hdr, req, old_name, new_name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1489 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1490 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1491 | // case FUSE_LINK: |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1492 | case FUSE_OPEN: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1493 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1494 | return handle_open(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1495 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1496 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1497 | case FUSE_READ: { /* read_in -> byte[] */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1498 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1499 | return handle_read(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1500 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1501 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1502 | case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1503 | const struct fuse_write_in *req = data; |
| 1504 | const void* buffer = (const __u8*)data + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1505 | return handle_write(fuse, handler, hdr, req, buffer); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1506 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1507 | |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1508 | case FUSE_STATFS: { /* getattr_in -> attr_out */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1509 | return handle_statfs(fuse, handler, hdr); |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1510 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1511 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1512 | case FUSE_RELEASE: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1513 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1514 | return handle_release(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1515 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1516 | |
Daisuke Okitsu | b2831a2 | 2014-02-17 10:33:11 +0100 | [diff] [blame] | 1517 | case FUSE_FSYNC: |
| 1518 | case FUSE_FSYNCDIR: { |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1519 | const struct fuse_fsync_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1520 | return handle_fsync(fuse, handler, hdr, req); |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1523 | // case FUSE_SETXATTR: |
| 1524 | // case FUSE_GETXATTR: |
| 1525 | // case FUSE_LISTXATTR: |
| 1526 | // case FUSE_REMOVEXATTR: |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1527 | case FUSE_FLUSH: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1528 | return handle_flush(fuse, handler, hdr); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1531 | case FUSE_OPENDIR: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1532 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1533 | return handle_opendir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1534 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1535 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1536 | case FUSE_READDIR: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1537 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1538 | return handle_readdir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1539 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1540 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1541 | case FUSE_RELEASEDIR: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1542 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1543 | return handle_releasedir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1544 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1545 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1546 | case FUSE_INIT: { /* init_in -> init_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1547 | const struct fuse_init_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1548 | return handle_init(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1549 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1550 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1551 | default: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1552 | TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n", |
| 1553 | handler->token, hdr->opcode, hdr->unique, hdr->nodeid); |
| 1554 | return -ENOSYS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1555 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1556 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1559 | static void handle_fuse_requests(struct fuse_handler* handler) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1560 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1561 | struct fuse* fuse = handler->fuse; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1562 | for (;;) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1563 | ssize_t len = read(fuse->fd, |
| 1564 | handler->request_buffer, sizeof(handler->request_buffer)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1565 | if (len < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1566 | if (errno != EINTR) { |
| 1567 | ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno); |
| 1568 | } |
| 1569 | continue; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1570 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1571 | |
| 1572 | if ((size_t)len < sizeof(struct fuse_in_header)) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1573 | ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len); |
| 1574 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1577 | const struct fuse_in_header *hdr = (void*)handler->request_buffer; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1578 | if (hdr->len != (size_t)len) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1579 | ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n", |
| 1580 | handler->token, (size_t)len, hdr->len); |
| 1581 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1584 | const void *data = handler->request_buffer + sizeof(struct fuse_in_header); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1585 | size_t data_len = len - sizeof(struct fuse_in_header); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1586 | __u64 unique = hdr->unique; |
| 1587 | int res = handle_fuse_request(fuse, handler, hdr, data, data_len); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1588 | |
| 1589 | /* We do not access the request again after this point because the underlying |
| 1590 | * buffer storage may have been reused while processing the request. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1591 | |
| 1592 | if (res != NO_STATUS) { |
| 1593 | if (res) { |
| 1594 | TRACE("[%d] ERROR %d\n", handler->token, res); |
| 1595 | } |
| 1596 | fuse_status(fuse, unique, res); |
| 1597 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1601 | static void* start_handler(void* data) |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1602 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1603 | struct fuse_handler* handler = data; |
| 1604 | handle_fuse_requests(handler); |
| 1605 | return NULL; |
| 1606 | } |
| 1607 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1608 | static bool remove_str_to_int(void *key, void *value, void *context) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1609 | Hashmap* map = context; |
| 1610 | hashmapRemove(map, key); |
| 1611 | free(key); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1612 | return true; |
| 1613 | } |
| 1614 | |
| 1615 | static bool remove_int_to_null(void *key, void *value, void *context) { |
| 1616 | Hashmap* map = context; |
| 1617 | hashmapRemove(map, key); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1618 | return true; |
| 1619 | } |
| 1620 | |
| 1621 | static int read_package_list(struct fuse *fuse) { |
| 1622 | pthread_mutex_lock(&fuse->lock); |
| 1623 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1624 | hashmapForEach(fuse->package_to_appid, remove_str_to_int, fuse->package_to_appid); |
| 1625 | hashmapForEach(fuse->appid_with_rw, remove_int_to_null, fuse->appid_with_rw); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1626 | |
| 1627 | FILE* file = fopen(kPackagesListFile, "r"); |
| 1628 | if (!file) { |
| 1629 | ERROR("failed to open package list: %s\n", strerror(errno)); |
| 1630 | pthread_mutex_unlock(&fuse->lock); |
| 1631 | return -1; |
| 1632 | } |
| 1633 | |
| 1634 | char buf[512]; |
| 1635 | while (fgets(buf, sizeof(buf), file) != NULL) { |
| 1636 | char package_name[512]; |
| 1637 | int appid; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1638 | char gids[512]; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1639 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1640 | if (sscanf(buf, "%s %d %*d %*s %*s %s", package_name, &appid, gids) == 3) { |
| 1641 | char* package_name_dup = strdup(package_name); |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 1642 | hashmapPut(fuse->package_to_appid, package_name_dup, (void*) (uintptr_t) appid); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1643 | |
| 1644 | char* token = strtok(gids, ","); |
| 1645 | while (token != NULL) { |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1646 | if (strtoul(token, NULL, 10) == fuse->write_gid) { |
Elliott Hughes | 5d9fe77 | 2014-02-05 17:50:35 -0800 | [diff] [blame] | 1647 | hashmapPut(fuse->appid_with_rw, (void*) (uintptr_t) appid, (void*) (uintptr_t) 1); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1648 | break; |
| 1649 | } |
| 1650 | token = strtok(NULL, ","); |
| 1651 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1655 | TRACE("read_package_list: found %d packages, %d with write_gid\n", |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1656 | hashmapSize(fuse->package_to_appid), |
| 1657 | hashmapSize(fuse->appid_with_rw)); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1658 | fclose(file); |
| 1659 | pthread_mutex_unlock(&fuse->lock); |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
| 1663 | static void watch_package_list(struct fuse* fuse) { |
| 1664 | struct inotify_event *event; |
| 1665 | char event_buf[512]; |
| 1666 | |
| 1667 | int nfd = inotify_init(); |
| 1668 | if (nfd < 0) { |
| 1669 | ERROR("inotify_init failed: %s\n", strerror(errno)); |
| 1670 | return; |
| 1671 | } |
| 1672 | |
| 1673 | bool active = false; |
| 1674 | while (1) { |
| 1675 | if (!active) { |
| 1676 | int res = inotify_add_watch(nfd, kPackagesListFile, IN_DELETE_SELF); |
| 1677 | if (res == -1) { |
| 1678 | if (errno == ENOENT || errno == EACCES) { |
| 1679 | /* Framework may not have created yet, sleep and retry */ |
| 1680 | ERROR("missing packages.list; retrying\n"); |
| 1681 | sleep(3); |
| 1682 | continue; |
| 1683 | } else { |
| 1684 | ERROR("inotify_add_watch failed: %s\n", strerror(errno)); |
| 1685 | return; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | /* Watch above will tell us about any future changes, so |
| 1690 | * read the current state. */ |
| 1691 | if (read_package_list(fuse) == -1) { |
| 1692 | ERROR("read_package_list failed: %s\n", strerror(errno)); |
| 1693 | return; |
| 1694 | } |
| 1695 | active = true; |
| 1696 | } |
| 1697 | |
| 1698 | int event_pos = 0; |
| 1699 | int res = read(nfd, event_buf, sizeof(event_buf)); |
| 1700 | if (res < (int) sizeof(*event)) { |
| 1701 | if (errno == EINTR) |
| 1702 | continue; |
| 1703 | ERROR("failed to read inotify event: %s\n", strerror(errno)); |
| 1704 | return; |
| 1705 | } |
| 1706 | |
| 1707 | while (res >= (int) sizeof(*event)) { |
| 1708 | int event_size; |
| 1709 | event = (struct inotify_event *) (event_buf + event_pos); |
| 1710 | |
| 1711 | TRACE("inotify event: %08x\n", event->mask); |
| 1712 | if ((event->mask & IN_IGNORED) == IN_IGNORED) { |
| 1713 | /* Previously watched file was deleted, probably due to move |
| 1714 | * that swapped in new data; re-arm the watch and read. */ |
| 1715 | active = false; |
| 1716 | } |
| 1717 | |
| 1718 | event_size = sizeof(*event) + event->len; |
| 1719 | res -= event_size; |
| 1720 | event_pos += event_size; |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1725 | static int ignite_fuse(struct fuse* fuse, int num_threads) |
| 1726 | { |
| 1727 | struct fuse_handler* handlers; |
| 1728 | int i; |
| 1729 | |
| 1730 | handlers = malloc(num_threads * sizeof(struct fuse_handler)); |
| 1731 | if (!handlers) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1732 | ERROR("cannot allocate storage for threads\n"); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1733 | return -ENOMEM; |
| 1734 | } |
| 1735 | |
| 1736 | for (i = 0; i < num_threads; i++) { |
| 1737 | handlers[i].fuse = fuse; |
| 1738 | handlers[i].token = i; |
| 1739 | } |
| 1740 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1741 | /* When deriving permissions, this thread is used to process inotify events, |
| 1742 | * otherwise it becomes one of the FUSE handlers. */ |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1743 | i = (fuse->derive == DERIVE_NONE) ? 1 : 0; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1744 | for (; i < num_threads; i++) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1745 | pthread_t thread; |
| 1746 | int res = pthread_create(&thread, NULL, start_handler, &handlers[i]); |
| 1747 | if (res) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1748 | ERROR("failed to start thread #%d, error=%d\n", i, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1749 | goto quit; |
| 1750 | } |
| 1751 | } |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1752 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1753 | if (fuse->derive == DERIVE_NONE) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1754 | handle_fuse_requests(&handlers[0]); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1755 | } else { |
| 1756 | watch_package_list(fuse); |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | ERROR("terminated prematurely\n"); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1760 | |
| 1761 | /* don't bother killing all of the other threads or freeing anything, |
| 1762 | * should never get here anyhow */ |
| 1763 | quit: |
| 1764 | exit(1); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1767 | static int usage() |
| 1768 | { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1769 | ERROR("usage: sdcard [OPTIONS] <source_path> <dest_path>\n" |
| 1770 | " -u: specify UID to run as\n" |
| 1771 | " -g: specify GID to run as\n" |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1772 | " -w: specify GID required to write (default sdcard_rw, requires -d or -l)\n" |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1773 | " -t: specify number of threads to use (default %d)\n" |
| 1774 | " -d: derive file permissions based on path\n" |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1775 | " -l: derive file permissions based on legacy internal layout\n" |
| 1776 | " -s: split derived permissions for pics, av\n" |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1777 | "\n", DEFAULT_NUM_THREADS); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1778 | return 1; |
| 1779 | } |
| 1780 | |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1781 | static int run(const char* source_path, const char* dest_path, uid_t uid, |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1782 | gid_t gid, gid_t write_gid, int num_threads, derive_t derive, |
| 1783 | bool split_perms) { |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1784 | int fd; |
| 1785 | char opts[256]; |
| 1786 | int res; |
| 1787 | struct fuse fuse; |
| 1788 | |
| 1789 | /* cleanup from previous instance, if necessary */ |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1790 | umount2(dest_path, 2); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1791 | |
| 1792 | fd = open("/dev/fuse", O_RDWR); |
| 1793 | if (fd < 0){ |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1794 | ERROR("cannot open fuse device: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1795 | return -1; |
| 1796 | } |
| 1797 | |
| 1798 | snprintf(opts, sizeof(opts), |
| 1799 | "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d", |
| 1800 | fd, uid, gid); |
| 1801 | |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1802 | res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1803 | if (res < 0) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1804 | ERROR("cannot mount fuse filesystem: %s\n", strerror(errno)); |
| 1805 | goto error; |
| 1806 | } |
| 1807 | |
| 1808 | res = setgroups(sizeof(kGroups) / sizeof(kGroups[0]), kGroups); |
| 1809 | if (res < 0) { |
| 1810 | ERROR("cannot setgroups: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1811 | goto error; |
| 1812 | } |
| 1813 | |
| 1814 | res = setgid(gid); |
| 1815 | if (res < 0) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1816 | ERROR("cannot setgid: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1817 | goto error; |
| 1818 | } |
| 1819 | |
| 1820 | res = setuid(uid); |
| 1821 | if (res < 0) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1822 | ERROR("cannot setuid: %s\n", strerror(errno)); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1823 | goto error; |
| 1824 | } |
| 1825 | |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1826 | fuse_init(&fuse, fd, source_path, write_gid, derive, split_perms); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1827 | |
| 1828 | umask(0); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1829 | res = ignite_fuse(&fuse, num_threads); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1830 | |
| 1831 | /* we do not attempt to umount the file system here because we are no longer |
| 1832 | * running as the root user */ |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1833 | |
| 1834 | error: |
| 1835 | close(fd); |
| 1836 | return res; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1837 | } |
| 1838 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1839 | int main(int argc, char **argv) |
| 1840 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1841 | int res; |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1842 | const char *source_path = NULL; |
| 1843 | const char *dest_path = NULL; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1844 | uid_t uid = 0; |
| 1845 | gid_t gid = 0; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1846 | gid_t write_gid = AID_SDCARD_RW; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1847 | int num_threads = DEFAULT_NUM_THREADS; |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1848 | derive_t derive = DERIVE_NONE; |
| 1849 | bool split_perms = false; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1850 | int i; |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 1851 | struct rlimit rlim; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1852 | |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1853 | int opt; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1854 | while ((opt = getopt(argc, argv, "u:g:w:t:dls")) != -1) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1855 | switch (opt) { |
| 1856 | case 'u': |
| 1857 | uid = strtoul(optarg, NULL, 10); |
| 1858 | break; |
| 1859 | case 'g': |
| 1860 | gid = strtoul(optarg, NULL, 10); |
| 1861 | break; |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1862 | case 'w': |
| 1863 | write_gid = strtoul(optarg, NULL, 10); |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1864 | break; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1865 | case 't': |
| 1866 | num_threads = strtoul(optarg, NULL, 10); |
| 1867 | break; |
| 1868 | case 'd': |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1869 | derive = DERIVE_UNIFIED; |
| 1870 | break; |
| 1871 | case 'l': |
| 1872 | derive = DERIVE_LEGACY; |
| 1873 | break; |
| 1874 | case 's': |
| 1875 | split_perms = true; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1876 | break; |
| 1877 | case '?': |
| 1878 | default: |
| 1879 | return usage(); |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | for (i = optind; i < argc; i++) { |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1884 | char* arg = argv[i]; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1885 | if (!source_path) { |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1886 | source_path = arg; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1887 | } else if (!dest_path) { |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1888 | dest_path = arg; |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1889 | } else if (!uid) { |
| 1890 | uid = strtoul(arg, NULL, 10); |
Jean-Baptiste Queru | e92372b | 2012-08-15 09:54:30 -0700 | [diff] [blame] | 1891 | } else if (!gid) { |
Jeff Sharkey | dfe0cba | 2013-07-03 17:08:29 -0700 | [diff] [blame] | 1892 | gid = strtoul(arg, NULL, 10); |
Jean-Baptiste Queru | e92372b | 2012-08-15 09:54:30 -0700 | [diff] [blame] | 1893 | } else { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1894 | ERROR("too many arguments\n"); |
| 1895 | return usage(); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1896 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
Jeff Sharkey | e169bd0 | 2012-08-13 16:44:42 -0700 | [diff] [blame] | 1899 | if (!source_path) { |
| 1900 | ERROR("no source path specified\n"); |
| 1901 | return usage(); |
| 1902 | } |
| 1903 | if (!dest_path) { |
| 1904 | ERROR("no dest path specified\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1905 | return usage(); |
| 1906 | } |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1907 | if (!uid || !gid) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1908 | ERROR("uid and gid must be nonzero\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1909 | return usage(); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1910 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1911 | if (num_threads < 1) { |
| 1912 | ERROR("number of threads must be at least 1\n"); |
| 1913 | return usage(); |
| 1914 | } |
Jeff Sharkey | 977a9f3 | 2013-08-12 20:23:49 -0700 | [diff] [blame] | 1915 | if (split_perms && derive == DERIVE_NONE) { |
| 1916 | ERROR("cannot split permissions without deriving\n"); |
| 1917 | return usage(); |
| 1918 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1919 | |
Ken Sumrall | 2fd72cc | 2013-02-08 16:50:55 -0800 | [diff] [blame] | 1920 | rlim.rlim_cur = 8192; |
| 1921 | rlim.rlim_max = 8192; |
| 1922 | if (setrlimit(RLIMIT_NOFILE, &rlim)) { |
| 1923 | ERROR("Error setting RLIMIT_NOFILE, errno = %d\n", errno); |
| 1924 | } |
| 1925 | |
Jeff Sharkey | e93a051 | 2013-10-08 10:14:24 -0700 | [diff] [blame] | 1926 | res = run(source_path, dest_path, uid, gid, write_gid, num_threads, derive, split_perms); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1927 | return res < 0 ? 1 : 0; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1928 | } |