退出代码大于 255 — 可能吗?

问题描述 投票:0回答:3

如果是,在哪个操作系统、shell 或其他什么上?

考虑以下 Java 程序(我使用 Java 只是作为示例;任何语言都适合这个问题,这更多地与操作系统有关):

public class ExitCode {
    public static void main(String args[]) {
        System.exit(Integer.parseInt(args[0]));
    }
}

在 Linux 和 bash 上运行它,它返回的值始终小于 255,例如(

echo $?
打印上一个执行命令的退出代码)

> java ExitCode 2; echo $?
2

> java ExitCode 128; echo $?
128

> java ExitCode 255; echo $?
255

> java ExitCode 256; echo $?
0

> java ExitCode 65536; echo $?
0

已编辑:下面的大多数答案都充分解释了 UNIX 变体上发生的情况。我仍然想知道其他操作系统。

shell operating-system exit-code
3个回答
39
投票

使用
wait()
waitpid()

在 Unix 及其衍生版本上使用 POSIX 函数(例如

wait()
waitpid()
)是不可能的。返回的退出状态信息由两个8位字段组成,一个包含退出状态,另一个包含死亡原因信息(0表示在程序控制下有序退出,其他值表示有信号杀死了它,并表示是否一个核心被倾倒)。

sigaction()
SA_SIGINFO

一起使用

如果你努力阅读

sigaction()
<signal.h>
信号操作的POSIX规范,你会发现你可以通过a来获取传递给
exit()
的32位值子进程。然而,它并不完全简单。

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

static siginfo_t sig_info = { 0 };
static volatile sig_atomic_t sig_num = 0;
static void *sig_ctxt = 0;

static void catcher(int signum, siginfo_t *info, void *vp)
{
    sig_num = signum;
    sig_info = *info;
    sig_ctxt = vp;
}

static void set_handler(int signum)
{
    struct sigaction sa;
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = catcher;
    sigemptyset(&sa.sa_mask);

    if (sigaction(signum, &sa, 0) != 0)
    {
        int errnum = errno;
        fprintf(stderr, "Failed to set signal handler (%d: %s)\n", errnum, strerror(errnum));
        exit(1);
    }
}

static void prt_interrupt(FILE *fp)
{
    if (sig_num != 0)
    {
        fprintf(fp, "Signal %d from PID %d (status 0x%.8X; UID %d)\n",
                sig_info.si_signo, (int)sig_info.si_pid, sig_info.si_status,
                (int)sig_info.si_uid);
        sig_num = 0;
    }
}

static void five_kids(void)
{
    const int base = 0xCC00FF40;
    for (int i = 0; i < 5; i++)
    {
        pid_t pid = fork();
        if (pid < 0)
            break;
        else if (pid == 0)
        {
            printf("PID %d - exiting with status %d (0x%.8X)\n",
                   (int)getpid(), base + i, base + i);
            exit(base + i);
        }
        else
        {
            int status = 0;
            pid_t corpse = wait(&status);
            if (corpse != -1)
                printf("Child: %d; Corpse: %d; Status = 0x%.4X - waited\n", pid, corpse, (status & 0xFFFF));
            struct timespec nap = { .tv_sec = 0, .tv_nsec = 1000000 }; // 1 millisecond
            nanosleep(&nap, 0);
            prt_interrupt(stdout);
            fflush(0);
        }
    }
}

int main(void)
{
    set_handler(SIGCHLD);
    five_kids();
}

运行时(从

sigexit73
编译的程序
sigexit73.c
),会产生如下输出:

$ sigexit73
PID 26599 - exiting with status -872349888 (0xCC00FF40)
Signal 20 from PID 26599 (status 0xCC00FF40; UID 501)
Child: 26600; Corpse: 26599; Status = 0x4000 - waited
PID 26600 - exiting with status -872349887 (0xCC00FF41)
Signal 20 from PID 26600 (status 0xCC00FF41; UID 501)
Child: 26601; Corpse: 26600; Status = 0x4100 - waited
PID 26601 - exiting with status -872349886 (0xCC00FF42)
Signal 20 from PID 26601 (status 0xCC00FF42; UID 501)
Child: 26602; Corpse: 26601; Status = 0x4200 - waited
PID 26602 - exiting with status -872349885 (0xCC00FF43)
Signal 20 from PID 26602 (status 0xCC00FF43; UID 501)
Child: 26603; Corpse: 26602; Status = 0x4300 - waited
PID 26603 - exiting with status -872349884 (0xCC00FF44)
Signal 20 from PID 26603 (status 0xCC00FF44; UID 501)
$

删除对

nanosleep()
的一毫秒调用后,输出很容易看起来像:

$ sigexit73
sigexit23
PID 26621 - exiting with status -872349888 (0xCC00FF40)
Signal 20 from PID 26621 (status 0xCC00FF40; UID 501)
Child: 26622; Corpse: 26621; Status = 0x4000 - waited
PID 26622 - exiting with status -872349887 (0xCC00FF41)
PID 26623 - exiting with status -872349886 (0xCC00FF42)
Signal 20 from PID 26622 (status 0xCC00FF41; UID 501)
Child: 26624; Corpse: 26623; Status = 0x4200 - waited
Signal 20 from PID 26623 (status 0xCC00FF42; UID 501)
Child: 26625; Corpse: 26622; Status = 0x4100 - waited
PID 26624 - exiting with status -872349885 (0xCC00FF43)
PID 26625 - exiting with status -872349884 (0xCC00FF44)
$

注意,这里只有三行以

Signal
开头,也只有三行以
waited
结尾;一些信号和退出状态丢失。这可能是因为设置到父进程的
SIGCHLD
信号之间存在时序问题。

但关键是,当代码使用

exit()
sigaction()
SIGCHLD
跟踪状态时,在
SA_SIGINFO
状态下可以传输 4 个字节的数据。

郑重声明,测试是在运行 macOS Mojave 10.14.6 的 MacBook Pro 上进行的,使用 GCC 9.2.0 和 XCode 11.3.1。该代码也可在我的 GitHub 上的 SOQ(Stack Overflow Questions)存储库中作为文件

sigexit73.c
位于 src/so-1843-7779 子目录中。


15
投票

在现代 Windows 上,操作系统本身和默认控制台 shell (

CMD.EXE
) 接受并显示至少整个 32 位有符号整数范围的退出代码。在
CMD.EXE
中运行上面的示例会给出您要求的退出代码:

> java ExitCode 2
> echo %errorlevel%
2

> java ExitCode 128
> echo %errorlevel%
128

> java ExitCode 255
> echo %errorlevel%
255

> java ExitCode 256
> echo %errorlevel%
256

> java ExitCode 65536
> echo %errorlevel%
65536

Windows 并不真正具有 Unix 信号的概念,也不会尝试劫持退出代码来添加额外信息,因此只要您的 shell(或任何最终读取退出代码的程序)也不这样做,您应该取回您返回的退出代码。幸运的是,使用 Microsoft C 运行时的程序(包括使用 MS Visual C++ 编译的所有程序)会按原样保留退出进程的退出代码。


1
投票

Windows 有更多的退出代码,超过 14,000 个。 (我确信您经常在自己的屏幕上看到其中一些)。

来了:

© www.soinside.com 2019 - 2024. All rights reserved.