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"
31 #define DUMP_TARGET "/dev/hvc1" // See virtualizationserice/crosvm.rs
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
main()44 int main() {
45 // Disable buffering for better display of the progress
46 if (setvbuf(stdout, NULL, _IONBF, 0) != 0) {
47 fprintf(stderr, "Failed to disable buffering for stdout: %s\n", strerror(errno));
48 // This isn't a critical error. Continue.
49 }
50
51 printf("Crashdump started\n");
52
53 if (mount("proc", "/proc", "proc", 0, NULL) == -1) {
54 FAIL("Failed to mount /proc");
55 }
56
57 if (mount("devtmpfs", "/dev", "devtmpfs", 0, NULL) == -1) {
58 FAIL("Failed to mount /dev");
59 }
60
61 int vmcore = open(DUMP_SOURCE, O_RDONLY);
62 if (vmcore == -1) {
63 FAIL("Failed to open %s", DUMP_SOURCE);
64 }
65
66 int dest = open(DUMP_TARGET, O_WRONLY);
67 if (dest == -1) {
68 FAIL("Failed to open %s", DUMP_TARGET);
69 }
70
71 // We need to turn the line discipline off, otherwise the virtio-console will automatically
72 // append more data than what we have written because some will be recognized as a control
73 // sequence.
74 struct termios term;
75 if (tcgetattr(dest, &term) != 0) {
76 FAIL("Failed to get termios for %s", DUMP_TARGET);
77 }
78
79 cfmakeraw(&term); // Always successful. Returns void.
80
81 if (tcsetattr(dest, TCSAFLUSH, &term) != 0) {
82 FAIL("Failed to set terminal to the raw mode for %s", DUMP_TARGET);
83 }
84
85 struct stat statbuf;
86 if (fstat(vmcore, &statbuf) == -1) {
87 FAIL("Failed to stat %s", DUMP_SOURCE);
88 }
89 printf("Size is %ld bytes\n", statbuf.st_size);
90
91 // sendfile(2) is faster, can't be used because /proc/vmcore doesn't support splice_read
92 size_t dumped = 0;
93 char buf[BUF_SIZE];
94 int progress = 0; // percentage
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 }
121