blob: a940f153b0c4dbb43198b1283a4ac7bdc16ee663 [file] [log] [blame] [edit]
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "secagentd/common.h"
#include <unistd.h>
#include <string>
#include <utility>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_format.h"
#include "secagentd/bpf/bpf_types.h"
#include "secagentd/bpf_skeleton_wrappers.h"
namespace secagentd {
extern "C" int indirect_c_callback(void* ctx, void* data, size_t size) {
if (ctx == nullptr || size < sizeof(bpf::cros_event)) {
return -1;
}
auto* f = static_cast<BpfEventCb*>(ctx);
f->Run(*static_cast<bpf::cros_event*>(data));
return 0;
}
namespace common {
namespace {
scoped_refptr<dbus::Bus> dbus{nullptr};
} // namespace
scoped_refptr<dbus::Bus> GetDBus() {
return dbus;
}
void SetDBus(scoped_refptr<dbus::Bus> bus) {
dbus = bus;
}
} // namespace common
namespace Types {
absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const BpfSkeleton& type,
const absl::FormatConversionSpec&,
absl::FormatSink* output_sink) {
static const absl::flat_hash_map<Types::BpfSkeleton, std::string>
kTypeToString{{Types::BpfSkeleton::kProcess, "Process"},
{Types::BpfSkeleton::kFile, "File"}};
auto i = kTypeToString.find(type);
output_sink->Append(i != kTypeToString.end() ? i->second : "Unknown");
return {.value = true};
}
} // namespace Types
std::ostream& operator<<(std::ostream& out, const Types::BpfSkeleton& type) {
out << absl::StreamFormat("%s", type);
return out;
}
absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const Types::Plugin& type,
const absl::FormatConversionSpec&,
absl::FormatSink* sink) {
static const absl::flat_hash_map<Types::Plugin, std::string> kTypeToString{
{Types::Plugin::kProcess, "Process"},
{Types::Plugin::kAgent, "Agent"},
{Types::Plugin::kFile, "File"}};
auto i = kTypeToString.find(type);
sink->Append(i != kTypeToString.end() ? i->second : "Unknown");
return {.value = true};
}
std::ostream& operator<<(std::ostream& out, const Types::Plugin& type) {
out << absl::StreamFormat("%s", type);
return out;
}
} // namespace secagentd