embedded - system() 返回错误值

标签 embedded arm embedded-linux

我有一个运行嵌入式 Linux 的基于 ARM 的设备,我观察到当我使用 C 库的 system() 调用时,返回代码不正确。这是一个演示行为的测试程序:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int ret = system("exit 42");
    printf("Should return 42 for system() call: %d\n", ret);
    printf("Returning 43 to shell..\n");
    exit(43);
};

这是设备上的程序输出:

# returnCodeTest                 
Should return 42 for system() call: 10752
Returning 43 to shell..

system() 返回值“10752”而不是“42”。 10752 左移 8 后为 42:

Python 2.7.3 (default, Feb 27 2014, 20:00:17) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 42<<8
10752

因此怀疑某处正在发生以下情况之一:

  1. 正在交换字节顺序
  2. 该值正在移动 8 位
  3. 正在使用不兼容的结构定义

当我运行 strace 时,我看到以下内容:

# strace /usr/bin/returnCodeTest 
...
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x4001e308) = 977
wait4(977, [{WIFEXITED(s) && WEXITSTATUS(s) == 42}], 0, NULL) = 977
rt_sigaction(SIGINT, {SIG_DFL, [], 0x4000000 /* SA_??? */}, NULL, 8) = 0
rt_sigaction(SIGQUIT, {SIG_DFL, [], 0x4000000 /* SA_??? */}, NULL, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=977, si_status=42, si_utime=0, si_stime=0} ---
fstat64(1, {st_mode=S_IFCHR|0622, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4001f000
write(1, "Should return 42 for system() ca"..., 42Should return 42 for system() call: 10752
) = 42
write(1, "Returning 43 to shell..\n", 24Returning 43 to shell..
) = 24
exit_group(43)                          = ?
+++ exited with 43 +++

wait4() 返回正确的状态 (si_status=42),但是当它被打印到标准输出时,值被移动了 8 位,看起来它发生在库中.有趣的是,写入返回值 42。我想知道这是否是关于正在发生的事情的提示......

不幸的是,我无法让 ltrace 在设备上编译和运行。有没有人以前见过这种类型的行为,或者对在哪里查看有任何想法(可能是特定于体系结构的)?

最佳答案

$man 3 system

Return Value

The value returned is -1 on error (e.g., fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status).

$man 2 wait

WEXITSTATUS(status) returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.

关于embedded - system() 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22567115/

相关文章:

将整数值转换为 ip 地址

linux - 生成寄存器信息和汇编代码 GCC - ARM

c - LINUX Kill_pid 失败

linux - 系统锁定或无限循环是否会导致重启?

c - 液晶屏坏了

c - 将 freeRTOS 集成到现有项目

c - Kinetis KEA : Frequency of LED toggling for the timer interrupt generated

c - for_each_process - 它是否也遍历线程和进程?

c - Raspberry Pi Pico DMA 到 I2C 设备

在 ARM Cortex 中从 float 转换为 q7_t?