c - 将用户的过程条目分解为较小的字符串

标签 c linux linux-kernel

我正在制作一个模块,在该模块中我从用户那里获取一些条目并将它们分解成更小的字符串并将它们存储到一个数组中。逻辑工作正常,但几分钟后内核出现 panic 。下面我给出了将条目从用户空间放入/proc 文件的代码,然后在模块中进行编码。

FILE *fp;
fp =fopen("/proc/tx_info","w");

fprintf(fp,"protocol   address      tos   payload interface");
fprintf(fp,"\n %3s %6s %7d %5d %6s\n",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 != '\0')
{
   j=0;
//while loop for blanks in between the user entries
   while(ch == ' ' && ch != '\0')
    {
      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 !='\0')
    {
       tmp[j] = ch;
       j++;
       i++;
       ch = tx_buffer[i];
    }
    tmp[j] = '\0';
    printk(KERN_ALERT"\n%s\n",tmp);
//putting user entries to an 2d array
    for(k=0;tmp[k] != '\0';k++)
         info.arr[y][k] = tmp[k];
         info.arr[y][k] = '\0';
    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 = '\n'。

现在,使用 ch = '\n', tmp[0] = "\n",将其存储在 info.arr[0] 中并打印其地址。因此,您已在 info.arr 中存储了一项非项目。这个变量的维度是多少?也许您的存储超出范围。

在最后一项处理中,您也在 tmp 中存储 \n 字符,因为 while(ch != ' ' && ch !='\0' )。这是正确的吗?

关于c - 将用户的过程条目分解为较小的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10070679/

相关文章:

clSetKernelArg 将 arg_value 从 16 更改为 140733193388048?

linux - 更改linux内核安装目录

c - 如何设置寄存器值以启用中断?

c - 如何确定 pid_t 的最大值?

C - 对附有姓名的数字进行排序

c - 使用 C99 代码作为 Windows Phone 运行时组件(使用/ZW 编译)

linux - 什么是锁步采样?

python - 使python进程写入立即安排回写而不被标记为脏

c - 是否有任何命令可以找到某个类型(不是函数)需要包含哪个头文件?

linux - 替换linux内核3中的系统调用