diff options
Diffstat (limited to 'init/service.cpp')
-rw-r--r-- | init/service.cpp | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/init/service.cpp b/init/service.cpp index bdecc324f..e509f46e2 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -172,6 +172,7 @@ bool Service::HandleClass(const std::vector<std::string>& args, std::string* err bool Service::HandleConsole(const std::vector<std::string>& args, std::string* err) { flags_ |= SVC_CONSOLE; + console_ = args.size() > 1 ? "/dev/" + args[1] : ""; return true; } @@ -282,7 +283,7 @@ Service::OptionHandlerMap::Map& Service::OptionHandlerMap::map() const { constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); static const Map option_handlers = { {"class", {1, 1, &Service::HandleClass}}, - {"console", {0, 0, &Service::HandleConsole}}, + {"console", {0, 1, &Service::HandleConsole}}, {"critical", {0, 0, &Service::HandleCritical}}, {"disabled", {0, 0, &Service::HandleDisabled}}, {"group", {1, NR_SVC_SUPP_GIDS + 1, &Service::HandleGroup}}, @@ -329,10 +330,18 @@ bool Service::Start(const std::vector<std::string>& dynamic_args) { } bool needs_console = (flags_ & SVC_CONSOLE); - if (needs_console && !have_console) { - ERROR("service '%s' requires console\n", name_.c_str()); - flags_ |= SVC_DISABLED; - return false; + if (needs_console) { + if (console_.empty()) { + console_ = default_console; + } + + bool have_console = (open(console_.c_str(), O_RDWR | O_CLOEXEC) != -1); + if (!have_console) { + ERROR("service '%s' couldn't open console '%s': %s\n", + name_.c_str(), console_.c_str(), strerror(errno)); + flags_ |= SVC_DISABLED; + return false; + } } struct stat sb; @@ -606,10 +615,8 @@ void Service::ZapStdio() const { } void Service::OpenConsole() const { - int fd; - if ((fd = open(console_name.c_str(), O_RDWR)) < 0) { - fd = open("/dev/null", O_RDWR); - } + int fd = open(console_.c_str(), O_RDWR); + if (fd == -1) fd = open("/dev/null", O_RDWR); ioctl(fd, TIOCSCTTY, 0); dup2(fd, 0); dup2(fd, 1); |