【问题标题】:open() : Device or resource busy (/dev/rtc(x))open() : 设备或资源繁忙 (/dev/rtc(x))
【发布时间】:2025-09-16 18:15:02
【问题描述】:

所以我在我的 beaglebone 上连接了一个 RTC,它可以通过 cat'ing /sys/class/rtc/rtx(x)/time file 正常工作,但是我用于监控时间的 C 代码有一个我似乎无法解决的错误。

if ((rtc_fd = open(RTC, O_RDONLY, 444)) < 0)
    REPORT_ERROR("open(RTC)");

其中 RTC 是/dev/rtc1 的路径。 REPORT_ERROR 是一个用于报告自定义错误的宏函数。

无论如何,我在 for 循环之前运行此代码,循环 10 次,它正在输出到日志文件。我总是收到strerror(perror) 消息: 设备或资源繁忙 但它仍然继续给我 10 个正确的输出。

我最后也使用close()

什么给了?

编辑:也许我应该补充一点,这是在一个守护进程中运行,我在循环期间使用iocotl()RTC_RD_TIME

#define REPORT_ERROR(X) do {\
        fprintf(log,"err@ "X": %s@ %s:%d\n",\
        strerror(errno), __FILE__, __LINE__ - 1);\
        exit(EXIT_FAILURE);\
        } while(0)

#define RTC "/dev/rtc1"

int main(void)
{
    int rtc_fd;
    FILE *log;
    struct rtc_time tm;

    if ((log = fopen(LOG_FILE, "a+")) == NULL)
        exit(EXIT_FAILURE);

    if ((dup2(fileno(log), STDERR_FILENO)) < 0)
        REPORT_ERROR("dup2()");

    if ((rtc_fd = open(RTC, O_RDONLY, 444)) < 0)
        REPORT_ERROR("open(RTC)");

    /* Main loop */
    for (int i = 0; i < 10; ++i)
    {
        if ((ioctl(rtc_fd, RTC_RD_TIME, &rtctime)) != 0)
            REPORT_ERROR("ioctl(rtc_fd)");

        fprintf(log, "%02d:%02d:%02.lf   %d-%d-%d\n", tm.hour, tm.minute,
                tm.second, tm.mon + 1, tm.mday, tm.year + 1900);
        sleep(1);
    }

    close(rtc_fd);
    return 0;
}

输出:

err@ open(RTC): Device or resource busy@ ha-daemon.c:80
05:06:09   2-12-2015
05:06:10   2-12-2015
05:06:11   2-12-2015
05:06:12   2-12-2015
05:06:13   2-12-2015
05:06:14   2-12-2015
05:06:15   2-12-2015
05:06:16   2-12-2015
05:06:17   2-12-2015
05:06:18   2-12-2015

【问题讨论】:

  • 请创建一个Minimal, Complete, and Verifiable Example 并展示给我们。
  • 您是否尝试过全局打开您的设备?
  • 至于您的实际问题(打开设备),您是否确定没有其他进程已经打开完全相同的设备?
  • @JoachimPileborg,文件 *log 不是未定义的,我只是没有剪掉那个代码,现在是。我想有人可能会在没有看到这么多代码的情况下知道这个问题的原因,毕竟我在我的问题中确实说过在初始错误消息之后输出是正确的..
  • @Shehryar 我不确定您在全球范围内打开设备是什么意思?如果我 su 比 cat /sys/class/rtc/rtc1/time 我可以得到时间。

标签: c linux embedded-linux beagleboneblack beagleboard


【解决方案1】:

这不是一个真正的答案,但我对问题的编辑被拒绝了,我应该将其作为答案发布。就这样吧。

发布的代码无法编译。我对其进行了最低限度的修改,使其编译时没有警告,我可以对其进行测试。这是更改列表。其中一些可能特定于我的环境(gcc 4.8.1 / Ubuntu Linux 14.04 / x86-64):

  • 添加了所有缺少的#includes
  • 定义了LOG_FILE,将其定义为/dev/tty只是让测试更容易
  • /dev/rtc1 替换为/dev/rtc0(我没有rtc1)
  • &amp;rtctime(ioctl 的最后一个参数)替换为&amp;tm,否则既无意义也无法编译
  • 修复了 printf 格式:“%02d”而不是“%02.lf”
  • 为 rtctime 结构字段输入正确的名称:tm_hourtm_min 等。

这是修改后的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/rtc.h>

#define LOG_FILE "/dev/tty"

#define REPORT_ERROR(X) do {\
        fprintf(log,"err@ "X": %s@ %s:%d\n",\
        strerror(errno), __FILE__, __LINE__ - 1);\
        exit(EXIT_FAILURE);\
        } while(0)

#define RTC "/dev/rtc0"

int main(void)
{
    int rtc_fd;
    FILE *log;
    struct rtc_time tm;

    if ((log = fopen(LOG_FILE, "a+")) == NULL)
        exit(EXIT_FAILURE);

    if ((dup2(fileno(log), STDERR_FILENO)) < 0)
        REPORT_ERROR("dup2()");

    if ((rtc_fd = open(RTC, O_RDONLY, 444)) < 0)
        REPORT_ERROR("open(RTC)");

    /* Main loop */
    for (int i = 0; i < 10; ++i)
    {
        if ((ioctl(rtc_fd, RTC_RD_TIME, &tm)) != 0)
            REPORT_ERROR("ioctl(rtc_fd)");

        fprintf(log, "%02d:%02d:%02d   %d-%d-%d\n", tm.tm_hour, tm.tm_min,
                tm.tm_sec, tm.tm_mon + 1, tm.tm_mday, tm.tm_year + 1900);
        sleep(1);
    }

    close(rtc_fd);
    return 0;
}

测试结果:它按预期编译和工作。它要么在没有错误消息的情况下运行,要么打印错误消息并立即退出。如果程序的一个实例正在运行,则启动第二个实例会按预期给出“设备或资源忙”错误消息。

换句话说,“为我工作”。

【讨论】: