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