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