error: positional initialization of field in ‘struct’ declared with ‘designated_init’ attribute [-Werror=designated-init]

error: positional initialization of field in ‘struct’ declared with ‘designated_init’ attribute [-Werror=designated-init]

我正在尝试为内核 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}}
    };

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

error: positional initialization of field in ‘struct’ declared with ‘designated_init’ attribute [-Werror=designated-init]
{{NET_TCPSP_TO_ES, "timeout_established",

这是否与新的 C 风格编程有关,或者是否存在任何语法问题。我真的不明白这是怎么回事。

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},
        /* ... */