Swift-Problem discovered when creating a helper












0














When i try to create a helper for an application that retrieve system software and hardware details using system_profiler command i got the following error.




Response from XPC service: HELLO XPC

Response from XPC service: /usr/sbin/system_profiler:
/usr/sbin/system_profiler: cannot execute binary file"




The code is given below.



class CommandHelper: NSObject,CommandHelperProtocol {
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) {
let response = string.uppercased()
reply(response)
}
func loadServerURL(_ string: String, withReply reply: @escaping (String) -> Void) {
let pipe = Pipe()
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["system_profiler","SPHardwareDataType"]
process.standardOutput = pipe
process.standardError = pipe
let fileHandle = pipe.fileHandleForReading
process.launch()
let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8)

print(response!)
reply(response!)
}
}


When i set launchPath to /usr/sbin/system_profiler i got blank output.










share|improve this question






















  • First question, why are you launching /bin/sh? Why not execute system_profiler directly?
    – James Bucanek
    Nov 23 at 19:11










  • Secondly, even if you want to use /bin/sh this won't work. From the bash man page: "If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands", emphasis mine.
    – James Bucanek
    Nov 23 at 19:12










  • Finally, if parsing the output of system_profiler programmatically I would suggest the -xml option.
    – James Bucanek
    Nov 23 at 19:16










  • Is the app sandboxed? If yes you have no permission to run shell scripts via Process
    – vadian
    Nov 23 at 20:52










  • Yes. The app is sandboxed.Is there is any issue for validation (for sandboxing) if i use "/usr/sbin/system_profiler" to grab the hardware information ?
    – biju
    Nov 26 at 4:13


















0














When i try to create a helper for an application that retrieve system software and hardware details using system_profiler command i got the following error.




Response from XPC service: HELLO XPC

Response from XPC service: /usr/sbin/system_profiler:
/usr/sbin/system_profiler: cannot execute binary file"




The code is given below.



class CommandHelper: NSObject,CommandHelperProtocol {
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) {
let response = string.uppercased()
reply(response)
}
func loadServerURL(_ string: String, withReply reply: @escaping (String) -> Void) {
let pipe = Pipe()
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["system_profiler","SPHardwareDataType"]
process.standardOutput = pipe
process.standardError = pipe
let fileHandle = pipe.fileHandleForReading
process.launch()
let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8)

print(response!)
reply(response!)
}
}


When i set launchPath to /usr/sbin/system_profiler i got blank output.










share|improve this question






















  • First question, why are you launching /bin/sh? Why not execute system_profiler directly?
    – James Bucanek
    Nov 23 at 19:11










  • Secondly, even if you want to use /bin/sh this won't work. From the bash man page: "If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands", emphasis mine.
    – James Bucanek
    Nov 23 at 19:12










  • Finally, if parsing the output of system_profiler programmatically I would suggest the -xml option.
    – James Bucanek
    Nov 23 at 19:16










  • Is the app sandboxed? If yes you have no permission to run shell scripts via Process
    – vadian
    Nov 23 at 20:52










  • Yes. The app is sandboxed.Is there is any issue for validation (for sandboxing) if i use "/usr/sbin/system_profiler" to grab the hardware information ?
    – biju
    Nov 26 at 4:13
















0












0








0







When i try to create a helper for an application that retrieve system software and hardware details using system_profiler command i got the following error.




Response from XPC service: HELLO XPC

Response from XPC service: /usr/sbin/system_profiler:
/usr/sbin/system_profiler: cannot execute binary file"




The code is given below.



class CommandHelper: NSObject,CommandHelperProtocol {
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) {
let response = string.uppercased()
reply(response)
}
func loadServerURL(_ string: String, withReply reply: @escaping (String) -> Void) {
let pipe = Pipe()
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["system_profiler","SPHardwareDataType"]
process.standardOutput = pipe
process.standardError = pipe
let fileHandle = pipe.fileHandleForReading
process.launch()
let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8)

print(response!)
reply(response!)
}
}


When i set launchPath to /usr/sbin/system_profiler i got blank output.










share|improve this question













When i try to create a helper for an application that retrieve system software and hardware details using system_profiler command i got the following error.




Response from XPC service: HELLO XPC

Response from XPC service: /usr/sbin/system_profiler:
/usr/sbin/system_profiler: cannot execute binary file"




The code is given below.



class CommandHelper: NSObject,CommandHelperProtocol {
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) {
let response = string.uppercased()
reply(response)
}
func loadServerURL(_ string: String, withReply reply: @escaping (String) -> Void) {
let pipe = Pipe()
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["system_profiler","SPHardwareDataType"]
process.standardOutput = pipe
process.standardError = pipe
let fileHandle = pipe.fileHandleForReading
process.launch()
let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8)

print(response!)
reply(response!)
}
}


