GoogleGit

blob: af7e1892ed63c8ae9adbffffb7d7ccfd805b4018 [file] [log] [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. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <mntent.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/cdefs.h>
  24. #include <sys/mount.h>
  25. #include <sys/reboot.h>
  26. #include <sys/stat.h>
  27. #include <sys/syscall.h>
  28. #include <sys/types.h>
  29. #include <unistd.h>
  30. #include <cutils/android_reboot.h>
  31. #include <cutils/klog.h>
  32. #include <cutils/list.h>
  33. #define TAG "android_reboot"
  34. #define READONLY_CHECK_MS 5000
  35. #define READONLY_CHECK_TIMES 50
  36. typedef struct {
  37. struct listnode list;
  38. struct mntent entry;
  39. } mntent_list;
  40. static bool has_mount_option(const char* opts, const char* opt_to_find)
  41. {
  42. bool ret = false;
  43. char* copy = NULL;
  44. char* opt;
  45. char* rem;
  46. while ((opt = strtok_r(copy ? NULL : (copy = strdup(opts)), ",", &rem))) {
  47. if (!strcmp(opt, opt_to_find)) {
  48. ret = true;
  49. break;
  50. }
  51. }
  52. free(copy);
  53. return ret;
  54. }
  55. static bool is_block_device(const char* fsname)
  56. {
  57. return !strncmp(fsname, "/dev/block", 10);
  58. }
  59. /* Find all read+write block devices in /proc/mounts and add them to
  60. * |rw_entries|.
  61. */
  62. static void find_rw(struct listnode* rw_entries)
  63. {
  64. FILE* fp;
  65. struct mntent* mentry;
  66. if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
  67. KLOG_WARNING(TAG, "Failed to open /proc/mounts.\n");
  68. return;
  69. }
  70. while ((mentry = getmntent(fp)) != NULL) {
  71. if (is_block_device(mentry->mnt_fsname) &&
  72. has_mount_option(mentry->mnt_opts, "rw")) {
  73. mntent_list* item = (mntent_list*)calloc(1, sizeof(mntent_list));
  74. item->entry = *mentry;
  75. item->entry.mnt_fsname = strdup(mentry->mnt_fsname);
  76. item->entry.mnt_dir = strdup(mentry->mnt_dir);
  77. item->entry.mnt_type = strdup(mentry->mnt_type);
  78. item->entry.mnt_opts = strdup(mentry->mnt_opts);
  79. list_add_tail(rw_entries, &item->list);
  80. }
  81. }
  82. endmntent(fp);
  83. }
  84. static void free_entries(struct listnode* entries)
  85. {
  86. struct listnode* node;
  87. struct listnode* n;
  88. list_for_each_safe(node, n, entries) {
  89. mntent_list* item = node_to_item(node, mntent_list, list);
  90. free(item->entry.mnt_fsname);
  91. free(item->entry.mnt_dir);
  92. free(item->entry.mnt_type);
  93. free(item->entry.mnt_opts);
  94. free(item);
  95. }
  96. }
  97. static mntent_list* find_item(struct listnode* rw_entries, const char* fsname_to_find)
  98. {
  99. struct listnode* node;
  100. list_for_each(node, rw_entries) {
  101. mntent_list* item = node_to_item(node, mntent_list, list);
  102. if (!strcmp(item->entry.mnt_fsname, fsname_to_find)) {
  103. return item;
  104. }
  105. }
  106. return NULL;
  107. }
  108. /* Remounting filesystems read-only is difficult when there are files
  109. * opened for writing or pending deletes on the filesystem. There is
  110. * no way to force the remount with the mount(2) syscall. The magic sysrq
  111. * 'u' command does an emergency remount read-only on all writable filesystems
  112. * that have a block device (i.e. not tmpfs filesystems) by calling
  113. * emergency_remount(), which knows how to force the remount to read-only.
  114. * Unfortunately, that is asynchronous, and just schedules the work and
  115. * returns. The best way to determine if it is done is to read /proc/mounts
  116. * repeatedly until there are no more writable filesystems mounted on
  117. * block devices.
  118. */
  119. static void remount_ro(void (*cb_on_remount)(const struct mntent*))
  120. {
  121. int fd, cnt;
  122. FILE* fp;
  123. struct mntent* mentry;
  124. struct listnode* node;
  125. list_declare(rw_entries);
  126. list_declare(ro_entries);
  127. sync();
  128. find_rw(&rw_entries);
  129. /* Trigger the remount of the filesystems as read-only,
  130. * which also marks them clean.
  131. */
  132. fd = TEMP_FAILURE_RETRY(open("/proc/sysrq-trigger", O_WRONLY));
  133. if (fd < 0) {
  134. KLOG_WARNING(TAG, "Failed to open sysrq-trigger.\n");
  135. /* TODO: Try to remount each rw parition manually in readonly mode.
  136. * This may succeed if no process is using the partition.
  137. */
  138. goto out;
  139. }
  140. if (TEMP_FAILURE_RETRY(write(fd, "u", 1)) != 1) {
  141. close(fd);
  142. KLOG_WARNING(TAG, "Failed to write to sysrq-trigger.\n");
  143. /* TODO: The same. Manually remount the paritions. */
  144. goto out;
  145. }
  146. close(fd);
  147. /* Now poll /proc/mounts till it's done */
  148. cnt = 0;
  149. while (cnt < READONLY_CHECK_TIMES) {
  150. if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
  151. /* If we can't read /proc/mounts, just give up. */
  152. KLOG_WARNING(TAG, "Failed to open /proc/mounts.\n");
  153. goto out;
  154. }
  155. while ((mentry = getmntent(fp)) != NULL) {
  156. if (!is_block_device(mentry->mnt_fsname) ||
  157. !has_mount_option(mentry->mnt_opts, "ro")) {
  158. continue;
  159. }
  160. mntent_list* item = find_item(&rw_entries, mentry->mnt_fsname);
  161. if (item) {
  162. /* |item| has now been ro remounted. */
  163. list_remove(&item->list);
  164. list_add_tail(&ro_entries, &item->list);
  165. }
  166. }
  167. endmntent(fp);
  168. if (list_empty(&rw_entries)) {
  169. /* All rw block devices are now readonly. */
  170. break;
  171. }
  172. TEMP_FAILURE_RETRY(
  173. usleep(READONLY_CHECK_MS * 1000 / READONLY_CHECK_TIMES));
  174. cnt++;
  175. }
  176. list_for_each(node, &rw_entries) {
  177. mntent_list* item = node_to_item(node, mntent_list, list);
  178. KLOG_WARNING(TAG, "Failed to remount %s in readonly mode.\n",
  179. item->entry.mnt_fsname);
  180. }
  181. if (cb_on_remount) {
  182. list_for_each(node, &ro_entries) {
  183. mntent_list* item = node_to_item(node, mntent_list, list);
  184. cb_on_remount(&item->entry);
  185. }
  186. }
  187. out:
  188. free_entries(&rw_entries);
  189. free_entries(&ro_entries);
  190. }
  191. int android_reboot_with_callback(
  192. int cmd, int flags __unused, const char *arg,
  193. void (*cb_on_remount)(const struct mntent*))
  194. {
  195. int ret;
  196. remount_ro(cb_on_remount);
  197. switch (cmd) {
  198. case ANDROID_RB_RESTART:
  199. ret = reboot(RB_AUTOBOOT);
  200. break;
  201. case ANDROID_RB_POWEROFF:
  202. ret = reboot(RB_POWER_OFF);
  203. break;
  204. case ANDROID_RB_RESTART2:
  205. ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
  206. LINUX_REBOOT_CMD_RESTART2, arg);
  207. break;
  208. default:
  209. ret = -1;
  210. }
  211. return ret;
  212. }
  213. int android_reboot(int cmd, int flags, const char *arg)
  214. {
  215. return android_reboot_with_callback(cmd, flags, arg, NULL);
  216. }