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