lib: zclient.c remove extern struct thread_master *

zclient.c depended upon link time inclusion of a
extern struct thread_master *master.  This is a violation of the
namespace of the calling daemon.  If a library needs the pointer
pass it in and save it for future use.

This code change also makes the zclient code consistent with
the other lib functions that need to schedule work on your behalf

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
diff --git a/bgpd/bgp_nexthop.c b/bgpd/bgp_nexthop.c
index 20302e3..33de365 100644
--- a/bgpd/bgp_nexthop.c
+++ b/bgpd/bgp_nexthop.c
@@ -1402,7 +1402,7 @@
 void
 bgp_scan_init (void)
 {
-  zlookup = zclient_new ();
+  zlookup = zclient_new (master);
   zlookup->sock = -1;
   zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
 
diff --git a/bgpd/bgp_zebra.c b/bgpd/bgp_zebra.c
index 2616351..e534bee 100644
--- a/bgpd/bgp_zebra.c
+++ b/bgpd/bgp_zebra.c
@@ -1094,10 +1094,10 @@
 }
 
 void
-bgp_zebra_init (void)
+bgp_zebra_init (struct thread_master *master)
 {
   /* Set default values. */
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   zclient_init (zclient, ZEBRA_ROUTE_BGP);
   zclient->zebra_connected = bgp_zebra_connected;
   zclient->router_id_update = bgp_router_id_update;
diff --git a/bgpd/bgp_zebra.h b/bgpd/bgp_zebra.h
index 8099193..50f727d 100644
--- a/bgpd/bgp_zebra.h
+++ b/bgpd/bgp_zebra.h
@@ -25,7 +25,7 @@
 
 extern struct stream *bgp_nexthop_buf;
 
-extern void bgp_zebra_init (void);
+extern void bgp_zebra_init (struct thread_master *master);
 extern int bgp_if_update_all (void);
 extern int bgp_config_write_maxpaths (struct vty *, struct bgp *, afi_t,
 				      safi_t, int *);
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index 0068037..af8bdb4 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -5421,7 +5421,7 @@
   bgp_vty_init ();
 
   /* Init zebra. */
-  bgp_zebra_init ();
+  bgp_zebra_init (master);
 
   /* BGP inits. */
   bgp_attr_init ();
diff --git a/isisd/isis_main.c b/isisd/isis_main.c
index e1af71f..fba7b10 100644
--- a/isisd/isis_main.c
+++ b/isisd/isis_main.c
@@ -339,7 +339,7 @@
   /* create the global 'isis' instance */
   isis_new (1);
 
-  isis_zebra_init ();
+  isis_zebra_init (master);
 
   /* parse config file */
   /* this is needed three times! because we have interfaces before the areas */
diff --git a/isisd/isis_zebra.c b/isisd/isis_zebra.c
index 6d0c157..8a78417 100644
--- a/isisd/isis_zebra.c
+++ b/isisd/isis_zebra.c
@@ -602,9 +602,9 @@
 }
 
 void
-isis_zebra_init ()
+isis_zebra_init (struct thread_master *master)
 {
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   zclient_init (zclient, ZEBRA_ROUTE_ISIS);
   zclient->zebra_connected = isis_zebra_connected;
   zclient->router_id_update = isis_router_id_update_zebra;
diff --git a/isisd/isis_zebra.h b/isisd/isis_zebra.h
index 889cd9b..0011478 100644
--- a/isisd/isis_zebra.h
+++ b/isisd/isis_zebra.h
@@ -24,7 +24,7 @@
 
 extern struct zclient *zclient;
 
-void isis_zebra_init (void);
+void isis_zebra_init (struct thread_master *);
 void isis_zebra_route_update (struct prefix *prefix,
 			      struct isis_route_info *route_info);
 int isis_distribute_list_update (int routetype);
diff --git a/lib/zclient.c b/lib/zclient.c
index 0ce46fe..bfff9a3 100644
--- a/lib/zclient.c
+++ b/lib/zclient.c
@@ -39,8 +39,6 @@
 /* Prototype for event manager. */
 static void zclient_event (enum event, struct zclient *);
 
-extern struct thread_master *master;
-
 const char *zclient_serv_path = NULL;
 
 /* This file local debug flag. */
@@ -48,7 +46,7 @@
 
 /* Allocate zclient structure. */
 struct zclient *
-zclient_new ()
+zclient_new (struct thread_master *master)
 {
   struct zclient *zclient;
   zclient = XCALLOC (MTYPE_ZCLIENT, sizeof (struct zclient));
@@ -56,6 +54,7 @@
   zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
   zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
   zclient->wb = buffer_new(0);
+  zclient->master = master;
 
   return zclient;
 }
@@ -258,8 +257,8 @@
       return zclient_failed(zclient);
       break;
     case BUFFER_PENDING:
-      zclient->t_write = thread_add_write(master, zclient_flush_data,
-					  zclient, zclient->sock);
+      zclient->t_write = thread_add_write (zclient->master, zclient_flush_data,
+					   zclient, zclient->sock);
       break;
     case BUFFER_EMPTY:
       break;
@@ -284,8 +283,8 @@
       THREAD_OFF(zclient->t_write);
       break;
     case BUFFER_PENDING:
-      THREAD_WRITE_ON(master, zclient->t_write,
-		      zclient_flush_data, zclient, zclient->sock);
+      THREAD_WRITE_ON (zclient->master, zclient->t_write,
+		       zclient_flush_data, zclient, zclient->sock);
       break;
     }
   return 0;
