Follow us on...
Follow us on Twitter Follow us on Facebook
Register
Page 1 of 2 12 LastLast
Results 1 to 8 of 11
  1. #1
    rharder's Avatar
    rharder is offline Do not read this sign.
    Join Date
    Mar 2001
    Location
    Virginia, USA
    Posts
    1,189
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Example of command line access from Cocoa

    I've not found any examples of how to call a command-line utility (ipfw in my case) from a Cocoa app, and all the applications I've seen where people do that are Shareware not open source, so they don't help me.

    I know there are already two good firewall config programs: Firewalk X and Brickhouse, but I'm starting an open source config utility. I've been putting off the parts of the code that actually call ipfw 'cause I don't know how!

    I'd sure appreciate a few url's or examples if anyone has some.

    -Rob

  2. #2
    ladavacm is offline Unperson Spotter
    Join Date
    Mar 2001
    Posts
    275
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Example of command line access from Cocoa

    Originally posted by rharder
    I've not found any examples of how to call a command-line utility (ipfw in my case) from a Cocoa app, and all the applications I've seen where people do that are Shareware not open source, so they don't help me.

    I'd sure appreciate a few url's or examples if anyone has some.

    -Rob
    You do not say whether you use ObjC or Java. If ObjC, it is quite simple: use the standard C or POSIX means of starting external programs.

    The C way would be

    system( "your command line comes here" );

    man 3 system to get more info about system C standard library API.

    The POSIX API is safer and has less overhead: check fork(2) and execve(2) family of syscalls.

    Since you need superuser privileges to load ipfw rules, a suid root command line wrapper called by your Cocoa app might be better because the Cocoa part then does not need the superuser privileges (and superuser privileges should be restricted to programs with as little complexity as possible, in order to avoid security issues)

    Happy hacking!


  3. #3
    RacerX's Avatar
    RacerX is offline Old Rhapsody User
    Join Date
    Apr 2001
    Location
    US version of Siberia
    Posts
    2,581
    Thanks
    0
    Thanked 4 Times in 4 Posts
    A good example might be OpenUp because of its use of the gzip and tar utilities built into the system. Scott has an Article outlining what he considers the strong points of his app and the source code is free for download.

    OpenUp - Highlights of a Practical Application ( http://www.stepwise.com/Software/OpenUp/OpenUp.html )

    OpenUp Home Page ( http://www.stepwise.com/Software/OpenUp/ )

    Hope that helps.

  4. #4
    endian's Avatar
    endian is offline Dis Member
    Join Date
    Sep 2000
    Location
    inside my skin
    Posts
    960
    Thanks
    0
    Thanked 0 Times in 0 Posts
    system( "your command line comes here" ); man 3 system to get more info about system C standard library API. The POSIX API is safer and has less overhead: check fork(2) and execve(2) family of syscalls.
    You shouldn't do it this way,for reasons that escape me at the moment (search the Omnigroup OSX-dev archives). The recommended way is to use NSTask or NSPipe
    g4 400 AGP - 512 RAM
    10GB internal (dumping ground) + 60GB (30 GB OSX partition/30 GB OSX Server partition) - 3GB external SCSI (OS9/and I think the Public Beta is still on there somewhere) - DVD-RAM - ZIP


  5. #5
    rharder's Avatar
    rharder is offline Do not read this sign.
    Join Date
    Mar 2001
    Location
    Virginia, USA
    Posts
    1,189
    Thanks
    0
    Thanked 3 Times in 3 Posts
    RacerX, thanks for the OpenUp idea. It's looking good.

    (back from looking) Oh, but the article doesn't discuss running a program externally. I'll look at the source...

    -Rob

  6. #6
    blb
    blb is offline `'
    Join Date
    Apr 2001
    Location
    CO, USA
    Posts
    651
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I agree with endian, the best Objective C way is to use NSTask and possibly NSPipe (if you want output from the program). For example:

    NSTask *theTask = [ [ NSTask alloc ] init ];
    NSPipe *taskPipe = [ [ NSPipe alloc ] init ];

    [ theTask setStandardError:taskPipe ];
    [ theTask setStandardOutput:taskPipe ];
    [ theTask setLaunchPath:@"/bin/ls" ];
    [ theTask setArguments:[ NSArray arrayWithObject:@"-l" ] ];
    [ theTask launch ];

    Then you can use the task pipe to read output.

  7. #7
    RacerX's Avatar
    RacerX is offline Old Rhapsody User
    Join Date
    Apr 2001
    Location
    US version of Siberia
    Posts
    2,581
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Thumbs up Wow, a lot of cool info here.

    I had been thinking of making a GUI app for the all the command line stuff I use from time to time (which was why I had planned on looking into the OpenUp source), but this looks like a lot of cool information.

    Thanks for the thread rharder.

  8. #8
    ladavacm is offline Unperson Spotter
    Join Date
    Mar 2001
    Posts
    275
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by endian


    You shouldn't do it this way,for reasons that escape me at the moment (search the Omnigroup OSX-dev archives). The recommended way is to use NSTask or NSPipe
    I would like to know the reasons (I can guess at some, e.g. internal cocoa mutex management, etc) why a fork(2) could be dangerous, but fork/execve ought to be safe. Needless to say that it gives more control over POSIX aspects of MacOS X (passing descriptors other than 0, 1, and 2, control over zombies, that kind of ilk).

    Granted, for 99+ percent of uses, NSTask is probably the better way.

 

 
Page 1 of 2 12 LastLast

Similar Threads

  1. [HOWTO] - Setup FTP access on your OSX machine.
    By swizcore in forum HOWTO & FAQs
    Replies: 43
    Last Post: February 8th, 2003, 08:33 AM
  2. Cocoa IE and MSN Messenger...but not office or WMP?
    By wdw_ in forum Apple News, Rumors & Discussion
    Replies: 8
    Last Post: October 4th, 2002, 07:55 AM
  3. Login mask on OS 9 & UNIX/Linux - How ?
    By Stephan H in forum Mac OS X System & Mac Software
    Replies: 21
    Last Post: May 10th, 2002, 11:07 AM
  4. FTP access question
    By lwk in forum Mac OS X System & Mac Software
    Replies: 1
    Last Post: January 26th, 2002, 02:40 PM
  5. GWorlds or direct bitmap access in Cocoa
    By dantheox in forum Software Programming & Web Scripting
    Replies: 0
    Last Post: December 20th, 2001, 03:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •