Re: Calling a Command line tool from an application
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com User-agent: Mozilla Thunderbird 1.0 (X11/20041206) Tommy Nordgren wrote: Apr 11, 2005 kl. 11:26 PM skrev Justin Walker: On Apr 11, 2005, at 14:17, Rick Steele wrote: There are two ways to do it: - the low-level API "exec(3)" will give you several ways to exec a program, with or without both command-line arguments and environment variables - system(3) will take a string that represents a command + arguments, and figure out how to call the appropriate exec system call -Kevin- _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... Can anybody point me to docs which explain calling a command line tool from an application an providing it argumetns? The latter has fallen into disrepute due to the potential for security problems, so I would recommend the former, especially if you plan on distribute the software to others. There is one caveat of using exec. It will actually REPLACE your application process with the command line tool process. To work around this it is necessary to also use the fork call, which will create a duplicate application process. There are other issues to consider as well. Signal masks, file descriptors (if the close-on-exec flag is not set), etc. will all be inherited by the executed process. It may be necessary to reset the signal masks and close those files in the child process after doing the initial fork. If the output of the program is required, a pipe will need to be created to read(2) stdout from the child process. Things become even more hairy if you want the parent process to terminate the child after some maximum delay time. In any case, the man pages for fork(2), pipe(2), close(2), dup2(2), exec(3), and wait(2) should all come in handy. For twiddling the close-on-exec behavior, the fcntl(2) man page (F_GETFD and F_SETFD) will also be useful. You can check http://www.opengroup.org/onlinepubs/009695399/ for more information about each of these system calls; there are some things documented there that you may not catch in the man pages. It will likely be MUCH easier to search for a library that handles all of the gory details for you. This email sent to site_archiver@lists.apple.com
participants (1)
-
Kevin Harris