开发者

String manipulation in Linux kernel module

I am having a hard time in manipulating strings while writing module for linux. My problem is that I have a int Array[10] with different values in it. I need to produce a string to be able send to the buffer in my_read procedure. If my array is {0,1,112,20,4,0,0,0,0,0} then my output should be:

0:(0)
1:-(1)
2:-------------------------------------------------------------------------------------------------------(112)
3:--------------------(20)
4:----(4)
5:(0)
6:(0)
7:(0)
8:(0)
9:(0)

when I try to place the above strings in char[] arrays some how weird characters end up there

here is the code

    int my_read (char *page, char **start, off_t off, int count, int *eof, void *data)
{
 int len;   
 if (off > 0){
  *eof =1;
  return 0;
 }
     /* get process tree */
 int task_dep=0;   /* depth of a task from INIT*/
 get_task_tree(&init_task,task_dep);

 char tmp[1024];

 char A[ProcPerDepth[0]],B[ProcPerDepth[1]],C[ProcPerDepth[2]],D[ProcPerDepth[3]],E[ProcPerDepth[4]],F[ProcPerDepth[5]],G[ProcPerDepth[6]],H[ProcPerDepth[7]],I[ProcPerDepth[8]],J[ProcPerDepth[9]];
 int i=0;
 for (i=0;i<1024;i++){ tmp[i]='\0';}

 memset(A, '\0', sizeof(A));memset(B, '\0', sizeof(B));memset(C, '\0', sizeof(C));
 memset(D, '\0', sizeof(D));memset(E, '\0', sizeof(E));memset(F, '\0', sizeof(F));
 memset(G, '\0', sizeof(G));memset(H, '\0', sizeof(H));memset(I, '\0', sizeof(I));mem开发者_运维百科set(J, '\0', sizeof(J));
 printk("A:%s\nB:%s\nC:%s\nD:%s\nE:%s\nF:%s\nG:%s\nH:%s\nI:%s\nJ:%s\n",A,B,C,D,E,F,G,H,I,J);
 memset(A,'-',sizeof(A));
 memset(B,'-',sizeof(B));
 memset(C,'-',sizeof(C));
 memset(D,'-',sizeof(D));
 memset(E,'-',sizeof(E));
 memset(F,'-',sizeof(F));
 memset(G,'-',sizeof(G));
 memset(H,'-',sizeof(H));
 memset(I,'-',sizeof(I));
 memset(J,'-',sizeof(J));
 printk("A:%s\nB:%s\nC:%s\nD:%s\nE:%s\nF:%s\nG:%s\nH:%s\nI:%s\nJ:%\n",A,B,C,D,E,F,G,H,I,J);


 len = sprintf(page,"0:%s(%d)\n1:%s(%d)\n2:%s(%d)\n3:%s(%d)\n4:%s(%d)\n5:%s(%d)\n6:%s(%d)\n7:%s(%d)\n8:%s(%d)\n9:%s(%d)\n",A,ProcPerDepth[0],B,ProcPerDepth[1],C,ProcPerDepth[2],D,ProcPerDepth[3],E,ProcPerDepth[4],F,ProcPerDepth[5],G,ProcPerDepth[6],H,ProcPerDepth[7],I,ProcPerDepth[8],J,ProcPerDepth[9]);  

 return len;
}

it worked out with this:

 char s[500];
 memset(s,'-',498); 

 for (i=len=0;i<10;++i){ 
      len+=sprintf(page+len,"%d:%.*s(%d)\n",i,ProcPerDepth[i],s,ProcPerDepth[i]); 
} 

I wonder if there is an easy flag to multiply string char in sprintf. thanx –


Here are a some issues:

  1. You have entirely filled the A, B, C ... arrays with characters. Then, you pass them to an I/O routine that is expecting null-terminated strings. Because your strings are not null-terminated, printk() will keep printing whatever is in stack memory after your object until it finds a null by luck.

  2. Multi-threaded kernels like Linux have strict and relatively small constraints regarding stack allocations. All instances in the kernel call chain must fit into a specific size or something will be overwritten. You may not get any detection of this error, just some kind of downstream crash as memory corruption leads to a panic or a wedge. Allocating large and variable arrays on a kernel stack is just not a good idea.

  3. If you are going to write the tmp[] array and properly nul-terminate it, there is no reason to also initialize it. But if you were going to initialize it, you could do so with compiler-generated code by just saying: char tmp[1024] = { 0 }; (A partial initialization of an aggregate requires by C99 initialization of the entire aggregate.) A similar observation applies to the other arrays.

  4. How about getting rid of most of those arrays and most of that code and just doing something along the lines of:


for(i = j = 0; i < n; ++i)
    j += sprintf(page + j, "...", ...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