繁体   English   中英

为什么我会收到Segmentation故障? 我正在使用stat,mmap,nftw和memcmp等等

[英]Why do I get a Segmentation fault? I'm using stat, mmap, nftw, and memcmp, among other things

这是我的代码。 我假设这与指针的不当使用有关,或者我可能没有正确映射和取消映射我的内存。

有谁能请我对这个问题有所了解?

#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h> 
#include <sys/mman.h>
#include <ftw.h>
#include <sys/stat.h>
#include <string.h>

int size;
int map1, map2;
void *tar, *temp;

int callback(const char *filename,
             const struct stat *sb2,
             int filetype,
             struct FTW *ftw)
{           
    printf("test");
    if(sb2->st_size == sb1->st_size){
        temp = mmap(NULL, sb2->st_size, PROT_NONE, 0, map2, 0);
        int cmp = memcmp(tar, temp, sb2->st_size);
        printf("%d\n", cmp);
        if(cmp == 0){
            printf("%s\n", filename);
        }  
        if(munmap(temp,sb2->st_size) == -1){
            fprintf(stderr, "Error in unmapping in callback function");
            exit(EXIT_FAILURE);
        }  
    }  

    return 0;   //continue to walk the tree
}  

int main(int argc, char *argv[])
{
    //check for correct arguments
    if (argc == 1 || argc > 3) {
        fprintf(stderr, "Syntax: %s filename dirname\n", argv[0]);
        exit(EXIT_FAILURE);
    }  


    //use stat to get size of filename
    struct stat sb1;
    if(stat(argv[1],&sb1) != 0){
        fprintf(stderr, "Error in stat().");
        exit(EXIT_FAILURE);
    }
    size = sb1.st_size;

    //fd = mmap filename
    tar = mmap(NULL,sb1->st_size, PROT_WRITE, MAP_SHARED, map1, 0);
    if(tar == 0){
        fprintf(stderr, "Main() mmap failed");
        exit(EXIT_FAILURE);
    }  


    //walk through the directory with callback function
    nftw(argv[2], callback, 20, 0);

    // use munmap to clear fd
    if (munmap(tar,sb1->st_size) == -1) {
        fprintf(stderr, "Error in unmapping");
        exit(EXIT_FAILURE);
    }  
}

编辑

我现在在使用stat函数之前声明我的struct stat sb1。 完成后我再次收到分段错误。 然后我注释掉了我的nftw()调用,并打印出大小变量(它有一个合理的数字,所以我相信这是有效的)。 新错误是:

取消映射时出错。

你声明:

struct stat *sb1;

你用:

stat(argv[1],sb1);

崩溃和刻录是因为sb1是空指针(因为变量是在文件范围定义的,所以它初始化为0)。

您需要声明(在文件范围内):

struct stat sb1;

然后在main()你可以使用:

if (stat(argv[1], &sb1) != 0)
    ...oops...

您必须检查sb1所有用途,以修复从指针到对象的状态更改,在必要时添加& ,并更改->. 在必要时。

mmap()通过示例

这是我编写的函数的温和编辑版本,它使用mmap()将文件映射到内存:

/* Map named file into memory and validate that it is a MSG file */
static int msg_mapfile(const char *file)
{
    int         fd;
    void       *vp;
    struct stat sb;

    if (file == 0)
        return(MSG_NOMSGFILE);
    if ((fd = open(file, O_RDONLY, 0)) < 0)
        return(MSG_OPENFAIL);
    if (fstat(fd, &sb) != 0)
    {
        close(fd);
        return(MSG_STATFAIL);
    }
    vp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    close(fd);
    if (vp == MAP_FAILED)
        return(MSG_MMAPFAIL);

MSG_xxxx常量是适用于它来自的程序的不同错误号。 它只需要读取文件,因此PROT_READ ; 我想你也可以这样做。

if (argc == 1 || argc > 3) {
    fprintf(stderr, "Syntax: %s filename dirname\n", argv[0]);
    exit(EXIT_FAILURE);
}

/* ... */

nftw(argv[2], callback, 20, 0);

我看到argv [2]有可能为NULL。 也许你的意思是:

if (argc != 3) {
    fprintf(stderr, "Syntax: %s filename dirname\n", argv[0]);
    exit(EXIT_FAILURE);
}

你正在读哪本书?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM