Skip to content

Commit 78aead9

Browse files
committed
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>
1 parent 9857adb commit 78aead9

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/netlink.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,3 +1140,27 @@ void netlink_dump_addr_table(const bool v6)
11401140

11411141
nlmsg_free(msg);
11421142
}
1143+
1144+
/* Shut down and free netlink sockets/registration. Safe to call multiple times. */
1145+
void netlink_shutdown(void)
1146+
{
1147+
/* Deregister event and free the event socket */
1148+
if (rtnl_event.sock) {
1149+
odhcpd_deregister(&rtnl_event.ev);
1150+
1151+
if (rtnl_event.ev.uloop.fd >= 0) {
1152+
uloop_fd_delete(&rtnl_event.ev.uloop);
1153+
close(rtnl_event.ev.uloop.fd);
1154+
rtnl_event.ev.uloop.fd = -1;
1155+
}
1156+
1157+
nl_socket_free(rtnl_event.sock);
1158+
rtnl_event.sock = NULL;
1159+
}
1160+
1161+
/* Free the primary rtnl socket */
1162+
if (rtnl_socket) {
1163+
nl_socket_free(rtnl_socket);
1164+
rtnl_socket = NULL;
1165+
}
1166+
}

src/odhcpd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ int main(int argc, char **argv)
187187
if (dhcpv4_init())
188188
return 4;
189189
#endif
190+
atexit(router_shutdown);
191+
atexit(netlink_shutdown);
190192

191193
return odhcpd_run();
192194
}

src/odhcpd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ struct nl_sock;
9292
extern struct config config;
9393
extern struct sys_conf sys_conf;
9494

95+
/* Shutdown helpers */
96+
void netlink_shutdown(void);
97+
void router_shutdown(void);
98+
9599
void __iflog(int lvl, const char *fmt, ...);
96100
#define debug(fmt, ...) __iflog(LOG_DEBUG, fmt __VA_OPT__(, ) __VA_ARGS__)
97101
#define info(fmt, ...) __iflog(LOG_INFO, fmt __VA_OPT__(, ) __VA_ARGS__)

src/router.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ int router_init(void)
6161
}
6262

6363
out:
64-
if (ret < 0 && fp_route) {
64+
if (ret < 0)
65+
router_shutdown();
66+
67+
return ret;
68+
}
69+
70+
/* Shutdown helper: close fp_route if open. Safe to call multiple times. */
71+
void router_shutdown(void)
72+
{
73+
if (fp_route) {
6574
fclose(fp_route);
6675
fp_route = NULL;
6776
}
68-
69-
return ret;
7077
}
7178

7279

0 commit comments

Comments
 (0)