I wanted to dump logcat to some file, So in shell If I do
logcat -d -f /data/logcat_dumped
above command will dump the logcat to file and exits, So I wanted to same through C- programming. Below is code
const char command[] = "logcat";
pid_t pid = fork();
/* handle error case */
if (pid < 0) {
printf("*** fork: %s\n", strerror(errno));
return pid;
}
/* handle child case */
if (pid == 0) {
const char *args[1024] = { "-d", "-f", "/data/logcat_dumped", NULL};
printf("start of exec\n");
execvp(command, (char**) args);
printf("*** exec(%s): %s\n", command, strerror(errno));
fflush(stdout);
_exit(-1);
}
else
{
int status;
printf("wat for pid %d\n", pid);
pid_t p = waitpid(pid, &status, WUNTRACED);
printf("pid %d ret %d\n", pid, p);
if (p == pid) {
if (WIFSIGNALED(status)) {
printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
} else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
}
return status;
}
}
Here execvp is not terminating yet all. Parent is waiting for child to terminate but in this case child is not terminating yet all and parent is blocked.
Child created the file /data/logcat_dumped and dumped logs to it but child is not terminated. What might be the error here?? logcat itself is not terminating?? Any logical error??
output of above program is
wat for pid 1581
start of exec
Not sure if this is all that is wrong, but like this:
const char *args[1024] = { "-d", "-f", "/data/logcat_dumped", NULL};
printf("start of exec\n");
execvp(command, (char**) args);
You are telling logcat
its process name is -d
and its first argument is -f
. Also, why do you need an array of 1024(!) pointers? 5 would do in this case, when you correctly pass a process name as first entry (argv[0]
... by convention the same as the command being executed).