Jiyong Park | 9117f01 | 2022-07-07 15:24:06 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | // This program runs as init in the crash kernel. |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <linux/reboot.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/mount.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <termios.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #define DUMP_SOURCE "/proc/vmcore" |
Jiyong Park | 4afe201 | 2022-07-08 05:38:49 +0900 | [diff] [blame] | 31 | #define DUMP_TARGET "/dev/hvc1" // See virtualizationserice/crosvm.rs |
Jiyong Park | 9117f01 | 2022-07-07 15:24:06 +0900 | [diff] [blame] | 32 | #define BUF_SIZE 4096 |
| 33 | |
| 34 | #define FAIL(format, ...) \ |
| 35 | { \ |
| 36 | fprintf(stderr, format ":%s\n", ##__VA_ARGS__, strerror(errno)); \ |
| 37 | goto fail; \ |
| 38 | } |
| 39 | |
| 40 | // Why declare? __reboot() is the Bionic's system call stub for the reboot syscall. It is |
| 41 | // automatically generated (and is part of API), but Bionic doesn't export this in its headers. |
| 42 | extern int __reboot(int, int, int, void*); |
| 43 | |
| 44 | int main() { |
| 45 | printf("Crashdump started\n"); |
| 46 | |
| 47 | if (mount("proc", "/proc", "proc", 0, NULL) == -1) { |
| 48 | FAIL("Failed to mount /proc"); |
| 49 | } |
| 50 | |
| 51 | if (mount("devtmpfs", "/dev", "devtmpfs", 0, NULL) == -1) { |
| 52 | FAIL("Failed to mount /dev"); |
| 53 | } |
| 54 | |
| 55 | int vmcore = open(DUMP_SOURCE, O_RDONLY); |
| 56 | if (vmcore == -1) { |
| 57 | FAIL("Failed to open %s", DUMP_SOURCE); |
| 58 | } |
| 59 | |
| 60 | int dest = open(DUMP_TARGET, O_WRONLY); |
| 61 | if (dest == -1) { |
| 62 | FAIL("Failed to open %s", DUMP_TARGET); |
| 63 | } |
| 64 | |
| 65 | // We need to turn the line discipline off, otherwise the virtio-console will automatically |
| 66 | // append more data than what we have written because some will be recognized as a control |
| 67 | // sequence. |
| 68 | struct termios term; |
| 69 | if (tcgetattr(dest, &term) != 0) { |
| 70 | FAIL("Failed to get termios for %s", DUMP_TARGET); |
| 71 | } |
| 72 | |
| 73 | cfmakeraw(&term); // Always successful. Returns void. |
| 74 | |
| 75 | if (tcsetattr(dest, TCSAFLUSH, &term) != 0) { |
| 76 | FAIL("Failed to set terminal to the raw mode for %s", DUMP_TARGET); |
| 77 | } |
| 78 | |
| 79 | struct stat statbuf; |
| 80 | if (fstat(vmcore, &statbuf) == -1) { |
| 81 | FAIL("Failed to stat %s", DUMP_SOURCE); |
| 82 | } |
Jiyong Park | 9e5ae63 | 2022-07-08 14:47:23 +0900 | [diff] [blame] | 83 | printf("Size is %ld bytes\n", statbuf.st_size); |
Jiyong Park | 9117f01 | 2022-07-07 15:24:06 +0900 | [diff] [blame] | 84 | |
| 85 | // sendfile(2) is faster, can't be used because /proc/vmcore doesn't support splice_read |
| 86 | size_t dumped = 0; |
| 87 | char buf[BUF_SIZE]; |
| 88 | int progress = 0; // percentage |
| 89 | |
| 90 | // Disable buffering for better display of the progress |
| 91 | if (setvbuf(stdout, NULL, _IONBF, 0) != 0) { |
| 92 | fprintf(stderr, "Failed to disable buffering for stdout: %s\n", strerror(errno)); |
| 93 | // This isn't a critical error. Continue. |
| 94 | } |
| 95 | |
| 96 | while (dumped < statbuf.st_size) { |
| 97 | ssize_t read_bytes = read(vmcore, buf, BUF_SIZE); |
| 98 | if (read_bytes == -1) { |
| 99 | FAIL("Failed to read from %s", DUMP_SOURCE); |
| 100 | } |
| 101 | ssize_t written_bytes = write(dest, buf, read_bytes); |
| 102 | if (written_bytes == -1) { |
| 103 | FAIL("Failed to write to %s", DUMP_TARGET); |
| 104 | } |
| 105 | dumped += written_bytes; |
| 106 | int new_progress = dumped * 100 / statbuf.st_size; |
| 107 | if (new_progress > progress) { |
| 108 | progress = new_progress; |
| 109 | printf("."); |
| 110 | } |
| 111 | } |
| 112 | printf("done\n"); |
| 113 | |
| 114 | __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "kernel panic"); |
| 115 | // Never reach here |
| 116 | |
| 117 | fail: |
| 118 | printf("Crashdump failed\n"); |
| 119 | return 1; |
| 120 | } |