[PATCH] Remove source3/lib/events.c dependency in nmbd
Jeremy Allison
jra at samba.org
Fri Sep 23 19:46:28 UTC 2016
On Fri, Sep 23, 2016 at 11:55:11AM -0700, Jeremy Allison wrote:
> Is attached. Dirty, but seems to work. Now to try
> and figure out how to split it up.. :-).
OK, here's the split-up micro-commit version.
Passes make test here ! Please review and push
if you're happy.
Cheers,
Jeremy.
-------------- next part --------------
>From 428afb9b1ce3c61fa009c83f29595140318be7e7 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Fri, 23 Sep 2016 12:05:59 -0700
Subject: [PATCH 1/7] s3: nmbd: Add fd, triggered elements to struct
socket_attributes.
Zero the attrs array on allocation, and mirror the fd's.
This will allow us to eventually remove source3/lib/events.c
dependency and make nmbd purely tevent based.
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/nmbd/nmbd_packets.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c
index c9a2dc7..619cc9a 100644
--- a/source3/nmbd/nmbd_packets.c
+++ b/source3/nmbd/nmbd_packets.c
@@ -1683,6 +1683,8 @@ on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_
struct socket_attributes {
enum packet_type type;
bool broadcast;
+ int fd;
+ bool triggered;
};
static bool create_listen_pollfds(struct pollfd **pfds,
@@ -1723,7 +1725,7 @@ static bool create_listen_pollfds(struct pollfd **pfds,
return true;
}
- attrs = talloc_array(NULL, struct socket_attributes, count);
+ attrs = talloc_zero_array(NULL, struct socket_attributes, count);
if (attrs == NULL) {
DEBUG(1, ("create_listen_pollfds: malloc fail for attrs. "
"size %d\n", count));
@@ -1734,11 +1736,13 @@ static bool create_listen_pollfds(struct pollfd **pfds,
num = 0;
fds[num].fd = ClientNMB;
+ attrs[num].fd = ClientNMB;
attrs[num].type = NMB_PACKET;
attrs[num].broadcast = false;
num += 1;
fds[num].fd = ClientDGRAM;
+ attrs[num].fd = ClientDGRAM;
attrs[num].type = DGRAM_PACKET;
attrs[num].broadcast = false;
num += 1;
@@ -1747,6 +1751,7 @@ static bool create_listen_pollfds(struct pollfd **pfds,
if (subrec->nmb_sock != -1) {
fds[num].fd = subrec->nmb_sock;
+ attrs[num].fd = subrec->nmb_sock;
attrs[num].type = NMB_PACKET;
attrs[num].broadcast = false;
num += 1;
@@ -1754,6 +1759,7 @@ static bool create_listen_pollfds(struct pollfd **pfds,
if (subrec->nmb_bcast != -1) {
fds[num].fd = subrec->nmb_bcast;
+ attrs[num].fd = subrec->nmb_bcast;
attrs[num].type = NMB_PACKET;
attrs[num].broadcast = true;
num += 1;
@@ -1761,6 +1767,7 @@ static bool create_listen_pollfds(struct pollfd **pfds,
if (subrec->dgram_sock != -1) {
fds[num].fd = subrec->dgram_sock;
+ attrs[num].fd = subrec->dgram_sock;
attrs[num].type = DGRAM_PACKET;
attrs[num].broadcast = false;
num += 1;
@@ -1768,6 +1775,7 @@ static bool create_listen_pollfds(struct pollfd **pfds,
if (subrec->dgram_bcast != -1) {
fds[num].fd = subrec->dgram_bcast;
+ attrs[num].fd = subrec->dgram_bcast;
attrs[num].type = DGRAM_PACKET;
attrs[num].broadcast = true;
num += 1;
--
2.7.4
>From 0f4aa12e4fa0b04c0a86eb4902ab093cf8d08ac8 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Fri, 23 Sep 2016 12:12:43 -0700
Subject: [PATCH 2/7] s3: nmbd: Ensure attrs array mirrors fd's array for dns.
This will allow us to eventually remove source3/lib/events.c
dependency and make nmbd purely tevent based.
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/nmbd/nmbd_packets.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c
index 619cc9a..2648679 100644
--- a/source3/nmbd/nmbd_packets.c
+++ b/source3/nmbd/nmbd_packets.c
@@ -1919,8 +1919,23 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
if (fds == NULL) {
return true;
}
+ attrs = talloc_realloc(NULL,
+ attrs,
+ struct socket_attributes,
+ num_sockets + 1);
+ if (attrs == NULL) {
+ TALLOC_FREE(fds);
+ return true;
+ }
dns_pollidx = num_sockets;
fds[num_sockets].fd = dns_fd;
+ attrs[dns_pollidx].fd = dns_fd;
+ /*
+ * dummy values, we only need
+ * fd and triggered.
+ */
+ attrs[dns_pollidx].type = NMB_PACKET;
+ attrs[dns_pollidx].broadcast = false;
num_sockets += 1;
}
#endif
--
2.7.4
>From 48389ff640724a28beee1d6edaf6c4031934665a Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Fri, 23 Sep 2016 12:16:58 -0700
Subject: [PATCH 3/7] s3: nmbd: Now attrs array mirrors fd's array use it in
preference.
This will allow us to eventually remove source3/lib/events.c
dependency and make nmbd purely tevent based.
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/nmbd/nmbd_packets.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c
index 2648679..7321922 100644
--- a/source3/nmbd/nmbd_packets.c
+++ b/source3/nmbd/nmbd_packets.c
@@ -2004,7 +2004,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
client_port = DGRAM_PORT;
}
- packet = read_packet(fds[i].fd, packet_type);
+ packet = read_packet(attrs[i].fd, packet_type);
if (!packet) {
continue;
}
@@ -2014,7 +2014,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
* only is set then check it came from one of our local nets.
*/
if (lp_bind_interfaces_only() &&
- (fds[i].fd == client_fd) &&
+ (attrs[i].fd == client_fd) &&
(!is_local_net_v4(packet->ip))) {
DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n",
packet_name, inet_ntoa(packet->ip), packet->port));
@@ -2053,10 +2053,10 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
if (attrs[i].broadcast) {
/* this is a broadcast socket */
- packet->send_fd = fds[i-1].fd;
+ packet->send_fd = attrs[i-1].fd;
} else {
/* this is already a unicast socket */
- packet->send_fd = fds[i].fd;
+ packet->send_fd = attrs[i].fd;
}
queue_packet(packet);
--
2.7.4
>From aa6e1c1910adecc882a91a5b0e03542710c9937d Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Fri, 23 Sep 2016 12:18:37 -0700
Subject: [PATCH 4/7] s3: nmbd: Add (currently unused) timeout and fd handlers.
This will allow us to eventually remove source3/lib/events.c
dependency and make nmbd purely tevent based.
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/nmbd/nmbd_packets.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c
index 7321922..3d2c4c7 100644
--- a/source3/nmbd/nmbd_packets.c
+++ b/source3/nmbd/nmbd_packets.c
@@ -1872,6 +1872,33 @@ static void free_processed_packet_list(struct processed_packet **pp_processed_pa
}
/****************************************************************************
+ Timeout callback - just notice we timed out.
+***************************************************************************/
+
+static void nmbd_timeout_handler(struct tevent_context *ev,
+ struct tevent_timer *te,
+ struct timeval current_time,
+ void *private_data)
+{
+ bool *got_timeout = (bool *)private_data;
+ *got_timeout = true;
+}
+
+/****************************************************************************
+ fd callback - remember the fd that triggered.
+***************************************************************************/
+
+static void nmbd_fd_handler(struct tevent_context *ev,
+ struct tevent_fd *fde,
+ uint16_t flags,
+ void *private_data)
+{
+ struct socket_attributes *attr =
+ (struct socket_attributes *)private_data;
+ attr->triggered = true;
+}
+
+/****************************************************************************
Listens for NMB or DGRAM packets, and queues them.
return True if the socket is dead
***************************************************************************/
--
2.7.4
>From cf65d75bc22deb71a905a684067fd7e4944fc339 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Fri, 23 Sep 2016 12:22:53 -0700
Subject: [PATCH 5/7] s3: nmbd: Add a talloc_stackframe().
We will use this to create real tevent timer and fd
events.
This will allow us to eventually remove source3/lib/events.c
dependency and make nmbd purely tevent based.
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/nmbd/nmbd_packets.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c
index 3d2c4c7..0bc1ffe 100644
--- a/source3/nmbd/nmbd_packets.c
+++ b/source3/nmbd/nmbd_packets.c
@@ -1918,10 +1918,12 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
int dns_pollidx = -1;
#endif
struct processed_packet *processed_packet_list = NULL;
+ TALLOC_CTX *frame = talloc_stackframe();
if ((fds == NULL) || rescan_listen_set) {
if (create_listen_pollfds(&fds, &attrs, &listen_number)) {
DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
+ TALLOC_FREE(frame);
return True;
}
rescan_listen_set = False;
@@ -1935,6 +1937,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
fds = talloc_realloc(NULL, fds, struct pollfd, listen_number);
if (fds == NULL) {
+ TALLOC_FREE(frame);
return true;
}
num_sockets = listen_number;
@@ -1944,6 +1947,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
if (dns_fd != -1) {
fds = talloc_realloc(NULL, fds, struct pollfd, num_sockets+1);
if (fds == NULL) {
+ TALLOC_FREE(frame);
return true;
}
attrs = talloc_realloc(NULL,
@@ -1952,6 +1956,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
num_sockets + 1);
if (attrs == NULL) {
TALLOC_FREE(fds);
+ TALLOC_FREE(frame);
return true;
}
dns_pollidx = num_sockets;
@@ -1973,6 +1978,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
/* Process a signal and timer events now... */
if (run_events_poll(nmbd_event_context(), 0, NULL, 0)) {
+ TALLOC_FREE(frame);
return False;
}
@@ -1992,10 +1998,12 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
pollrtn = poll(fds, num_sockets, timeout);
if (run_events_poll(nmbd_event_context(), pollrtn, fds, num_sockets)) {
+ TALLOC_FREE(frame);
return False;
}
if (pollrtn == -1) {
+ TALLOC_FREE(frame);
return False;
}
@@ -2090,6 +2098,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
}
free_processed_packet_list(&processed_packet_list);
+ TALLOC_FREE(frame);
return False;
}
--
2.7.4
>From 4c5e4c608de4fd644525ae75b1b377135c5c9c52 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Fri, 23 Sep 2016 12:31:00 -0700
Subject: [PATCH 6/7] s3: nmbd: Change over to using tevent functions from
direct poll.
This will allow us to eventually remove source3/lib/events.c
dependency and make nmbd purely tevent based.
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/nmbd/nmbd_packets.c | 58 +++++++++++++++++++++++++++++++--------------
1 file changed, 40 insertions(+), 18 deletions(-)
diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c
index 0bc1ffe..bf6d86d 100644
--- a/source3/nmbd/nmbd_packets.c
+++ b/source3/nmbd/nmbd_packets.c
@@ -1910,6 +1910,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
static int listen_number = 0;
int num_sockets;
int i;
+ int loop_rtn;
int pollrtn;
int timeout;
@@ -1918,6 +1919,9 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
int dns_pollidx = -1;
#endif
struct processed_packet *processed_packet_list = NULL;
+ struct timeval tv;
+ struct tevent_timer *te = NULL;
+ bool got_timeout = false;
TALLOC_CTX *frame = talloc_stackframe();
if ((fds == NULL) || rescan_listen_set) {
@@ -1973,13 +1977,17 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
#endif
for (i=0; i<num_sockets; i++) {
- fds[i].events = POLLIN|POLLHUP;
- }
-
- /* Process a signal and timer events now... */
- if (run_events_poll(nmbd_event_context(), 0, NULL, 0)) {
- TALLOC_FREE(frame);
- return False;
+ struct tevent_fd *tfd = tevent_add_fd(nmbd_event_context(),
+ frame,
+ attrs[i].fd,
+ TEVENT_FD_READ,
+ nmbd_fd_handler,
+ &attrs[i]);
+ if (tfd == NULL) {
+ TALLOC_FREE(frame);
+ return true;
+ }
+ attrs[i].triggered = false;
}
/*
@@ -1989,28 +1997,42 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
* the time we are expecting the next netbios packet.
*/
- timeout = ((run_election||num_response_packets)
- ? 1 : NMBD_SELECT_LOOP) * 1000;
+ if (run_election||num_response_packets) {
+ /* One second. */
+ tv = tevent_timeval_current_ofs(1, 0);
+ } else {
+ /* NMBD_SELECT_LOOP seconds. */
+ tv = tevent_timeval_current_ofs(NMBD_SELECT_LOOP, 0);
+ }
- event_add_to_poll_args(nmbd_event_context(), NULL,
- &fds, &num_sockets, &timeout);
+ te = tevent_add_timer(nmbd_event_context(),
+ frame,
+ tv,
+ nmbd_timeout_handler,
+ &got_timeout);
+ if (te == NULL) {
+ TALLOC_FREE(frame);
+ return true;
+ }
- pollrtn = poll(fds, num_sockets, timeout);
+ loop_rtn = tevent_loop_once(nmbd_event_context());
- if (run_events_poll(nmbd_event_context(), pollrtn, fds, num_sockets)) {
+ if (loop_rtn == -1) {
TALLOC_FREE(frame);
- return False;
+ return true;
}
- if (pollrtn == -1) {
+ if (got_timeout) {
TALLOC_FREE(frame);
- return False;
+ return false;
}
#ifndef SYNC_DNS
if ((dns_fd != -1) && (dns_pollidx != -1) &&
- (fds[dns_pollidx].revents & (POLLIN|POLLHUP|POLLERR))) {
+ attrs[dns_pollidx].triggered){
run_dns_queue(msg);
+ TALLOC_FREE(frame);
+ return false;
}
#endif
@@ -2021,7 +2043,7 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
int client_fd;
int client_port;
- if ((fds[i].revents & (POLLIN|POLLHUP|POLLERR)) == 0) {
+ if (attrs[i].triggered == false) {
continue;
}
--
2.7.4
>From 90cb960e3b74975a387783d16fb87bd6ae4e4b68 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra at samba.org>
Date: Fri, 23 Sep 2016 12:37:52 -0700
Subject: [PATCH 7/7] s3: nmbd: Final changeover to stock tevent for nmbd.
Removes unused references to fds array used for (removed)
poll call. Renames create_listen_pollfds() to
create_listen_array().
Signed-off-by: Jeremy Allison <jra at samba.org>
---
source3/nmbd/nmbd_packets.c | 48 ++++-----------------------------------------
1 file changed, 4 insertions(+), 44 deletions(-)
diff --git a/source3/nmbd/nmbd_packets.c b/source3/nmbd/nmbd_packets.c
index bf6d86d..dc434f3 100644
--- a/source3/nmbd/nmbd_packets.c
+++ b/source3/nmbd/nmbd_packets.c
@@ -1687,14 +1687,12 @@ struct socket_attributes {
bool triggered;
};
-static bool create_listen_pollfds(struct pollfd **pfds,
- struct socket_attributes **pattrs,
+static bool create_listen_array(struct socket_attributes **pattrs,
int *pnum_sockets)
{
struct subnet_record *subrec = NULL;
int count = 0;
int num = 0;
- struct pollfd *fds;
struct socket_attributes *attrs;
/* The ClientNMB and ClientDGRAM sockets */
@@ -1718,30 +1716,20 @@ static bool create_listen_pollfds(struct pollfd **pfds,
}
}
- fds = talloc_zero_array(NULL, struct pollfd, count);
- if (fds == NULL) {
- DEBUG(1, ("create_listen_pollfds: malloc fail for fds. "
- "size %d\n", count));
- return true;
- }
-
attrs = talloc_zero_array(NULL, struct socket_attributes, count);
if (attrs == NULL) {
- DEBUG(1, ("create_listen_pollfds: malloc fail for attrs. "
+ DEBUG(1, ("talloc fail for attrs. "
"size %d\n", count));
- TALLOC_FREE(fds);
return true;
}
num = 0;
- fds[num].fd = ClientNMB;
attrs[num].fd = ClientNMB;
attrs[num].type = NMB_PACKET;
attrs[num].broadcast = false;
num += 1;
- fds[num].fd = ClientDGRAM;
attrs[num].fd = ClientDGRAM;
attrs[num].type = DGRAM_PACKET;
attrs[num].broadcast = false;
@@ -1750,7 +1738,6 @@ static bool create_listen_pollfds(struct pollfd **pfds,
for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
if (subrec->nmb_sock != -1) {
- fds[num].fd = subrec->nmb_sock;
attrs[num].fd = subrec->nmb_sock;
attrs[num].type = NMB_PACKET;
attrs[num].broadcast = false;
@@ -1758,7 +1745,6 @@ static bool create_listen_pollfds(struct pollfd **pfds,
}
if (subrec->nmb_bcast != -1) {
- fds[num].fd = subrec->nmb_bcast;
attrs[num].fd = subrec->nmb_bcast;
attrs[num].type = NMB_PACKET;
attrs[num].broadcast = true;
@@ -1766,7 +1752,6 @@ static bool create_listen_pollfds(struct pollfd **pfds,
}
if (subrec->dgram_sock != -1) {
- fds[num].fd = subrec->dgram_sock;
attrs[num].fd = subrec->dgram_sock;
attrs[num].type = DGRAM_PACKET;
attrs[num].broadcast = false;
@@ -1774,7 +1759,6 @@ static bool create_listen_pollfds(struct pollfd **pfds,
}
if (subrec->dgram_bcast != -1) {
- fds[num].fd = subrec->dgram_bcast;
attrs[num].fd = subrec->dgram_bcast;
attrs[num].type = DGRAM_PACKET;
attrs[num].broadcast = true;
@@ -1782,9 +1766,6 @@ static bool create_listen_pollfds(struct pollfd **pfds,
}
}
- TALLOC_FREE(*pfds);
- *pfds = fds;
-
TALLOC_FREE(*pattrs);
*pattrs = attrs;
@@ -1905,15 +1886,12 @@ static void nmbd_fd_handler(struct tevent_context *ev,
bool listen_for_packets(struct messaging_context *msg, bool run_election)
{
- static struct pollfd *fds = NULL;
static struct socket_attributes *attrs = NULL;
static int listen_number = 0;
int num_sockets;
int i;
int loop_rtn;
- int pollrtn;
- int timeout;
#ifndef SYNC_DNS
int dns_fd;
int dns_pollidx = -1;
@@ -1924,8 +1902,8 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
bool got_timeout = false;
TALLOC_CTX *frame = talloc_stackframe();
- if ((fds == NULL) || rescan_listen_set) {
- if (create_listen_pollfds(&fds, &attrs, &listen_number)) {
+ if ((attrs == NULL) || rescan_listen_set) {
+ if (create_listen_array(&attrs, &listen_number)) {
DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
TALLOC_FREE(frame);
return True;
@@ -1933,38 +1911,20 @@ bool listen_for_packets(struct messaging_context *msg, bool run_election)
rescan_listen_set = False;
}
- /*
- * "fds" can be enlarged by event_add_to_poll_args
- * below. Shrink it again to what was given to us by
- * create_listen_pollfds.
- */
-
- fds = talloc_realloc(NULL, fds, struct pollfd, listen_number);
- if (fds == NULL) {
- TALLOC_FREE(frame);
- return true;
- }
num_sockets = listen_number;
#ifndef SYNC_DNS
dns_fd = asyncdns_fd();
if (dns_fd != -1) {
- fds = talloc_realloc(NULL, fds, struct pollfd, num_sockets+1);
- if (fds == NULL) {
- TALLOC_FREE(frame);
- return true;
- }
attrs = talloc_realloc(NULL,
attrs,
struct socket_attributes,
num_sockets + 1);
if (attrs == NULL) {
- TALLOC_FREE(fds);
TALLOC_FREE(frame);
return true;
}
dns_pollidx = num_sockets;
- fds[num_sockets].fd = dns_fd;
attrs[dns_pollidx].fd = dns_fd;
/*
* dummy values, we only need
--
2.7.4
More information about the samba-technical
mailing list