1 /*
2  * Copyright (C) 2020 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 "first_stage_console.h"
18 
19 #include <stdio.h>
20 #include <sys/stat.h>
21 #include <sys/sysmacros.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <termios.h>
25 
26 #include <string>
27 #include <thread>
28 
29 #include <android-base/chrono_utils.h>
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
32 
KernelConsolePresent(const std::string & cmdline)33 static bool KernelConsolePresent(const std::string& cmdline) {
34     size_t pos = 0;
35     while (true) {
36         pos = cmdline.find("console=", pos);
37         if (pos == std::string::npos) return false;
38         if (pos == 0 || cmdline[pos - 1] == ' ') return true;
39         pos++;
40     }
41 }
42 
SetupConsole()43 static bool SetupConsole() {
44     if (mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1))) {
45         PLOG(ERROR) << "unable to create /dev/console";
46         return false;
47     }
48     int fd = -1;
49     int tries = 50;  // should timeout after 5s
50     // The device driver for console may not be ready yet so retry for a while in case of failure.
51     while (tries--) {
52         fd = open("/dev/console", O_RDWR);
53         if (fd != -1) break;
54         std::this_thread::sleep_for(100ms);
55     }
56     if (fd == -1) {
57         PLOG(ERROR) << "could not open /dev/console";
58         return false;
59     }
60     ioctl(fd, TIOCSCTTY, 0);
61     dup2(fd, STDIN_FILENO);
62     dup2(fd, STDOUT_FILENO);
63     dup2(fd, STDERR_FILENO);
64     close(fd);
65     return true;
66 }
67 
RunScript()68 static void RunScript() {
69     LOG(INFO) << "Attempting to run /first_stage.sh...";
70     pid_t pid = fork();
71     if (pid != 0) {
72         int status;
73         waitpid(pid, &status, 0);
74         LOG(INFO) << "/first_stage.sh exited with status " << status;
75         return;
76     }
77     const char* path = "/system/bin/sh";
78     const char* args[] = {path, "/first_stage.sh", nullptr};
79     int rv = execv(path, const_cast<char**>(args));
80     LOG(ERROR) << "unable to execv /first_stage.sh, returned " << rv << " errno " << errno;
81 }
82 
83 namespace android {
84 namespace init {
85 
StartConsole(const std::string & cmdline)86 void StartConsole(const std::string& cmdline) {
87     bool console = KernelConsolePresent(cmdline);
88     // Use a simple sigchld handler -- first_stage_console doesn't need to track or log zombies
89     const struct sigaction chld_act { .sa_handler = SIG_DFL, .sa_flags = SA_NOCLDWAIT };
90 
91     sigaction(SIGCHLD, &chld_act, nullptr);
92     pid_t pid = fork();
93     if (pid != 0) {
94         int status;
95         waitpid(pid, &status, 0);
96         LOG(ERROR) << "console shell exited with status " << status;
97         return;
98     }
99 
100     if (console) console = SetupConsole();
101     RunScript();
102     if (console) {
103         const char* path = "/system/bin/sh";
104         const char* args[] = {path, nullptr};
105         int rv = execv(path, const_cast<char**>(args));
106         LOG(ERROR) << "unable to execv, returned " << rv << " errno " << errno;
107     }
108     _exit(127);
109 }
110 
FirstStageConsole(const std::string & cmdline,const std::string & bootconfig)111 int FirstStageConsole(const std::string& cmdline, const std::string& bootconfig) {
112     auto pos = bootconfig.find("androidboot.first_stage_console =");
113     if (pos != std::string::npos) {
114         int val = 0;
115         if (sscanf(bootconfig.c_str() + pos, "androidboot.first_stage_console = \"%d\"", &val) !=
116             1) {
117             return FirstStageConsoleParam::DISABLED;
118         }
119         if (val <= FirstStageConsoleParam::MAX_PARAM_VALUE && val >= 0) {
120             return val;
121         }
122     }
123 
124     pos = cmdline.find("androidboot.first_stage_console=");
125     if (pos != std::string::npos) {
126         int val = 0;
127         if (sscanf(cmdline.c_str() + pos, "androidboot.first_stage_console=%d", &val) != 1) {
128             return FirstStageConsoleParam::DISABLED;
129         }
130         if (val <= FirstStageConsoleParam::MAX_PARAM_VALUE && val >= 0) {
131             return val;
132         }
133     }
134     return FirstStageConsoleParam::DISABLED;
135 }
136 
137 }  // namespace init
138 }  // namespace android
139