Merge lp:~jan-kneschke/mysql-proxy/fdlimit-int-ranges into lp:mysql-proxy

Proposed by Jan Kneschke
Status: Approved
Approved by: Leith
Approved revision: 1132
Proposed branch: lp:~jan-kneschke/mysql-proxy/fdlimit-int-ranges
Merge into: lp:mysql-proxy
Diff against target: 227 lines (+135/-5)
4 files modified
src/chassis-limits.c (+57/-5)
tests/unit/CMakeLists.txt (+13/-0)
tests/unit/Makefile.am (+12/-0)
tests/unit/t_chassis_limits.c (+53/-0)
To merge this branch: bzr merge lp:~jan-kneschke/mysql-proxy/fdlimit-int-ranges
Reviewer Review Type Date Requested Status
Leith (community) Approve
Review via email: mp+37127@code.launchpad.net

This proposal supersedes a proposal from 2010-09-30.

Description of the change

setmaxstdio() wants a int, setrlimit() wants unsigned int of 32 or 64 size.

This patch checks that we don't cut off bits or treat negative values are large positive values and get a false positive set().

To post a comment you must log in.
Revision history for this message
Leith (mleith) wrote :

Looks good to me.

review: Approve

Unmerged revisions

1132. By <email address hidden>

added a test for chassis_fdlimit_set() and check that we don't try to set values what would
be interpreted wrongly

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/chassis-limits.c'
2--- src/chassis-limits.c 2010-04-06 14:26:51 +0000
3+++ src/chassis-limits.c 2010-09-30 13:39:45 +0000
4@@ -35,6 +35,7 @@
5 #include <stdio.h> /* for _getmaxstdio() */
6 #endif
7 #include <errno.h>
8+#include <stdint.h>
9
10 #include "chassis-limits.h"
11
12@@ -60,13 +61,11 @@
13 return chassis_fdlimit_set(max_files_number);
14 }
15
16+#ifdef _WIN32
17 /**
18- * set the upper limit of open files
19- *
20- * @return -1 on error, 0 on success
21+ * set the max-open-files limit on win32
22 */
23-int chassis_fdlimit_set(gint64 max_files_number) {
24-#ifdef _WIN32
25+static int chassis_fdlimit_set_win32(int max_files_number) {
26 int max_files_number_set;
27
28 max_files_number_set = _setmaxstdio(max_files_number);
29@@ -79,7 +78,12 @@
30 }
31
32 return 0;
33+}
34 #else
35+/**
36+ * set the max-open-files limit on unix
37+ */
38+static int chassis_fdlimit_set_unix(guint64 max_files_number) {
39 struct rlimit max_files_rlimit;
40 rlim_t soft_limit;
41 rlim_t hard_limit;
42@@ -91,6 +95,31 @@
43 soft_limit = max_files_rlimit.rlim_cur;
44 hard_limit = max_files_rlimit.rlim_max;
45
46+ /**
47+ * as max_files_number a int64 and rlim_t is either a
48+ * 32bit or 64bit value we have to check if we our out of range before
49+ * we pass it down to not
50+ *
51+ * on macosx 10.6 it is a __uint64_t
52+ * on linux it is a __ULONGWORD_TYPE which may be 32bit or 64bit
53+ */
54+ switch (sizeof(rlim_t)) {
55+ case 8:
56+ /* our size matches, just pass it on */
57+ break;
58+ case 4:
59+ if (max_files_number > UINT32_MAX) {
60+ /* out of range */
61+ return -1;
62+ }
63+ break;
64+ default:
65+ g_error("%s: we expected rlim_t to be either 8 or 4, but here it is %lu",
66+ G_STRLOC,
67+ sizeof(rlim_t));
68+ break;
69+ }
70+
71 max_files_rlimit.rlim_cur = max_files_number;
72 if (hard_limit < max_files_number) { /* raise the hard-limit too in case it is smaller than the soft-limit, otherwise we get a EINVAL */
73 max_files_rlimit.rlim_max = max_files_number;
74@@ -101,6 +130,29 @@
75 }
76
77 return 0;
78+}
79+#endif
80+
81+/**
82+ * set the upper limit of open files
83+ *
84+ * @return -1 on error, 0 on success
85+ */
86+int chassis_fdlimit_set(gint64 max_files_number) {
87+#ifdef _WIN32
88+ /* make sure we don't cut off the high bits */
89+ if (max_files_number > INT_MAX) {
90+ return -1;
91+ }
92+
93+ return chassis_fdlimit_set_win32((int) max_files_number);
94+#else
95+ /* want to have a uint, but have a int. Make sure negative values don't map to large unsigned ones */
96+ if (max_files_number < 0) {
97+ return -1;
98+ }
99+
100+ return chassis_fdlimit_set_unix((guint64) max_files_number);
101 #endif
102 }
103
104
105=== modified file 'tests/unit/CMakeLists.txt'
106--- tests/unit/CMakeLists.txt 2010-09-27 15:54:18 +0000
107+++ tests/unit/CMakeLists.txt 2010-09-30 13:39:45 +0000
108@@ -28,6 +28,17 @@
109 ${GTHREAD_LIBRARIES}
110 ${GMODULE_LIBRARIES}
111 )
112+
113+ADD_EXECUTABLE(t_chassis_limits
114+ t_chassis_limits.c
115+)
116+TARGET_LINK_LIBRARIES(t_chassis_limits
117+ mysql-chassis
118+ ${GLIB_LIBRARIES}
119+ ${GTHREAD_LIBRARIES}
120+ ${GMODULE_LIBRARIES}
121+)
122+
123 ADD_EXECUTABLE(check_plugin
124 check_plugin.c
125 ../../src/chassis-plugin.c
126@@ -166,6 +177,7 @@
127 set_property(TARGET check_chassis_log check_plugin check_mysqld_proto
128 check_loadscript check_chassis_path check_chassis_filemode
129 t_network_injection t_network_backend t_network_queue
130+ t_chassis_limits
131 APPEND PROPERTY COMPILE_DEFINITIONS "mysql_chassis_proxy_STATIC"
132 COMPILE_DEFINITIONS "mysql_chassis_STATIC")
133 ENDIF(WIN32)
134@@ -181,4 +193,5 @@
135 ADD_TEST(check_chassis_filemode check_chassis_filemode)
136 ADD_TEST(t_network_injection t_network_injection)
137 ADD_TEST(t_network_backend t_network_backend)
138+ADD_TEST(t_chassis_limits t_chassis_limits)
139
140
141=== modified file 'tests/unit/Makefile.am'
142--- tests/unit/Makefile.am 2010-09-27 15:54:18 +0000
143+++ tests/unit/Makefile.am 2010-09-30 13:39:45 +0000
144@@ -34,6 +34,7 @@
145 t_network_mysqld_packet \
146 t_network_mysqld_type \
147 t_network_mysqld_masterinfo \
148+ t_chassis_limits \
149 t_chassis_timings \
150 t_chassis_shutdown_hooks \
151 check_chassis_filemode \
152@@ -53,6 +54,17 @@
153 t_chassis_shutdown_hooks_LDADD = $(GLIB_LIBS) $(GTHREAD_LIBS)
154
155
156+t_chassis_limits_SOURCES = t_chassis_limits.c \
157+ $(top_srcdir)/src/chassis-limits.c \
158+ $(top_srcdir)/src/glib-ext.c
159+
160+t_chassis_limits_CPPFLAGS = \
161+ -I$(top_srcdir)/src/ $(GLIB_CFLAGS) -I$(top_srcdir) \
162+ $(MYSQL_CFLAGS)
163+
164+t_chassis_limits_LDADD = $(GLIB_LIBS) $(GTHREAD_LIBS)
165+
166+
167 check_chassis_filemode_SOURCES = check_chassis_filemode.c \
168 $(top_srcdir)/src/chassis-filemode.c
169
170
171=== added file 'tests/unit/t_chassis_limits.c'
172--- tests/unit/t_chassis_limits.c 1970-01-01 00:00:00 +0000
173+++ tests/unit/t_chassis_limits.c 2010-09-30 13:39:45 +0000
174@@ -0,0 +1,53 @@
175+/* $%BEGINLICENSE%$
176+ Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
177+
178+ This program is free software; you can redistribute it and/or
179+ modify it under the terms of the GNU General Public License as
180+ published by the Free Software Foundation; version 2 of the
181+ License.
182+
183+ This program is distributed in the hope that it will be useful,
184+ but WITHOUT ANY WARRANTY; without even the implied warranty of
185+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
186+ GNU General Public License for more details.
187+
188+ You should have received a copy of the GNU General Public License
189+ along with this program; if not, write to the Free Software
190+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
191+ 02110-1301 USA
192+
193+ $%ENDLICENSE%$ */
194+
195+#include <stdio.h>
196+#include <stdlib.h>
197+#include <string.h>
198+
199+#include <glib.h>
200+
201+#include "chassis-limits.h"
202+
203+/**
204+ * try if we can set the fdlimit to a reasonable value
205+ */
206+void t_limits_set_fdlimit(void) {
207+ g_assert_cmpint(0, ==, chassis_fdlimit_set(128));
208+}
209+
210+/**
211+ * try if out-of-range errors are handled correctly
212+ */
213+void t_limits_set_fdlimit_out_of_range(void) {
214+ g_assert_cmpint(-1, ==, chassis_fdlimit_set(INT_MAX));
215+ g_assert_cmpint(-1, ==, chassis_fdlimit_set(-1));
216+}
217+
218+int main(int argc, char **argv) {
219+ g_test_init(&argc, &argv, NULL);
220+ g_test_bug_base("http://bugs.mysql.com/");
221+
222+ g_test_add_func("/chassis/limits/set_fdlimit", t_limits_set_fdlimit);
223+ g_test_add_func("/chassis/limits/set_fdlimit_out_of_range", t_limits_set_fdlimit_out_of_range);
224+
225+ return g_test_run();
226+}
227+

Subscribers

People subscribed via source and target branches