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