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