2011-03-08 08:29:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2011, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/reboot.h>
|
2013-11-21 20:56:37 +01:00
|
|
|
#include <sys/syscall.h>
|
2011-03-08 08:29:42 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-02 23:02:14 +01:00
|
|
|
#include <mntent.h>
|
2011-03-08 08:29:42 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <cutils/android_reboot.h>
|
|
|
|
|
2013-11-22 16:36:45 +01:00
|
|
|
#define UNUSED __attribute__((unused))
|
|
|
|
|
2011-03-08 08:29:42 +01:00
|
|
|
/* Check to see if /proc/mounts contains any writeable filesystems
|
|
|
|
* backed by a block device.
|
|
|
|
* Return true if none found, else return false.
|
|
|
|
*/
|
|
|
|
static int remount_ro_done(void)
|
|
|
|
{
|
2015-01-02 23:02:14 +01:00
|
|
|
FILE* fp;
|
|
|
|
struct mntent* mentry;
|
2011-03-08 08:29:42 +01:00
|
|
|
int found_rw_fs = 0;
|
|
|
|
|
2015-01-02 23:02:14 +01:00
|
|
|
if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
|
|
|
|
/* If we can't read /proc/mounts, just give up. */
|
2011-03-08 08:29:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-01-02 23:02:14 +01:00
|
|
|
while ((mentry = getmntent(fp)) != NULL) {
|
|
|
|
if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) {
|
2011-03-08 08:29:42 +01:00
|
|
|
found_rw_fs = 1;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-02 23:02:14 +01:00
|
|
|
}
|
|
|
|
endmntent(fp);
|
2011-03-08 08:29:42 +01:00
|
|
|
|
|
|
|
return !found_rw_fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
int fd, cnt = 0;
|
|
|
|
|
|
|
|
/* Trigger the remount of the filesystems as read-only,
|
|
|
|
* which also marks them clean.
|
|
|
|
*/
|
|
|
|
fd = open("/proc/sysrq-trigger", O_WRONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
write(fd, "u", 1);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
|
|
|
|
/* Now poll /proc/mounts till it's done */
|
|
|
|
while (!remount_ro_done() && (cnt < 50)) {
|
|
|
|
usleep(100000);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-04 00:44:16 +01:00
|
|
|
int android_reboot(int cmd, int flags UNUSED, const char *arg)
|
2011-03-08 08:29:42 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-04-18 21:20:02 +02:00
|
|
|
sync();
|
|
|
|
remount_ro();
|
2011-03-08 08:29:42 +01:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case ANDROID_RB_RESTART:
|
|
|
|
ret = reboot(RB_AUTOBOOT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANDROID_RB_POWEROFF:
|
|
|
|
ret = reboot(RB_POWER_OFF);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANDROID_RB_RESTART2:
|
2013-11-21 20:56:37 +01:00
|
|
|
ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
|
2011-03-08 08:29:42 +01:00
|
|
|
LINUX_REBOOT_CMD_RESTART2, arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|