@@ -1092,7 +1091,7 @@
     case ZCLIENT_SCHEDULE:
       if (! zclient->t_connect)
 	zclient->t_connect =
-	  thread_add_event (master, zclient_connect, zclient, 0);
+	  thread_add_event (zclient->master, zclient_connect, zclient, 0);
       break;
     case ZCLIENT_CONNECT:
       if (zclient->fail >= 10)
@@ -1102,12 +1101,12 @@
 		   zclient->fail < 3 ? 10 : 60);
       if (! zclient->t_connect)
 	zclient->t_connect = 
-	  thread_add_timer (master, zclient_connect, zclient,
+	  thread_add_timer (zclient->master, zclient_connect, zclient,
 			    zclient->fail < 3 ? 10 : 60);
       break;
     case ZCLIENT_READ:
       zclient->t_read = 
-	thread_add_read (master, zclient_read, zclient, zclient->sock);
+	thread_add_read (zclient->master, zclient_read, zclient, zclient->sock);
       break;
     }
 }
diff --git a/lib/zclient.h b/lib/zclient.h
index 3490b32..aa935c1 100644
--- a/lib/zclient.h
+++ b/lib/zclient.h
@@ -40,6 +40,9 @@
 /* Structure for the zebra client. */
 struct zclient
 {
+  /* The thread master we schedule ourselves on */
+  struct thread_master *master;
+
   /* Socket to zebra daemon. */
   int sock;
 
@@ -132,7 +135,7 @@
 };
 
 /* Prototypes of zebra client service functions. */
-extern struct zclient *zclient_new (void);
+extern struct zclient *zclient_new (struct thread_master *);
 extern void zclient_init (struct zclient *, int);
 extern int zclient_start (struct zclient *);
 extern void zclient_stop (struct zclient *);
diff --git a/ospf6d/ospf6_zebra.c b/ospf6d/ospf6_zebra.c
index 951e11d..d37e508 100644
--- a/ospf6d/ospf6_zebra.c
+++ b/ospf6d/ospf6_zebra.c
@@ -578,10 +578,10 @@
 }
 
 void
