Skip to content

Commit

Permalink
only call WSACleanup once
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Feb 5, 2024
1 parent 064045a commit 2da2949
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 80 deletions.
6 changes: 6 additions & 0 deletions include/private/config/have_winsock2.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
void
winsock2_close_network_target( const struct network_target *target );

/**
* TODO
*/
void
winsock2_free_all( void );

struct network_target *
winsock2_init_network_target( struct network_target *target );

Expand Down
160 changes: 82 additions & 78 deletions include/private/config/wrapper/network_supported.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,91 +17,95 @@
*/

#ifndef __STUMPLESS_PRIVATE_CONFIG_WRAPPER_NETWORK_SUPPORTED_H
# define __STUMPLESS_PRIVATE_CONFIG_WRAPPER_NETWORK_SUPPORTED_H
#define __STUMPLESS_PRIVATE_CONFIG_WRAPPER_NETWORK_SUPPORTED_H

# include <stumpless/config.h>
# include "private/config.h"
#include <stumpless/config.h>
#include "private/config.h"

# ifndef HAVE_SYS_SOCKET_H
# ifdef HAVE_WINSOCK2_H
# include "private/windows_wrapper.h"
# endif

#ifndef HAVE_SYS_SOCKET_H
# ifdef HAVE_WINSOCK2_H
# include "private/windows_wrapper.h"
# endif
#endif


