Re: Msync Invalid Arg

Michal Nazarewicz <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
[email protected] writes:

> Whne I try to execute following code I get "Invalid Argumemt" error
> message in msync.

/* Code rewritten to be shorter */

> #include <fcntl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/mman.h>
> #include <unistd.h>
> 
> #define DIE_IF(cnd, msg) do{ if (cnd) { perror(msg); return 1; } }while(0)
> 
> int main(void)
> {
> 	char data[20];
> 	void *addr;
> 	int fd, ret;
> 
> 	fd = open("/dev/mem", O_RDWR | O_SYNC);
> 	DIE_IF(fd < 0, "open");
> 
> 	addr = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> 	DIE_IF(addr == MAP_FAILED, "mmap");
> 
> 	strcpy(data, "Test Msg");
> 	ret = write(fd, data, strlen(data));
> 	DIE_IF(ret < 0, "write");
> 
> 	ret = msync(addr, 4096, MS_SYNC);
> 	DIE_IF(ret < 0, "msync");
> 
> 	ret = lseek(fd, 0, SEEK_SET);
> 	DIE_IF(ret < 0, "lseek");
> 
> 	memset(data, 0, sizeof data);
> 	ret = read(fd, data, sizeof data);
> 	DIE_IF(ret < 0, "read");
> 	printf("Read: %s.\n", data);
> 
> 	return 0;
> }


The msync(2) fails because /dev/mem driver (drivers/char/mem.c) does not
implement fsync file operation which is used by msync(2) system call.

This sort of makes sense since there is no need to sync the /dev/mem
file -- there is no backing storage on hard drive or anywhere, when you
write data there you write directly to the memory and same goes for
reading.

Anyhow, I'm Ccing this to linux-kernel as maybe we need a dummy fsync
operation for /dev/mem which would do nothing and return zero?

--
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
signature.asc (application/pgp-signature, 197 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (GNU/Linux)

iEYEARECAAYFAks6EokACgkQUyzLALfG3x4DJQCffDNvg08atG4/kAtlS3cNVARo
cJYAniaVQS2Lo5kytNwCJe1JQh/j4uvN
=2ln3
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.