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
2423ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn#include <dirent.h>
259685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <errno.h>
2623ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn#include <fcntl.h>
279685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#include <limits.h>
28cfd5b080af8de527d768f0ff7902c26af8d49307Mark Salyzyn#include <stdio.h>
2969ce489fc3946b7b89ca837a01d3828f1af2e0a0Nick Kralevich#include <stdlib.h>
3023ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn#include <string.h>
3123ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn#include <sys/stat.h>
3223ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn#include <sys/types.h>
3323ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn#include <unistd.h>
3423ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn
3523ed4c242a01052696dde84babd7631a7ec5a691Mark Salyzyn#include <cutils/fs.h>
3630f991f251940be3ed11566fb71139852286f68aMark Salyzyn#include <log/log.h>
379685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
389685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
399685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey#define BUF_SIZE 64
409685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
419812105b20a19354d9bd6f912da707203d62b397Calin Juravlestatic int fs_prepare_path_impl(const char* path, mode_t mode, uid_t uid, gid_t gid,
429812105b20a19354d9bd6f912da707203d62b397Calin Juravle        int allow_fixup, int prepare_as_dir) {
439685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    // Check if path needs to be created
449685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    struct stat sb;
459812105b20a19354d9bd6f912da707203d62b397Calin Juravle    int create_result = -1;
46ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey    if (TEMP_FAILURE_RETRY(lstat(path, &sb)) == -1) {
479685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        if (errno == ENOENT) {
489685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey            goto create;
499685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        } else {
50ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey            ALOGE("Failed to lstat(%s): %s", path, strerror(errno));
519685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey            return -1;
529685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        }
539685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
549685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
559685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    // Exists, verify status
569812105b20a19354d9bd6f912da707203d62b397Calin Juravle    int type_ok = prepare_as_dir ? S_ISDIR(sb.st_mode) : S_ISREG(sb.st_mode);
579812105b20a19354d9bd6f912da707203d62b397Calin Juravle    if (!type_ok) {
589812105b20a19354d9bd6f912da707203d62b397Calin Juravle        ALOGE("Not a %s: %s", (prepare_as_dir ? "directory" : "regular file"), path);
599685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
609685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
619812105b20a19354d9bd6f912da707203d62b397Calin Juravle
62814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey    int owner_match = ((sb.st_uid == uid) && (sb.st_gid == gid));
63814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey    int mode_match = ((sb.st_mode & ALL_PERMS) == mode);
64814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey    if (owner_match && mode_match) {
659685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return 0;
66cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkey    } else if (allow_fixup) {
679685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fixup;
68cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkey    } else {
69814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey        if (!owner_match) {
70814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey            ALOGE("Expected path %s with owner %d:%d but found %d:%d",
71814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey                    path, uid, gid, sb.st_uid, sb.st_gid);
72814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey            return -1;
73814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey        } else {
74814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey            ALOGW("Expected path %s with mode %o but found %o",
75814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey                    path, mode, (sb.st_mode & ALL_PERMS));
76814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey            return 0;
77814640315a91ec9de43a34ef6b360540edbfb092Jeff Sharkey        }
789685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
799685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
809685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeycreate:
819812105b20a19354d9bd6f912da707203d62b397Calin Juravle    create_result = prepare_as_dir
829812105b20a19354d9bd6f912da707203d62b397Calin Juravle        ? TEMP_FAILURE_RETRY(mkdir(path, mode))
8309175a0264d86b6e0cae9324cd95aef0711ae560George Burgess IV        : TEMP_FAILURE_RETRY(open(path, O_CREAT | O_CLOEXEC | O_NOFOLLOW | O_RDONLY, 0644));
849812105b20a19354d9bd6f912da707203d62b397Calin Juravle    if (create_result == -1) {
85489609bb44fe8834c76c772f2cff8f03dbb84e08Jeff Sharkey        if (errno != EEXIST) {
869812105b20a19354d9bd6f912da707203d62b397Calin Juravle            ALOGE("Failed to %s(%s): %s",
879812105b20a19354d9bd6f912da707203d62b397Calin Juravle                    (prepare_as_dir ? "mkdir" : "open"), path, strerror(errno));
88489609bb44fe8834c76c772f2cff8f03dbb84e08Jeff Sharkey            return -1;
89489609bb44fe8834c76c772f2cff8f03dbb84e08Jeff Sharkey        }
909812105b20a19354d9bd6f912da707203d62b397Calin Juravle    } else if (!prepare_as_dir) {
919812105b20a19354d9bd6f912da707203d62b397Calin Juravle        // For regular files we need to make sure we close the descriptor
929812105b20a19354d9bd6f912da707203d62b397Calin Juravle        if (close(create_result) == -1) {
939812105b20a19354d9bd6f912da707203d62b397Calin Juravle            ALOGW("Failed to close file after create %s: %s", path, strerror(errno));
949812105b20a19354d9bd6f912da707203d62b397Calin Juravle        }
959685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
969685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfixup:
97ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey    if (TEMP_FAILURE_RETRY(chmod(path, mode)) == -1) {
98ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey        ALOGE("Failed to chmod(%s, %d): %s", path, mode, strerror(errno));
999685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
1009685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
101ddb173394430a7b55b0c24896a843556f5f8de7aJeff Sharkey    if (TEMP_FAILURE_RETRY(chown(path, uid, gid)) == -1) {
1029685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to chown(%s, %d, %d): %s", path, uid, gid, strerror(errno));
1039685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
1049685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1059685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1069685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return 0;
1079685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey}
1089685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
109cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkeyint fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid) {
1109812105b20a19354d9bd6f912da707203d62b397Calin Juravle    return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 1, /*prepare_as_dir*/ 1);
111cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkey}
112cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkey
113cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkeyint fs_prepare_dir_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) {
1149812105b20a19354d9bd6f912da707203d62b397Calin Juravle    return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 0, /*prepare_as_dir*/ 1);
1159812105b20a19354d9bd6f912da707203d62b397Calin Juravle}
1169812105b20a19354d9bd6f912da707203d62b397Calin Juravle
1179812105b20a19354d9bd6f912da707203d62b397Calin Juravleint fs_prepare_file_strict(const char* path, mode_t mode, uid_t uid, gid_t gid) {
1189812105b20a19354d9bd6f912da707203d62b397Calin Juravle    return fs_prepare_path_impl(path, mode, uid, gid, /*allow_fixup*/ 0, /*prepare_as_dir*/ 0);
119cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkey}
120cf94fe152e220110d606dcbe2f8a2bd9da0546bcJeff Sharkey
1219685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyint fs_read_atomic_int(const char* path, int* out_value) {
1226de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY));
1239685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (fd == -1) {
1249685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to read %s: %s", path, strerror(errno));
1259685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
1269685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1279685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1289685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    char buf[BUF_SIZE];
1296de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    if (TEMP_FAILURE_RETRY(read(fd, buf, BUF_SIZE)) == -1) {
1309685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to read %s: %s", path, strerror(errno));
1319685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
1329685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1339685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (sscanf(buf, "%d", out_value) != 1) {
1349685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to parse %s: %s", path, strerror(errno));
1359685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
1369685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1379685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    close(fd);
1389685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return 0;
1399685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1409685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfail:
1419685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    close(fd);
1429685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    *out_value = -1;
1439685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return -1;
1449685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey}
1459685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1469685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyint fs_write_atomic_int(const char* path, int value) {
1479685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    char temp[PATH_MAX];
1489685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (snprintf(temp, PATH_MAX, "%s.XXXXXX", path) >= PATH_MAX) {
1499685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Path too long");
1509685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
1519685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1529685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1536de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    int fd = TEMP_FAILURE_RETRY(mkstemp(temp));
1549685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (fd == -1) {
1559685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to open %s: %s", temp, strerror(errno));
1569685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        return -1;
1579685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1589685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1599685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    char buf[BUF_SIZE];
1609685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    int len = snprintf(buf, BUF_SIZE, "%d", value) + 1;
1619685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (len > BUF_SIZE) {
1629685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Value %d too large: %s", value, strerror(errno));
1639685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
1649685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1656de702679089fab13ec2d4d435054f68688df545Jeff Sharkey    if (TEMP_FAILURE_RETRY(write(fd, buf, len)) < len) {
1669685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to write %s: %s", temp, strerror(errno));
1679685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail;
1689685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1699685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (close(fd) == -1) {
1709685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to close %s: %s", temp, strerror(errno));
1719685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail_closed;
1729685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1739685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1749685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    if (rename(temp, path) == -1) {
1759685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        ALOGE("Failed to rename %s to %s: %s", temp, path, strerror(errno));
1769685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey        goto fail_closed;
1779685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    }
1789685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1799685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return 0;
1809685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey
1819685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfail:
1829685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    close(fd);
1839685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkeyfail_closed:
1849685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    unlink(temp);
1859685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey    return -1;
1869685194fc94510a33201aee9b80c23f206ccfe67Jeff Sharkey}
18744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
1880ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey#ifndef __APPLE__
1890ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey
19044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkeyint fs_mkdirs(const char* path, mode_t mode) {
19144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    int res = 0;
19244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    int fd = 0;
19344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    struct stat sb;
19444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    char* buf = strdup(path);
19544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
19644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    if (*buf != '/') {
19744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        ALOGE("Relative paths are not allowed: %s", buf);
19844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        res = -EINVAL;
19944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        goto done;
20044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    }
20144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
20244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    if ((fd = open("/", 0)) == -1) {
20344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        ALOGE("Failed to open(/): %s", strerror(errno));
20444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        res = -errno;
20544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        goto done;
20644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    }
20744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
20844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    char* segment = buf + 1;
20944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    char* p = segment;
21044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    while (*p != '\0') {
21144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        if (*p == '/') {
21244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            *p = '\0';
21344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
21444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            if (!strcmp(segment, "..") || !strcmp(segment, ".") || !strcmp(segment, "")) {
21544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                ALOGE("Invalid path: %s", buf);
21644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                res = -EINVAL;
21744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                goto done_close;
21844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            }
21944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
22044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            if (fstatat(fd, segment, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
22144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                if (errno == ENOENT) {
22244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    /* Nothing there yet; let's create it! */
22344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    if (mkdirat(fd, segment, mode) != 0) {
22444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                        if (errno == EEXIST) {
22544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            /* We raced with someone; ignore */
22644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                        } else {
22744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            ALOGE("Failed to mkdirat(%s): %s", buf, strerror(errno));
22844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            res = -errno;
22944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                            goto done_close;
23044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                        }
23144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    }
23244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                } else {
23344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    ALOGE("Failed to fstatat(%s): %s", buf, strerror(errno));
23444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    res = -errno;
23544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    goto done_close;
23644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                }
23744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            } else {
23844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                if (S_ISLNK(sb.st_mode)) {
23944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    ALOGE("Symbolic links are not allowed: %s", buf);
24044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    res = -ELOOP;
24144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    goto done_close;
24244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                }
24344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                if (!S_ISDIR(sb.st_mode)) {
24444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    ALOGE("Existing segment not a directory: %s", buf);
24544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    res = -ENOTDIR;
24644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                    goto done_close;
24744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                }
24844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            }
24944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
25044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            /* Yay, segment is ready for us to step into */
25144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            int next_fd;
25230a86ebc7a0f5a2e0fc698f432ee626cd96525b8Nick Kralevich            if ((next_fd = openat(fd, segment, O_NOFOLLOW | O_CLOEXEC)) == -1) {
25344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                ALOGE("Failed to openat(%s): %s", buf, strerror(errno));
25444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                res = -errno;
25544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey                goto done_close;
25644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            }
25744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
25844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            close(fd);
25944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            fd = next_fd;
26044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
26144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            *p = '/';
26244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey            segment = p + 1;
26344d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        }
26444d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey        p++;
26544d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    }
26644d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey
26744d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkeydone_close:
26844d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    close(fd);
26944d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkeydone:
27044d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    free(buf);
27144d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey    return res;
27244d6342caa0db1f613809e9ba1ea8d9af0183b74Jeff Sharkey}
2730ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey
2740ee7d8c68b57c02d02f707d6f71c731234d56eecJeff Sharkey#endif
275