iPhone SDK System Commands

Discussion in 'iOS Development' started by SpeedyApocalypse, May 23, 2010.

  1. SpeedyApocalypse

    SpeedyApocalypse Member

    Joined:
    Dec 27, 2009
    Messages:
    564
    Likes Received:
    13
    Device:
    4G iPod touch
    For my application, I am needing to run some system commands (it will be appearing in Cydia, not App Store).

    I am able to run a few commands using system("") but I cannot figure out how to log into root using the su command. How would I be able to run multiple commands (just like if I typed in multiple commands in terminal).

    I tried the following but it did not work:

    -(IBAction)power {
    system("su");
    system("alpine");
    system("halt");
    }

    and

    -(IBAction)power {
    system("su alpine");
    system("halt");
    }

    and

    -(IBAction)power {
    system("su", "alpine", "halt");
    }

    None of those did anything on simulator or my device. How would I run multiple commands that would work with eachother?
    ------------------double post merged------------------
    Anyone have any way to do this?
  2. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    1. Not everyone's root password is alpine, especially after all of these iPhone worms.
    2. system() only executes one command. It doesn't magically open up an invisible shell in the background, so it can't remember what you typed in previously.
    3. If you really need to login as root that way, use NSTask and NSPipe. They actually let you run something in the baclkground and feed input to it.
    4. A better solution would be to make your own command/shell script to do the things you need root for, then chown root and chmod 4755 it (that makes the script always run as root, even if you launch it from somewhere else).
    5. You could also make your entire app run as root (look at Cydia)
  3. SpeedyApocalypse

    SpeedyApocalypse Member

    Joined:
    Dec 27, 2009
    Messages:
    564
    Likes Received:
    13
    Device:
    4G iPod touch
    How would I run the entire app as root? Also, is Cydia still open source(ish)?
  4. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    To run the entire app as root, you need to execute the following (as root):
    Code:
    chown root <App Executable Here>
    chmod 4755 <App Executable Here>
    However, apps will not lauch like this for some reason, so you'll have to work around this bug (or whatever it is):
    1. Rename your executable file to something else (Cydia uses 'Cydia_')
    2. Create a shell script in place of the original (without the .sh) and make sure to set its permissions to 755.
    3. Saurik's looks something like this:
      Code:
      #!/bin/bash
      Bundle=$(dirname "$0")
      exec "${Bundle}/<new path of actual executable here>"
    BTW: If you want to shut down the device, you can simply call
    Code:
    reboot(RB_HALT);
    (if you're root) instead of executing the command.

    EDIT: Yeah, Cydia is still 100% open source (but you could get to the shell script anyway)…
  5. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    how would you implement this into source code?

  6. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    What do you mean? The shell script _is_ source code.
  7. maxk96

    maxk96 New Member

    Joined:
    Jul 8, 2009
    Messages:
    37
    Likes Received:
    0
    Device:
    2G iPod touch
    i mean my own source code?
  8. Ryan

    Ryan Well-Known Member

    Joined:
    Jan 19, 2008
    Messages:
    4,129
    Likes Received:
    28
    Device:
    Nexus 4
    Just create a file

    Code:
    #!/bin/bash
    C=/${0}
    C=${C%/*}
    declare -a flags
    [[ :${DYLD_INSERT_LIBRARIES}: == */MobileSubstrate.dylib: ]] && flags[${#flags[@]}]=--substrate
    exec "${C:-.}"/YOUR EXECUTABLE NAME WITH _ ON THE END"${flags[@]}" -- "$@" 2>>/tmp/cydia.log

Share This Page