2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2010-04-14 10:35:09 +08:00
|
|
|
#include <time.h>
|
2013-07-12 06:38:26 +08:00
|
|
|
#include <ftw.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2012-01-13 21:48:47 +08:00
|
|
|
#include <selinux/label.h>
|
2014-01-28 23:34:09 +08:00
|
|
|
#include <selinux/android.h>
|
2012-01-13 21:48:47 +08:00
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
#include <base/file.h>
|
2015-05-07 10:19:24 +08:00
|
|
|
#include <base/strings.h>
|
2015-03-17 01:08:46 +08:00
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
/* for ANDROID_SOCKET_* */
|
|
|
|
#include <cutils/sockets.h>
|
2015-05-08 23:30:33 +08:00
|
|
|
#include <base/stringprintf.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2012-01-13 21:48:47 +08:00
|
|
|
#include "init.h"
|
2010-04-20 08:05:34 +08:00
|
|
|
#include "log.h"
|
2015-08-27 02:43:36 +08:00
|
|
|
#include "property_service.h"
|
2010-04-22 03:04:20 +08:00
|
|
|
#include "util.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* android_name_to_id - returns the integer uid/gid associated with the given
|
2015-06-19 08:46:54 +08:00
|
|
|
* name, or UINT_MAX on error.
|
2009-03-04 11:32:55 +08:00
|
|
|
*/
|
|
|
|
static unsigned int android_name_to_id(const char *name)
|
|
|
|
{
|
2012-07-27 02:09:13 +08:00
|
|
|
const struct android_id_info *info = android_ids;
|
2009-03-04 11:32:55 +08:00
|
|
|
unsigned int n;
|
|
|
|
|
|
|
|
for (n = 0; n < android_id_count; n++) {
|
|
|
|
if (!strcmp(info[n].name, name))
|
|
|
|
return info[n].aid;
|
|
|
|
}
|
|
|
|
|
2015-06-19 08:46:54 +08:00
|
|
|
return UINT_MAX;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-06-19 08:46:54 +08:00
|
|
|
static unsigned int do_decode_uid(const char *s)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
unsigned int v;
|
|
|
|
|
|
|
|
if (!s || *s == '\0')
|
2015-06-19 08:46:54 +08:00
|
|
|
return UINT_MAX;
|
2009-03-04 11:32:55 +08:00
|
|
|
if (isalpha(s[0]))
|
|
|
|
return android_name_to_id(s);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
v = (unsigned int) strtoul(s, 0, 0);
|
|
|
|
if (errno)
|
2015-06-19 08:46:54 +08:00
|
|
|
return UINT_MAX;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* decode_uid - decodes and returns the given string, which can be either the
|
|
|
|
* numeric or name representation, into the integer uid or gid. Returns
|
|
|
|
* UINT_MAX on error.
|
|
|
|
*/
|
|
|
|
unsigned int decode_uid(const char *s) {
|
|
|
|
unsigned int v = do_decode_uid(s);
|
|
|
|
if (v == UINT_MAX) {
|
|
|
|
ERROR("decode_uid: Unable to find UID for '%s'. Returning UINT_MAX\n", s);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
|
|
|
|
* ("/dev/socket") as dictated in init.rc. This socket is inherited by the
|
|
|
|
* daemon. We communicate the file descriptor's value via the environment
|
|
|
|
* variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
|
|
|
|
*/
|
2013-05-14 00:37:04 +08:00
|
|
|
int create_socket(const char *name, int type, mode_t perm, uid_t uid,
|
|
|
|
gid_t gid, const char *socketcon)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int fd, ret;
|
2013-05-14 00:37:04 +08:00
|
|
|
char *filecon;
|
|
|
|
|
|
|
|
if (socketcon)
|
|
|
|
setsockcreatecon(socketcon);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
fd = socket(PF_UNIX, type, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-14 00:37:04 +08:00
|
|
|
if (socketcon)
|
|
|
|
setsockcreatecon(NULL);
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
memset(&addr, 0 , sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
|
|
|
|
name);
|
|
|
|
|
|
|
|
ret = unlink(addr.sun_path);
|
|
|
|
if (ret != 0 && errno != ENOENT) {
|
|
|
|
ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2013-05-14 00:37:04 +08:00
|
|
|
filecon = NULL;
|
2012-01-13 21:48:47 +08:00
|
|
|
if (sehandle) {
|
2013-05-14 00:37:04 +08:00
|
|
|
ret = selabel_lookup(sehandle, &filecon, addr.sun_path, S_IFSOCK);
|
2012-01-13 21:48:47 +08:00
|
|
|
if (ret == 0)
|
2013-05-14 00:37:04 +08:00
|
|
|
setfscreatecon(filecon);
|
2012-01-13 21:48:47 +08:00
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
|
|
|
|
if (ret) {
|
|
|
|
ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
|
|
|
|
goto out_unlink;
|
|
|
|
}
|
|
|
|
|
2012-01-13 21:48:47 +08:00
|
|
|
setfscreatecon(NULL);
|
2013-05-14 00:37:04 +08:00
|
|
|
freecon(filecon);
|
2012-01-13 21:48:47 +08:00
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
chown(addr.sun_path, uid, gid);
|
|
|
|
chmod(addr.sun_path, perm);
|
|
|
|
|
|
|
|
INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
|
|
|
|
addr.sun_path, perm, uid, gid);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
out_unlink:
|
|
|
|
unlink(addr.sun_path);
|
|
|
|
out_close:
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
bool read_file(const char* path, std::string* content) {
|
|
|
|
content->clear();
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
|
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
// For security reasons, disallow world-writable
|
|
|
|
// or group-writable files.
|
|
|
|
struct stat sb;
|
|
|
|
if (fstat(fd, &sb) == -1) {
|
|
|
|
ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
|
|
|
|
return false;
|
2012-01-19 02:39:01 +08:00
|
|
|
}
|
|
|
|
if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
|
2015-02-07 04:19:48 +08:00
|
|
|
ERROR("skipping insecure file '%s'\n", path);
|
|
|
|
return false;
|
2012-01-19 02:39:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
bool okay = android::base::ReadFdToString(fd, content);
|
2015-05-16 10:16:40 +08:00
|
|
|
close(fd);
|
2015-02-07 04:19:48 +08:00
|
|
|
return okay;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
int write_file(const char* path, const char* content) {
|
|
|
|
int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
|
|
|
|
if (fd == -1) {
|
2015-04-26 05:10:03 +08:00
|
|
|
NOTICE("write_file: Unable to open '%s': %s\n", path, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int result = android::base::WriteStringToFd(content, fd) ? 0 : -1;
|
|
|
|
if (result == -1) {
|
|
|
|
NOTICE("write_file: Unable to write to '%s': %s\n", path, strerror(errno));
|
2015-02-07 04:19:48 +08:00
|
|
|
}
|
2015-05-16 10:16:40 +08:00
|
|
|
close(fd);
|
2015-02-07 04:19:48 +08:00
|
|
|
return result;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2010-04-13 09:51:06 +08:00
|
|
|
#define MAX_MTD_PARTITIONS 16
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
char name[16];
|
|
|
|
int number;
|
|
|
|
} mtd_part_map[MAX_MTD_PARTITIONS];
|
|
|
|
|
|
|
|
static int mtd_part_count = -1;
|
|
|
|
|
|
|
|
static void find_mtd_partitions(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char buf[1024];
|
|
|
|
char *pmtdbufp;
|
|
|
|
ssize_t pmtdsize;
|
|
|
|
int r;
|
|
|
|
|
2015-02-03 06:37:22 +08:00
|
|
|
fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
|
2010-04-13 09:51:06 +08:00
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
pmtdsize = read(fd, buf, sizeof(buf) - 1);
|
|
|
|
pmtdbufp = buf;
|
|
|
|
while (pmtdsize > 0) {
|
|
|
|
int mtdnum, mtdsize, mtderasesize;
|
|
|
|
char mtdname[16];
|
|
|
|
mtdname[0] = '\0';
|
|
|
|
mtdnum = -1;
|
|
|
|
r = sscanf(pmtdbufp, "mtd%d: %x %x %15s",
|
|
|
|
&mtdnum, &mtdsize, &mtderasesize, mtdname);
|
|
|
|
if ((r == 4) && (mtdname[0] == '"')) {
|
|
|
|
char *x = strchr(mtdname + 1, '"');
|
|
|
|
if (x) {
|
|
|
|
*x = 0;
|
|
|
|
}
|
|
|
|
INFO("mtd partition %d, %s\n", mtdnum, mtdname + 1);
|
|
|
|
if (mtd_part_count < MAX_MTD_PARTITIONS) {
|
|
|
|
strcpy(mtd_part_map[mtd_part_count].name, mtdname + 1);
|
|
|
|
mtd_part_map[mtd_part_count].number = mtdnum;
|
|
|
|
mtd_part_count++;
|
|
|
|
} else {
|
|
|
|
ERROR("too many mtd partitions\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (pmtdsize > 0 && *pmtdbufp != '\n') {
|
|
|
|
pmtdbufp++;
|
|
|
|
pmtdsize--;
|
|
|
|
}
|
|
|
|
if (pmtdsize > 0) {
|
|
|
|
pmtdbufp++;
|
|
|
|
pmtdsize--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mtd_name_to_number(const char *name)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
if (mtd_part_count < 0) {
|
|
|
|
mtd_part_count = 0;
|
|
|
|
find_mtd_partitions();
|
|
|
|
}
|
|
|
|
for (n = 0; n < mtd_part_count; n++) {
|
|
|
|
if (!strcmp(name, mtd_part_map[n].name)) {
|
|
|
|
return mtd_part_map[n].number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-14 10:35:09 +08:00
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
time_t gettime() {
|
|
|
|
timespec now;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
return now.tv_sec;
|
|
|
|
}
|
2010-04-14 10:35:09 +08:00
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
uint64_t gettime_ns() {
|
|
|
|
timespec now;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
|
2010-04-14 10:35:09 +08:00
|
|
|
}
|
2010-04-09 07:16:20 +08:00
|
|
|
|
|
|
|
int mkdir_recursive(const char *pathname, mode_t mode)
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
const char *slash;
|
|
|
|
const char *p = pathname;
|
|
|
|
int width;
|
|
|
|
int ret;
|
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
while ((slash = strchr(p, '/')) != NULL) {
|
|
|
|
width = slash - pathname;
|
|
|
|
p = slash + 1;
|
|
|
|
if (width < 0)
|
|
|
|
break;
|
|
|
|
if (width == 0)
|
|
|
|
continue;
|
|
|
|
if ((unsigned int)width > sizeof(buf) - 1) {
|
|
|
|
ERROR("path too long for mkdir_recursive\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(buf, pathname, width);
|
|
|
|
buf[width] = 0;
|
|
|
|
if (stat(buf, &info) != 0) {
|
2012-06-12 01:37:39 +08:00
|
|
|
ret = make_dir(buf, mode);
|
2010-04-09 07:16:20 +08:00
|
|
|
if (ret && errno != EEXIST)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 01:37:39 +08:00
|
|
|
ret = make_dir(pathname, mode);
|
2010-04-09 07:16:20 +08:00
|
|
|
if (ret && errno != EEXIST)
|
|
|
|
return ret;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 22:41:19 +08:00
|
|
|
/*
|
|
|
|
* replaces any unacceptable characters with '_', the
|
|
|
|
* length of the resulting string is equal to the input string
|
|
|
|
*/
|
2010-04-09 07:16:20 +08:00
|
|
|
void sanitize(char *s)
|
|
|
|
{
|
2012-04-18 22:41:19 +08:00
|
|
|
const char* accept =
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"0123456789"
|
|
|
|
"_-.";
|
|
|
|
|
2010-04-09 07:16:20 +08:00
|
|
|
if (!s)
|
|
|
|
return;
|
2012-04-18 22:41:19 +08:00
|
|
|
|
2014-09-23 02:35:54 +08:00
|
|
|
while (*s) {
|
2012-04-18 22:41:19 +08:00
|
|
|
s += strspn(s, accept);
|
2014-09-23 02:35:54 +08:00
|
|
|
if (*s) *s++ = '_';
|
2012-04-18 22:41:19 +08:00
|
|
|
}
|
2010-04-09 07:16:20 +08:00
|
|
|
}
|
2012-04-18 22:41:19 +08:00
|
|
|
|
2013-09-06 02:19:21 +08:00
|
|
|
void make_link_init(const char *oldpath, const char *newpath)
|
2010-04-09 07:16:20 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char buf[256];
|
|
|
|
char *slash;
|
|
|
|
int width;
|
|
|
|
|
|
|
|
slash = strrchr(newpath, '/');
|
|
|
|
if (!slash)
|
|
|
|
return;
|
|
|
|
width = slash - newpath;
|
|
|
|
if (width <= 0 || width > (int)sizeof(buf) - 1)
|
|
|
|
return;
|
|
|
|
memcpy(buf, newpath, width);
|
|
|
|
buf[width] = 0;
|
|
|
|
ret = mkdir_recursive(buf, 0755);
|
|
|
|
if (ret)
|
|
|
|
ERROR("Failed to create directory %s: %s (%d)\n", buf, strerror(errno), errno);
|
|
|
|
|
|
|
|
ret = symlink(oldpath, newpath);
|
|
|
|
if (ret && errno != EEXIST)
|
|
|
|
ERROR("Failed to symlink %s to %s: %s (%d)\n", oldpath, newpath, strerror(errno), errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_link(const char *oldpath, const char *newpath)
|
|
|
|
{
|
|
|
|
char path[256];
|
|
|
|
ssize_t ret;
|
|
|
|
ret = readlink(newpath, path, sizeof(path) - 1);
|
|
|
|
if (ret <= 0)
|
|
|
|
return;
|
|
|
|
path[ret] = 0;
|
|
|
|
if (!strcmp(path, oldpath))
|
|
|
|
unlink(newpath);
|
|
|
|
}
|
2010-04-20 08:10:24 +08:00
|
|
|
|
|
|
|
int wait_for_file(const char *filename, int timeout)
|
|
|
|
{
|
|
|
|
struct stat info;
|
2015-05-23 06:56:14 +08:00
|
|
|
uint64_t timeout_time_ns = gettime_ns() + timeout * UINT64_C(1000000000);
|
2010-04-20 08:10:24 +08:00
|
|
|
int ret = -1;
|
|
|
|
|
2015-05-23 06:56:14 +08:00
|
|
|
while (gettime_ns() < timeout_time_ns && ((ret = stat(filename, &info)) < 0))
|
2010-04-20 08:10:24 +08:00
|
|
|
usleep(10000);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-04-22 03:04:20 +08:00
|
|
|
|
|
|
|
void open_devnull_stdio(void)
|
|
|
|
{
|
2015-04-26 07:24:53 +08:00
|
|
|
// Try to avoid the mknod() call if we can. Since SELinux makes
|
|
|
|
// a /dev/null replacement available for free, let's use it.
|
|
|
|
int fd = open("/sys/fs/selinux/null", O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
// OOPS, /sys/fs/selinux/null isn't available, likely because
|
|
|
|
// /sys/fs/selinux isn't mounted. Fall back to mknod.
|
|
|
|
static const char *name = "/dev/__null__";
|
|
|
|
if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
|
|
|
|
fd = open(name, O_RDWR);
|
|
|
|
unlink(name);
|
|
|
|
}
|
|
|
|
if (fd == -1) {
|
|
|
|
exit(1);
|
2010-04-22 03:04:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-26 07:24:53 +08:00
|
|
|
dup2(fd, 0);
|
|
|
|
dup2(fd, 1);
|
|
|
|
dup2(fd, 2);
|
|
|
|
if (fd > 2) {
|
|
|
|
close(fd);
|
|
|
|
}
|
2010-04-22 03:04:20 +08:00
|
|
|
}
|
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
void import_kernel_cmdline(bool in_qemu,
|
|
|
|
std::function<void(const std::string&, const std::string&, bool)> fn) {
|
|
|
|
std::string cmdline;
|
|
|
|
android::base::ReadFileToString("/proc/cmdline", &cmdline);
|
2011-09-29 00:55:31 +08:00
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
for (const auto& entry : android::base::Split(android::base::Trim(cmdline), " ")) {
|
|
|
|
std::vector<std::string> pieces = android::base::Split(entry, "=");
|
|
|
|
if (pieces.size() == 2) {
|
|
|
|
fn(pieces[0], pieces[1], in_qemu);
|
|
|
|
}
|
2011-09-29 00:55:31 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-12 01:37:39 +08:00
|
|
|
|
|
|
|
int make_dir(const char *path, mode_t mode)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
char *secontext = NULL;
|
|
|
|
|
|
|
|
if (sehandle) {
|
|
|
|
selabel_lookup(sehandle, &secontext, path, mode);
|
|
|
|
setfscreatecon(secontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = mkdir(path, mode);
|
|
|
|
|
|
|
|
if (secontext) {
|
|
|
|
int save_errno = errno;
|
|
|
|
freecon(secontext);
|
|
|
|
setfscreatecon(NULL);
|
|
|
|
errno = save_errno;
|
|
|
|
}
|
2012-10-17 14:07:05 +08:00
|
|
|
|
2012-06-12 01:37:39 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-01-28 23:34:09 +08:00
|
|
|
int restorecon(const char* pathname)
|
2012-06-12 01:37:39 +08:00
|
|
|
{
|
2014-02-07 22:14:13 +08:00
|
|
|
return selinux_android_restorecon(pathname, 0);
|
2013-07-12 06:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int restorecon_recursive(const char* pathname)
|
|
|
|
{
|
2014-02-07 22:14:13 +08:00
|
|
|
return selinux_android_restorecon(pathname, SELINUX_ANDROID_RESTORECON_RECURSE);
|
2013-07-12 06:38:26 +08:00
|
|
|
}
|
2015-05-08 23:30:33 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Writes hex_len hex characters (1/2 byte) to hex from bytes.
|
|
|
|
*/
|
|
|
|
std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
|
|
|
|
std::string hex("0x");
|
|
|
|
for (size_t i = 0; i < bytes_len; i++)
|
|
|
|
android::base::StringAppendF(&hex, "%02x", bytes[i]);
|
|
|
|
return hex;
|
|
|
|
}
|
2015-07-25 07:57:14 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true is pathname is a directory
|
|
|
|
*/
|
|
|
|
bool is_dir(const char* pathname) {
|
|
|
|
struct stat info;
|
|
|
|
if (stat(pathname, &info) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return S_ISDIR(info.st_mode);
|
|
|
|
}
|
2015-08-27 02:43:36 +08:00
|
|
|
|
|
|
|
bool expand_props(const std::string& src, std::string* dst) {
|
|
|
|
const char* src_ptr = src.c_str();
|
|
|
|
|
|
|
|
if (!dst) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* - variables can either be $x.y or ${x.y}, in case they are only part
|
|
|
|
* of the string.
|
|
|
|
* - will accept $$ as a literal $.
|
|
|
|
* - no nested property expansion, i.e. ${foo.${bar}} is not supported,
|
|
|
|
* bad things will happen
|
|
|
|
*/
|
|
|
|
while (*src_ptr) {
|
|
|
|
const char* c;
|
|
|
|
|
|
|
|
c = strchr(src_ptr, '$');
|
|
|
|
if (!c) {
|
|
|
|
dst->append(src_ptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->append(src_ptr, c);
|
|
|
|
c++;
|
|
|
|
|
|
|
|
if (*c == '$') {
|
|
|
|
dst->push_back(*(c++));
|
|
|
|
src_ptr = c;
|
|
|
|
continue;
|
|
|
|
} else if (*c == '\0') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string prop_name;
|
|
|
|
if (*c == '{') {
|
|
|
|
c++;
|
|
|
|
const char* end = strchr(c, '}');
|
|
|
|
if (!end) {
|
|
|
|
// failed to find closing brace, abort.
|
|
|
|
ERROR("unexpected end of string in '%s', looking for }\n", src.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
prop_name = std::string(c, end);
|
|
|
|
c = end + 1;
|
|
|
|
} else {
|
|
|
|
prop_name = c;
|
|
|
|
ERROR("using deprecated syntax for specifying property '%s', use ${name} instead\n",
|
|
|
|
c);
|
|
|
|
c += prop_name.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prop_name.empty()) {
|
|
|
|
ERROR("invalid zero-length prop name in '%s'\n", src.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string prop_val = property_get(prop_name.c_str());
|
|
|
|
if (prop_val.empty()) {
|
|
|
|
ERROR("property '%s' doesn't exist while expanding '%s'\n",
|
|
|
|
prop_name.c_str(), src.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->append(prop_val);
|
|
|
|
src_ptr = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|