-ospf6_zebra_init (void)
+ospf6_zebra_init (struct thread_master *master)
 {
   /* Allocate zebra structure. */
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   zclient_init (zclient, ZEBRA_ROUTE_OSPF6);
   zclient->zebra_connected = ospf6_zebra_connected;
   zclient->router_id_update = ospf6_router_id_update_zebra;
diff --git a/ospf6d/ospf6_zebra.h b/ospf6d/ospf6_zebra.h
index a219450..05694d3 100644
--- a/ospf6d/ospf6_zebra.h
+++ b/ospf6d/ospf6_zebra.h
@@ -44,7 +44,7 @@
 extern void ospf6_zebra_no_redistribute (int);
 #define ospf6_zebra_is_redistribute(type) \
     vrf_bitmap_check (zclient->redist[type], VRF_DEFAULT)
-extern void ospf6_zebra_init (void);
+extern void ospf6_zebra_init(struct thread_master *);
 
 extern int config_write_ospf6_debug_zebra (struct vty *vty);
 extern void install_element_ospf6_debug_zebra (void);
diff --git a/ospf6d/ospf6d.c b/ospf6d/ospf6d.c
index 3cdd5c1..c2baa31 100644
--- a/ospf6d/ospf6d.c
+++ b/ospf6d/ospf6d.c
@@ -1764,7 +1764,7 @@
   ospf6_area_init ();
   ospf6_interface_init ();
   ospf6_neighbor_init ();
-  ospf6_zebra_init ();
+  ospf6_zebra_init (master);
 
   ospf6_lsa_init ();
   ospf6_spf_init ();
diff --git a/ospfd/ospf_main.c b/ospfd/ospf_main.c
index 826fc98..10565fe 100644
--- a/ospfd/ospf_main.c
+++ b/ospfd/ospf_main.c
@@ -298,7 +298,7 @@
 
   /* OSPFd inits. */
   ospf_if_init ();
-  ospf_zebra_init ();
+  ospf_zebra_init (master);
 
   /* OSPF vty inits. */
   ospf_vty_init ();
diff --git a/ospfd/ospf_zebra.c b/ospfd/ospf_zebra.c
index 588f0fb..cf2ea81 100644
--- a/ospfd/ospf_zebra.c
+++ b/ospfd/ospf_zebra.c
@@ -1304,10 +1304,10 @@
 }
 
 void
-ospf_zebra_init ()
+ospf_zebra_init (struct thread_master *master)
 {
   /* Allocate zebra structure. */
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   zclient_init (zclient, ZEBRA_ROUTE_OSPF);
   zclient->zebra_connected = ospf_zebra_connected;
   zclient->router_id_update = ospf_router_id_update_zebra;
diff --git a/ospfd/ospf_zebra.h b/ospfd/ospf_zebra.h
index 148f652..32a0271 100644
--- a/ospfd/ospf_zebra.h
+++ b/ospfd/ospf_zebra.h
@@ -72,7 +72,7 @@
 			      const char *, const char *);
 extern int ospf_distance_unset (struct vty *, struct ospf *, const char *,
 				const char *, const char *);
-extern void ospf_zebra_init (void);
+extern void ospf_zebra_init (struct thread_master *);
 
 #endif /* _ZEBRA_OSPF_ZEBRA_H */
 
diff --git a/pimd/pim_main.c b/pimd/pim_main.c
index 95c1816..5f4711e 100644
--- a/pimd/pim_main.c
+++ b/pimd/pim_main.c
@@ -205,7 +205,7 @@
   /*
    * Initialize zclient "update" and "lookup" sockets
    */
-  pim_zebra_init(zebra_sock_path);
+  pim_zebra_init (master, zebra_sock_path);
 
   zlog_notice("Loading configuration - begin");
 
diff --git a/pimd/pim_zebra.c b/pimd/pim_zebra.c
index dfc871b..1a39271 100644
--- a/pimd/pim_zebra.c
+++ b/pimd/pim_zebra.c
@@ -655,7 +655,7 @@
   zclient_send_requests(zclient, VRF_DEFAULT);
 }
 
