refclock: convert SOCK to new socket API

This commit is contained in:
Miroslav Lichvar 2019-07-17 11:26:28 +02:00
parent f06c1cfa97
commit 207f9fb128

View file

@ -33,6 +33,7 @@
#include "logging.h" #include "logging.h"
#include "util.h" #include "util.h"
#include "sched.h" #include "sched.h"
#include "socket.h"
#define SOCK_MAGIC 0x534f434b #define SOCK_MAGIC 0x534f434b
@ -97,7 +98,6 @@ static void read_sample(int sockfd, int event, void *anything)
static int sock_initialise(RCL_Instance instance) static int sock_initialise(RCL_Instance instance)
{ {
struct sockaddr_un s;
int sockfd; int sockfd;
char *path; char *path;
@ -105,25 +105,9 @@ static int sock_initialise(RCL_Instance instance)
path = RCL_GetDriverParameter(instance); path = RCL_GetDriverParameter(instance);
s.sun_family = AF_UNIX; sockfd = SCK_OpenUnixDatagramSocket(NULL, path, 0);
if (snprintf(s.sun_path, sizeof (s.sun_path), "%s", path) >= sizeof (s.sun_path)) { if (sockfd < 0)
LOG_FATAL("Path %s too long", path); LOG_FATAL("Could not open socket %s", path);
return 0;
}
sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sockfd < 0) {
LOG_FATAL("socket() failed");
return 0;
}
UTI_FdSetCloexec(sockfd);
unlink(path);
if (bind(sockfd, (struct sockaddr *)&s, sizeof (s)) < 0) {
LOG_FATAL("bind(%s) failed : %s", path, strerror(errno));
return 0;
}
RCL_SetDriverData(instance, (void *)(long)sockfd); RCL_SetDriverData(instance, (void *)(long)sockfd);
SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance); SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance);
@ -136,7 +120,7 @@ static void sock_finalise(RCL_Instance instance)
sockfd = (long)RCL_GetDriverData(instance); sockfd = (long)RCL_GetDriverData(instance);
SCH_RemoveFileHandler(sockfd); SCH_RemoveFileHandler(sockfd);
close(sockfd); SCK_CloseSocket(sockfd);
} }
RefclockDriver RCL_SOCK_driver = { RefclockDriver RCL_SOCK_driver = {