59 #include <arpa/inet.h> 60 #include <sys/socket.h> 61 #include <sys/types.h> 75 #define RUNNING_DIR "/var/run" 76 #define LOCK_FILE "mbgsvcd.pid" 78 #define MBG_FIRST_COPYRIGHT_YEAR 2010 79 #define MBG_LAST_COPYRIGHT_YEAR 0 // use default 81 static const char *
pname =
"mbgsvcd";
112 vsnprintf( ws,
sizeof( ws ), fmt, ap );
115 syslog( lvl,
"%s", ws );
116 fprintf( stdout,
"%s\n", ws );
135 for ( i = 0, n_devices = 0; i < n_devices_found; i++ )
144 mbg_log( LOG_WARNING,
"Failed to read device info from device #%i.", i );
151 mbg_log( LOG_WARNING,
"Device %s does not support HR time stamps.",
158 devs[n_devices] = dev_info;
164 if ( n_devices == 0 )
166 mbg_log( LOG_WARNING,
"No usable device found!" );
175 mbg_log( LOG_INFO,
"Found %d devices usable for the NTP daemon", n_devices );
183 mbg_log( LOG_INFO,
"%sPC cycles counter clock frequency: %Lu Hz",
184 (
cyc_freq == 0 ) ?
"*** Warning: " :
"",
192 for ( i = 0; i < n_devices; i++ )
211 clock_gettime(CLOCK_MONOTONIC, &ts);
215 mbg_log( LOG_WARNING,
"Device #%i, entering holdover mode", i );
222 mbg_log( LOG_WARNING,
"Device #%i, trust time expired", i );
234 struct shmTime *p = shmTime[i];
288 printf(
"%s, leap: %02X%s\n", ws,
leap, cp );
302 for ( i = 0; i < n_devices; i++ )
315 "This program periodically reads a reference time stamp and an associated\n" 316 "system time stamp from every mbgclock device, and feeds the time stamp pairs\n" 317 "to the NTP daemon's shared memory refclock driver.\n" 318 "It usually runs as daemon but can also be run in the foreground to monitor the\n" 319 "time stamps and offsets.\n" 320 "This works only for cards supporting high resolution time stamps.\n" 328 mbg_print_opt_info(
"-t num",
"set num seconds for refclock trust time, default 4 days (345600 seconds)" );
346 if ( getppid() == 1 )
349 mbg_log( LOG_INFO,
"Daemon mode, backgrounding" );
362 for ( i = getdtablesize(); i >= 0; --i )
366 i = open(
"/dev/null", O_RDWR );
373 lfp = open(
LOCK_FILE, O_RDWR | O_CREAT, 0640 );
378 if ( lockf( lfp, F_TLOCK, 0 ) < 0 )
380 mbg_log( LOG_ERR,
"Lock file already exists, another instance of this daemon seems to be running" );
386 snprintf( str,
sizeof( str ),
"%d\n", getpid() );
387 rc = write( lfp, str, strlen( str ) );
391 signal( SIGCHLD, SIG_IGN );
392 signal( SIGTSTP, SIG_IGN );
393 signal( SIGTTOU, SIG_IGN );
394 signal( SIGTTIN, SIG_IGN );
400 int main(
int argc,
char *argv[] )
408 while ( ( c = getopt( argc, argv,
"fpqrst:n:o:h?" ) ) != -1 )
434 long tt = atol( optarg );
447 mbg_log( LOG_WARNING,
"Configured number of SHM units %i out of range, truncating to %i",
459 mbg_log( LOG_WARNING,
"Configured SHM unit offset %i out of range %i to %i, truncating to %i",
486 mbg_log( LOG_INFO,
"Starting Meinberg Service Daemon %s", ws );
_MBG_API_ATTR int _MBG_API mbg_find_devices(void)
Get the number of devices installed on the computer.
MBG_PC_CYCLES exec_cyc
Computed execution time of the call, in [cycles].
MBG_SYS_TIME_CYCLES sys_time_cycles
system time stamp plus associated cycles
MBG_PC_CYCLES_FREQUENCY cyc_freq
Must be set up tby the application.
#define _pcps_has_hr_time(_d)
Requested action completed successfully.
System time plus associated cycles counter values.
unsigned clockTimeStampNSec
bool mbg_cond_err_msg(int rc, const char *what)
Check if a value is an error code and print an associated error message.
static long int ref_trust_time_start[MAX_SHM_REFCLOCKS]
announced leap second is negative
Unable to handle requested action, usage printed.
#define mbg_rc_is_success(_rc)
int main(int argc, char *argv[])
double d_ref_comp
Compensated reference timestamp in floating point format, [s].
time_t receiveTimeStampSec
static int do_mbgsvctasks(void)
int ntpshm_init(struct shmTime **shmTime, int n_units, int n_unit0)
MBG_TIME_INFO_HRT hrti
System time, device time, and cycles read from the driver.
unsigned receiveTimeStampNSec
A structure to store the results of the mbg_chk_time_info routine.
PCPS_TIME_STATUS_X status
status bits, see PCPS_TIME_STATUS_FLAGS
#define MAX_SHM_UNIT_OFFSET
Max. Number of SHM unit offset.
_MBG_API_ATTR int _MBG_API mbg_get_default_cycles_frequency_from_dev(MBG_DEV_HANDLE dh, MBG_PC_CYCLES_FREQUENCY *p)
Retrieve the system's default cycles counter frequency from the kernel driver.
_MBG_API_ATTR int _MBG_API mbg_get_device_info(MBG_DEV_HANDLE dh, PCPS_DEV *p)
Read detailed device information.
_MBG_API_ATTR MBG_DEV_HANDLE _MBG_API mbg_open_device(int dev_idx)
Open a device by index number.
MBG_PC_CYCLES exec_cyc_limit
Execution time limit, in [cycles], computed based on usual execution time.
MBG_SYS_TIME sys_time
system time stamp
#define MBG_LAST_COPYRIGHT_YEAR
long wave or time code receiver has sync'ed at least once after pwr up, sat receiver is synchronized ...
int leap
see NTP Leap Bits
static long int ref_trust_time_expire[MAX_SHM_REFCLOCKS]
PCPS_HR_TIME_CYCLES ref_hr_time_cycles
HR time read from the card, plus cycles.
_MBG_API_ATTR void _MBG_API mbg_close_device(MBG_DEV_HANDLE *dev_handle)
Close a device handle and set the handle value to MBG_INVALID_DEV_HANDLE.
Structure of NTP's shared memory segment.
void mbg_log(int lvl, const char *fmt,...)
Action failed for specified device.
uint64_t MBG_PC_CYCLES_FREQUENCY
int mbg_chk_time_info(MBG_DEV_HANDLE dh, MBG_CHK_TIME_INFO *p, CYCLES_FILTER_DATA *p_filter, int fast_ts_only)
Read and evaluate a system timestamp / reference timestamp pair.
static void startup_daemon(void)
#define _pcps_type_name(_d)
static int has_synced_after_reset[MAX_SHM_REFCLOCKS]
static const char * pname
#define MBG_FIRST_COPYRIGHT_YEAR
int64_t nano_secs
[nanoseconds]
int64_t secs
[seconds], usually since 1970-01-01 00:00:00
#define _pcps_is_irig_rx(_d)
int snprint_chk_time_info(char *s, size_t max_len, const MBG_CHK_TIME_INFO *p, const PCPS_DEV *p_dev, int frac_digits, int print_raw)
Print info from a MBG_CHK_TIME_INFO structure into a string buffer.
#define MAX_SHM_REFCLOCKS
Number of units (refclocks) supported by the SMH segment.
A structure to keep filter data for cycles values.
long wave or time code receiver running on xtal, satellite receiver has not verified its position ...
static CYCLES_FILTER_DATA filter[MAX_SHM_REFCLOCKS]
static unsigned long trust_time_seconds
leap second announced, for very old clocks see REV_PCPS_LS_ANN_PC31PS31