/* definition of network target functions */
# ifdef HAVE_SYS_SOCKET_H
# define config_socket_handle_t int
# include "private/config/have_sys_socket.h"
# define config_close_tcp4_target sys_socket_close_network_target
# define config_close_tcp6_target sys_socket_close_network_target
# define config_close_udp4_target sys_socket_close_network_target
# define config_close_udp6_target sys_socket_close_network_target
# define config_init_tcp4 sys_socket_init_network_target
# define config_init_tcp6 sys_socket_init_network_target
# define config_init_udp4 sys_socket_init_network_target
# define config_init_udp6 sys_socket_init_network_target
# define config_open_tcp4_target sys_socket_open_tcp4_target
# define config_open_tcp6_target sys_socket_open_tcp6_target
# define config_open_udp4_target sys_socket_open_udp4_target
# define config_open_udp6_target sys_socket_open_udp6_target
# define config_reopen_tcp4_target sys_socket_reopen_tcp4_target
# define config_reopen_tcp6_target sys_socket_reopen_tcp6_target
# define config_reopen_udp4_target sys_socket_reopen_udp4_target
# define config_reopen_udp6_target sys_socket_reopen_udp6_target
# define config_sendto_tcp4_target sys_socket_sendto_tcp_target
# define config_sendto_tcp6_target sys_socket_sendto_tcp_target
# define config_sendto_udp4_target sys_socket_sendto_udp_target
# define config_sendto_udp6_target sys_socket_sendto_udp_target
# define config_tcp4_is_open sys_socket_network_target_is_open
# define config_tcp6_is_open sys_socket_network_target_is_open
# define config_udp4_is_open sys_socket_network_target_is_open
# define config_udp6_is_open sys_socket_network_target_is_open
# elif HAVE_WINSOCK2_H
# define config_socket_handle_t SOCKET
# include "private/config/have_winsock2.h"
# define config_close_tcp4_target winsock2_close_network_target
# define config_close_tcp6_target winsock2_close_network_target
# define config_close_udp4_target winsock2_close_network_target
# define config_close_udp6_target winsock2_close_network_target
# define config_init_tcp4 winsock2_init_network_target
# define config_init_tcp6 winsock2_init_network_target
# define config_init_udp4 winsock2_init_network_target
# define config_init_udp6 winsock2_init_network_target
# define config_open_tcp4_target winsock2_open_tcp4_target
# define config_open_tcp6_target winsock2_open_tcp6_target
# define config_open_udp4_target winsock2_open_udp4_target
# define config_open_udp6_target winsock2_open_udp6_target
# define config_reopen_tcp4_target winsock2_reopen_tcp4_target
# define config_reopen_tcp6_target winsock2_reopen_tcp6_target
# define config_reopen_udp4_target winsock2_reopen_udp4_target
# define config_reopen_udp6_target winsock2_reopen_udp6_target
# define config_sendto_tcp4_target winsock2_sendto_tcp_target
# define config_sendto_tcp6_target winsock2_sendto_tcp_target
# define config_sendto_udp4_target winsock2_sendto_udp_target
# define config_sendto_udp6_target winsock2_sendto_udp_target
# define config_tcp4_is_open winsock2_network_target_is_open
# define config_tcp6_is_open winsock2_network_target_is_open
# define config_udp4_is_open winsock2_network_target_is_open
# define config_udp6_is_open winsock2_network_target_is_open
# endif
#ifdef HAVE_SYS_SOCKET_H
# define config_socket_handle_t int
# include "private/config/have_sys_socket.h"
# define config_close_tcp4_target sys_socket_close_network_target
# define config_close_tcp6_target sys_socket_close_network_target
# define config_close_udp4_target sys_socket_close_network_target
# define config_close_udp6_target sys_socket_close_network_target
# define config_init_tcp4 sys_socket_init_network_target
# define config_init_tcp6 sys_socket_init_network_target
# define config_init_udp4 sys_socket_init_network_target
# define config_init_udp6 sys_socket_init_network_target
# define config_network_provider_free_all ( ( void ) 0 )
# define config_open_tcp4_target sys_socket_open_tcp4_target
# define config_open_tcp6_target sys_socket_open_tcp6_target
# define config_open_udp4_target sys_socket_open_udp4_target
# define config_open_udp6_target sys_socket_open_udp6_target
# define config_reopen_tcp4_target sys_socket_reopen_tcp4_target
# define config_reopen_tcp6_target sys_socket_reopen_tcp6_target
# define config_reopen_udp4_target sys_socket_reopen_udp4_target
# define config_reopen_udp6_target sys_socket_reopen_udp6_target
# define config_sendto_tcp4_target sys_socket_sendto_tcp_target
# define config_sendto_tcp6_target sys_socket_sendto_tcp_target
# define config_sendto_udp4_target sys_socket_sendto_udp_target
# define config_sendto_udp6_target sys_socket_sendto_udp_target
# define config_tcp4_is_open sys_socket_network_target_is_open
# define config_tcp6_is_open sys_socket_network_target_is_open
# define config_udp4_is_open sys_socket_network_target_is_open
# define config_udp6_is_open sys_socket_network_target_is_open
#elif HAVE_WINSOCK2_H
# define config_socket_handle_t SOCKET
# include "private/config/have_winsock2.h"
# define config_close_tcp4_target winsock2_close_network_target
# define config_close_tcp6_target winsock2_close_network_target
# define config_close_udp4_target winsock2_close_network_target
# define config_close_udp6_target winsock2_close_network_target
# define config_init_tcp4 winsock2_init_network_target
# define config_init_tcp6 winsock2_init_network_target
# define config_init_udp4 winsock2_init_network_target
# define config_init_udp6 winsock2_init_network_target
# define config_network_provider_free_all winsock2_free_all
# define config_open_tcp4_target winsock2_open_tcp4_target
# define config_open_tcp6_target winsock2_open_tcp6_target
# define config_open_udp4_target winsock2_open_udp4_target
# define config_open_udp6_target winsock2_open_udp6_target
# define config_reopen_tcp4_target winsock2_reopen_tcp4_target
# define config_reopen_tcp6_target winsock2_reopen_tcp6_target
# define config_reopen_udp4_target winsock2_reopen_udp4_target
# define config_reopen_udp6_target winsock2_reopen_udp6_target
# define config_sendto_tcp4_target winsock2_sendto_tcp_target
# define config_sendto_tcp6_target winsock2_sendto_tcp_target
# define config_sendto_udp4_target winsock2_sendto_udp_target
# define config_sendto_udp6_target winsock2_sendto_udp_target
# define config_tcp4_is_open winsock2_network_target_is_open
# define config_tcp6_is_open winsock2_network_target_is_open
# define config_udp4_is_open winsock2_network_target_is_open
# define config_udp6_is_open winsock2_network_target_is_open
#endif


