How can I use execv in NDK

1,235 views
Skip to first unread message

Billy Cui

unread,
Sep 4, 2009, 2:32:10 AM9/4/09
to android-ndk
I use it like this:

char *exec_args[] = {"echo + >>/sdcard/t", (char *)0};
int ret;
ret = execv("/system/bin/sh", exec_args);

But all the time it fails and make app died.......

Dianne Hackborn

unread,
Sep 4, 2009, 1:07:57 PM9/4/09
to andro...@googlegroups.com
I would really strongly recommend against using exec or fork: (1) none of the commands on the system are part of the public APIs and so can have different behavior (or not even exist) on different platform versions or different devices. (2) the android system wants to be the one managing processes, so it can decide how important they are; if you create a process is doesn't know about, it can't tell the kernel how important it is, and your process can get killed unexpectedly and other issues happen.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

fadden

unread,
Sep 4, 2009, 1:09:48 PM9/4/09
to android-ndk
(1) The first argument in exec_args[] is passed in as argv[0]. You
want it to be the same as the command you're executing (usually).

(2) You need to specify a "-c" argument to sh so it knows that the
next argument is a shell command. Try:

char* exec_args[] = {"sh", "-c", "echo + >> /sdcard/t", NULL};

Doug Schaefer

unread,
Sep 4, 2009, 2:15:51 PM9/4/09
to andro...@googlegroups.com
Never mind. Missed the echo.

On Fri, Sep 4, 2009 at 2:15 PM, Doug Schaefer <cdt...@gmail.com> wrote:
That and the SD card is apparently mounted noexec, which means you can't run executables from there, so I've heard. Something to check.

Doug Schaefer

unread,
Sep 4, 2009, 2:15:03 PM9/4/09
to andro...@googlegroups.com
That and the SD card is apparently mounted noexec, which means you can't run executables from there, so I've heard. Something to check.

On Fri, Sep 4, 2009 at 1:09 PM, fadden <fad...@android.com> wrote:

David Turner

unread,
Sep 4, 2009, 3:16:59 PM9/4/09
to andro...@googlegroups.com
And what is the value of "ret" or "errno" after the call? That might give you some indications.

On Thu, Sep 3, 2009 at 11:32 PM, Billy Cui <bill...@gmail.com> wrote:

Billy Cui

unread,
Sep 7, 2009, 11:22:09 PM9/7/09
to android-ndk
Thanks guy, that's helpful for me, I am using another method now.

to david: ret is 0, and I did not test errno.
Reply all
Reply to author
Forward
0 new messages