1/* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software Foundation,
14 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15
16/**
17 @file storage/perfschema/pfs_global.cc
18 Miscellaneous global dependencies (implementation).
19*/
20
21#include <my_global.h>
22#include "pfs_global.h"
23#include <my_sys.h>
24#include <my_net.h>
25#ifdef HAVE_MALLOC_H
26#include <malloc.h> /* memalign() may be here */
27#endif
28
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif
32
33#ifdef HAVE_STRING_H
34#include <string.h>
35#endif
36
37#ifdef __WIN__
38 #include <winsock2.h>
39#else
40 #include <arpa/inet.h>
41#endif
42
43bool pfs_initialized= false;
44size_t pfs_allocated_memory= 0;
45
46/**
47 Memory allocation for the performance schema.
48 The memory used internally in the performance schema implementation
49 is allocated once during startup, and considered static thereafter.
50*/
51void *pfs_malloc(size_t size, myf flags)
52{
53 DBUG_ASSERT(! pfs_initialized);
54 DBUG_ASSERT(size > 0);
55
56 void *ptr= NULL;
57
58#ifdef PFS_ALIGNEMENT
59#ifdef HAVE_POSIX_MEMALIGN
60 /* Linux */
61 if (unlikely(posix_memalign(& ptr, PFS_ALIGNEMENT, size)))
62 return NULL;
63#else
64#ifdef HAVE_MEMALIGN
65 /* Solaris */
66 ptr= memalign(PFS_ALIGNEMENT, size);
67 if (unlikely(ptr == NULL))
68 return NULL;
69#else
70#ifdef HAVE_ALIGNED_MALLOC
71 /* Windows */
72 ptr= _aligned_malloc(size, PFS_ALIGNEMENT);
73 if (unlikely(ptr == NULL))
74 return NULL;
75#else
76#error "Missing implementation for PFS_ALIGNENT"
77#endif /* HAVE_ALIGNED_MALLOC */
78#endif /* HAVE_MEMALIGN */
79#endif /* HAVE_POSIX_MEMALIGN */
80#else /* PFS_ALIGNMENT */
81 /* Everything else */
82 ptr= malloc(size);
83 if (unlikely(ptr == NULL))
84 return NULL;
85#endif
86
87 pfs_allocated_memory+= size;
88 if (flags & MY_ZEROFILL)
89 memset(ptr, 0, size);
90 return ptr;
91}
92
93void pfs_free(void *ptr)
94{
95 if (ptr == NULL)
96 return;
97
98#ifdef HAVE_POSIX_MEMALIGN
99 /* Allocated with posix_memalign() */
100 free(ptr);
101#else
102#ifdef HAVE_MEMALIGN
103 /* Allocated with memalign() */
104 free(ptr);
105#else
106#ifdef HAVE_ALIGNED_MALLOC
107 /* Allocated with _aligned_malloc() */
108 _aligned_free(ptr);
109#else
110 /* Allocated with malloc() */
111 free(ptr);
112#endif /* HAVE_ALIGNED_MALLOC */
113#endif /* HAVE_MEMALIGN */
114#endif /* HAVE_POSIX_MEMALIGN */
115}
116
117void pfs_print_error(const char *format, ...)
118{
119 va_list args;
120 va_start(args, format);
121 /*
122 Printing to anything else, like the error log, would generate even more
123 recursive calls to the performance schema implementation
124 (file io is instrumented), so that could lead to catastrophic results.
125 Printing to something safe, and low level: stderr only.
126 */
127 vfprintf(stderr, format, args);
128 va_end(args);
129 fflush(stderr);
130}
131
132/**
133 Array allocation for the performance schema.
134 Checks for overflow of n * size before allocating.
135 @param n number of array elements
136 @param size element size
137 @param flags malloc flags
138 @return pointer to memory on success, else NULL
139*/
140void *pfs_malloc_array(size_t n, size_t size, myf flags)
141{
142 DBUG_ASSERT(n > 0);
143 DBUG_ASSERT(size > 0);
144 size_t array_size= n * size;
145 /* Check for overflow before allocating. */
146 if (is_overflow(array_size, n, size))
147 return NULL;
148 return pfs_malloc(array_size, flags);
149}
150
151/**
152 Detect multiplication overflow.
153 @param product multiplication product
154 @param n1 operand
155 @param n2 operand
156 @return true if multiplication caused an overflow.
157*/
158bool is_overflow(size_t product, size_t n1, size_t n2)
159{
160 if (n1 != 0 && (product / n1 != n2))
161 return true;
162 else
163 return false;
164}
165
166/** Convert raw ip address into readable format. Do not do a reverse DNS lookup. */
167
168uint pfs_get_socket_address(char *host,
169 uint host_len,
170 uint *port,
171 const struct sockaddr_storage *src_addr,
172 socklen_t src_len)
173{
174 DBUG_ASSERT(host);
175 DBUG_ASSERT(src_addr);
176 DBUG_ASSERT(port);
177
178 memset(host, 0, host_len);
179 *port= 0;
180
181 switch (src_addr->ss_family)
182 {
183 case AF_INET:
184 {
185 if (host_len < INET_ADDRSTRLEN+1)
186 return 0;
187 struct sockaddr_in *sa4= (struct sockaddr_in *)(src_addr);
188 #ifdef __WIN__
189 /* Older versions of Windows do not support inet_ntop() */
190 getnameinfo((struct sockaddr *)sa4, sizeof(struct sockaddr_in),
191 host, host_len, NULL, 0, NI_NUMERICHOST);
192 #else
193 inet_ntop(AF_INET, &(sa4->sin_addr), host, INET_ADDRSTRLEN);
194 #endif
195 *port= ntohs(sa4->sin_port);
196 }
197 break;
198
199#ifdef HAVE_IPV6
200 case AF_INET6:
201 {
202 if (host_len < INET6_ADDRSTRLEN+1)
203 return 0;
204 struct sockaddr_in6 *sa6= (struct sockaddr_in6 *)(src_addr);
205 #ifdef __WIN__
206 /* Older versions of Windows do not support inet_ntop() */
207 getnameinfo((struct sockaddr *)sa6, sizeof(struct sockaddr_in6),
208 host, host_len, NULL, 0, NI_NUMERICHOST);
209 #else
210 inet_ntop(AF_INET6, &(sa6->sin6_addr), host, INET6_ADDRSTRLEN);
211 #endif
212 *port= ntohs(sa6->sin6_port);
213 }
214 break;
215#endif
216
217 default:
218 break;
219 }
220
221 /* Return actual IP address string length */
222 return ((uint)strlen((const char*)host));
223}
224
225