mbgtools-lx  4.2.8
ntp_shm.c
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: ntp_shm.c 1.2 2017/07/05 16:49:00 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * NTP shared memory support functions.
10  *
11  * -----------------------------------------------------------------------
12  * $Log: ntp_shm.c $
13  * Revision 1.2 2017/07/05 16:49:00 martin
14  * Patch submitted by <juergen.perlinger@t-online.de>:
15  * Support number of the first SHM unit to use.
16  * This intentionally changes the prototype for ntpshm_init().
17  * Revision 1.1 2012/05/29 09:54:15 martin
18  * Initial revision.
19  *
20  **************************************************************************/
21 
22 #define _NTP_SHM
23  #include <ntp_shm.h>
24 #undef _NTP_SHM
25 
26 #include <syslog.h>
27 #include <errno.h>
28 #include <string.h>
29 
30 
31 
32 /*HDR*/
33 struct shmTime *getShmTime( int unit )
34 {
35  struct shmTime *p;
36 
37  int shmid = shmget( (key_t) ( NTPD_BASE + unit ),
38  sizeof( struct shmTime ), IPC_CREAT | 0644 );
39 
40  if ( shmid == -1 )
41  {
42  syslog( LOG_ERR, "shmget %i failed: %s", unit, strerror( errno ) );
43  return NULL;
44  }
45 
46 
47  p = (struct shmTime *) shmat( shmid, 0, 0 );
48 
49  if ( (long) p == -1L )
50  {
51  syslog( LOG_ERR, "shmat %i failed: %s", unit, strerror( errno ) );
52  return NULL;
53  }
54 
55  return p;
56 
57 } // getShmTime
58 
59 
60 
61 /*HDR*/
62 int ntpshm_init( struct shmTime **shmTime, int n_units, int n_unit0 )
63 {
64  int i;
65  int ret_val = 0;
66 
67  for ( i = 0; i < n_units; i++ )
68  {
69  int u = i + n_unit0;
70  struct shmTime *p = getShmTime( u );
71  shmTime[i] = p;
72 
73  if ( p == NULL )
74  {
75  syslog( LOG_WARNING, "** Failed to initialize NTP SHM unit %i", u );
76  ret_val = -1;
77  continue;
78  }
79 
80  memset( p, 0, sizeof( *p ) );
81 
82  p->mode = 1;
83  p->precision = -5; /* initially 0.5 sec */
84  p->nsamples = 3; /* stages of median filter */
85 
86  syslog( LOG_INFO, "NTP SHM unit %i initialized successfully", u );
87  }
88 
89  return ret_val;
90 
91 } // ntpshm_init
92 
93 
static int n_units
Definition: mbgsvcd.c:90
struct shmTime * getShmTime(int unit)
Definition: ntp_shm.c:33
static int n_unit0
Definition: mbgsvcd.c:89
int precision
Definition: ntp_shm.h:80
int ntpshm_init(struct shmTime **shmTime, int n_units, int n_unit0)
Definition: ntp_shm.c:62
Structure of NTP&#39;s shared memory segment.
Definition: ntp_shm.h:63
int mode
Definition: ntp_shm.h:65
#define NTPD_BASE
Basic SHM unit identifier (unit 0)
Definition: ntp_shm.h:118
int nsamples
Definition: ntp_shm.h:81