Ticket #424: xapian-omega-sysadmin-limits.diff
File xapian-omega-sysadmin-limits.diff, 4.0 KB (added by , 14 years ago) |
---|
-
runfilter.cc
44 44 # include <csignal> 45 45 #endif 46 46 47 #include " freemem.h"47 #include "configfile.h" 48 48 #include "runfilter.h" 49 49 50 50 #ifdef __WIN32__ … … 85 85 // Connect stdout to our side of the socket pair. 86 86 dup2(fds[1], 1); 87 87 88 // Impose some pretty generousresource limits to prevent run-away88 // Impose resource limits to prevent run-away 89 89 // filter programs from causing problems. 90 90 91 // Limit CPU time to 300 seconds (5 minutes). 92 struct rlimit cpu_limit = { 300, RLIM_INFINITY } ; 93 setrlimit(RLIMIT_CPU, &cpu_limit); 91 if (filter_timelimit) { 92 // Limit CPU time 93 struct rlimit cpu_limit = { filter_timelimit, RLIM_INFINITY } ; 94 setrlimit(RLIMIT_CPU, &cpu_limit); 95 } 96 #if defined RLIMIT_AS || defined RLIMIT_VMEM || defined RLIMIT_DATA 94 97 95 #if defined RLIMIT_AS || defined RLIMIT_VMEM || defined RLIMIT_DATA 96 // Limit process data to 7/8 of free physical memory. 97 long mem = get_free_physical_memory(); 98 if (mem > 0) { 99 mem = (mem / 8) * 7; 100 struct rlimit ram_limit = { mem, RLIM_INFINITY } ; 98 // Limit process data 99 if (filter_memlimit) { 100 struct rlimit ram_limit = { filter_memlimit, RLIM_INFINITY } ; 101 101 #ifdef RLIMIT_AS 102 102 setrlimit(RLIMIT_AS, &ram_limit); 103 103 #elif defined RLIMIT_VMEM … … 109 109 #endif 110 110 } 111 111 #endif 112 112 113 113 execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), (void*)NULL); 114 114 _exit(-1); 115 115 } -
configfile.h
24 24 #define HGUARD_OMEGA_CONFIGFILE 25 25 26 26 #include <string> 27 #include <cstddef> 27 28 using std::string; 28 29 29 30 extern string database_dir; 30 31 extern string template_dir; 31 32 extern string log_dir; 32 33 extern string cdb_dir; 33 34 extern int filter_timelimit; 35 extern size_t filter_memlimit; 34 36 void read_config_file(); 35 37 36 38 #endif /* HGUARD_OMEGA_CONFIGFILE */ -
configfile.cc
42 42 string template_dir = "/var/lib/omega/templates/"; 43 43 string log_dir = "/var/log/omega/"; 44 44 string cdb_dir = "/var/lib/omega/cdb/"; 45 int filter_timelimit = 300; 46 size_t filter_memlimit = 0; /* don't limit memory unless explicitly told to. */ 45 47 48 /** Parse memory figures 49 */ 50 enum pstate_e { 51 PSTATE_INITIAL, 52 PSTATE_BODY, 53 PSTATE_SUFFIX 54 }; 55 56 static size_t 57 parse_size(char *strin) 58 { 59 enum pstate_e pstate = PSTATE_INITIAL; 60 int rv = 0; 61 int negate = 0; 62 char * cptr = strin; 63 64 for (;*cptr != '\0'; cptr++) { 65 switch (*cptr) { 66 case '0': 67 case '1': 68 case '2': 69 case '3': 70 case '4': 71 case '5': 72 case '6': 73 case '7': 74 case '8': 75 case '9': 76 if (pstate == PSTATE_SUFFIX) { 77 goto badparse; 78 } 79 rv = (rv*10) + (*cptr) - '0'; 80 pstate = PSTATE_BODY; 81 break; 82 case 'K': 83 case 'k': 84 if (pstate != PSTATE_BODY) { 85 goto badparse; 86 } 87 rv *= 1024; 88 pstate = PSTATE_SUFFIX; 89 break; 90 case 'M': 91 case 'm': 92 if (pstate != PSTATE_BODY) { 93 goto badparse; 94 } 95 rv *= 1024*1024; 96 pstate = PSTATE_SUFFIX; 97 break; 98 case 'G': 99 case 'g': 100 if (pstate != PSTATE_BODY) { 101 goto badparse; 102 } 103 rv *= 1024 * 1024 * 1024; 104 pstate = PSTATE_SUFFIX; 105 break; 106 default: 107 goto badparse; 108 } 109 } 110 if (pstate == PSTATE_INITIAL) { 111 /* incomplete numbers. */ 112 goto badparse; 113 } 114 if (negate) 115 rv = -rv; 116 return rv; 117 badparse: 118 cerr << "Failed to parse \"" << strin << "\" as size." << endl; 119 return 0; 120 } 121 46 122 /** Return true if the file fname exists. 47 123 */ 48 124 static bool … … 94 170 log_dir = value + "/"; 95 171 } else if (name == "cdb_dir") { 96 172 cdb_dir = value + "/"; 173 } else if (name == "filter_timelimit") { 174 filter_timelimit = atoi(value.c_str()); 175 } else if (name == "filter_memlimit") { 176 filter_memlimit = parse_size(value.c_str()); 97 177 } 98 178 } 99 179