gpt4 book ai didi

c - 在 arm 上的 docker 容器中运行 C musl 应用程序时的 time() 问题

转载 作者:行者123 更新时间:2023-12-03 23:01:30 35 4
gpt4 key购买 nike

我的应用程序无法处理像 time(2) 这样的时间操作当它在 arm 设备上的 alpine docker 容器中运行时。
我拥有的:
我正在构建一个 native c 应用程序,该应用程序使用来自 musl.cc (arm-linux-musleabihf-gcc) 的工具链静态链接到 musl。我正在使用最新的 Alpine 容器(没有图像标签)。
它的行为方式:

  • 直接在 arm 设备上运行二进制文件按预期工作
  • 在 x64 设备上的 alpine 容器中运行按预期工作
  • 在 arm 设备上的 alpine 容器中运行 不是 工作

  • 出了什么问题:
  • time(NULL);返回 ((time_t) -1) 并且错误=1:“不允许操作”
  • 日志输出中的时间戳已启动时间戳
  • SSH 握手失败,因为远程证书的有效性在 future 。

  • 但是,如果我执行 date在容器的 Ember 中,输出是有效的。因此,似乎存在仅在 ARM 架构上的 alpine 容器中出现的问题。有趣的是,我正像以前一样从 Ubuntu 切换到 Alpine similar problems那里。
    有谁知道我做错了什么?
    更新 #1:
    ubuntu 上同样的问题。所以问题似乎出在任何基于 docker 的图像上,但仅在 arm 设备上。
    更新 #2:这是一个最小的例子
    TimeTest.c
    #include <stdio.h>
    #include <string.h>
    #include "time.h"
    #include "errno.h"

    int main()
    {
    printf("hallo\n");

    time_t myTime;
    time_t result = time(&myTime);

    printf("result: %lld\n", result);

    if ((long)result < 0)
    {
    printf("time() error=%d: %s\n", errno, strerror(errno));
    }
    else
    {
    struct tm* tm_info = localtime(&myTime);
    printf("Current local time and date: %s\n", asctime(tm_info));
    }

    return 0;
    }
    CMakeLists.txt
    cmake_minimum_required (VERSION 3.8)
    project ("TimeTest")
    if(BUILD_TARGET STREQUAL "ARM_MUSL")
    set(CMAKE_SYSTEM_PROCESSOR arm)
    set(CMAKE_C_COMPILER /usr/bin/arm-linux-musleabi-gcc)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-stack-protector -mfloat-abi=softfp -static --static")
    set(CMAKE_LINK_SEARCH_END_STATIC TRUE)
    endif()
    add_executable (TimeTest "TimeTest.c")
    ARM 设备上的输出
    pi@raspberrypi:/tmp $ docker run --rm -it -v /tmp/TimeTest:/TimeTest alpine ash
    / # /TimeTest
    hallo
    result: -4696377169665647048
    time() error=1: Operation not permitted

    最佳答案

    我能够完全按照问题中的描述重现这一点。在我的特定 ARM 硬件上:

    $ docker --version
    Docker version 19.03.6, build 369ce74a3c [released approx. 2020-02-12]

    $ file demo
    demo: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV),
    statically linked, with debug_info, not stripped
    使用 strace我们可以在报告中启动的容器内观察到这种行为:
    ...
    clock_gettime64(CLOCK_REALTIME, 0xbe9c5e10) = -1 EPERM (Operation not permitted)
    ...
    但是,如果我们添加 --privileged标记到 Docker 命令,它可以工作:
    # ./demo
    hallo
    result: 1608983884
    Current local time and date: Sat Dec 26 11:58:04 2020
    此行为是由 引起的Docker 问题 : https://gitlab.alpinelinux.org/alpine/aports/-/issues/11774 , 固定在 this commit并滚动到 Docker 版本 19.03.12 (?) 及以上。

    关于c - 在 arm 上的 docker 容器中运行 C musl 应用程序时的 time() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65424630/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com