1050{
1052 const char *port_range = prefs_get ("port_range");
1053 const char *p;
1054 struct in6_addr *p_addr;
1055 unsigned int timeout = 0, max_cnx, min_cnx, x;
1057
1058 p = prefs_get ("checks_read_timeout");
1059 if (p != NULL)
1060 timeout = atoi (p);
1061 if (timeout <= 0)
1062 timeout = 5;
1063 {
1064 int max_host = 0, max_checks = 0, cur_sys_fd = 0, max_sys_fd = 0;
1065 struct rlimit rlim;
1066 FILE *fp;
1067 int i;
1068 double loadavg[3], maxloadavg = -1.0;
1069 int stderr_fd = dup (2);
1070 int devnull_fd = open ("/dev/null", O_WRONLY);
1071
1072 if (devnull_fd <= 0)
1073 {
1074 if (stderr_fd != -1)
1075 close (stderr_fd);
1076 return NULL;
1077 }
1078 dup2 (devnull_fd, 2);
1079
1080 p = prefs_get ("max_hosts");
1081 if (p != NULL)
1082 max_host = atoi (p);
1083 if (max_host <= 0)
1084 max_host = 15;
1085
1086 p = prefs_get ("max_checks");
1087 if (p != NULL)
1088 max_checks = atoi (p);
1089 if (max_checks <= 0 || max_checks > 5)
1090 {
1091 max_checks = 5;
1092 g_debug ("openvas_tcp_scanner: max_checks forced to %d", max_checks);
1093 }
1094
1095 min_cnx = 8 * max_checks;
1097 max_cnx = 24 * max_checks;
1098 else
1099 max_cnx = 80 * max_checks;
1100
1101 getloadavg (loadavg, 3);
1102 for (i = 0; i < 3; i++)
1103 if (loadavg[i] > maxloadavg)
1104 maxloadavg = loadavg[i];
1105
1106 if (max_sys_fd <= 0)
1107 {
1108 fp = popen ("sysctl fs.file-nr", "r");
1109 if (fp != NULL)
1110 {
1111 if (fscanf (fp, "%*s = %*d %d %d", &cur_sys_fd, &max_sys_fd) == 1)
1112 max_sys_fd -= cur_sys_fd;
1113 else
1114 max_sys_fd = 0;
1115 pclose (fp);
1116 }
1117 }
1118 if (max_sys_fd <= 0)
1119 {
1120 fp = popen ("sysctl fs.file-max", "r");
1121 if (fp != NULL)
1122 {
1123 if (fscanf (fp, "%*s = %d", &max_sys_fd) < 1)
1124 max_sys_fd = 0;
1125 pclose (fp);
1126 }
1127 }
1128
1129 if (max_sys_fd <= 0)
1130 {
1131 fp = popen ("sysctl kern.maxfiles", "r");
1132 if (fp != NULL)
1133 {
1134 if (fscanf (fp, "%*s = %d", &max_sys_fd) < 1)
1135 max_sys_fd = 0;
1136 pclose (fp);
1137 }
1138 }
1139
1140
1141 close (devnull_fd);
1142 dup2 (stderr_fd, 2);
1143 close (stderr_fd);
1144
1145 if (maxloadavg >= 0.0)
1146 max_cnx /= (1.0 + maxloadavg);
1147
1148 if (max_sys_fd <= 0)
1149 max_sys_fd = 16384;
1150
1151 if (max_sys_fd < 1024)
1153 else
1154 {
1155 max_sys_fd -= 1024;
1156 x = max_sys_fd / max_host;
1157 }
1158 if (max_cnx > x)
1159 max_cnx = x;
1164
1167
1168 if (getrlimit (RLIMIT_NOFILE, &rlim) < 0)
1169 perror ("getrlimit(RLIMIT_NOFILE)");
1170 else
1171 {
1172
1173 if (rlim.rlim_cur != RLIM_INFINITY && max_cnx >= rlim.rlim_cur)
1174 max_cnx = rlim.rlim_cur - 1;
1175 }
1176 x = max_cnx / 2;
1177 if (min_cnx > x)
1178 min_cnx = x > 0 ? x : 1;
1179 }
1180
1182 if (p_addr == NULL)
1183 return NULL;
1184 if (
banner_grab (p_addr, port_range, timeout, min_cnx, max_cnx, desc) < 0)
1185 return NULL;
1188 return NULL;
1189}
#define GRAB_MAX_SOCK_SAFE
static int banner_grab(const struct in6_addr *pia, const char *portrange, const int read_timeout, int min_cnx, int max_cnx, struct script_infos *desc)
tree_cell * safe_checks(lex_ctxt *lexic)
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)