When I run the below program I get that the mmap() fail with the EPERM error. My expected O/P is EOPNOTSUPP error, as any flag outside the LEGACY_MAP_MASK flag would fail with EOPNOTSUPP.
#define TEST_FILE "file_to_mmap"
#define TEST_FILE_SIZE 1024
#define TEST_FILE_MODE 0600
unsigned long legacy_flags;
unsigned long map_flags;
int fd_file;
void *mapped_address;
fd_file = open(TEST_FILE, O_CREAT | O_RDWR, TEST_FILE_MODE);
legacy_flags = LEGACY_MAP_MASK;
map_flags = ~(legacy_flags) + MAP_SHARED_VALIDATE;
mapped_address = mmap(NULL, TEST_FILE_SIZE, PROT_READ | PROT_WRITE, map_flags, fd_file, 0);
if (errno == EOPNOTSUPP)
printf("Have a nice day");
Error:
mmap( ) failed with the unexpected error: EPERM (1)
uname -a: O/P:
Linux debian 4.19.0-10-amd64 #1 SMP Debian 4.19.132-1 (2020-07-24) x86_64 GNU/Linux
Related
I run ./umask as the following step:
[root filedir]#
[root filedir]#ls
access bar cdpwd.c changemod.c devrdev.c filetype.c ftw8 Makefile mycd.c umask.c unlink.c zap.c
access.c cdpwd changemod devrdev filetype foo ftw8.c mycd umask unlink zap
[root filedir]#./umask
[root filedir]#ll foo bar
-rw-r--r--. 1 root root 0 7月 23 11:56 bar
-rw-rw-rw-. 1 root root 0 7月 23 11:56 foo
Here is the code umask.c
#include "apue.h"
#include <fcntl.h>
#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
int
main(void)
{
umask(0);
if (creat("foo", RWRWRW) < 0)
err_sys("creat error for foo");
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (creat("bar", RWRWRW) < 0)
err_sys("creat error for bar");
exit(0);
}
as the code above,umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH),why does bar has read access for group and other?
system info :
[root filedir]#umask
0022
[root filedir]#cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root filedir]#gcc
gcc 4.8.5
Linux MiWiFi-R1CL-srv 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
It's embarrassed for me,I make a mistake.
My friend and I share the same virtual machine,he read the book faster than me.He runs 4-12 program,which the code like the following:
#include "apue.h"
int
main(void)
{
struct stat statbuf;
/* turn on set-group-ID and turn off group-execute */
if (stat("foo", &statbuf) < 0)
err_sys("stat error for foo");
if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0)
err_sys("chmod error for foo");
/* set absolute mode to "rw-r--r--" */
if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)
err_sys("chmod error for bar");
exit(0);
}
As for this problem ,I delete the foo and bar,and run ./umask,then everything run well.
I am actually recoding malloc, using an avl, and when i test it with a script like:
#!/bin/sh
export DYLD_LIBRARY_PATH=.
export DYLD_INSERT_LIBRARIES="libft_malloc.so"
export DYLD_FORCE_FLAT_NAMESPACE=1`
$#
and then ./script ls or ./script vim, it works well. But when i export manually the variables in my shell, i have this error:
dyld: warning: could not load inserted library 'libft_malloc.so' into
library validated process because no suitable image found. Did find:
libft_malloc.so: code signing blocked mmap() of 'libft_malloc.so'
here is how is use mmap:
pges_ctrl.header_pge = mmap(NULL, getpagesize() * NB_PAGES, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (pges_ctrl.header_pge == MAP_FAILED)
return (0);
and
tmp = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (tmp == MAP_FAILED)
{
pges_ctrl.errors |= MMAP_BAD_ALLOC;
return NULL;
}
If you have idea about what could be the cause of it, or how i could debug that, that would help a lot !
I am working on Mac OSX Sierra 10.12.6
Compiled with gcc -Wall -Wextra -Werror
Thanks for the help!
Can a process read /proc/self/exe using mmap? This program fails to mmap the file:
$ cat e.c
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
int main()
{
int f=open("/proc/self/exe",O_RDONLY);
char*p=mmap(NULL,0,PROT_READ,0,f,0);
return 0;
}
$ cc e.c -o e
$ strace ./e
[snip]
open("/proc/self/exe", O_RDONLY) = 3
mmap(NULL, 0, PROT_READ, MAP_FILE, 3, 0) = -1 EINVAL (Invalid argument)
exit_group(0) = ?
+++ exited with 0 +++
You are making 2 mistakes here:
Mapped size must be > 0. Zero-size mappings are invalid.
You have to specify, if you want to create a shared (MAP_SHARED) or a private (MAP_PRIVATE) mapping.
The following should work for example:
char *p = mmap(NULL, 4096, PROT_READ, MAP_SHARED, f, 0);
If you wish to map the full executable, you will have to do a stat() on it first, to retrieve the correct file size and then use that as the second parameter to mmap().
I'm trying to create a memory map using the c commands
void* mem_map = mmap(NULL,
sizeof(serverData), //200000
PROT_READ | PROT_WRITE,
MAP_SHARED,
mem_map_fp,
0);
if(mem_map == MAP_FAILED){
bail_out(EXIT_FAILURE, "mmap");
}
The program compiles, but when trying to run the following error is produced:
mmap: No such device
To my understanding there is nothing wrong with the code which makes me suspect that the reason might be a little more complex.
I'm running this linux version:
Linux ubuntu 4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
I suppose you are trying to allocate memory, so you should use the MAP_ANON or MAP_ANONYMOUS flag, along with the standard arguments -1 for the file descriptor and 0 for the offset, like so :
mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
The Mac OS X mmap man page says that it is possible to allocate superpages and I gather it is the same thing as Linux huge pages.
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/mmap.2.html
However the following simple test fails on Mac OS X (Yosemite 10.10.5):
#include <stdio.h>
#include <sys/mman.h>
#include <mach/vm_statistics.h>
int
main()
{
void *p = mmap((void *) 0x000200000000, 8 * 1024 * 1024,
PROT_READ | PROT_WRITE,
MAP_ANON | MAP_FIXED | MAP_PRIVATE,
VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);
printf("%p\n", p);
if (p == MAP_FAILED)
perror(NULL);
return 0;
}
The output is:
0xffffffffffffffff
Cannot allocate memory
The result is the same with MAP_FIXED removed from the flags and NULL supplied as the address argument. Replacing VM_FLAGS_SUPERPAGE_SIZE_2MB with -1 results in the expected result, that is no error occurs, but obviously the allocated memory space uses regular 4k pages then.
What might be a problem with allocating superpages this way?
This minor modification to the posted example works for me on Mac OS 10.10.5:
#include <stdio.h>
#include <sys/mman.h>
#include <mach/vm_statistics.h>
int
main()
{
void *p = mmap(NULL,
8*1024*1024,
PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE,
VM_FLAGS_SUPERPAGE_SIZE_2MB, // mach flags in fd argument
0);
printf("%p\n", p);
if (p == MAP_FAILED)
perror(NULL);
return 0;
}