#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "ntp_types.h"
#if defined(REFCLOCK) && defined(CLOCK_SHM)
#include "ntpd.h"
#undef fileno
#include "ntp_io.h"
#undef fileno
#include "ntp_refclock.h"
#undef fileno
#include "timespecops.h"
#undef fileno
#include "ntp_stdlib.h"
#include "ntp_assert.h"
#undef fileno
#include <ctype.h>
#undef fileno
#ifndef SYS_WINNT
# include <sys/ipc.h>
# include <sys/shm.h>
# include <assert.h>
# include <unistd.h>
# include <stdio.h>
#endif
#ifdef HAVE_STDATOMIC_H
# include <stdatomic.h>
#endif
#define PRECISION (-1)
#define REFID "SHM"
#define DESCRIPTION "SHM/Shared memory interface"
#define NSAMPLES 3
#define SHM_MODE_PRIVATE 0x0001
static int shm_start (int unit, struct peer *peer);
static void shm_shutdown (int unit, struct peer *peer);
static void shm_poll (int unit, struct peer *peer);
static void shm_timer (int unit, struct peer *peer);
static void shm_clockstats (int unit, struct peer *peer);
static void shm_control (int unit, const struct refclockstat * in_st,
struct refclockstat * out_st, struct peer *peer);
struct refclock refclock_shm = {
shm_start,
shm_shutdown,
shm_poll,
shm_control,
noentry,
noentry,
shm_timer,
};
struct shmTime {
int mode;
volatile int count;
time_t clockTimeStampSec;
int clockTimeStampUSec;
time_t receiveTimeStampSec;
int receiveTimeStampUSec;
int leap;
int precision;
int nsamples;
volatile int valid;
unsigned clockTimeStampNSec;
unsigned receiveTimeStampNSec;
int dummy[8];
};
struct shmunit {
struct shmTime *shm;
int forall;
int ticks;
int good;
int notready;
int bad;
int clash;
time_t max_delta;
time_t max_delay;
};
static struct shmTime*
getShmTime(
int unit,
int forall
)
{
struct shmTime *p = NULL;
#ifndef SYS_WINNT
int shmid;
shmid=shmget(0x4e545030 + unit, sizeof (struct shmTime),
IPC_CREAT | (forall ? 0666 : 0600));
if (shmid == -1) {
msyslog(LOG_ERR, "SHM shmget (unit %d): %m", unit);
return NULL;
}
p = (struct shmTime *)shmat (shmid, 0, 0);
if (p == (struct shmTime *)-1) {
msyslog(LOG_ERR, "SHM shmat (unit %d): %m", unit);
return NULL;
}
return p;
#else
static const char * nspref[2] = { "Local", "Global" };
char buf[20];
LPSECURITY_ATTRIBUTES psec = 0;
HANDLE shmid = 0;
SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa;
unsigned int numch;
numch = snprintf(buf, sizeof(buf), "%s\\NTP%d",
nspref[forall != 0], (unit & 0xFF));
if (numch >= sizeof(buf)) {
msyslog(LOG_ERR, "SHM name too long (unit %d)", unit);
return NULL;
}
if (forall) {
if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
msyslog(LOG_ERR,"SHM InitializeSecurityDescriptor (unit %d): %m", unit);
return NULL;
}
if (!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE)) {
msyslog(LOG_ERR, "SHM SetSecurityDescriptorDacl (unit %d): %m", unit);
return NULL;
}
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = &sd;
sa.bInheritHandle = FALSE;
psec = &sa;
}
shmid = CreateFileMapping ((HANDLE)0xffffffff, psec, PAGE_READWRITE,
0, sizeof (struct shmTime), buf);
if (shmid == NULL) {
char buf[1000];
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
0, GetLastError (), 0, buf, sizeof (buf), 0);
msyslog(LOG_ERR, "SHM CreateFileMapping (unit %d): %s", unit, buf);
return NULL;
}
p = (struct shmTime *)MapViewOfFile(shmid, FILE_MAP_WRITE, 0, 0,
sizeof (struct shmTime));
if (p == NULL) {
char buf[1000];
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
0, GetLastError (), 0, buf, sizeof (buf), 0);
msyslog(LOG_ERR,"SHM MapViewOfFile (unit %d): %s", unit, buf);
return NULL;
}
return p;
#endif
ENSURE(!"getShmTime(): Not reached.");
}
static int
shm_start(
int unit,
struct peer *peer
)
{
struct refclockproc * const pp = peer->procptr;
struct shmunit * const up = emalloc_zero(sizeof(*up));
pp->io.clock_recv = noentry;
pp->io.srcclock = peer;
pp->io.datalen = 0;
pp->io.fd = -1;
up->forall = (unit >= 2) && !(peer->ttl & SHM_MODE_PRIVATE);
up->shm = getShmTime(unit, up->forall);
memcpy((char *)&pp->refid, REFID, 4);
if (up->shm != 0) {
pp->unitptr = up;
up->shm->precision = PRECISION;
peer->precision = up->shm->precision;
up->shm->valid = 0;
up->shm->nsamples = NSAMPLES;
pp->clockdesc = DESCRIPTION;
up->max_delay = 5;
up->max_delta = 4*3600;
return 1;
} else {
free(up);
pp->unitptr = NULL;
return 0;
}
}
static void
shm_control(
int unit,
const struct refclockstat * in_st,
struct refclockstat * out_st,
struct peer * peer
)
{
struct refclockproc * const pp = peer->procptr;
struct shmunit * const up = pp->unitptr;
UNUSED_ARG(unit);
UNUSED_ARG(in_st);
UNUSED_ARG(out_st);
if (NULL == up)
return;
if (pp->sloppyclockflag & CLK_FLAG1)
up->max_delta = 0;
else if (pp->fudgetime2 < 1. || pp->fudgetime2 > 86400.)
up->max_delta = 4*3600;
else
up->max_delta = (time_t)floor(pp->fudgetime2 + 0.5);
}
static void
shm_shutdown(
int unit,
struct peer *peer
)
{
struct refclockproc * const pp = peer->procptr;
struct shmunit * const up = pp->unitptr;
UNUSED_ARG(unit);
if (NULL == up)
return;
#ifndef SYS_WINNT
(void)shmdt((char *)up->shm);
#else
UnmapViewOfFile(up->shm);
#endif
free(up);
}
static void
shm_poll(
int unit,
struct peer *peer
)
{
struct refclockproc * const pp = peer->procptr;
struct shmunit * const up = pp->unitptr;
int major_error;
pp->polls++;
major_error = max(up->notready, up->bad);
major_error = max(major_error, up->clash);
if (pp->coderecv != pp->codeproc) {
pp->lastref = pp->lastrec;
refclock_receive(peer);
} else if (NULL == up->shm) {
refclock_report(peer, CEVNT_FAULT);
} else if (major_error == up->clash) {
refclock_report(peer, CEVNT_PROP);
} else if (major_error == up->bad) {
refclock_report(peer, CEVNT_BADREPLY);
} else {
refclock_report(peer, CEVNT_TIMEOUT);
}
shm_clockstats(unit, peer);
}
enum segstat_t {
OK, NO_SEGMENT, NOT_READY, BAD_MODE, CLASH
};
struct shm_stat_t {
int status;
int mode;
struct timespec tvc, tvr, tvt;
int precision;
int leap;
};
static inline void memory_barrier(void)
{
#ifdef HAVE_ATOMIC_THREAD_FENCE
atomic_thread_fence(memory_order_seq_cst);
#endif
}
static enum segstat_t shm_query(volatile struct shmTime *shm_in, struct shm_stat_t *shm_stat)
{
struct shmTime shmcopy;
volatile struct shmTime *shm = shm_in;
volatile int cnt;
unsigned int cns_new, rns_new;
if (shm == NULL) {
shm_stat->status = NO_SEGMENT;
return NO_SEGMENT;
}
shm_stat->tvc.tv_sec = shm_stat->tvc.tv_nsec = 0;
{
time_t now;
time(&now);
shm_stat->tvc.tv_sec = now;
}
if (shm->valid == 0) {
shm_stat->status = NOT_READY;
return NOT_READY;
}
cnt = shm->count;
memory_barrier();
memcpy(&shmcopy, (void*)(uintptr_t)shm, sizeof(struct shmTime));
shm->valid = 0;
memory_barrier();
if (shmcopy.mode > 0 && cnt != shm->count) {
shm_stat->status = CLASH;
return shm_stat->status;
}
shm_stat->status = OK;
shm_stat->mode = shmcopy.mode;
switch (shmcopy.mode) {
case 0:
shm_stat->tvr.tv_sec = shmcopy.receiveTimeStampSec;
shm_stat->tvr.tv_nsec = shmcopy.receiveTimeStampUSec * 1000;
rns_new = shmcopy.receiveTimeStampNSec;
shm_stat->tvt.tv_sec = shmcopy.clockTimeStampSec;
shm_stat->tvt.tv_nsec = shmcopy.clockTimeStampUSec * 1000;
cns_new = shmcopy.clockTimeStampNSec;
if ( ((cns_new - (unsigned)shm_stat->tvt.tv_nsec) < 1000)
&& ((rns_new - (unsigned)shm_stat->tvr.tv_nsec) < 1000)) {
shm_stat->tvt.tv_nsec = cns_new;
shm_stat->tvr.tv_nsec = rns_new;
}
break;
case 1:
shm_stat->tvr.tv_sec = shmcopy.receiveTimeStampSec;
shm_stat->tvr.tv_nsec = shmcopy.receiveTimeStampUSec * 1000;
rns_new = shmcopy.receiveTimeStampNSec;
shm_stat->tvt.tv_sec = shmcopy.clockTimeStampSec;
shm_stat->tvt.tv_nsec = shmcopy.clockTimeStampUSec * 1000;
cns_new = shmcopy.clockTimeStampNSec;
if ( ((cns_new - (unsigned)shm_stat->tvt.tv_nsec) < 1000)
&& ((rns_new - (unsigned)shm_stat->tvr.tv_nsec) < 1000)) {
shm_stat->tvt.tv_nsec = cns_new;
shm_stat->tvr.tv_nsec = rns_new;
}
break;
default:
shm_stat->status = BAD_MODE;
break;
}
shm_stat->leap = shmcopy.leap;
shm_stat->precision = shmcopy.precision;
return shm_stat->status;
}
static void
shm_timer(
int unit,
struct peer *peer
)
{
struct refclockproc * const pp = peer->procptr;
struct shmunit * const up = pp->unitptr;
volatile struct shmTime *shm;
l_fp tsrcv;
l_fp tsref;
int c;
struct calendar cd;
time_t tt;
vint64 ts;
enum segstat_t status;
struct shm_stat_t shm_stat;
up->ticks++;
if ((shm = up->shm) == NULL) {
shm = up->shm = getShmTime(unit, up->forall);
if (shm == NULL) {
DPRINTF(1, ("%s: no SHM segment\n",
refnumtoa(&peer->srcadr)));
return;
}
}
status = shm_query(shm, &shm_stat);
switch (status) {
case OK:
DPRINTF(2, ("%s: SHM type %d sample\n",
refnumtoa(&peer->srcadr), shm_stat.mode));
break;
case NO_SEGMENT:
return;
case NOT_READY:
DPRINTF(1, ("%s: SHM not ready\n",refnumtoa(&peer->srcadr)));
up->notready++;
return;
case BAD_MODE:
DPRINTF(1, ("%s: SHM type blooper, mode=%d\n",
refnumtoa(&peer->srcadr), shm->mode));
up->bad++;
msyslog (LOG_ERR, "SHM: bad mode found in shared memory: %d",
shm->mode);
return;
case CLASH:
DPRINTF(1, ("%s: type 1 access clash\n",
refnumtoa(&peer->srcadr)));
msyslog (LOG_NOTICE, "SHM: access clash in shared memory");
up->clash++;
return;
default:
DPRINTF(1, ("%s: internal error, unknown SHM fetch status\n",
refnumtoa(&peer->srcadr)));
msyslog (LOG_NOTICE, "internal error, unknown SHM fetch status");
up->bad++;
return;
}
tt = (time_t)shm_stat.tvt.tv_sec;
ts = time_to_vint64(&tt);
ntpcal_time_to_date(&cd, &ts);
c = snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
"%04u-%02u-%02uT%02u:%02u:%02u.%09ldZ",
cd.year, cd.month, cd.monthday,
cd.hour, cd.minute, cd.second,
(long)shm_stat.tvt.tv_nsec);
pp->lencode = (c > 0 && (size_t)c < sizeof(pp->a_lastcode)) ? c : 0;
tt = shm_stat.tvc.tv_sec - shm_stat.tvr.tv_sec;
if (tt < 0 || tt > up->max_delay) {
DPRINTF(1, ("%s:SHM stale/bad receive time, delay=%llds\n",
refnumtoa(&peer->srcadr), (long long)tt));
up->bad++;
msyslog (LOG_ERR, "SHM: stale/bad receive time, delay=%llds",
(long long)tt);
return;
}
tt = shm_stat.tvr.tv_sec - shm_stat.tvt.tv_sec - (shm_stat.tvr.tv_nsec < shm_stat.tvt.tv_nsec);
if (tt < 0)
tt = -tt;
if (up->max_delta > 0 && tt > up->max_delta) {
DPRINTF(1, ("%s: SHM diff limit exceeded, delta=%llds\n",
refnumtoa(&peer->srcadr), (long long)tt));
up->bad++;
msyslog (LOG_ERR, "SHM: difference limit exceeded, delta=%llds\n",
(long long)tt);
return;
}
DPRINTF(2, ("%s: SHM feeding data\n",
refnumtoa(&peer->srcadr)));
tsrcv = tspec_stamp_to_lfp(shm_stat.tvr);
tsref = tspec_stamp_to_lfp(shm_stat.tvt);
pp->leap = shm_stat.leap;
peer->precision = shm_stat.precision;
refclock_process_offset(pp, tsref, tsrcv, pp->fudgetime1);
up->good++;
}
static void shm_clockstats(
int unit,
struct peer *peer
)
{
struct refclockproc * const pp = peer->procptr;
struct shmunit * const up = pp->unitptr;
UNUSED_ARG(unit);
if (pp->sloppyclockflag & CLK_FLAG4) {
mprintf_clock_stats(
&peer->srcadr, "%3d %3d %3d %3d %3d",
up->ticks, up->good, up->notready,
up->bad, up->clash);
}
up->ticks = up->good = up->notready = up->bad = up->clash = 0;
}
#else
NONEMPTY_TRANSLATION_UNIT
#endif