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