blob: aef30544c562745db46f9ffa5ec04b7ef61d3682 [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>
23#include <stdio.h>
24#include <string.h>
25
26#include <cutils/android_reboot.h>
27
28/* Check to see if /proc/mounts contains any writeable filesystems
29 * backed by a block device.
30 * Return true if none found, else return false.
31 */
32static int remount_ro_done(void)
33{
34 FILE *f;
35 char mount_dev[256];
36 char mount_dir[256];
37 char mount_type[256];
38 char mount_opts[256];
39 int mount_freq;
40 int mount_passno;
41 int match;
42 int found_rw_fs = 0;
43
44 f = fopen("/proc/mounts", "r");
45 if (! f) {
46 /* If we can't read /proc/mounts, just give up */
47 return 1;
48 }
49
50 do {
51 match = fscanf(f, "%255s %255s %255s %255s %d %d\n",
52 mount_dev, mount_dir, mount_type,
53 mount_opts, &mount_freq, &mount_passno);
54 mount_dev[255] = 0;
55 mount_dir[255] = 0;
56 mount_type[255] = 0;
57 mount_opts[255] = 0;
58 if ((match == 6) && !strncmp(mount_dev, "/dev/block", 10) && strstr(mount_opts, "rw")) {
59 found_rw_fs = 1;
60 break;
61 }
62 } while (match != EOF);
63
64 fclose(f);
65
66 return !found_rw_fs;
67}
68
69/* Remounting filesystems read-only is difficult when there are files
70 * opened for writing or pending deletes on the filesystem. There is
71 * no way to force the remount with the mount(2) syscall. The magic sysrq
72 * 'u' command does an emergency remount read-only on all writable filesystems
73 * that have a block device (i.e. not tmpfs filesystems) by calling
74 * emergency_remount(), which knows how to force the remount to read-only.
75 * Unfortunately, that is asynchronous, and just schedules the work and
76 * returns. The best way to determine if it is done is to read /proc/mounts
77 * repeatedly until there are no more writable filesystems mounted on
78 * block devices.
79 */
80static void remount_ro(void)
81{
82 int fd, cnt = 0;
83
84 /* Trigger the remount of the filesystems as read-only,
85 * which also marks them clean.
86 */
87 fd = open("/proc/sysrq-trigger", O_WRONLY);
88 if (fd < 0) {
89 return;
90 }
91 write(fd, "u", 1);
92 close(fd);
93
94
95 /* Now poll /proc/mounts till it's done */
96 while (!remount_ro_done() && (cnt < 50)) {
97 usleep(100000);
98 cnt++;
99 }
100
101 return;
102}
103
104
105int android_reboot(int cmd, int flags, char *arg)
106{
107 int ret;
108
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700109 sync();
110 remount_ro();
Ken Sumralle3aeeb42011-03-07 23:29:42 -0800111
112 switch (cmd) {
113 case ANDROID_RB_RESTART:
114 ret = reboot(RB_AUTOBOOT);
115 break;
116
117 case ANDROID_RB_POWEROFF:
118 ret = reboot(RB_POWER_OFF);
119 break;
120
121 case ANDROID_RB_RESTART2:
Pavel Chupindccdb942013-11-21 23:56:37 +0400122 ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
Ken Sumralle3aeeb42011-03-07 23:29:42 -0800123 LINUX_REBOOT_CMD_RESTART2, arg);
124 break;
125
126 default:
127 ret = -1;
128 }
129
130 return ret;
131}
132