Operation not permitted when executing 'killall' with Swift


Operation not permitted when executing 'killall' with Swift



I'm trying to create a menu bar app to hide desktop icons and hopefully various other things mostly to learn more about Swift, and for some reason I can't get it to work. When I run this program and click on one of the menu items, nothing happens and I get this warning in the console:


killall: warning: kill -TERM 15175: Operation not permitted



Other commands work, but any variant I try on "killall" spits out something like the above. Currently my code looks like this:


@discardableResult
func killStuff(_ args: String...) -> Int32 {
let task = Process()
let pipe = Pipe()

task.launchPath = "/usr/bin/killall"
task.arguments = args
task.standardOutput = pipe
task.standardError = pipe
task.launch()
task.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
print(output)
}

return task.terminationStatus
}



I've tried numerous variations on the accepted solutions found here, here, and what I've found on Google, but I keep getting the same "Operation not permitted." When I run the same code in an Xcode playground, it works just fine.



Thanks in advance!





Have you tried using sudo?
– kilojoules
Mar 28 at 21:45


sudo





@kilojoules yeah, when I try it with the killStuff function it has the same message, and then when I try it with the shell function in that first link I have it says "env: sudo: Operation not permitted"
– twistedbagel
Mar 28 at 21:50





You aren't allowed to do that with a sandboxed application.
– El Tomato
Mar 28 at 22:02





@ElTomato Oh okay thanks. I removed the entitlements file from my Build Settings and it worked. In the spirit of good programming practices, is it acceptable to just remove it like that?
– twistedbagel
Mar 28 at 22:15




2 Answers
2



Mac apps, like iOS apps, are sandboxed by default, which means they have very limited access to system resources. They are only allowed to read and write to a small number of sandboxed directories, for example, and read/write/execute from usr/bin is most definitely not allowed, and nor will you be allowed to launch processes.


usr/bin



As I recall, by deleting your entitlements file you are building an app that isn't sandboxed. You can run it from Xcode, but will need to change your system settings in order to be able to run it from the finder.



If you're creating it as a local app for yourself you can open your entitlements file and change "App Sandbox" to NO. I would not recommend doing that for a production app. enter image description here


NO






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV