4

I want to login as a root using system call in Xcode. I try this code without success:

System("su");
System("alpine");

or

System("su root");
System("alpine");

or

System("su root alpine");

When I google it I came to know that this is done using NSTask or NSPipe. Can anybody tell me how it possible to run multiple system commands using NSTask and NSPipe?

Please give me some hints about this or another method to do this. I am using this application on jailbroken iPhone.

Some more details are here.

3
  • 1
    Mind that if you put your application in /Applications you'll already be root when your app launches.
    – v1Axvw
    Jan 3, 2012 at 23:28
  • Also mind that the iOS is a Unix operating system. And I think you cannot give an already existing process the root privileges. You can only launch new processes with higher permissions. This means you'll need auxiliary executables. (Unless you relaunch the app as root)
    – v1Axvw
    Jan 3, 2012 at 23:31
  • Also keep in mind that with system() it's as if you typed the exact thing you pass to it to your command-line shell. (You don't get to call it twice to pass input in.) So system("alpine") isn't the correct approach. And with system() (unlike using fork() and exec()) you don't have direct control of the I/O of the child process. What exactly are you trying to do once you "log on as root"? Because even if you manage to get a shell, system() will just hang your process and wait for the shell to exit. Suggested reading: man 3 system, man 2 fork, and man 3 exec.
    – mpontillo
    Jan 3, 2012 at 23:33

1 Answer 1

1

If you want to launch something as root you may use sudo.

Example of it's usage in one line with the password:

echo <password> | sudo -S <command>
6
  • This would of course require the sudo executable to be installed on the device.
    – v1Axvw
    Jan 3, 2012 at 23:33
  • 1
    If your iphone is jailbroken you may install sudo though Cydia.
    – dimme
    Jan 3, 2012 at 23:34
  • In case you application is going to be released though some kind of repo through Cydia, you can set sudo as a dependency.
    – dimme
    Jan 3, 2012 at 23:35
  • True, but if he releases the app through Cydia, he could easily install his app to /Applications and gain super user rights automatically, but this of course actually makes the whole original question useless.
    – v1Axvw
    Jan 3, 2012 at 23:44
  • WHat keeps you from adding him?
    – dimme
    Apr 23, 2012 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.