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