-
Notifications
You must be signed in to change notification settings - Fork 71
Use a separate D-Bus server #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6f48466
a90d3c1
6c5de31
18a9590
d8c9e05
3fd005f
cbbf944
36970b7
c8e358d
3eff53b
0277449
4b97d02
3fbe599
7446abf
ca49315
a199091
77def21
e9d2897
c7fdcf5
3eafc10
3070588
47d0f73
6f41e60
0a8b4ae
8dddfd3
144c60c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright (c) [2022] SUSE LLC | ||
| # | ||
| # All Rights Reserved. | ||
| # | ||
| # This program is free software; you can redistribute it and/or modify it | ||
| # under the terms of version 2 of the GNU General Public License as published | ||
| # by the Free Software Foundation. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| # more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License along | ||
| # with this program; if not, contact SUSE LLC. | ||
| # | ||
| # To contact SUSE LLC about this file by physical or electronic mail, you may | ||
| # find current contact information at www.suse.com. | ||
|
|
||
| require "dbus" | ||
| require "dinstaller/dbus/server_manager" | ||
|
|
||
| module DInstaller | ||
| module DBus | ||
| # Represents the D-Installer bus, a distinct one from the system and session buses. | ||
| class Bus < ::DBus::Connection | ||
| class << self | ||
| # Returns the current D-Bus connection | ||
| # | ||
| # @return [Bus] D-Bus connection | ||
| def current | ||
| return @current if @current | ||
|
|
||
| dbus_manager = ServerManager.new | ||
| dbus_manager.find_or_start_server | ||
| @current = new(dbus_manager.address) | ||
| end | ||
|
|
||
| def reset | ||
| @current = nil | ||
| end | ||
| end | ||
|
|
||
| # @param address [String] a connectable address | ||
| # @see https://dbus.freedesktop.org/doc/dbus-specification.html#addresses | ||
| def initialize(address) | ||
| super(address) | ||
| send_hello | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Copyright (c) [2022] SUSE LLC | ||
| # | ||
| # All Rights Reserved. | ||
| # | ||
| # This program is free software; you can redistribute it and/or modify it | ||
| # under the terms of version 2 of the GNU General Public License as published | ||
| # by the Free Software Foundation. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| # more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License along | ||
| # with this program; if not, contact SUSE LLC. | ||
| # | ||
| # To contact SUSE LLC about this file by physical or electronic mail, you may | ||
| # find current contact information at www.suse.com. | ||
|
|
||
| require "cheetah" | ||
| require "fileutils" | ||
|
|
||
| module DInstaller | ||
| module DBus | ||
| # This class takes care of setting up the D-Installer D-Bus server | ||
| # | ||
| # @example Find the current server or start a new if it is not running | ||
| # manager = DBusManager.new | ||
| # manager.find_or_start_server | ||
| class ServerManager | ||
| # @return [String] Default run directory path | ||
| DEFAULT_RUN_DIRECTORY = "/run/d-installer" | ||
| private_constant :DEFAULT_RUN_DIRECTORY | ||
|
|
||
| attr_reader :run_directory | ||
|
|
||
| def initialize(run_directory: DEFAULT_RUN_DIRECTORY) | ||
| @run_directory = run_directory | ||
| end | ||
|
|
||
| # Finds the current D-Bus server or starts a new one | ||
| # | ||
| # @return [Integer,nil] PID of the server process or nil if it | ||
| # was not possible to start a new one | ||
| def find_or_start_server | ||
| find_server || start_server | ||
| end | ||
|
|
||
| # Starts a D-Bus server | ||
| # | ||
| # @return [Integer,nil] PID of the new server. Returns nil if it failed to start | ||
| # the server. | ||
| def start_server | ||
| FileUtils.mkdir_p(run_directory) | ||
|
|
||
| output = Cheetah.run( | ||
| "/usr/bin/dbus-daemon", | ||
imobachgs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "--config-file", config_file, | ||
| "--address", address, | ||
| "--fork", "--systemd-activation", | ||
| "--print-pid", | ||
| stdout: :capture | ||
| ) | ||
| File.write(address_file, address) | ||
| pid = output.strip | ||
| File.write(pid_file, pid) | ||
| pid.to_i | ||
| rescue Cheetah::ExecutionFailed => e | ||
| puts "Could not start the DBus daemon: #{e.message}" | ||
| nil | ||
| end | ||
|
|
||
| # Gets the PID of the running server | ||
| # | ||
| # @return [Integer,nil] PID of the process if it exists, nil otherwise. | ||
| def find_server | ||
| return nil unless File.exist?(pid_file) | ||
|
|
||
| pid = File.read(pid_file).to_i | ||
| return nil if pid.zero? | ||
|
|
||
| begin | ||
| Process.getpgid(pid) | ||
| pid | ||
| rescue Errno::ESRCH # the process is not found | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| # Stops the running server | ||
| def stop_server | ||
| pid = find_server | ||
| return unless pid | ||
|
|
||
| Process.kill("KILL", pid.to_i) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NP: note that usually preferred way for killing is something like SIGTERM wait 5 seconds and then SIGKILL if it does not help. Here you do not allow process to finish anything.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Let's postpone this change a bit, anyway. |
||
| end | ||
|
|
||
| # Returns the D-Bus address | ||
| # | ||
| # @return [String] | ||
| def address | ||
| @address ||= "unix:path=#{File.join(run_directory, "bus")}" | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Returns the path to the configuration file | ||
| # | ||
| # It prefers a local configuration under `share/dbus.conf`. Otherwise, it falls back to a | ||
| # system-wide location. | ||
| def config_file | ||
| file = File.join(Dir.pwd, "share", "dbus.conf") | ||
| return file if File.exist?(file) | ||
|
|
||
| "/usr/share/dbus-1/d-installer.conf" | ||
| end | ||
|
|
||
| # Returns the path to the file containing the PID | ||
| # | ||
| # @return [String] | ||
| def pid_file | ||
| @pid_file ||= File.join(run_directory, "bus.pid") | ||
| end | ||
|
|
||
| # Returns the path to the file containing the D-Bus address | ||
| # | ||
| # @return [String] | ||
| def address_file | ||
| @address_file ||= File.join(run_directory, "bus.address") | ||
| end | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.