1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611 |
- #include "gpsd_config.h"
- #include <arpa/inet.h>
- #include <assert.h>
- #include <ctype.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <grp.h>
- #include <math.h>
- #include <netdb.h>
- #include <pthread.h>
- #include <pwd.h>
- #include <setjmp.h>
- #include <signal.h>
- #include <stdarg.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <syslog.h>
- #include <sys/param.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/un.h>
- #include <time.h>
- #include <unistd.h>
- #ifndef AF_UNSPEC
- #include <sys/socket.h>
- #endif
- #ifndef INADDR_ANY
- #include <netinet/in.h>
- #endif
- #include "gpsd.h"
- #include "gps_json.h"
- #include "revision.h"
- #include "sockaddr.h"
- #include "strfuncs.h"
- #if defined(SYSTEMD_ENABLE)
- #include "sd_socket.h"
- #endif
- #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
- #define PROTO_TTY "/dev/tty00"
- #else
- #define PROTO_TTY "/dev/ttyS0"
- #endif
- #define COMMAND_TIMEOUT 60*15
- #define NOREAD_TIMEOUT 60*3
- #define RELEASE_TIMEOUT 60
- #define DEVICE_REAWAKE 0.01
- #define DEVICE_RECONNECT 2
- #define QLEN 5
- #define NICEVAL -10
- #if (defined(TIMESERVICE_ENABLE) || \
- !defined(SOCKET_EXPORT_ENABLE))
-
- #define FORCE_NOWAIT
- #endif
- #ifdef SOCKET_EXPORT_ENABLE
- static const int af_allowed = AF_UNSPEC;
- #endif
- #define AFCOUNT 2
- static fd_set all_fds;
- static int maxfd;
- static int highwater;
- #ifndef FORCE_GLOBAL_ENABLE
- static bool listen_global = false;
- #endif
- #ifdef FORCE_NOWAIT
- static bool nowait = true;
- #else
- static bool nowait = false;
- #endif
- static bool batteryRTC = false;
- static jmp_buf restartbuf;
- static struct gps_context_t context;
- #if defined(SYSTEMD_ENABLE)
- static int sd_socket_count = 0;
- #endif
- #ifndef IPV6_TCLASS
- # if defined(__GNU__)
- # define IPV6_TCLASS 61
- # elif defined(__APPLE__)
- # define IPV6_TCLASS 36
- # endif
- #endif
- static volatile sig_atomic_t signalled;
- static void onsig(int sig)
- {
-
- signalled = (sig_atomic_t) sig;
- }
- static void typelist(void)
- {
- const struct gps_type_t **dp;
- for (dp = gpsd_drivers; *dp; dp++) {
- if ((*dp)->packet_type == COMMENT_PACKET)
- continue;
- #ifdef RECONFIGURE_ENABLE
- if ((*dp)->mode_switcher != NULL)
- (void)fputs("n\t", stdout);
- else
- (void)fputc('\t', stdout);
- if ((*dp)->speed_switcher != NULL)
- (void)fputs("b\t", stdout);
- else
- (void)fputc('\t', stdout);
- if ((*dp)->rate_switcher != NULL)
- (void)fputs("c\t", stdout);
- else
- (void)fputc('\t', stdout);
- if ((*dp)->packet_type > NMEA_PACKET)
- (void)fputs("*\t", stdout);
- else
- (void)fputc('\t', stdout);
- #endif
- (void)puts((*dp)->type_name);
- }
- (void)printf("# n: mode switch, b: speed switch, "
- "c: rate switch, *: non-NMEA packet type.\n");
- #if defined(SOCKET_EXPORT_ENABLE)
- (void)printf("# Socket export enabled.\n");
- #endif
- #if defined(SHM_EXPORT_ENABLE)
- (void)printf("# Shared memory export enabled.\n");
- #endif
- #if defined(DBUS_EXPORT_ENABLE)
- (void)printf("# DBUS export enabled\n");
- #endif
- exit(EXIT_SUCCESS);
- }
- static void usage(void)
- {
- (void)printf("usage: gpsd [OPTIONS] device...\n\n\
- Options include: \n\
- -b = bluetooth-safe: open data sources read-only\n\
- -D integer (default 0) = set debug level \n\
- -F sockfile = specify control socket location\n\
- -f FRAMING = fix device framing to FRAMING (8N1, 8O1, etc.)\n\
- -G = make gpsd listen on INADDR_ANY\n"
- #ifndef FORCE_GLOBAL_ENABLE
- " forced on in this binary\n"
- #endif
- " -h = help message \n\
- -n = don't wait for client connects to poll GPS\n"
- #ifdef FORCE_NOWAIT
- " forced on in this binary\n"
- #endif
- " -N = don't go into background\n\
- -P pidfile = set file to record process ID\n\
- -r = use GPS time even if no fix\n\
- -S PORT (default %s) = set port for daemon \n\
- -s SPEED = fix device speed to SPEED\n\
- -V = emit version and exit.\n"
- #ifdef NETFEED_ENABLE
- "\nA device may be a local serial device for GNSS input, plus an optional\n\
- PPS device, or a URL in one of the following forms:\n\
- tcp://host[:port]\n\
- udp://host[:port]\n\
- {dgpsip|ntrip}://[user:passwd@]host[:port][/stream]\n\
- gpsd://host[:port][/device][?protocol]\n\
- in which case it specifies an input source for device, DGPS or ntrip data.\n"
- #endif
- "\n\
- The following driver types are compiled into this gpsd instance:\n",
- DEFAULT_GPSD_PORT);
- typelist();
- }
- #ifdef CONTROL_SOCKET_ENABLE
- static socket_t filesock(char *filename)
- {
- struct sockaddr_un addr;
- socket_t sock;
- if (BAD_SOCKET(sock = socket(AF_UNIX, SOCK_STREAM, 0))) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Can't create device-control socket\n");
- return -1;
- }
- (void)strlcpy(addr.sun_path, filename, sizeof(addr.sun_path));
- addr.sun_family = (sa_family_t)AF_UNIX;
- if (bind(sock, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) < 0) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't bind to local socket %s\n", filename);
- (void)close(sock);
- return -1;
- }
- if (listen(sock, QLEN) == -1) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't listen on local socket %s\n", filename);
- (void)close(sock);
- return -1;
- }
-
- return sock;
- }
- #endif
- #define sub_index(s) (int)((s) - subscribers)
- #define allocated_device(devp) ((devp)->gpsdata.dev.path[0] != '\0')
- #define free_device(devp) (devp)->gpsdata.dev.path[0] = '\0'
- #define initialized_device(devp) ((devp)->context != NULL)
- static struct gps_device_t devices[MAX_DEVICES];
- static void adjust_max_fd(int fd, bool on)
- {
- if (on) {
- if (fd > maxfd)
- maxfd = fd;
- } else if (fd == maxfd) {
- int tfd;
- for (maxfd = tfd = 0; tfd < (int)FD_SETSIZE; tfd++)
- if (FD_ISSET(tfd, &all_fds))
- maxfd = tfd;
- }
- }
- #ifdef SOCKET_EXPORT_ENABLE
- #ifndef IPTOS_LOWDELAY
- #define IPTOS_LOWDELAY 0x10
- #endif
- static socket_t passivesock_af(int af, char *service, char *tcp_or_udp, int qlen)
- {
- volatile socket_t s;
-
- struct servent *pse;
- struct protoent *ppe;
- sockaddr_t sat;
- size_t sin_len = 0;
- int type, proto, one = 1;
- in_port_t port;
- char *af_str = "";
- const int dscp = IPTOS_LOWDELAY;
- 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) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't get \"%s\" service entry.\n", service);
- return -1;
- }
- ppe = getprotobyname(tcp_or_udp);
- if (strcmp(tcp_or_udp, "udp") == 0) {
- type = SOCK_DGRAM;
- proto = (ppe) ? ppe->p_proto : IPPROTO_UDP;
- } else {
- type = SOCK_STREAM;
- proto = (ppe) ? ppe->p_proto : IPPROTO_TCP;
- }
- switch (af) {
- case AF_INET:
- sin_len = sizeof(sat.sa_in);
- memset((char *)&sat.sa_in, 0, sin_len);
- sat.sa_in.sin_family = (sa_family_t) AF_INET;
- #ifndef FORCE_GLOBAL_ENABLE
- if (!listen_global)
- sat.sa_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- else
- #endif
- sat.sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
- sat.sa_in.sin_port = htons(port);
- af_str = "IPv4";
-
- s = socket(PF_INET, type, proto);
- if (s > -1 ) {
-
- if (setsockopt(s, IPPROTO_IP, IP_TOS, &dscp,
- (socklen_t)sizeof(dscp)) == -1)
- GPSD_LOG(LOG_WARN, &context.errout,
- "Warning: SETSOCKOPT TOS failed\n");
- }
- break;
- case AF_INET6:
- sin_len = sizeof(sat.sa_in6);
- memset((char *)&sat.sa_in6, 0, sin_len);
- sat.sa_in6.sin6_family = (sa_family_t) AF_INET6;
- #ifndef FORCE_GLOBAL_ENABLE
- if (!listen_global)
- sat.sa_in6.sin6_addr = in6addr_loopback;
- else
- #endif
- sat.sa_in6.sin6_addr = in6addr_any;
- sat.sa_in6.sin6_port = htons(port);
-
- af_str = "IPv6";
- s = socket(PF_INET6, type, proto);
-
- if (s > -1) {
- int on = 1;
- if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on,
- (socklen_t)sizeof(on)) == -1) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Error: SETSOCKOPT IPV6_V6ONLY\n");
- (void)close(s);
- return -1;
- }
- #ifdef IPV6_TCLASS
-
- if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, &dscp,
- (socklen_t)sizeof(dscp)) == -1)
- GPSD_LOG(LOG_WARN, &context.errout,
- "Warning: SETSOCKOPT TOS failed\n");
- #endif
- }
- break;
- default:
- GPSD_LOG(LOG_ERROR, &context.errout,
- "unhandled address family %d\n", af);
- return -1;
- }
- GPSD_LOG(LOG_IO, &context.errout,
- "opening %s socket\n", af_str);
- if (BAD_SOCKET(s)) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't create %s socket\n", af_str);
- return -1;
- }
- if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one,
- (socklen_t)sizeof(one)) == -1) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Error: SETSOCKOPT SO_REUSEADDR\n");
- (void)close(s);
- return -1;
- }
- if (bind(s, &sat.sa, (socklen_t)sin_len) < 0) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't bind to %s port %s, %s\n", af_str,
- service, strerror(errno));
- if (errno == EADDRINUSE) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "maybe gpsd is already running!\n");
- }
- (void)close(s);
- return -1;
- }
- if (type == SOCK_STREAM && listen(s, qlen) == -1) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't listen on port %s\n", service);
- (void)close(s);
- return -1;
- }
- GPSD_LOG(LOG_SPIN, &context.errout, "passivesock_af() -> %d\n", s);
- return s;
- }
- static int passivesocks(char *service, char *tcp_or_udp,
- int qlen, socket_t socks[])
- {
- int numsocks = AFCOUNT;
- int i;
- for (i = 0; i < AFCOUNT; i++)
- INVALIDATE_SOCKET(socks[i]);
- #if defined(SYSTEMD_ENABLE)
- if (sd_socket_count > 0) {
- for (i = 0; i < AFCOUNT && i < sd_socket_count - 1; i++) {
- socks[i] = SD_SOCKET_FDS_START + i + 1;
- }
- return sd_socket_count - 1;
- }
- #endif
- if (AF_UNSPEC == af_allowed || (AF_INET == af_allowed))
- socks[0] = passivesock_af(AF_INET, service, tcp_or_udp, qlen);
- if (AF_UNSPEC == af_allowed || (AF_INET6 == af_allowed))
- socks[1] = passivesock_af(AF_INET6, service, tcp_or_udp, qlen);
- for (i = 0; i < AFCOUNT; i++)
- if (socks[i] < 0)
- numsocks--;
-
- return numsocks;
- }
- struct subscriber_t
- {
- int fd;
- time_t active;
- struct gps_policy_t policy;
- pthread_mutex_t mutex;
- };
- #define subscribed(sub, devp) (sub->policy.watcher && (sub->policy.devpath[0]=='\0' || strcmp(sub->policy.devpath, devp->gpsdata.dev.path)==0))
- static struct subscriber_t subscribers[MAX_CLIENTS];
- static void lock_subscriber(struct subscriber_t *sub)
- {
- (void)pthread_mutex_lock(&sub->mutex);
- }
- static void unlock_subscriber(struct subscriber_t *sub)
- {
- (void)pthread_mutex_unlock(&sub->mutex);
- }
- static struct subscriber_t *allocate_client(void)
- {
- int si;
- #if UNALLOCATED_FD == 0
- #error client allocation code will fail horribly
- #endif
- for (si = 0; si < NITEMS(subscribers); si++) {
- if (subscribers[si].fd == UNALLOCATED_FD) {
- subscribers[si].fd = 0;
- return &subscribers[si];
- }
- }
- return NULL;
- }
- static void detach_client(struct subscriber_t *sub)
- {
- char *c_ip;
- lock_subscriber(sub);
- if (sub->fd == UNALLOCATED_FD) {
- unlock_subscriber(sub);
- return;
- }
- c_ip = netlib_sock2ip(sub->fd);
- (void)shutdown(sub->fd, SHUT_RDWR);
- GPSD_LOG(LOG_SPIN, &context.errout,
- "close(%d) in detach_client()\n",
- sub->fd);
- (void)close(sub->fd);
- GPSD_LOG(LOG_INF, &context.errout,
- "detaching %s (sub %d, fd %d) in detach_client\n",
- c_ip, sub_index(sub), sub->fd);
- FD_CLR(sub->fd, &all_fds);
- adjust_max_fd(sub->fd, false);
- sub->active = 0;
- sub->policy.watcher = false;
- sub->policy.json = false;
- sub->policy.nmea = false;
- sub->policy.raw = 0;
- sub->policy.scaled = false;
- sub->policy.timing = false;
- sub->policy.split24 = false;
- sub->policy.devpath[0] = '\0';
- sub->fd = UNALLOCATED_FD;
- unlock_subscriber(sub);
- }
- static ssize_t throttled_write(struct subscriber_t *sub, char *buf,
- size_t len)
- {
- ssize_t status;
- if (context.errout.debug >= LOG_CLIENT) {
- if (isprint((unsigned char) buf[0]))
- GPSD_LOG(LOG_CLIENT, &context.errout,
- "=> client(%d): %s\n", sub_index(sub), buf);
- else {
- #ifndef __clang_analyzer__
- char *cp, buf2[MAX_PACKET_LENGTH * 3];
- buf2[0] = '\0';
- for (cp = buf; cp < buf + len; cp++)
- str_appendf(buf2, sizeof(buf2),
- "%02x", (unsigned int)(*cp & 0xff));
- GPSD_LOG(LOG_CLIENT, &context.errout,
- "=> client(%d): =%s\n", sub_index(sub), buf2);
- #endif
- }
- }
- gpsd_acquire_reporting_lock();
- status = send(sub->fd, buf, len, 0);
- gpsd_release_reporting_lock();
- if (status == (ssize_t) len)
- return status;
- else if (status > -1) {
- GPSD_LOG(LOG_INF, &context.errout,
- "short write disconnecting client(%d)\n",
- sub_index(sub));
- detach_client(sub);
- return 0;
- } else if (errno == EAGAIN || errno == EINTR)
- return 0;
- else if (errno == EBADF)
- GPSD_LOG(LOG_WARN, &context.errout,
- "client(%d) has vanished.\n", sub_index(sub));
- else if (errno == EWOULDBLOCK
- && time(NULL) - sub->active > NOREAD_TIMEOUT)
- GPSD_LOG(LOG_INF, &context.errout,
- "client(%d) timed out.\n", sub_index(sub));
- else
- GPSD_LOG(LOG_INF, &context.errout,
- "client(%d) write: %s\n",
- sub_index(sub), strerror(errno));
- detach_client(sub);
- return status;
- }
- static void notify_watchers(struct gps_device_t *device,
- bool onjson, bool onpps,
- const char *sentence, ...)
- {
- va_list ap;
- char buf[BUFSIZ];
- struct subscriber_t *sub;
- va_start(ap, sentence);
- (void)vsnprintf(buf, sizeof(buf), sentence, ap);
- va_end(ap);
- for (sub = subscribers; sub < subscribers + MAX_CLIENTS; sub++)
- if (sub->active != 0 && subscribed(sub, device)) {
- if ((onjson && sub->policy.json) || (onpps && sub->policy.pps))
- (void)throttled_write(sub, buf, strlen(buf));
- }
- }
- #endif
- static void deactivate_device(struct gps_device_t *device)
- {
- #ifdef SOCKET_EXPORT_ENABLE
- notify_watchers(device, true, false,
- "{\"class\":\"DEVICE\",\"path\":\"%s\",\"activated\":0}\r\n",
- device->gpsdata.dev.path);
- #endif
- if (!BAD_SOCKET(device->gpsdata.gps_fd)) {
- FD_CLR(device->gpsdata.gps_fd, &all_fds);
- adjust_max_fd(device->gpsdata.gps_fd, false);
- ntpshm_link_deactivate(device);
- gpsd_deactivate(device);
- }
- }
- #if defined(SOCKET_EXPORT_ENABLE) || defined(CONTROL_SOCKET_ENABLE)
- static struct gps_device_t *find_device(const char
- *device_name)
- {
- struct gps_device_t *devp;
- for (devp = devices; devp < devices + MAX_DEVICES; devp++)
- {
- if (allocated_device(devp) && NULL != device_name &&
- strcmp(devp->gpsdata.dev.path, device_name) == 0)
- return devp;
- }
- return NULL;
- }
- #endif
- static bool open_device( struct gps_device_t *device)
- {
- int activated = -1;
- if (NULL == device ) {
- return false;
- }
- activated = gpsd_activate(device, O_OPTIMIZE);
- if ( ( 0 > activated ) && ( PLACEHOLDING_FD != activated ) ) {
-
- return false;
- }
-
- ntpshm_link_activate(device);
- GPSD_LOG(LOG_INF, &context.errout,
- "PPS:%s ntpshm_link_activate: %d\n",
- device->gpsdata.dev.path,
- device->shm_clock != NULL);
- GPSD_LOG(LOG_INF, &context.errout,
- "device %s activated\n", device->gpsdata.dev.path);
- if ( PLACEHOLDING_FD == activated ) {
-
- return true;
- }
- FD_SET(device->gpsdata.gps_fd, &all_fds);
- adjust_max_fd(device->gpsdata.gps_fd, true);
- ++highwater;
- return true;
- }
- bool gpsd_add_device(const char *device_name, bool flag_nowait)
- {
- struct gps_device_t *devp;
- bool ret = false;
- #ifdef SOCKET_EXPORT_ENABLE
- char tbuf[JSON_DATE_MAX+1];
- #endif
-
- if (strlen(device_name) >= GPS_PATH_MAX) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "ignoring device %s: path length exceeds maximum %d\n",
- device_name, GPS_PATH_MAX);
- return false;
- }
-
- for (devp = devices; devp < devices + MAX_DEVICES; devp++)
- if (!allocated_device(devp)) {
- gpsd_init(devp, &context, device_name);
- ntpshm_session_init(devp);
- GPSD_LOG(LOG_INF, &context.errout,
- "stashing device %s at slot %d\n",
- device_name, (int)(devp - devices));
- if (!flag_nowait) {
- devp->gpsdata.gps_fd = UNALLOCATED_FD;
- ret = true;
- } else {
- ret = open_device(devp);
- }
- #ifdef SOCKET_EXPORT_ENABLE
- notify_watchers(devp, true, false,
- "{\"class\":\"DEVICE\",\"path\":\"%s\","
- "\"activated\":%s}\r\n",
- devp->gpsdata.dev.path,
- now_to_iso8601(tbuf, sizeof(tbuf)));
- #endif
- break;
- }
- return ret;
- }
- static int write_gps(char *device, char *hex) {
- struct gps_device_t *devp;
- size_t len;
- int st;
- if ((devp = find_device(device)) == NULL) {
- GPSD_LOG(LOG_INF, &context.errout, "GPS <=: %s not active\n", device);
- return 1;
- }
- if (devp->context->readonly || (devp->sourcetype <= source_blockdev)) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "GPS <=: attempted to write to a read-only device\n");
- return 1;
- }
- len = strlen(hex);
-
- st = gpsd_hexpack(hex, hex, len);
- if (st <= 0) {
- GPSD_LOG(LOG_INF, &context.errout,
- "GPS <=: invalid hex string (error %d).\n", st);
- return 1;
- }
- GPSD_LOG(LOG_INF, &context.errout,
- "GPS <=: writing %d bytes fromhex(%s) to %s\n",
- st, hex, device);
- if (write(devp->gpsdata.gps_fd, hex, (size_t) st) <= 0) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "GPS <=: write to device failed\n");
- return 1;
- }
- return 0;
- }
- #ifdef CONTROL_SOCKET_ENABLE
- static char *snarfline(char *p, char **out)
- {
- char *q;
- static char stash[BUFSIZ];
- for (q = p;
- isprint((unsigned char) *p) &&
- !isspace((unsigned char) *p) &&
- (p - q < (ssize_t)(sizeof(stash) - 1));
- p++)
- continue;
- (void)memcpy(stash, q, (size_t) (p - q));
- stash[p - q] = '\0';
- *out = stash;
- return p;
- }
- static void handle_control(int sfd, char *buf)
- {
- char *stash;
- struct gps_device_t *devp;
-
- if (buf[0] == '-') {
-
- (void)snarfline(buf + 1, &stash);
- GPSD_LOG(LOG_INF, &context.errout,
- "<= control(%d): removing %s\n", sfd, stash);
- if ((devp = find_device(stash))) {
- deactivate_device(devp);
- free_device(devp);
- ignore_return(write(sfd, "OK\n", 3));
- } else
- ignore_return(write(sfd, "ERROR\n", 6));
- } else if (buf[0] == '+') {
-
- (void)snarfline(buf + 1, &stash);
- if (find_device(stash)) {
- GPSD_LOG(LOG_INF, &context.errout,
- "<= control(%d): %s already active \n", sfd,
- stash);
- ignore_return(write(sfd, "ERROR\n", 6));
- } else {
- GPSD_LOG(LOG_INF, &context.errout,
- "<= control(%d): adding %s\n", sfd, stash);
- if (gpsd_add_device(stash, nowait))
- ignore_return(write(sfd, "OK\n", 3));
- else {
- ignore_return(write(sfd, "ERROR\n", 6));
- GPSD_LOG(LOG_INF, &context.errout,
- "control(%d): adding %s failed, too many "
- "devices active\n",
- sfd, stash);
- }
- }
- } else if (buf[0] == '!') {
-
- char *eq;
- (void)snarfline(buf + 1, &stash);
- eq = strchr(stash, '=');
- if (eq == NULL) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "<= control(%d): ill-formed command \n",
- sfd);
- ignore_return(write(sfd, "ERROR\n", 6));
- } else {
- *eq++ = '\0';
- if ((devp = find_device(stash))) {
- if (devp->context->readonly ||
- (devp->sourcetype <= source_blockdev)) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "<= control(%d): attempted to write to a "
- "read-only device\n",
- sfd);
- ignore_return(write(sfd, "ERROR\n", 6));
- } else {
- GPSD_LOG(LOG_INF, &context.errout,
- "<= control(%d): writing to %s \n", sfd,
- stash);
- if (write(devp->gpsdata.gps_fd, eq, strlen(eq)) <= 0) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "<= control(%d): write to device failed\n",
- sfd);
- ignore_return(write(sfd, "ERROR\n", 6));
- } else {
- ignore_return(write(sfd, "OK\n", 3));
- }
- }
- } else {
- GPSD_LOG(LOG_INF, &context.errout,
- "<= control(%d): %s not active \n", sfd,
- stash);
- ignore_return(write(sfd, "ERROR\n", 6));
- }
- }
- } else if (buf[0] == '&') {
-
- char *eq;
- (void)snarfline(buf + 1, &stash);
- eq = strchr(stash, '=');
- if (eq == NULL) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "<= control(%d): ill-formed command\n",
- sfd);
- ignore_return(write(sfd, "ERROR\n", 6));
- } else {
- *eq++ = '\0';
- if (0 == write_gps(stash, eq)) {
- ignore_return(write(sfd, "OK\n", 3));
- } else {
- ignore_return(write(sfd, "ERROR\n", 6));
- }
- }
- } else if (strstr(buf, "?devices")==buf) {
-
- for (devp = devices; devp < devices + MAX_DEVICES; devp++) {
- char *path = devp->gpsdata.dev.path;
- ignore_return(write(sfd, path, strlen(path)));
- ignore_return(write(sfd, "\n", 1));
- }
- ignore_return(write(sfd, "OK\n", 3));
- } else {
-
- ignore_return(write(sfd, "ERROR\n", 6));
- }
- }
- #endif
- #ifdef SOCKET_EXPORT_ENABLE
- static bool awaken(struct gps_device_t *device)
- {
-
- if (!initialized_device(device)) {
- if (!open_device(device)) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "%s: open failed\n",
- device->gpsdata.dev.path);
- free_device(device);
- return false;
- }
- }
- if (!BAD_SOCKET(device->gpsdata.gps_fd)) {
- GPSD_LOG(LOG_PROG, &context.errout,
- "device %d (fd=%d, path %s) already active.\n",
- (int)(device - devices),
- device->gpsdata.gps_fd, device->gpsdata.dev.path);
- return true;
- } else {
- if (gpsd_activate(device, O_OPTIMIZE) < 0) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "%s: device activation failed.\n",
- device->gpsdata.dev.path);
- GPSD_LOG(LOG_ERROR, &context.errout,
- "%s: activation failed, freeing device\n",
- device->gpsdata.dev.path);
-
- free_device(device);
- return false;
- } else {
- GPSD_LOG(LOG_RAW, &context.errout,
- "flagging descriptor %d in assign_channel()\n",
- device->gpsdata.gps_fd);
- FD_SET(device->gpsdata.gps_fd, &all_fds);
- adjust_max_fd(device->gpsdata.gps_fd, true);
- return true;
- }
- }
- }
- #ifdef RECONFIGURE_ENABLE
- #if __UNUSED_RECONFIGURE__
- static bool privileged_user(struct gps_device_t *device)
- {
-
- struct subscriber_t *sub;
- int subcount = 0;
- for (sub = subscribers; sub < subscribers + MAX_CLIENTS; sub++) {
- if (subscribed(sub, device))
- subcount++;
- }
-
- return subcount <= 1;
- }
- #endif
- static void set_serial(struct gps_device_t *device,
- speed_t speed, char *modestring)
- {
- unsigned int stopbits = device->gpsdata.dev.stopbits;
- char parity = device->gpsdata.dev.parity;
- int wordsize = 8;
- struct timespec delay;
- #ifndef __clang_analyzer__
- while (isspace((unsigned char) *modestring))
- modestring++;
- if (*modestring && (NULL != strchr("78", *modestring))) {
- wordsize = (int)(*modestring++ - '0');
- if (*modestring && (NULL != strchr("NOE", *modestring))) {
- parity = *modestring++;
- while (isspace((unsigned char) *modestring))
- modestring++;
- if (*modestring && (NULL != strchr("12", *modestring))) {
- stopbits = (unsigned int)(*modestring - '0');
- }
- }
- }
- #endif
- GPSD_LOG(LOG_PROG, &context.errout,
- "set_serial(%s,%u,%s) %c%d\n",
- device->gpsdata.dev.path,
- (unsigned int)speed, modestring, parity, stopbits);
-
-
- if (wordsize == (int)(9 - stopbits)
- && device->device_type->speed_switcher != NULL) {
- if (device->device_type->speed_switcher(device, speed, parity, (int)stopbits)) {
-
- (void)tcdrain(device->gpsdata.gps_fd);
-
- delay.tv_sec = 0;
- delay.tv_nsec = 50000000L;
- nanosleep(&delay, NULL);
- gpsd_set_speed(device, speed, parity, stopbits);
- }
- }
-
- }
- #endif
- #ifdef SOCKET_EXPORT_ENABLE
- static void json_devicelist_dump(char *reply, size_t replylen)
- {
- struct gps_device_t *devp;
- (void)strlcpy(reply, "{\"class\":\"DEVICES\",\"devices\":[", replylen);
- for (devp = devices; devp < devices + MAX_DEVICES; devp++)
- if (allocated_device(devp)
- && strlen(reply) + strlen(devp->gpsdata.dev.path) + 3 <
- replylen - 1) {
- char *cp;
- json_device_dump(devp,
- reply + strlen(reply), replylen - strlen(reply));
- cp = reply + strlen(reply);
- *--cp = '\0';
- *--cp = '\0';
- (void)strlcat(reply, ",", replylen);
- }
- str_rstrip_char(reply, ',');
- (void)strlcat(reply, "]}\r\n", replylen);
- }
- #endif
- static void rstrip(char *str)
- {
- char *strend;
- strend = str + strlen(str) - 1;
- while (isspace((unsigned char) *strend)) {
- *strend = '\0';
- --strend;
- }
- }
- static void handle_request(struct subscriber_t *sub,
- const char *buf, const char **after,
- char *reply, size_t replylen)
- {
- struct gps_device_t *devp;
- const char *end = NULL;
- if (str_starts_with(buf, "?DEVICES;")) {
- buf += 9;
- json_devicelist_dump(reply, replylen);
- } else if (str_starts_with(buf, "?WATCH")
- && (buf[6] == ';' || buf[6] == '=')) {
- const char *start = buf;
- buf += 6;
- if (*buf == ';') {
- ++buf;
- } else {
- int status = json_watch_read(buf + 1, &sub->policy, &end);
- sub->policy.timing = false;
- if (end == NULL)
- buf += strlen(buf);
- else {
- if (*end == ';')
- ++end;
- buf = end;
- }
- if (status != 0) {
- (void)snprintf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":"
- "\"Invalid WATCH: %s\"}\r\n",
- json_error_string(status));
- GPSD_LOG(LOG_ERROR, &context.errout, "response: %s\n", reply);
- } else if (sub->policy.watcher) {
- if (sub->policy.devpath[0] == '\0') {
-
- for (devp = devices; devp < devices + MAX_DEVICES; devp++)
- if (allocated_device(devp)) {
- (void)awaken(devp);
- if (devp->sourcetype == source_gpsd) {
-
- (void)gpsd_write(devp, start,
- (size_t)(end-start));
- }
- }
- } else {
- devp = find_device(sub->policy.devpath);
- if (devp == NULL) {
- (void)snprintf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":"
- "\"No such device as %s\"}\r\n",
- sub->policy.devpath);
- GPSD_LOG(LOG_ERROR, &context.errout,
- "response: %s\n", reply);
- goto bailout;
- } else if (awaken(devp)) {
- if (devp->sourcetype == source_gpsd) {
- (void)gpsd_write(devp, start, (size_t)(end-start));
- }
- } else {
- (void)snprintf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":\"Can't assign %s\"}\r\n",
- sub->policy.devpath);
- GPSD_LOG(LOG_ERROR, &context.errout,
- "response: %s\n", reply);
- goto bailout;
- }
- }
- }
- }
-
- json_devicelist_dump(reply + strlen(reply), replylen - strlen(reply));
- json_watch_dump(&sub->policy,
- reply + strlen(reply), replylen - strlen(reply));
- } else if (str_starts_with(buf, "?DEVICE")
- && (buf[7] == ';' || buf[7] == '=')) {
- struct devconfig_t devconf;
- buf += 7;
- devconf.path[0] = '\0';
- if (*buf == ';') {
- ++buf;
- } else {
- #ifdef RECONFIGURE_ENABLE
- struct gps_device_t *device;
-
- int status = json_device_read(buf + 1, &devconf, &end);
- if (end == NULL)
- buf += strlen(buf);
- else {
- if (*end == ';')
- ++end;
- buf = end;
- }
- device = NULL;
- if (status != 0) {
- (void)snprintf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":\"Invalid DEVICE: \"%s\"}\r\n",
- json_error_string(status));
- GPSD_LOG(LOG_ERROR, &context.errout, "response: %s\n", reply);
- goto bailout;
- } else {
- if (devconf.path[0] != '\0') {
-
- device = find_device(devconf.path);
-
- if (device == NULL || !awaken(device)) {
- (void)snprintf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":\"Can't open %s.\"}\r\n",
- devconf.path);
- GPSD_LOG(LOG_ERROR, &context.errout,
- "response: %s\n", reply);
- goto bailout;
- }
- } else {
-
- int devcount = 0;
- for (devp = devices; devp < devices + MAX_DEVICES; devp++)
- if (allocated_device(devp)) {
- device = devp;
- devcount++;
- }
- if (devcount == 0) {
- (void)strlcat(reply,
- "{\"class\":\"ERROR\",\"message\":"
- "\"Can't perform DEVICE configuration, "
- "no devices attached.\"}\r\n",
- replylen);
- GPSD_LOG(LOG_ERROR, &context.errout,
- "response: %s\n", reply);
- goto bailout;
- } else if (devcount > 1) {
- str_appendf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":"
- "\"No path specified in DEVICE, but "
- "multiple devices are attached.\"}\r\n");
- GPSD_LOG(LOG_ERROR, &context.errout,
- "response: %s\n", reply);
- goto bailout;
- }
-
- }
- if (device->device_type == NULL)
- str_appendf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":\"Type of %s is unknown.\"}\r\n",
- device->gpsdata.dev.path);
- else {
- timespec_t delta1, delta2;
- const struct gps_type_t *dt = device->device_type;
- bool no_serial_change =
- (devconf.baudrate == DEVDEFAULT_BPS)
- && (devconf.parity == DEVDEFAULT_PARITY)
- && (devconf.stopbits == DEVDEFAULT_STOPBITS);
-
- if (devconf.baudrate == DEVDEFAULT_BPS)
- devconf.baudrate =
- (unsigned int) gpsd_get_speed(device);
- if (devconf.parity == DEVDEFAULT_PARITY)
- devconf.stopbits = device->gpsdata.dev.stopbits;
- if (devconf.stopbits == DEVDEFAULT_STOPBITS)
- devconf.stopbits = device->gpsdata.dev.stopbits;
- if (0 < devconf.cycle.tv_sec ||
- 0 < devconf.cycle.tv_nsec) {
- devconf.cycle = device->gpsdata.dev.cycle;
- }
-
- if (devconf.driver_mode != device->gpsdata.dev.driver_mode
- && devconf.driver_mode != DEVDEFAULT_NATIVE
- && dt->mode_switcher != NULL)
- dt->mode_switcher(device, devconf.driver_mode);
- if (!no_serial_change) {
- char serialmode[3];
- serialmode[0] = devconf.parity;
- serialmode[1] = '0' + devconf.stopbits;
- serialmode[2] = '\0';
- set_serial(device,
- (speed_t) devconf.baudrate, serialmode);
- }
- TS_SUB(&delta1, &devconf.cycle, &device->gpsdata.dev.cycle);
- if (TS_NZ(&delta1)) {
-
- TS_SUB(&delta2, &devconf.cycle, &dt->min_cycle);
- if (TS_GZ(&delta2) &&
- dt->rate_switcher != NULL) {
-
- if (dt->rate_switcher(device,
- TSTONS(&devconf.cycle))) {
- device->gpsdata.dev.cycle = devconf.cycle;
- }
- }
- }
- if ('\0' != devconf.hexdata[0]) {
- write_gps(device->gpsdata.dev.path, devconf.hexdata);
- }
- }
- }
- #else
- str_appendf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":\"Device configuration support not compiled.\"}\r\n");
- #endif
- }
-
- for (devp = devices; devp < devices + MAX_DEVICES; devp++) {
- if (!allocated_device(devp))
- continue;
- else if (devconf.path[0] != '\0'
- && strcmp(devp->gpsdata.dev.path, devconf.path) != 0)
- continue;
- else {
- json_device_dump(devp,
- reply + strlen(reply),
- replylen - strlen(reply));
- }
- }
- } else if (str_starts_with(buf, "?POLL;")) {
- char tbuf[JSON_DATE_MAX+1];
- int active = 0;
- buf += 6;
- for (devp = devices; devp < devices + MAX_DEVICES; devp++) {
- if (allocated_device(devp) && subscribed(sub, devp))
- if ((devp->observed & GPS_TYPEMASK) != 0)
- active++;
- }
- (void)snprintf(reply, replylen,
- "{\"class\":\"POLL\",\"time\":\"%s\",\"active\":%d, "
- "\"tpv\":[",
- now_to_iso8601(tbuf, sizeof(tbuf)), active);
- for (devp = devices; devp < devices + MAX_DEVICES; devp++) {
- if (allocated_device(devp) && subscribed(sub, devp)) {
- if ((devp->observed & GPS_TYPEMASK) != 0) {
- json_tpv_dump(devp, &sub->policy,
- reply + strlen(reply),
- replylen - strlen(reply));
- rstrip(reply);
- (void)strlcat(reply, ",", replylen);
- }
- }
- }
- str_rstrip_char(reply, ',');
- (void)strlcat(reply, "],\"gst\":[", replylen);
- for (devp = devices; devp < devices + MAX_DEVICES; devp++) {
- if (allocated_device(devp) && subscribed(sub, devp)) {
- if ((devp->observed & GPS_TYPEMASK) != 0) {
- json_noise_dump(&devp->gpsdata,
- reply + strlen(reply),
- replylen - strlen(reply));
- rstrip(reply);
- (void)strlcat(reply, ",", replylen);
- }
- }
- }
- str_rstrip_char(reply, ',');
- (void)strlcat(reply, "],\"sky\":[", replylen);
- for (devp = devices; devp < devices + MAX_DEVICES; devp++) {
- if (allocated_device(devp) && subscribed(sub, devp)) {
- if ((devp->observed & GPS_TYPEMASK) != 0) {
- json_sky_dump(&devp->gpsdata,
- reply + strlen(reply),
- replylen - strlen(reply));
- rstrip(reply);
- (void)strlcat(reply, ",", replylen);
- }
- }
- }
- str_rstrip_char(reply, ',');
- (void)strlcat(reply, "]}\r\n", replylen);
- } else if (str_starts_with(buf, "?VERSION;")) {
- buf += 9;
- json_version_dump(reply, replylen);
- } else {
- const char *errend;
- errend = buf + strlen(buf) - 1;
- while (isspace((unsigned char) *errend) && errend > buf)
- --errend;
- (void)snprintf(reply, replylen,
- "{\"class\":\"ERROR\",\"message\":"
- "\"Unrecognized request '%.*s'\"}\r\n",
- (int)(errend - buf), buf);
- GPSD_LOG(LOG_ERROR, &context.errout, "ERROR response: %s\n", reply);
- buf += strlen(buf);
- }
- bailout:
- *after = buf;
- }
- static void raw_report(struct subscriber_t *sub, struct gps_device_t *device)
- {
-
-
- if (TEXTUAL_PACKET_TYPE(device->lexer.type)
- && (sub->policy.raw > 0 || sub->policy.nmea)) {
- (void)throttled_write(sub,
- (char *)device->lexer.outbuffer,
- device->lexer.outbuflen);
- return;
- }
-
- if (sub->policy.raw > 1) {
- (void)throttled_write(sub,
- (char *)device->lexer.outbuffer,
- device->lexer.outbuflen);
- return;
- }
- #ifdef BINARY_ENABLE
-
- if (sub->policy.raw == 1) {
- const char *hd =
- gpsd_hexdump(device->msgbuf, sizeof(device->msgbuf),
- (char *)device->lexer.outbuffer,
- device->lexer.outbuflen);
- (void)strlcat((char *)hd, "\r\n", sizeof(device->msgbuf));
- (void)throttled_write(sub, (char *)hd, strlen(hd));
- }
- #endif
- }
- static void pseudonmea_report(struct subscriber_t *sub,
- gps_mask_t changed,
- struct gps_device_t *device)
- {
- if (GPS_PACKET_TYPE(device->lexer.type)
- && !TEXTUAL_PACKET_TYPE(device->lexer.type)) {
- char buf[MAX_PACKET_LENGTH * 3 + 2];
- if ((changed & REPORT_IS) != 0) {
- nmea_tpv_dump(device, buf, sizeof(buf));
- GPSD_LOG(LOG_IO, &context.errout,
- "<= GPS (binary tpv) %s: %s\n",
- device->gpsdata.dev.path, buf);
- (void)throttled_write(sub, buf, strlen(buf));
- }
- if ((changed & (SATELLITE_SET|USED_IS)) != 0) {
- nmea_sky_dump(device, buf, sizeof(buf));
- GPSD_LOG(LOG_IO, &context.errout,
- "<= GPS (binary sky) %s: %s\n",
- device->gpsdata.dev.path, buf);
- (void)throttled_write(sub, buf, strlen(buf));
- }
- if ((changed & SUBFRAME_SET) != 0) {
- nmea_subframe_dump(device, buf, sizeof(buf));
- GPSD_LOG(LOG_IO, &context.errout,
- "<= GPS (binary subframe) %s: %s\n",
- device->gpsdata.dev.path, buf);
- (void)throttled_write(sub, buf, strlen(buf));
- }
- #ifdef AIVDM_ENABLE
- if ((changed & AIS_SET) != 0) {
- nmea_ais_dump(device, buf, sizeof(buf));
- GPSD_LOG(LOG_IO, &context.errout,
- "<= AIS (binary ais) %s: %s\n",
- device->gpsdata.dev.path, buf);
- (void)throttled_write(sub, buf, strlen(buf));
- }
- #endif
- }
- }
- #endif
- static void all_reports(struct gps_device_t *device, gps_mask_t changed)
- {
- #ifdef SOCKET_EXPORT_ENABLE
- struct subscriber_t *sub;
-
- if ((changed & DRIVER_IS) != 0) {
- bool listeners = false;
- for (sub = subscribers;
- sub < subscribers + MAX_CLIENTS; sub++)
- if (sub->active != 0
- && sub->policy.watcher
- && subscribed(sub, device))
- listeners = true;
- if (listeners) {
- (void)awaken(device);
- }
- }
-
- if ((changed & (DEVICEID_SET | DRIVER_IS)) != 0) {
- if (device->device_type == NULL)
- GPSD_LOG(LOG_ERROR, &context.errout,
- "internal error - device type of %s not set "
- "when expected\n",
- device->gpsdata.dev.path);
- else
- {
- char id2[GPS_JSON_RESPONSE_MAX];
- json_device_dump(device, id2, sizeof(id2));
- notify_watchers(device, true, false, id2);
- }
- }
- #endif
-
- if ((changed & RTCM2_SET) != 0 || (changed & RTCM3_SET) != 0) {
- if ((changed & RTCM2_SET) != 0
- && device->lexer.outbuflen > RTCM_MAX) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "overlong RTCM packet (%zd bytes)\n",
- device->lexer.outbuflen);
- } else if ((changed & RTCM3_SET) != 0
- && device->lexer.outbuflen > RTCM3_MAX) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "overlong RTCM3 packet (%zd bytes)\n",
- device->lexer.outbuflen);
- } else {
- struct gps_device_t *dp;
- for (dp = devices; dp < devices+MAX_DEVICES; dp++) {
- if (allocated_device(dp)) {
- if (NULL != dp->device_type &&
- NULL != dp->device_type->rtcm_writer) {
-
- ssize_t ret = dp->device_type->rtcm_writer(dp,
- (const char *)device->lexer.outbuffer,
- device->lexer.outbuflen);
- if (0 < ret) {
- GPSD_LOG(LOG_IO, &context.errout,
- "<= DGPS: %zd bytes of RTCM relayed.\n",
- device->lexer.outbuflen);
- } else if (0 == ret) {
-
- } else {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Write to RTCM sink failed, type %s\n",
- dp->device_type->type_name);
- }
- }
- }
- }
- }
- }
-
- if ((changed & CLEAR_IS)!=0)
- device->ship_to_ntpd = false;
- if ((changed & NTPTIME_IS)!=0)
- device->ship_to_ntpd = true;
-
- if ((changed & TIME_SET) == 0) {
-
- } else if ( 0 >= device->fixcnt && !batteryRTC ) {
-
-
-
- } else if (0 == device->newdata.time.tv_sec) {
-
- } else if (device->newdata.time.tv_sec <=
- device->pps_thread.fix_in.real.tv_sec) {
-
- } else if (!device->ship_to_ntpd) {
-
-
- } else {
- struct timedelta_t td;
- struct gps_device_t *ppsonly;
- ntp_latch(device, &td);
-
- for (ppsonly = devices; ppsonly < devices + MAX_DEVICES; ppsonly++)
- if (ppsonly->sourcetype == source_pps)
- pps_thread_fixin(&ppsonly->pps_thread, &td);
- if (device->shm_clock != NULL) {
- (void)ntpshm_put(device, device->shm_clock, &td);
- }
- #ifdef SOCKET_EXPORT_ENABLE
- notify_watchers(device, false, true,
- "{\"class\":\"TOFF\",\"device\":\"%s\",\"real_sec\":"
- "%lld, \"real_nsec\":%ld,\"clock_sec\":%lld,"
- "\"clock_nsec\":%ld}\r\n",
- device->gpsdata.dev.path,
- (long long)td.real.tv_sec, td.real.tv_nsec,
- (long long)td.clock.tv_sec, td.clock.tv_nsec);
- #endif
- }
-
- if (!device->cycle_end_reliable && (changed & (LATLON_SET | MODE_SET))!=0)
- changed |= REPORT_IS;
-
- if ((changed & REPORT_IS) != 0) {
- #ifdef NETFEED_ENABLE
- if (device->gpsdata.fix.mode == MODE_3D) {
- struct gps_device_t *dgnss;
-
- for (dgnss = devices; dgnss < devices + MAX_DEVICES; dgnss++)
- if (dgnss != device)
- netgnss_report(&context, device, dgnss);
- }
- #endif
- #if defined(DBUS_EXPORT_ENABLE)
- if (device->gpsdata.fix.mode > MODE_NO_FIX)
- send_dbus_fix(device);
- #endif
- }
- #ifdef SHM_EXPORT_ENABLE
- if ((changed & (REPORT_IS|GST_SET|SATELLITE_SET|SUBFRAME_SET|
- ATTITUDE_SET|RTCM2_SET|RTCM3_SET|AIS_SET)) != 0)
- shm_update(&context, &device->gpsdata);
- #endif
- #ifdef SOCKET_EXPORT_ENABLE
-
- for (sub = subscribers; sub < subscribers + MAX_CLIENTS; sub++) {
- if (sub == NULL || sub->active == 0 || !subscribed(sub, device))
- continue;
- #ifdef PASSTHROUGH_ENABLE
-
- if ((changed & PASSTHROUGH_IS) != 0) {
- (void)strlcat((char *)device->lexer.outbuffer,
- "\r\n",
- sizeof(device->lexer.outbuffer));
- (void)throttled_write(sub,
- (char *)device->lexer.outbuffer,
- device->lexer.outbuflen+2);
- continue;
- }
- #endif
-
- raw_report(sub, device);
-
- if (sub->policy.watcher) {
- if (changed & DATA_IS) {
- GPSD_LOG(LOG_PROG, &context.errout,
- "Changed mask: %s with %sreliable "
- "cycle detection\n",
- gps_maskdump(changed),
- device->cycle_end_reliable ? "" : "un");
- if ((changed & REPORT_IS) != 0)
- GPSD_LOG(LOG_PROG, &context.errout,
- "time to report a fix\n");
- if (sub->policy.nmea)
- pseudonmea_report(sub, changed, device);
- if (sub->policy.json) {
- char buf[GPS_JSON_RESPONSE_MAX * 4];
- if ((changed & AIS_SET) != 0)
- if (device->gpsdata.ais.type == 24
- && device->gpsdata.ais.type24.part != both
- && !sub->policy.split24)
- continue;
- json_data_report(changed, device, &sub->policy,
- buf, sizeof(buf));
- if (buf[0] != '\0')
- (void)throttled_write(sub, buf, strlen(buf));
- }
- }
- }
- }
- #endif
- }
- #ifdef SOCKET_EXPORT_ENABLE
- static int handle_gpsd_request(struct subscriber_t *sub, const char *buf)
- {
- char reply[GPS_JSON_RESPONSE_MAX + 1];
- reply[0] = '\0';
- if (buf[0] == '?') {
- const char *end;
- for (end = buf; *buf != '\0'; buf = end)
- if (isspace((unsigned char) *buf))
- end = buf + 1;
- else
- handle_request(sub, buf, &end,
- reply + strlen(reply),
- sizeof(reply) - strlen(reply));
- }
- return (int)throttled_write(sub, reply, strlen(reply));
- }
- #endif
- #if defined(CONTROL_SOCKET_ENABLE) && defined(SOCKET_EXPORT_ENABLE)
- static void ship_pps_message(struct gps_device_t *session,
- struct timedelta_t *td)
- {
- int precision = -20;
- char buf[GPS_JSON_RESPONSE_MAX];
- char ts_str[TIMESPEC_LEN];
- if ( source_usb == session->sourcetype) {
-
- precision = -10;
- }
- GPSD_LOG(LOG_DATA, &session->context->errout,
- "ship_pps: qErr_time %s qErr %ld, pps.tv_sec %lld\n",
- timespec_str(&session->gpsdata.qErr_time, ts_str, sizeof(ts_str)),
- session->gpsdata.qErr,
- (long long)td->real.tv_sec);
-
-
- (void)snprintf(buf, sizeof(buf),
- "{\"class\":\"PPS\",\"device\":\"%s\",\"real_sec\":%lld,"
- "\"real_nsec\":%ld,\"clock_sec\":%lld,\"clock_nsec\":%ld,"
- "\"precision\":%d",
- session->gpsdata.dev.path,
- (long long)td->real.tv_sec, td->real.tv_nsec,
- (long long)td->clock.tv_sec, td->clock.tv_nsec,
- precision);
-
- if (td->real.tv_sec == session->gpsdata.qErr_time.tv_sec) {
- str_appendf(buf, sizeof(buf), ",\"qErr\":%ld",
- session->gpsdata.qErr);
- }
- (void)strlcat(buf, "}\r\n", sizeof(buf));
- notify_watchers(session, true, true, buf);
-
- (void)clock_gettime(CLOCK_REALTIME, &session->gpsdata.online);
- }
- #endif
- #ifdef __UNUSED_AUTOCONNECT__
- #define DGPS_THRESHOLD 1600000
- #define SERVER_SAMPLE 12
- struct dgps_server_t
- {
- double lat, lon;
- char server[257];
- double dist;
- };
- static int srvcmp(const void *s, const void *t)
- {
- return (int)(((const struct dgps_server_t *)s)->dist - ((const struct dgps_server_t *)t)->dist);
- }
- static void netgnss_autoconnect(struct gps_context_t *context,
- double lat, double lon, const char *serverlist)
- {
- struct dgps_server_t keep[SERVER_SAMPLE], hold, *sp, *tp;
- char buf[BUFSIZ];
- FILE *sfp = fopen(serverlist, "r");
- if (sfp == NULL) {
- GPSD_LOG(LOG_ERROR, &context.errout, "no DGPS server list found.\n");
- return;
- }
- for (sp = keep; sp < keep + SERVER_SAMPLE; sp++) {
- sp->dist = DGPS_THRESHOLD;
- sp->server[0] = '\0';
- }
- hold.lat = hold.lon = 0;
- while (fgets(buf, (int)sizeof(buf), sfp)) {
- char *cp = strchr(buf, '#');
- if (cp != NULL)
- *cp = '\0';
- if (sscanf(buf, "%32lf %32lf %256s", &hold.lat, &hold.lon, hold.server) ==
- 3) {
- hold.dist = earth_distance(lat, lon, hold.lat, hold.lon);
- tp = NULL;
-
- for (sp = keep; sp < keep + SERVER_SAMPLE; sp++)
- if (hold.dist < sp->dist
- && (tp == NULL || hold.dist > tp->dist))
- tp = sp;
- if (tp != NULL)
- *tp = hold;
- }
- }
- (void)fclose(sfp);
- if (keep[0].server[0] == '\0') {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "no DGPS servers within %dm.\n",
- (int)(DGPS_THRESHOLD / 1000));
- return;
- }
-
- qsort((void *)keep, SERVER_SAMPLE, sizeof(struct dgps_server_t), srvcmp);
- for (sp = keep; sp < keep + SERVER_SAMPLE; sp++) {
- if (sp->server[0] != '\0') {
- GPSD_LOG(LOG_INF, &context.errout,
- "%s is %dkm away.\n", sp->server,
- (int)(sp->dist / 1000));
- if (dgpsip_open(context, sp->server) >= 0)
- break;
- }
- }
- }
- #endif
- static void gpsd_terminate(struct gps_context_t *context)
- {
- int dfd;
- for (dfd = 0; dfd < MAX_DEVICES; dfd++) {
- if (allocated_device(&devices[dfd])) {
- (void)gpsd_wrap(&devices[dfd]);
- }
- }
- context->pps_hook = NULL;
- }
- int main(int argc, char *argv[])
- {
-
- #ifdef SOCKET_EXPORT_ENABLE
- static char *gpsd_service = NULL;
- struct subscriber_t *sub;
- #endif
- fd_set rfds;
- #ifdef CONTROL_SOCKET_ENABLE
- fd_set control_fds;
- #endif
- #ifdef CONTROL_SOCKET_ENABLE
- static socket_t csock;
- socket_t cfd;
- static char *control_socket = NULL;
- #endif
- #if defined(SOCKET_EXPORT_ENABLE) || defined(CONTROL_SOCKET_ENABLE)
- sockaddr_t fsin;
- #endif
- static char *pid_file = NULL;
- struct gps_device_t *device;
- int i, option;
- int msocks[2] = {-1, -1};
- bool device_opened = false;
- bool go_background = true;
- volatile bool in_restart;
- gps_context_init(&context, "gpsd");
- #ifdef CONTROL_SOCKET_ENABLE
- INVALIDATE_SOCKET(csock);
- #if defined(SOCKET_EXPORT_ENABLE)
- context.pps_hook = ship_pps_message;
- #endif
- #endif
- while ((option = getopt(argc, argv, "bD:F:f:GhlNnP:rS:s:V")) != -1) {
- switch (option) {
- case 'b':
- context.readonly = true;
- break;
- case 'D':
-
- context.errout.debug = (int)strtol(optarg, 0, 0);
- #ifdef CLIENTDEBUG_ENABLE
- gps_enable_debug(context.errout.debug, stderr);
- #endif
- break;
- #ifdef CONTROL_SOCKET_ENABLE
- case 'F':
- control_socket = optarg;
- break;
- #endif
- case 'f':
-
- if (3 == strlen(optarg) &&
- ('7' == optarg[0] || '8' == optarg[0]) &&
- ('E' == optarg[1] || 'N' == optarg[1] ||
- 'O' == optarg[1]) &&
- ('0' <= optarg[2] && '2' >= optarg[2])) {
-
- (void)strlcpy(context.fixed_port_framing, optarg,
- sizeof(context.fixed_port_framing));
- } else {
-
- GPSD_LOG(LOG_ERROR, &context.errout,
- "-f has invalid framing %s\n", optarg);
- exit(1);
- }
- break;
- #ifndef FORCE_GLOBAL_ENABLE
- case 'G':
- listen_global = true;
- break;
- #endif
- case 'l':
- typelist();
- break;
- case 'N':
- go_background = false;
- break;
- case 'n':
- nowait = true;
- break;
- case 'P':
- pid_file = optarg;
- break;
- case 'r':
- batteryRTC = true;
- break;
- case 'S':
- #ifdef SOCKET_EXPORT_ENABLE
- gpsd_service = optarg;
- #endif
- break;
- case 's':
- {
-
- long speed = strtol(optarg, 0, 0);
- if (0 < speed) {
-
- context.fixed_port_speed = (speed_t)speed;
- } else {
-
- GPSD_LOG(LOG_ERROR, &context.errout,
- "-s has invalid speed %ld\n", speed);
- exit(1);
- }
- }
- break;
- case 'V':
- (void)printf("%s: %s (revision %s)\n", argv[0], VERSION, REVISION);
- exit(EXIT_SUCCESS);
- case 'h':
-
- case '?':
-
- default:
- usage();
- exit(EXIT_SUCCESS);
- }
- }
-
- if (argc - optind > MAX_DEVICES) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Too many devices on command line.\n");
- exit(1);
- }
- if (8 > sizeof(time_t)) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "This system has a 32-bit time_t.\n");
- GPSD_LOG(LOG_WARN, &context.errout,
- "This gpsd will fail on 2038-01-19T03:14:07Z.\n");
- }
- #if defined(SYSTEMD_ENABLE) && defined(CONTROL_SOCKET_ENABLE)
- sd_socket_count = sd_get_socket_count();
- if (sd_socket_count > 0 && control_socket != NULL) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "control socket passed on command line ignored\n");
- control_socket = NULL;
- }
- #endif
- #if defined(CONTROL_SOCKET_ENABLE) || defined(SYSTEMD_ENABLE)
- if (
- #ifdef CONTROL_SOCKET_ENABLE
- control_socket == NULL
- #endif
- #if defined(CONTROL_SOCKET_ENABLE) && defined(SYSTEMD_ENABLE)
- &&
- #endif
- #ifdef SYSTEMD_ENABLE
- sd_socket_count <= 0
- #endif
- && optind >= argc) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't run with neither control socket nor devices\n");
- exit(EXIT_FAILURE);
- }
-
- #if defined(SYSTEMD_ENABLE) && defined(CONTROL_SOCKET_ENABLE)
- if (sd_socket_count > 0) {
- csock = SD_SOCKET_FDS_START;
- FD_SET(csock, &all_fds);
- adjust_max_fd(csock, true);
- }
- #endif
- #ifdef CONTROL_SOCKET_ENABLE
- if (control_socket) {
- (void)unlink(control_socket);
- if (BAD_SOCKET(csock = filesock(control_socket))) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "control socket create failed, netlib error %d\n",
- csock);
- exit(EXIT_FAILURE);
- } else
- GPSD_LOG(LOG_SPIN, &context.errout,
- "control socket %s is fd %d\n",
- control_socket, csock);
- FD_SET(csock, &all_fds);
- adjust_max_fd(csock, true);
- GPSD_LOG(LOG_PROG, &context.errout,
- "control socket opened at %s\n",
- control_socket);
- }
- #endif
- #else
- if (optind >= argc) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't run with no devices specified\n");
- exit(EXIT_FAILURE);
- }
- #endif
-
-
- if (go_background) {
-
- if (os_daemon(0, 0) != 0)
- GPSD_LOG(LOG_ERROR, &context.errout,
- "daemonization failed: %s\n",strerror(errno));
- }
- if (pid_file != NULL) {
- FILE *fp;
- if ((fp = fopen(pid_file, "w")) != NULL) {
- (void)fprintf(fp, "%u\n", (unsigned int)getpid());
- (void)fclose(fp);
- } else {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Cannot create PID file: %s.\n", pid_file);
- }
- }
- openlog("gpsd", LOG_PID, LOG_USER);
- GPSD_LOG(LOG_INF, &context.errout, "launching (Version %s)\n", VERSION);
- #ifdef SOCKET_EXPORT_ENABLE
- if (!gpsd_service)
- gpsd_service =
- getservbyname("gpsd", "tcp") ? "gpsd" : DEFAULT_GPSD_PORT;
- if (passivesocks(gpsd_service, "tcp", QLEN, msocks) < 1) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "command sockets creation failed, netlib errors %d, %d\n",
- msocks[0], msocks[1]);
- if (pid_file != NULL)
- (void)unlink(pid_file);
- exit(EXIT_FAILURE);
- }
- GPSD_LOG(LOG_INF, &context.errout, "listening on port %s\n",
- gpsd_service);
- #endif
- if (getuid() == 0) {
- errno = 0;
-
-
- if (nice(NICEVAL) == -1 && errno != 0)
- GPSD_LOG(LOG_WARN, &context.errout,
- "PPS: o=priority setting failed. Time accuracy "
- "will be degraded\n");
- }
-
- (void)ntpshm_context_init(&context);
- #if defined(DBUS_EXPORT_ENABLE)
-
- if (initialize_dbus_connection()) {
-
- GPSD_LOG(LOG_ERROR, &context.errout,
- "unable to connect to the DBUS system bus\n");
- } else
- GPSD_LOG(LOG_PROG, &context.errout,
- "successfully connected to the DBUS system bus\n");
- #endif
- #ifdef SHM_EXPORT_ENABLE
-
- (void)shm_acquire(&context);
- #endif
-
- in_restart = false;
- for (i = optind; i < argc; i++) {
- if (!gpsd_add_device(argv[i], nowait)) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "initial GPS device %s open failed\n",
- argv[i]);
- } else {
- device_opened = true;
- }
- }
- if (
- #ifdef CONTROL_SOCKET_ENABLE
- control_socket == NULL &&
- #endif
- #ifdef SYSTEMD_ENABLE
- sd_socket_count <= 0 &&
- #endif
- !device_opened) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "can't run with neither control socket nor devices open\n");
- exit(EXIT_FAILURE);
- }
-
- if (0 == getuid()) {
- struct passwd *pw;
- struct stat stb;
-
- for (i = optind; i < argc; i++)
-
- if (stat(argv[i], &stb) == 0)
- (void)chmod(argv[i], stb.st_mode | S_IRGRP | S_IWGRP);
-
- if (setgroups(0, NULL) != 0)
- GPSD_LOG(LOG_ERROR, &context.errout,
- "setgroups() failed, errno %s\n",
- strerror(errno));
- #ifdef GPSD_GROUP
- {
- struct group *grp = getgrnam(GPSD_GROUP);
- if (grp)
- if (setgid(grp->gr_gid) != 0)
- GPSD_LOG(LOG_ERROR, &context.errout,
- "setgid() failed, errno %s\n",
- strerror(errno));
- }
- #else
- if ((optind < argc && stat(argv[optind], &stb) == 0)
- || stat(PROTO_TTY, &stb) == 0) {
- GPSD_LOG(LOG_PROG, &context.errout,
- "changing to group %d\n", stb.st_gid);
- if (setgid(stb.st_gid) != 0)
- GPSD_LOG(LOG_ERROR, &context.errout,
- "setgid() failed, errno %s\n",
- strerror(errno));
- }
- #endif
- pw = getpwnam(GPSD_USER);
- if (pw)
- if (setuid(pw->pw_uid) != 0)
- GPSD_LOG(LOG_ERROR, &context.errout,
- "setuid() failed, errno %s\n",
- strerror(errno));
- }
- GPSD_LOG(LOG_INF, &context.errout,
- "running with effective group ID %d\n", getegid());
- GPSD_LOG(LOG_INF, &context.errout,
- "running with effective user ID %d\n", geteuid());
- #ifdef SOCKET_EXPORT_ENABLE
- for (i = 0; i < NITEMS(subscribers); i++) {
- subscribers[i].fd = UNALLOCATED_FD;
- (void)pthread_mutex_init(&subscribers[i].mutex, NULL);
- }
- #endif
- {
- struct sigaction sa;
- sa.sa_flags = 0;
- #ifdef __COVERITY__
-
- sa.sa_restorer = NULL;
- #endif
- sa.sa_handler = onsig;
- (void)sigfillset(&sa.sa_mask);
- (void)sigaction(SIGHUP, &sa, NULL);
- (void)sigaction(SIGINT, &sa, NULL);
- (void)sigaction(SIGTERM, &sa, NULL);
- (void)sigaction(SIGQUIT, &sa, NULL);
- (void)signal(SIGPIPE, SIG_IGN);
- }
-
- if (setjmp(restartbuf) > 0) {
- gpsd_terminate(&context);
- in_restart = true;
- GPSD_LOG(LOG_WARN, &context.errout, "gpsd restarted by SIGHUP\n");
- }
- signalled = 0;
- for (i = 0; i < AFCOUNT; i++)
- if (msocks[i] >= 0) {
- FD_SET(msocks[i], &all_fds);
- adjust_max_fd(msocks[i], true);
- }
- #ifdef CONTROL_SOCKET_ENABLE
- FD_ZERO(&control_fds);
- #endif
-
- gpsd_time_init(&context, time(NULL));
-
- if (in_restart)
- for (i = optind; i < argc; i++) {
- if (!gpsd_add_device(argv[i], nowait)) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "GPS device %s open failed\n",
- argv[i]);
- }
- }
- while (0 == signalled) {
- fd_set efds;
- GPSD_LOG(LOG_RAW + 1, &context.errout, "await data\n");
- switch(gpsd_await_data(&rfds, &efds, maxfd, &all_fds, &context.errout))
- {
- case AWAIT_GOT_INPUT:
- break;
- case AWAIT_NOT_READY:
- for (device = devices; device < devices + MAX_DEVICES; device++)
-
- if (allocated_device(device)
- && (0 <= device->gpsdata.gps_fd && device->gpsdata.gps_fd < (socket_t)FD_SETSIZE)
- && FD_ISSET(device->gpsdata.gps_fd, &efds)) {
- deactivate_device(device);
- free_device(device);
- }
- continue;
- case AWAIT_FAILED:
- exit(EXIT_FAILURE);
- }
- #ifdef SOCKET_EXPORT_ENABLE
-
- for (i = 0; i < AFCOUNT; i++) {
- if (msocks[i] >= 0 && FD_ISSET(msocks[i], &rfds)) {
- socklen_t alen = (socklen_t) sizeof(fsin);
- socket_t ssock =
- accept(msocks[i], (struct sockaddr *)&fsin, &alen);
- if (BAD_SOCKET(ssock))
- GPSD_LOG(LOG_ERROR, &context.errout,
- "accept: fail: %s\n", strerror(errno));
- else {
- struct subscriber_t *client = NULL;
- int opts = fcntl(ssock, F_GETFL);
- static struct linger linger = { 1, RELEASE_TIMEOUT };
- char *c_ip;
- if (opts >= 0)
- (void)fcntl(ssock, F_SETFL, opts | O_NONBLOCK);
- c_ip = netlib_sock2ip(ssock);
- client = allocate_client();
- if (client == NULL) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Client %s connect on fd %d -"
- "no subscriber slots available\n", c_ip,
- ssock);
- (void)close(ssock);
- } else
- if (setsockopt
- (ssock, SOL_SOCKET, SO_LINGER, (char *)&linger,
- (int)sizeof(struct linger)) == -1) {
- GPSD_LOG(LOG_ERROR, &context.errout,
- "Error: SETSOCKOPT SO_LINGER\n");
- (void)close(ssock);
- } else {
- char announce[GPS_JSON_RESPONSE_MAX];
- FD_SET(ssock, &all_fds);
- adjust_max_fd(ssock, true);
- client->fd = ssock;
- client->active = time(NULL);
- GPSD_LOG(LOG_SPIN, &context.errout,
- "client %s (%d) connect on fd %d\n", c_ip,
- sub_index(client), ssock);
- json_version_dump(announce, sizeof(announce));
- (void)throttled_write(client, announce,
- strlen(announce));
- }
- }
- FD_CLR(msocks[i], &rfds);
- }
- }
- #endif
- #ifdef CONTROL_SOCKET_ENABLE
-
- if (csock > -1 && FD_ISSET(csock, &rfds)) {
- socklen_t alen = (socklen_t) sizeof(fsin);
- socket_t ssock = accept(csock, (struct sockaddr *)&fsin, &alen);
- if (BAD_SOCKET(ssock))
- GPSD_LOG(LOG_ERROR, &context.errout,
- "accept: %s\n", strerror(errno));
- else {
- GPSD_LOG(LOG_INF, &context.errout,
- "control socket connect on fd %d\n",
- ssock);
- FD_SET(ssock, &all_fds);
- FD_SET(ssock, &control_fds);
- adjust_max_fd(ssock, true);
- }
- FD_CLR(csock, &rfds);
- }
-
- GPSD_LOG(LOG_RAW + 1, &context.errout, "read control commands");
- for (cfd = 0; cfd < (int)FD_SETSIZE; cfd++)
- if (FD_ISSET(cfd, &control_fds)) {
- char buf[BUFSIZ];
- ssize_t rd;
- while ((rd = read(cfd, buf, sizeof(buf) - 1)) > 0) {
- buf[rd] = '\0';
- GPSD_LOG(LOG_CLIENT, &context.errout,
- "<= control(%d): %s\n", cfd, buf);
-
- handle_control(cfd, buf);
- }
- GPSD_LOG(LOG_SPIN, &context.errout,
- "close(%d) of control socket\n", cfd);
- (void)close(cfd);
- FD_CLR(cfd, &all_fds);
- FD_CLR(cfd, &control_fds);
- adjust_max_fd(cfd, false);
- }
- #endif
-
- GPSD_LOG(LOG_RAW + 1, &context.errout, "poll active devices");
- for (device = devices; device < devices + MAX_DEVICES; device++)
- if (allocated_device(device) && device->gpsdata.gps_fd > 0)
- switch (gpsd_multipoll(FD_ISSET(device->gpsdata.gps_fd, &rfds),
- device, all_reports, DEVICE_REAWAKE))
- {
- case DEVICE_READY:
- FD_SET(device->gpsdata.gps_fd, &all_fds);
- adjust_max_fd(device->gpsdata.gps_fd, true);
- break;
- case DEVICE_UNREADY:
- FD_CLR(device->gpsdata.gps_fd, &all_fds);
- adjust_max_fd(device->gpsdata.gps_fd, false);
- break;
- case DEVICE_ERROR:
- case DEVICE_EOF:
- deactivate_device(device);
- break;
- default:
- break;
- }
- #ifdef __UNUSED_AUTOCONNECT__
- if (context.fixcnt > 0 && !context.autconnect) {
- for (device = devices; device < devices + MAX_DEVICES; device++) {
- if (device->gpsdata.fix.mode > MODE_NO_FIX) {
- netgnss_autoconnect(&context,
- device->gpsdata.fix.latitude,
- device->gpsdata.fix.longitude);
- context.autconnect = True;
- break;
- }
- }
- }
- #endif
- #ifdef SOCKET_EXPORT_ENABLE
-
- for (sub = subscribers; sub < subscribers + MAX_CLIENTS; sub++) {
- if (sub->active == 0)
- continue;
- lock_subscriber(sub);
- if (FD_ISSET(sub->fd, &rfds)) {
- char buf[BUFSIZ];
- int buflen;
- unlock_subscriber(sub);
- GPSD_LOG(LOG_PROG, &context.errout,
- "checking client(%d)\n",
- sub_index(sub));
- if ((buflen =
- (int)recv(sub->fd, buf, sizeof(buf) - 1, 0)) <= 0) {
- detach_client(sub);
- } else {
- if (buf[buflen - 1] != '\n')
- buf[buflen++] = '\n';
- buf[buflen] = '\0';
- GPSD_LOG(LOG_CLIENT, &context.errout,
- "<= client(%d): %s\n", sub_index(sub), buf);
-
- sub->active = time(NULL);
- if (handle_gpsd_request(sub, buf) < 0)
- detach_client(sub);
- }
- } else {
- unlock_subscriber(sub);
- if (!sub->policy.watcher
- && time(NULL) - sub->active > COMMAND_TIMEOUT) {
- GPSD_LOG(LOG_WARN, &context.errout,
- "client(%d) timed out on command wait.\n",
- sub_index(sub));
- detach_client(sub);
- }
- }
- }
-
- for (device = devices; device < devices + MAX_DEVICES; device++) {
- bool device_needed = nowait;
- if (!allocated_device(device))
- continue;
- if (!device_needed)
- for (sub=subscribers; sub<subscribers+MAX_CLIENTS; sub++) {
- if (sub->active == 0)
- continue;
- device_needed = subscribed(sub, device);
- if (device_needed)
- break;
- }
- if (!device_needed && device->gpsdata.gps_fd > -1 &&
- device->lexer.type != BAD_PACKET) {
- if (device->releasetime == 0) {
- device->releasetime = time(NULL);
- GPSD_LOG(LOG_PROG, &context.errout,
- "device %d (fd %d) released\n",
- (int)(device - devices),
- device->gpsdata.gps_fd);
- } else if (time(NULL) - device->releasetime > RELEASE_TIMEOUT) {
- GPSD_LOG(LOG_PROG, &context.errout,
- "device %d closed\n",
- (int)(device - devices));
- GPSD_LOG(LOG_RAW, &context.errout,
- "unflagging descriptor %d\n",
- device->gpsdata.gps_fd);
- deactivate_device(device);
- }
- }
- if (device_needed && BAD_SOCKET(device->gpsdata.gps_fd) &&
- (device->opentime == 0 ||
- time(NULL) - device->opentime > DEVICE_RECONNECT)) {
- device->opentime = time(NULL);
- GPSD_LOG(LOG_INF, &context.errout,
- "reconnection attempt on device %d\n",
- (int)(device - devices));
- (void)awaken(device);
- }
- }
- #endif
-
- if (argc == optind && highwater > 0) {
- int subcount = 0, devcount = 0;
- #ifdef SOCKET_EXPORT_ENABLE
- for (sub = subscribers; sub < subscribers + MAX_CLIENTS; sub++)
- if (sub->active != 0)
- ++subcount;
- #endif
- for (device = devices; device < devices + MAX_DEVICES; device++)
- if (allocated_device(device))
- ++devcount;
- if (subcount == 0 && devcount == 0) {
- GPSD_LOG(LOG_SHOUT, &context.errout,
- "no subscribers or devices, shutting down.\n");
- goto shutdown;
- }
- }
- }
-
-
- if (SIGHUP == (int)signalled)
- longjmp(restartbuf, 1);
- GPSD_LOG(LOG_WARN, &context.errout,
- "received terminating signal %d.\n", (int)signalled);
- shutdown:
- gpsd_terminate(&context);
- GPSD_LOG(LOG_WARN, &context.errout, "exiting.\n");
- #ifdef SOCKET_EXPORT_ENABLE
-
- for (sub = subscribers; sub < subscribers + MAX_CLIENTS; sub++) {
- if (sub->active != 0)
- detach_client(sub);
- }
- #endif
- #ifdef SHM_EXPORT_ENABLE
- shm_release(&context);
- #endif
- #ifdef CONTROL_SOCKET_ENABLE
- if (control_socket)
- (void)unlink(control_socket);
- #endif
- if (pid_file)
- (void)unlink(pid_file);
- return 0;
- }
|