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