When i set launchPath to /usr/sbin/system_profiler i got blank output.







swift cocoa nsxpcconnection system-profiler






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 10:08









biju

54




54












  • First question, why are you launching /bin/sh? Why not execute system_profiler directly?
    – James Bucanek
    Nov 23 at 19:11










  • Secondly, even if you want to use /bin/sh this won't work. From the bash man page: "If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands", emphasis mine.
    – James Bucanek
    Nov 23 at 19:12










  • Finally, if parsing the output of system_profiler programmatically I would suggest the -xml option.
    – James Bucanek
    Nov 23 at 19:16










  • Is the app sandboxed? If yes you have no permission to run shell scripts via Process
    – vadian
    Nov 23 at 20:52










  • Yes. The app is sandboxed.Is there is any issue for validation (for sandboxing) if i use "/usr/sbin/system_profiler" to grab the hardware information ?
    – biju
    Nov 26 at 4:13




















  • First question, why are you launching /bin/sh? Why not execute system_profiler directly?
    – James Bucanek
    Nov 23 at 19:11










  • Secondly, even if you want to use /bin/sh this won't work. From the bash man page: "If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands", emphasis mine.
    – James Bucanek
    Nov 23 at 19:12










  • Finally, if parsing the output of system_profiler programmatically I would suggest the -xml option.
    – James Bucanek
    Nov 23 at 19:16










  • Is the app sandboxed? If yes you have no permission to run shell scripts via Process
    – vadian
    Nov 23 at 20:52










  • Yes. The app is sandboxed.Is there is any issue for validation (for sandboxing) if i use "/usr/sbin/system_profiler" to grab the hardware information ?
    – biju
    Nov 26 at 4:13


















First question, why are you launching /bin/sh? Why not execute system_profiler directly?
– James Bucanek
Nov 23 at 19:11




First question, why are you launching /bin/sh? Why not execute system_profiler directly?
– James Bucanek
Nov 23 at 19:11












Secondly, even if you want to use /bin/sh this won't work. From the bash man page: "If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands", emphasis mine.
– James Bucanek
Nov 23 at 19:12




Secondly, even if you want to use /bin/sh this won't work. From the bash man page: "If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands", emphasis mine.
– James Bucanek
Nov 23 at 19:12












Finally, if parsing the output of system_profiler programmatically I would suggest the -xml option.
– James Bucanek
Nov 23 at 19:16




Finally, if parsing the output of system_profiler programmatically I would suggest the -xml option.
– James Bucanek
Nov 23 at 19:16












Is the app sandboxed? If yes you have no permission to run shell scripts via Process
– vadian
Nov 23 at 20:52




Is the app sandboxed? If yes you have no permission to run shell scripts via Process
– vadian
Nov 23 at 20:52












Yes. The app is sandboxed.Is there is any issue for validation (for sandboxing) if i use "/usr/sbin/system_profiler" to grab the hardware information ?
– biju
Nov 26 at 4:13






Yes. The app is sandboxed.Is there is any issue for validation (for sandboxing) if i use "/usr/sbin/system_profiler" to grab the hardware information ?
– biju
Nov 26 at 4:13














1 Answer
1






active

oldest

votes


















0














Shells execute scripts, not binaries. The solution is to run the tool directly; there's hardly any reason to launch a shell just to execute a program:



process.launchPath = "/usr/sbin/system_profiler"
process.arguments = ["SPHardwareDataType"]


Also, there's no point in setting the stderr pipe if you're not going to use it:



/* process.standardError = pipe */





share|improve this answer





















  • How use grep in arguments? like this . system_profiler -xml SPHardwareDataType | grep "serial_number"
    – biju
    Nov 26 at 4:25










  • Using system_profiler like this some of the DataTypes (eg: SPManagedClientDataType,SPConfigurationProfileDataType) gives blank output. But it gives output if run in terminal.
    – biju
    Nov 26 at 4:48










  • You don't need grep; you get back data, which you decode and turn into a string–see String(data:,encoding:)–which you can then search using String methods or (if you need it) NSRegularExpression.
    – James Bucanek
    Nov 26 at 7:29












  • There should be no difference between the output you get in the shell and the output you read from the tool: same program, same output. But you might want to check to see if there's anything being sent to the stderr pipe. (I know this contradicts my previous comment, but if you're launching system_profiler with the right arguments, I can't imagine why there should be any error output.)
    – James Bucanek
    Nov 26 at 7:31










  • func loadProvisioningProfileDetails(_ string: String, withReply reply: @escaping (String) -> Void) { let pipe = Pipe() let process = Process() process.launchPath = "/usr/sbin/system_profiler" process.arguments = ["SPConfigurationProfileDataType"] process.standardOutput = pipe let fileHandle = pipe.fileHandleForReading process.launch() let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) reply(response!) }. The code used now.
    – biju
    Nov 26 at 9:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444585%2fswift-problem-discovered-when-creating-a-helper%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Shells execute scripts, not binaries. The solution is to run the tool directly; there's hardly any reason to launch a shell just to execute a program:



