1/*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <unistd.h>
18#include <sys/reboot.h>
19#include <sys/syscall.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <mntent.h>
24#include <stdio.h>
25#include <string.h>
26
27#include <cutils/android_reboot.h>
28
29#define UNUSED __attribute__((unused))
30
31/* Check to see if /proc/mounts contains any writeable filesystems
32 * backed by a block device.
33 * Return true if none found, else return false.
34 */
35static int remount_ro_done(void)
36{
37    FILE* fp;
38    struct mntent* mentry;
39    int found_rw_fs = 0;
40
41    if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
42        /* If we can't read /proc/mounts, just give up. */
43        return 1;
44    }
45    while ((mentry = getmntent(fp)) != NULL) {
46        if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) {
47            found_rw_fs = 1;
48            break;
49        }
50    }
51    endmntent(fp);
52
53    return !found_rw_fs;
54}
55
56/* Remounting filesystems read-only is difficult when there are files
57 * opened for writing or pending deletes on the filesystem.  There is
58 * no way to force the remount with the mount(2) syscall.  The magic sysrq
59 * 'u' command does an emergency remount read-only on all writable filesystems
60 * that have a block device (i.e. not tmpfs filesystems) by calling
61 * emergency_remount(), which knows how to force the remount to read-only.
62 * Unfortunately, that is asynchronous, and just schedules the work and
63 * returns.  The best way to determine if it is done is to read /proc/mounts
64 * repeatedly until there are no more writable filesystems mounted on
65 * block devices.
66 */
67static void remount_ro(void)
68{
69    int fd, cnt = 0;
70
71    /* Trigger the remount of the filesystems as read-only,
72     * which also marks them clean.
73     */
74    fd = open("/proc/sysrq-trigger", O_WRONLY);
75    if (fd < 0) {
76        return;
77    }
78    write(fd, "u", 1);
79    close(fd);
80
81
82    /* Now poll /proc/mounts till it's done */
83    while (!remount_ro_done() && (cnt < 50)) {
84        usleep(100000);
85        cnt++;
86    }
87
88    return;
89}
90
91
92int android_reboot(int cmd, int flags UNUSED, const char *arg)
93{
94    int ret;
95
96    sync();
97    remount_ro();
98
99    switch (cmd) {
100        case ANDROID_RB_RESTART:
101            ret = reboot(RB_AUTOBOOT);
102            break;
103
104        case ANDROID_RB_POWEROFF:
105            ret = reboot(RB_POWER_OFF);
106            break;
107
108        case ANDROID_RB_RESTART2:
109            ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
110                           LINUX_REBOOT_CMD_RESTART2, arg);
111            break;
112
113        default:
114            ret = -1;
115    }
116
117    return ret;
118}
119
120