2

I have an XCode MacOS application project in Swift. I need to send a signal to another process. Calling of the kill(pid_t, Int32) function doesn't work. My process-receiver doesn't receive any signal. Also, I tried to call bash code from swift using Process:

let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = ["kill", "-s", "SIGUSR1", receiverID]
task.launch()
task.waitUntilExit()
return task.terminationStatus

But I got an error in console kill: 6340: Operation not permitted.

Could you help me? How can I send a signal to another process?

  • Are you creating the process that you're trying to send the signal to? Are you working in a sandboxed environment? If it's not a subprocess and you're in the sandboxed environment, you will not be able to do this without using a privilege elevation. – gaige Apr 12 '20 at 18:27
  • The process is created out of my application. So how can I elevate a privilege? – Ponomarenko Artur Apr 13 '20 at 21:32

1 Answers1

1

Elevation of privileges is covered in: Apple's security Access Control documentation

However, if you're spawning the original sub-process from your application using NSTask (Process in Swift, thank's Apple!), it should be a child and you should be able to kill it. If you run it using NSTask to spawn it, you should be able to take the returned Process instance and call the terminate() method in order to kill it off.

Thus, if you were to spawn your original child using code similar to your original post (resulting in a Process instance called task), you should be able to call task.terminate() to terminate the process.

gaige
  • 17,263
  • 6
  • 57
  • 68
  • Oh, I forgot to mention that the process I need to send a signal to (i.e. process-receiver) is created out of my application. To be precise I create a process out of my app and this process launches my app as another process. So my MacOS Swift app is a child process and I need to send a signal to parent-process. – Ponomarenko Artur Apr 17 '20 at 09:00
  • @PonomarenkoArtur Sorry, I misinterpreted "out of my application" as being "by my application" as opposed to "outside of my application". At any rate, in that case, you'll need to follow the privilege elevation process outlined in the first paragraph. – gaige Apr 17 '20 at 14:37