| // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <grp.h> |
| #include <sys/resource.h> |
| #include <sys/types.h> |
| |
| #include <base/file_util.h> |
| #include <base/files/file_path.h> |
| #include <base/logging.h> |
| #include <base/memory/scoped_ptr.h> |
| #include <base/strings/string_util.h> |
| #include <base/strings/stringprintf.h> |
| #include <chromeos/ui/chromium_command_builder.h> |
| #include <chromeos/ui/util.h> |
| #include <chromeos/ui/x_server_runner.h> |
| |
| using chromeos::ui::ChromiumCommandBuilder; |
| using chromeos::ui::XServerRunner; |
| using chromeos::ui::util::EnsureDirectoryExists; |
| using chromeos::ui::util::SetPermissions; |
| |
| namespace { |
| |
| // Authority file used for running the X server. |
| const char kXauthPath[] = "/var/run/x11.auth"; |
| |
| // Path to the app_shell binary. |
| const char kAppShellPath[] = "/opt/google/chrome/app_shell"; |
| |
| // Subdirectory under $DATA_DIR where user data should be stored. |
| const char kUserSubdir[] = "user"; |
| |
| // Files in $DATA_DIR containing the ID of the app that should be loaded and the |
| // name of a preferred network to connect to. |
| const char kAppIdFile[] = "app_id"; |
| const char kPreferredNetworkFile[] = "preferred_network"; |
| |
| // Subdirectory under $DATA_DIR from which an app is loaded if kAppIdFile |
| // doesn't exist. |
| const char kAppSubdir[] = "app"; |
| |
| // Adds app_shell-specific flags. |
| void AddAppShellFlags(ChromiumCommandBuilder* builder) { |
| const base::FilePath data_dir(builder->ReadEnvVar("DATA_DIR")); |
| |
| // Set --data-path to tell app_shell where to store user data. |
| const base::FilePath user_path = data_dir.Append(kUserSubdir); |
| CHECK(EnsureDirectoryExists(user_path, builder->uid(), builder->gid(), 0700)); |
| builder->AddArg("--data-path=" + user_path.value()); |
| |
| std::string app_id; |
| if (base::ReadFileToString(data_dir.Append(kAppIdFile), &app_id)) { |
| base::TrimWhitespaceASCII(app_id, base::TRIM_ALL, &app_id); |
| builder->AddArg("--app-shell-app-id=" + app_id); |
| } else { |
| const base::FilePath app_path = data_dir.Append(kAppSubdir); |
| if (base::PathExists(app_path)) |
| builder->AddArg("--app-shell-app-path=" + app_path.value()); |
| } |
| |
| std::string preferred_network; |
| if (base::ReadFileToString(data_dir.Append(kPreferredNetworkFile), |
| &preferred_network)) { |
| base::TrimWhitespaceASCII( |
| preferred_network, base::TRIM_ALL, &preferred_network); |
| builder->AddArg("--app-shell-preferred-network=" + preferred_network); |
| } |
| } |
| |
| // Replaces the currently-running process with app_shell. |
| void ExecAppShell(const ChromiumCommandBuilder& builder) { |
| if (setpriority(PRIO_PROCESS, 0, -20) != 0) |
| PLOG(WARNING) << "setpriority() failed"; |
| |
| PCHECK(initgroups(ChromiumCommandBuilder::kUser, builder.gid()) == 0); |
| PCHECK(setgid(builder.gid()) == 0); |
| PCHECK(setuid(builder.uid()) == 0); |
| |
| PCHECK(clearenv() == 0); |
| for (ChromiumCommandBuilder::StringMap::const_iterator it = |
| builder.environment_variables().begin(); |
| it != builder.environment_variables().end(); ++it) { |
| PCHECK(setenv(it->first.c_str(), it->second.c_str(), 1) == 0); |
| } |
| |
| const size_t kMaxArgs = 64; |
| CHECK_LE(builder.arguments().size(), kMaxArgs); |
| |
| // One extra pointer for the initial command and one for the terminating NULL. |
| char* argv[kMaxArgs + 2]; |
| for (size_t i = 0; i < arraysize(argv); ++i) |
| argv[i] = NULL; |
| |
| argv[0] = const_cast<char*>(kAppShellPath); |
| for (size_t i = 0; i < builder.arguments().size(); ++i) |
| argv[i + 1] = const_cast<char*>(builder.arguments()[i].c_str()); |
| |
| PCHECK(execv(argv[0], argv) == 0); |
| } |
| |
| } // namespace |
| |
| int main(int argc, char** argv) { |
| ChromiumCommandBuilder builder; |
| CHECK(builder.Init()); |
| |
| // Start the X server in the background before doing more-expensive setup. |
| scoped_ptr<XServerRunner> x_runner; |
| const base::FilePath xauth_path(kXauthPath); |
| const bool using_x11 = builder.UseFlagIsSet("X"); |
| if (using_x11) { |
| x_runner.reset(new XServerRunner); |
| CHECK(x_runner->StartServer( |
| XServerRunner::kDefaultUser, XServerRunner::kDefaultVt, |
| builder.is_developer_end_user(), xauth_path)); |
| } |
| |
| builder.SetUpChromium(using_x11 ? xauth_path : base::FilePath()); |
| builder.EnableCoreDumps(); |
| AddAppShellFlags(&builder); |
| |
| if (using_x11) |
| CHECK(x_runner->WaitForServer()); |
| |
| // Do not add setup code below this point. Potentially-expensive work should |
| // be done between StartServer() and WaitForServer(). |
| |
| // This call never returns. |
| ExecAppShell(builder); |
| return 1; |
| } |