| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 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> | 
| Pavel Chupin | dccdb94 | 2013-11-21 23:56:37 +0400 | [diff] [blame] | 19 | #include <sys/syscall.h> | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 20 | #include <sys/types.h> | 
|  | 21 | #include <sys/stat.h> | 
|  | 22 | #include <fcntl.h> | 
| Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 23 | #include <mntent.h> | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 24 | #include <stdio.h> | 
|  | 25 | #include <string.h> | 
|  | 26 |  | 
|  | 27 | #include <cutils/android_reboot.h> | 
|  | 28 |  | 
| Mark Salyzyn | 2b94cc2 | 2013-11-22 07:36:45 -0800 | [diff] [blame] | 29 | #define UNUSED __attribute__((unused)) | 
|  | 30 |  | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 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 | */ | 
|  | 35 | static int remount_ro_done(void) | 
|  | 36 | { | 
| Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 37 | FILE* fp; | 
|  | 38 | struct mntent* mentry; | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 39 | int found_rw_fs = 0; | 
|  | 40 |  | 
| Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 41 | if ((fp = setmntent("/proc/mounts", "r")) == NULL) { | 
|  | 42 | /* If we can't read /proc/mounts, just give up. */ | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 43 | return 1; | 
|  | 44 | } | 
| Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 45 | while ((mentry = getmntent(fp)) != NULL) { | 
|  | 46 | if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) { | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 47 | found_rw_fs = 1; | 
|  | 48 | break; | 
|  | 49 | } | 
| Yabin Cui | d6bd9bf | 2015-01-02 14:02:14 -0800 | [diff] [blame] | 50 | } | 
|  | 51 | endmntent(fp); | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 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 | */ | 
|  | 67 | static 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 Hughes | 0068da6 | 2015-02-03 15:44:16 -0800 | [diff] [blame] | 92 | int android_reboot(int cmd, int flags UNUSED, const char *arg) | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 93 | { | 
|  | 94 | int ret; | 
|  | 95 |  | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 96 | sync(); | 
|  | 97 | remount_ro(); | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 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: | 
| Pavel Chupin | dccdb94 | 2013-11-21 23:56:37 +0400 | [diff] [blame] | 109 | ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | 
| Ken Sumrall | e3aeeb4 | 2011-03-07 23:29:42 -0800 | [diff] [blame] | 110 | LINUX_REBOOT_CMD_RESTART2, arg); | 
|  | 111 | break; | 
|  | 112 |  | 
|  | 113 | default: | 
|  | 114 | ret = -1; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | return ret; | 
|  | 118 | } | 
|  | 119 |  |