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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdio.h>
#include <unistd.h>
#include <optional>
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "chrome/updater/constants.h"
#include "chrome/updater/linux/ipc_constants.h"
#include "chrome/updater/linux/systemd_util.h"
#include "chrome/updater/posix/setup.h"
#include "chrome/updater/util/posix_util.h"
#include "chrome/updater/util/util.h"
namespace updater {
int Setup(UpdaterScope scope) {
VLOG(1) << base::CommandLine::ForCurrentProcess()->GetCommandLineString()
<< " : " << __func__;
std::optional<base::FilePath> dest_path = GetVersionedInstallDirectory(scope);
if (!dest_path) {
return kErrorFailedToGetVersionedInstallDirectory;
}
if (base::PathExists(*dest_path)) {
if (!DeleteExcept(dest_path->Append("Crashpad"))) {
LOG(ERROR) << "Could not remove existing copy of this updater.";
return kErrorFailedToDeleteFolder;
}
}
dest_path = dest_path->Append(GetExecutableRelativePath());
base::FilePath exe_path;
if (!base::PathService::Get(base::FILE_EXE, &exe_path)) {
return kErrorPathServiceFailed;
}
if (!base::CopyFile(exe_path, dest_path.value())) {
return kErrorFailedToCopyBinary;
}
// rwx------ for user installs, rwxr-xr-x for system installs.
int permissions_mask = base::FILE_PERMISSION_USER_MASK;
if (IsSystemInstall(scope)) {
permissions_mask |= base::FILE_PERMISSION_READ_BY_GROUP |
base::FILE_PERMISSION_EXECUTE_BY_GROUP |
base::FILE_PERMISSION_READ_BY_OTHERS |
base::FILE_PERMISSION_EXECUTE_BY_OTHERS;
}
if (!base::SetPosixFilePermissions(dest_path.value(), permissions_mask)) {
return kErrorFailedToCopyBinary;
}
return kErrorOk;
}
int UninstallCandidate(UpdaterScope scope) {
VLOG(1) << base::CommandLine::ForCurrentProcess()->GetCommandLineString()
<< " : " << __func__;
int error = kErrorOk;
if (!DeleteCandidateInstallFolder(scope)) {
VLOG(1) << "Failed to delete versioned folder.";
error = kErrorFailedToDeleteFolder;
}
std::optional<base::FilePath> versioned_socket =
GetActiveDutyInternalSocketPath(scope);
if (!versioned_socket || !base::DeleteFile(versioned_socket.value())) {
VLOG(1) << "Failed to delete versioned socket file.";
error = kErrorFailedToDeleteSocket;
}
return error;
}
int PromoteCandidate(UpdaterScope scope) {
// Create a hard link in the base install directory to this updater.
std::optional<base::FilePath> launcher_path =
GetUpdateServiceLauncherPath(scope);
base::FilePath updater_executable;
if (!launcher_path ||
!base::PathService::Get(base::FILE_EXE, &updater_executable)) {
return kErrorFailedToGetVersionedInstallDirectory;
}
base::FilePath tmp_launcher_name =
launcher_path->DirName().Append("launcher_new");
if (link(updater_executable.value().c_str(),
tmp_launcher_name.value().c_str())) {
return kErrorFailedToLinkCurrent;
}
if (rename(tmp_launcher_name.value().c_str(),
launcher_path->value().c_str())) {
return kErrorFailedToRenameCurrent;
}
if (!InstallSystemdUnits(scope)) {
return kErrorFailedToInstallSystemdUnit;
}
return kErrorOk;
}
int Uninstall(UpdaterScope scope) {
VLOG(1) << base::CommandLine::ForCurrentProcess()->GetCommandLineString()
<< " : " << __func__;
int error = kErrorOk;
if (!UninstallSystemdUnits(scope)) {
error = kErrorFailedToRemoveSystemdUnit;
}
if (!DeleteFolder(GetInstallDirectory(scope)) ||
!DeleteFolder(GetInstallDirectory(scope))) {
error = kErrorFailedToDeleteFolder;
}
return error;
}
} // namespace updater
|