Skip to content

Commit

Permalink
Correctly kill processes spawned by mac-crafter if mac-crafter quits/…
Browse files Browse the repository at this point in the history
…is killed/etc

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
  • Loading branch information
claucambra committed Sep 19, 2024
1 parent 278ccb8 commit 74ab24e
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions admin/osx/mac-crafter/Sources/Utils/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,40 @@

import Foundation

var task: Process?

@discardableResult
func run(
_ launchPath: String,
_ args: [String],
env: [String: String]? = nil,
quiet: Bool = false
) -> Int32 {
let task = Process()
task.launchPath = launchPath
task.arguments = args
defer { task = nil }
task = Process()

signal(SIGINT) { _ in
task?.terminate() // Send terminate signal to the task
exit(0) // Exit the script after cleanup
}

task?.launchPath = launchPath
task?.arguments = args

if let env,
let combinedEnv = task.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
let combinedEnv = task?.environment?.merging(env, uniquingKeysWith: { (_, new) in new })
{
task.environment = combinedEnv
task?.environment = combinedEnv
}

if quiet {
task.standardOutput = nil
task.standardError = nil
task?.standardOutput = nil
task?.standardError = nil
}

task.launch()
task.waitUntilExit()
return task.terminationStatus
task?.launch()
task?.waitUntilExit()
return task?.terminationStatus ?? 1
}

func run(
Expand Down

0 comments on commit 74ab24e

Please sign in to comment.