2008-10-21 16:00:00 +02: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-26 20:43:36 +02:00
|
|
|
#include "builtins.h"
|
|
|
|
|
2016-04-05 22:43:40 +02:00
|
|
|
#include <dirent.h>
|
2015-03-20 17:45:18 +01:00
|
|
|
#include <errno.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <fcntl.h>
|
2015-07-08 23:57:07 +02:00
|
|
|
#include <mntent.h>
|
2015-03-20 17:45:18 +01:00
|
|
|
#include <net/if.h>
|
2015-07-08 23:57:07 +02:00
|
|
|
#include <signal.h>
|
2016-04-05 17:10:25 +02:00
|
|
|
#include <sched.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-03-20 17:45:18 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/resource.h>
|
2016-03-28 01:55:59 +02:00
|
|
|
#include <sys/syscall.h>
|
2015-01-30 06:31:23 +01:00
|
|
|
#include <sys/time.h>
|
2015-03-20 17:45:18 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-04-18 02:20:16 +02:00
|
|
|
#include <sys/wait.h>
|
2015-03-20 17:45:18 +01:00
|
|
|
#include <unistd.h>
|
2008-11-20 04:37:30 +01:00
|
|
|
#include <linux/loop.h>
|
2016-05-17 12:49:10 +02:00
|
|
|
#include <linux/module.h>
|
2016-05-10 17:52:06 +02:00
|
|
|
#include <ext4_crypt.h>
|
2015-04-29 00:07:10 +02:00
|
|
|
#include <ext4_crypt_init_extensions.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2012-01-13 14:48:47 +01:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
|
2015-03-20 17:45:18 +01:00
|
|
|
#include <fs_mgr.h>
|
2016-04-05 22:43:40 +02:00
|
|
|
#include <android-base/file.h>
|
2015-12-18 20:39:59 +01:00
|
|
|
#include <android-base/parseint.h>
|
2016-05-17 12:49:10 +02:00
|
|
|
#include <android-base/strings.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2016-04-07 00:56:49 +02:00
|
|
|
#include <bootloader_message_writer.h>
|
2015-03-20 17:45:18 +01:00
|
|
|
#include <cutils/partition_utils.h>
|
|
|
|
#include <cutils/android_reboot.h>
|
2015-07-08 23:57:07 +02:00
|
|
|
#include <logwrap/logwrap.h>
|
2015-03-20 17:45:18 +01:00
|
|
|
|
2015-07-24 02:53:11 +02:00
|
|
|
#include "action.h"
|
2015-08-26 20:43:36 +02:00
|
|
|
#include "bootchart.h"
|
2015-07-31 21:45:25 +02:00
|
|
|
#include "devices.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
#include "init.h"
|
2015-07-31 21:45:25 +02:00
|
|
|
#include "init_parser.h"
|
|
|
|
#include "log.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
#include "property_service.h"
|
2015-07-31 21:45:25 +02:00
|
|
|
#include "service.h"
|
2015-12-18 20:39:59 +01:00
|
|
|
#include "signal_handler.h"
|
2010-04-14 05:35:46 +02:00
|
|
|
#include "util.h"
|
2008-10-21 16:00:00 +02: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 06:39:46 +01:00
|
|
|
#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
|
2015-07-08 23:57:07 +02: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 06:39:46 +01:00
|
|
|
|
2015-12-18 20:39:59 +01:00
|
|
|
static const int kTerminateServiceDelayMicroSeconds = 50000;
|
|
|
|
|
2016-05-17 12:49:10 +02:00
|
|
|
static int insmod(const char *filename, const char *options, int flags) {
|
2016-03-28 01:55:59 +02:00
|
|
|
int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
|
|
|
if (fd == -1) {
|
|
|
|
ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
|
2008-10-21 16:00:00 +02:00
|
|
|
return -1;
|
2015-02-06 21:19:48 +01:00
|
|
|
}
|
2016-05-17 12:49:10 +02:00
|
|
|
int rc = syscall(__NR_finit_module, fd, options, flags);
|
2016-03-28 01:55:59 +02:00
|
|
|
if (rc == -1) {
|
|
|
|
ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return rc;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int __ifupdown(const char *interface, int up) {
|
2008-10-21 16:00:00 +02: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-04 19:25:09 +01:00
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
done:
|
|
|
|
close(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:43:40 +02:00
|
|
|
// Turn off backlight while we are performing power down cleanup activities.
|
|
|
|
static void turnOffBacklight() {
|
|
|
|
static const char off[] = "0";
|
|
|
|
|
|
|
|
android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
|
|
|
|
|
|
|
|
static const char backlightDir[] = "/sys/class/backlight";
|
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
|
|
|
|
if (!dir) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *dp;
|
|
|
|
while ((dp = readdir(dir.get())) != NULL) {
|
|
|
|
if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
|
|
|
|
(dp->d_name[0] == '.')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string fileName = android::base::StringPrintf("%s/%s/brightness",
|
|
|
|
backlightDir,
|
|
|
|
dp->d_name);
|
|
|
|
android::base::WriteStringToFile(off, fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 17:52:06 +02:00
|
|
|
static int wipe_data_via_recovery(const std::string& reason) {
|
|
|
|
const std::vector<std::string> options = {"--wipe_data", std::string() + "--reason=" + reason};
|
|
|
|
std::string err;
|
|
|
|
if (!write_bootloader_message(options, &err)) {
|
|
|
|
ERROR("failed to set bootloader message: %s", err.c_str());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
|
|
|
|
while (1) { pause(); } // never reached
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static void unmount_and_fsck(const struct mntent *entry) {
|
2015-07-08 23:57:07 +02: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-07-31 21:45:25 +02:00
|
|
|
ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
|
2015-07-08 23:57:07 +02:00
|
|
|
TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
|
|
|
|
|
2016-04-05 17:10:25 +02:00
|
|
|
// Restart Watchdogd to allow us to complete umounting and fsck
|
|
|
|
Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
|
|
|
|
if (svc) {
|
|
|
|
do {
|
|
|
|
sched_yield(); // do not be so eager, let cleanup have priority
|
|
|
|
ServiceManager::GetInstance().ReapAnyOutstandingChildren();
|
|
|
|
} while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
|
|
|
|
svc->Start();
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:43:40 +02:00
|
|
|
turnOffBacklight();
|
|
|
|
|
2015-07-08 23:57:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 17:10:25 +02:00
|
|
|
// NB: With watchdog still running, there is no cap on the time it takes
|
|
|
|
// to complete the fsck, from the users perspective the device graphics
|
|
|
|
// and responses are locked-up and they may choose to hold the power
|
|
|
|
// button in frustration if it drags out.
|
|
|
|
|
2015-07-08 23:57:07 +02:00
|
|
|
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 10:22:53 +02:00
|
|
|
&st, true, LOG_KLOG, true, NULL, NULL, 0);
|
2015-07-08 23:57:07 +02: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 10:22:53 +02:00
|
|
|
&st, true, LOG_KLOG, true, NULL, NULL, 0);
|
2015-07-08 23:57:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_class_start(const std::vector<std::string>& args) {
|
2008-10-21 16:00:00 +02:00
|
|
|
/* Starting a class does not start services
|
|
|
|
* which are explicitly disabled. They must
|
|
|
|
* be started individually.
|
|
|
|
*/
|
2015-07-31 21:45:25 +02:00
|
|
|
ServiceManager::GetInstance().
|
|
|
|
ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_class_stop(const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02:00
|
|
|
ServiceManager::GetInstance().
|
|
|
|
ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_class_reset(const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02:00
|
|
|
ServiceManager::GetInstance().
|
|
|
|
ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
|
2010-12-04 01:33:31 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_domainname(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
return write_file("/proc/sys/kernel/domainname", args[1].c_str());
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_enable(const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02:00
|
|
|
Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
|
|
|
|
if (!svc) {
|
2014-05-03 06:14:29 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
return svc->Enable();
|
2014-05-03 06:14:29 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_exec(const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02:00
|
|
|
Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
|
|
|
|
if (!svc) {
|
|
|
|
return -1;
|
2015-07-30 22:52:55 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
if (!svc->Start()) {
|
2015-02-07 05:15:18 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
waiting_for_exec = true;
|
2015-02-07 05:15:18 +01:00
|
|
|
return 0;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_export(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
return add_environment(args[1].c_str(), args[2].c_str());
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_hostname(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
return write_file("/proc/sys/kernel/hostname", args[1].c_str());
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_ifup(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
return __ifupdown(args[1].c_str(), 1);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_insmod(const std::vector<std::string>& args) {
|
2016-05-17 12:49:10 +02:00
|
|
|
int flags = 0;
|
|
|
|
auto it = args.begin() + 1;
|
2008-12-18 03:08:08 +01:00
|
|
|
|
2016-05-17 12:49:10 +02:00
|
|
|
if (!(*it).compare("-f")) {
|
|
|
|
flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
|
|
|
|
it++;
|
2008-12-18 03:08:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 12:49:10 +02:00
|
|
|
std::string filename = *it++;
|
|
|
|
std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
|
|
|
|
return insmod(filename.c_str(), options.c_str(), flags);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_mkdir(const std::vector<std::string>& args) {
|
2008-10-21 16:00:00 +02:00
|
|
|
mode_t mode = 0755;
|
2011-07-08 21:57:36 +02:00
|
|
|
int ret;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
/* mkdir <path> [mode] [owner] [group] */
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
if (args.size() >= 3) {
|
|
|
|
mode = std::stoul(args[2], 0, 8);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
ret = make_dir(args[1].c_str(), mode);
|
2011-07-08 21:57:36 +02:00
|
|
|
/* chmod in case the directory already exists */
|
|
|
|
if (ret == -1 && errno == EEXIST) {
|
2015-07-30 22:52:55 +02:00
|
|
|
ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
|
2011-07-08 21:57:36 +02:00
|
|
|
}
|
|
|
|
if (ret == -1) {
|
2008-10-21 16:00:00 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
if (args.size() >= 4) {
|
|
|
|
uid_t uid = decode_uid(args[3].c_str());
|
2008-10-21 16:00:00 +02:00
|
|
|
gid_t gid = -1;
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
if (args.size() == 5) {
|
|
|
|
gid = decode_uid(args[4].c_str());
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
if (lchown(args[1].c_str(), uid, gid) == -1) {
|
2008-10-21 16:00:00 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
2012-08-15 00:43:46 +02:00
|
|
|
|
|
|
|
/* chown may have cleared S_ISUID and S_ISGID, chmod again */
|
|
|
|
if (mode & (S_ISUID | S_ISGID)) {
|
2015-07-30 22:52:55 +02:00
|
|
|
ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
|
2012-08-15 00:43:46 +02:00
|
|
|
if (ret == -1) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 17:52:06 +02:00
|
|
|
if (e4crypt_is_native()) {
|
|
|
|
if (e4crypt_set_directory_policy(args[1].c_str())) {
|
|
|
|
wipe_data_via_recovery(std::string() + "set_policy_failed:" + args[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
unsigned flag;
|
|
|
|
} mount_flags[] = {
|
|
|
|
{ "noatime", MS_NOATIME },
|
2011-07-14 13:39:09 +02:00
|
|
|
{ "noexec", MS_NOEXEC },
|
2008-10-21 16:00:00 +02:00
|
|
|
{ "nosuid", MS_NOSUID },
|
|
|
|
{ "nodev", MS_NODEV },
|
|
|
|
{ "nodiratime", MS_NODIRATIME },
|
|
|
|
{ "ro", MS_RDONLY },
|
|
|
|
{ "rw", 0 },
|
|
|
|
{ "remount", MS_REMOUNT },
|
2012-08-14 20:34:34 +02:00
|
|
|
{ "bind", MS_BIND },
|
|
|
|
{ "rec", MS_REC },
|
|
|
|
{ "unbindable", MS_UNBINDABLE },
|
|
|
|
{ "private", MS_PRIVATE },
|
|
|
|
{ "slave", MS_SLAVE },
|
|
|
|
{ "shared", MS_SHARED },
|
2008-10-21 16:00:00 +02:00
|
|
|
{ "defaults", 0 },
|
|
|
|
{ 0, 0 },
|
|
|
|
};
|
|
|
|
|
2010-12-04 01:33:31 +01:00
|
|
|
#define DATA_MNT_POINT "/data"
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
/* mount <type> <device> <path> <flags ...> <options> */
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_mount(const std::vector<std::string>& args) {
|
2008-10-21 16:00:00 +02:00
|
|
|
char tmp[64];
|
2015-07-30 22:52:55 +02:00
|
|
|
const char *source, *target, *system;
|
|
|
|
const char *options = NULL;
|
2008-10-21 16:00:00 +02:00
|
|
|
unsigned flags = 0;
|
2015-07-30 22:52:55 +02:00
|
|
|
std::size_t na = 0;
|
2008-10-21 16:00:00 +02:00
|
|
|
int n, i;
|
2010-04-20 02:10:24 +02:00
|
|
|
int wait = 0;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
for (na = 4; na < args.size(); na++) {
|
2008-10-21 16:00:00 +02:00
|
|
|
for (i = 0; mount_flags[i].name; i++) {
|
2015-07-30 22:52:55 +02:00
|
|
|
if (!args[na].compare(mount_flags[i].name)) {
|
2008-10-21 16:00:00 +02:00
|
|
|
flags |= mount_flags[i].flag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-20 02:10:24 +02:00
|
|
|
if (!mount_flags[i].name) {
|
2015-07-30 22:52:55 +02:00
|
|
|
if (!args[na].compare("wait"))
|
2010-04-20 02:10:24 +02:00
|
|
|
wait = 1;
|
|
|
|
/* if our last argument isn't a flag, wolf it up as an option string */
|
2015-07-30 22:52:55 +02:00
|
|
|
else if (na + 1 == args.size())
|
|
|
|
options = args[na].c_str();
|
2010-04-20 02:10:24 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
system = args[1].c_str();
|
|
|
|
source = args[2].c_str();
|
|
|
|
target = args[3].c_str();
|
2008-11-20 04:37:30 +01:00
|
|
|
|
2016-06-25 00:15:03 +02:00
|
|
|
if (!strncmp(source, "loop@", 5)) {
|
2008-11-20 04:37:30 +01:00
|
|
|
int mode, loop, fd;
|
|
|
|
struct loop_info info;
|
|
|
|
|
|
|
|
mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
|
2015-02-02 23:37:22 +01:00
|
|
|
fd = open(source + 5, mode | O_CLOEXEC);
|
2008-11-20 04:37:30 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2008-11-20 04:37:30 +01:00
|
|
|
|
|
|
|
for (n = 0; ; n++) {
|
2015-02-18 04:27:51 +01:00
|
|
|
snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
|
2015-02-02 23:37:22 +01:00
|
|
|
loop = open(tmp, mode | O_CLOEXEC);
|
2008-11-20 04:37:30 +01:00
|
|
|
if (loop < 0) {
|
2014-02-06 08:57:27 +01:00
|
|
|
close(fd);
|
2008-11-20 04:37:30 +01: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 03:09:47 +01:00
|
|
|
goto exit_success;
|
2008-11-20 04:37:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
ERROR("out of loopback devices");
|
|
|
|
return -1;
|
|
|
|
} else {
|
2010-04-20 02:10:24 +02:00
|
|
|
if (wait)
|
|
|
|
wait_for_file(source, COMMAND_RETRY_TIMEOUT);
|
2008-11-20 04:37:30 +01:00
|
|
|
if (mount(source, target, system, flags, options) < 0) {
|
2012-04-18 02:20:16 +02:00
|
|
|
return -1;
|
2008-11-20 04:37:30 +01:00
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2011-02-18 03:09:47 +01:00
|
|
|
|
|
|
|
exit_success:
|
2012-04-18 02:20:16 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2011-03-09 02:01:29 +01:00
|
|
|
|
2016-01-14 04:18:21 +01: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-26 20:43:36 +02:00
|
|
|
Parser& parser = Parser::GetInstance();
|
2016-01-14 04:18:21 +01: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-22 23:23:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 04:18:21 +01:00
|
|
|
/* mount_all <fstab> [ <path> ]*
|
|
|
|
*
|
2014-07-02 23:26:54 +02:00
|
|
|
* This function might request a reboot, in which case it will
|
|
|
|
* not return.
|
|
|
|
*/
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_mount_all(const std::vector<std::string>& args) {
|
2012-04-18 02:20:16 +02:00
|
|
|
pid_t pid;
|
|
|
|
int ret = -1;
|
|
|
|
int child_ret = -1;
|
|
|
|
int status;
|
2013-02-13 21:58:40 +01:00
|
|
|
struct fstab *fstab;
|
2012-04-18 02:20:16 +02:00
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
const char* fstabfile = args[1].c_str();
|
2012-04-18 02:20:16 +02: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-16 23:31:23 +02: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 01:05:56 +01:00
|
|
|
NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
|
2014-09-16 23:31:23 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 02:20:16 +02:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
ret = WEXITSTATUS(status);
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
2011-02-18 03:09:47 +01:00
|
|
|
}
|
2012-04-18 02:20:16 +02: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 13:44:07 +02:00
|
|
|
fstab = fs_mgr_read_fstab(fstabfile);
|
2013-02-13 21:58:40 +01:00
|
|
|
child_ret = fs_mgr_mount_all(fstab);
|
|
|
|
fs_mgr_free_fstab(fstab);
|
2012-04-18 02:20:16 +02:00
|
|
|
if (child_ret == -1) {
|
|
|
|
ERROR("fs_mgr_mount_all returned an error\n");
|
2011-02-18 03:09:47 +01:00
|
|
|
}
|
2014-07-02 22:16:04 +02:00
|
|
|
_exit(child_ret);
|
2012-04-18 02:20:16 +02:00
|
|
|
} else {
|
|
|
|
/* fork failed, return an error */
|
|
|
|
return -1;
|
2011-02-18 03:09:47 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 04:18:21 +01:00
|
|
|
/* Paths of .rc files are specified at the 2nd argument and beyond */
|
|
|
|
import_late(args, 2);
|
2015-07-22 23:23:33 +02:00
|
|
|
|
2014-07-02 22:16:04 +02:00
|
|
|
if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
|
2016-04-19 00:37:31 +02:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("encrypt");
|
2014-07-02 22:16:04 +02:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
|
2012-04-18 02:20:16 +02:00
|
|
|
property_set("ro.crypto.state", "encrypted");
|
2015-04-29 00:07:10 +02:00
|
|
|
property_set("ro.crypto.type", "block");
|
2016-04-19 00:37:31 +02:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
|
2014-07-02 22:16:04 +02:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
|
2012-04-18 02:20:16 +02:00
|
|
|
property_set("ro.crypto.state", "unencrypted");
|
2016-03-05 00:52:33 +01:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
|
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
|
|
|
|
property_set("ro.crypto.state", "unsupported");
|
2015-07-24 02:53:11 +02:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
|
2014-07-02 23:26:54 +02: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");
|
2016-05-10 17:52:06 +02:00
|
|
|
ret = wipe_data_via_recovery("wipe_data_via_recovery");
|
2014-07-02 23:26:54 +02:00
|
|
|
/* If reboot worked, there is no return. */
|
2016-02-02 19:31:30 +01:00
|
|
|
} else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
|
2015-03-26 16:49:42 +01:00
|
|
|
if (e4crypt_install_keyring()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
property_set("ro.crypto.state", "encrypted");
|
2015-04-29 00:07:10 +02:00
|
|
|
property_set("ro.crypto.type", "file");
|
2015-03-26 16:49:42 +01:00
|
|
|
|
|
|
|
// Although encrypted, we have device key, so we do not need to
|
|
|
|
// do anything different from the nonencrypted case.
|
2015-07-24 02:53:11 +02:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
|
2014-07-02 23:26:54 +02:00
|
|
|
} else if (ret > 0) {
|
|
|
|
ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
|
2012-04-18 02:20:16 +02:00
|
|
|
}
|
2014-07-02 22:16:04 +02:00
|
|
|
/* else ... < 0: error */
|
2011-02-18 03:09:47 +01:00
|
|
|
|
2012-04-18 02:20:16 +02:00
|
|
|
return ret;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_swapon_all(const std::vector<std::string>& args) {
|
2013-07-10 03:42:09 +02:00
|
|
|
struct fstab *fstab;
|
|
|
|
int ret;
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
fstab = fs_mgr_read_fstab(args[1].c_str());
|
2013-07-10 03:42:09 +02:00
|
|
|
ret = fs_mgr_swapon_all(fstab);
|
|
|
|
fs_mgr_free_fstab(fstab);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_setprop(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
const char* name = args[1].c_str();
|
|
|
|
const char* value = args[2].c_str();
|
2015-07-24 22:26:04 +02:00
|
|
|
property_set(name, value);
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_setrlimit(const std::vector<std::string>& args) {
|
2008-10-21 16:00:00 +02:00
|
|
|
struct rlimit limit;
|
|
|
|
int resource;
|
2015-07-30 22:52:55 +02:00
|
|
|
resource = std::stoi(args[1]);
|
|
|
|
limit.rlim_cur = std::stoi(args[2]);
|
|
|
|
limit.rlim_max = std::stoi(args[3]);
|
2008-10-21 16:00:00 +02:00
|
|
|
return setrlimit(resource, &limit);
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_start(const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02: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 16:00:00 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
if (!svc->Start())
|
|
|
|
return -1;
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_stop(const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02: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 16:00:00 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
svc->Stop();
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_restart(const std::vector<std::string>& args) {
|
2015-07-31 21:45:25 +02: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 16:00:00 +02:00
|
|
|
}
|
2015-07-31 21:45:25 +02:00
|
|
|
svc->Restart();
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_powerctl(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
const char* command = args[1].c_str();
|
2013-04-18 21:20:02 +02:00
|
|
|
int len = 0;
|
2015-07-22 00:50:59 +02:00
|
|
|
unsigned int cmd = 0;
|
|
|
|
const char *reboot_target = "";
|
2015-07-08 23:57:07 +02:00
|
|
|
void (*callback_on_ro_remount)(const struct mntent*) = NULL;
|
2013-04-18 21:20:02 +02: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 00:50:59 +02: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-18 21:20:02 +02:00
|
|
|
ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-12-18 20:39:59 +01: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-08 23:57:07 +02:00
|
|
|
return android_reboot_with_callback(cmd, 0, reboot_target,
|
|
|
|
callback_on_ro_remount);
|
2013-04-18 21:20:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_trigger(const std::vector<std::string>& args) {
|
2015-07-24 02:53:11 +02:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger(args[1]);
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_symlink(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
return symlink(args[1].c_str(), args[2].c_str());
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_rm(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
return unlink(args[1].c_str());
|
2011-01-19 02:37:41 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_rmdir(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
return rmdir(args[1].c_str());
|
2011-01-19 02:37:41 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_sysclktz(const std::vector<std::string>& args) {
|
2008-12-18 03:08:08 +01:00
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
memset(&tz, 0, sizeof(tz));
|
2015-07-30 22:52:55 +02:00
|
|
|
tz.tz_minuteswest = std::stoi(args[1]);
|
2008-12-18 03:08:08 +01:00
|
|
|
if (settimeofday(NULL, &tz))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_verity_load_state(const std::vector<std::string>& args) {
|
2015-03-20 17:45:18 +01:00
|
|
|
int mode = -1;
|
|
|
|
int rc = fs_mgr_load_verity_state(&mode);
|
2015-12-02 15:23:09 +01:00
|
|
|
if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
|
2015-07-24 02:53:11 +02:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("verity-logging");
|
2015-02-16 12:03:34 +01:00
|
|
|
}
|
2015-03-20 17:45:18 +01:00
|
|
|
return rc;
|
2015-02-16 12:03:34 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static void verity_update_property(fstab_rec *fstab, const char *mount_point,
|
|
|
|
int mode, int status) {
|
2015-03-30 12:38:38 +02:00
|
|
|
property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
|
|
|
|
android::base::StringPrintf("%d", mode).c_str());
|
2015-03-19 11:00:34 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_verity_update_state(const std::vector<std::string>& args) {
|
2015-03-20 17:45:18 +01:00
|
|
|
return fs_mgr_update_verity_state(verity_update_property);
|
2015-03-19 11:00:34 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_write(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
const char* path = args[1].c_str();
|
|
|
|
const char* value = args[2].c_str();
|
2015-07-24 22:26:04 +02:00
|
|
|
return write_file(path, value);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_copy(const std::vector<std::string>& args) {
|
2009-08-27 01:39:25 +02:00
|
|
|
char *buffer = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
int fd1 = -1, fd2 = -1;
|
|
|
|
struct stat info;
|
|
|
|
int brtw, brtr;
|
|
|
|
char *p;
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
if (stat(args[1].c_str(), &info) < 0)
|
2009-08-27 01:39:25 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
|
2009-08-27 01:39:25 +02:00
|
|
|
goto out_err;
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
|
2009-08-27 01:39:25 +02:00
|
|
|
goto out_err;
|
|
|
|
|
2015-02-04 02:12:07 +01:00
|
|
|
if (!(buffer = (char*) malloc(info.st_size)))
|
2009-08-27 01:39:25 +02: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-26 20:43:36 +02:00
|
|
|
static int do_chown(const std::vector<std::string>& args) {
|
2008-10-21 16:00:00 +02:00
|
|
|
/* GID is optional. */
|
2015-07-30 22:52:55 +02:00
|
|
|
if (args.size() == 3) {
|
|
|
|
if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
|
2008-10-21 16:00:00 +02:00
|
|
|
return -errno;
|
2015-07-30 22:52:55 +02: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 16:00:00 +02: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-26 20:43:36 +02:00
|
|
|
static int do_chmod(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02: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 16:00:00 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_restorecon(const std::vector<std::string>& args) {
|
2013-10-09 22:02:09 +02:00
|
|
|
int ret = 0;
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
for (auto it = std::next(args.begin()); it != args.end(); ++it) {
|
|
|
|
if (restorecon(it->c_str()) < 0)
|
2013-10-09 22:02:09 +02:00
|
|
|
ret = -errno;
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
2013-10-09 22:02:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_restorecon_recursive(const std::vector<std::string>& args) {
|
2013-10-09 22:02:09 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
2015-07-30 22:52:55 +02:00
|
|
|
for (auto it = std::next(args.begin()); it != args.end(); ++it) {
|
|
|
|
if (restorecon_recursive(it->c_str()) < 0)
|
2013-10-09 22:02:09 +02:00
|
|
|
ret = -errno;
|
|
|
|
}
|
|
|
|
return ret;
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_loglevel(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02:00
|
|
|
int log_level = std::stoi(args[1]);
|
2014-06-26 22:55:03 +02: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 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2015-09-02 00:05:34 +02:00
|
|
|
static int do_load_persist_props(const std::vector<std::string>& args) {
|
2015-08-26 20:43:36 +02:00
|
|
|
load_persist_props();
|
|
|
|
return 0;
|
2011-03-09 02:01:29 +01:00
|
|
|
}
|
|
|
|
|
2015-09-02 00:05:34 +02:00
|
|
|
static int do_load_system_props(const std::vector<std::string>& args) {
|
|
|
|
load_system_props();
|
2015-08-26 20:43:36 +02:00
|
|
|
return 0;
|
2014-06-17 00:06:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_wait(const std::vector<std::string>& args) {
|
2015-07-30 22:52:55 +02: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-04 19:51:39 +01:00
|
|
|
} else
|
|
|
|
return -1;
|
2010-04-20 02:10:24 +02:00
|
|
|
}
|
2015-03-26 16:49:42 +01:00
|
|
|
|
2015-04-29 00:07:10 +02:00
|
|
|
/*
|
|
|
|
* Callback to make a directory from the ext4 code
|
|
|
|
*/
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_installkeys_ensure_dir_exists(const char* dir) {
|
2015-04-29 00:07:10 +02:00
|
|
|
if (make_dir(dir, 0700) && errno != EEXIST) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:35:06 +02:00
|
|
|
static bool is_file_crypto() {
|
2015-07-24 22:58:03 +02:00
|
|
|
std::string value = property_get("ro.crypto.type");
|
|
|
|
return value == "file";
|
2015-05-28 18:35:06 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
static int do_installkey(const std::vector<std::string>& args) {
|
2015-05-28 18:35:06 +02:00
|
|
|
if (!is_file_crypto()) {
|
2015-04-29 00:07:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2015-07-30 22:52:55 +02:00
|
|
|
return e4crypt_create_device_key(args[1].c_str(),
|
2015-04-29 00:07:10 +02:00
|
|
|
do_installkeys_ensure_dir_exists);
|
2015-03-26 16:49:42 +01:00
|
|
|
}
|
2015-05-28 18:35:06 +02:00
|
|
|
|
2016-02-01 17:37:13 +01:00
|
|
|
static int do_init_user0(const std::vector<std::string>& args) {
|
|
|
|
return e4crypt_do_init_user0();
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02: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}},
|
2016-02-01 17:37:13 +01:00
|
|
|
{"init_user0", {0, 0, do_init_user0}},
|
2015-08-26 20:43:36 +02:00
|
|
|
{"insmod", {1, kMax, do_insmod}},
|
|
|
|
{"installkey", {1, 1, do_installkey}},
|
|
|
|
{"load_persist_props", {0, 0, do_load_persist_props}},
|
2015-09-02 00:05:34 +02:00
|
|
|
{"load_system_props", {0, 0, do_load_system_props}},
|
2015-08-26 20:43:36 +02:00
|
|
|
{"loglevel", {1, 1, do_loglevel}},
|
|
|
|
{"mkdir", {1, 4, do_mkdir}},
|
2016-01-14 04:18:21 +01:00
|
|
|
{"mount_all", {1, kMax, do_mount_all}},
|
2015-08-26 20:43:36 +02: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}},
|
|
|
|
{"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;
|
|
|
|
}
|