19 #ifndef MYSQL_SOCKET_H
20 #define MYSQL_SOCKET_H
33 #define SOCKBUF_T char
35 #include <netinet/in.h>
36 #define SOCKBUF_T void
55 #ifdef HAVE_PSI_SOCKET_INTERFACE
56 #define mysql_socket_register(P1, P2, P3) \
57 inline_mysql_socket_register(P1, P2, P3)
59 #define mysql_socket_register(P1, P2, P3) \
88 #define MYSQL_INVALID_SOCKET mysql_socket_invalid()
96 mysql_socket_invalid()
111 mysql_socket_set_address(
112 #ifdef HAVE_PSI_SOCKET_INTERFACE
114 const struct sockaddr *addr,
118 const struct sockaddr *addr __attribute__ ((unused)),
119 socklen_t addr_len __attribute__ ((unused))
123 #ifdef HAVE_PSI_SOCKET_INTERFACE
124 if (socket.m_psi != NULL)
125 PSI_SOCKET_CALL(set_socket_info)(socket.m_psi, NULL, addr, addr_len);
135 mysql_socket_set_thread_owner(
136 #ifdef HAVE_PSI_SOCKET_INTERFACE
143 #ifdef HAVE_PSI_SOCKET_INTERFACE
144 if (socket.m_psi != NULL)
145 PSI_SOCKET_CALL(set_socket_thread_owner)(socket.m_psi);
154 static inline my_socket
157 return mysql_socket.
fd;
169 if (likely(mysql_socket != NULL))
170 mysql_socket->
fd=
fd;
183 #ifdef HAVE_PSI_SOCKET_INTERFACE
184 #define MYSQL_SOCKET_WAIT_VARIABLES(LOCKER, STATE) \
185 struct PSI_socket_locker* LOCKER; \
186 PSI_socket_locker_state STATE;
188 #define MYSQL_SOCKET_WAIT_VARIABLES(LOCKER, STATE)
203 #ifdef HAVE_PSI_SOCKET_INTERFACE
204 #define MYSQL_START_SOCKET_WAIT(LOCKER, STATE, SOCKET, OP, COUNT) \
205 LOCKER= inline_mysql_start_socket_wait(STATE, SOCKET, OP, COUNT,\
208 #define MYSQL_START_SOCKET_WAIT(LOCKER, STATE, SOCKET, OP, COUNT) \
220 #ifdef HAVE_PSI_SOCKET_INTERFACE
221 #define MYSQL_END_SOCKET_WAIT(LOCKER, COUNT) \
222 inline_mysql_end_socket_wait(LOCKER, COUNT)
224 #define MYSQL_END_SOCKET_WAIT(LOCKER, COUNT) \
228 #ifdef HAVE_PSI_SOCKET_INTERFACE
229 #define MYSQL_SOCKET_SET_STATE(SOCKET, STATE) \
230 inline_mysql_socket_set_state(SOCKET, STATE)
232 #define MYSQL_SOCKET_SET_STATE(SOCKET, STATE) \
236 #ifdef HAVE_PSI_SOCKET_INTERFACE
241 static inline struct PSI_socket_locker*
242 inline_mysql_start_socket_wait(PSI_socket_locker_state *state,
244 enum PSI_socket_operation op,
246 const char *src_file,
int src_line)
248 struct PSI_socket_locker *locker;
249 if (mysql_socket.
m_psi != NULL)
251 locker= PSI_SOCKET_CALL(start_socket_wait)
252 (state, mysql_socket.
m_psi, op, byte_count, src_file, src_line);
264 inline_mysql_end_socket_wait(
struct PSI_socket_locker *locker,
size_t byte_count)
267 PSI_SOCKET_CALL(end_socket_wait)(locker, byte_count);
277 inline_mysql_socket_set_state(
MYSQL_SOCKET socket,
enum PSI_socket_state state)
279 if (socket.
m_psi != NULL)
280 PSI_SOCKET_CALL(set_socket_state)(socket.
m_psi, state);
294 #ifdef HAVE_PSI_SOCKET_INTERFACE
295 #define mysql_socket_socket(K, D, T, P) \
296 inline_mysql_socket_socket(K, D, T, P)
298 #define mysql_socket_socket(K, D, T, P) \
299 inline_mysql_socket_socket(D, T, P)
310 #ifdef HAVE_PSI_SOCKET_INTERFACE
311 #define mysql_socket_bind(FD, AP, L) \
312 inline_mysql_socket_bind(__FILE__, __LINE__, FD, AP, L)
314 #define mysql_socket_bind(FD, AP, L) \
315 inline_mysql_socket_bind(FD, AP, L)
326 #ifdef HAVE_PSI_SOCKET_INTERFACE
327 #define mysql_socket_getsockname(FD, AP, LP) \
328 inline_mysql_socket_getsockname(__FILE__, __LINE__, FD, AP, LP)
330 #define mysql_socket_getsockname(FD, AP, LP) \
331 inline_mysql_socket_getsockname(FD, AP, LP)
342 #ifdef HAVE_PSI_SOCKET_INTERFACE
343 #define mysql_socket_connect(FD, AP, L) \
344 inline_mysql_socket_connect(__FILE__, __LINE__, FD, AP, L)
346 #define mysql_socket_connect(FD, AP, L) \
347 inline_mysql_socket_connect(FD, AP, L)
358 #ifdef HAVE_PSI_SOCKET_INTERFACE
359 #define mysql_socket_getpeername(FD, AP, LP) \
360 inline_mysql_socket_getpeername(__FILE__, __LINE__, FD, AP, LP)
362 #define mysql_socket_getpeername(FD, AP, LP) \
363 inline_mysql_socket_getpeername(FD, AP, LP)
375 #ifdef HAVE_PSI_SOCKET_INTERFACE
376 #define mysql_socket_send(FD, B, N, FL) \
377 inline_mysql_socket_send(__FILE__, __LINE__, FD, B, N, FL)
379 #define mysql_socket_send(FD, B, N, FL) \
380 inline_mysql_socket_send(FD, B, N, FL)
392 #ifdef HAVE_PSI_SOCKET_INTERFACE
393 #define mysql_socket_recv(FD, B, N, FL) \
394 inline_mysql_socket_recv(__FILE__, __LINE__, FD, B, N, FL)
396 #define mysql_socket_recv(FD, B, N, FL) \
397 inline_mysql_socket_recv(FD, B, N, FL)
411 #ifdef HAVE_PSI_SOCKET_INTERFACE
412 #define mysql_socket_sendto(FD, B, N, FL, AP, L) \
413 inline_mysql_socket_sendto(__FILE__, __LINE__, FD, B, N, FL, AP, L)
415 #define mysql_socket_sendto(FD, B, N, FL, AP, L) \
416 inline_mysql_socket_sendto(FD, B, N, FL, AP, L)
430 #ifdef HAVE_PSI_SOCKET_INTERFACE
431 #define mysql_socket_recvfrom(FD, B, N, FL, AP, LP) \
432 inline_mysql_socket_recvfrom(__FILE__, __LINE__, FD, B, N, FL, AP, LP)
434 #define mysql_socket_recvfrom(FD, B, N, FL, AP, LP) \
435 inline_mysql_socket_recvfrom(FD, B, N, FL, AP, LP)
448 #ifdef HAVE_PSI_SOCKET_INTERFACE
449 #define mysql_socket_getsockopt(FD, LV, ON, OP, OL) \
450 inline_mysql_socket_getsockopt(__FILE__, __LINE__, FD, LV, ON, OP, OL)
452 #define mysql_socket_getsockopt(FD, LV, ON, OP, OL) \
453 inline_mysql_socket_getsockopt(FD, LV, ON, OP, OL)
466 #ifdef HAVE_PSI_SOCKET_INTERFACE
467 #define mysql_socket_setsockopt(FD, LV, ON, OP, OL) \
468 inline_mysql_socket_setsockopt(__FILE__, __LINE__, FD, LV, ON, OP, OL)
470 #define mysql_socket_setsockopt(FD, LV, ON, OP, OL) \
471 inline_mysql_socket_setsockopt(FD, LV, ON, OP, OL)
481 #ifdef HAVE_PSI_SOCKET_INTERFACE
482 #define mysql_socket_listen(FD, N) \
483 inline_mysql_socket_listen(__FILE__, __LINE__, FD, N)
485 #define mysql_socket_listen(FD, N) \
486 inline_mysql_socket_listen(FD, N)
498 #ifdef HAVE_PSI_SOCKET_INTERFACE
499 #define mysql_socket_accept(K, FD, AP, LP) \
500 inline_mysql_socket_accept(__FILE__, __LINE__, K, FD, AP, LP)
502 #define mysql_socket_accept(K, FD, AP, LP) \
503 inline_mysql_socket_accept(FD, AP, LP)
512 #ifdef HAVE_PSI_SOCKET_INTERFACE
513 #define mysql_socket_close(FD) \
514 inline_mysql_socket_close(__FILE__, __LINE__, FD)
516 #define mysql_socket_close(FD) \
517 inline_mysql_socket_close(FD)
527 #ifdef HAVE_PSI_SOCKET_INTERFACE
528 #define mysql_socket_shutdown(FD, H) \
529 inline_mysql_socket_shutdown(__FILE__, __LINE__, FD, H)
531 #define mysql_socket_shutdown(FD, H) \
532 inline_mysql_socket_shutdown(FD, H)
535 #ifdef HAVE_PSI_SOCKET_INTERFACE
536 static inline void inline_mysql_socket_register(
537 const char *category,
538 PSI_socket_info *info,
541 PSI_SOCKET_CALL(register_socket)(category, info, count);
548 inline_mysql_socket_socket
550 #ifdef HAVE_PSI_SOCKET_INTERFACE
553 int domain,
int type,
int protocol)
556 mysql_socket.
fd= socket(domain, type, protocol);
558 #ifdef HAVE_PSI_SOCKET_INTERFACE
559 if (likely(mysql_socket.
fd != INVALID_SOCKET))
561 mysql_socket.
m_psi= PSI_SOCKET_CALL(init_socket)
562 (key, (
const my_socket*)&mysql_socket.
fd, NULL, 0);
571 inline_mysql_socket_bind
573 #ifdef HAVE_PSI_SOCKET_INTERFACE
574 const char *src_file, uint src_line,
576 MYSQL_SOCKET mysql_socket,
const struct sockaddr *addr, socklen_t len)
580 #ifdef HAVE_PSI_SOCKET_INTERFACE
581 if (mysql_socket.m_psi != NULL)
584 PSI_socket_locker_state state;
585 PSI_socket_locker *locker;
586 locker= PSI_SOCKET_CALL(start_socket_wait)
587 (&state, mysql_socket.m_psi, PSI_SOCKET_BIND, (size_t)0, src_file, src_line);
590 result= bind(mysql_socket.fd, addr, len);
594 PSI_SOCKET_CALL(set_socket_info)(mysql_socket.m_psi, NULL, addr, len);
597 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
604 result= bind(mysql_socket.fd, addr, len);
611 inline_mysql_socket_getsockname
613 #ifdef HAVE_PSI_SOCKET_INTERFACE
614 const char *src_file, uint src_line,
616 MYSQL_SOCKET mysql_socket,
struct sockaddr *addr, socklen_t *len)
620 #ifdef HAVE_PSI_SOCKET_INTERFACE
621 if (mysql_socket.m_psi != NULL)
624 PSI_socket_locker *locker;
625 PSI_socket_locker_state state;
626 locker= PSI_SOCKET_CALL(start_socket_wait)
627 (&state, mysql_socket.m_psi, PSI_SOCKET_BIND, (size_t)0, src_file, src_line);
630 result= getsockname(mysql_socket.fd, addr, len);
634 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
641 result= getsockname(mysql_socket.fd, addr, len);
649 inline_mysql_socket_connect
651 #ifdef HAVE_PSI_SOCKET_INTERFACE
652 const char *src_file, uint src_line,
654 MYSQL_SOCKET mysql_socket,
const struct sockaddr *addr, socklen_t len)
658 #ifdef HAVE_PSI_SOCKET_INTERFACE
659 if (mysql_socket.m_psi != NULL)
662 PSI_socket_locker *locker;
663 PSI_socket_locker_state state;
664 locker= PSI_SOCKET_CALL(start_socket_wait)
665 (&state, mysql_socket.m_psi, PSI_SOCKET_CONNECT, (size_t)0, src_file, src_line);
668 result= connect(mysql_socket.fd, addr, len);
672 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
679 result= connect(mysql_socket.fd, addr, len);
687 inline_mysql_socket_getpeername
689 #ifdef HAVE_PSI_SOCKET_INTERFACE
690 const char *src_file, uint src_line,
692 MYSQL_SOCKET mysql_socket,
struct sockaddr *addr, socklen_t *len)
696 #ifdef HAVE_PSI_SOCKET_INTERFACE
697 if (mysql_socket.m_psi != NULL)
700 PSI_socket_locker *locker;
701 PSI_socket_locker_state state;
702 locker= PSI_SOCKET_CALL(start_socket_wait)
703 (&state, mysql_socket.m_psi, PSI_SOCKET_BIND, (size_t)0, src_file, src_line);
706 result= getpeername(mysql_socket.fd, addr, len);
710 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
717 result= getpeername(mysql_socket.fd, addr, len);
724 static inline ssize_t
725 inline_mysql_socket_send
727 #ifdef HAVE_PSI_SOCKET_INTERFACE
728 const char *src_file, uint src_line,
734 #ifdef HAVE_PSI_SOCKET_INTERFACE
735 if (mysql_socket.m_psi != NULL)
738 PSI_socket_locker *locker;
739 PSI_socket_locker_state state;
740 locker= PSI_SOCKET_CALL(start_socket_wait)
741 (&state, mysql_socket.m_psi, PSI_SOCKET_SEND,
n, src_file, src_line);
744 result= send(mysql_socket.fd, buf, IF_WIN((
int),) n, flags);
749 size_t bytes_written;
750 bytes_written= (result > -1) ? result : 0;
751 PSI_SOCKET_CALL(end_socket_wait)(locker, bytes_written);
759 result= send(mysql_socket.fd, buf, IF_WIN((
int),) n, flags);
766 static inline ssize_t
767 inline_mysql_socket_recv
769 #ifdef HAVE_PSI_SOCKET_INTERFACE
770 const char *src_file, uint src_line,
772 MYSQL_SOCKET mysql_socket, SOCKBUF_T *buf,
size_t n,
int flags)
776 #ifdef HAVE_PSI_SOCKET_INTERFACE
777 if (mysql_socket.m_psi != NULL)
780 PSI_socket_locker *locker;
781 PSI_socket_locker_state state;
782 locker= PSI_SOCKET_CALL(start_socket_wait)
783 (&state, mysql_socket.m_psi, PSI_SOCKET_RECV, (size_t)0, src_file, src_line);
786 result= recv(mysql_socket.fd, buf, IF_WIN((
int),) n, flags);
792 bytes_read= (result > -1) ? result : 0;
793 PSI_SOCKET_CALL(end_socket_wait)(locker, bytes_read);
801 result= recv(mysql_socket.fd, buf, IF_WIN((
int),) n, flags);
808 static inline ssize_t
809 inline_mysql_socket_sendto
811 #ifdef HAVE_PSI_SOCKET_INTERFACE
812 const char *src_file, uint src_line,
814 MYSQL_SOCKET mysql_socket,
const SOCKBUF_T *buf,
size_t n,
int flags,
const struct sockaddr *addr, socklen_t addr_len)
818 #ifdef HAVE_PSI_SOCKET_INTERFACE
819 if (mysql_socket.m_psi != NULL)
822 PSI_socket_locker *locker;
823 PSI_socket_locker_state state;
824 locker= PSI_SOCKET_CALL(start_socket_wait)
825 (&state, mysql_socket.m_psi, PSI_SOCKET_SEND,
n, src_file, src_line);
828 result= sendto(mysql_socket.fd, buf, IF_WIN((
int),) n, flags, addr, addr_len);
833 size_t bytes_written;
834 bytes_written = (result > -1) ? result : 0;
835 PSI_SOCKET_CALL(end_socket_wait)(locker, bytes_written);
843 result= sendto(mysql_socket.fd, buf, IF_WIN((
int),) n, flags, addr, addr_len);
850 static inline ssize_t
851 inline_mysql_socket_recvfrom
853 #ifdef HAVE_PSI_SOCKET_INTERFACE
854 const char *src_file, uint src_line,
856 MYSQL_SOCKET mysql_socket, SOCKBUF_T *buf,
size_t n,
int flags,
857 struct sockaddr *addr, socklen_t *addr_len)
861 #ifdef HAVE_PSI_SOCKET_INTERFACE
862 if (mysql_socket.m_psi != NULL)
865 PSI_socket_locker *locker;
866 PSI_socket_locker_state state;
867 locker= PSI_SOCKET_CALL(start_socket_wait)
868 (&state, mysql_socket.m_psi, PSI_SOCKET_RECV, (size_t)0, src_file, src_line);
871 result= recvfrom(mysql_socket.fd, buf, IF_WIN((
int),) n, flags, addr, addr_len);
877 bytes_read = (result > -1) ? result : 0;
878 PSI_SOCKET_CALL(end_socket_wait)(locker, bytes_read);
886 result= recvfrom(mysql_socket.fd, buf, IF_WIN((
int),) n, flags, addr, addr_len);
894 inline_mysql_socket_getsockopt
896 #ifdef HAVE_PSI_SOCKET_INTERFACE
897 const char *src_file, uint src_line,
899 MYSQL_SOCKET mysql_socket,
int level,
int optname, SOCKBUF_T *optval, socklen_t *optlen)
903 #ifdef HAVE_PSI_SOCKET_INTERFACE
904 if (mysql_socket.m_psi != NULL)
907 PSI_socket_locker *locker;
908 PSI_socket_locker_state state;
909 locker= PSI_SOCKET_CALL(start_socket_wait)
910 (&state, mysql_socket.m_psi, PSI_SOCKET_OPT, (size_t)0, src_file, src_line);
913 result= getsockopt(mysql_socket.fd, level, optname, optval, optlen);
917 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
924 result= getsockopt(mysql_socket.fd, level, optname, optval, optlen);
932 inline_mysql_socket_setsockopt
934 #ifdef HAVE_PSI_SOCKET_INTERFACE
935 const char *src_file, uint src_line,
937 MYSQL_SOCKET mysql_socket,
int level,
int optname,
const SOCKBUF_T *optval,
942 #ifdef HAVE_PSI_SOCKET_INTERFACE
943 if (mysql_socket.m_psi)
946 PSI_socket_locker *locker;
947 PSI_socket_locker_state state;
948 locker= PSI_SOCKET_CALL(start_socket_wait)
949 (&state, mysql_socket.m_psi, PSI_SOCKET_OPT, (size_t)0, src_file, src_line);
952 result= setsockopt(mysql_socket.fd, level, optname, optval, optlen);
956 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
963 result= setsockopt(mysql_socket.fd, level, optname, optval, optlen);
971 inline_mysql_socket_listen
973 #ifdef HAVE_PSI_SOCKET_INTERFACE
974 const char *src_file, uint src_line,
980 #ifdef HAVE_PSI_SOCKET_INTERFACE
981 if (mysql_socket.m_psi != NULL)
984 PSI_socket_locker *locker;
985 PSI_socket_locker_state state;
986 locker= PSI_SOCKET_CALL(start_socket_wait)
987 (&state, mysql_socket.m_psi, PSI_SOCKET_CONNECT, (size_t)0, src_file, src_line);
990 result= listen(mysql_socket.fd, backlog);
994 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
1001 result= listen(mysql_socket.fd, backlog);
1009 inline_mysql_socket_accept
1011 #ifdef HAVE_PSI_SOCKET_INTERFACE
1012 const char *src_file, uint src_line, PSI_socket_key key,
1014 MYSQL_SOCKET socket_listen,
struct sockaddr *addr, socklen_t *addr_len)
1017 socklen_t addr_length= (addr_len != NULL) ? *addr_len : 0;
1019 #ifdef HAVE_PSI_SOCKET_INTERFACE
1020 if (socket_listen.m_psi != NULL)
1023 PSI_socket_locker *locker;
1024 PSI_socket_locker_state state;
1025 locker= PSI_SOCKET_CALL(start_socket_wait)
1026 (&state, socket_listen.m_psi, PSI_SOCKET_CONNECT, (size_t)0, src_file, src_line);
1029 socket_accept.
fd= accept(socket_listen.fd, addr, &addr_length);
1033 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
1039 socket_accept.
fd= accept(socket_listen.fd, addr, &addr_length);
1042 #ifdef HAVE_PSI_SOCKET_INTERFACE
1043 if (likely(socket_accept.
fd != INVALID_SOCKET))
1046 socket_accept.
m_psi= PSI_SOCKET_CALL(init_socket)
1047 (key, (
const my_socket*)&socket_accept.
fd, addr, addr_length);
1051 return socket_accept;
1057 inline_mysql_socket_close
1059 #ifdef HAVE_PSI_SOCKET_INTERFACE
1060 const char *src_file, uint src_line,
1066 #ifdef HAVE_PSI_SOCKET_INTERFACE
1067 if (mysql_socket.m_psi != NULL)
1070 PSI_socket_locker *locker;
1071 PSI_socket_locker_state state;
1072 locker= PSI_SOCKET_CALL(start_socket_wait)
1073 (&state, mysql_socket.m_psi, PSI_SOCKET_CLOSE, (size_t)0, src_file, src_line);
1076 result= closesocket(mysql_socket.fd);
1080 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
1082 if (mysql_socket.m_psi != NULL)
1090 result= closesocket(mysql_socket.fd);
1098 inline_mysql_socket_shutdown
1100 #ifdef HAVE_PSI_SOCKET_INTERFACE
1101 const char *src_file, uint src_line,
1108 static LPFN_DISCONNECTEX DisconnectEx = NULL;
1109 if (DisconnectEx == NULL)
1111 DWORD dwBytesReturned;
1112 GUID guidDisconnectEx = WSAID_DISCONNECTEX;
1113 WSAIoctl(mysql_socket.fd, SIO_GET_EXTENSION_FUNCTION_POINTER,
1114 &guidDisconnectEx,
sizeof(GUID),
1115 &DisconnectEx,
sizeof(DisconnectEx),
1116 &dwBytesReturned, NULL, NULL);
1121 #ifdef HAVE_PSI_SOCKET_INTERFACE
1122 if (mysql_socket.m_psi != NULL)
1124 PSI_socket_locker *locker;
1125 PSI_socket_locker_state state;
1126 locker= PSI_SOCKET_CALL(start_socket_wait)
1127 (&state, mysql_socket.m_psi, PSI_SOCKET_SHUTDOWN, (size_t)0, src_file, src_line);
1132 result= (DisconnectEx(mysql_socket.fd, (LPOVERLAPPED) NULL,
1133 (DWORD) 0, (DWORD) 0) == TRUE) ? 0 : -1;
1136 result= shutdown(mysql_socket.fd, how);
1140 PSI_SOCKET_CALL(end_socket_wait)(locker, (size_t)0);
1149 result= (DisconnectEx(mysql_socket.fd, (LPOVERLAPPED) NULL,
1150 (DWORD) 0, (DWORD) 0) == TRUE) ? 0 : -1;
1153 result= shutdown(mysql_socket.fd, how);