static int sock_initialise(RCL_Instance instance) { struct sockaddr_un s; int sockfd; char *path; path = RCL_GetDriverParameter(instance); s.sun_family = AF_UNIX; if (snprintf(s.sun_path, sizeof (s.sun_path), "%s", path) >= sizeof (s.sun_path)) { LOG_FATAL(LOGF_Refclock, "path %s is too long", path); return 0; } sockfd = socket(AF_UNIX, SOCK_DGRAM, 0); if (sockfd < 0) { LOG_FATAL(LOGF_Refclock, "socket() failed"); return 0; } UTI_FdSetCloexec(sockfd); unlink(path); if (bind(sockfd, (struct sockaddr *)&s, sizeof (s)) < 0) { LOG_FATAL(LOGF_Refclock, "bind() failed"); return 0; } RCL_SetDriverData(instance, (void *)(long)sockfd); SCH_AddFileHandler(sockfd, SCH_FILE_INPUT, read_sample, instance); return 1; }
int RTC_Linux_Initialise(void) { rtc_sec = MallocArray(time_t, MAX_SAMPLES); rtc_trim = MallocArray(double, MAX_SAMPLES); system_times = MallocArray(struct timeval, MAX_SAMPLES); /* Setup details depending on configuration options */ setup_config(); /* In case it didn't get done by pre-init */ coefs_file_name = CNF_GetRtcFile(); /* Try to open device */ fd = open (CNF_GetRtcDevice(), O_RDWR); if (fd < 0) { LOG(LOGS_ERR, LOGF_RtcLinux, "Could not open RTC device %s : %s", CNF_GetRtcDevice(), strerror(errno)); return 0; } /* Close on exec */ UTI_FdSetCloexec(fd); n_samples = 0; n_samples_since_regression = 0; n_runs = 0; coefs_valid = 0; measurement_period = LOWEST_MEASUREMENT_PERIOD; operating_mode = OM_NORMAL; /* Register file handler */ SCH_AddInputFileHandler(fd, read_from_device, NULL); /* Register slew handler */ LCL_AddParameterChangeHandler(slew_samples, NULL); logfileid = CNF_GetLogRtc() ? LOG_FileOpen("rtc", " Date (UTC) Time RTC fast (s) Val Est fast (s) Slope (ppm) Ns Nr Meas") : -1; return 1; }
static int prepare_socket(int family, int port_number) { int sock_fd; socklen_t my_addr_len; union sockaddr_all my_addr; IPAddr bind_address; int on_off = 1; sock_fd = socket(family, SOCK_DGRAM, 0); if (sock_fd < 0) { LOG(LOGS_ERR, LOGF_CmdMon, "Could not open %s command socket : %s", UTI_SockaddrFamilyToString(family), strerror(errno)); return -1; } /* Close on exec */ UTI_FdSetCloexec(sock_fd); if (family != AF_UNIX) { /* Allow reuse of port number */ if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on_off, sizeof(on_off)) < 0) { LOG(LOGS_ERR, LOGF_CmdMon, "Could not set reuseaddr socket options"); /* Don't quit - we might survive anyway */ } #ifdef IP_FREEBIND /* Allow binding to address that doesn't exist yet */ if (setsockopt(sock_fd, IPPROTO_IP, IP_FREEBIND, (char *)&on_off, sizeof(on_off)) < 0) { LOG(LOGS_ERR, LOGF_CmdMon, "Could not set free bind socket option"); } #endif #ifdef FEAT_IPV6 if (family == AF_INET6) { #ifdef IPV6_V6ONLY /* Receive IPv6 packets only */ if (setsockopt(sock_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&on_off, sizeof(on_off)) < 0) { LOG(LOGS_ERR, LOGF_CmdMon, "Could not request IPV6_V6ONLY socket option"); } #endif } #endif } memset(&my_addr, 0, sizeof (my_addr)); switch (family) { case AF_INET: my_addr_len = sizeof (my_addr.in4); my_addr.in4.sin_family = family; my_addr.in4.sin_port = htons((unsigned short)port_number); CNF_GetBindCommandAddress(IPADDR_INET4, &bind_address); if (bind_address.family == IPADDR_INET4) my_addr.in4.sin_addr.s_addr = htonl(bind_address.addr.in4); else my_addr.in4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); break; #ifdef FEAT_IPV6 case AF_INET6: my_addr_len = sizeof (my_addr.in6); my_addr.in6.sin6_family = family; my_addr.in6.sin6_port = htons((unsigned short)port_number); CNF_GetBindCommandAddress(IPADDR_INET6, &bind_address); if (bind_address.family == IPADDR_INET6) memcpy(my_addr.in6.sin6_addr.s6_addr, bind_address.addr.in6, sizeof (my_addr.in6.sin6_addr.s6_addr)); else my_addr.in6.sin6_addr = in6addr_loopback; break; #endif case AF_UNIX: my_addr_len = sizeof (my_addr.un); my_addr.un.sun_family = family; if (snprintf(my_addr.un.sun_path, sizeof (my_addr.un.sun_path), "%s", CNF_GetBindCommandPath()) >= sizeof (my_addr.un.sun_path)) LOG_FATAL(LOGF_CmdMon, "Unix socket path too long"); unlink(my_addr.un.sun_path); break; default: assert(0); } if (bind(sock_fd, &my_addr.sa, my_addr_len) < 0) { LOG(LOGS_ERR, LOGF_CmdMon, "Could not bind %s command socket : %s", UTI_SockaddrFamilyToString(family), strerror(errno)); close(sock_fd); return -1; } /* Register handler for read events on the socket */ SCH_AddFileHandler(sock_fd, SCH_FILE_INPUT, read_from_cmd_socket, NULL); return sock_fd; }