Возникла внештатная ситуация(вернее застрял по самые [классический секс])
имеем такие 5 функций в арсенале
Код: Выделить всё
void ipfw_table(void) {
int i;
syslog(LOG_INFO, "ipfw_table function work");
if (mysql_query(conn, "SELECT `ip`, `bw` FROM `clients` WHERE `status` != 'off';")) {
syslog(LOG_ERR, "Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(EXIT_FAILURE);
}
result = mysql_store_result(conn);
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
for(i = 0; i < num_fields; i++) {
add_bw(row[i],ipfw2_table_in,atoi(row[i+1]));
add_bw(row[i],ipfw2_table_out,atoi(row[i+1]));
i++ ;
}
}
mysql_free_result(result);
}
void add_bw(char* ip, int ipfw_table, int bw) {
char* cmd = malloc(100);
sprintf(cmd, "/sbin/ipfw -q table %d add %s %d",ipfw_table,ip,bw);
execute(cmd);
syslog(LOG_INFO, "IP %15s with BW %10s added to BW-table successfully\n", ip, bw);
free(cmd);
}
void arp_table(void) {
int i;
if (mysql_query(conn, "SELECT `ip`, `mac` FROM `clients` WHERE `mac` != '';")) {
syslog(LOG_ERR, "Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(EXIT_FAILURE);
}
result = mysql_store_result(conn);
int num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result))) {
for(i = 0; i < num_fields; i++) {
add_mac(row[i], row[i+1]);
i++;
}
}
mysql_free_result(result);
}
void add_mac(char* ip, char* mac) {
char* cmd = malloc(100);
sprintf(cmd, "/usr/sbin/arp -s %s %s only",ip,mac);
execute(cmd);
syslog(LOG_INFO, "IP %15s with MAC %10s added to table successfully\n", ip, mac);
free(cmd);
}
char *execute(char* string) {
FILE *fpipe;
char* buff = malloc(100);
if (!(fpipe = (FILE*)popen(string,"r"))) {
syslog(LOG_ERR,"problems with open command pipe");
return 0;
}
fgets(buff, sizeof(buff),fpipe);
pclose(fpipe);
return buff;
}
Проблема:
при вызове комманды add_bw(....) программа падает в корку
но при этом комманда add_mac(....) отрабатывает нормально
В чём подвох?
З.Ы. Знаю что программа написана на костылях, просто набираю функционал постепенно