LINUX.ORG.RU

История изменений

Исправление cumvillain, (текущая версия) :

Да ты угораешь что ли:

$ cat test.c
#define _GNU_SOURCE

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

#define DATA_LEN 9

void
map_and_print(int fd)
{
	char *s;

	s = mmap(NULL, DATA_LEN, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	printf(" pre: %s\n", s);
	snprintf(s, DATA_LEN, "%d", getpid());
	printf("post: %s\n", s);
}

int
main(void)
{
	int fd, cfd;
	size_t written;
	pid_t pid;

	fd = memfd_create("data", MFD_ALLOW_SEALING);
	if (fd == -1)
		err(1, "cannot create memfd");

	written = write(fd, "xxxxxxxx", DATA_LEN);
	if (written == -1)
		err(1, "cannot write data to memory area");

	if (fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE) == -1)
		err(1, "cannot seal");

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		exit(0);
	}
	
	sleep(5);

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		exit(0);
	}

	wait(NULL);
	wait(NULL);

	return 0;
}
$ ./test
 pre: xxxxxxxx
post: 62583
 pre: xxxxxxxx
post: 62598

Исправление cumvillain, :

Да ты угораешь что ли:

$ cat test.c
#define _GNU_SOURCE

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

#define DATA_LEN 9

void
map_and_print(int fd)
{
	char *s;

	s = mmap(NULL, DATA_LEN, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	printf(" pre: %s\n", s);
	snprintf(s, DATA_LEN, "%d", getpid());
	printf("post: %s\n", s);
}

int
main(void)
{
	int fd, cfd;
	size_t written;
	pid_t pid;

	fd = memfd_create("data", MFD_ALLOW_SEALING);
	if (fd == -1)
		err(1, "cannot create memfd");

	written = write(fd, "xxxxxxxx", 9);
	if (written == -1)
		err(1, "cannot write data to memory area");

	if (fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE) == -1)
		err(1, "cannot seal");

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		exit(0);
	}
	
	sleep(5);

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		exit(0);
	}

	wait(NULL);
	wait(NULL);

	return 0;
}
$ ./test
 pre: xxxxxxxx
post: 62583
 pre: xxxxxxxx
post: 62598

Исправление cumvillain, :

Да ты угораешь что ли:

$ cat test.c
#define _GNU_SOURCE

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

#define DATA_LEN 9

void
map_and_print(int fd)
{
	char *s;

	s = mmap(NULL, DATA_LEN, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	printf(" pre: %s\n", s);
	snprintf(s, DATA_LEN, "%d", getpid());
	printf("post: %s\n", s);
}

int
main(void)
{
	int fd, cfd;
	size_t written;
	pid_t pid;

	fd = memfd_create("data", MFD_ALLOW_SEALING);
	if (fd == -1)
		err(1, "cannot create memfd");

	lseek(fd, 0, SEEK_SET);
	written = write(fd, "xxxxxxxx", 9);
	if (written == -1)
		err(1, "cannot write data to memory area");

	if (fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE) == -1)
		err(1, "cannot seal");

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		exit(0);
	}
	
	sleep(5);

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		exit(0);
	}

	wait(NULL);
	wait(NULL);

	return 0;
}
$ ./test
 pre: xxxxxxxx
post: 62583
 pre: xxxxxxxx
post: 62598

Исправление cumvillain, :

Да ты угораешь что ли:

$ cat test.c
#define _GNU_SOURCE

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

#define DATA_LEN 9

void
map_and_print(int fd)
{
	char *s;

	s = mmap(NULL, DATA_LEN, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	printf(" pre: %s\n", s);
	snprintf(s, DATA_LEN, "%d", getpid());
	printf("post: %s\n", s);
}

int
main(void)
{
	int fd, cfd;
	size_t written;
	pid_t pid;

	fd = memfd_create("data", MFD_ALLOW_SEALING);
	if (fd == -1)
		err(1, "cannot create memfd");

	lseek(fd, 0, SEEK_SET);
	written = write(fd, "xxxxxxxx", 9);
	if (written == -1)
		err(1, "cannot write data to memory area");

	if (fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE) == -1)
		err(1, "cannot seal");

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		lseek(fd, 0, SEEK_SET);
		exit(0);
	}
	
	sleep(5);

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		lseek(fd, 0, SEEK_SET);
		exit(0);
	}

	wait(NULL);
	wait(NULL);

	return 0;
}
$ ./test
 pre: xxxxxxxx
post: 62583
 pre: xxxxxxxx
post: 62598

Исходная версия cumvillain, :

Да ты угораешь что ли:

$ cat test.c
#define _GNU_SOURCE

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

#define DATA_LEN 9

void
map_and_print(int fd)
{
	char *s;

	s = mmap(NULL, DATA_LEN, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	printf(" pre: %s\n", s);
	snprintf(s, DATA_LEN, "%d", getpid());
	printf("post: %s\n", s);
}

int
main(void)
{
	int fd, cfd;
	size_t written;
	pid_t pid;

	fd = memfd_create("data", MFD_ALLOW_SEALING);
	if (fd == -1)
		err(1, "cannot create memfd");

	lseek(fd, 0, SEEK_SET);
	written = write(fd, "xxxxxxxx", 9);
	if (written == -1)
		err(1, "cannot write data to memory area");

	if (fcntl(fd, F_ADD_SEALS, F_SEAL_WRITE) == -1)
		err(1, "cannot seal");

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		lseek(fd, 0, SEEK_SET);
		exit(0);
	}
	
	sleep(5);

	pid = fork();
	if (pid == 0) {
		cfd = dup(fd);
		map_and_print(cfd);
		lseek(fd, 0, SEEK_SET);
		exit(0);
	}

	wait(NULL);
	wait(NULL);

	return 0;
}
$ ./test
 pre: xxxxxxxx
post:62583
 pre: xxxxxxxx
post:62598