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