/* definition of network target support */
# ifdef STUMPLESS_NETWORK_TARGETS_SUPPORTED
# include <stumpless/target/network.h>
# include "private/target/network.h"
# define config_close_network_target stumpless_close_network_target
# define config_network_free_all network_free_all
# define config_network_target_is_open network_target_is_open
# define config_open_network_target open_network_target
# define config_sendto_network_target sendto_network_target
# else
# include "private/target.h"
# define config_close_network_target close_unsupported_target
# define config_network_free_all() ( ( void ) 0 )
# define config_network_target_is_open unsupported_target_is_open
# define config_open_network_target open_unsupported_target
# define config_sendto_network_target sendto_unsupported_target
# endif
#ifdef STUMPLESS_NETWORK_TARGETS_SUPPORTED
# include <stumpless/target/network.h>
# include "private/target/network.h"
# define config_close_network_target stumpless_close_network_target
# define config_network_free_all network_free_all
# define config_network_target_is_open network_target_is_open
# define config_open_network_target open_network_target
# define config_sendto_network_target sendto_network_target
#else
# include "private/target.h"
# define config_close_network_target close_unsupported_target
# define config_network_free_all() ( ( void ) 0 )
# define config_network_target_is_open unsupported_target_is_open
# define config_open_network_target open_unsupported_target
# define config_sendto_network_target sendto_unsupported_target
#endif

#endif /* __STUMPLESS_PRIVATE_CONFIG_WRAPPER_NETWORK_SUPPORTED_H */
23 changes: 21 additions & 2 deletions src/config/have_winsock2.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/* this must be included first to avoid errors */
#include "private/windows_wrapper.h"

#include <stdbool.h>
#include <stddef.h>
#include "private/config/have_winsock2.h"
#include "private/config/locale/wrapper.h"
Expand Down Expand Up @@ -52,7 +53,9 @@ init_wsa( void ) {
}

result = WSAStartup( MAKEWORD( 2, 2 ), &wsa_data );
config_write_bool( &wsa_initialized, true );
if( result == 0 ) {
config_write_bool( &wsa_initialized, true );
}

cleanup_and_return:
config_write_bool( &wsa_data_free, true );
Expand Down Expand Up @@ -124,10 +127,26 @@ winsock2_close_network_target( const struct network_target *target ) {
closesocket( target->handle );
}

WSACleanup( );
config_destroy_mutex( &target->mutex );
}

void
winsock2_free_all( void ) {
bool locked;
int result;

do {
locked = config_compare_exchange_bool( &wsa_data_free, true, false );
} while( !locked );

result = WSACleanup();
if( result != 0 ) {
config_write_bool( &wsa_initialized, false );
}

config_write_bool( &wsa_data_free, true );
}

struct network_target *
winsock2_init_network_target( struct network_target *target ) {
if( init_wsa() != 0 ) {
Expand Down
1 change: 1 addition & 0 deletions src/target/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ lock_network_target( const struct network_target *target ) {

void
network_free_all( void ) {
config_network_provider_free_all();
free_mem( tcp_send_buffer );
tcp_send_buffer = NULL;
tcp_send_buffer_length = 0;
Expand Down
2 changes: 2 additions & 0 deletions tools/check_headers/stumpless_private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"config_int_connect": "private/config/wrapper/int_connect.h"
"config_journald_free_thread": "private/config/wrapper/journald.h"
"config_lock_mutex": "private/config/wrapper/thread_safety.h"
"config_network_provider_free_all": "private/config/wrapper/network_supported.h"
"config_read_flag": "private/config/wrapper/thread_safety.h"
"config_read_ptr": "private/config/wrapper/thread_safety.h"
"config_send_entry_to_journald_target": "private/config/wrapper/journald.h"
Expand Down Expand Up @@ -221,6 +222,7 @@
"windows_lock_mutex": "private/config/have_windows.h"
"windows_unlock_mutex": "private/config/have_windows.h"
"winsock2_close_network_target": "private/config/have_winsock2.h"
"winsock2_free_all": "private/config/have_winsock2.h"
"winsock2_init_network_target": "private/config/have_winsock2.h"
"winsock2_network_free_all": "private/config/have_winsock2.h"
"winsock2_open_tcp4_target": "private/config/have_winsock2.h"
Expand Down

0 comments on commit 2da2949

Please sign in to comment.