2008-10-21 22:00:00 +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.
|
|
|
|
*/
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
#include "builtins.h"
|
|
|
|
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <errno.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
#include <fcntl.h>
|
2015-07-09 05:57:07 +08:00
|
|
|
#include <mntent.h>
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <net/if.h>
|
2015-07-09 05:57:07 +08:00
|
|
|
#include <signal.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/resource.h>
|
2015-01-30 13:31:23 +08:00
|
|
|
#include <sys/time.h>
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-04-18 08:20:16 +08:00
|
|
|
#include <sys/wait.h>
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <unistd.h>
|
2008-11-20 11:37:30 +08:00
|
|
|
#include <linux/loop.h>
|
2015-04-29 06:07:10 +08:00
|
|
|
#include <ext4_crypt_init_extensions.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2012-01-13 21:48:47 +08:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <fs_mgr.h>
|
2015-12-19 03:39:59 +08:00
|
|
|
#include <android-base/parseint.h>
|
2015-12-05 14:00:26 +08:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <cutils/partition_utils.h>
|
|
|
|
#include <cutils/android_reboot.h>
|
2015-07-09 05:57:07 +08:00
|
|
|
#include <logwrap/logwrap.h>
|
2015-03-21 00:45:18 +08:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2015-07-24 08:53:11 +08:00
|
|
|
#include "action.h"
|
2015-08-27 02:43:36 +08:00
|
|
|
#include "bootchart.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "devices.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
#include "init.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "init_parser.h"
|
|
|
|
#include "log.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
#include "property_service.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "service.h"
|
2015-12-19 03:39:59 +08:00
|
|
|
#include "signal_handler.h"
|
2010-04-14 11:35:46 +08:00
|
|
|
#include "util.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
|
builtins.c: Don't require file open() for chmod/chown
42a9349dc4e98019d27d7f8d19bc6c431695d7e1 modified init's
builtin chmod, chown, and mkdir calls to avoid following
symlinks. This addressed a number of attacks we were seeing
at the time where poorly written init scripts were following
attacker supplied symlinks resulting in rooting vulnerabilities.
To avoid race conditions, the previous implementation only ran
fchown / fchmod on file descriptors opened with open(O_NOFOLLOW).
Unfortunately, unlike the normal "chown" or "chmod" calls, this
requires read or write access to the underlying file. This
isn't ideal, as opening some files may have side effects, or
init may not have permission to open certain files (such as when
SELinux is enabled).
Instead of using open(O_NOFOLLOW) + fchown(), use lchown() instead.
As before, the target of the symlink won't be modified by chown.
This also supports setting the ownership of symlinks.
Instead of using open(O_NOFOLLOW) + fchmod(), use
fchmodat(AT_SYMLINK_NOFOLLOW) instead. As before, the target of the
symlink won't be modified by chmod.
This change will continue to ensure that chown/chmod/mkdir doesn't
follow symlinks, without requiring init to open every file in
read-only or read-write mode.
This change depends on bionic commit I1eba0cdb2c509d9193ceecf28f13118188a3cfa7
Addresses the following mako/occam SELinux denial:
audit(1422770408.951:6): avc: denied { write } for pid=1 comm="init" name="smd7" dev="tmpfs" ino=7207 scontext=u:r:init:s0 tcontext=u:object_r:radio_device:s0 tclass=chr_file
Change-Id: I14fde956784d65c44e7aa91dd7eea9a004df3081
2015-02-01 13:39:46 +08:00
|
|
|
#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
|
2015-07-09 05:57:07 +08:00
|
|
|
#define UNMOUNT_CHECK_MS 5000
|
|
|
|
#define UNMOUNT_CHECK_TIMES 10
|
builtins.c: Don't require file open() for chmod/chown
42a9349dc4e98019d27d7f8d19bc6c431695d7e1 modified init's
builtin chmod, chown, and mkdir calls to avoid following
symlinks. This addressed a number of attacks we were seeing
at the time where poorly written init scripts were following
attacker supplied symlinks resulting in rooting vulnerabilities.
To avoid race conditions, the previous implementation only ran
fchown / fchmod on file descriptors opened with open(O_NOFOLLOW).
Unfortunately, unlike the normal "chown" or "chmod" calls, this
requires read or write access to the underlying file. This
isn't ideal, as opening some files may have side effects, or
init may not have permission to open certain files (such as when
SELinux is enabled).
Instead of using open(O_NOFOLLOW) + fchown(), use lchown() instead.
As before, the target of the symlink won't be modified by chown.
This also supports setting the ownership of symlinks.
Instead of using open(O_NOFOLLOW) + fchmod(), use
fchmodat(AT_SYMLINK_NOFOLLOW) instead. As before, the target of the
symlink won't be modified by chmod.
This change will continue to ensure that chown/chmod/mkdir doesn't
follow symlinks, without requiring init to open every file in
read-only or read-write mode.
This change depends on bionic commit I1eba0cdb2c509d9193ceecf28f13118188a3cfa7
Addresses the following mako/occam SELinux denial:
audit(1422770408.951:6): avc: denied { write } for pid=1 comm="init" name="smd7" dev="tmpfs" ino=7207 scontext=u:r:init:s0 tcontext=u:object_r:radio_device:s0 tclass=chr_file
Change-Id: I14fde956784d65c44e7aa91dd7eea9a004df3081
2015-02-01 13:39:46 +08:00
|
|
|
|
2015-02-04 09:12:07 +08:00
|
|
|
// System call provided by bionic but not in any header file.
|
|
|
|
extern "C" int init_module(void *, unsigned long, const char *);
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-12-19 03:39:59 +08:00
|
|
|
static const int kTerminateServiceDelayMicroSeconds = 50000;
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int insmod(const char *filename, const char *options) {
|
2015-05-13 04:54:41 +08:00
|
|
|
std::string module;
|
2015-07-25 04:26:04 +08:00
|
|
|
if (!read_file(filename, &module)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
return -1;
|
2015-02-07 04:19:48 +08:00
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
// TODO: use finit_module for >= 3.8 kernels.
|
|
|
|
return init_module(&module[0], module.size(), options);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int __ifupdown(const char *interface, int up) {
|
2008-10-21 22:00:00 +08:00
|
|
|
struct ifreq ifr;
|
|
|
|
int s, ret;
|
|
|
|
|
|
|
|
strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
|
|
|
|
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = ioctl(s, SIOCGIFFLAGS, &ifr);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up)
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
|
|
|
else
|
|
|
|
ifr.ifr_flags &= ~IFF_UP;
|
|
|
|
|
|
|
|
ret = ioctl(s, SIOCSIFFLAGS, &ifr);
|
2015-02-05 02:25:09 +08:00
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
done:
|
|
|
|
close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static void unmount_and_fsck(const struct mntent *entry) {
|
2015-07-09 05:57:07 +08:00
|
|
|
if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* First, lazily unmount the directory. This unmount request finishes when
|
|
|
|
* all processes that open a file or directory in |entry->mnt_dir| exit.
|
|
|
|
*/
|
|
|
|
TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
|
|
|
|
|
|
|
|
/* Next, kill all processes except init, kthreadd, and kthreadd's
|
|
|
|
* children to finish the lazy unmount. Killing all processes here is okay
|
|
|
|
* because this callback function is only called right before reboot().
|
|
|
|
* It might be cleaner to selectively kill processes that actually use
|
|
|
|
* |entry->mnt_dir| rather than killing all, probably by reusing a function
|
|
|
|
* like killProcessesWithOpenFiles() in vold/, but the selinux policy does
|
|
|
|
* not allow init to scan /proc/<pid> files which the utility function
|
|
|
|
* heavily relies on. The policy does not allow the process to execute
|
|
|
|
* killall/pkill binaries either. Note that some processes might
|
|
|
|
* automatically restart after kill(), but that is not really a problem
|
|
|
|
* because |entry->mnt_dir| is no longer visible to such new processes.
|
|
|
|
*/
|
2015-08-01 03:45:25 +08:00
|
|
|
ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
|
2015-07-09 05:57:07 +08:00
|
|
|
TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
while (count++ < UNMOUNT_CHECK_TIMES) {
|
|
|
|
int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
|
|
|
|
if (fd >= 0) {
|
|
|
|
/* |entry->mnt_dir| has sucessfully been unmounted. */
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
} else if (errno == EBUSY) {
|
|
|
|
/* Some processes using |entry->mnt_dir| are still alive. Wait for a
|
|
|
|
* while then retry.
|
|
|
|
*/
|
|
|
|
TEMP_FAILURE_RETRY(
|
|
|
|
usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* Cannot open the device. Give up. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int st;
|
|
|
|
if (!strcmp(entry->mnt_type, "f2fs")) {
|
|
|
|
const char *f2fs_argv[] = {
|
|
|
|
"/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
|
|
|
|
};
|
|
|
|
android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
|
2015-08-14 16:22:53 +08:00
|
|
|
&st, true, LOG_KLOG, true, NULL, NULL, 0);
|
2015-07-09 05:57:07 +08:00
|
|
|
} else if (!strcmp(entry->mnt_type, "ext4")) {
|
|
|
|
const char *ext4_argv[] = {
|
|
|
|
"/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
|
|
|
|
};
|
|
|
|
android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
|
2015-08-14 16:22:53 +08:00
|
|
|
&st, true, LOG_KLOG, true, NULL, NULL, 0);
|
2015-07-09 05:57:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_class_start(const std::vector<std::string>& args) {
|
2008-10-21 22:00:00 +08:00
|
|
|
/* Starting a class does not start services
|
|
|
|
* which are explicitly disabled. They must
|
|
|
|
* be started individually.
|
|
|
|
*/
|
2015-08-01 03:45:25 +08:00
|
|
|
ServiceManager::GetInstance().
|
|
|
|
ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_class_stop(const std::vector<std::string>& args) {
|
2015-08-01 03:45:25 +08:00
|
|
|
ServiceManager::GetInstance().
|
|
|
|
ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_class_reset(const std::vector<std::string>& args) {
|
2015-08-01 03:45:25 +08:00
|
|
|
ServiceManager::GetInstance().
|
|
|
|
ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
|
2010-12-04 08:33:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_domainname(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
return write_file("/proc/sys/kernel/domainname", args[1].c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_enable(const std::vector<std::string>& args) {
|
2015-08-01 03:45:25 +08:00
|
|
|
Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
|
|
|
|
if (!svc) {
|
2014-05-03 12:14:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-01 03:45:25 +08:00
|
|
|
return svc->Enable();
|
2014-05-03 12:14:29 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_exec(const std::vector<std::string>& args) {
|
2015-08-01 03:45:25 +08:00
|
|
|
Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
|
|
|
|
if (!svc) {
|
|
|
|
return -1;
|
2015-07-31 04:52:55 +08:00
|
|
|
}
|
2015-08-01 03:45:25 +08:00
|
|
|
if (!svc->Start()) {
|
2015-02-07 12:15:18 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-01 03:45:25 +08:00
|
|
|
waiting_for_exec = true;
|
2015-02-07 12:15:18 +08:00
|
|
|
return 0;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_export(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
return add_environment(args[1].c_str(), args[2].c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_hostname(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
return write_file("/proc/sys/kernel/hostname", args[1].c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_ifup(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
return __ifupdown(args[1].c_str(), 1);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_insmod(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
std::string options;
|
2008-12-18 10:08:08 +08:00
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if (args.size() > 2) {
|
|
|
|
options += args[2];
|
|
|
|
for (std::size_t i = 3; i < args.size(); ++i) {
|
|
|
|
options += ' ';
|
|
|
|
options += args[i];
|
2008-12-18 10:08:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
return insmod(args[1].c_str(), options.c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_mkdir(const std::vector<std::string>& args) {
|
2008-10-21 22:00:00 +08:00
|
|
|
mode_t mode = 0755;
|
2011-07-09 03:57:36 +08:00
|
|
|
int ret;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
/* mkdir <path> [mode] [owner] [group] */
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if (args.size() >= 3) {
|
|
|
|
mode = std::stoul(args[2], 0, 8);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
ret = make_dir(args[1].c_str(), mode);
|
2011-07-09 03:57:36 +08:00
|
|
|
/* chmod in case the directory already exists */
|
|
|
|
if (ret == -1 && errno == EEXIST) {
|
2015-07-31 04:52:55 +08:00
|
|
|
ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
|
2011-07-09 03:57:36 +08:00
|
|
|
}
|
|
|
|
if (ret == -1) {
|
2008-10-21 22:00:00 +08:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if (args.size() >= 4) {
|
|
|
|
uid_t uid = decode_uid(args[3].c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
gid_t gid = -1;
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if (args.size() == 5) {
|
|
|
|
gid = decode_uid(args[4].c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if (lchown(args[1].c_str(), uid, gid) == -1) {
|
2008-10-21 22:00:00 +08:00
|
|
|
return -errno;
|
|
|
|
}
|
2012-08-15 06:43:46 +08:00
|
|
|
|
|
|
|
/* chown may have cleared S_ISUID and S_ISGID, chmod again */
|
|
|
|
if (mode & (S_ISUID | S_ISGID)) {
|
2015-07-31 04:52:55 +08:00
|
|
|
ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
|
2012-08-15 06:43:46 +08:00
|
|
|
if (ret == -1) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
return e4crypt_set_directory_policy(args[1].c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
unsigned flag;
|
|
|
|
} mount_flags[] = {
|
|
|
|
{ "noatime", MS_NOATIME },
|
2011-07-14 19:39:09 +08:00
|
|
|
{ "noexec", MS_NOEXEC },
|
2008-10-21 22:00:00 +08:00
|
|
|
{ "nosuid", MS_NOSUID },
|
|
|
|
{ "nodev", MS_NODEV },
|
|
|
|
{ "nodiratime", MS_NODIRATIME },
|
|
|
|
{ "ro", MS_RDONLY },
|
|
|
|
{ "rw", 0 },
|
|
|
|
{ "remount", MS_REMOUNT },
|
2012-08-15 02:34:34 +08:00
|
|
|
{ "bind", MS_BIND },
|
|
|
|
{ "rec", MS_REC },
|
|
|
|
{ "unbindable", MS_UNBINDABLE },
|
|
|
|
{ "private", MS_PRIVATE },
|
|
|
|
{ "slave", MS_SLAVE },
|
|
|
|
{ "shared", MS_SHARED },
|
2008-10-21 22:00:00 +08:00
|
|
|
{ "defaults", 0 },
|
|
|
|
{ 0, 0 },
|
|
|
|
};
|
|
|
|
|
2010-12-04 08:33:31 +08:00
|
|
|
#define DATA_MNT_POINT "/data"
|
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
/* mount <type> <device> <path> <flags ...> <options> */
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_mount(const std::vector<std::string>& args) {
|
2008-10-21 22:00:00 +08:00
|
|
|
char tmp[64];
|
2015-07-31 04:52:55 +08:00
|
|
|
const char *source, *target, *system;
|
|
|
|
const char *options = NULL;
|
2008-10-21 22:00:00 +08:00
|
|
|
unsigned flags = 0;
|
2015-07-31 04:52:55 +08:00
|
|
|
std::size_t na = 0;
|
2008-10-21 22:00:00 +08:00
|
|
|
int n, i;
|
2010-04-20 08:10:24 +08:00
|
|
|
int wait = 0;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
for (na = 4; na < args.size(); na++) {
|
2008-10-21 22:00:00 +08:00
|
|
|
for (i = 0; mount_flags[i].name; i++) {
|
2015-07-31 04:52:55 +08:00
|
|
|
if (!args[na].compare(mount_flags[i].name)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
flags |= mount_flags[i].flag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-20 08:10:24 +08:00
|
|
|
if (!mount_flags[i].name) {
|
2015-07-31 04:52:55 +08:00
|
|
|
if (!args[na].compare("wait"))
|
2010-04-20 08:10:24 +08:00
|
|
|
wait = 1;
|
|
|
|
/* if our last argument isn't a flag, wolf it up as an option string */
|
2015-07-31 04:52:55 +08:00
|
|
|
else if (na + 1 == args.size())
|
|
|
|
options = args[na].c_str();
|
2010-04-20 08:10:24 +08:00
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
system = args[1].c_str();
|
|
|
|
source = args[2].c_str();
|
|
|
|
target = args[3].c_str();
|
2008-11-20 11:37:30 +08:00
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
if (!strncmp(source, "mtd@", 4)) {
|
|
|
|
n = mtd_name_to_number(source + 4);
|
2008-11-20 11:37:30 +08:00
|
|
|
if (n < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-18 11:27:51 +08:00
|
|
|
snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
|
2008-11-20 11:37:30 +08:00
|
|
|
|
2010-04-20 08:10:24 +08:00
|
|
|
if (wait)
|
|
|
|
wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
|
2008-11-20 11:37:30 +08:00
|
|
|
if (mount(tmp, target, system, flags, options) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-18 10:09:47 +08:00
|
|
|
goto exit_success;
|
2008-11-20 11:37:30 +08:00
|
|
|
} else if (!strncmp(source, "loop@", 5)) {
|
|
|
|
int mode, loop, fd;
|
|
|
|
struct loop_info info;
|
|
|
|
|
|
|
|
mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
|
2015-02-03 06:37:22 +08:00
|
|
|
fd = open(source + 5, mode | O_CLOEXEC);
|
2008-11-20 11:37:30 +08:00
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
2008-11-20 11:37:30 +08:00
|
|
|
|
|
|
|
for (n = 0; ; n++) {
|
2015-02-18 11:27:51 +08:00
|
|
|
snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
|
2015-02-03 06:37:22 +08:00
|
|
|
loop = open(tmp, mode | O_CLOEXEC);
|
2008-11-20 11:37:30 +08:00
|
|
|
if (loop < 0) {
|
2014-02-06 15:57:27 +08:00
|
|
|
close(fd);
|
2008-11-20 11:37:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if it is a blank loop device */
|
|
|
|
if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
|
|
|
|
/* if it becomes our loop device */
|
|
|
|
if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (mount(tmp, target, system, flags, options) < 0) {
|
|
|
|
ioctl(loop, LOOP_CLR_FD, 0);
|
|
|
|
close(loop);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(loop);
|
2011-02-18 10:09:47 +08:00
|
|
|
goto exit_success;
|
2008-11-20 11:37:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
ERROR("out of loopback devices");
|
|
|
|
return -1;
|
|
|
|
} else {
|
2010-04-20 08:10:24 +08:00
|
|
|
if (wait)
|
|
|
|
wait_for_file(source, COMMAND_RETRY_TIMEOUT);
|
2008-11-20 11:37:30 +08:00
|
|
|
if (mount(source, target, system, flags, options) < 0) {
|
2012-04-18 08:20:16 +08:00
|
|
|
return -1;
|
2008-11-20 11:37:30 +08:00
|
|
|
}
|
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
2011-02-18 10:09:47 +08:00
|
|
|
|
|
|
|
exit_success:
|
2012-04-18 08:20:16 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2011-03-09 09:01:29 +08:00
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int wipe_data_via_recovery() {
|
2014-07-03 05:26:54 +08:00
|
|
|
mkdir("/cache/recovery", 0700);
|
2015-02-03 06:37:22 +08:00
|
|
|
int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
|
2014-07-03 05:26:54 +08:00
|
|
|
if (fd >= 0) {
|
2014-09-25 02:46:36 +08:00
|
|
|
write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
|
|
|
|
write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
|
2014-07-03 05:26:54 +08:00
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
ERROR("could not open /cache/recovery/command\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
|
|
|
|
while (1) { pause(); } // never reached
|
|
|
|
}
|
|
|
|
|
2016-01-14 11:18:21 +08:00
|
|
|
/* Imports .rc files from the specified paths. Default ones are applied if none is given.
|
|
|
|
*
|
|
|
|
* start_index: index of the first path in the args list
|
|
|
|
*/
|
|
|
|
static void import_late(const std::vector<std::string>& args, size_t start_index) {
|
2015-08-27 02:43:36 +08:00
|
|
|
Parser& parser = Parser::GetInstance();
|
2016-01-14 11:18:21 +08:00
|
|
|
if (args.size() <= start_index) {
|
|
|
|
// Use the default set if no path is given
|
|
|
|
static const std::vector<std::string> init_directories = {
|
|
|
|
"/system/etc/init",
|
|
|
|
"/vendor/etc/init",
|
|
|
|
"/odm/etc/init"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& dir : init_directories) {
|
|
|
|
parser.ParseConfig(dir);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (size_t i = start_index; i < args.size(); ++i) {
|
|
|
|
parser.ParseConfig(args[i]);
|
|
|
|
}
|
2015-07-23 05:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 11:18:21 +08:00
|
|
|
/* mount_all <fstab> [ <path> ]*
|
|
|
|
*
|
2014-07-03 05:26:54 +08:00
|
|
|
* This function might request a reboot, in which case it will
|
|
|
|
* not return.
|
|
|
|
*/
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_mount_all(const std::vector<std::string>& args) {
|
2012-04-18 08:20:16 +08:00
|
|
|
pid_t pid;
|
|
|
|
int ret = -1;
|
|
|
|
int child_ret = -1;
|
|
|
|
int status;
|
2013-02-14 04:58:40 +08:00
|
|
|
struct fstab *fstab;
|
2012-04-18 08:20:16 +08:00
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
const char* fstabfile = args[1].c_str();
|
2012-04-18 08:20:16 +08:00
|
|
|
/*
|
|
|
|
* Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
|
|
|
|
* do the call in the child to provide protection to the main init
|
|
|
|
* process if anything goes wrong (crash or memory leak), and wait for
|
|
|
|
* the child to finish in the parent.
|
|
|
|
*/
|
|
|
|
pid = fork();
|
|
|
|
if (pid > 0) {
|
|
|
|
/* Parent. Wait for the child to return */
|
2014-09-17 05:31:23 +08:00
|
|
|
int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
|
|
|
|
if (wp_ret < 0) {
|
|
|
|
/* Unexpected error code. We will continue anyway. */
|
2015-03-21 08:05:56 +08:00
|
|
|
NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
|
2014-09-17 05:31:23 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 08:20:16 +08:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
ret = WEXITSTATUS(status);
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
2011-02-18 10:09:47 +08:00
|
|
|
}
|
2012-04-18 08:20:16 +08:00
|
|
|
} else if (pid == 0) {
|
|
|
|
/* child, call fs_mgr_mount_all() */
|
|
|
|
klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
|
2015-07-21 19:44:07 +08:00
|
|
|
fstab = fs_mgr_read_fstab(fstabfile);
|
2013-02-14 04:58:40 +08:00
|
|
|
child_ret = fs_mgr_mount_all(fstab);
|
|
|
|
fs_mgr_free_fstab(fstab);
|
2012-04-18 08:20:16 +08:00
|
|
|
if (child_ret == -1) {
|
|
|
|
ERROR("fs_mgr_mount_all returned an error\n");
|
2011-02-18 10:09:47 +08:00
|
|
|
}
|
2014-07-03 04:16:04 +08:00
|
|
|
_exit(child_ret);
|
2012-04-18 08:20:16 +08:00
|
|
|
} else {
|
|
|
|
/* fork failed, return an error */
|
|
|
|
return -1;
|
2011-02-18 10:09:47 +08:00
|
|
|
}
|
|
|
|
|
2016-01-14 11:18:21 +08:00
|
|
|
/* Paths of .rc files are specified at the 2nd argument and beyond */
|
|
|
|
import_late(args, 2);
|
2015-07-23 05:23:33 +08:00
|
|
|
|
2014-07-03 04:16:04 +08:00
|
|
|
if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
|
2014-02-04 05:27:49 +08:00
|
|
|
property_set("vold.decrypt", "trigger_encryption");
|
2014-07-03 04:16:04 +08:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
|
2012-04-18 08:20:16 +08:00
|
|
|
property_set("ro.crypto.state", "encrypted");
|
2015-04-29 06:07:10 +08:00
|
|
|
property_set("ro.crypto.type", "block");
|
2014-01-31 02:43:52 +08:00
|
|
|
property_set("vold.decrypt", "trigger_default_encryption");
|
2014-07-03 04:16:04 +08:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
|
2012-04-18 08:20:16 +08:00
|
|
|
property_set("ro.crypto.state", "unencrypted");
|
|
|
|
/* If fs_mgr determined this is an unencrypted device, then trigger
|
|
|
|
* that action.
|
|
|
|
*/
|
2015-07-24 08:53:11 +08:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
|
2014-07-03 05:26:54 +08:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
|
|
|
|
/* Setup a wipe via recovery, and reboot into recovery */
|
|
|
|
ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
|
|
|
|
ret = wipe_data_via_recovery();
|
|
|
|
/* If reboot worked, there is no return. */
|
2015-03-26 23:49:42 +08:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
|
|
|
|
if (e4crypt_install_keyring()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
property_set("ro.crypto.state", "encrypted");
|
2015-04-29 06:07:10 +08:00
|
|
|
property_set("ro.crypto.type", "file");
|
2015-03-26 23:49:42 +08:00
|
|
|
|
|
|
|
// Although encrypted, we have device key, so we do not need to
|
|
|
|
// do anything different from the nonencrypted case.
|
2015-07-24 08:53:11 +08:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
|
2015-03-26 23:49:42 +08:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
|
|
|
|
if (e4crypt_install_keyring()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
property_set("ro.crypto.state", "encrypted");
|
2015-04-29 06:07:10 +08:00
|
|
|
property_set("ro.crypto.type", "file");
|
2015-03-26 23:49:42 +08:00
|
|
|
property_set("vold.decrypt", "trigger_restart_min_framework");
|
2014-07-03 05:26:54 +08:00
|
|
|
} else if (ret > 0) {
|
|
|
|
ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
|
2012-04-18 08:20:16 +08:00
|
|
|
}
|
2014-07-03 04:16:04 +08:00
|
|
|
/* else ... < 0: error */
|
2011-02-18 10:09:47 +08:00
|
|
|
|
2012-04-18 08:20:16 +08:00
|
|
|
return ret;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_swapon_all(const std::vector<std::string>& args) {
|
2013-07-10 09:42:09 +08:00
|
|
|
struct fstab *fstab;
|
|
|
|
int ret;
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
fstab = fs_mgr_read_fstab(args[1].c_str());
|
2013-07-10 09:42:09 +08:00
|
|
|
ret = fs_mgr_swapon_all(fstab);
|
|
|
|
fs_mgr_free_fstab(fstab);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_setprop(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
const char* name = args[1].c_str();
|
|
|
|
const char* value = args[2].c_str();
|
2015-07-25 04:26:04 +08:00
|
|
|
property_set(name, value);
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_setrlimit(const std::vector<std::string>& args) {
|
2008-10-21 22:00:00 +08:00
|
|
|
struct rlimit limit;
|
|
|
|
int resource;
|
2015-07-31 04:52:55 +08:00
|
|
|
resource = std::stoi(args[1]);
|
|
|
|
limit.rlim_cur = std::stoi(args[2]);
|
|
|
|
limit.rlim_max = std::stoi(args[3]);
|
2008-10-21 22:00:00 +08:00
|
|
|
return setrlimit(resource, &limit);
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_start(const std::vector<std::string>& args) {
|
2015-08-01 03:45:25 +08:00
|
|
|
Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
|
|
|
|
if (!svc) {
|
|
|
|
ERROR("do_start: Service %s not found\n", args[1].c_str());
|
|
|
|
return -1;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
2015-08-01 03:45:25 +08:00
|
|
|
if (!svc->Start())
|
|
|
|
return -1;
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_stop(const std::vector<std::string>& args) {
|
2015-08-01 03:45:25 +08:00
|
|
|
Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
|
|
|
|
if (!svc) {
|
|
|
|
ERROR("do_stop: Service %s not found\n", args[1].c_str());
|
|
|
|
return -1;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
2015-08-01 03:45:25 +08:00
|
|
|
svc->Stop();
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_restart(const std::vector<std::string>& args) {
|
2015-08-01 03:45:25 +08:00
|
|
|
Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
|
|
|
|
if (!svc) {
|
|
|
|
ERROR("do_restart: Service %s not found\n", args[1].c_str());
|
|
|
|
return -1;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
2015-08-01 03:45:25 +08:00
|
|
|
svc->Restart();
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_powerctl(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
const char* command = args[1].c_str();
|
2013-04-19 03:20:02 +08:00
|
|
|
int len = 0;
|
2015-07-22 06:50:59 +08:00
|
|
|
unsigned int cmd = 0;
|
|
|
|
const char *reboot_target = "";
|
2015-07-09 05:57:07 +08:00
|
|
|
void (*callback_on_ro_remount)(const struct mntent*) = NULL;
|
2013-04-19 03:20:02 +08:00
|
|
|
|
|
|
|
if (strncmp(command, "shutdown", 8) == 0) {
|
|
|
|
cmd = ANDROID_RB_POWEROFF;
|
|
|
|
len = 8;
|
|
|
|
} else if (strncmp(command, "reboot", 6) == 0) {
|
|
|
|
cmd = ANDROID_RB_RESTART2;
|
|
|
|
len = 6;
|
|
|
|
} else {
|
|
|
|
ERROR("powerctl: unrecognized command '%s'\n", command);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command[len] == ',') {
|
2015-07-22 06:50:59 +08:00
|
|
|
if (cmd == ANDROID_RB_POWEROFF &&
|
|
|
|
!strcmp(&command[len + 1], "userrequested")) {
|
|
|
|
// The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
|
|
|
|
// Run fsck once the file system is remounted in read-only mode.
|
|
|
|
callback_on_ro_remount = unmount_and_fsck;
|
|
|
|
} else if (cmd == ANDROID_RB_RESTART2) {
|
|
|
|
reboot_target = &command[len + 1];
|
|
|
|
}
|
|
|
|
} else if (command[len] != '\0') {
|
2013-04-19 03:20:02 +08:00
|
|
|
ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-12-19 03:39:59 +08:00
|
|
|
std::string timeout = property_get("ro.build.shutdown_timeout");
|
|
|
|
unsigned int delay = 0;
|
|
|
|
|
|
|
|
if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
|
|
|
|
Timer t;
|
|
|
|
// Ask all services to terminate.
|
|
|
|
ServiceManager::GetInstance().ForEachService(
|
|
|
|
[] (Service* s) { s->Terminate(); });
|
|
|
|
|
|
|
|
while (t.duration() < delay) {
|
|
|
|
ServiceManager::GetInstance().ReapAnyOutstandingChildren();
|
|
|
|
|
|
|
|
int service_count = 0;
|
|
|
|
ServiceManager::GetInstance().ForEachService(
|
|
|
|
[&service_count] (Service* s) {
|
|
|
|
// Count the number of services running.
|
|
|
|
// Exclude the console as it will ignore the SIGTERM signal
|
|
|
|
// and not exit.
|
|
|
|
// Note: SVC_CONSOLE actually means "requires console" but
|
|
|
|
// it is only used by the shell.
|
|
|
|
if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
|
|
|
|
service_count++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (service_count == 0) {
|
|
|
|
// All terminable services terminated. We can exit early.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a bit before recounting the number or running services.
|
|
|
|
usleep(kTerminateServiceDelayMicroSeconds);
|
|
|
|
}
|
|
|
|
NOTICE("Terminating running services took %.02f seconds", t.duration());
|
|
|
|
}
|
|
|
|
|
2015-07-09 05:57:07 +08:00
|
|
|
return android_reboot_with_callback(cmd, 0, reboot_target,
|
|
|
|
callback_on_ro_remount);
|
2013-04-19 03:20:02 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_trigger(const std::vector<std::string>& args) {
|
2015-07-24 08:53:11 +08:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger(args[1]);
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_symlink(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
return symlink(args[1].c_str(), args[2].c_str());
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_rm(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
return unlink(args[1].c_str());
|
2011-01-19 09:37:41 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_rmdir(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
return rmdir(args[1].c_str());
|
2011-01-19 09:37:41 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_sysclktz(const std::vector<std::string>& args) {
|
2008-12-18 10:08:08 +08:00
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
memset(&tz, 0, sizeof(tz));
|
2015-07-31 04:52:55 +08:00
|
|
|
tz.tz_minuteswest = std::stoi(args[1]);
|
2008-12-18 10:08:08 +08:00
|
|
|
if (settimeofday(NULL, &tz))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_verity_load_state(const std::vector<std::string>& args) {
|
2015-03-21 00:45:18 +08:00
|
|
|
int mode = -1;
|
|
|
|
int rc = fs_mgr_load_verity_state(&mode);
|
2015-12-02 22:23:09 +08:00
|
|
|
if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
|
2015-07-24 08:53:11 +08:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("verity-logging");
|
2015-02-16 19:03:34 +08:00
|
|
|
}
|
2015-03-21 00:45:18 +08:00
|
|
|
return rc;
|
2015-02-16 19:03:34 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static void verity_update_property(fstab_rec *fstab, const char *mount_point,
|
|
|
|
int mode, int status) {
|
2015-03-30 18:38:38 +08:00
|
|
|
property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
|
|
|
|
android::base::StringPrintf("%d", mode).c_str());
|
2015-03-19 18:00:34 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_verity_update_state(const std::vector<std::string>& args) {
|
2015-03-21 00:45:18 +08:00
|
|
|
return fs_mgr_update_verity_state(verity_update_property);
|
2015-03-19 18:00:34 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_write(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
const char* path = args[1].c_str();
|
|
|
|
const char* value = args[2].c_str();
|
2015-07-25 04:26:04 +08:00
|
|
|
return write_file(path, value);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_copy(const std::vector<std::string>& args) {
|
2009-08-27 07:39:25 +08:00
|
|
|
char *buffer = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
int fd1 = -1, fd2 = -1;
|
|
|
|
struct stat info;
|
|
|
|
int brtw, brtr;
|
|
|
|
char *p;
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if (stat(args[1].c_str(), &info) < 0)
|
2009-08-27 07:39:25 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
|
2009-08-27 07:39:25 +08:00
|
|
|
goto out_err;
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
|
2009-08-27 07:39:25 +08:00
|
|
|
goto out_err;
|
|
|
|
|
2015-02-04 09:12:07 +08:00
|
|
|
if (!(buffer = (char*) malloc(info.st_size)))
|
2009-08-27 07:39:25 +08:00
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
brtr = info.st_size;
|
|
|
|
while(brtr) {
|
|
|
|
rc = read(fd1, p, brtr);
|
|
|
|
if (rc < 0)
|
|
|
|
goto out_err;
|
|
|
|
if (rc == 0)
|
|
|
|
break;
|
|
|
|
p += rc;
|
|
|
|
brtr -= rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
brtw = info.st_size;
|
|
|
|
while(brtw) {
|
|
|
|
rc = write(fd2, p, brtw);
|
|
|
|
if (rc < 0)
|
|
|
|
goto out_err;
|
|
|
|
if (rc == 0)
|
|
|
|
break;
|
|
|
|
p += rc;
|
|
|
|
brtw -= rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
goto out;
|
|
|
|
out_err:
|
|
|
|
rc = -1;
|
|
|
|
out:
|
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
|
|
|
if (fd1 >= 0)
|
|
|
|
close(fd1);
|
|
|
|
if (fd2 >= 0)
|
|
|
|
close(fd2);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_chown(const std::vector<std::string>& args) {
|
2008-10-21 22:00:00 +08:00
|
|
|
/* GID is optional. */
|
2015-07-31 04:52:55 +08:00
|
|
|
if (args.size() == 3) {
|
|
|
|
if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
|
2008-10-21 22:00:00 +08:00
|
|
|
return -errno;
|
2015-07-31 04:52:55 +08:00
|
|
|
} else if (args.size() == 4) {
|
|
|
|
if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
|
|
|
|
decode_uid(args[2].c_str())) == -1)
|
2008-10-21 22:00:00 +08:00
|
|
|
return -errno;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mode_t get_mode(const char *s) {
|
|
|
|
mode_t mode = 0;
|
|
|
|
while (*s) {
|
|
|
|
if (*s >= '0' && *s <= '7') {
|
|
|
|
mode = (mode<<3) | (*s-'0');
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_chmod(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
mode_t mode = get_mode(args[1].c_str());
|
|
|
|
if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
|
2008-10-21 22:00:00 +08:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_restorecon(const std::vector<std::string>& args) {
|
2013-10-10 04:02:09 +08:00
|
|
|
int ret = 0;
|
2012-01-13 21:48:47 +08:00
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
for (auto it = std::next(args.begin()); it != args.end(); ++it) {
|
|
|
|
if (restorecon(it->c_str()) < 0)
|
2013-10-10 04:02:09 +08:00
|
|
|
ret = -errno;
|
2012-01-13 21:48:47 +08:00
|
|
|
}
|
2013-10-10 04:02:09 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_restorecon_recursive(const std::vector<std::string>& args) {
|
2013-10-10 04:02:09 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
for (auto it = std::next(args.begin()); it != args.end(); ++it) {
|
|
|
|
if (restorecon_recursive(it->c_str()) < 0)
|
2013-10-10 04:02:09 +08:00
|
|
|
ret = -errno;
|
|
|
|
}
|
|
|
|
return ret;
|
2012-01-13 21:48:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_loglevel(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
int log_level = std::stoi(args[1]);
|
2014-06-27 04:55:03 +08:00
|
|
|
if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
|
|
|
|
ERROR("loglevel: invalid log level'%d'\n", log_level);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
klog_set_level(log_level);
|
|
|
|
return 0;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2015-09-02 06:05:34 +08:00
|
|
|
static int do_load_persist_props(const std::vector<std::string>& args) {
|
2015-08-27 02:43:36 +08:00
|
|
|
load_persist_props();
|
|
|
|
return 0;
|
2011-03-09 09:01:29 +08:00
|
|
|
}
|
|
|
|
|
2015-09-02 06:05:34 +08:00
|
|
|
static int do_load_system_props(const std::vector<std::string>& args) {
|
|
|
|
load_system_props();
|
2015-08-27 02:43:36 +08:00
|
|
|
return 0;
|
2014-06-17 06:06:21 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_wait(const std::vector<std::string>& args) {
|
2015-07-31 04:52:55 +08:00
|
|
|
if (args.size() == 2) {
|
|
|
|
return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
|
|
|
|
} else if (args.size() == 3) {
|
|
|
|
return wait_for_file(args[1].c_str(), std::stoi(args[2]));
|
2011-02-05 02:51:39 +08:00
|
|
|
} else
|
|
|
|
return -1;
|
2010-04-20 08:10:24 +08:00
|
|
|
}
|
2015-03-26 23:49:42 +08:00
|
|
|
|
2015-04-29 06:07:10 +08:00
|
|
|
/*
|
|
|
|
* Callback to make a directory from the ext4 code
|
|
|
|
*/
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_installkeys_ensure_dir_exists(const char* dir) {
|
2015-04-29 06:07:10 +08:00
|
|
|
if (make_dir(dir, 0700) && errno != EEXIST) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:35:06 +08:00
|
|
|
static bool is_file_crypto() {
|
2015-07-25 04:58:03 +08:00
|
|
|
std::string value = property_get("ro.crypto.type");
|
|
|
|
return value == "file";
|
2015-05-29 00:35:06 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
static int do_installkey(const std::vector<std::string>& args) {
|
2015-05-29 00:35:06 +08:00
|
|
|
if (!is_file_crypto()) {
|
2015-04-29 06:07:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-07-31 04:52:55 +08:00
|
|
|
return e4crypt_create_device_key(args[1].c_str(),
|
2015-04-29 06:07:10 +08:00
|
|
|
do_installkeys_ensure_dir_exists);
|
2015-03-26 23:49:42 +08:00
|
|
|
}
|
2015-05-29 00:35:06 +08:00
|
|
|
|
2015-09-02 06:05:34 +08:00
|
|
|
static int do_setusercryptopolicies(const std::vector<std::string>& args) {
|
2015-05-29 00:35:06 +08:00
|
|
|
if (!is_file_crypto()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-08-04 05:19:35 +08:00
|
|
|
return e4crypt_set_user_crypto_policies(args[1].c_str());
|
2015-05-29 00:35:06 +08:00
|
|
|
}
|
2015-09-02 06:05:34 +08:00
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
|
|
|
|
constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
|
|
|
|
static const Map builtin_functions = {
|
|
|
|
{"bootchart_init", {0, 0, do_bootchart_init}},
|
|
|
|
{"chmod", {2, 2, do_chmod}},
|
|
|
|
{"chown", {2, 3, do_chown}},
|
|
|
|
{"class_reset", {1, 1, do_class_reset}},
|
|
|
|
{"class_start", {1, 1, do_class_start}},
|
|
|
|
{"class_stop", {1, 1, do_class_stop}},
|
|
|
|
{"copy", {2, 2, do_copy}},
|
|
|
|
{"domainname", {1, 1, do_domainname}},
|
|
|
|
{"enable", {1, 1, do_enable}},
|
|
|
|
{"exec", {1, kMax, do_exec}},
|
|
|
|
{"export", {2, 2, do_export}},
|
|
|
|
{"hostname", {1, 1, do_hostname}},
|
|
|
|
{"ifup", {1, 1, do_ifup}},
|
|
|
|
{"insmod", {1, kMax, do_insmod}},
|
|
|
|
{"installkey", {1, 1, do_installkey}},
|
|
|
|
{"load_persist_props", {0, 0, do_load_persist_props}},
|
2015-09-02 06:05:34 +08:00
|
|
|
{"load_system_props", {0, 0, do_load_system_props}},
|
2015-08-27 02:43:36 +08:00
|
|
|
{"loglevel", {1, 1, do_loglevel}},
|
|
|
|
{"mkdir", {1, 4, do_mkdir}},
|
2016-01-14 11:18:21 +08:00
|
|
|
{"mount_all", {1, kMax, do_mount_all}},
|
2015-08-27 02:43:36 +08:00
|
|
|
{"mount", {3, kMax, do_mount}},
|
|
|
|
{"powerctl", {1, 1, do_powerctl}},
|
|
|
|
{"restart", {1, 1, do_restart}},
|
|
|
|
{"restorecon", {1, kMax, do_restorecon}},
|
|
|
|
{"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
|
|
|
|
{"rm", {1, 1, do_rm}},
|
|
|
|
{"rmdir", {1, 1, do_rmdir}},
|
|
|
|
{"setprop", {2, 2, do_setprop}},
|
|
|
|
{"setrlimit", {3, 3, do_setrlimit}},
|
2015-09-02 06:05:34 +08:00
|
|
|
{"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
|
2015-08-27 02:43:36 +08:00
|
|
|
{"start", {1, 1, do_start}},
|
|
|
|
{"stop", {1, 1, do_stop}},
|
|
|
|
{"swapon_all", {1, 1, do_swapon_all}},
|
|
|
|
{"symlink", {2, 2, do_symlink}},
|
|
|
|
{"sysclktz", {1, 1, do_sysclktz}},
|
|
|
|
{"trigger", {1, 1, do_trigger}},
|
|
|
|
{"verity_load_state", {0, 0, do_verity_load_state}},
|
|
|
|
{"verity_update_state", {0, 0, do_verity_update_state}},
|
|
|
|
{"wait", {1, 2, do_wait}},
|
|
|
|
{"write", {2, 2, do_write}},
|
|
|
|
};
|
|
|
|
return builtin_functions;
|
|
|
|
}
|