Skip to contentSkip to conversation

Msync Invalid Arg

DISCUSSION [email protected] 2 participants 2 messages Dec 29, 2009

lore
Share & tools
lore
#0 lore raw

Whne I try to execute following code then get an erro message that "Invalid Argumemt" in msync.

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/types.h>

int main()

{
	int fd;
	char data[20];
	__u8 *start_addr,*end_addr;
	int PageSize;

	if ( (PageSize = sysconf(_SC_PAGE_SIZE)) < 0) {
	    perror("sysconf() Error=");
		    return -1;
	}
	printf("PageSize = %d\n",PageSize);

	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
	{ 
		printf("Error opening /dev/mem\n");
		return -1;
	}

	start_addr = mmap(0, PageSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
										0);
	if(start_addr == MAP_FAILED)
	{
		printf("enc_status map failed %s\n", strerror(errno));
		return -1;
	}

	end_addr = start_addr + (4096 - 1);
	strcpy(data, "Test Msg");
	printf("\n%s:Writing NVRAM.......\n", __FUNCTION__);
	if(write(fd, data, strlen(data)) <= 0)
	{
		printf("%s: Not able to write on NVRAM. %s.\n", __FUNCTION__,
				strerror(errno));
		return -1;
	}
	printf("\n%s:Synching NVRAM.......\n", __FUNCTION__);
	if(msync(start_addr, PageSize, MS_SYNC) <= 0)
	{
		printf("%s: Not able to sync to NVRAM. %s.\n", __FUNCTION__,
				strerror(errno));
		return -1;
	}
	printf("\n%s:reading NVRAM.......\n", __FUNCTION__);
	strcpy(data, "");
	if(read(fd, data, sizeof(data)) <= 0)
	{
		printf("%s: Not able to read on NVRAM. %s.\n", __FUNCTION__,
				strerror(errno));
		return -1;
	}
	printf("%s: data  %s.\n", __FUNCTION__, data);
	return 0;
}

--
This message was sent on behalf of [email protected] at openSubscriber.com
http://www.opensubscriber.com/messages/[email protected]/topic.html

#1 lore raw
Quoted 2 lines · [email protected] writes:

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

/* Code rewritten to be shorter */

Quoted 31 lines

#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");
Quoted 7 lines
	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--