1 | /* |
---|
2 | * sht.c - Testprogram for shared memory refclock |
---|
3 | * read/write shared memory segment; see usage |
---|
4 | */ |
---|
5 | #ifndef SYS_WINNT |
---|
6 | #include <sys/types.h> |
---|
7 | #include <sys/ipc.h> |
---|
8 | #include <sys/shm.h> |
---|
9 | #include <stdio.h> |
---|
10 | #include <time.h> |
---|
11 | #include <unistd.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #else |
---|
14 | #include <windows.h> |
---|
15 | #include <time.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <stdio.h> |
---|
18 | #include <iostream.h> |
---|
19 | #define sleep(x) Sleep(x*1000) |
---|
20 | #endif |
---|
21 | #include <assert.h> |
---|
22 | struct shmTime { |
---|
23 | int mode; /* 0 - if valid set |
---|
24 | * use values, |
---|
25 | * clear valid |
---|
26 | * 1 - if valid set |
---|
27 | * if count before and after read of values is equal, |
---|
28 | * use values |
---|
29 | * clear valid |
---|
30 | */ |
---|
31 | int count; |
---|
32 | time_t clockTimeStampSec; |
---|
33 | int clockTimeStampUSec; |
---|
34 | time_t receiveTimeStampSec; |
---|
35 | int receiveTimeStampUSec; |
---|
36 | int leap; |
---|
37 | int precision; |
---|
38 | int nsamples; |
---|
39 | int valid; |
---|
40 | }; |
---|
41 | struct shmTime *getShmTime (int unit) { |
---|
42 | #ifndef SYS_WINNT |
---|
43 | int shmid=shmget (0x4e545030+unit, sizeof (struct shmTime), IPC_CREAT|0777); |
---|
44 | if (shmid==-1) { |
---|
45 | perror ("shmget"); |
---|
46 | exit (1); |
---|
47 | } |
---|
48 | else { |
---|
49 | struct shmTime *p=(struct shmTime *)shmat (shmid, 0, 0); |
---|
50 | if ((int)(long)p==-1) { |
---|
51 | perror ("shmat"); |
---|
52 | p=0; |
---|
53 | } |
---|
54 | assert (p!=0); |
---|
55 | return p; |
---|
56 | } |
---|
57 | #else |
---|
58 | char buf[10]; |
---|
59 | LPSECURITY_ATTRIBUTES psec=0; |
---|
60 | sprintf (buf,"NTP%d",unit); |
---|
61 | SECURITY_DESCRIPTOR sd; |
---|
62 | SECURITY_ATTRIBUTES sa; |
---|
63 | HANDLE shmid; |
---|
64 | |
---|
65 | assert (InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)); |
---|
66 | assert (SetSecurityDescriptorDacl(&sd,1,0,0)); |
---|
67 | sa.nLength=sizeof (SECURITY_ATTRIBUTES); |
---|
68 | sa.lpSecurityDescriptor=&sd; |
---|
69 | sa.bInheritHandle=0; |
---|
70 | shmid=CreateFileMapping ((HANDLE)0xffffffff, 0, PAGE_READWRITE, |
---|
71 | psec, sizeof (struct shmTime),buf); |
---|
72 | if (!shmid) { |
---|
73 | shmid=CreateFileMapping ((HANDLE)0xffffffff, 0, PAGE_READWRITE, |
---|
74 | 0, sizeof (struct shmTime),buf); |
---|
75 | cout <<"CreateFileMapping with psec!=0 failed"<<endl; |
---|
76 | } |
---|
77 | |
---|
78 | if (!shmid) { |
---|
79 | char mbuf[1000]; |
---|
80 | FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, |
---|
81 | 0, GetLastError (), 0, mbuf, sizeof (mbuf), 0); |
---|
82 | int x=GetLastError (); |
---|
83 | cout <<"CreateFileMapping "<<buf<<":"<<mbuf<<endl; |
---|
84 | exit (1); |
---|
85 | } |
---|
86 | else { |
---|
87 | struct shmTime *p=(struct shmTime *) MapViewOfFile (shmid, |
---|
88 | FILE_MAP_WRITE, 0, 0, sizeof (struct shmTime)); |
---|
89 | if (p==0) { |
---|
90 | char mbuf[1000]; |
---|
91 | FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, |
---|
92 | 0, GetLastError (), 0, mbuf, sizeof (mbuf), 0); |
---|
93 | cout <<"MapViewOfFile "<<buf<<":"<<mbuf<<endl; |
---|
94 | exit (1); |
---|
95 | } |
---|
96 | return p; |
---|
97 | } |
---|
98 | return 0; |
---|
99 | #endif |
---|
100 | } |
---|
101 | void main (int argc, char *argv[]) { |
---|
102 | volatile struct shmTime *p=getShmTime(2); |
---|
103 | if (argc<=1) { |
---|
104 | printf ("usage: %s r[c][l]|w|snnn\n",argv[0]); |
---|
105 | printf (" r read shared memory\n"); |
---|
106 | printf (" c clear valid-flag\n"); |
---|
107 | printf (" l loop (so, rcl will read and clear in a loop\n"); |
---|
108 | printf (" w write shared memory with current time\n"); |
---|
109 | printf (" snnnn set nsamples to nnn\n"); |
---|
110 | printf (" lnnnn set leap to nnn\n"); |
---|
111 | printf (" pnnnn set precision to -nnn\n"); |
---|
112 | exit (0); |
---|
113 | } |
---|
114 | switch (argv[1][0]) { |
---|
115 | case 's': { |
---|
116 | p->nsamples=atoi(&argv[1][1]); |
---|
117 | } |
---|
118 | break; |
---|
119 | case 'l': { |
---|
120 | p->leap=atoi(&argv[1][1]); |
---|
121 | } |
---|
122 | break; |
---|
123 | case 'p': { |
---|
124 | p->precision=-atoi(&argv[1][1]); |
---|
125 | } |
---|
126 | break; |
---|
127 | case 'r': { |
---|
128 | char *ap=&argv[1][1]; |
---|
129 | int clear=0; |
---|
130 | int loop=0; |
---|
131 | printf ("reader\n"); |
---|
132 | while (*ap) { |
---|
133 | switch (*ap) { |
---|
134 | case 'l' : loop=1; break; |
---|
135 | case 'c' : clear=1; break; |
---|
136 | } |
---|
137 | ap++; |
---|
138 | } |
---|
139 | do { |
---|
140 | printf ("mode=%d, count=%d, clock=%d.%d, rec=%d.%d,\n", |
---|
141 | p->mode,p->count,p->clockTimeStampSec,p->clockTimeStampUSec, |
---|
142 | p->receiveTimeStampSec,p->receiveTimeStampUSec); |
---|
143 | printf (" leap=%d, precision=%d, nsamples=%d, valid=%d\n", |
---|
144 | p->leap, p->precision, p->nsamples, p->valid); |
---|
145 | if (!p->valid) |
---|
146 | printf ("***\n"); |
---|
147 | if (clear) { |
---|
148 | p->valid=0; |
---|
149 | printf ("cleared\n"); |
---|
150 | } |
---|
151 | if (loop) |
---|
152 | sleep (1); |
---|
153 | } while (loop); |
---|
154 | } |
---|
155 | break; |
---|
156 | case 'w': { |
---|
157 | printf ("writer\n"); |
---|
158 | p->mode=0; |
---|
159 | if (!p->valid) { |
---|
160 | p->clockTimeStampSec=time(0)-20; |
---|
161 | p->clockTimeStampUSec=0; |
---|
162 | p->receiveTimeStampSec=time(0)-1; |
---|
163 | p->receiveTimeStampUSec=0; |
---|
164 | printf ("%d %d\n",p->clockTimeStampSec, p->receiveTimeStampSec); |
---|
165 | p->valid=1; |
---|
166 | } |
---|
167 | else { |
---|
168 | printf ("p->valid still set\n"); // not an error! |
---|
169 | } |
---|
170 | } |
---|
171 | break; |
---|
172 | } |
---|
173 | } |
---|