Blame refclock_sock.c

Packit 96c956
/*
Packit 96c956
  chronyd/chronyc - Programs for keeping computer clocks accurate.
Packit 96c956
Packit 96c956
 **********************************************************************
Packit 96c956
 * Copyright (C) Miroslav Lichvar  2009
Packit 96c956
 * 
Packit 96c956
 * This program is free software; you can redistribute it and/or modify
Packit 96c956
 * it under the terms of version 2 of the GNU General Public License as
Packit 96c956
 * published by the Free Software Foundation.
Packit 96c956
 * 
Packit 96c956
 * This program is distributed in the hope that it will be useful, but
Packit 96c956
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 96c956
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 96c956
 * General Public License for more details.
Packit 96c956
 * 
Packit 96c956
 * You should have received a copy of the GNU General Public License along
Packit 96c956
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 96c956
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit 96c956
 * 
Packit 96c956
 **********************************************************************
Packit 96c956
Packit 96c956
  =======================================================================
Packit 96c956
Packit 96c956
  Unix domain socket refclock driver.
Packit 96c956
Packit 96c956
  */
Packit 96c956
Packit 96c956
#include "config.h"
Packit 96c956
Packit 96c956
#include "sysincl.h"
Packit 96c956
Packit 96c956
#include "refclock.h"
Packit 96c956
#include "logging.h"
Packit 96c956
#include "util.h"
Packit 96c956
#include "sched.h"
Packit 96c956
Packit 96c956
#define SOCK_MAGIC 0x534f434b
Packit 96c956
Packit 96c956
struct sock_sample {
Packit 96c956
  /* Time of the measurement (system time) */
Packit 96c956
  struct timeval tv;
Packit 96c956
Packit 96c956
  /* Offset between the true time and the system time (in seconds) */
Packit 96c956
  double offset;
Packit 96c956
Packit 96c956
  /* Non-zero if the sample is from a PPS signal, i.e. another source
Packit 96c956
     is needed to obtain seconds */
Packit 96c956
  int pulse;
Packit 96c956
Packit 96c956
  /* 0 - normal, 1 - insert leap second, 2 - delete leap second */
Packit 96c956
  int leap;
Packit 96c956
Packit 96c956
  /* Padding, ignored */
Packit 96c956
  int _pad;
Packit 96c956
Packit 96c956
  /* Protocol identifier (0x534f434b) */
Packit 96c956
  int magic;
Packit 96c956
};
Packit 96c956
Packit 96c956
static void read_sample(int sockfd, int event, void *anything)
Packit 96c956
{
Packit 96c956
  struct sock_sample sample;
Packit 96c956
  struct timespec ts;
Packit 96c956
  RCL_Instance instance;
Packit 96c956
  int s;
Packit 96c956
Packit 96c956
  instance = (RCL_Instance)anything;
Packit 96c956
Packit 96c956
  s = recv(sockfd, &sample, sizeof (sample), 0);
Packit 96c956
Packit 96c956
  if (s < 0) {
Packit 96c956
    DEBUG_LOG("Could not read SOCK sample : %s", strerror(errno));
Packit 96c956
    return;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  if (s != sizeof (sample)) {
Packit 96c956
    DEBUG_LOG("Unexpected length of SOCK sample : %d != %ld",
Packit 96c956
              s, (long)sizeof (sample));
Packit 96c956
    return;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  if (sample.magic != SOCK_MAGIC) {
Packit 96c956
    DEBUG_LOG("Unexpected magic number in SOCK sample : %x != %x",
Packit 96c956
              (unsigned int)sample.magic, (unsigned int)SOCK_MAGIC);
Packit 96c956
    return;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  UTI_TimevalToTimespec(&sample.tv, &ts);
Packit 96c956
  UTI_NormaliseTimespec(&ts);
Packit 96c956
Packit 96c956
  if (sample.pulse) {
Packit 96c956
    RCL_AddPulse(instance, &ts, sample.offset);
Packit 96c956
  } else {
Packit 96c956
    RCL_AddSample(instance, &ts, sample.offset, sample.leap);
Packit 96c956
  }
Packit 96c956
}
Packit 96c956
Packit 96c956
static int sock_initialise(RCL_Instance instance)
Packit 96c956
{
Packit 96c956
  struct sockaddr_un s;
Packit 96c956
  int sockfd;
Packit 96c956
  char *path;
Packit 96c956
Packit 96c956
  RCL_CheckDriverOptions(instance, NULL);
Packit 96c956
Packit 96c956
  path = RCL_GetDriverParameter(instance);
Packit 96c956
 
Packit 96c956
  s.sun_family = AF_UNIX;
Packit 96c956
  if (snprintf(s.sun_path, sizeof (s.sun_path), "%s", path) >= sizeof (s.sun_path)) {
Packit 96c956
    LOG_FATAL("Path %s too long", path);
Packit 96c956
    return 0;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
Packit 96c956
  if (sockfd < 0) {
Packit 96c956
    LOG_FATAL("socket() failed");
Packit 96c956
    return 0;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  UTI_FdSetCloexec(sockfd);
Packit 96c956
Packit 96c956
  unlink(path);
Packit 96c956
  if (bind(sockfd, (struct sockaddr *)&s, sizeof (s)) < 0) {
Packit 96c956
    LOG_FATAL("bind(%s) failed : %s", path, strerror(errno));
Packit 96c956
    return 0;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  RCL_SetDriverData(instance, (void *)(long)sockfd);
Packit 96c956
  SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance);
Packit 96c956
  return 1;
Packit 96c956
}
Packit 96c956
Packit 96c956
static void sock_finalise(RCL_Instance instance)
Packit 96c956
{
Packit 96c956
  int sockfd;
Packit 96c956
Packit 96c956
  sockfd = (long)RCL_GetDriverData(instance);
Packit 96c956
  SCH_RemoveFileHandler(sockfd);
Packit 96c956
  close(sockfd);
Packit 96c956
}
Packit 96c956
Packit 96c956
RefclockDriver RCL_SOCK_driver = {
Packit 96c956
  sock_initialise,
Packit 96c956
  sock_finalise,
Packit 96c956
  NULL
Packit 96c956
};