summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Tatarinov <kukabu@gmail.com>2011-06-27 11:33:23 +0400
committerEric S. Raymond <esr@thyrsus.com>2011-06-27 03:38:52 -0400
commitd590e2186b4cf573cd24cb9f0efaa5cb12c98b03 (patch)
treef9b6679760578b5c42e423c8c7bb00bd633fb1b4
parent1dcbdc6955748a97611b59bf8e1d3a49f18fdb83 (diff)
downloadgpsd-d590e2186b4cf573cd24cb9f0efaa5cb12c98b03.tar.gz
Removed duplicate code.
Signed-off-by: Eric S. Raymond <esr@thyrsus.com>
-rw-r--r--gpsd.h-tail2
-rw-r--r--gpsdctl.c7
-rw-r--r--netlib.c4
-rw-r--r--ntpshm.c42
4 files changed, 25 insertions, 30 deletions
diff --git a/gpsd.h-tail b/gpsd.h-tail
index 62366288..621aa08f 100644
--- a/gpsd.h-tail
+++ b/gpsd.h-tail
@@ -680,7 +680,7 @@ extern void gpsd_position_fix_dump(struct gps_device_t *,
/*@out@*/char[], size_t);
extern void gpsd_clear_data(struct gps_device_t *);
extern socket_t netlib_connectsock(int, const char *, const char *, const char *);
-extern socket_t netlib_localsocket(const char *);
+extern socket_t netlib_localsocket(const char *, int);
extern char /*@observer@*/ *netlib_errstr(const int);
extern char /*@observer@*/ *netlib_sock2ip(int);
diff --git a/gpsdctl.c b/gpsdctl.c
index 10ce96df..1c9a0473 100644
--- a/gpsdctl.c
+++ b/gpsdctl.c
@@ -14,8 +14,9 @@
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
+#include <sys/socket.h>
-#include "gpsd.h" /* only for the strlcpy() prototype */
+#include "gpsd.h"
static char *control_socket = "/var/run/gpsd.sock";
static char *gpsd_options = "";
@@ -31,7 +32,7 @@ static int gpsd_control(char *action, char *argument)
(void)syslog(LOG_ERR, "socket %s doesn't exist", control_socket);
return -1;
}
- connect = netlib_localsocket(control_socket);
+ connect = netlib_localsocket(control_socket, SOCK_STREAM);
if (connect >= 0)
syslog(LOG_INFO, "reached a running gpsd");
else if (strcmp(action, "add") == 0) {
@@ -42,7 +43,7 @@ static int gpsd_control(char *action, char *argument)
(void)syslog(LOG_ERR, "launch of gpsd failed");
return -1;
}
- connect = netlib_localsocket(control_socket);
+ connect = netlib_localsocket(control_socket, SOCK_STREAM);
}
if (connect < 0) {
syslog(LOG_ERR, "can't reach gpsd");
diff --git a/netlib.c b/netlib.c
index d5ca89fe..d83d6a63 100644
--- a/netlib.c
+++ b/netlib.c
@@ -147,12 +147,12 @@ char /*@observer@*/ *netlib_errstr(const int err)
}
}
-socket_t netlib_localsocket(const char *sockfile)
+socket_t netlib_localsocket(const char *sockfile, int socktype)
/* acquire a connection to an existing Unix-domain socket */
{
int sock;
- if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ if ((sock = socket(AF_UNIX, socktype, 0)) < 0) {
return -1;
} else {
struct sockaddr_un saddr;
diff --git a/ntpshm.c b/ntpshm.c
index 76697c2e..d6283c15 100644
--- a/ntpshm.c
+++ b/ntpshm.c
@@ -555,44 +555,38 @@ static /*@null@*/ void *gpsd_ppsmonitor(void *arg)
} sample;
/* chrony must be started first as chrony insists on creating the socket */
/* open the chrony socket */
- struct sockaddr_un s;
int chronyfd;
- char *chrony_path = NULL;
+ char chrony_path[PATH_MAX];
gpsd_report(LOG_PROG, "PPS Create Thread gpsd_ppsmonitor\n");
+ /* TODO the socket for root would be
+ * /var/run/chrony.ttyXX.sock
+ * for now it is always
+ * /tmp/chrony.ttyXX.sock
+ */
#ifdef __UNUSED__
/* sadly root was dropped very early, until a way if found to run this
* before dropping root it will not work. */
if( 0 == getuid() ) {
/* only root can use /var/run */
- chrony_path = "/var/run/chrony";
+ (void)snprintf(chrony_path, sizeof (chrony_path),
+ "/var/run/chrony.%s.sock", basename(session->gpsdata.dev.path));
} else
#endif
{
- chrony_path = "/tmp/chrony";
+ (void)snprintf(chrony_path, sizeof (chrony_path),
+ "/tmp/chrony.%s.sock", basename(session->gpsdata.dev.path));
}
- s.sun_family = AF_UNIX;
- (void)snprintf(s.sun_path, sizeof (s.sun_path), "%s.%s.sock", chrony_path,
- basename(session->gpsdata.dev.path));
- /* TODO the socket for root would be
- * /var/run/chrony.ttyXX.sock
- * for now it is always
- * /tmp/chrony.ttyXX.sock
- */
-
- /* root privs are required for this device open */
- chronyfd = socket(AF_UNIX, SOCK_DGRAM, 0);
- if (chronyfd < 0) {
- gpsd_report(LOG_PROG, "PPS can not open chrony socket: %s\n",
- s.sun_path);
- } else if (connect(chronyfd, (struct sockaddr *)&s, (int)sizeof(s))) {
- (void)close(chronyfd);
- chronyfd = -1;
- gpsd_report(LOG_PROG, "PPS can not connect chrony socket: %s\n",
- s.sun_path);
+ if (access(chrony_path, F_OK) != 0) {
+ gpsd_report(LOG_PROG, "PPS chrony socket %s doesn't exist", chrony_path);
} else {
- gpsd_report(LOG_RAW, "PPS using chrony socket: %s\n", s.sun_path);
+ chronyfd = netlib_localsocket(chrony_path, SOCK_DGRAM);
+ if (chronyfd < 0)
+ gpsd_report(LOG_PROG, "PPS can not connect chrony socket: %s\n",
+ chrony_path);
+ else
+ gpsd_report(LOG_RAW, "PPS using chrony socket: %s\n", chrony_path);
}
/* end chrony */