我正在制作一个模块,在其中我从用户那里获取一些条目并将它们分解成更小的字符串并将它们存储到数组中。逻辑工作正常,但几分钟后内核崩溃。下面我给出了将条目从用户空间放入/proc 文件的代码,然后在模块中进行编码。
FILE *fp;
fp =fopen("/proc/tx_info","w");
fprintf(fp,"protocol address tos payload interface");
fprintf(fp,"n %3s %6s %7d %5d %6sn",prot,addr,tos,pld,inter);
fclose(fp);
现在模块中的代码
void tx_break(void)
{
char ch;
char tmp[25];
int i =0;
int k=0,j=0,y = 0;
ch = tx_buffer[0];
//runing the while loop while the ch goes to next line from where the real user entries starts
while(ch != 'n')
{
ch = tx_buffer[i];
i++;
}
while(ch != ' ')
{
j=0;
//while loop for blanks in between the user entries
while(ch == ' ' && ch != ' ')
{
i++;
ch = tx_buffer[i];
}
//while loop to put the user defined entries to a temporary tmp which would become an entry of 2d array(info.arr[][])
while(ch != ' ' && ch !=' ')
{
tmp[j] = ch;
j++;
i++;
ch = tx_buffer[i];
}
tmp[j] = ' ';
printk(KERN_ALERT"n%sn",tmp);
//putting user entries to an 2d array
for(k=0;tmp[k] != ' ';k++)
info.arr[y][k] = tmp[k];
info.arr[y][k] = ' ';
printk("the 2d::%s",&info.arr[y][0]);
y++;
// memcpy(&info.arr[k][0],tmp,j+1);
// k++;
}
}
我真的不知道你是如何阅读tx_bufferp
所以我想你已经存储了/proc/tx_info
文件中的所有信息。
从这里,我看到一些错误:
while(ch != 'n')
{
ch = tx_buffer[i];
i++;
}
在上次执行时保持 ch = ''。
现在,使用 ch = '',tmp[0] = "n"
,将其存储在 info.arr[0]
中并打印其地址。因此,您已在info.arr
中存储了一个非项目。这个变量的维度是什么?也许您正在越界存储。
在最后一项处理中,您也将n
字符存储在tmp
中,因为while(ch != ' ' && ch !=' ')
.这是对的吗?