I am writing to /proc/tx_info through user space by following programme:
int main()
{
char *prot;
char addr[14];
FILE *fp;
int i = 0;
prot = (char *)malloc(sizeof(char *));
//addr = (char *)malloc(sizeof(char *));
printf("\n enter the protocol for test\n");
scanf(" %s",prot);
printf("\n enter the addr::");
scanf(" %s",addr);
fp =fopen("/proc/tx_info","w");
if(fp == NULL)
{
printf("\n unable to write on /proc/tx_info \n");
}
fprintf(fp,"%s ",prot);
while(addr[i] != '\0')
{
fprintf(fp,"%c",addr[i]);
i++;
}
fclose(fp);
and have a proc read and write programme as follows
char tx_buffer[100];
char tx_buffer[100];
static int proc_max_size = 100;
static unsigned long buffer_size =0;
int proc_read(char *buffer,char **buffer_location,off_t offset,int buffer_length,int *eof,void *data)
{
int ret;
if(offset>0)
{
ret=0;
} else {
memcpy(buffer,tx_buffer,buffer_size);
ret = buffer_size;
}
return ret;
}
int proc_write(struct file *filp, const char *buffer, unsigned long count, void *data)
{
if(count > proc_max_size)
count = proc_max_size;
if(copy_from_user(tx_buffer,buffer,count))
return -EFAULT;
// tx_buffer[count] = '\0';
buffer_size = count;
return count;
}
my i/p to prog was tcp 192.137.190.187
and i do cat /proc/tx_info gives me following o/p:
tcp 192.137.190.18�
why last digit of ip address is not printing
You’re only allocating 14 characters to your address array. You need potentially 15 plus the null terminator, so you should allocate 16 characters.
In other words, you’re writing past the end if the array and it’s being clobbered before you print. You’re invoking undefined behavior by writing past the end of the array.