blob: 6ae23c1f00f59b92c1b1a03a30ee4f597d444373 [file] [log] [blame]
Ken Sumralle3aeeb42011-03-07 23:29:42 -08001/*
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>
Pavel Chupindccdb942013-11-21 23:56:37 +040019#include <sys/syscall.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080020#include <sys/types.h>
21#include <sys/stat.h>
22#include <fcntl.h>
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080023#include <mntent.h>
Ken Sumralle3aeeb42011-03-07 23:29:42 -080024#include <stdio.h>
25#include <string.h>
26
27#include <cutils/android_reboot.h>
28
Mark Salyzyn2b94cc22013-11-22 07:36:45 -080029#define UNUSED __attribute__((unused))
30
Ken Sumralle3aeeb42011-03-07 23:29:42 -080031/* 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{
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080037 FILE* fp;
38 struct mntent* mentry;
Ken Sumralle3aeeb42011-03-07 23:29:42 -080039 int found_rw_fs = 0;
40
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080041 if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
42 /* If we can't read /proc/mounts, just give up. */
Ken Sumralle3aeeb42011-03-07 23:29:42 -080043 return 1;
44 }
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080045 while ((mentry = getmntent(fp)) != NULL) {
46 if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) {
Ken Sumralle3aeeb42011-03-07 23:29:42 -080047 found_rw_fs = 1;
48 break;
49 }
Yabin Cuid6bd9bf2015-01-02 14:02:14 -080050 }
51 endmntent(fp);
Ken Sumralle3aeeb42011-03-07 23:29:42 -080052
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
Elliott Hughes0068da62015-02-03 15:44:16 -080092int android_reboot(int cmd, int flags UNUSED, const char *arg)
Ken Sumralle3aeeb42011-03-07 23:29:42 -080093{
94 int ret;
95
Nick Kralevichca8e66a2013-04-18 12:20:02 -070096 sync();
97 remount_ro();
Ken Sumralle3aeeb42011-03-07 23:29:42 -080098
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:
Pavel Chupindccdb942013-11-21 23:56:37 +0400109 ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
Ken Sumralle3aeeb42011-03-07 23:29:42 -0800110 LINUX_REBOOT_CMD_RESTART2, arg);
111 break;
112
113 default:
114 ret = -1;
115 }
116
117 return ret;
118}
119