mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_file.c
blobf8e0c9e7bad0dca97ead5ef9399a56dd4cab81dc
1 /*
2 Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "mysys_priv.h"
19 #include "my_static.h"
20 #include <m_string.h>
23 set how many open files we want to be able to handle
25 SYNOPSIS
26 set_maximum_open_files()
27 max_file_limit Files to open
29 NOTES
30 The request may not fulfilled becasue of system limitations
32 RETURN
33 Files available to open.
34 May be more or less than max_file_limit!
37 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
39 #ifndef RLIM_INFINITY
40 #define RLIM_INFINITY ((uint) 0xffffffff)
41 #endif
43 static uint set_max_open_files(uint max_file_limit)
45 struct rlimit rlimit;
46 uint old_cur;
47 DBUG_ENTER("set_max_open_files");
48 DBUG_PRINT("enter",("files: %u", max_file_limit));
50 if (!getrlimit(RLIMIT_NOFILE,&rlimit))
52 old_cur= (uint) rlimit.rlim_cur;
53 DBUG_PRINT("info", ("rlim_cur: %u rlim_max: %u",
54 (uint) rlimit.rlim_cur,
55 (uint) rlimit.rlim_max));
56 if (rlimit.rlim_cur == RLIM_INFINITY)
57 rlimit.rlim_cur = max_file_limit;
58 if (rlimit.rlim_cur >= max_file_limit)
59 DBUG_RETURN(rlimit.rlim_cur); /* purecov: inspected */
60 rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
61 if (setrlimit(RLIMIT_NOFILE, &rlimit))
62 max_file_limit= old_cur; /* Use original value */
63 else
65 rlimit.rlim_cur= 0; /* Safety if next call fails */
66 (void) getrlimit(RLIMIT_NOFILE,&rlimit);
67 DBUG_PRINT("info", ("rlim_cur: %u", (uint) rlimit.rlim_cur));
68 if (rlimit.rlim_cur) /* If call didn't fail */
69 max_file_limit= (uint) rlimit.rlim_cur;
72 DBUG_PRINT("exit",("max_file_limit: %u", max_file_limit));
73 DBUG_RETURN(max_file_limit);
76 #else
77 static uint set_max_open_files(uint max_file_limit)
79 /* We don't know the limit. Return best guess */
80 return min(max_file_limit, OS_FILE_LIMIT);
82 #endif
86 Change number of open files
88 SYNOPSIS:
89 my_set_max_open_files()
90 files Number of requested files
92 RETURN
93 number of files available for open
96 uint my_set_max_open_files(uint files)
98 struct st_my_file_info *tmp;
99 DBUG_ENTER("my_set_max_open_files");
100 DBUG_PRINT("enter",("files: %u my_file_limit: %u", files, my_file_limit));
102 files= set_max_open_files(min(files, OS_FILE_LIMIT));
103 if (files <= MY_NFILE)
104 DBUG_RETURN(files);
106 if (!(tmp= (struct st_my_file_info*) my_malloc(sizeof(*tmp) * files,
107 MYF(MY_WME))))
108 DBUG_RETURN(MY_NFILE);
110 /* Copy any initialized files */
111 memcpy((char*) tmp, (char*) my_file_info,
112 sizeof(*tmp) * min(my_file_limit, files));
113 bzero((char*) (tmp + my_file_limit),
114 max((int) (files- my_file_limit), 0)*sizeof(*tmp));
115 my_free_open_file_info(); /* Free if already allocated */
116 my_file_info= tmp;
117 my_file_limit= files;
118 DBUG_PRINT("exit",("files: %u", files));
119 DBUG_RETURN(files);
123 void my_free_open_file_info()
125 DBUG_ENTER("my_free_file_info");
126 if (my_file_info != my_file_info_default)
128 /* Copy data back for my_print_open_files */
129 memcpy((char*) my_file_info_default, my_file_info,
130 sizeof(*my_file_info_default)* MY_NFILE);
131 my_free((char*) my_file_info, MYF(0));
132 my_file_info= my_file_info_default;
133 my_file_limit= MY_NFILE;
135 DBUG_VOID_RETURN;