1 /* $NetBSD: sht.c,v 1.1.1.2 2013/12/27 23:31:40 christos Exp $ */ 2 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 32 */ 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; 42 }; 43 44 struct shmTime * 45 getShmTime ( 46 int unit 47 ) 48 { 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); 54 } 55 else { 56 struct shmTime *p=(struct shmTime *)shmat (shmid, 0, 0); 57 if ((int)(long)p==-1) { 58 perror ("shmat"); 59 p=0; 60 } 61 assert (p!=0); 62 return p; 63 } 64 #else 65 char buf[10]; 66 LPSECURITY_ATTRIBUTES psec=0; 67 snprintf (buf, sizeof(buf), "NTP%d", unit); 68 SECURITY_DESCRIPTOR sd; 69 SECURITY_ATTRIBUTES sa; 70 HANDLE shmid; 71 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; 83 } 84 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); 92 } 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); 102 } 103 return p; 104 } 105 return 0; 106 #endif 107 } 108 109 110 int 111 main ( 112 int argc, 113 char *argv[] 114 ) 115 { 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); 127 } 128 switch (argv[1][0]) { 129 case 's': { 130 p->nsamples=atoi(&argv[1][1]); 131 } 132 break; 133 case 'l': { 134 p->leap=atoi(&argv[1][1]); 135 } 136 break; 137 case 'p': { 138 p->precision=-atoi(&argv[1][1]); 139 } 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; 150 } 151 ap++; 152 } 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"); 164 } 165 if (loop) 166 sleep (1); 167 } while (loop); 168 } 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; 180 } 181 else { 182 printf ("p->valid still set\n"); /* not an error! */ 183 } 184 } 185 break; 186 } 187 } 188