Skip to content

Commit 3cbbea8

Browse files
systemcrashNoltari
authored andcommitted
netlink: clean up sockets, close files
Sockets are handled for most usage paths, except for shutdown. Handle those at shutdown. Signed-off-by: Paul Donald <newtwen+github@gmail.com> Link: openwrt#367 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
1 parent 9857adb commit 3cbbea8

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/netlink.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ static struct event_socket rtnl_event = {
5353
.sock_bufsize = 133120,
5454
};
5555

56+
/* Shut down and free netlink sockets/registration. Safe to call multiple times. */
57+
static void netlink_shutdown(void)
58+
{
59+
/* Deregister event and free the event socket */
60+
if (rtnl_event.sock) {
61+
odhcpd_deregister(&rtnl_event.ev);
62+
63+
if (rtnl_event.ev.uloop.fd >= 0) {
64+
close(rtnl_event.ev.uloop.fd);
65+
rtnl_event.ev.uloop.fd = -1;
66+
}
67+
68+
nl_socket_free(rtnl_event.sock);
69+
rtnl_event.sock = NULL;
70+
}
71+
72+
/* Free the primary rtnl socket */
73+
if (rtnl_socket) {
74+
nl_socket_free(rtnl_socket);
75+
rtnl_socket = NULL;
76+
}
77+
}
78+
5679
int netlink_init(void)
5780
{
5881
rtnl_socket = create_socket(NETLINK_ROUTE);
@@ -85,19 +108,12 @@ int netlink_init(void)
85108

86109
odhcpd_register(&rtnl_event.ev);
87110

111+
atexit(netlink_shutdown);
112+
88113
return 0;
89114

90115
err:
91-
if (rtnl_socket) {
92-
nl_socket_free(rtnl_socket);
93-
rtnl_socket = NULL;
94-
}
95-
96-
if (rtnl_event.sock) {
97-
nl_socket_free(rtnl_event.sock);
98-
rtnl_event.sock = NULL;
99-
rtnl_event.ev.uloop.fd = -1;
100-
}
116+
netlink_shutdown();
101117

102118
return -1;
103119
}

src/router.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ static FILE *fp_route = NULL;
4545

4646
#define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
4747

48+
/* Shutdown helper: close fp_route if open. Safe to call multiple times. */
49+
static void router_shutdown(void)
50+
{
51+
if (fp_route) {
52+
fclose(fp_route);
53+
fp_route = NULL;
54+
}
55+
}
56+
4857
int router_init(void)
4958
{
5059
int ret = 0;
@@ -60,11 +69,11 @@ int router_init(void)
6069
ret = -1;
6170
}
6271

72+
atexit(router_shutdown);
73+
6374
out:
64-
if (ret < 0 && fp_route) {
65-
fclose(fp_route);
66-
fp_route = NULL;
67-
}
75+
if (ret < 0)
76+
router_shutdown();
6877

6978
return ret;
7079
}

0 commit comments

Comments
 (0)