[Git][NTPsec/ntpsec][master] Address GitLab issue #322: ntpwait should not terminate with a traceback at SIGINT

Eric S. Raymond gitlab at mg.gitlab.com
Sun May 28 19:40:21 UTC 2017


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
05e60a00 by Eric S. Raymond at 2017-05-28T15:40:08-04:00
Address GitLab issue #322: ntpwait should not terminate with a traceback at SIGINT

- - - - -


1 changed file:

- ntpclients/ntpwait


Changes:

=====================================
ntpclients/ntpwait
=====================================
--- a/ntpclients/ntpwait
+++ b/ntpclients/ntpwait
@@ -133,82 +133,86 @@ if __name__ == "__main__":
     except getopt.GetoptError as err:
         sys.stderr.write(str(err) + "\n")
         raise SystemExit(2)
-    tries = 100
-    sleep = 6
-    verbose = 0
-    for (switch, val) in options:
-        if switch in ("-n", "--tries"):
-            errmsg = "Error: -n parameter '%s' not a number\n"
-            tries = ntp.util.safeargcast(val, int, errmsg, __doc__)
-        elif switch in ("-s", "--sleep"):
-            errmsg = "Error: -s parameter '%s' not a number\n"
-            sleep = ntp.util.safeargcast(val, int, errmsg, __doc__)
-        elif switch in ("-v", "--verbose"):
-            verbose += 1
-        elif switch in ("-h", "--help"):
-            sys.stdout.write(__doc__)
-            raise SystemExit(0)
-
-    # Autoflush stdout
-    sys.stdout = Unbuffered(sys.stdout)
-
-    if verbose:
-        sys.stdout.write("Waiting for ntpd to synchronize...  ")
-
-    for i in range(1, tries):
-        session = ntp.packet.ControlSession()
-        # session.debug = 4
-        if not session.openhost("localhost"):
-            if verbose:
-                sys.stdout.write("\bntpd is not running!\n")
-            continue
-
-        msg = None
-        try:
-            msg = session.doquery(2)     # Request system variables
-        except ntp.packet.ControlException as e:
-            sys.stderr.write("localhost: timed out, nothing received\n")
-            sys.stderr.write(e.message)
-        except socket.error:
-            if verbose:
-                sys.stdout.write("\b" + "*+:."[i % 4])
-            time.sleep(sleep)
-            continue
-
-        if verbose >= 2:
-            sys.stderr.write(repr(session.response) + "\n")
-
-        if msg and msg.startswith("***"):
-            if verbose:
-                sys.stdout.write("\b" + msg + "\n")
-            sys.exit(1)
-
-        m = re.search(r"leap=([^,]*),", session.response)
-        if m:
-            leap = int(m.group(1))
-        else:
-            sys.stdout.write("\bLeap status not available\n")
-            sys.exit(1)
-
-        if leap == ntp.magic.LEAP_NOTINSYNC:
-            if verbose:
-                sys.stdout.write("\b" + "*+:."[i % 4])
-            if i < tries:
+    try:
+        tries = 100
+        sleep = 6
+        verbose = 0
+        for (switch, val) in options:
+            if switch in ("-n", "--tries"):
+                errmsg = "Error: -n parameter '%s' not a number\n"
+                tries = ntp.util.safeargcast(val, int, errmsg, __doc__)
+            elif switch in ("-s", "--sleep"):
+                errmsg = "Error: -s parameter '%s' not a number\n"
+                sleep = ntp.util.safeargcast(val, int, errmsg, __doc__)
+            elif switch in ("-v", "--verbose"):
+                verbose += 1
+            elif switch in ("-h", "--help"):
+                sys.stdout.write(__doc__)
+                raise SystemExit(0)
+
+        # Autoflush stdout
+        sys.stdout = Unbuffered(sys.stdout)
+
+        if verbose:
+            sys.stdout.write("Waiting for ntpd to synchronize...  ")
+
+        for i in range(1, tries):
+            session = ntp.packet.ControlSession()
+            # session.debug = 4
+            if not session.openhost("localhost"):
+                if verbose:
+                    sys.stdout.write("\bntpd is not running!\n")
+                continue
+
+            msg = None
+            try:
+                msg = session.doquery(2)     # Request system variables
+            except ntp.packet.ControlException as e:
+                sys.stderr.write("localhost: timed out, nothing received\n")
+                sys.stderr.write(e.message)
+            except socket.error:
+                if verbose:
+                    sys.stdout.write("\b" + "*+:."[i % 4])
                 time.sleep(sleep)
-            continue
-
-        if leap in (ntp.magic.LEAP_NOWARNING, ntp.magic.LEAP_ADDSECOND,
-                    ntp.magic.LEAP_DELSECOND):
-            # We could check "sync" here to make sure we like the source...
-            if verbose:
-                sys.stdout.write("\bOK!\n")
-            sys.exit(0)
+                continue
+
+            if verbose >= 2:
+                sys.stderr.write(repr(session.response) + "\n")
+
+            if msg and msg.startswith("***"):
+                if verbose:
+                    sys.stdout.write("\b" + msg + "\n")
+                sys.exit(1)
+
+            m = re.search(r"leap=([^,]*),", session.response)
+            if m:
+                leap = int(m.group(1))
+            else:
+                sys.stdout.write("\bLeap status not available\n")
+                sys.exit(1)
+
+            if leap == ntp.magic.LEAP_NOTINSYNC:
+                if verbose:
+                    sys.stdout.write("\b" + "*+:."[i % 4])
+                if i < tries:
+                    time.sleep(sleep)
+                continue
+
+            if leap in (ntp.magic.LEAP_NOWARNING, ntp.magic.LEAP_ADDSECOND,
+                        ntp.magic.LEAP_DELSECOND):
+                # We could check "sync" here to make sure we like the source...
+                if verbose:
+                    sys.stdout.write("\bOK!\n")
+                sys.exit(0)
+
+            sys.stdout.write("\bUnexpected 'leap' status <%s>\n" % leap)
+            sys.exit(1)
 
-        sys.stdout.write("\bUnexpected 'leap' status <%s>\n" % leap)
+        if verbose:
+            sys.stdout.write("\bNo!\nntpd did not synchronize.\n")
         sys.exit(1)
-
-    if verbose:
-        sys.stdout.write("\bNo!\nntpd did not synchronize.\n")
-    sys.exit(1)
+    except KeyboardInterrupt:
+        sys.stderr.write("Interrupted.\n")
+        sys.exit(2)
 
 # end



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/05e60a00adf16b86ae1e1f03e6093a3b956c72e0

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/05e60a00adf16b86ae1e1f03e6093a3b956c72e0
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170528/c086ebf4/attachment.html>


More information about the vc mailing list