I'm using mmap to read from a file.
The mmap returns errno 22, invalid argument.
The stat.st_size in this case is 400 which I don't think it is "too large".
I don't think I'm encountering "we dont like addr, length or offset".
I'm running this program on Intel Xeon E5(which I dont think its relevant).
What am I missing here?
if( argc > 1 ) {
struct stat stat;
for( int i = 1; i < argc; i++ ) {
if( access(argv[i], R_OK) == -1 ) {
printf("\n Cannot access datatype description file %s\n", argv[i]);
continue;
}
int fd = open(argv[i], O_RDONLY);
if( fd == -1 ) {
printf("\n Cannot open datatype description from file %s\n", argv[i]);
continue;
}
if( fstat(fd, &stat) == -1 ) {
printf("\n Cannot stat the %s file\n", argv[i]);
continue;
}
void* addr = mmap(NULL, stat.st_size, PROT_READ, MAP_FILE, fd, 0);
if( MAP_FAILED == addr ) {
printf("\nCannot map the datatype description file %s\n", argv[i]);
printf("%s %d stat.st_size %d\n", strerror(errno), errno, stat.st_size );
perror("mmap");
close(fd);
continue;
}
munmap(addr, stat.st_size);
free(addr);
close(fd);
}
}
From man 2 mmap
:
MAP_FILE
Compatibility flag. Ignored.
...
EINVAL flags contained neither MAP_PRIVATE or MAP_SHARED, or contained
both of these values.
You need to pass one of MAP_PRIVATE
or MAP_SHARED
, and you should stop passing MAP_FILE
. (What did you even think it did?)