Require subscriptions to be renewed regularly. This way, the subscription
automatically times out when phc2sys is killed.

Signed-off-by: Jiri Benc <jb...@redhat.com>
---
 clock.c   |   24 ++++++++++++++++++++++++
 phc2sys.c |   10 ++++++----
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/clock.c b/clock.c
index c893a58391ce..d9ee6c4c77e2 100644
--- a/clock.c
+++ b/clock.c
@@ -68,6 +68,7 @@ struct clock_subscriber {
        uint8_t addr[TRANSPORT_RECV_ADDR_LEN];
        int addrlen;
        UInteger16 sequenceId;
+       time_t subscribed;
 };
 
 struct clock {
@@ -142,6 +143,7 @@ static void clock_update_subscription(struct clock *c, 
struct ptp_message *req,
        uint8_t addr[TRANSPORT_RECV_ADDR_LEN];
        int addrlen;
        struct clock_subscriber *s;
+       struct timespec now;
 
        addrlen = port_recv_addr(uds, addr);
        if (!addrlen) {
@@ -157,6 +159,8 @@ static void clock_update_subscription(struct clock *c, 
struct ptp_message *req,
                                memcpy(s->addr, addr, addrlen);
                                s->addrlen = addrlen;
                                s->events = events;
+                               clock_gettime(CLOCK_MONOTONIC, &now);
+                               s->subscribed = now.tv_sec;
                        } else {
                                remove_subscriber(s);
                        }
@@ -175,6 +179,8 @@ static void clock_update_subscription(struct clock *c, 
struct ptp_message *req,
        memcpy(s->addr, addr, addrlen);
        s->addrlen = addrlen;
        s->events = events;
+       clock_gettime(CLOCK_MONOTONIC, &now);
+       s->subscribed = now.tv_sec;
        s->sequenceId = 0;
        LIST_INSERT_HEAD(&c->subscribers, s, list);
 }
@@ -188,6 +194,23 @@ static void clock_flush_subscriptions(struct clock *c)
        }
 }
 
+static void clock_prune_subscriptions(struct clock *c)
+{
+       struct clock_subscriber *s, *tmp;
+       struct timespec now;
+
+       clock_gettime(CLOCK_MONOTONIC, &now);
+       LIST_FOREACH_SAFE(s, &c->subscribers, list, tmp) {
+               /* prune subscribers older than 3 minutes */
+               if (s->subscribed + 180 < now.tv_sec) {
+                       pr_info("subscriber %s timed out (not renewed in %ld 
seconds)",
+                               pid2str(&s->targetPortIdentity),
+                               now.tv_sec - s->subscribed);
+                       remove_subscriber(s);
+               }
+       }
+}
+
 void clock_send_notification(struct clock *c, struct ptp_message *msg,
                             int msglen, enum notification event)
 {
@@ -1165,6 +1188,7 @@ int clock_poll(struct clock *c)
        if (sde)
                handle_state_decision_event(c);
 
+       clock_prune_subscriptions(c);
        return 0;
 }
 
diff --git a/phc2sys.c b/phc2sys.c
index d130232f70de..72864484828f 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -109,7 +109,7 @@ struct node {
        struct clock *master;
 };
 
-static int update_sync_offset(struct node *node);
+static int update_pmc(struct node *node, int subscribe);
 static int clock_handle_leap(struct node *node, struct clock *clock,
                             int64_t offset, uint64_t ts, int do_leap);
 static int run_pmc_get_utc_offset(struct node *node, int timeout);
@@ -544,7 +544,7 @@ static int do_pps_loop(struct node *node, struct clock 
*clock, int fd)
                        pps_offset = pps_ts - phc_ts;
                }
 
-               do_leap = update_sync_offset(node);
+               do_leap = update_pmc(node, 0);
                if (do_leap < 0)
                        continue;
                update_clock(node, clock, pps_offset, pps_ts, -1, do_leap);
@@ -566,7 +566,7 @@ static int do_loop(struct node *node, int subscriptions)
 
        while (1) {
                clock_nanosleep(CLOCK_MONOTONIC, 0, &interval, NULL);
-               do_leap = update_sync_offset(node);
+               do_leap = update_pmc(node, subscriptions);
                if (do_leap < 0)
                        continue;
 
@@ -1007,7 +1007,7 @@ out:
 }
 
 /* Returns: -1 in case of error, 0 for normal sync, 1 to leap clock */
-static int update_sync_offset(struct node *node)
+static int update_pmc(struct node *node, int subscribe)
 {
        struct timespec tp;
        uint64_t ts;
@@ -1022,6 +1022,8 @@ static int update_sync_offset(struct node *node)
        if (node->pmc &&
            !(ts > node->pmc_last_update &&
              ts - node->pmc_last_update < PMC_UPDATE_INTERVAL)) {
+               if (subscribe)
+                       run_pmc_subscribe(node, 0);
                if (run_pmc_get_utc_offset(node, 0) > 0)
                        node->pmc_last_update = ts;
        }
-- 
1.7.6.5


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to