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
=== modified file 'src/chassis-limits.c'
--- src/chassis-limits.c 2010-04-06 14:26:51 +0000
+++ src/chassis-limits.c 2010-09-30 13:39:45 +0000
@@ -35,6 +35,7 @@
35#include <stdio.h> /* for _getmaxstdio() */35#include <stdio.h> /* for _getmaxstdio() */
36#endif36#endif
37#include <errno.h>37#include <errno.h>
38#include <stdint.h>
3839
39#include "chassis-limits.h"40#include "chassis-limits.h"
4041
@@ -60,13 +61,11 @@
60 return chassis_fdlimit_set(max_files_number);61 return chassis_fdlimit_set(max_files_number);
61}62}
6263
64#ifdef _WIN32
63/**65/**
64 * set the upper limit of open files66 * set the max-open-files limit on win32
65 *
66 * @return -1 on error, 0 on success
67 */67 */
68int chassis_fdlimit_set(gint64 max_files_number) {68static int chassis_fdlimit_set_win32(int max_files_number) {
69#ifdef _WIN32
70 int max_files_number_set;69 int max_files_number_set;
7170
72 max_files_number_set = _setmaxstdio(max_files_number);71 max_files_number_set = _setmaxstdio(max_files_number);
@@ -79,7 +78,12 @@
79 }78 }
8079
81 return 0;80 return 0;
81}
82#else82#else
83/**
84 * set the max-open-files limit on unix
85 */
86static int chassis_fdlimit_set_unix(guint64 max_files_number) {
83 struct rlimit max_files_rlimit;87 struct rlimit max_files_rlimit;
84 rlim_t soft_limit;88 rlim_t soft_limit;
85 rlim_t hard_limit;89 rlim_t hard_limit;
@@ -91,6 +95,31 @@
91 soft_limit = max_files_rlimit.rlim_cur;95 soft_limit = max_files_rlimit.rlim_cur;
92 hard_limit = max_files_rlimit.rlim_max;96 hard_limit = max_files_rlimit.rlim_max;
9397
98 /**
99 * as max_files_number a int64 and rlim_t is either a
100 * 32bit or 64bit value we have to check if we our out of range before
101 * we pass it down to not
102 *
103 * on macosx 10.6 it is a __uint64_t
104 * on linux it is a __ULONGWORD_TYPE which may be 32bit or 64bit
105 */
106 switch (sizeof(rlim_t)) {
107 case 8:
108 /* our size matches, just pass it on */
109 break;
110 case 4:
111 if (max_files_number > UINT32_MAX) {
112 /* out of range */
113 return -1;
114 }
115 break;
116 default:
117 g_error("%s: we expected rlim_t to be either 8 or 4, but here it is %lu",
118 G_STRLOC,
119 sizeof(rlim_t));
120 break;
121 }
122
94 max_files_rlimit.rlim_cur = max_files_number;123 max_files_rlimit.rlim_cur = max_files_number;
95 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 */124 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 */
96 max_files_rlimit.rlim_max = max_files_number;125 max_files_rlimit.rlim_max = max_files_number;
@@ -101,6 +130,29 @@
101 }130 }
102131
103 return 0;132 return 0;
133}
134#endif
135
136/**
137 * set the upper limit of open files
138 *
139 * @return -1 on error, 0 on success
140 */
141int chassis_fdlimit_set(gint64 max_files_number) {
142#ifdef _WIN32
143 /* make sure we don't cut off the high bits */
144 if (max_files_number > INT_MAX) {
145 return -1;
146 }
147
148 return chassis_fdlimit_set_win32((int) max_files_number);
149#else
150 /* want to have a uint, but have a int. Make sure negative values don't map to large unsigned ones */
151 if (max_files_number < 0) {
152 return -1;
153 }
154
155 return chassis_fdlimit_set_unix((guint64) max_files_number);
104#endif156#endif
105}157}
106158
107159
=== modified file 'tests/unit/CMakeLists.txt'
--- tests/unit/CMakeLists.txt 2010-09-27 15:54:18 +0000
+++ tests/unit/CMakeLists.txt 2010-09-30 13:39:45 +0000
@@ -28,6 +28,17 @@
28 ${GTHREAD_LIBRARIES}28 ${GTHREAD_LIBRARIES}
29 ${GMODULE_LIBRARIES} 29 ${GMODULE_LIBRARIES}
30)30)
31
32ADD_EXECUTABLE(t_chassis_limits
33 t_chassis_limits.c
34)
35TARGET_LINK_LIBRARIES(t_chassis_limits
36 mysql-chassis
37 ${GLIB_LIBRARIES}
38 ${GTHREAD_LIBRARIES}
39 ${GMODULE_LIBRARIES}
40)
41
31ADD_EXECUTABLE(check_plugin42ADD_EXECUTABLE(check_plugin
32 check_plugin.c 43 check_plugin.c
33 ../../src/chassis-plugin.c44 ../../src/chassis-plugin.c
@@ -166,6 +177,7 @@
166set_property(TARGET check_chassis_log check_plugin check_mysqld_proto177set_property(TARGET check_chassis_log check_plugin check_mysqld_proto
167 check_loadscript check_chassis_path check_chassis_filemode178 check_loadscript check_chassis_path check_chassis_filemode
168 t_network_injection t_network_backend t_network_queue179 t_network_injection t_network_backend t_network_queue
180 t_chassis_limits
169 APPEND PROPERTY COMPILE_DEFINITIONS "mysql_chassis_proxy_STATIC"181 APPEND PROPERTY COMPILE_DEFINITIONS "mysql_chassis_proxy_STATIC"
170 COMPILE_DEFINITIONS "mysql_chassis_STATIC")182 COMPILE_DEFINITIONS "mysql_chassis_STATIC")
171ENDIF(WIN32)183ENDIF(WIN32)
@@ -181,4 +193,5 @@
181ADD_TEST(check_chassis_filemode check_chassis_filemode)193ADD_TEST(check_chassis_filemode check_chassis_filemode)
182ADD_TEST(t_network_injection t_network_injection)194ADD_TEST(t_network_injection t_network_injection)
183ADD_TEST(t_network_backend t_network_backend)195ADD_TEST(t_network_backend t_network_backend)
196ADD_TEST(t_chassis_limits t_chassis_limits)
184197
185198
=== modified file 'tests/unit/Makefile.am'
--- tests/unit/Makefile.am 2010-09-27 15:54:18 +0000
+++ tests/unit/Makefile.am 2010-09-30 13:39:45 +0000
@@ -34,6 +34,7 @@
34 t_network_mysqld_packet \34 t_network_mysqld_packet \
35 t_network_mysqld_type \35 t_network_mysqld_type \
36 t_network_mysqld_masterinfo \36 t_network_mysqld_masterinfo \
37 t_chassis_limits \
37 t_chassis_timings \38 t_chassis_timings \
38 t_chassis_shutdown_hooks \39 t_chassis_shutdown_hooks \
39 check_chassis_filemode \40 check_chassis_filemode \
@@ -53,6 +54,17 @@
53t_chassis_shutdown_hooks_LDADD = $(GLIB_LIBS) $(GTHREAD_LIBS)54t_chassis_shutdown_hooks_LDADD = $(GLIB_LIBS) $(GTHREAD_LIBS)
5455
5556
57t_chassis_limits_SOURCES = t_chassis_limits.c \
58 $(top_srcdir)/src/chassis-limits.c \
59 $(top_srcdir)/src/glib-ext.c
60
61t_chassis_limits_CPPFLAGS = \
62 -I$(top_srcdir)/src/ $(GLIB_CFLAGS) -I$(top_srcdir) \
63 $(MYSQL_CFLAGS)
64
65t_chassis_limits_LDADD = $(GLIB_LIBS) $(GTHREAD_LIBS)
66
67
56check_chassis_filemode_SOURCES = check_chassis_filemode.c \68check_chassis_filemode_SOURCES = check_chassis_filemode.c \
57 $(top_srcdir)/src/chassis-filemode.c69 $(top_srcdir)/src/chassis-filemode.c
5870
5971
=== added file 'tests/unit/t_chassis_limits.c'
--- tests/unit/t_chassis_limits.c 1970-01-01 00:00:00 +0000
+++ tests/unit/t_chassis_limits.c 2010-09-30 13:39:45 +0000
@@ -0,0 +1,53 @@
1/* $%BEGINLICENSE%$
2 Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; version 2 of the
7 License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 02110-1301 USA
18
19 $%ENDLICENSE%$ */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <glib.h>
26
27#include "chassis-limits.h"
28
29/**
30 * try if we can set the fdlimit to a reasonable value
31 */
32void t_limits_set_fdlimit(void) {
33 g_assert_cmpint(0, ==, chassis_fdlimit_set(128));
34}
35
36/**
37 * try if out-of-range errors are handled correctly
38 */
39void t_limits_set_fdlimit_out_of_range(void) {
40 g_assert_cmpint(-1, ==, chassis_fdlimit_set(INT_MAX));
41 g_assert_cmpint(-1, ==, chassis_fdlimit_set(-1));
42}
43
44int main(int argc, char **argv) {
45 g_test_init(&argc, &argv, NULL);
46 g_test_bug_base("http://bugs.mysql.com/");
47
48 g_test_add_func("/chassis/limits/set_fdlimit", t_limits_set_fdlimit);
49 g_test_add_func("/chassis/limits/set_fdlimit_out_of_range", t_limits_set_fdlimit_out_of_range);
50
51 return g_test_run();
52}
53

Subscribers

People subscribed via source and target branches