[英]Passing a structure as a char buffer to a proc entry
I want to pass a character buffer to a proc entry (say /proc/my_file) from my program. 我想从程序中将字符缓冲区传递给proc条目(例如/ proc / my_file)。 This character buffer contains my structure's elements which is of the form: 此字符缓冲区包含我的结构元素,其形式为:
struct my_table { char src_ip[4]; char dest_ip[4]; int out_flag; }my_t;
I assign the elements of my_table and copy its contents to an unsigned char buffer as follows: 我分配my_table的元素,并将其内容复制到未签名的char缓冲区,如下所示:
memcpy(buffer, &my_t, sizeof(struct my_table));
Then I write the buffer's contents into a proc entry created by me (by the name my_file) as: 然后,我将缓冲区的内容写入由我创建的proc条目(名称为my_file)中,如下所示:
write(fd, buffer, sizeof(buffer));
where fd is the file descriptor returned by open() after opening the /proc/my_file with O_WRONLY | 其中fd是用O_WRONLY打开/ proc / my_file后,open()返回的文件描述符。 O_APPEND flags. O_APPEND标志。
What I couldn't understand is that I can only see the first character string ie my_t.src_ip in this case to be written to /proc/my_file (did a 我无法理解的是,在这种情况下,我只能看到第一个字符串即my_t.src_ip写入/ proc / my_file(没有
cat /proc/my_file 猫/ proc / my_file
to check the content written) and subsequently I observed that the write() operation to /proc/my_file ends as soon as it encounters a null character in the buffer content. 来检查写入的内容),随后我发现对/ proc / my_file的write()操作在缓冲区内容中遇到空字符后立即结束。
Can I know why does this happen and how to solve this problem of writing a structure's contents into a /proc entry? 我能知道为什么会发生这种情况,以及如何解决将结构的内容写入/ proc条目的问题吗?
Edit: SSCCE for my question: The structure: 编辑:我的问题的SSCCE:结构:
struct my_iptable {
char protocol[5]; // to check whether the protocol mentioned is tcp/udp/icmp
char src_ip[16]; // source ip address
char dest_ip[16]; // destination ip address
char src_net_mask[16]; // source net mask
char dest_net_mask[16]; // destination net mask
int src_port; // source port number
int dest_port; // destination port number
char action[8]; // either block or unblock
int delete_rule; // gets the rule number to be deleted
int print_flag; // is set if we are asked to print the rules that have been set
int out_flag; // is set if the packet is outbound, else set to 0;
};
The assignment of my_ipt to null: 将my_ipt分配为null:
struct my_iptable my_ipt; struct my_iptable my_ipt; memset(&my_ipt, '\\0', sizeof(struct my_iptable)); memset(&my_ipt,'\\ 0',sizeof(struct my_iptable));
I have assigned my_ipt's fields properly. 我已经正确分配了my_ipt的字段。
The copying to buffer and writing to proc part: 复制到缓冲区并写入proc部分:
unsigned char write_buf[sizeof(struct my_iptable)];
memcpy(write_buf, &my_ipt, sizeof(struct my_iptable));
int proc_fp = open("/proc/minifw", O_WRONLY | O_APPEND);
if(proc_fp < 0) {
printf("Couldn't open /proc/minifw for writing\n");
exit(EXIT_FAILURE);}
if(write(proc_fp, write_buf, sizeof(struct my_iptable)) == -1) {
printf("There was an error writing to minifw buffer\n");
exit(EXIT_FAILURE);
}
I hope this gives you appropriate info on what I want to understand. 我希望这能为您提供有关我想了解的信息。
Thanks! 谢谢!
use sizeof(struct my_table)
instead 使用sizeof(struct my_table)
代替
write(fd, buffer, sizeof(struct my_table));
If your buffer
is defined as pointer: 如果您的buffer
被定义为指针:
struct my_table *buffer;
then the size of buffer
will be equal to the size of pointer (4 for 32-bit systems and 8 for 64-bit systems) and not the real size of the struct my_table
那么buffer
的大小将等于指针的大小(对于32位系统为4,对于64位系统为8),而不是struct my_table
的实际大小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.