summaryrefslogtreecommitdiff
path: root/init/first_stage_console.cpp
blob: e2ea0ab406908448db12a0fd33006e4eeb1e1fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "first_stage_console.h"

#include <stdio.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>

#include <string>
#include <thread>

#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>

static bool KernelConsolePresent(const std::string& cmdline) {
    size_t pos = 0;
    while (true) {
        pos = cmdline.find("console=", pos);
        if (pos == std::string::npos) return false;
        if (pos == 0 || cmdline[pos - 1] == ' ') return true;
        pos++;
    }
}

static bool SetupConsole() {
    if (mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1))) {
        PLOG(ERROR) << "unable to create /dev/console";
        return false;
    }
    int fd = -1;
    int tries = 50;  // should timeout after 5s
    // The device driver for console may not be ready yet so retry for a while in case of failure.
    while (tries--) {
        fd = open("/dev/console", O_RDWR);
        if (fd != -1) break;
        std::this_thread::sleep_for(100ms);
    }
    if (fd == -1) {
        PLOG(ERROR) << "could not open /dev/console";
        return false;
    }
    ioctl(fd, TIOCSCTTY, 0);
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    close(fd);
    return true;
}

static void RunScript() {
    LOG(INFO) << "Attempting to run /first_stage.sh...";
    pid_t pid = fork();
    if (pid != 0) {
        int status;
        waitpid(pid, &status, 0);
        LOG(INFO) << "/first_stage.sh exited with status " << status;
        return;
    }
    const char* path = "/system/bin/sh";
    const char* args[] = {path, "/first_stage.sh", nullptr};
    int rv = execv(path, const_cast<char**>(args));
    LOG(ERROR) << "unable to execv /first_stage.sh, returned " << rv << " errno " << errno;
}

namespace android {
namespace init {

void StartConsole(const std::string& cmdline) {
    bool console = KernelConsolePresent(cmdline);

    pid_t pid = fork();
    if (pid != 0) {
        int status;
        waitpid(pid, &status, 0);
        LOG(ERROR) << "console shell exited with status " << status;
        return;
    }

    if (console) console = SetupConsole();
    RunScript();
    if (console) {
        const char* path = "/system/bin/sh";
        const char* args[] = {path, nullptr};
        int rv = execv(path, const_cast<char**>(args));
        LOG(ERROR) << "unable to execv, returned " << rv << " errno " << errno;
    }
    _exit(127);
}

int FirstStageConsole(const std::string& cmdline, const std::string& bootconfig) {
    auto pos = bootconfig.find("androidboot.first_stage_console =");
    if (pos != std::string::npos) {
        int val = 0;
        if (sscanf(bootconfig.c_str() + pos, "androidboot.first_stage_console = \"%d\"", &val) !=
            1) {
            return FirstStageConsoleParam::DISABLED;
        }
        if (val <= FirstStageConsoleParam::MAX_PARAM_VALUE && val >= 0) {
            return val;
        }
    }

    pos = cmdline.find("androidboot.first_stage_console=");
    if (pos != std::string::npos) {
        int val = 0;
        if (sscanf(cmdline.c_str() + pos, "androidboot.first_stage_console=%d", &val) != 1) {
            return FirstStageConsoleParam::DISABLED;
        }
        if (val <= FirstStageConsoleParam::MAX_PARAM_VALUE && val >= 0) {
            return val;
        }
    }
    return FirstStageConsoleParam::DISABLED;
}

}  // namespace init
}  // namespace android