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> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 30 | |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
| 32 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 33 | #include "fuse.h" |
| 34 | |
| 35 | /* README |
| 36 | * |
| 37 | * What is this? |
| 38 | * |
| 39 | * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style |
| 40 | * directory permissions (all files are given fixed owner, group, and |
| 41 | * permissions at creation, owner, group, and permissions are not |
| 42 | * changeable, symlinks and hardlinks are not createable, etc. |
| 43 | * |
| 44 | * usage: sdcard <path> <uid> <gid> |
| 45 | * |
| 46 | * It must be run as root, but will change to uid/gid as soon as it |
Jeff Sharkey | cfa9f65 | 2012-04-09 17:09:38 -0700 | [diff] [blame] | 47 | * mounts a filesystem on /storage/sdcard. It will refuse to run if uid or |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 48 | * gid are zero. |
| 49 | * |
| 50 | * |
| 51 | * Things I believe to be true: |
| 52 | * |
| 53 | * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK, |
| 54 | * CREAT) must bump that node's refcount |
| 55 | * - don't forget that FORGET can forget multiple references (req->nlookup) |
| 56 | * - if an op that returns a fuse_entry fails writing the reply to the |
| 57 | * kernel, you must rollback the refcount to reflect the reference the |
| 58 | * kernel did not actually acquire |
| 59 | * |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | #define FUSE_TRACE 0 |
| 63 | |
| 64 | #if FUSE_TRACE |
| 65 | #define TRACE(x...) fprintf(stderr,x) |
| 66 | #else |
| 67 | #define TRACE(x...) do {} while (0) |
| 68 | #endif |
| 69 | |
| 70 | #define ERROR(x...) fprintf(stderr,x) |
| 71 | |
| 72 | #define FUSE_UNKNOWN_INO 0xffffffff |
| 73 | |
Jeff Sharkey | cfa9f65 | 2012-04-09 17:09:38 -0700 | [diff] [blame] | 74 | #define MOUNT_POINT "/storage/sdcard0" |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 75 | |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 76 | /* Maximum number of bytes to write in one request. */ |
| 77 | #define MAX_WRITE (256 * 1024) |
| 78 | |
| 79 | /* Maximum number of bytes to read in one request. */ |
| 80 | #define MAX_READ (128 * 1024) |
| 81 | |
| 82 | /* Largest possible request. |
| 83 | * The request size is bounded by the maximum size of a FUSE_WRITE request because it has |
| 84 | * the largest possible data payload. */ |
| 85 | #define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE) |
| 86 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 87 | struct handle { |
| 88 | struct node *node; |
| 89 | int fd; |
| 90 | }; |
| 91 | |
| 92 | struct dirhandle { |
| 93 | struct node *node; |
| 94 | DIR *d; |
| 95 | }; |
| 96 | |
| 97 | struct node { |
| 98 | __u64 nid; |
| 99 | __u64 gen; |
| 100 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 101 | struct node *next; /* per-dir sibling list */ |
| 102 | struct node *child; /* first contained file by this dir */ |
| 103 | struct node *all; /* global node list */ |
| 104 | struct node *parent; /* containing directory */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 105 | |
| 106 | __u32 refcount; |
| 107 | __u32 namelen; |
| 108 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 109 | char *name; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 110 | /* If non-null, this is the real name of the file in the underlying storage. |
| 111 | * This may differ from the field "name" only by case. |
| 112 | * strlen(actual_name) will always equal strlen(name), so it is safe to use |
| 113 | * namelen for both fields. |
| 114 | */ |
| 115 | char *actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 118 | /* Global data structure shared by all fuse handlers. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 119 | struct fuse { |
| 120 | __u64 next_generation; |
| 121 | __u64 next_node_id; |
| 122 | |
| 123 | int fd; |
| 124 | |
| 125 | struct node *all; |
| 126 | |
| 127 | struct node root; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 128 | char rootpath[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 131 | /* Private data used by a single fuse handler. */ |
| 132 | struct fuse_handler { |
| 133 | /* To save memory, we never use the contents of the request buffer and the read |
| 134 | * buffer at the same time. This allows us to share the underlying storage. */ |
| 135 | union { |
| 136 | __u8 request_buffer[MAX_REQUEST_SIZE]; |
| 137 | __u8 read_buffer[MAX_READ]; |
| 138 | }; |
| 139 | }; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 140 | |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 141 | #define NO_CASE_SENSITIVE_MATCH 0 |
| 142 | #define CASE_SENSITIVE_MATCH 1 |
Mike Lockwood | b94d320 | 2011-01-17 21:06:26 -0800 | [diff] [blame] | 143 | |
Paul Eastham | f43219e | 2010-09-21 17:14:31 -0700 | [diff] [blame] | 144 | /* |
| 145 | * Get the real-life absolute path to a node. |
| 146 | * node: start at this node |
| 147 | * buf: storage for returned string |
| 148 | * name: append this string to path if set |
| 149 | */ |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 150 | char *do_node_get_path(struct node *node, char *buf, const char *name, int match_case_insensitive) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 151 | { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 152 | struct node *in_node = node; |
| 153 | const char *in_name = name; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 154 | char *out = buf + PATH_MAX - 1; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 155 | int len; |
| 156 | out[0] = 0; |
| 157 | |
| 158 | if (name) { |
| 159 | len = strlen(name); |
| 160 | goto start; |
| 161 | } |
| 162 | |
| 163 | while (node) { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 164 | name = (node->actual_name ? node->actual_name : node->name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 165 | len = node->namelen; |
| 166 | node = node->parent; |
| 167 | start: |
| 168 | if ((len + 1) > (out - buf)) |
| 169 | return 0; |
| 170 | out -= len; |
| 171 | memcpy(out, name, len); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 172 | /* avoid double slash at beginning of path */ |
| 173 | if (out[0] != '/') { |
| 174 | out --; |
| 175 | out[0] = '/'; |
| 176 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 179 | /* If we are searching for a file within node (rather than computing node's path) |
| 180 | * and fail, then we need to look for a case insensitive match. |
| 181 | */ |
| 182 | if (in_name && match_case_insensitive && access(out, F_OK) != 0) { |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 183 | char *path, buffer[PATH_MAX]; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 184 | DIR* dir; |
| 185 | struct dirent* entry; |
| 186 | path = do_node_get_path(in_node, buffer, NULL, NO_CASE_SENSITIVE_MATCH); |
| 187 | dir = opendir(path); |
| 188 | if (!dir) { |
| 189 | ERROR("opendir %s failed: %s", path, strerror(errno)); |
| 190 | return out; |
| 191 | } |
| 192 | |
| 193 | while ((entry = readdir(dir))) { |
| 194 | if (!strcasecmp(entry->d_name, in_name)) { |
| 195 | /* we have a match - replace the name */ |
| 196 | len = strlen(in_name); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 197 | memcpy(buf + PATH_MAX - len - 1, entry->d_name, len); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | } |
| 201 | closedir(dir); |
| 202 | } |
| 203 | |
| 204 | return out; |
| 205 | } |
| 206 | |
| 207 | char *node_get_path(struct node *node, char *buf, const char *name) |
| 208 | { |
| 209 | /* We look for case insensitive matches by default */ |
| 210 | return do_node_get_path(node, buf, name, CASE_SENSITIVE_MATCH); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void attr_from_stat(struct fuse_attr *attr, struct stat *s) |
| 214 | { |
| 215 | attr->ino = s->st_ino; |
| 216 | attr->size = s->st_size; |
| 217 | attr->blocks = s->st_blocks; |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 218 | attr->atime = s->st_atime; |
| 219 | attr->mtime = s->st_mtime; |
| 220 | attr->ctime = s->st_ctime; |
| 221 | attr->atimensec = s->st_atime_nsec; |
| 222 | attr->mtimensec = s->st_mtime_nsec; |
| 223 | attr->ctimensec = s->st_ctime_nsec; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 224 | attr->mode = s->st_mode; |
| 225 | attr->nlink = s->st_nlink; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 226 | |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 227 | /* force permissions to something reasonable: |
| 228 | * world readable |
| 229 | * writable by the sdcard group |
| 230 | */ |
| 231 | if (attr->mode & 0100) { |
| 232 | attr->mode = (attr->mode & (~0777)) | 0775; |
| 233 | } else { |
| 234 | attr->mode = (attr->mode & (~0777)) | 0664; |
| 235 | } |
| 236 | |
| 237 | /* all files owned by root.sdcard */ |
| 238 | attr->uid = 0; |
| 239 | attr->gid = AID_SDCARD_RW; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | int node_get_attr(struct node *node, struct fuse_attr *attr) |
| 243 | { |
| 244 | int res; |
| 245 | struct stat s; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 246 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 247 | |
| 248 | path = node_get_path(node, buffer, 0); |
| 249 | res = lstat(path, &s); |
| 250 | if (res < 0) { |
| 251 | ERROR("lstat('%s') errno %d\n", path, errno); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | attr_from_stat(attr, &s); |
| 256 | attr->ino = node->nid; |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 261 | static void add_node_to_parent(struct node *node, struct node *parent) { |
| 262 | node->parent = parent; |
| 263 | node->next = parent->child; |
| 264 | parent->child = node; |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 265 | parent->refcount++; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 268 | /* Check to see if our parent directory already has a file with a name |
| 269 | * that differs only by case. If we find one, store it in the actual_name |
| 270 | * field so node_get_path will map it to this file in the underlying storage. |
| 271 | */ |
| 272 | static void node_find_actual_name(struct node *node) |
| 273 | { |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 274 | char *path, buffer[PATH_MAX]; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 275 | const char *node_name = node->name; |
| 276 | DIR* dir; |
| 277 | struct dirent* entry; |
| 278 | |
| 279 | if (!node->parent) return; |
| 280 | |
| 281 | path = node_get_path(node->parent, buffer, 0); |
| 282 | dir = opendir(path); |
| 283 | if (!dir) { |
| 284 | ERROR("opendir %s failed: %s", path, strerror(errno)); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | while ((entry = readdir(dir))) { |
| 289 | const char *test_name = entry->d_name; |
| 290 | if (strcmp(test_name, node_name) && !strcasecmp(test_name, node_name)) { |
| 291 | /* we have a match - differs but only by case */ |
| 292 | node->actual_name = strdup(test_name); |
| 293 | if (!node->actual_name) { |
| 294 | ERROR("strdup failed - out of memory\n"); |
| 295 | exit(1); |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | closedir(dir); |
| 301 | } |
| 302 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 303 | struct node *node_create(struct node *parent, const char *name, __u64 nid, __u64 gen) |
| 304 | { |
| 305 | struct node *node; |
| 306 | int namelen = strlen(name); |
| 307 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 308 | node = calloc(1, sizeof(struct node)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 309 | if (node == 0) { |
| 310 | return 0; |
| 311 | } |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 312 | node->name = malloc(namelen + 1); |
| 313 | if (node->name == 0) { |
| 314 | free(node); |
| 315 | return 0; |
| 316 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 317 | |
| 318 | node->nid = nid; |
| 319 | node->gen = gen; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 320 | add_node_to_parent(node, parent); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 321 | memcpy(node->name, name, namelen + 1); |
| 322 | node->namelen = namelen; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 323 | node_find_actual_name(node); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 324 | return node; |
| 325 | } |
| 326 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 327 | static char *rename_node(struct node *node, const char *name) |
| 328 | { |
| 329 | node->namelen = strlen(name); |
| 330 | char *newname = realloc(node->name, node->namelen + 1); |
| 331 | if (newname == 0) |
| 332 | return 0; |
| 333 | node->name = newname; |
| 334 | memcpy(node->name, name, node->namelen + 1); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 335 | node_find_actual_name(node); |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 336 | return node->name; |
| 337 | } |
| 338 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 339 | void fuse_init(struct fuse *fuse, int fd, const char *path) |
| 340 | { |
| 341 | fuse->fd = fd; |
| 342 | fuse->next_node_id = 2; |
| 343 | fuse->next_generation = 0; |
| 344 | |
| 345 | fuse->all = &fuse->root; |
| 346 | |
Terry Heo (Woncheol) | 8349cce | 2011-03-16 13:02:05 +0900 | [diff] [blame] | 347 | memset(&fuse->root, 0, sizeof(fuse->root)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 348 | fuse->root.nid = FUSE_ROOT_ID; /* 1 */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 349 | fuse->root.refcount = 2; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 350 | rename_node(&fuse->root, path); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static inline void *id_to_ptr(__u64 nid) |
| 354 | { |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 355 | return (void *) (uintptr_t) nid; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static inline __u64 ptr_to_id(void *ptr) |
| 359 | { |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 360 | return (__u64) (uintptr_t) ptr; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | struct node *lookup_by_inode(struct fuse *fuse, __u64 nid) |
| 365 | { |
| 366 | if (nid == FUSE_ROOT_ID) { |
| 367 | return &fuse->root; |
| 368 | } else { |
| 369 | return id_to_ptr(nid); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | struct node *lookup_child_by_name(struct node *node, const char *name) |
| 374 | { |
| 375 | for (node = node->child; node; node = node->next) { |
| 376 | if (!strcmp(name, node->name)) { |
| 377 | return node; |
| 378 | } |
| 379 | } |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | struct node *lookup_child_by_inode(struct node *node, __u64 nid) |
| 384 | { |
| 385 | for (node = node->child; node; node = node->next) { |
| 386 | if (node->nid == nid) { |
| 387 | return node; |
| 388 | } |
| 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 393 | static void dec_refcount(struct node *node) { |
| 394 | if (node->refcount > 0) { |
| 395 | node->refcount--; |
| 396 | TRACE("dec_refcount %p(%s) -> %d\n", node, node->name, node->refcount); |
| 397 | } else { |
| 398 | ERROR("Zero refcnt %p\n", node); |
| 399 | } |
| 400 | } |
| 401 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 402 | static struct node *remove_child(struct node *parent, __u64 nid) |
| 403 | { |
| 404 | struct node *prev = 0; |
| 405 | struct node *node; |
| 406 | |
| 407 | for (node = parent->child; node; node = node->next) { |
| 408 | if (node->nid == nid) { |
| 409 | if (prev) { |
| 410 | prev->next = node->next; |
| 411 | } else { |
| 412 | parent->child = node->next; |
| 413 | } |
| 414 | node->next = 0; |
| 415 | node->parent = 0; |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 416 | dec_refcount(parent); |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 417 | return node; |
| 418 | } |
| 419 | prev = node; |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 424 | struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name, |
| 425 | struct fuse_attr *attr) |
| 426 | { |
| 427 | int res; |
| 428 | struct stat s; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 429 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 430 | struct node *node; |
| 431 | |
| 432 | path = node_get_path(parent, buffer, name); |
| 433 | /* XXX error? */ |
| 434 | |
| 435 | res = lstat(path, &s); |
| 436 | if (res < 0) |
| 437 | return 0; |
| 438 | |
| 439 | node = lookup_child_by_name(parent, name); |
| 440 | if (!node) { |
| 441 | node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++); |
| 442 | if (!node) |
| 443 | return 0; |
| 444 | node->nid = ptr_to_id(node); |
| 445 | node->all = fuse->all; |
| 446 | fuse->all = node; |
| 447 | } |
| 448 | |
| 449 | attr_from_stat(attr, &s); |
| 450 | attr->ino = node->nid; |
| 451 | |
| 452 | return node; |
| 453 | } |
| 454 | |
| 455 | void node_release(struct node *node) |
| 456 | { |
| 457 | TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 458 | dec_refcount(node); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 459 | if (node->refcount == 0) { |
| 460 | if (node->parent->child == node) { |
| 461 | node->parent->child = node->parent->child->next; |
| 462 | } else { |
| 463 | struct node *node2; |
| 464 | |
| 465 | node2 = node->parent->child; |
| 466 | while (node2->next != node) |
| 467 | node2 = node2->next; |
| 468 | node2->next = node->next; |
| 469 | } |
| 470 | |
| 471 | TRACE("DESTROY %p (%s)\n", node, node->name); |
| 472 | |
| 473 | node_release(node->parent); |
| 474 | |
| 475 | node->parent = 0; |
| 476 | node->next = 0; |
| 477 | |
| 478 | /* TODO: remove debugging - poison memory */ |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 479 | memset(node->name, 0xef, node->namelen); |
| 480 | free(node->name); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 481 | free(node->actual_name); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 482 | memset(node, 0xfc, sizeof(*node)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 483 | free(node); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void fuse_status(struct fuse *fuse, __u64 unique, int err) |
| 488 | { |
| 489 | struct fuse_out_header hdr; |
| 490 | hdr.len = sizeof(hdr); |
| 491 | hdr.error = err; |
| 492 | hdr.unique = unique; |
| 493 | if (err) { |
| 494 | // ERROR("*** %d ***\n", err); |
| 495 | } |
| 496 | write(fuse->fd, &hdr, sizeof(hdr)); |
| 497 | } |
| 498 | |
| 499 | void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len) |
| 500 | { |
| 501 | struct fuse_out_header hdr; |
| 502 | struct iovec vec[2]; |
| 503 | int res; |
| 504 | |
| 505 | hdr.len = len + sizeof(hdr); |
| 506 | hdr.error = 0; |
| 507 | hdr.unique = unique; |
| 508 | |
| 509 | vec[0].iov_base = &hdr; |
| 510 | vec[0].iov_len = sizeof(hdr); |
| 511 | vec[1].iov_base = data; |
| 512 | vec[1].iov_len = len; |
| 513 | |
| 514 | res = writev(fuse->fd, vec, 2); |
| 515 | if (res < 0) { |
| 516 | ERROR("*** REPLY FAILED *** %d\n", errno); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | void lookup_entry(struct fuse *fuse, struct node *node, |
| 521 | const char *name, __u64 unique) |
| 522 | { |
| 523 | struct fuse_entry_out out; |
| 524 | |
| 525 | memset(&out, 0, sizeof(out)); |
| 526 | |
| 527 | node = node_lookup(fuse, node, name, &out.attr); |
| 528 | if (!node) { |
| 529 | fuse_status(fuse, unique, -ENOENT); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | node->refcount++; |
| 534 | // fprintf(stderr,"ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount); |
| 535 | out.nodeid = node->nid; |
| 536 | out.generation = node->gen; |
| 537 | out.entry_valid = 10; |
| 538 | out.attr_valid = 10; |
| 539 | |
| 540 | fuse_reply(fuse, unique, &out, sizeof(out)); |
| 541 | } |
| 542 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 543 | void handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler, |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 544 | const struct fuse_in_header *hdr, const void *data, size_t data_len) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 545 | { |
| 546 | struct node *node; |
| 547 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 548 | if (hdr->nodeid) { |
| 549 | node = lookup_by_inode(fuse, hdr->nodeid); |
| 550 | if (!node) { |
| 551 | fuse_status(fuse, hdr->unique, -ENOENT); |
| 552 | return; |
| 553 | } |
| 554 | } else { |
| 555 | node = 0; |
| 556 | } |
| 557 | |
| 558 | switch (hdr->opcode) { |
| 559 | case FUSE_LOOKUP: { /* bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 560 | const char* name = data; |
| 561 | TRACE("LOOKUP %llx %s\n", hdr->nodeid, name); |
| 562 | lookup_entry(fuse, node, name, hdr->unique); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 563 | return; |
| 564 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 565 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 566 | case FUSE_FORGET: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 567 | const struct fuse_forget_in *req = data; |
| 568 | __u64 n = req->nlookup; |
| 569 | TRACE("FORGET %llx (%s) #%lld\n", hdr->nodeid, node->name, n); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 570 | /* no reply */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 571 | while (n--) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 572 | node_release(node); |
| 573 | return; |
| 574 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 575 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 576 | case FUSE_GETATTR: { /* getattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 577 | const struct fuse_getattr_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 578 | struct fuse_attr_out out; |
| 579 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 580 | TRACE("GETATTR flags=%x fh=%llx\n", req->getattr_flags, req->fh); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 581 | |
| 582 | memset(&out, 0, sizeof(out)); |
| 583 | node_get_attr(node, &out.attr); |
| 584 | out.attr_valid = 10; |
| 585 | |
| 586 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
| 587 | return; |
| 588 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 589 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 590 | case FUSE_SETATTR: { /* setattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 591 | const struct fuse_setattr_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 592 | struct fuse_attr_out out; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 593 | char *path, buffer[PATH_MAX]; |
Paul Eastham | f43219e | 2010-09-21 17:14:31 -0700 | [diff] [blame] | 594 | int res = 0; |
Ken Sumrall | 9791965 | 2011-03-18 11:53:15 -0700 | [diff] [blame] | 595 | struct timespec times[2]; |
Paul Eastham | f43219e | 2010-09-21 17:14:31 -0700 | [diff] [blame] | 596 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 597 | TRACE("SETATTR fh=%llx id=%llx valid=%x\n", |
| 598 | req->fh, hdr->nodeid, req->valid); |
| 599 | |
Ken Sumrall | 9791965 | 2011-03-18 11:53:15 -0700 | [diff] [blame] | 600 | /* XXX: incomplete implementation on purpose. chmod/chown |
| 601 | * should NEVER be implemented.*/ |
Paul Eastham | f43219e | 2010-09-21 17:14:31 -0700 | [diff] [blame] | 602 | |
| 603 | path = node_get_path(node, buffer, 0); |
| 604 | if (req->valid & FATTR_SIZE) |
| 605 | res = truncate(path, req->size); |
Ken Sumrall | 9791965 | 2011-03-18 11:53:15 -0700 | [diff] [blame] | 606 | if (res) |
| 607 | goto getout; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 608 | |
Ken Sumrall | 9791965 | 2011-03-18 11:53:15 -0700 | [diff] [blame] | 609 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 610 | * are both set, then set it to the current time. Else, set it to the |
| 611 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 612 | * as it allows ATIME and MTIME to be changed independently, and has |
| 613 | * nanosecond resolution which fuse also has. |
| 614 | */ |
| 615 | if (req->valid & (FATTR_ATIME | FATTR_MTIME)) { |
| 616 | times[0].tv_nsec = UTIME_OMIT; |
| 617 | times[1].tv_nsec = UTIME_OMIT; |
| 618 | if (req->valid & FATTR_ATIME) { |
| 619 | if (req->valid & FATTR_ATIME_NOW) { |
| 620 | times[0].tv_nsec = UTIME_NOW; |
| 621 | } else { |
| 622 | times[0].tv_sec = req->atime; |
| 623 | times[0].tv_nsec = req->atimensec; |
| 624 | } |
| 625 | } |
| 626 | if (req->valid & FATTR_MTIME) { |
| 627 | if (req->valid & FATTR_MTIME_NOW) { |
| 628 | times[1].tv_nsec = UTIME_NOW; |
| 629 | } else { |
| 630 | times[1].tv_sec = req->mtime; |
| 631 | times[1].tv_nsec = req->mtimensec; |
| 632 | } |
| 633 | } |
| 634 | TRACE("Calling utimensat on %s with atime %ld, mtime=%ld\n", path, times[0].tv_sec, times[1].tv_sec); |
| 635 | res = utimensat(-1, path, times, 0); |
| 636 | } |
| 637 | |
| 638 | getout: |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 639 | memset(&out, 0, sizeof(out)); |
| 640 | node_get_attr(node, &out.attr); |
| 641 | out.attr_valid = 10; |
Paul Eastham | f43219e | 2010-09-21 17:14:31 -0700 | [diff] [blame] | 642 | |
| 643 | if (res) |
| 644 | fuse_status(fuse, hdr->unique, -errno); |
| 645 | else |
| 646 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 647 | return; |
| 648 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 649 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 650 | // case FUSE_READLINK: |
| 651 | // case FUSE_SYMLINK: |
| 652 | case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 653 | const struct fuse_mknod_in *req = data; |
| 654 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 655 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 656 | int res; |
Mike Lockwood | 51b3a2d | 2011-01-12 07:35:46 -0500 | [diff] [blame] | 657 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 658 | TRACE("MKNOD %s @ %llx\n", name, hdr->nodeid); |
| 659 | path = node_get_path(node, buffer, name); |
| 660 | |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 661 | __u32 mode = (req->mode & (~0777)) | 0664; |
| 662 | res = mknod(path, mode, req->rdev); /* XXX perm?*/ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 663 | if (res < 0) { |
| 664 | fuse_status(fuse, hdr->unique, -errno); |
| 665 | } else { |
| 666 | lookup_entry(fuse, node, name, hdr->unique); |
| 667 | } |
| 668 | return; |
| 669 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 670 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 671 | case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 672 | const struct fuse_mkdir_in *req = data; |
| 673 | const char *name = ((const char*) data) + sizeof(*req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 674 | struct fuse_entry_out out; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 675 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 676 | int res; |
Mike Lockwood | 51b3a2d | 2011-01-12 07:35:46 -0500 | [diff] [blame] | 677 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 678 | TRACE("MKDIR %s @ %llx 0%o\n", name, hdr->nodeid, req->mode); |
| 679 | path = node_get_path(node, buffer, name); |
| 680 | |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 681 | __u32 mode = (req->mode & (~0777)) | 0775; |
| 682 | res = mkdir(path, mode); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 683 | if (res < 0) { |
| 684 | fuse_status(fuse, hdr->unique, -errno); |
| 685 | } else { |
| 686 | lookup_entry(fuse, node, name, hdr->unique); |
| 687 | } |
| 688 | return; |
| 689 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 690 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 691 | case FUSE_UNLINK: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 692 | const char* name = data; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 693 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 694 | int res; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 695 | TRACE("UNLINK %s @ %llx\n", name, hdr->nodeid); |
| 696 | path = node_get_path(node, buffer, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 697 | res = unlink(path); |
| 698 | fuse_status(fuse, hdr->unique, res ? -errno : 0); |
| 699 | return; |
| 700 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 701 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 702 | case FUSE_RMDIR: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 703 | const char* name = data; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 704 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 705 | int res; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 706 | TRACE("RMDIR %s @ %llx\n", name, hdr->nodeid); |
| 707 | path = node_get_path(node, buffer, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 708 | res = rmdir(path); |
| 709 | fuse_status(fuse, hdr->unique, res ? -errno : 0); |
| 710 | return; |
| 711 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 712 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 713 | case FUSE_RENAME: { /* rename_in, oldname, newname -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 714 | const struct fuse_rename_in *req = data; |
| 715 | const char *oldname = ((const char*) data) + sizeof(*req); |
| 716 | const char *newname = oldname + strlen(oldname) + 1; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 717 | char *oldpath, oldbuffer[PATH_MAX]; |
| 718 | char *newpath, newbuffer[PATH_MAX]; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 719 | struct node *target; |
| 720 | struct node *newparent; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 721 | int res; |
| 722 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 723 | TRACE("RENAME %s->%s @ %llx\n", oldname, newname, hdr->nodeid); |
| 724 | |
| 725 | target = lookup_child_by_name(node, oldname); |
| 726 | if (!target) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 727 | fuse_status(fuse, hdr->unique, -ENOENT); |
| 728 | return; |
| 729 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 730 | oldpath = node_get_path(node, oldbuffer, oldname); |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 731 | |
| 732 | newparent = lookup_by_inode(fuse, req->newdir); |
| 733 | if (!newparent) { |
| 734 | fuse_status(fuse, hdr->unique, -ENOENT); |
| 735 | return; |
| 736 | } |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 737 | if (newparent == node) { |
| 738 | /* Special case for renaming a file where destination |
| 739 | * is same path differing only by case. |
| 740 | * In this case we don't want to look for a case insensitive match. |
| 741 | * This allows commands like "mv foo FOO" to work as expected. |
| 742 | */ |
| 743 | newpath = do_node_get_path(newparent, newbuffer, newname, NO_CASE_SENSITIVE_MATCH); |
| 744 | } else { |
| 745 | newpath = node_get_path(newparent, newbuffer, newname); |
| 746 | } |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 747 | |
| 748 | if (!remove_child(node, target->nid)) { |
| 749 | ERROR("RENAME remove_child not found"); |
| 750 | fuse_status(fuse, hdr->unique, -ENOENT); |
| 751 | return; |
| 752 | } |
| 753 | if (!rename_node(target, newname)) { |
| 754 | fuse_status(fuse, hdr->unique, -ENOMEM); |
| 755 | return; |
| 756 | } |
| 757 | add_node_to_parent(target, newparent); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 758 | |
| 759 | res = rename(oldpath, newpath); |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 760 | TRACE("RENAME result %d\n", res); |
| 761 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 762 | fuse_status(fuse, hdr->unique, res ? -errno : 0); |
| 763 | return; |
| 764 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 765 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 766 | // case FUSE_LINK: |
| 767 | case FUSE_OPEN: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 768 | const struct fuse_open_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 769 | struct fuse_open_out out; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 770 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 771 | struct handle *h; |
| 772 | |
| 773 | h = malloc(sizeof(*h)); |
| 774 | if (!h) { |
| 775 | fuse_status(fuse, hdr->unique, -ENOMEM); |
| 776 | return; |
| 777 | } |
| 778 | |
| 779 | path = node_get_path(node, buffer, 0); |
| 780 | TRACE("OPEN %llx '%s' 0%o fh=%p\n", hdr->nodeid, path, req->flags, h); |
| 781 | h->fd = open(path, req->flags); |
| 782 | if (h->fd < 0) { |
| 783 | ERROR("ERROR\n"); |
Yuncheol Heo | 2fc9fc7 | 2011-07-22 17:42:22 +0900 | [diff] [blame] | 784 | fuse_status(fuse, hdr->unique, -errno); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 785 | free(h); |
| 786 | return; |
| 787 | } |
| 788 | out.fh = ptr_to_id(h); |
| 789 | out.open_flags = 0; |
| 790 | out.padding = 0; |
| 791 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
| 792 | return; |
| 793 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 794 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 795 | case FUSE_READ: { /* read_in -> byte[] */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 796 | const struct fuse_read_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 797 | struct handle *h = id_to_ptr(req->fh); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 798 | __u64 unique = hdr->unique; |
| 799 | __u32 size = req->size; |
| 800 | __u64 offset = req->offset; |
| 801 | /* Don't access any other fields of hdr or req beyond this point, the read buffer |
| 802 | * overlaps the request buffer and will clobber data in the request. This |
| 803 | * saves us 128KB per request handler thread at the cost of this scary comment. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 804 | int res; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 805 | TRACE("READ %p(%d) %u@%llu\n", h, h->fd, size, offset); |
| 806 | if (size > sizeof(handler->read_buffer)) { |
| 807 | fuse_status(fuse, unique, -EINVAL); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 808 | return; |
| 809 | } |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 810 | res = pread64(h->fd, handler->read_buffer, size, offset); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 811 | if (res < 0) { |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 812 | fuse_status(fuse, unique, -errno); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 813 | return; |
| 814 | } |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 815 | fuse_reply(fuse, unique, handler->read_buffer, res); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 816 | return; |
| 817 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 818 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 819 | case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 820 | const struct fuse_write_in *req = data; |
| 821 | const void* buffer = (const __u8*)data + sizeof(*req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 822 | struct fuse_write_out out; |
| 823 | struct handle *h = id_to_ptr(req->fh); |
| 824 | int res; |
| 825 | TRACE("WRITE %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 826 | res = pwrite64(h->fd, buffer, req->size, req->offset); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 827 | if (res < 0) { |
Yuncheol Heo | 2fc9fc7 | 2011-07-22 17:42:22 +0900 | [diff] [blame] | 828 | fuse_status(fuse, hdr->unique, -errno); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | out.size = res; |
| 832 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 833 | return; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 834 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 835 | |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 836 | case FUSE_STATFS: { /* getattr_in -> attr_out */ |
| 837 | struct statfs stat; |
| 838 | struct fuse_statfs_out out; |
| 839 | int res; |
| 840 | |
| 841 | TRACE("STATFS\n"); |
| 842 | |
| 843 | if (statfs(fuse->root.name, &stat)) { |
| 844 | fuse_status(fuse, hdr->unique, -errno); |
| 845 | return; |
| 846 | } |
| 847 | |
| 848 | memset(&out, 0, sizeof(out)); |
| 849 | out.st.blocks = stat.f_blocks; |
| 850 | out.st.bfree = stat.f_bfree; |
| 851 | out.st.bavail = stat.f_bavail; |
| 852 | out.st.files = stat.f_files; |
| 853 | out.st.ffree = stat.f_ffree; |
| 854 | out.st.bsize = stat.f_bsize; |
| 855 | out.st.namelen = stat.f_namelen; |
| 856 | out.st.frsize = stat.f_frsize; |
| 857 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
| 858 | return; |
| 859 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 860 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 861 | case FUSE_RELEASE: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 862 | const struct fuse_release_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 863 | struct handle *h = id_to_ptr(req->fh); |
| 864 | TRACE("RELEASE %p(%d)\n", h, h->fd); |
| 865 | close(h->fd); |
| 866 | free(h); |
| 867 | fuse_status(fuse, hdr->unique, 0); |
| 868 | return; |
| 869 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 870 | |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 871 | case FUSE_FSYNC: { |
| 872 | const struct fuse_fsync_in *req = data; |
| 873 | int is_data_sync = req->fsync_flags & 1; |
| 874 | struct handle *h = id_to_ptr(req->fh); |
| 875 | int res; |
| 876 | TRACE("FSYNC %p(%d) is_data_sync=%d\n", h, h->fd, is_data_sync); |
| 877 | res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd); |
| 878 | if (res < 0) { |
| 879 | fuse_status(fuse, hdr->unique, -errno); |
| 880 | return; |
| 881 | } |
| 882 | fuse_status(fuse, hdr->unique, 0); |
| 883 | return; |
| 884 | } |
| 885 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 886 | // case FUSE_SETXATTR: |
| 887 | // case FUSE_GETXATTR: |
| 888 | // case FUSE_LISTXATTR: |
| 889 | // case FUSE_REMOVEXATTR: |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 890 | case FUSE_FLUSH: { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 891 | fuse_status(fuse, hdr->unique, 0); |
| 892 | return; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 895 | case FUSE_OPENDIR: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 896 | const struct fuse_open_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 897 | struct fuse_open_out out; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 898 | char *path, buffer[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 899 | struct dirhandle *h; |
| 900 | |
| 901 | h = malloc(sizeof(*h)); |
| 902 | if (!h) { |
| 903 | fuse_status(fuse, hdr->unique, -ENOMEM); |
| 904 | return; |
| 905 | } |
| 906 | |
| 907 | path = node_get_path(node, buffer, 0); |
| 908 | TRACE("OPENDIR %llx '%s'\n", hdr->nodeid, path); |
| 909 | h->d = opendir(path); |
| 910 | if (h->d == 0) { |
| 911 | ERROR("ERROR\n"); |
| 912 | fuse_status(fuse, hdr->unique, -errno); |
| 913 | free(h); |
| 914 | return; |
| 915 | } |
| 916 | out.fh = ptr_to_id(h); |
| 917 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
| 918 | return; |
| 919 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 920 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 921 | case FUSE_READDIR: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 922 | const struct fuse_read_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 923 | char buffer[8192]; |
| 924 | struct fuse_dirent *fde = (struct fuse_dirent*) buffer; |
| 925 | struct dirent *de; |
| 926 | struct dirhandle *h = id_to_ptr(req->fh); |
| 927 | TRACE("READDIR %p\n", h); |
Mike Lockwood | 75e17a8 | 2011-01-25 17:22:47 -0800 | [diff] [blame] | 928 | if (req->offset == 0) { |
| 929 | /* rewinddir() might have been called above us, so rewind here too */ |
| 930 | TRACE("calling rewinddir()\n"); |
| 931 | rewinddir(h->d); |
| 932 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 933 | de = readdir(h->d); |
| 934 | if (!de) { |
| 935 | fuse_status(fuse, hdr->unique, 0); |
| 936 | return; |
| 937 | } |
| 938 | fde->ino = FUSE_UNKNOWN_INO; |
Mike Lockwood | 75e17a8 | 2011-01-25 17:22:47 -0800 | [diff] [blame] | 939 | /* increment the offset so we can detect when rewinddir() seeks back to the beginning */ |
| 940 | fde->off = req->offset + 1; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 941 | fde->type = de->d_type; |
| 942 | fde->namelen = strlen(de->d_name); |
| 943 | memcpy(fde->name, de->d_name, fde->namelen + 1); |
| 944 | fuse_reply(fuse, hdr->unique, fde, |
| 945 | FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); |
| 946 | return; |
| 947 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 948 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 949 | case FUSE_RELEASEDIR: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 950 | const struct fuse_release_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 951 | struct dirhandle *h = id_to_ptr(req->fh); |
| 952 | TRACE("RELEASEDIR %p\n",h); |
| 953 | closedir(h->d); |
| 954 | free(h); |
| 955 | fuse_status(fuse, hdr->unique, 0); |
| 956 | return; |
| 957 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 958 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 959 | // case FUSE_FSYNCDIR: |
| 960 | case FUSE_INIT: { /* init_in -> init_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 961 | const struct fuse_init_in *req = data; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 962 | struct fuse_init_out out; |
| 963 | |
| 964 | TRACE("INIT ver=%d.%d maxread=%d flags=%x\n", |
| 965 | req->major, req->minor, req->max_readahead, req->flags); |
| 966 | |
| 967 | out.major = FUSE_KERNEL_VERSION; |
| 968 | out.minor = FUSE_KERNEL_MINOR_VERSION; |
| 969 | out.max_readahead = req->max_readahead; |
Sundar Raman | e5d3212 | 2012-02-09 10:31:10 -0600 | [diff] [blame] | 970 | out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 971 | out.max_background = 32; |
| 972 | out.congestion_threshold = 32; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 973 | out.max_write = MAX_WRITE; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 974 | |
| 975 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
| 976 | return; |
| 977 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 978 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 979 | default: { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 980 | ERROR("NOTIMPL op=%d uniq=%llx nid=%llx\n", |
| 981 | hdr->opcode, hdr->unique, hdr->nodeid); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 982 | fuse_status(fuse, hdr->unique, -ENOSYS); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 983 | break; |
| 984 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 985 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 988 | void handle_fuse_requests(struct fuse *fuse, struct fuse_handler* handler) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 989 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 990 | for (;;) { |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 991 | ssize_t len = read(fuse->fd, handler->request_buffer, sizeof(handler->request_buffer)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 992 | if (len < 0) { |
| 993 | if (errno == EINTR) |
| 994 | continue; |
| 995 | ERROR("handle_fuse_requests: errno=%d\n", errno); |
| 996 | return; |
| 997 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 998 | |
| 999 | if ((size_t)len < sizeof(struct fuse_in_header)) { |
| 1000 | ERROR("request too short: len=%zu\n", (size_t)len); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 1004 | const struct fuse_in_header *hdr = (void*)handler->request_buffer; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1005 | if (hdr->len != (size_t)len) { |
| 1006 | ERROR("malformed header: len=%zu, hdr->len=%u\n", (size_t)len, hdr->len); |
| 1007 | return; |
| 1008 | } |
| 1009 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 1010 | const void *data = handler->request_buffer + sizeof(struct fuse_in_header); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1011 | size_t data_len = len - sizeof(struct fuse_in_header); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 1012 | handle_fuse_request(fuse, handler, hdr, data, data_len); |
| 1013 | |
| 1014 | /* We do not access the request again after this point because the underlying |
| 1015 | * buffer storage may have been reused while processing the request. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 1019 | int ignite_fuse(struct fuse* fuse) |
| 1020 | { |
| 1021 | /* use only one handler thread for now */ |
| 1022 | struct fuse_handler handler; |
| 1023 | handle_fuse_requests(fuse, &handler); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1027 | static int usage() |
| 1028 | { |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1029 | ERROR("usage: sdcard <path> <uid> <gid>\n\n"); |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
| 1033 | static int run(const char* path, uid_t uid, gid_t gid) |
| 1034 | { |
| 1035 | int fd; |
| 1036 | char opts[256]; |
| 1037 | int res; |
| 1038 | struct fuse fuse; |
| 1039 | |
| 1040 | /* cleanup from previous instance, if necessary */ |
| 1041 | umount2(MOUNT_POINT, 2); |
| 1042 | |
| 1043 | fd = open("/dev/fuse", O_RDWR); |
| 1044 | if (fd < 0){ |
| 1045 | ERROR("cannot open fuse device (error %d)\n", errno); |
| 1046 | return -1; |
| 1047 | } |
| 1048 | |
| 1049 | snprintf(opts, sizeof(opts), |
| 1050 | "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d", |
| 1051 | fd, uid, gid); |
| 1052 | |
| 1053 | res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts); |
| 1054 | if (res < 0) { |
| 1055 | ERROR("cannot mount fuse filesystem (error %d)\n", errno); |
| 1056 | goto error; |
| 1057 | } |
| 1058 | |
| 1059 | res = setgid(gid); |
| 1060 | if (res < 0) { |
| 1061 | ERROR("cannot setgid (error %d)\n", errno); |
| 1062 | goto error; |
| 1063 | } |
| 1064 | |
| 1065 | res = setuid(uid); |
| 1066 | if (res < 0) { |
| 1067 | ERROR("cannot setuid (error %d)\n", errno); |
| 1068 | goto error; |
| 1069 | } |
| 1070 | |
| 1071 | fuse_init(&fuse, fd, path); |
| 1072 | |
| 1073 | umask(0); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame^] | 1074 | res = ignite_fuse(&fuse); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1075 | |
| 1076 | /* we do not attempt to umount the file system here because we are no longer |
| 1077 | * running as the root user */ |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1078 | |
| 1079 | error: |
| 1080 | close(fd); |
| 1081 | return res; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1082 | } |
| 1083 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1084 | int main(int argc, char **argv) |
| 1085 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1086 | int fd; |
| 1087 | int res; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1088 | const char *path = NULL; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1089 | uid_t uid = 0; |
| 1090 | gid_t gid = 0; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1091 | int i; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1092 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1093 | for (i = 1; i < argc; i++) { |
| 1094 | char* arg = argv[i]; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1095 | if (!path) |
| 1096 | path = arg; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1097 | else if (!uid) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1098 | uid = strtoul(arg, 0, 10); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1099 | else if (!gid) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1100 | gid = strtoul(arg, 0, 10); |
| 1101 | else { |
| 1102 | ERROR("too many arguments\n"); |
| 1103 | return usage(); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1104 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1107 | if (!path) { |
| 1108 | ERROR("no path specified\n"); |
| 1109 | return usage(); |
| 1110 | } |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1111 | if (!uid || !gid) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1112 | ERROR("uid and gid must be nonzero\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1113 | return usage(); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1116 | res = run(path, uid, gid); |
| 1117 | return res < 0 ? 1 : 0; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1118 | } |