Module: Homebrew::Services::Cli Private
- Extended by:
- FileUtils, Utils::Output::Mixin
- Defined in:
- services/cli.rb
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Class Method Summary collapse
-
.bin ⇒ String
private
Binary name.
-
.check!(targets) ⇒ Boolean
private
Check if formula has been found.
- .install_service_file(service, file) ⇒ void private
-
.kill(targets, verbose: false) ⇒ void
private
Stop a service but keep it registered.
-
.kill_orphaned_services ⇒ Array<String>
private
Kill services that don't have a service file.
- .launchctl_load(service, file:, enable:) ⇒ String private
- .remove_unused_service_files ⇒ Array<String> private
- .report_service_running_or_loaded?(service, running_status:) ⇒ Boolean private
-
.run(targets, service_file = nil, verbose: false) ⇒ void
private
Run a service as defined in the formula.
-
.running ⇒ Array<String>
private
Find all currently running services via launchctl list or systemctl list-units.
- .service_load(service, file, enable:) ⇒ void private
-
.start(targets, service_file = nil, verbose: false) ⇒ void
private
Start a service.
-
.stop(targets, verbose: false, no_wait: false, max_wait: 0, keep: false) ⇒ void
private
Stop a service and unload it.
- .sudo_service_user ⇒ String? private
- .sudo_service_user=(sudo_service_user) ⇒ void private
- .systemd_load(service, enable:) ⇒ void private
-
.take_root_ownership?(service) ⇒ Boolean
private
protections to avoid users editing root services.
Methods included from Utils::Output::Mixin
issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning
Class Method Details
.bin ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Binary name.
28 29 30 |
# File 'services/cli.rb', line 28 def self.bin "brew services" end |
.check!(targets) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Check if formula has been found.
50 51 52 53 54 |
# File 'services/cli.rb', line 50 def self.check!(targets) raise UsageError, "Formula(e) missing, please provide a formula name or use `--all`." if targets.empty? true end |
.install_service_file(service, file) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'services/cli.rb', line 523 def self.install_service_file(service, file) raise UsageError, "Formula `#{service.name}` is not installed." unless service.installed? unless service.source_service_file.exist? raise UsageError, "Formula `#{service.name}` has not implemented #plist, #service or provided a locatable service file." end temp = Tempfile.new(service.service_name) temp << if file.nil? contents = service.service_contents if sudo_service_user && System.launchctl? # set the username in the new plist file ohai "Setting username in #{service.service_name} to: #{sudo_service_user}" require "plist" plist_data = Plist.parse_xml(contents, marshal: false) plist_data["UserName"] = sudo_service_user plist_data.to_plist else contents end else file.read end temp.flush remove_service_files(service) service.dest_dir.mkpath unless service.dest_dir.directory? temp_path = temp.path raise "Could not create a temporary service file for `#{service.name}`." if temp_path.nil? cp temp_path, service.dest # Clear tempfile. temp.close chmod 0644, service.dest if System.systemctl? && service.timed? service.timer_dest.atomic_write(service.timer_contents) chmod 0644, service.timer_dest end System::Systemctl.run("daemon-reload") if System.systemctl? end |
.kill(targets, verbose: false) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Stop a service but keep it registered.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'services/cli.rb', line 360 def self.kill(targets, verbose: false) targets.each do |service| if !service.pid? puts "Service `#{service.name}` is not started." elsif service.keep_alive? puts "Service `#{service.name}` is set to automatically restart and can't be killed." else puts "Killing `#{service.name}`... (might take a while)" killed_service_names = service.loaded_service_names.presence || [service.active_service_name] if System.systemctl? killed_service_names.each { |service_name| System::Systemctl.quiet_run("stop", service_name) } elsif System.launchctl? killed_service_names.each do |service_name| SystemCommand.quiet_system System.launchctl, "stop", service_name end end service.reset_cache! if service.pid? opoo "Unable to kill `#{service.name}` (label: #{killed_service_names.join(", ")})" else ohai "Successfully killed `#{service.name}` (label: #{killed_service_names.join(", ")})" end end end end |
.kill_orphaned_services ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Kill services that don't have a service file
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'services/cli.rb', line 89 def self.kill_orphaned_services cleaned_labels = [] cleaned_services = [] running.each do |label| if (service = FormulaWrapper.from(label)) unless service.dest.file? cleaned_labels << label cleaned_services << service end else opoo "Service #{label} not managed by `#{bin}` => skipping" end end kill(cleaned_services) cleaned_labels end |
.launchctl_load(service, file:, enable:) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
462 463 464 465 466 467 468 469 470 |
# File 'services/cli.rb', line 462 def self.launchctl_load(service, file:, enable:) service_name = FormulaWrapper.service_file_label(file) || service.service_name if enable SystemCommand.safe_system System.launchctl, "enable", "#{System.domain_target}/#{service_name}" end SystemCommand.safe_system System.launchctl, "bootstrap", System.domain_target, file service_name end |
.remove_unused_service_files ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'services/cli.rb', line 107 def self.remove_unused_service_files cleaned = [] running_services = running System.path.glob("{homebrew.*,sh.brew.*}.{plist,service,timer}").each do |file| label = FormulaWrapper.service_file_label(file) if file.extname.casecmp?(".plist") service_name = label || File.basename(file).sub(/\.(?:plist|service|timer)$/i, "") next if running_services.include?(service_name) next if System.systemctl? && !file.extname.casecmp?(".plist") && System::Systemctl.quiet_run("status", "#{service_name}.timer") next if label && System.launchctl? && System.launchctl_service_running?(label) puts "Removing unused service file: #{file}" rm file cleaned << file.to_s end cleaned end |
.report_service_running_or_loaded?(service, running_status:) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
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 |
# File 'services/cli.rb', line 57 def self.report_service_running_or_loaded?(service, running_status:) running = service.pid? loaded_name = if running service.active_service_name else service.loaded_service_names.find { |name| name != service.service_name } end if loaded_name.present? && loaded_name != service.service_name if service.service_file_generated? && service.service_names.include?(loaded_name) puts "Service `#{service.name}` is already loaded as `#{loaded_name}`; " \ "a service label migration is pending. Use `#{bin} restart #{service.name}` " \ "to migrate to `#{service.service_name}`." else status = running ? running_status : "loaded" puts "Service `#{service.name}` already #{status} " \ "(label: #{loaded_name}), use `#{bin} restart #{service.name}` to restart." end return true end status = if running running_status end return false if status.nil? puts "Service `#{service.name}` already #{status}, use `#{bin} restart #{service.name}` to restart." true end |
.run(targets, service_file = nil, verbose: false) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Run a service as defined in the formula. This does not clean the service file like start does.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'services/cli.rb', line 154 def self.run(targets, service_file = nil, verbose: false) if service_file.present? file = Pathname.new service_file raise UsageError, "Provided service file does not exist." unless file.exist? end targets.each do |service| next if report_service_running_or_loaded?(service, running_status: "running") if System.root? puts "Service `#{service.name}` cannot be run (but can be started) as root." next end service_load(service, file, enable: false) end end |
.running ⇒ Array<String>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Find all currently running services via launchctl list or systemctl list-units.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'services/cli.rb', line 34 def self.running if System.launchctl? Utils.popen_read(System.launchctl, "list") else System::Systemctl.popen_read("list-units", "--type=service", "--state=running", "--no-pager", "--no-legend") end.chomp.split("\n").filter_map do |svc| svc[/(?:homebrew(?>\.mxcl)?|sh\.brew)\.[\w+-.@]+/]&.delete_suffix(".service") end end |
.service_load(service, file, enable:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'services/cli.rb', line 484 def self.service_load(service, file, enable:) if System.root? && !service.service_startup? && !sudo_service_user opoo "`#{service.name}` must be run as non-root to start at user login!" elsif !System.root? && service.service_startup? opoo "`#{service.name}` must be run as root to start at system startup!" end if (service_user = sudo_service_user) && !System.user_exists?(service_user) function = enable ? "start" : "run" odie "Cannot #{function} `#{service.name}` as `#{service_user}` is not a user!" end loaded_service_name = service.service_name if System.launchctl? service.path_dirs.each(&:mkpath) if file.nil? && !enable && service.service_file_generated? && (contents = service.service_contents) != service.source_service_file.read Tempfile.create([service.service_name, ".plist"]) do |tempfile| tempfile.write(contents) tempfile.flush loaded_service_name = launchctl_load(service, file: Pathname(tempfile.path), enable:) end else file ||= enable ? service.dest : service.source_service_file loaded_service_name = launchctl_load(service, file:, enable:) end elsif System.systemctl? # Systemctl loads based upon location so only install service # file when it is not installed. Used with the `run` command. install_service_file(service, file) unless service.dest.exist? service.path_dirs.each(&:mkpath) systemd_load(service, enable:) end function = enable ? "started" : "ran" ohai("Successfully #{function} `#{service.name}` (label: #{loaded_service_name})") end |
.start(targets, service_file = nil, verbose: false) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Start a service.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'services/cli.rb', line 180 def self.start(targets, service_file = nil, verbose: false) file = T.let(nil, T.nilable(Pathname)) if service_file.present? file = Pathname.new service_file raise UsageError, "Provided service file does not exist." unless file.exist? end targets.each do |service| next if report_service_running_or_loaded?(service, running_status: "started") odie "Formula `#{service.name}` is not installed." unless service.installed? file ||= if service.source_service_file.exist? || System.systemctl? nil elsif service.formula.opt_prefix.exist? && (keg = Keg.for service.formula.opt_prefix) && keg.plist_installed? service_file = Dir["#{keg}/*#{service.service_file.extname}"].first Pathname.new service_file if service_file.present? end install_service_file(service, file) if !file && verbose ohai "Generated service file for #{service.formula.name}:" puts " #{service.dest.read.gsub("\n", "\n ")}" puts end # Never skip loading when ownership was taken, otherwise # only skip a `--sudo-service-user` service when not root. root_ownership_taken = take_root_ownership?(service) next if !root_ownership_taken && sudo_service_user && !System.root? service_load(service, nil, enable: true) end end |
.stop(targets, verbose: false, no_wait: false, max_wait: 0, keep: false) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
Stop a service and unload it.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'services/cli.rb', line 229 def self.stop(targets, verbose: false, no_wait: false, max_wait: 0, keep: false) targets.each do |service| loaded = service.loaded? running = !loaded && service.pid? if !loaded && !running remove_service_files(service) unless keep if service.service_file_present? odie <<~EOS Service `#{service.name}` is started as `#{service.owner}`. Try: #{"sudo " unless System.root?}#{bin} stop #{service.name} EOS elsif System.launchctl? && (stopped_name = service.service_names.find do |name| SystemCommand.quiet_system(System.launchctl, "bootout", "#{System.domain_target}/#{name}") end) ohai "Successfully stopped `#{service.name}` (label: #{stopped_name})" else opoo "Service `#{service.name}` is not started." end next end systemctl_args = [] systemctl_stop_results = T.let([], T::Array[T::Boolean]) disabled_units = T.let([], T::Array[String]) loaded_service_names = service.loaded_service_names loaded_service_names = [service.active_service_name] if loaded_service_names.empty? && running if no_wait systemctl_args << "--no-block" puts "Stopping `#{service.name}`..." else puts "Stopping `#{service.name}`... (might take a while)" end if System.systemctl? loaded_service_names.each do |service_name| if keep if service.timed? systemctl_stop_results << System::Systemctl.quiet_run( *systemctl_args, "stop", "#{service_name}.timer" ) end systemctl_stop_results << System::Systemctl.quiet_run(*systemctl_args, "stop", service_name) elsif service.timed? timer_name = "#{service_name}.timer" result = System::Systemctl.quiet_run(*systemctl_args, "disable", "--now", timer_name) systemctl_stop_results << result disabled_units << timer_name if result result = System::Systemctl.quiet_run(*systemctl_args, "disable", "--now", service_name) systemctl_stop_results << result disabled_units << "#{service_name}.service" if result else result = System::Systemctl.quiet_run(*systemctl_args, "disable", "--now", service_name) systemctl_stop_results << result disabled_units << "#{service_name}.service" if result end end elsif System.launchctl? launchctl = System.launchctl raise "launchctl is unavailable" if launchctl.nil? dont_wait_statuses = [ Errno::ESRCH::Errno, System::LAUNCHCTL_DOMAIN_ACTION_NOT_SUPPORTED, ] loaded_service_names.each do |service_name| System.candidate_domain_targets.each do |domain_target| break unless System.launchctl_service_running?(service_name) if no_wait SystemCommand.quiet_system launchctl, "bootout", "#{domain_target}/#{service_name}" else time_slept = 0 sleep_time = 1 exit_status = SystemCommand.run( launchctl, args: ["bootout", "#{domain_target}/#{service_name}"], print_stderr: false, debug: false, verbose: false, ).exit_status while dont_wait_statuses.exclude?(exit_status) && (exit_status == Errno::EINPROGRESS::Errno || System.launchctl_service_running?(service_name)) && (max_wait.zero? || time_slept < max_wait) sleep(sleep_time) time_slept += sleep_time exit_status = SystemCommand.run( launchctl, args: ["bootout", "#{domain_target}/#{service_name}"], print_stderr: false, debug: false, verbose: false, ).exit_status end end SystemCommand.quiet_system launchctl, "stop", service_name if System.launchctl_service_running?(service_name) end end end service.reset_cache! service_still_loaded = if System.systemctl? if no_wait && systemctl_stop_results.present? && systemctl_stop_results.all? false else service.loaded? || service.pid? end else # System.launchctl? loaded_service_names.any? { |service_name| System.launchctl_service_running?(service_name) } end if !keep && !service_still_loaded remove_service_files(service, disabled_units:) # Run daemon-reload on systemctl to finish unloading stopped and deleted service. System::Systemctl.run(*systemctl_args, "daemon-reload") if System.systemctl? end labels = loaded_service_names.presence || service.service_names if service_still_loaded opoo "Unable to stop `#{service.name}` (label: #{labels.join(", ")})" else ohai "Successfully stopped `#{service.name}` (label: #{labels.join(", ")})" end end end |
.sudo_service_user ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
17 18 19 |
# File 'services/cli.rb', line 17 def self.sudo_service_user @sudo_service_user end |
.sudo_service_user=(sudo_service_user) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
22 23 24 |
# File 'services/cli.rb', line 22 def self.sudo_service_user=(sudo_service_user) @sudo_service_user = T.let(sudo_service_user, T.nilable(String)) end |
.systemd_load(service, enable:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
473 474 475 476 477 478 479 480 481 |
# File 'services/cli.rb', line 473 def self.systemd_load(service, enable:) System::Systemctl.run("start", service.service_name) if service.timed? System::Systemctl.run("start", service.timer_name) System::Systemctl.run("enable", service.timer_name) if enable elsif enable System::Systemctl.run("enable", service.service_name) end end |
.take_root_ownership?(service) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
protections to avoid users editing root services
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'services/cli.rb', line 389 def self.take_root_ownership?(service) return false unless System.root? return false if sudo_service_user root_paths = T.let([], T::Array[Pathname]) if System.systemctl? group = "root" elsif System.launchctl? group = "admin" chown "root", group, service.dest require "plist" plist_data = service.dest.read plist = begin Plist.parse_xml(plist_data, marshal: false) rescue nil end return false unless plist program_location = plist["ProgramArguments"]&.first key = "first ProgramArguments value" if program_location.blank? program_location = plist["Program"] key = "Program" end if program_location.present? Dir.chdir("/") do if File.exist?(program_location) program_location_path = Pathname(program_location).realpath root_paths += [ program_location_path, program_location_path.parent.realpath, ] else opoo <<~EOS #{service.name}: the #{key} does not exist: #{program_location} EOS end end end end if (formula = service.formula) root_paths += [ formula.opt_prefix, formula.linked_keg, formula.bin, formula.sbin, ] end root_paths = root_paths.sort.uniq.select(&:exist?) opoo <<~EOS Taking root:#{group} ownership of some #{service.formula} paths: #{root_paths.join("\n ")} This will require manual removal of these paths using `sudo rm` on brew upgrade/reinstall/uninstall. EOS chown "root", group, root_paths chmod "+t", root_paths true end |