libutil++  1.9.3
 All Classes Functions Variables
ResourceLimit.cpp
1 /*
2 ** libutil++
3 ** $Id: ResourceLimit.cpp 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: ResourceLimit.cpp 1653 2016-02-28 19:54:59Z sella $";
10 
11 #include "ResourceLimit.h"
12 
13 #include <sys/resource.h>
14 
15 using namespace sella::util;
16 
17 void ResourceLimit::setCoredumpSize(unsigned long size, Type type) throw (ResourceLimitException) {
18  struct rlimit limit;
19 
20  if (type == Both) {
21  limit.rlim_cur = limit.rlim_max = size;
22  } else if (type == Hard) {
23  limit.rlim_max = size;
24  } else {
25  limit.rlim_cur = size;
26  }
27 
28  if (setrlimit(RLIMIT_CORE, &limit) != 0) {
29  THROW2(ResourceLimitException, "Failed to adjust coredump size");
30  }
31 }
32 
33 void ResourceLimit::setFileDescriptors(unsigned long size, Type type) throw (ResourceLimitException) {
34  struct rlimit limit;
35 
36  if (type == Both) {
37  limit.rlim_cur = limit.rlim_max = size;
38  } else if (type == Hard) {
39  limit.rlim_max = size;
40  } else {
41  limit.rlim_cur = size;
42  }
43 
44  if (setrlimit(RLIMIT_NOFILE, &limit) != 0) {
45  THROW2(ResourceLimitException, "Failed to adjust file descriptor limit");
46  }
47 }
48 
49 unsigned long ResourceLimit::getCoredumpSize(Type type) throw (ResourceLimitException) {
50  struct rlimit limit;
51 
52  if (getrlimit(RLIMIT_CORE, &limit) != 0) {
53  THROW2(ResourceLimitException, "Failed to access coredump size");
54  }
55 
56  if (type == Hard) {
57  return limit.rlim_max;
58  }
59 
60  return limit.rlim_cur;
61 }
62 
63 unsigned long ResourceLimit::getFileDescriptors(Type type) throw (ResourceLimitException) {
64  struct rlimit limit;
65 
66  if (getrlimit(RLIMIT_NOFILE, &limit) != 0) {
67  THROW2(ResourceLimitException, "Failed to access file descriptor limit");
68  }
69 
70  if (type == Hard) {
71  return limit.rlim_max;
72  }
73 
74  return limit.rlim_cur;
75 }
76 
77 /*
78 ** vim: noet ts=3 sw=3
79 */