process.launchPath = "/usr/sbin/system_profiler"
process.arguments = ["SPHardwareDataType"]


Also, there's no point in setting the stderr pipe if you're not going to use it:



/* process.standardError = pipe */





share|improve this answer





















  • How use grep in arguments? like this . system_profiler -xml SPHardwareDataType | grep "serial_number"
    – biju
    Nov 26 at 4:25










  • Using system_profiler like this some of the DataTypes (eg: SPManagedClientDataType,SPConfigurationProfileDataType) gives blank output. But it gives output if run in terminal.
    – biju
    Nov 26 at 4:48










  • You don't need grep; you get back data, which you decode and turn into a string–see String(data:,encoding:)–which you can then search using String methods or (if you need it) NSRegularExpression.
    – James Bucanek
    Nov 26 at 7:29












  • There should be no difference between the output you get in the shell and the output you read from the tool: same program, same output. But you might want to check to see if there's anything being sent to the stderr pipe. (I know this contradicts my previous comment, but if you're launching system_profiler with the right arguments, I can't imagine why there should be any error output.)
    – James Bucanek
    Nov 26 at 7:31










  • func loadProvisioningProfileDetails(_ string: String, withReply reply: @escaping (String) -> Void) { let pipe = Pipe() let process = Process() process.launchPath = "/usr/sbin/system_profiler" process.arguments = ["SPConfigurationProfileDataType"] process.standardOutput = pipe let fileHandle = pipe.fileHandleForReading process.launch() let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) reply(response!) }. The code used now.
    – biju
    Nov 26 at 9:21
















0














Shells execute scripts, not binaries. The solution is to run the tool directly; there's hardly any reason to launch a shell just to execute a program:



process.launchPath = "/usr/sbin/system_profiler"
process.arguments = ["SPHardwareDataType"]


Also, there's no point in setting the stderr pipe if you're not going to use it:



/* process.standardError = pipe */





share|improve this answer





















  • How use grep in arguments? like this . system_profiler -xml SPHardwareDataType | grep "serial_number"
    – biju
    Nov 26 at 4:25










  • Using system_profiler like this some of the DataTypes (eg: SPManagedClientDataType,SPConfigurationProfileDataType) gives blank output. But it gives output if run in terminal.
    – biju
    Nov 26 at 4:48










  • You don't need grep; you get back data, which you decode and turn into a string–see String(data:,encoding:)–which you can then search using String methods or (if you need it) NSRegularExpression.
    – James Bucanek
    Nov 26 at 7:29












  • There should be no difference between the output you get in the shell and the output you read from the tool: same program, same output. But you might want to check to see if there's anything being sent to the stderr pipe. (I know this contradicts my previous comment, but if you're launching system_profiler with the right arguments, I can't imagine why there should be any error output.)
    – James Bucanek
    Nov 26 at 7:31










  • func loadProvisioningProfileDetails(_ string: String, withReply reply: @escaping (String) -> Void) { let pipe = Pipe() let process = Process() process.launchPath = "/usr/sbin/system_profiler" process.arguments = ["SPConfigurationProfileDataType"] process.standardOutput = pipe let fileHandle = pipe.fileHandleForReading process.launch() let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) reply(response!) }. The code used now.
    – biju
    Nov 26 at 9:21














0












0








0






Shells execute scripts, not binaries. The solution is to run the tool directly; there's hardly any reason to launch a shell just to execute a program:



process.launchPath = "/usr/sbin/system_profiler"
process.arguments = ["SPHardwareDataType"]


Also, there's no point in setting the stderr pipe if you're not going to use it:



/* process.standardError = pipe */





share|improve this answer












Shells execute scripts, not binaries. The solution is to run the tool directly; there's hardly any reason to launch a shell just to execute a program:



process.launchPath = "/usr/sbin/system_profiler"
process.arguments = ["SPHardwareDataType"]


Also, there's no point in setting the stderr pipe if you're not going to use it:



/* process.standardError = pipe */






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 at 20:40









James Bucanek

1,5531617




