Home | History | Annotate | Download | only in update_engine
      1 //
      2 // Copyright (C) 2012 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 <sys/stat.h>
     18 #include <sys/types.h>
     19 #include <unistd.h>
     20 #include <xz.h>
     21 
     22 #include <string>
     23 
     24 #include <base/at_exit.h>
     25 #include <base/command_line.h>
     26 #include <base/files/file_util.h>
     27 #include <base/logging.h>
     28 #include <base/strings/string_util.h>
     29 #include <base/strings/stringprintf.h>
     30 #include <brillo/flag_helper.h>
     31 
     32 #include "update_engine/common/terminator.h"
     33 #include "update_engine/common/utils.h"
     34 #include "update_engine/daemon.h"
     35 
     36 using std::string;
     37 
     38 namespace chromeos_update_engine {
     39 namespace {
     40 
     41 void SetupLogSymlink(const string& symlink_path, const string& log_path) {
     42   // TODO(petkov): To ensure a smooth transition between non-timestamped and
     43   // timestamped logs, move an existing log to start the first timestamped
     44   // one. This code can go away once all clients are switched to this version or
     45   // we stop caring about the old-style logs.
     46   if (utils::FileExists(symlink_path.c_str()) &&
     47       !utils::IsSymlink(symlink_path.c_str())) {
     48     base::ReplaceFile(base::FilePath(symlink_path),
     49                       base::FilePath(log_path),
     50                       nullptr);
     51   }
     52   base::DeleteFile(base::FilePath(symlink_path), true);
     53   if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
     54     PLOG(ERROR) << "Unable to create symlink " << symlink_path
     55                 << " pointing at " << log_path;
     56   }
     57 }
     58 
     59 string GetTimeAsString(time_t utime) {
     60   struct tm tm;
     61   CHECK_EQ(localtime_r(&utime, &tm), &tm);
     62   char str[16];
     63   CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
     64   return str;
     65 }
     66 
     67 string SetupLogFile(const string& kLogsRoot) {
     68   const string kLogSymlink = kLogsRoot + "/update_engine.log";
     69   const string kLogsDir = kLogsRoot + "/update_engine";
     70   const string kLogPath =
     71       base::StringPrintf("%s/update_engine.%s",
     72                          kLogsDir.c_str(),
     73                          GetTimeAsString(::time(nullptr)).c_str());
     74   mkdir(kLogsDir.c_str(), 0755);
     75   SetupLogSymlink(kLogSymlink, kLogPath);
     76   return kLogSymlink;
     77 }
     78 
     79 void SetupLogging(bool log_to_std_err) {
     80   string log_file;
     81   logging::LoggingSettings log_settings;
     82   log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
     83   log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
     84 
     85   if (log_to_std_err) {
     86     // Log to stderr initially.
     87     log_settings.log_file = nullptr;
     88     log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
     89   } else {
     90     log_file = SetupLogFile("/var/log");
     91     log_settings.log_file = log_file.c_str();
     92     log_settings.logging_dest = logging::LOG_TO_FILE;
     93   }
     94 
     95   logging::InitLogging(log_settings);
     96 }
     97 
     98 }  // namespace
     99 }  // namespace chromeos_update_engine
    100 
    101 int main(int argc, char** argv) {
    102   DEFINE_bool(logtostderr, false,
    103               "Write logs to stderr instead of to a file in log_dir.");
    104   DEFINE_bool(foreground, false,
    105               "Don't daemon()ize; run in foreground.");
    106 
    107   chromeos_update_engine::Terminator::Init();
    108   brillo::FlagHelper::Init(argc, argv, "Chromium OS Update Engine");
    109   chromeos_update_engine::SetupLogging(FLAGS_logtostderr);
    110   if (!FLAGS_foreground)
    111     PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
    112 
    113   LOG(INFO) << "Chrome OS Update Engine starting";
    114 
    115   // xz-embedded requires to initialize its CRC-32 table once on startup.
    116   xz_crc32_init();
    117 
    118   // Ensure that all written files have safe permissions.
    119   // This is a mask, so we _block_ all permissions for the group owner and other
    120   // users but allow all permissions for the user owner. We allow execution
    121   // for the owner so we can create directories.
    122   // Done _after_ log file creation.
    123   umask(S_IRWXG | S_IRWXO);
    124 
    125   chromeos_update_engine::UpdateEngineDaemon update_engine_daemon;
    126   int exit_code = update_engine_daemon.Run();
    127 
    128   LOG(INFO) << "Chrome OS Update Engine terminating with exit code "
    129             << exit_code;
    130   return exit_code;
    131 }
    132