-void pim_zebra_init(char *zebra_sock_path)
+void pim_zebra_init (struct thread_master *master, char *zebra_sock_path)
 {
   int i;
 
@@ -669,7 +669,7 @@
 #endif
 
   /* Socket for receiving updates from Zebra daemon */
-  qpim_zclient_update = zclient_new();
+  qpim_zclient_update = zclient_new (master);
 
   qpim_zclient_update->zebra_connected          = pim_zebra_connected;
   qpim_zclient_update->router_id_update         = pim_router_id_update_zebra;
diff --git a/pimd/pim_zebra.h b/pimd/pim_zebra.h
index d624c86..af5baef 100644
--- a/pimd/pim_zebra.h
+++ b/pimd/pim_zebra.h
@@ -26,7 +26,7 @@
 #include "pim_igmp.h"
 #include "pim_ifchannel.h"
 
-void pim_zebra_init(char *zebra_sock_path);
+void pim_zebra_init (struct thread_master *master, char *zebra_sock_path);
 
 void pim_scan_oil(void);
 
diff --git a/pimd/pim_zlookup.c b/pimd/pim_zlookup.c
index 67896d9..297a2a8 100644
--- a/pimd/pim_zlookup.c
+++ b/pimd/pim_zlookup.c
@@ -122,7 +122,7 @@
 {
   struct zclient *zlookup;
 
-  zlookup = zclient_new();
+  zlookup = zclient_new (master);
   if (!zlookup) {
     zlog_err("%s: zclient_new() failure",
 	     __PRETTY_FUNCTION__);
diff --git a/ripd/rip_main.c b/ripd/rip_main.c
index 95b1f6d..4ead9b0 100644
--- a/ripd/rip_main.c
+++ b/ripd/rip_main.c
@@ -286,7 +286,7 @@
   /* RIP related initialization. */
   rip_init ();
   rip_if_init ();
-  rip_zclient_init ();
+  rip_zclient_init (master);
   rip_peer_init ();
 
   /* Get configuration file. */
diff --git a/ripd/rip_zebra.c b/ripd/rip_zebra.c
index de98162..0b6c22a 100644
--- a/ripd/rip_zebra.c
+++ b/ripd/rip_zebra.c
@@ -710,10 +710,10 @@
 }
 
 void
-rip_zclient_init ()
+rip_zclient_init (struct thread_master *master)
 {
   /* Set default value to the zebra client structure. */
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   zclient_init (zclient, ZEBRA_ROUTE_RIP);
   zclient->zebra_connected = rip_zebra_connected;
   zclient->interface_add = rip_interface_add;
diff --git a/ripd/ripd.h b/ripd/ripd.h
index 4f40e79..a768ccc 100644
--- a/ripd/ripd.h
+++ b/ripd/ripd.h
@@ -389,7 +389,7 @@
 extern void rip_route_map_init (void);
 extern void rip_route_map_reset (void);
 extern void rip_snmp_init (void);
-extern void rip_zclient_init (void);
+extern void rip_zclient_init (struct thread_master *);
 extern void rip_zclient_start (void);
 extern void rip_zclient_reset (void);
 extern void rip_offset_init (void);
diff --git a/ripngd/ripng_main.c b/ripngd/ripng_main.c
index d8f2241..1c184e2 100644
--- a/ripngd/ripng_main.c
+++ b/ripngd/ripng_main.c
@@ -281,7 +281,7 @@
 
   /* RIPngd inits. */
   ripng_init ();
-  zebra_init ();
+  zebra_init (master);
   ripng_peer_init ();
 
   /* Get configuration file. */
diff --git a/ripngd/ripng_zebra.c b/ripngd/ripng_zebra.c
index 58f8860..13b1853 100644
--- a/ripngd/ripng_zebra.c
+++ b/ripngd/ripng_zebra.c
@@ -541,10 +541,10 @@
 
 /* Initialize zebra structure and it's commands. */
 void
-zebra_init ()
+zebra_init (struct thread_master *master)
 {
   /* Allocate zebra structure. */
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   zclient_init (zclient, ZEBRA_ROUTE_RIPNG);
 
   zclient->zebra_connected = ripng_zebra_connected;
diff --git a/ripngd/ripngd.h b/ripngd/ripngd.h
index 28ca41b..6cbbd84 100644
--- a/ripngd/ripngd.h
+++ b/ripngd/ripngd.h
@@ -358,7 +358,7 @@
 extern void ripng_route_map_reset (void);
 extern void ripng_terminate (void);
  /* zclient_init() is done by ripng_zebra.c:zebra_init() */
-extern void zebra_init (void);
+extern void zebra_init (struct thread_master *);
 extern void ripng_zclient_start (void);
 extern void ripng_zclient_reset (void);
 extern void ripng_offset_init (void);
diff --git a/tests/bgp_mpath_test.c b/tests/bgp_mpath_test.c
index 3594753..ed54d9c 100644
--- a/tests/bgp_mpath_test.c
+++ b/tests/bgp_mpath_test.c
@@ -376,7 +376,7 @@
 global_test_init (void)
 {
   master = thread_master_create ();
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   bgp_master_init ();
   bgp_option_set (BGP_OPT_NO_LISTEN);
   
diff --git a/zebra/client_main.c b/zebra/client_main.c
index 06afc56..43ab299 100644
--- a/zebra/client_main.c
+++ b/zebra/client_main.c
@@ -193,13 +193,15 @@
 int
 main (int argc, char **argv)
 {
+  struct thread_master *master;
   FILE *fp;
 
   if (argc == 1)
       usage_exit ();
 
+  master = thread_master_create ();
   /* Establish connection to zebra. */
-  zclient = zclient_new ();
+  zclient = zclient_new (master);
   zclient->enable = 1;
 #ifdef HAVE_TCP_ZEBRA
   zclient->sock = zclient_socket ();