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