错误:使用'specified_init'属性声明的'struct'中字段的位置初始化[-Werror = specified-init]

问题描述 投票:1回答:1

我正在尝试为内核4.6.x调整旧的内核模块(为2.6.x内核编写)。

代码有一个结构声明,如下所示:

struct tcpsp_sysctl_table {
    struct ctl_table_header *sysctl_header;
    struct ctl_table tcpsp_vars[NET_TCPSP_LAST];
    struct ctl_table tcpsp_dir[2];
    struct ctl_table root_dir[2];
};

结构初始化写成:

static struct tcpsp_sysctl_table tcpsp_table = {
        NULL,
        {{NET_TCPSP_TO_ES, "timeout_established",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_ESTABLISHED],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_SS, "timeout_synsent",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_SYN_SENT],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_SR, "timeout_synrecv",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_SYN_RECV],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_FW, "timeout_finwait",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_FIN_WAIT],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_TW, "timeout_timewait",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_TIME_WAIT],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_CL, "timeout_close",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_CLOSE],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_CW, "timeout_closewait",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_CLOSE_WAIT],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_LA, "timeout_lastack",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_LAST_ACK],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_LI, "timeout_listen",
        &tcpsp_timeout_tbl.timeout[TCPSP_S_LISTEN],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_TO_SA, "timeout_synack",
          &tcpsp_timeout_tbl.timeout[TCPSP_S_SYNACK],
          sizeof(int), 0644, NULL, &proc_dointvec_jiffies},
         {NET_TCPSP_DEBUG_LEVEL, "debug_level",
          &sysctl_tcpsp_debug_level, sizeof(int), 0644, NULL,
          &proc_dointvec},
         {0}},
        {{NET_TCPSP, "tcpsp", NULL, 0, 0555, tcpsp_table.tcpsp_vars},
         {0}},
        {{CTL_NET, "net", NULL, 0, 0555, tcpsp_table.tcpsp_dir},
         {0}}
    };

当我编译模块时,我收到与这些代码行相关的错误:

错误:使用'specified_init'属性声明的'struct'中字段的位置初始化[-Werror = specified-init] {{NET_TCPSP_TO_ES,“timeout_established”,

这与新的C风格编程有关还是存在语法问题。我真的不明白发生了什么。

c linux-kernel kernel-module
1个回答
3
投票

struct ctl_table现在受到随机结构布局的限制。这意味着此类结构的初始化程序必须使用指定的初始化程序 - 这些是初始化程序,它们明确命名结构中的每个字段。自编写代码的内核版本以来,struct ctl_table中的字段也发生了变化 - 最初的ctl_name成员不再存在。

您可以像这样更新它:

static struct tcpsp_sysctl_table tcpsp_table = {
        NULL,
        {{/* NET_TCPSP_TO_ES */
          .procname = "timeout_established",
          .data = &tcpsp_timeout_tbl.timeout[TCPSP_S_ESTABLISHED],
          .maxlen = sizeof(int),
          .mode = 0644,
          .child = NULL,
          .proc_handler = &proc_dointvec_jiffies},
         {/* NET_TCPSP_TO_SS */
          .procname = "timeout_synsent",
          .data = &tcpsp_timeout_tbl.timeout[TCPSP_S_SYN_SENT],
          .maxlen = sizeof(int),
          .mode = 0644,
          .child = NULL,
          .proc_handler = &proc_dointvec_jiffies},
        /* ... */
© www.soinside.com 2019 - 2024. All rights reserved.