2 * refclock_shm - clock driver for utc via shared memory
3 * - under construction -
4 * To add new modes: Extend or union the shmTime-struct. Do not
5 * extend/shrink size, because otherwise existing implementations
6 * will specify wrong size of shared memory-segment
14 #if defined(REFCLOCK) && defined(CLOCK_SHM)
20 #include "ntp_refclock.h"
22 #include "ntp_unixtime.h"
24 #include "ntp_stdlib.h"
39 * This driver supports a reference clock attached thru shared memory
43 * SHM interface definitions
45 #define PRECISION (-1) /* precision assumed (0.5 s) */
46 #define REFID "SHM" /* reference ID */
47 #define DESCRIPTION "SHM/Shared memory interface"
49 #define NSAMPLES 3 /* stages of median filter */
54 static int shm_start (int, struct peer *);
55 static void shm_shutdown (int, struct peer *);
56 static void shm_poll (int unit, struct peer *);
61 struct refclock refclock_shm = {
62 shm_start, /* start up driver */
63 shm_shutdown, /* shut down driver */
64 shm_poll, /* transmit poll message */
65 noentry, /* not used */
66 noentry, /* initialize driver (not used) */
67 noentry, /* not used */
68 NOFLAGS /* not used */
71 int mode; /* 0 - if valid set
75 * if count before and after read of values is equal,
80 time_t clockTimeStampSec;
81 int clockTimeStampUSec;
82 time_t receiveTimeStampSec;
83 int receiveTimeStampUSec;
91 struct shmTime *getShmTime(int);
93 struct shmTime *getShmTime (int unit) {
97 assert (unit<10); /* MAXUNIT is 4, so should never happen */
98 shmid=shmget (0x4e545030+unit, sizeof (struct shmTime),
99 IPC_CREAT|(unit<2?0700:0777));
100 if (shmid==-1) { /*error */
101 msyslog(LOG_ERR,"SHM shmget (unit %d): %s",unit,strerror(errno));
104 else { /* no error */
105 struct shmTime *p=(struct shmTime *)shmat (shmid, 0, 0);
106 if ((int)(long)p==-1) { /* error */
107 msyslog(LOG_ERR,"SHM shmat (unit %d): %s",unit,strerror(errno));
114 LPSECURITY_ATTRIBUTES psec=0;
116 SECURITY_DESCRIPTOR sd;
117 SECURITY_ATTRIBUTES sa;
118 sprintf (buf,"NTP%d",unit);
119 if (unit>=2) { /* world access */
120 if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
121 msyslog(LOG_ERR,"SHM InitializeSecurityDescriptor (unit %d): %m",unit);
124 if (!SetSecurityDescriptorDacl(&sd,1,0,0)) {
125 msyslog(LOG_ERR,"SHM SetSecurityDescriptorDacl (unit %d): %m",unit);
128 sa.nLength=sizeof (SECURITY_ATTRIBUTES);
129 sa.lpSecurityDescriptor=&sd;
133 shmid=CreateFileMapping ((HANDLE)0xffffffff, psec, PAGE_READWRITE,
134 0, sizeof (struct shmTime),buf);
135 if (!shmid) { /*error*/
137 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
138 0, GetLastError (), 0, buf, sizeof (buf), 0);
139 msyslog(LOG_ERR,"SHM CreateFileMapping (unit %d): %s",unit,buf);
143 struct shmTime *p=(struct shmTime *) MapViewOfFile (shmid,
144 FILE_MAP_WRITE, 0, 0, sizeof (struct shmTime));
145 if (p==0) { /*error*/
147 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
148 0, GetLastError (), 0, buf, sizeof (buf), 0);
149 msyslog(LOG_ERR,"SHM MapViewOfFile (unit %d): %s",unit,buf);
157 * shm_start - attach to shared memory
165 struct refclockproc *pp;
167 pp->io.clock_recv = noentry;
168 pp->io.srcclock = (caddr_t)peer;
171 pp->unitptr = (caddr_t)getShmTime(unit);
174 * Initialize miscellaneous peer variables
176 memcpy((char *)&pp->refid, REFID, 4);
177 if (pp->unitptr!=0) {
178 ((struct shmTime*)pp->unitptr)->precision=PRECISION;
179 peer->precision = ((struct shmTime*)pp->unitptr)->precision;
180 ((struct shmTime*)pp->unitptr)->valid=0;
181 ((struct shmTime*)pp->unitptr)->nsamples=NSAMPLES;
182 pp->clockdesc = DESCRIPTION;
192 * shm_shutdown - shut down the clock
200 register struct shmTime *up;
201 struct refclockproc *pp;
204 up = (struct shmTime *)pp->unitptr;
206 /* HMS: shmdt()wants char* or const void * */
209 UnmapViewOfFile (up);
215 * shm_poll - called by the transmit procedure
223 register struct shmTime *up;
224 struct refclockproc *pp;
227 * This is the main routine. It snatches the time from the shm
228 * board and tacks on a local timestamp.
231 up = (struct shmTime*)pp->unitptr;
232 if (up==0) { /* try to map again - this may succeed if meanwhile some-
233 body has ipcrm'ed the old (unaccessible) shared mem
235 pp->unitptr = (caddr_t)getShmTime(unit);
236 up = (struct shmTime*)pp->unitptr;
239 refclock_report(peer, CEVNT_FAULT);
253 tvr.tv_sec=up->receiveTimeStampSec;
254 tvr.tv_usec=up->receiveTimeStampUSec;
255 tvt.tv_sec=up->clockTimeStampSec;
256 tvt.tv_usec=up->clockTimeStampUSec;
261 tvr.tv_sec=up->receiveTimeStampSec;
262 tvr.tv_usec=up->receiveTimeStampUSec;
263 tvt.tv_sec=up->clockTimeStampSec;
264 tvt.tv_usec=up->clockTimeStampUSec;
269 msyslog (LOG_ERR, "SHM: bad mode found in shared memory: %d",up->mode);
273 time_t help; /* XXX NetBSD has incompatible tv_sec */
275 TVTOTS(&tvr,&pp->lastrec);
276 pp->lastrec.l_ui += JAN_1970;
277 /* pp->lasttime = current_time; */
281 pp->day=t->tm_yday+1;
283 pp->minute=t->tm_min;
284 pp->second=t->tm_sec;
285 pp->nsec=tvt.tv_usec * 1000;
286 peer->precision=up->precision;
290 refclock_report(peer, CEVNT_FAULT);
291 msyslog (LOG_NOTICE, "SHM: access clash in shared memory");
296 refclock_report(peer, CEVNT_TIMEOUT);
298 msyslog (LOG_NOTICE, "SHM: no new value found in shared memory");
302 if (!refclock_process(pp)) {
303 refclock_report(peer, CEVNT_BADTIME);
306 pp->lastref = pp->lastrec;
307 refclock_receive(peer);
312 #endif /* REFCLOCK */