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