Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f31ecce6 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

floss: Create PID file when gd shim is init

gd idle module and gd shim module both get initialized on start up which
makes btmanagerd think that the adapter is enabled, disabled and then
enabled again (looking at PID files). Moving the pid creation to just
the gd shim module prevents this and only a single Enable event is seen.

Bug: 193070105
Test: btclient enable and verify only 1 callback seen
Tag: #floss
Change-Id: Ic6a79ab9c8d8e04f974443776c7639c7f7f0b2ec
parent dda6aec3
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -16,9 +16,7 @@

#include "stack_manager.h"

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <chrono>
#include <future>
#include <queue>
@@ -36,9 +34,6 @@ using ::bluetooth::os::WakelockManager;

namespace bluetooth {

// Assume we are hci0 for now
constexpr char bluetooth_pid_file[] = "/var/run/bluetooth/bluetooth0.pid";

void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
  management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
  handler_ = new Handler(management_thread_);
@@ -59,10 +54,6 @@ void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
      "Can't start stack, last instance: %s",
      registry_.last_instance_.c_str());

  pid_fd_ = open(bluetooth_pid_file, O_WRONLY | O_CREAT, 0644);
  pid_t my_pid = getpid();
  dprintf(pid_fd_, "%d\n", my_pid);

  LOG_INFO("init complete");
}

@@ -92,9 +83,6 @@ void StackManager::ShutDown() {
  handler_->WaitUntilStopped(std::chrono::milliseconds(2000));
  delete handler_;
  delete management_thread_;

  unlink(bluetooth_pid_file);
  close(pid_fd_);
}

void StackManager::handle_shut_down(std::promise<void> promise) {
+0 −1
Original line number Diff line number Diff line
@@ -41,7 +41,6 @@ class StackManager {
  os::Thread* management_thread_ = nullptr;
  os::Handler* handler_ = nullptr;
  ModuleRegistry registry_;
  int pid_fd_ = 0;

  void handle_start_up(ModuleList* modules, os::Thread* stack_thread, std::promise<void> promise);
  void handle_shut_down(std::promise<void> promise);
+43 −0
Original line number Diff line number Diff line
@@ -18,9 +18,15 @@

#include "device/include/controller.h"

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string>

#include "gd/att/att_module.h"
#include "gd/btaa/activity_attribution.h"
#include "gd/common/init_flags.h"
#include "gd/common/strings.h"
#include "gd/hal/hci_hal.h"
#include "gd/hci/acl_manager.h"
#include "gd/hci/controller.h"
@@ -55,6 +61,34 @@
namespace bluetooth {
namespace shim {

using ::bluetooth::common::InitFlags;
using ::bluetooth::common::StringFormat;

namespace {
// PID file format
constexpr char pid_file_format[] = "/var/run/bluetooth/bluetooth%d.pid";

void CreatePidFile() {
  std::string pid_file =
      StringFormat(pid_file_format, InitFlags::GetAdapterIndex());
  int pid_fd_ = open(pid_file.c_str(), O_WRONLY | O_CREAT, 0644);
  if (!pid_fd_) return;

  pid_t my_pid = getpid();
  dprintf(pid_fd_, "%d\n", my_pid);
  close(pid_fd_);

  LOG_INFO("%s - Created pid file %s", __func__, pid_file.c_str());
}

void RemovePidFile() {
  std::string pid_file =
      StringFormat(pid_file_format, InitFlags::GetAdapterIndex());
  unlink(pid_file.c_str());
  LOG_INFO("%s - Deleted pid file %s", __func__, pid_file.c_str());
}
}  // namespace

Stack* Stack::GetInstance() {
  static Stack instance;
  return &instance;
@@ -87,6 +121,9 @@ void Stack::StartEverything() {
          rust::get_controller(**rust_stack_));
    }
    bluetooth::shim::hci_on_reset_complete();

    // Create pid since we're up and running
    CreatePidFile();
    return;
  }

@@ -168,6 +205,9 @@ void Stack::StartEverything() {
  if (common::init_flags::btaa_hci_is_enabled()) {
    bluetooth::shim::init_activity_attribution();
  }

  // Create pid since we're up and running
  CreatePidFile();
}

void Stack::Start(ModuleList* modules) {
@@ -184,6 +224,9 @@ void Stack::Start(ModuleList* modules) {
}

void Stack::Stop() {
  // First remove pid file so clients no stack is going down
  RemovePidFile();

  if (common::init_flags::gd_rust_is_enabled()) {
    if (rust_stack_ != nullptr) {
      rust::stack_stop(**rust_stack_);