From bd256883be8eeb43adb6f537e64b761936438182 Mon Sep 17 00:00:00 2001 From: GottemHams Date: Thu, 30 Jun 2022 16:08:07 +0200 Subject: [PATCH 1/2] Added support for running services fully backgrounded on macOS --- lib/service/services_cli.rb | 28 ++++++++++++++++++++++++++-- lib/service/system.rb | 2 +- spec/homebrew/system_spec.rb | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/service/services_cli.rb b/lib/service/services_cli.rb index a8685e08f3..85086c7a72 100644 --- a/lib/service/services_cli.rb +++ b/lib/service/services_cli.rb @@ -272,12 +272,36 @@ def install_service_file(service, file) odie "Formula `#{service.name}` has not implemented #plist, #service or installed a locatable service file" end - temp = Tempfile.new(service.service_name) - temp << if file.blank? + data = if file.blank? service.service_file.read else file.read end + + if System.launchctl? + plist = begin + Plist.parse_xml(data) + rescue + nil + end + odie "Unable to parse plist for service `#{service.name}`" unless plist + + # If the key is merely present then we can already skip adding it ourselves: + # * If somehow the plist already comes with this key by default then we won't touch it + # * If it exists but it's empty that *might* be on purpose, so again won't touch it + # * Otherwise we'll just add all known session types so the service can start regardless of what it is + # + # Adding all session types also means that if you initialise it through e.g. a Background session and you later "physically" + # sign in to the owning account (Aqua session), things shouldn't flip out + limit_sessiontype = plist["LimitLoadToSessionType"]&.first + unless limit_sessiontype.present? + plist["LimitLoadToSessionType"] = ["Aqua", "Background", "LoginWindow", "StandardIO", "System"] + data = plist.to_plist + end + end + + temp = Tempfile.new(service.service_name) + temp << data temp.flush rm service.dest if service.dest.exist? diff --git a/lib/service/system.rb b/lib/service/system.rb index ed11034fd6..18e3ee6676 100644 --- a/lib/service/system.rb +++ b/lib/service/system.rb @@ -76,7 +76,7 @@ def domain_target if root? "system" else - "gui/#{Process.uid}" + "user/#{Process.uid}" end end end diff --git a/spec/homebrew/system_spec.rb b/spec/homebrew/system_spec.rb index 6332b7ba58..8f7c8e070b 100644 --- a/spec/homebrew/system_spec.rb +++ b/spec/homebrew/system_spec.rb @@ -75,7 +75,7 @@ describe "#domain_target" do it "returns the current domain target" do allow(described_class).to receive(:root?).and_return(false) - expect(described_class.domain_target).to match(%r{gui/(\d+)}) + expect(described_class.domain_target).to match(%r{user/(\d+)}) end it "returns the root domain target" do From bb973294546c50f2f58aecc553efb9099416e15c Mon Sep 17 00:00:00 2001 From: GottemHams Date: Sun, 3 Jul 2022 15:03:46 +0200 Subject: [PATCH 2/2] Revert plist installation function to original state --- lib/service/services_cli.rb | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/lib/service/services_cli.rb b/lib/service/services_cli.rb index 85086c7a72..a8685e08f3 100644 --- a/lib/service/services_cli.rb +++ b/lib/service/services_cli.rb @@ -272,36 +272,12 @@ def install_service_file(service, file) odie "Formula `#{service.name}` has not implemented #plist, #service or installed a locatable service file" end - data = if file.blank? + temp = Tempfile.new(service.service_name) + temp << if file.blank? service.service_file.read else file.read end - - if System.launchctl? - plist = begin - Plist.parse_xml(data) - rescue - nil - end - odie "Unable to parse plist for service `#{service.name}`" unless plist - - # If the key is merely present then we can already skip adding it ourselves: - # * If somehow the plist already comes with this key by default then we won't touch it - # * If it exists but it's empty that *might* be on purpose, so again won't touch it - # * Otherwise we'll just add all known session types so the service can start regardless of what it is - # - # Adding all session types also means that if you initialise it through e.g. a Background session and you later "physically" - # sign in to the owning account (Aqua session), things shouldn't flip out - limit_sessiontype = plist["LimitLoadToSessionType"]&.first - unless limit_sessiontype.present? - plist["LimitLoadToSessionType"] = ["Aqua", "Background", "LoginWindow", "StandardIO", "System"] - data = plist.to_plist - end - end - - temp = Tempfile.new(service.service_name) - temp << data temp.flush rm service.dest if service.dest.exist?