File: mmap.c

package info (click to toggle)
comedilib 0.7.22-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,348 kB
  • ctags: 2,465
  • sloc: ansic: 16,316; sh: 8,492; ruby: 1,360; makefile: 325; yacc: 262; perl: 257; lex: 79; python: 59
file content (161 lines) | stat: -rw-r--r-- 2,821 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

#include <stdio.h>
#include <comedilib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#include <sys/mman.h>
#include <signal.h>
#include <setjmp.h>
#include <stdlib.h>

#include "comedi_test.h"

/* XXX this should come from elsewhere */
#define PAGE_SIZE 4096

#define N_SAMPLES 10000

#define BUFSZ N_SAMPLES*sizeof(sampl_t)

#define MAPLEN 20480

sigjmp_buf jump_env;

void segv_handler(int num)
{
	siglongjmp(jump_env,1);
}

int test_segfault(void *memptr)
{
	volatile char tmp;
	int ret;
	struct sigaction act;
	struct sigaction oldact;

	memset(&act,0,sizeof(act));
	act.sa_handler=&segv_handler;
	ret = sigaction(SIGSEGV,&act,&oldact);
	if(ret)
	{
		fprintf(stderr, "sigaction failed\n");
		return 0;
	}
	ret=sigsetjmp(jump_env, 1);
	if(!ret) tmp = *((char *)(memptr));
	sigaction(SIGSEGV,&oldact,NULL);
	return ret;
}

int test_mmap(void)
{
	comedi_cmd cmd;
	unsigned char *buf;
	unsigned int chanlist[1];
	int go;
	int fails;
	int total=0;
	int ret;
	void *b;
	unsigned char *adr;
	unsigned char *map;
	unsigned int flags;
	int i;

	flags = comedi_get_subdevice_flags(device,subdevice);

	if(!(flags&SDF_CMD) || flags&SDF_WRITEABLE){
		printf("not applicable\n");
		return 0;
	}

	if(comedi_get_cmd_generic_timed(device,subdevice,&cmd,1)<0){
		printf("E: comedi_get_cmd_generic_timed failed\n");
		return 0;
	}

	buf=malloc(BUFSZ);

	map=mmap(NULL,MAPLEN,PROT_READ,MAP_SHARED,comedi_fileno(device),0);
	if(!map){
		printf("E: mmap() failed\n");
		return 0;
	}

	/* test readability */
	for(adr=map;adr<map+MAPLEN;adr+=PAGE_SIZE){
		ret=test_segfault(adr);
		if(ret){
			printf("E: %p failed\n",adr);
		}else{
			printf("%p ok\n",adr);
		}
	}

	if(realtime)cmd.flags |= TRIG_RT;

	cmd.chanlist = chanlist;
	cmd.scan_end_arg = 1;
	cmd.stop_arg = N_SAMPLES;
	cmd.chanlist_len = 1;
	chanlist[0] = CR_PACK(0,0,0);

	comedi_command(device,&cmd);

	go=1;
	b=buf;
	while(go){
		ret = read(comedi_fileno(device),b,BUFSZ);
		if(ret<0){
			if(errno==EAGAIN){
				usleep(10000);
			}else{
				go = 0;
				perror("read");
			}
		}else if(ret==0){
			go = 0;
		}else{
			total += ret;
			b += ret;
			if(verbose) printf("read %d %d\n",ret,total);
		}
	}

	fails = 0;
	for(i=0;i<total;i++){
		if(buf[i]!=map[i]){
			if(fails==0)printf("E: mmap compare failed\n");
			printf("offset %d (read=%02x mmap=%02x)\n",i,
				buf[i], map[i]);
			go = 0;
			fails++;
			if(fails>10)break;
		}
	}
	if(fails==0) printf("compare ok\n");

	munmap(map,MAPLEN);

	/* test if area is really unmapped */
	for(adr=map;adr<map+MAPLEN;adr+=PAGE_SIZE){
		ret=test_segfault(adr);
		if(ret){
			printf("%p segfaulted (ok)\n",adr);
		}else{
			printf("E: %p still mapped\n",adr);
		}
	}
	
	free(buf);

	return 0;
}