~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to src/chassis-limits.c

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#ifdef HAVE_CONFIG_H
22
 
#include "config.h"
23
 
#endif
24
 
 
25
 
#include <glib.h>
26
 
 
27
 
#include <sys/types.h>
28
 
#ifdef HAVE_SYS_TIME_H
29
 
#include <sys/time.h>
30
 
#endif
31
 
#ifdef HAVE_SYS_RESOURCE_H
32
 
#include <sys/resource.h>
33
 
#endif
34
 
#ifdef _WIN32
35
 
#include <stdio.h> /* for _getmaxstdio() */
36
 
#endif
37
 
#include <errno.h>
38
 
 
39
 
#include "chassis-limits.h"
40
 
 
41
 
gint64 chassis_fdlimit_get() {
42
 
#ifdef _WIN32
43
 
        return _getmaxstdio();
44
 
#else
45
 
        struct rlimit max_files_rlimit;
46
 
 
47
 
        if (-1 == getrlimit(RLIMIT_NOFILE, &max_files_rlimit)) {
48
 
                return -1;
49
 
        } else {
50
 
                return max_files_rlimit.rlim_cur;
51
 
        }
52
 
#endif
53
 
 
54
 
}
55
 
 
56
 
/**
57
 
 * redirect the old call 
58
 
 */
59
 
int chassis_set_fdlimit(int max_files_number) {
60
 
        return chassis_fdlimit_set(max_files_number);
61
 
}
62
 
 
63
 
/**
64
 
 * set the upper limit of open files
65
 
 *
66
 
 * @return -1 on error, 0 on success
67
 
 */
68
 
int chassis_fdlimit_set(gint64 max_files_number) {
69
 
#ifdef _WIN32
70
 
        int max_files_number_set;
71
 
 
72
 
        max_files_number_set = _setmaxstdio(max_files_number);
73
 
 
74
 
        if (-1 == max_files_number_set) {
75
 
                return -1;
76
 
        } else if (max_files_number_set != max_files_number) {
77
 
                g_critical("%s: failed to increase the maximum number of open files for stdio: %s (%d)", G_STRLOC, g_strerror(errno), errno);
78
 
                return -1;
79
 
        }
80
 
 
81
 
        return 0;
82
 
#else
83
 
        struct rlimit max_files_rlimit;
84
 
        rlim_t soft_limit;
85
 
        rlim_t hard_limit;
86
 
 
87
 
        if (-1 == getrlimit(RLIMIT_NOFILE, &max_files_rlimit)) {
88
 
                return -1;
89
 
        }
90
 
 
91
 
        soft_limit = max_files_rlimit.rlim_cur;
92
 
        hard_limit = max_files_rlimit.rlim_max;
93
 
 
94
 
        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 */
96
 
                max_files_rlimit.rlim_max = max_files_number;
97
 
        }
98
 
 
99
 
        if (-1 == setrlimit(RLIMIT_NOFILE, &max_files_rlimit)) {
100
 
                return -1;
101
 
        }
102
 
 
103
 
        return 0;
104
 
#endif
105
 
}
106