File tree Expand file tree Collapse file tree 2 files changed +39
-14
lines changed
Expand file tree Collapse file tree 2 files changed +39
-14
lines changed Original file line number Diff line number Diff 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+
5679int 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
90115err :
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}
Original file line number Diff line number Diff 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+
4857int 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+
6374out :
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}
You can’t perform that action at this time.
0 commit comments