C 错误:‘;中字段的位置初始化;结构&x2019;声明为‘;指定的_init’;属性[-Werror=指定的初始值]

C 错误:‘;中字段的位置初始化;结构&x2019;声明为‘;指定的_init’;属性[-Werror=指定的初始值],c,linux-kernel,kernel-module,C,Linux Kernel,Kernel Module,我正在尝试为内核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]; }; 结构初始

我正在尝试为内核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}}
    };
当我编译模块时,我得到一个与以下代码行相关的错误:

错误:“struct”中字段的位置初始化已用声明 “指定初始化”属性[-Werror=指定初始化]
{NET_TCPSP_TO_ES,“超时已建立”


这是否与新的C风格编程有关,或者是否存在语法问题。我无法真正理解发生了什么。

结构ctl_表现在服从随机结构布局。这意味着此类结构的初始值设定器必须使用指定的初始值设定器-这些是显式命名每个字段的初始值设定器n结构。自为其编写代码的内核版本以来,
struct ctl_表
中的字段也发生了更改-初始的
ctl_名称
成员不再存在

您可以这样更新它:

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

出于好奇,为什么代码使用八进制数?@Lundin:该字段是UNIX文件模式,传统上使用八进制(因为它们在逻辑上由3位字段组成)。您可以立即看到,其中的权限是对所有者(6)的读/写权限,以及对所有者组和世界(4)的只读权限。