From 7b7806b85096391e364a03ef3551970620b5492e Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sat, 22 Sep 2012 08:59:54 -0400 Subject: Lose the assumption that socket_t is an integer. --- driver_nmea2000.c | 8 ++++---- driver_zodiac.c | 2 +- gps.h | 2 ++ gpsd.c | 28 ++++++++++++++++------------ lcdgps.c | 2 +- libgps_core.c | 6 +++--- libgpsd_core.c | 6 +++--- net_ntrip.c | 2 +- netlib.c | 1 + ntpshm.c | 2 +- serial.c | 2 +- 11 files changed, 34 insertions(+), 27 deletions(-) diff --git a/driver_nmea2000.c b/driver_nmea2000.c index cea77907..7c2463c2 100644 --- a/driver_nmea2000.c +++ b/driver_nmea2000.c @@ -834,7 +834,7 @@ int nmea2000_open(struct gps_device_t *session) struct sockaddr_can addr; char *unit_ptr; - session->gpsdata.gps_fd = -1; + INVALIDATE_SOCKET(session->gpsdata.gps_fd); session->driver.nmea2000.can_net = 0; can_net = -1; @@ -899,7 +899,7 @@ int nmea2000_open(struct gps_device_t *session) /* Create the socket */ sock = socket(PF_CAN, SOCK_RAW, CAN_RAW); - if (sock == -1) { + if (BAD_SOCKET(sock)) { gpsd_report(LOG_ERROR, "NMEA2000 open: can not get socket.\n"); return -1; } @@ -957,11 +957,11 @@ int nmea2000_open(struct gps_device_t *session) void nmea2000_close(struct gps_device_t *session) { - if (session->gpsdata.gps_fd != -1) { + if (!BAD_SOCKET(session->gpsdata.gps_fd)) { gpsd_report(LOG_SPIN, "close(%d) in nmea2000_close(%s)\n", session->gpsdata.gps_fd, session->gpsdata.dev.path); (void)close(session->gpsdata.gps_fd); - session->gpsdata.gps_fd = -1; + INVALIDATE_SOCKET(session->gpsdata.gps_fd); } } diff --git a/driver_zodiac.c b/driver_zodiac.c index 9a99c663..63ee40e8 100644 --- a/driver_zodiac.c +++ b/driver_zodiac.c @@ -87,7 +87,7 @@ static ssize_t zodiac_spew(struct gps_device_t *session, unsigned short type, h.flags = 0; h.csum = zodiac_checksum((unsigned short *)&h, 4); - if (session->gpsdata.gps_fd != -1) { + if (!BAD_SOCKET(session->gpsdata.gps_fd)) { size_t hlen, datlen; hlen = sizeof(h); datlen = sizeof(unsigned short) * dlen; diff --git a/gps.h b/gps.h index f8552f1d..aaceaca3 100644 --- a/gps.h +++ b/gps.h @@ -1776,6 +1776,8 @@ struct policy_t { * binary compatibility. */ typedef int socket_t; +#define BAD_SOCKET(s) ((s) == -1) +#define INVALIDATE_SOCKET(s) s = -1 /* mode flags for setting streaming policy */ #define WATCH_ENABLE 0x000001u /* enable streaming */ diff --git a/gpsd.c b/gpsd.c index 2ad6fb8d..bc9c89c6 100644 --- a/gpsd.c +++ b/gpsd.c @@ -292,7 +292,7 @@ static socket_t filesock(char *filename) socket_t sock; /*@ -mayaliasunique -usedef @*/ - if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + if (BAD_SOCKET(sock = socket(AF_UNIX, SOCK_STREAM, 0))) { gpsd_report(LOG_ERROR, "Can't create device-control socket\n"); return -1; } @@ -368,7 +368,7 @@ static void adjust_max_fd(int fd, bool on) static socket_t passivesock_af(int af, char *service, char *tcp_or_udp, int qlen) /* bind a passive command socket for the daemon */ { - volatile socket_t s = -1; /* why gcc warned about this I don't know */ + volatile socket_t s; /* * af = address family, * service = IANA protocol name or number. @@ -383,6 +383,7 @@ static socket_t passivesock_af(int af, char *service, char *tcp_or_udp, int qlen in_port_t port; char *af_str = ""; + INVALIDATE_SOCKET(s); if ((pse = getservbyname(service, tcp_or_udp))) port = ntohs((in_port_t) pse->s_port); else if ((port = (in_port_t) atoi(service)) == 0) { @@ -464,7 +465,7 @@ static socket_t passivesock_af(int af, char *service, char *tcp_or_udp, int qlen } gpsd_report(LOG_IO, "opening %s socket\n", af_str); - if (s == -1) { + if (BAD_SOCKET(s)) { gpsd_report(LOG_ERROR, "can't create %s socket\n", af_str); return -1; } @@ -502,7 +503,7 @@ static int passivesocks(char *service, char *tcp_or_udp, int i; for (i = 0; i < AFCOUNT; i++) - socks[i] = -1; + INVALIDATE_SOCKET(socks[i]); #if defined(SYSTEMD_ENABLE) if (sd_socket_count > 0) { @@ -670,7 +671,7 @@ static void deactivate_device(struct gps_device_t *device) "{\"class\":\"DEVICE\",\"path\":\"%s\",\"activated\":0}\r\n", device->gpsdata.dev.path); #endif /* SOCKET_EXPORT_ENABLE */ - if (device->gpsdata.gps_fd != -1) { + if (!BAD_SOCKET(device->gpsdata.gps_fd)) { FD_CLR(device->gpsdata.gps_fd, &all_fds); adjust_max_fd(device->gpsdata.gps_fd, false); #if defined(PPS_ENABLE) && defined(TIOCMIWAIT) @@ -926,7 +927,7 @@ static bool awaken(struct gps_device_t *device) } } - if (device->gpsdata.gps_fd != -1) { + if (!BAD_SOCKET(device->gpsdata.gps_fd)) { gpsd_report(LOG_PROG, "device %d (fd=%d, path %s) already active.\n", (int)(device - devices), @@ -1435,7 +1436,7 @@ static void consume_packets(struct gps_device_t *device) && device->ntrip.conn_state != ntrip_conn_established) { /* the socket descriptor might change during connection */ - if (device->gpsdata.gps_fd != -1) { + if (!BAD_SOCKET(device->gpsdata.gps_fd)) { FD_CLR(device->gpsdata.gps_fd, &all_fds); } (void)ntrip_open(device, ""); @@ -1830,7 +1831,7 @@ int main(int argc, char *argv[]) struct subscriber_t *sub; #endif /* SOCKET_EXPORT_ENABLE */ #ifdef CONTROL_SOCKET_ENABLE - static socket_t csock = -1; + static socket_t csock; fd_set control_fds; socket_t cfd; static char *control_socket = NULL; @@ -1855,6 +1856,9 @@ int main(int argc, char *argv[]) context.debug = 0; gps_context_init(&context); +#ifdef CONTROL_SOCKET_ENABLE + INVALIDATE_SOCKET(csock); +#endif /* CONTROL_SOCKET_ENABLE */ #ifdef PPS_ENABLE /*@-nullpass@*/ (void)pthread_mutex_init(&report_mutex, NULL); @@ -1970,7 +1974,7 @@ int main(int argc, char *argv[]) #ifdef CONTROL_SOCKET_ENABLE if (control_socket) { (void)unlink(control_socket); - if ((csock = filesock(control_socket)) == -1) { + if (BAD_SOCKET(csock = filesock(control_socket))) { gpsd_report(LOG_ERROR, "control socket create failed, netlib error %d\n", csock); @@ -2274,7 +2278,7 @@ int main(int argc, char *argv[]) accept(msocks[i], (struct sockaddr *)&fsin, &alen); /*@+matchanyintegral@*/ - if (ssock == -1) + if (BAD_SOCKET(ssock)) gpsd_report(LOG_ERROR, "accept: %s\n", strerror(errno)); else { struct subscriber_t *client = NULL; @@ -2325,7 +2329,7 @@ int main(int argc, char *argv[]) socket_t ssock = accept(csock, (struct sockaddr *)&fsin, &alen); /*@-matchanyintegral@*/ - if (ssock == -1) + if (BAD_SOCKET(ssock)) gpsd_report(LOG_ERROR, "accept: %s\n", strerror(errno)); else { gpsd_report(LOG_INF, "control socket connect on fd %d\n", @@ -2477,7 +2481,7 @@ int main(int argc, char *argv[]) } } - if (device_needed && device->gpsdata.gps_fd == -1 && + if (device_needed && BAD_SOCKET(device->gpsdata.gps_fd) && (device->opentime == 0 || timestamp() - device->opentime > DEVICE_RECONNECT)) { device->opentime = timestamp(); diff --git a/lcdgps.c b/lcdgps.c index 9ba21900..fde243c2 100644 --- a/lcdgps.c +++ b/lcdgps.c @@ -375,7 +375,7 @@ int main(int argc, char *argv[]) /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); - if(sd == -1) { + if(BAD_SOCKET(sd)) { perror("cannot open socket "); exit(EXIT_FAILURE); } diff --git a/libgps_core.c b/libgps_core.c index 3f91ee68..855154fd 100644 --- a/libgps_core.c +++ b/libgps_core.c @@ -110,7 +110,7 @@ int gps_close(struct gps_data_t *gpsdata) libgps_debug_trace((DEBUG_CALLS, "gps_close()\n")); #ifdef SHM_EXPORT_ENABLE - if ((intptr_t)(gpsdata->gps_fd) == -1) { + if (BAD_SOCKET((intptr_t)(gpsdata->gps_fd))) { gps_shm_close(gpsdata); status = 0; } @@ -134,13 +134,13 @@ int gps_read(struct gps_data_t *gpsdata) /*@ -usedef -compdef -uniondef @*/ #ifdef SHM_EXPORT_ENABLE - if ((intptr_t)(gpsdata->gps_fd) == -1) { + if (BAD_SOCKET((intptr_t)(gpsdata->gps_fd))) { status = gps_shm_read(gpsdata); } #endif /* SHM_EXPORT_ENABLE */ #ifdef SOCKET_EXPORT_ENABLE - if (status == -1 && (intptr_t)(gpsdata->gps_fd) != -1) { + if (status == -1 && BAD_SOCKET((intptr_t)(gpsdata->gps_fd))) { status = gps_sock_read(gpsdata); } #endif /* SOCKET_EXPORT_ENABLE */ diff --git a/libgpsd_core.c b/libgpsd_core.c index 2298d35f..abab142e 100644 --- a/libgpsd_core.c +++ b/libgpsd_core.c @@ -237,7 +237,7 @@ int gpsd_open(struct gps_device_t *session) char server[strlen(session->gpsdata.dev.path)], *port; socket_t dsock; (void)strlcpy(server, session->gpsdata.dev.path + 6, sizeof(server)); - session->gpsdata.gps_fd = -1; + INVALIDATE_SOCKET(session->gpsdata.gps_fd); port = strchr(server, ':'); if (port == NULL) { gpsd_report(LOG_ERROR, "Missing colon in TCP feed spec.\n"); @@ -286,7 +286,7 @@ int gpsd_open(struct gps_device_t *session) char server[strlen(session->gpsdata.dev.path)], *port; socket_t dsock; (void)strlcpy(server, session->gpsdata.dev.path + 7, sizeof(server)); - session->gpsdata.gps_fd = -1; + INVALIDATE_SOCKET(session->gpsdata.gps_fd); if ((port = strchr(server, ':')) == NULL) { port = DEFAULT_GPSD_PORT; } else @@ -1116,7 +1116,7 @@ gps_mask_t gpsd_poll(struct gps_device_t *session) void gpsd_wrap(struct gps_device_t *session) /* end-of-session wrapup */ { - if (session->gpsdata.gps_fd != -1) + if (!BAD_SOCKET(session->gpsdata.gps_fd)) gpsd_deactivate(session); } diff --git a/net_ntrip.c b/net_ntrip.c index 5f1f7e53..dd21f139 100644 --- a/net_ntrip.c +++ b/net_ntrip.c @@ -359,7 +359,7 @@ static int ntrip_stream_get_req(const struct ntrip_stream_t *stream) char buf[BUFSIZ]; dsock = netlib_connectsock(AF_UNSPEC, stream->url, stream->port, "tcp"); - if (dsock == -1) { + if (BAD_SOCKET(dsock)) { gpsd_report(LOG_ERROR, "ntrip stream connect error %d\n", dsock); return -1; } diff --git a/netlib.c b/netlib.c index e09406e2..5adc9a89 100644 --- a/netlib.c +++ b/netlib.c @@ -33,6 +33,7 @@ socket_t netlib_connectsock(int af, const char *host, const char *service, socket_t s = -1; bool bind_me; + INVALIDATE_SOCKET(s); /*@-type@*/ ppe = getprotobyname(protocol); if (strcmp(protocol, "udp") == 0) { diff --git a/ntpshm.c b/ntpshm.c index 6077a477..501099d8 100644 --- a/ntpshm.c +++ b/ntpshm.c @@ -574,7 +574,7 @@ static /*@null@*/ void *gpsd_ppsmonitor(void *arg) gpsd_report(LOG_PROG, "PPS Create Thread gpsd_ppsmonitor\n"); /* wait for the device to go active - makes this safe to call early */ - while (session->gpsdata.gps_fd == -1) { + while (BAD_SOCKET(session->gpsdata.gps_fd)) { /* should probably remove this once code is verified */ gpsd_report(LOG_PROG, "PPS thread awaiting device activation\n"); (void)sleep(1); diff --git a/serial.c b/serial.c index 5f8ea346..585e0d9d 100644 --- a/serial.c +++ b/serial.c @@ -605,7 +605,7 @@ void gpsd_assert_sync(struct gps_device_t *session) void gpsd_close(struct gps_device_t *session) { - if (session->gpsdata.gps_fd != -1) { + if (!BAD_SOCKET(session->gpsdata.gps_fd)) { (void)ioctl(session->gpsdata.gps_fd, (unsigned long)TIOCNXCL); (void)tcdrain(session->gpsdata.gps_fd); if (isatty(session->gpsdata.gps_fd) != 0) { -- cgit v1.2.1