From b476ef5544e20915ff323e3c3605d05738574766 Mon Sep 17 00:00:00 2001 From: John Klug Date: Wed, 14 Mar 2018 09:36:40 -0500 Subject: Fix stalled ntpd startup on certain systems --- recipes-navigation/gpsd/gpsd/gpsd_ubx_fixed.sh | 87 ++++++++++++++---------- recipes-navigation/gpsd/gpsd/gpsd_ubx_settime.sh | 78 ++++++++++++++------- 2 files changed, 105 insertions(+), 60 deletions(-) diff --git a/recipes-navigation/gpsd/gpsd/gpsd_ubx_fixed.sh b/recipes-navigation/gpsd/gpsd/gpsd_ubx_fixed.sh index c975942..6c9c126 100755 --- a/recipes-navigation/gpsd/gpsd/gpsd_ubx_fixed.sh +++ b/recipes-navigation/gpsd/gpsd/gpsd_ubx_fixed.sh @@ -5,6 +5,10 @@ # This is to know when to set the system # and hardware clock, and when # it is safe to start ntp. +# DEBUG=1 to set debug +NAME=gpsd_ubx_fixed +shopt -s expand_aliases +alias LG=logger\ -t\ ${NAME} if [[ -r /etc/default/gpsd ]] ; then . /etc/default/gpsd else @@ -20,48 +24,62 @@ function rm_gps_file rm -f "${GPS_FIXFILE}" fi } +function kill_it +{ + cmd=$1 + pid=$2 + sig=$3 + ((DEBUG)) && LG -p user.info "terminating $cmd(${pid}) with SIG${sig}" + kill -SIG${sig} ${pid} >/dev/null 2>&1 + result=$? + ((DEBUG)) && LG -p user.info "kill -SIG${sig} ${pid} result is $result" +} + # Most exits are errors, so remove the GPS fix file. trap rm_gps_file EXIT if ! [[ -x /usr/bin/gpsmon ]] ; then - logger -p user.err "gpsd_ubx_3dfix.sh: Please install gpsmon" + LG -p user.err "Please install gpsmon" exit 0 fi -ppid=$$ -# echo gpsd_ubx_fixed parent $$ >>/tmp/gpsdlog.${ppid} -# Restrict future kill to our process group. During boot, -# this is the entire rc complex, but not any daemons. -pgid=$(ps -o pgrp -p $ppid --no-heading) # Terminate gpsmon after 10 seconds if it is our child +# gpsmon has issues with terminals and stalling on terminal +# read of the console. ( sleep 10 - # echo in child pgid $pgid >>"/tmp/gpsdlog.${ppid}" - # ps -o pid,pgid,args -e >>"/tmp/log1.${ppid}" - # Kill a gpsmon -a in our process group. Hopefully this is our - # parents gpsmon -a. - gpsmonpid=$(ps -o pid,pgid,args -e | egrep "[[:space:]]${pgid}[[:space:]]+gpsmon[[:space:]]-a$" | sed -r 's/^[[:space:]]*([0-9]*)[[:space:]]+.*/\1/') - # echo sleeper TERM found pid $gpsmonpid >>"/tmp/gpsdlog.${ppid}" - if ((${#gpsmonpid})) ; then - logger -p user.info "terminating gpsmon(${gpsmonpid}) with SIGTERM" - # echo "terminating gpsmon(${gpsmonpid}) with SIGTERM" - kill ${gpsmonpid} - else + # We use BASHPID because our parent, $$, may have already exited, and + # then ps will not work. BASHPID is the current subshell. + ppid=$BASHPID + pgid=$(ps --no-heading -o pgid -p $ppid) + + if ((${#pgid} == 0)) ; then + LG -p user.err "Could not find a pgid for $ppid" + ps -fjp $ppid | LG -p user.error + fi + + gpsmonpid=$(pgrep -lg $pgid | egrep '[[:space:]]gpsmon$' | sed 's/[[:space:]].*//') + ((DEBUG)) && LG -p user.info "$pgid is pgid $gpsmonpid is gpsmonpid" + # Find the gpsmonpid that is our grandchild + ((didkill=0)) + + # for loop is in case gpsmonpid has children. + for p in ${gpsmonpid} ; do + if ((${#p})) ; then + kill_it gpsmon $p TERM + ((didkill=p)) + break + fi + done + if ((didkill == 0)) ; then exit 0 fi + # Do a sigkill to be sure. sleep 2 - # echo "SIGKILL next" >>"/tmp/gpsdlog.${ppid}" - # ps -o pid,pgid,comm -e >>"/tmp/log2.${ppid}" - gpsmonpid=$(ps -o pid,pgid,args -e | egrep "[[:space:]]${pgid}[[:space:]]+gpsmon[[:space:]]-a$" | sed -r 's/^[[:space:]]*([0-9]*)[[:space:]]+.*/\1/') - # echo sleeper KILL found pid $gpsmonpid >>"/tmp/gpsdlog.${ppid}" - # Kill with SIGKILL, in case SIGTERM fails. - if ((${#gpsmonpid})) ; then - logger -p user.info "terminating gpsmon(${gpsmonpid}) with SIGKILL" - kill -9 ${gpsmonpid} - fi + kill_it gpsmon $didkill KILL ) & - + fix="" status="" @@ -93,14 +111,13 @@ stuff="$(gpsmon -a 2>&1 | { fi })" -# echo Past GPSMON in gpsd_ubx_fixed pid $$ if ((${#stuff} == 0)) ; then - logger -p user.err No data from GPS + LG -p user.err "No data from GPS" exit 1 fi if [[ -t 1 ]] ; then - stty echo icanon + stty echo icanon 2>/dev/null fi OIFS=${IFS} @@ -119,8 +136,8 @@ case $status in ((fixOK=0)) ;; *) - logger -p user.err "Is GPSD running?" - logger -p user.err "FIX OK field should be single hex digit: gpsmon data: $stuff" + LG -p user.err "Is GPSD running?" + LG -p user.err "FIX OK field should be single hex digit: gpsmon data: $stuff" exit 1 break ;; @@ -130,7 +147,7 @@ esac if ((fixOK == 0)) ; then - logger -p user.err "Problem with the GPS fix. The fix is an even value, \"$status\", and should be an odd value. fixOK=${fixOK}" + LG -p user.err "Problem with the GPS fix. The fix is an even value, \"$status\", and should be an odd value. fixOK=${fixOK}" exit 1 # Retry later. How? fi @@ -140,14 +157,14 @@ fi for x in $GPSFIX ; do # echo test $x with $fix if [[ $x == $fix ]] ; then - logger user.info "GPS has fix $fix found in list GPSFIX: $GPSFIX" + LG -p user.info "GPS has fix $fix found in list GPSFIX: $GPSFIX" echo $x >"${GPS_FIXFILE}" GPS_FIXFILE="" exit 0 fi done -logger -p user.info "GPS fix is bad: $fix and should be one of: $GPSFIX" +LG -p user.info "GPS fix is bad: $fix and should be one of: $GPSFIX" # Start later exit 1 diff --git a/recipes-navigation/gpsd/gpsd/gpsd_ubx_settime.sh b/recipes-navigation/gpsd/gpsd/gpsd_ubx_settime.sh index 5644d57..f74e7b0 100755 --- a/recipes-navigation/gpsd/gpsd/gpsd_ubx_settime.sh +++ b/recipes-navigation/gpsd/gpsd/gpsd_ubx_settime.sh @@ -1,36 +1,63 @@ #!/bin/bash # PPS= 1484247472.29561104 clock= 1484247990.00000000 offset= 517.704388959 +# DEBUG=1 to set debug +NAME=gpsd_ubx_settime +shopt -s expand_aliases +alias LG="logger -t $NAME" +function kill_it +{ + cmd=$1 + pid=$2 + sig=$3 + ((DEBUG)) && LG -p user.info "terminating $cmd(${pid}) with SIG${sig}" + kill -SIG${sig} ${pid} + result=$? + ((DEBUG)) && LG -p user.info "kill -SIG${sig} ${pid} result is $result" +} trap "stty echo icanon" exit OIFS=$IFS IFS=$'\n' -if ! /usr/sbin/gpsd_ubx_fixed ; then - logger -s -p user.warn "GPS does not have a fix yet. Try again later." +if ! /usr/sbin/gpsd_ubx_fixed &1 | ( while read ln ; do if [[ $ln =~ ^[[:space:]]*PPS=.*clock=[[:space:]]*([0-9]*)\. ]] ; then @@ -39,9 +66,10 @@ epoch=$(gpsmon -a 2>&1 | ( while read ln ; do fi done )) if [[ $epoch =~ ^[0-9]+$ ]] ; then + LG -p user.warn "Updating time to epoch time, ${epoch}." date +%s -s @${epoch} >/dev/null else - logger user.err "gpsmon output is bad." + LG -p user.err "gpsmon output is bad." exit 1 fi exit 0 -- cgit v1.2.3