/contrib/ntp/util/sht.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 185 lines · 169 code · 5 blank · 11 comment · 17 complexity · c6df623326a7dece28d9b97c31072b5f MD5 · raw file

  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 *
  42. getShmTime (
  43. int unit
  44. )
  45. {
  46. #ifndef SYS_WINNT
  47. int shmid=shmget (0x4e545030+unit, sizeof (struct shmTime), IPC_CREAT|0777);
  48. if (shmid==-1) {
  49. perror ("shmget");
  50. exit (1);
  51. }
  52. else {
  53. struct shmTime *p=(struct shmTime *)shmat (shmid, 0, 0);
  54. if ((int)(long)p==-1) {
  55. perror ("shmat");
  56. p=0;
  57. }
  58. assert (p!=0);
  59. return p;
  60. }
  61. #else
  62. char buf[10];
  63. LPSECURITY_ATTRIBUTES psec=0;
  64. sprintf (buf,"NTP%d",unit);
  65. SECURITY_DESCRIPTOR sd;
  66. SECURITY_ATTRIBUTES sa;
  67. HANDLE shmid;
  68. assert (InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION));
  69. assert (SetSecurityDescriptorDacl(&sd,1,0,0));
  70. sa.nLength=sizeof (SECURITY_ATTRIBUTES);
  71. sa.lpSecurityDescriptor=&sd;
  72. sa.bInheritHandle=0;
  73. shmid=CreateFileMapping ((HANDLE)0xffffffff, 0, PAGE_READWRITE,
  74. psec, sizeof (struct shmTime),buf);
  75. if (!shmid) {
  76. shmid=CreateFileMapping ((HANDLE)0xffffffff, 0, PAGE_READWRITE,
  77. 0, sizeof (struct shmTime),buf);
  78. cout <<"CreateFileMapping with psec!=0 failed"<<endl;
  79. }
  80. if (!shmid) {
  81. char mbuf[1000];
  82. FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
  83. 0, GetLastError (), 0, mbuf, sizeof (mbuf), 0);
  84. int x=GetLastError ();
  85. cout <<"CreateFileMapping "<<buf<<":"<<mbuf<<endl;
  86. exit (1);
  87. }
  88. else {
  89. struct shmTime *p=(struct shmTime *) MapViewOfFile (shmid,
  90. FILE_MAP_WRITE, 0, 0, sizeof (struct shmTime));
  91. if (p==0) {
  92. char mbuf[1000];
  93. FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
  94. 0, GetLastError (), 0, mbuf, sizeof (mbuf), 0);
  95. cout <<"MapViewOfFile "<<buf<<":"<<mbuf<<endl;
  96. exit (1);
  97. }
  98. return p;
  99. }
  100. return 0;
  101. #endif
  102. }
  103. int
  104. main (
  105. int argc,
  106. char *argv[]
  107. )
  108. {
  109. volatile struct shmTime *p=getShmTime(2);
  110. if (argc<=1) {
  111. printf ("usage: %s r[c][l]|w|snnn\n",argv[0]);
  112. printf (" r read shared memory\n");
  113. printf (" c clear valid-flag\n");
  114. printf (" l loop (so, rcl will read and clear in a loop\n");
  115. printf (" w write shared memory with current time\n");
  116. printf (" snnnn set nsamples to nnn\n");
  117. printf (" lnnnn set leap to nnn\n");
  118. printf (" pnnnn set precision to -nnn\n");
  119. exit (0);
  120. }
  121. switch (argv[1][0]) {
  122. case 's': {
  123. p->nsamples=atoi(&argv[1][1]);
  124. }
  125. break;
  126. case 'l': {
  127. p->leap=atoi(&argv[1][1]);
  128. }
  129. break;
  130. case 'p': {
  131. p->precision=-atoi(&argv[1][1]);
  132. }
  133. break;
  134. case 'r': {
  135. char *ap=&argv[1][1];
  136. int clear=0;
  137. int loop=0;
  138. printf ("reader\n");
  139. while (*ap) {
  140. switch (*ap) {
  141. case 'l' : loop=1; break;
  142. case 'c' : clear=1; break;
  143. }
  144. ap++;
  145. }
  146. do {
  147. printf ("mode=%d, count=%d, clock=%d.%d, rec=%d.%d,\n",
  148. p->mode,p->count,p->clockTimeStampSec,p->clockTimeStampUSec,
  149. p->receiveTimeStampSec,p->receiveTimeStampUSec);
  150. printf (" leap=%d, precision=%d, nsamples=%d, valid=%d\n",
  151. p->leap, p->precision, p->nsamples, p->valid);
  152. if (!p->valid)
  153. printf ("***\n");
  154. if (clear) {
  155. p->valid=0;
  156. printf ("cleared\n");
  157. }
  158. if (loop)
  159. sleep (1);
  160. } while (loop);
  161. }
  162. break;
  163. case 'w': {
  164. printf ("writer\n");
  165. p->mode=0;
  166. if (!p->valid) {
  167. p->clockTimeStampSec=time(0)-20;
  168. p->clockTimeStampUSec=0;
  169. p->receiveTimeStampSec=time(0)-1;
  170. p->receiveTimeStampUSec=0;
  171. printf ("%d %d\n",p->clockTimeStampSec, p->receiveTimeStampSec);
  172. p->valid=1;
  173. }
  174. else {
  175. printf ("p->valid still set\n"); /* not an error! */
  176. }
  177. }
  178. break;
  179. }
  180. }