19685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey/*
29685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Copyright (C) 2012 The Android Open Source Project
39685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey *
49685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
59685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * you may not use this file except in compliance with the License.
69685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * You may obtain a copy of the License at
79685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey *
89685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
99685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey *
109685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
119685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
129685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * See the License for the specific language governing permissions and
149685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey * limitations under the License.
159685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey */
169685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
17ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey#define LOG_TAG "cutils"
18ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey
1944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey/* These defines are only needed because prebuilt headers are out of date */
2044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey#define __USE_XOPEN2K8 1
2144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey#define _ATFILE_SOURCE 1
2244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey#define _GNU_SOURCE 1
2344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
249685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <cutils/fs.h>
259685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <cutils/log.h>
269685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
279685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <sys/types.h>
289685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <sys/stat.h>
299685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <fcntl.h>
309685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <unistd.h>
319685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <errno.h>
329685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <string.h>
339685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <limits.h>
3469ce489fc3946b7b89ca837a01d3828f1af2e0a0Nick Kralevich#include <stdlib.h>
3544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey#include <dirent.h>
369685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
379685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
389685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#define BUF_SIZE 64
399685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
409685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyint fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
419685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    // Check if path needs to be created
429685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    struct stat sb;
43ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey    if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
449685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        if (errno == ENOENT) {
459685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey            goto create;
469685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        } else {
47ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey            ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
489685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey            return -1;
499685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        }
509685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
519685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
529685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    // Exists, verify status
539685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (!S_ISDIR(sb.st_mode)) {
549685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Not a directory: %s", path);
559685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
569685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
579685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (((sb.st_mode & ALL_PERMS) == mode) && (sb.st_uid == uid) && (sb.st_gid == gid)) {
589685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return 0;
599685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    } else {
609685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fixup;
619685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
629685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
639685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeycreate:
64ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey    if (TEMP_FAILURE_RETRY(mkdir(path, mode)) == -1) {
65489609bb44fe8834c76c772f2cff8f03dbb84e08Jeff Sharkey        if (errno != EEXIST) {
66489609bb44fe8834c76c772f2cff8f03dbb84e08Jeff Sharkey            ALOGE("Failed to mkdir(%s): %s", path, strerror(errno));
67489609bb44fe8834c76c772f2cff8f03dbb84e08Jeff Sharkey            return -1;
68489609bb44fe8834c76c772f2cff8f03dbb84e08Jeff Sharkey        }
699685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
709685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
719685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfixup:
72ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey    if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
73ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey        ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
749685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
759685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
76ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey    if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
779685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
789685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
799685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
809685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
819685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return 0;
829685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey}
839685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
849685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyint fs_read_atomic_int(const char* path, int* out_value) {
856de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
869685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (fd == -1) {
879685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to read %s: %s", path, strerror(errno));
889685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
899685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
909685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
919685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    char buf[BUF_SIZE];
926de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
939685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to read %s: %s", path, strerror(errno));
949685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
959685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
969685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (sscanf(buf, "%d", out_value) != 1) {
979685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to parse %s: %s", path, strerror(errno));
989685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
999685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1009685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    close(fd);
1019685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return 0;
1029685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1039685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfail:
1049685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    close(fd);
1059685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    *out_value = -1;
1069685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return -1;
1079685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey}
1089685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1099685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyint fs_write_atomic_int(const char* path, int value) {
1109685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    char temp[PATH_MAX];
1119685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
1129685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Path too long");
1139685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
1149685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1159685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1166de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
1179685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (fd == -1) {
1189685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to open %s: %s", temp, strerror(errno));
1199685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
1209685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1219685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1229685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    char buf[BUF_SIZE];
1239685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
1249685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (len > BUF_SIZE) {
1259685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Value %d too large: %s", value, strerror(errno));
1269685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
1279685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1286de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
1299685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to write %s: %s", temp, strerror(errno));
1309685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
1319685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1329685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (close(fd) == -1) {
1339685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to close %s: %s", temp, strerror(errno));
1349685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail_closed;
1359685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1369685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1379685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (rename(temp, path) == -1) {
1389685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
1399685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail_closed;
1409685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1419685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1429685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return 0;
1439685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1449685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfail:
1459685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    close(fd);
1469685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfail_closed:
1479685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    unlink(temp);
1489685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return -1;
1499685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey}
15044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
1510ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey#ifndef __APPLE__
1520ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey
15344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkeyint fs_mkdirs(const char* path, mode_t mode) {
15444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    int res = 0;
15544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    int fd = 0;
15644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    struct stat sb;
15744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    char* buf = strdup(path);
15844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
15944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    if (*buf != '/') {
16044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        ALOGE("Relative paths are not allowed: %s", buf);
16144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        res = -EINVAL;
16244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        goto done;
16344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    }
16444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
16544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    if ((fd = open("/", 0)) == -1) {
16644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        ALOGE("Failed to open(/): %s", strerror(errno));
16744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        res = -errno;
16844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        goto done;
16944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    }
17044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
17144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    char* segment = buf + 1;
17244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    char* p = segment;
17344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    while (*p != '\0') {
17444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        if (*p == '/') {
17544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            *p = '\0';
17644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
17744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            if (!strcmp(segment, "..") || !strcmp(segment, ".") || !strcmp(segment, "")) {
17844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                ALOGE("Invalid path: %s", buf);
17944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                res = -EINVAL;
18044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                goto done_close;
18144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            }
18244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
18344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            if (fstatat(fd, segment, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
18444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                if (errno == ENOENT) {
18544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    /* Nothing there yet; let's create it! */
18644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    if (mkdirat(fd, segment, mode) != 0) {
18744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                        if (errno == EEXIST) {
18844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            /* We raced with someone; ignore */
18944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                        } else {
19044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            ALOGE("Failed to mkdirat(%s): %s", buf, strerror(errno));
19144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            res = -errno;
19244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            goto done_close;
19344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                        }
19444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    }
19544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                } else {
19644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    ALOGE("Failed to fstatat(%s): %s", buf, strerror(errno));
19744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    res = -errno;
19844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    goto done_close;
19944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                }
20044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            } else {
20144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                if (S_ISLNK(sb.st_mode)) {
20244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    ALOGE("Symbolic links are not allowed: %s", buf);
20344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    res = -ELOOP;
20444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    goto done_close;
20544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                }
20644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                if (!S_ISDIR(sb.st_mode)) {
20744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    ALOGE("Existing segment not a directory: %s", buf);
20844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    res = -ENOTDIR;
20944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    goto done_close;
21044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                }
21144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            }
21244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
21344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            /* Yay, segment is ready for us to step into */
21444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            int next_fd;
21530a86ebc7a0f5a2e0fc698f432ee626cd96525b8Nick Kralevich            if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) {
21644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                ALOGE("Failed to openat(%s): %s", buf, strerror(errno));
21744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                res = -errno;
21844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                goto done_close;
21944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            }
22044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
22144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            close(fd);
22244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            fd = next_fd;
22344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
22444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            *p = '/';
22544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            segment = p + 1;
22644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        }
22744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        p++;
22844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    }
22944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
23044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkeydone_close:
23144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    close(fd);
23244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkeydone:
23344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    free(buf);
23444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    return res;
23544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey}
2360ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey
2370ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey#endif
238