remove emergency shutdown and improve init's reboot logic
- Emergency shutdown just marks the fs as clean while leaving fs
in the middle of any state. Do not use it anymore.
- Changed android_reboot to set sys.powerctl property so that
all shutdown can be done by init.
- Normal reboot sequence changed to
1. Terminate processes (give time to clean up). And wait for
completion based on ro.build.shutdown_timeout.
Default value (when not set) is changed to 3 secs. If it is 0, do not
terminate processes.
2. Kill all remaining services except critical services for shutdown.
3. Shutdown vold using "vdc volume shutdown"
4. umount all emulated partitions. If it fails, just detach.
Wait in step 5 can handle it.
5. Try umounting R/W block devices for up to max timeout.
If it fails, try DETACH.
If umount fails to complete before reboot, it can be detected when
system reboots.
6. Reboot
- Log shutdown time and umount stat to log so that it can be collected after reboot
- To umount emulated partitions, all pending writes inside kernel should
be completed.
- To umount /data partition, all emulated partitions on top of /data should
be umounted and all pending writes should be completed.
- umount retry will only wait up to timeout. If there are too many pending
writes, reboot will discard them and e2fsck after reboot will fix any file system
issues.
bug: 36004738
bug: 32246772
Test: many reboots combining reboot from UI and adb reboot. Check last_kmsg and
fs_stat after reboot.
Change-Id: I6e74d6c68a21e76e08cc0438573d1586fd9aaee2
diff --git a/libcutils/android_reboot.c b/libcutils/android_reboot.c
index 06026d1..a33e45f 100644
--- a/libcutils/android_reboot.c
+++ b/libcutils/android_reboot.c
@@ -13,259 +13,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-#include <errno.h>
-#include <fcntl.h>
-#include <mntent.h>
-#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <sys/cdefs.h>
-#include <sys/mount.h>
-#include <sys/reboot.h>
-#include <sys/stat.h>
-#include <sys/syscall.h>
-#include <sys/types.h>
-#include <unistd.h>
#include <cutils/android_reboot.h>
-#include <cutils/klog.h>
-#include <cutils/list.h>
+#include <cutils/properties.h>
#define TAG "android_reboot"
-#define READONLY_CHECK_MS 5000
-#define READONLY_CHECK_TIMES 50
-typedef struct {
- struct listnode list;
- struct mntent entry;
-} mntent_list;
-
-static bool is_block_device(const char* fsname)
-{
- return !strncmp(fsname, "/dev/block", 10);
-}
-
-/* Find all read+write block devices in /proc/mounts and add them to
- * |rw_entries|.
- */
-static void find_rw(struct listnode* rw_entries)
-{
- FILE* fp;
- struct mntent* mentry;
-
- if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
- KLOG_WARNING(TAG, "Failed to open /proc/mounts.\n");
- return;
- }
- while ((mentry = getmntent(fp)) != NULL) {
- if (is_block_device(mentry->mnt_fsname) && hasmntopt(mentry, "rw")) {
- mntent_list* item = (mntent_list*)calloc(1, sizeof(mntent_list));
- item->entry = *mentry;
- item->entry.mnt_fsname = strdup(mentry->mnt_fsname);
- item->entry.mnt_dir = strdup(mentry->mnt_dir);
- item->entry.mnt_type = strdup(mentry->mnt_type);
- item->entry.mnt_opts = strdup(mentry->mnt_opts);
- list_add_tail(rw_entries, &item->list);
- }
- }
- endmntent(fp);
-}
-
-static void free_entries(struct listnode* entries)
-{
- struct listnode* node;
- struct listnode* n;
- list_for_each_safe(node, n, entries) {
- mntent_list* item = node_to_item(node, mntent_list, list);
- free(item->entry.mnt_fsname);
- free(item->entry.mnt_dir);
- free(item->entry.mnt_type);
- free(item->entry.mnt_opts);
- free(item);
- }
-}
-
-static mntent_list* find_item(struct listnode* rw_entries, const char* fsname_to_find)
-{
- struct listnode* node;
- list_for_each(node, rw_entries) {
- mntent_list* item = node_to_item(node, mntent_list, list);
- if (!strcmp(item->entry.mnt_fsname, fsname_to_find)) {
- return item;
- }
- }
- return NULL;
-}
-
-/* Remounting filesystems read-only is difficult when there are files
- * opened for writing or pending deletes on the filesystem. There is
- * no way to force the remount with the mount(2) syscall. The magic sysrq
- * 'u' command does an emergency remount read-only on all writable filesystems
- * that have a block device (i.e. not tmpfs filesystems) by calling
- * emergency_remount(), which knows how to force the remount to read-only.
- * Unfortunately, that is asynchronous, and just schedules the work and
- * returns. The best way to determine if it is done is to read /proc/mounts
- * repeatedly until there are no more writable filesystems mounted on
- * block devices.
- */
-static void remount_ro(void (*cb_on_remount)(const struct mntent*))
-{
- int fd, cnt;
- FILE* fp;
- struct mntent* mentry;
- struct listnode* node;
-
- list_declare(rw_entries);
- list_declare(ro_entries);
-
- sync();
- find_rw(&rw_entries);
-
- /* Trigger the remount of the filesystems as read-only,
- * which also marks them clean.
- */
- fd = TEMP_FAILURE_RETRY(open("/proc/sysrq-trigger", O_WRONLY));
- if (fd < 0) {
- KLOG_WARNING(TAG, "Failed to open sysrq-trigger.\n");
- /* TODO: Try to remount each rw parition manually in readonly mode.
- * This may succeed if no process is using the partition.
- */
- goto out;
- }
- if (TEMP_FAILURE_RETRY(write(fd, "u", 1)) != 1) {
- close(fd);
- KLOG_WARNING(TAG, "Failed to write to sysrq-trigger.\n");
- /* TODO: The same. Manually remount the paritions. */
- goto out;
- }
- close(fd);
-
- /* Now poll /proc/mounts till it's done */
- cnt = 0;
- while (cnt < READONLY_CHECK_TIMES) {
- if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
- /* If we can't read /proc/mounts, just give up. */
- KLOG_WARNING(TAG, "Failed to open /proc/mounts.\n");
- goto out;
- }
- while ((mentry = getmntent(fp)) != NULL) {
- if (!is_block_device(mentry->mnt_fsname) || !hasmntopt(mentry, "ro")) {
- continue;
- }
- mntent_list* item = find_item(&rw_entries, mentry->mnt_fsname);
- if (item) {
- /* |item| has now been ro remounted. */
- list_remove(&item->list);
- list_add_tail(&ro_entries, &item->list);
- }
- }
- endmntent(fp);
- if (list_empty(&rw_entries)) {
- /* All rw block devices are now readonly. */
- break;
- }
- TEMP_FAILURE_RETRY(
- usleep(READONLY_CHECK_MS * 1000 / READONLY_CHECK_TIMES));
- cnt++;
- }
-
- list_for_each(node, &rw_entries) {
- mntent_list* item = node_to_item(node, mntent_list, list);
- KLOG_WARNING(TAG, "Failed to remount %s in readonly mode.\n",
- item->entry.mnt_fsname);
- }
-
- if (cb_on_remount) {
- list_for_each(node, &ro_entries) {
- mntent_list* item = node_to_item(node, mntent_list, list);
- cb_on_remount(&item->entry);
- }
- }
-
-out:
- free_entries(&rw_entries);
- free_entries(&ro_entries);
-}
-
-static void save_reboot_reason(int cmd, const char *arg)
-{
- FILE *fp;
- const char *reason = NULL;
-
- fp = fopen(LAST_REBOOT_REASON_FILE, "w");
- if (fp == NULL) {
- KLOG_WARNING(TAG, "Error creating " LAST_REBOOT_REASON_FILE
- ": %s\n", strerror(errno));
- return;
- }
- switch (cmd) {
- case ANDROID_RB_RESTART:
- reason = "restart";
- break;
-
- case ANDROID_RB_POWEROFF:
- reason = "power-off";
- break;
-
- case ANDROID_RB_RESTART2:
- reason = arg && strlen(arg) ? arg : "restart";
- break;
-
- case ANDROID_RB_THERMOFF:
- reason = "thermal-shutdown";
- break;
-
- default:
- fprintf(fp,"0x%08X\n", cmd);
- break;
- }
-
- if (reason) {
- if (fprintf(fp, "%s\n", reason) < 0) {
- KLOG_WARNING(TAG, "Error writing " LAST_REBOOT_REASON_FILE
- ": %s\n", strerror(errno));
- }
- }
-
- fclose(fp);
-}
-
-int android_reboot_with_callback(
- int cmd, int flags __unused, const char *arg,
- void (*cb_on_remount)(const struct mntent*))
-{
+int android_reboot(int cmd, int flags __unused, const char* arg) {
int ret;
+ const char* restart_cmd = NULL;
+ char* prop_value;
- save_reboot_reason(cmd, arg);
- remount_ro(cb_on_remount);
switch (cmd) {
- case ANDROID_RB_RESTART:
- ret = reboot(RB_AUTOBOOT);
- break;
-
- case ANDROID_RB_POWEROFF:
- ret = reboot(RB_POWER_OFF);
- break;
-
+ case ANDROID_RB_RESTART: // deprecated
case ANDROID_RB_RESTART2:
- ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
- LINUX_REBOOT_CMD_RESTART2, arg);
+ restart_cmd = "reboot";
break;
-
+ case ANDROID_RB_POWEROFF:
+ restart_cmd = "shutdown";
+ break;
case ANDROID_RB_THERMOFF:
- ret = reboot(RB_POWER_OFF);
+ restart_cmd = "thermal-shutdown";
break;
-
- default:
- ret = -1;
}
-
+ if (!restart_cmd) return -1;
+ if (arg) {
+ ret = asprintf(&prop_value, "%s,%s", restart_cmd, arg);
+ } else {
+ ret = asprintf(&prop_value, "%s", restart_cmd);
+ }
+ if (ret < 0) return -1;
+ ret = property_set(ANDROID_RB_PROPERTY, prop_value);
+ free(prop_value);
return ret;
}
-
-int android_reboot(int cmd, int flags, const char *arg)
-{
- return android_reboot_with_callback(cmd, flags, arg, NULL);
-}