1,5531617












  • How use grep in arguments? like this . system_profiler -xml SPHardwareDataType | grep "serial_number"
    – biju
    Nov 26 at 4:25










  • Using system_profiler like this some of the DataTypes (eg: SPManagedClientDataType,SPConfigurationProfileDataType) gives blank output. But it gives output if run in terminal.
    – biju
    Nov 26 at 4:48










  • You don't need grep; you get back data, which you decode and turn into a string–see String(data:,encoding:)–which you can then search using String methods or (if you need it) NSRegularExpression.
    – James Bucanek
    Nov 26 at 7:29












  • There should be no difference between the output you get in the shell and the output you read from the tool: same program, same output. But you might want to check to see if there's anything being sent to the stderr pipe. (I know this contradicts my previous comment, but if you're launching system_profiler with the right arguments, I can't imagine why there should be any error output.)
    – James Bucanek
    Nov 26 at 7:31










  • func loadProvisioningProfileDetails(_ string: String, withReply reply: @escaping (String) -> Void) { let pipe = Pipe() let process = Process() process.launchPath = "/usr/sbin/system_profiler" process.arguments = ["SPConfigurationProfileDataType"] process.standardOutput = pipe let fileHandle = pipe.fileHandleForReading process.launch() let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) reply(response!) }. The code used now.
    – biju
    Nov 26 at 9:21


















  • How use grep in arguments? like this . system_profiler -xml SPHardwareDataType | grep "serial_number"
    – biju
    Nov 26 at 4:25










  • Using system_profiler like this some of the DataTypes (eg: SPManagedClientDataType,SPConfigurationProfileDataType) gives blank output. But it gives output if run in terminal.
    – biju
    Nov 26 at 4:48










  • You don't need grep; you get back data, which you decode and turn into a string–see String(data:,encoding:)–which you can then search using String methods or (if you need it) NSRegularExpression.
    – James Bucanek
    Nov 26 at 7:29












  • There should be no difference between the output you get in the shell and the output you read from the tool: same program, same output. But you might want to check to see if there's anything being sent to the stderr pipe. (I know this contradicts my previous comment, but if you're launching system_profiler with the right arguments, I can't imagine why there should be any error output.)
    – James Bucanek
    Nov 26 at 7:31










  • func loadProvisioningProfileDetails(_ string: String, withReply reply: @escaping (String) -> Void) { let pipe = Pipe() let process = Process() process.launchPath = "/usr/sbin/system_profiler" process.arguments = ["SPConfigurationProfileDataType"] process.standardOutput = pipe let fileHandle = pipe.fileHandleForReading process.launch() let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) reply(response!) }. The code used now.
    – biju
    Nov 26 at 9:21
















How use grep in arguments? like this . system_profiler -xml SPHardwareDataType | grep "serial_number"
– biju
Nov 26 at 4:25




How use grep in arguments? like this . system_profiler -xml SPHardwareDataType | grep "serial_number"
– biju
Nov 26 at 4:25












Using system_profiler like this some of the DataTypes (eg: SPManagedClientDataType,SPConfigurationProfileDataType) gives blank output. But it gives output if run in terminal.
– biju
Nov 26 at 4:48




Using system_profiler like this some of the DataTypes (eg: SPManagedClientDataType,SPConfigurationProfileDataType) gives blank output. But it gives output if run in terminal.
– biju
Nov 26 at 4:48












You don't need grep; you get back data, which you decode and turn into a string–see String(data:,encoding:)–which you can then search using String methods or (if you need it) NSRegularExpression.
– James Bucanek
Nov 26 at 7:29






You don't need grep; you get back data, which you decode and turn into a string–see String(data:,encoding:)–which you can then search using String methods or (if you need it) NSRegularExpression.
– James Bucanek
Nov 26 at 7:29














There should be no difference between the output you get in the shell and the output you read from the tool: same program, same output. But you might want to check to see if there's anything being sent to the stderr pipe. (I know this contradicts my previous comment, but if you're launching system_profiler with the right arguments, I can't imagine why there should be any error output.)
– James Bucanek
Nov 26 at 7:31




There should be no difference between the output you get in the shell and the output you read from the tool: same program, same output. But you might want to check to see if there's anything being sent to the stderr pipe. (I know this contradicts my previous comment, but if you're launching system_profiler with the right arguments, I can't imagine why there should be any error output.)
– James Bucanek
Nov 26 at 7:31












func loadProvisioningProfileDetails(_ string: String, withReply reply: @escaping (String) -> Void) { let pipe = Pipe() let process = Process() process.launchPath = "/usr/sbin/system_profiler" process.arguments = ["SPConfigurationProfileDataType"] process.standardOutput = pipe let fileHandle = pipe.fileHandleForReading process.launch() let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) reply(response!) }. The code used now.
– biju
Nov 26 at 9:21




func loadProvisioningProfileDetails(_ string: String, withReply reply: @escaping (String) -> Void) { let pipe = Pipe() let process = Process() process.launchPath = "/usr/sbin/system_profiler" process.arguments = ["SPConfigurationProfileDataType"] process.standardOutput = pipe let fileHandle = pipe.fileHandleForReading process.launch() let response = String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8) reply(response!) }. The code used now.
– biju
Nov 26 at 9:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444585%2fswift-problem-discovered-when-creating-a-helper%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks