使用execvp子调用转储logcat

时间:2021-08-04 19:57:26

I wanted to dump logcat to some file, So in shell If I do

我想将logcat转储到某个文件,所以在shell中如果我这样做

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

上面的命令会将logcat转储到文件并退出,所以我想通过C编程来实现。下面是代码

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.

这里execvp还没有终止。 Parent正在等待子进程终止,但在这种情况下,子进程尚未终止并且父进程被阻止。

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??

Child创建了文件/ data / logcat_dumped并将日志转储给它,但是子节点没有终止。这可能是什么错误? logcat本身没有终止?任何逻辑错误?

output of above program is

上述程序的输出是

wat for pid 1581
start of exec      

2 个解决方案

#1


1  

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).

你告诉logcat它的进程名是-d,它的第一个参数是-f。另外,为什么需要一个1024(!)指针数组?在这种情况下,当你正确地将进程名称作为第一个条目(argv [0] ...按惯例传递时与正在执行的命令相同)时,将执行5操作。

#2


1  

Change this

改变这个

const char *args[1024] = { "-d", "-f", "/data/logcat_dumped", NULL};

to be

成为

const char * args[] = {command, "-d", "-f", "/data/logcat_dumped", NULL};

From man 3 exec:

来自man 3的执行官:

The first argument, by convention, should point to the filename associated with the file being executed.

按照惯例,第一个参数应指向与正在执行的文件关联的文件名。


Also there is no need to pass WUNTRACED to waitpid(). Just call it like this:

也没有必要将WUNTRACED传递给waitpid()。只需将其称为:

pid_t p = waitpid(pid, &status, 0);

Adding error checking is always a good idea, so extend the logic following the call to waitpid() like this:

添加错误检查总是一个好主意,所以在调用waitpid()之后扩展逻辑,如下所示:

if (p == pid) {
...
}
else if ((pid_t) -1) = p)
{
  perror("waitpid() failed");
}

...

Also^2 there is no need to cast args when passing them to execvp().

另外^ 2在将它们传递给execvp()时不需要强制转换args。

This should do:

这应该做:

execvp(command, args);

#1


1  

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).

你告诉logcat它的进程名是-d,它的第一个参数是-f。另外,为什么需要一个1024(!)指针数组?在这种情况下,当你正确地将进程名称作为第一个条目(argv [0] ...按惯例传递时与正在执行的命令相同)时,将执行5操作。

#2


1  

Change this

改变这个

const char *args[1024] = { "-d", "-f", "/data/logcat_dumped", NULL};

to be

成为

const char * args[] = {command, "-d", "-f", "/data/logcat_dumped", NULL};

From man 3 exec:

来自man 3的执行官:

The first argument, by convention, should point to the filename associated with the file being executed.

按照惯例,第一个参数应指向与正在执行的文件关联的文件名。


Also there is no need to pass WUNTRACED to waitpid(). Just call it like this:

也没有必要将WUNTRACED传递给waitpid()。只需将其称为:

pid_t p = waitpid(pid, &status, 0);

Adding error checking is always a good idea, so extend the logic following the call to waitpid() like this:

添加错误检查总是一个好主意,所以在调用waitpid()之后扩展逻辑,如下所示:

if (p == pid) {
...
}
else if ((pid_t) -1) = p)
{
  perror("waitpid() failed");
}

...

Also^2 there is no need to cast args when passing them to execvp().

另外^ 2在将它们传递给execvp()时不需要强制转换args。

This should do:

这应该做:

execvp(command, args);