1
/* Copyright (C) 2000 MySQL AB
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.
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.
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
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
17
#include "my_static.h"
18
#include <mystrings/m_string.h>
21
set how many open files we want to be able to handle
24
set_maximum_open_files()
25
max_file_limit Files to open
28
The request may not fulfilled becasue of system limitations
31
Files available to open.
32
May be more or less than max_file_limit!
35
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
38
#define RLIM_INFINITY ((uint) 0xffffffff)
41
static uint32_t set_max_open_files(uint32_t max_file_limit)
46
if (!getrlimit(RLIMIT_NOFILE,&rlimit))
48
old_cur= (uint) rlimit.rlim_cur;
49
if (rlimit.rlim_cur == RLIM_INFINITY)
50
rlimit.rlim_cur = max_file_limit;
51
if (rlimit.rlim_cur >= max_file_limit)
52
return(rlimit.rlim_cur); /* purecov: inspected */
53
rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
54
if (setrlimit(RLIMIT_NOFILE, &rlimit))
55
max_file_limit= old_cur; /* Use original value */
58
rlimit.rlim_cur= 0; /* Safety if next call fails */
59
(void) getrlimit(RLIMIT_NOFILE,&rlimit);
60
if (rlimit.rlim_cur) /* If call didn't fail */
61
max_file_limit= (uint) rlimit.rlim_cur;
64
return(max_file_limit);
68
static int set_max_open_files(uint32_t max_file_limit)
70
/* We don't know the limit. Return best guess */
71
return cmin(max_file_limit, OS_FILE_LIMIT);
77
Change number of open files
80
my_set_max_open_files()
81
files Number of requested files
84
number of files available for open
87
uint32_t my_set_max_open_files(uint32_t files)
89
struct st_my_file_info *tmp;
91
files= set_max_open_files(cmin(files, OS_FILE_LIMIT));
92
if (files <= MY_NFILE)
95
if (!(tmp= (struct st_my_file_info*) my_malloc(sizeof(*tmp) * files,
99
/* Copy any initialized files */
100
memcpy(tmp, my_file_info, sizeof(*tmp) * cmin(my_file_limit, files));
102
The int cast is necessary since 'my_file_limits' might be greater
105
memset(tmp + my_file_limit, 0,
106
cmax((int) (files - my_file_limit), 0)*sizeof(*tmp));
107
my_free_open_file_info(); /* Free if already allocated */
109
my_file_limit= files;
114
void my_free_open_file_info()
116
if (my_file_info != my_file_info_default)
118
/* Copy data back for my_print_open_files */
119
memcpy(my_file_info_default, my_file_info,
120
sizeof(*my_file_info_default)* MY_NFILE);
121
free((char*) my_file_info);
122
my_file_info= my_file_info_default;
123
my_file_limit= MY_NFILE;