strdup 之后 mmap 中断
我尝试使用 mmap 进行以下配置:
- 打开文件(文件超过 2 kB)
- 从文件 *f_file*
- 映射文件请求统计信息(文件小于页面,偏移页 0,大小为预期大小)
- 验证 *f_footer* 的值地图 *f_fpage*
- 用法 strdup
代码:
union{
...
struct {
char *f_fname;
struct clog_footer *f_footer;
char *f_fpage;
size_t f_size;
} f_ring; /* circular log file */
char *f_fname; /* Name use for Files|Pipes|TTYs. */
} f_un;
...
struct clog_footer {
uint32_t cf_magic;
};
...
1995 f->f_file = open(p+1, O_RDWR, 0 );
1996 if (f->f_file == -1) {
2000 }
2001 if (fstat(f->f_file,&sb)<0) {
2006 }
2014 f->f_un.f_ring.f_fpage = mmap(NULL,sb.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,f->f_file,0);
2015 if (f->f_un.f_ring.f_fpage == MAP_FAILED) {
2020 }
2021 f->f_un.f_ring.f_footer = (struct clog_footer*)(f->f_un.f_ring.f_fpage + sb.st_size-sizeof(struct clog_footer));
2022 if (memcmp(&(f->f_un.f_ring.f_footer->cf_magic),MAGIC_CONST,4)!=0) {
2029 }
2031 f->f_un.f_fname = strdup (p+1);
...
我使用读/写,文件用零填充最多 2 Kb。我使用文件大小和零页面大小参数化了 mmap,但是 mmap 无法映射文件。
要映射的文件是否应该具有其他属性?
一旦我想从头开始映射文件,“0”作为 mmap 中的偏移量是可接受的参数吗?
I tried the following configuration with mmap:
- open file (file is over 2 kB)
- request statistics from file *f_file*
- map file (file is smaller than a page, offset page 0, size is expected size)
- verify values of *f_footer* in the map *f_fpage*
- usage strdup
Code:
union{
...
struct {
char *f_fname;
struct clog_footer *f_footer;
char *f_fpage;
size_t f_size;
} f_ring; /* circular log file */
char *f_fname; /* Name use for Files|Pipes|TTYs. */
} f_un;
...
struct clog_footer {
uint32_t cf_magic;
};
...
1995 f->f_file = open(p+1, O_RDWR, 0 );
1996 if (f->f_file == -1) {
2000 }
2001 if (fstat(f->f_file,&sb)<0) {
2006 }
2014 f->f_un.f_ring.f_fpage = mmap(NULL,sb.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,f->f_file,0);
2015 if (f->f_un.f_ring.f_fpage == MAP_FAILED) {
2020 }
2021 f->f_un.f_ring.f_footer = (struct clog_footer*)(f->f_un.f_ring.f_fpage + sb.st_size-sizeof(struct clog_footer));
2022 if (memcmp(&(f->f_un.f_ring.f_footer->cf_magic),MAGIC_CONST,4)!=0) {
2029 }
2031 f->f_un.f_fname = strdup (p+1);
...
I used read/write, file is filled with zero's up to 2 Kb. I parametrized mmap with file size and zero page size, but mmap fails to map the file.
Should the file to map have additional properties?
Is "0" a acceptable parameter as offset in mmap, once i want to map the file from beginning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论