Browse code

Use 'H' instead of 'F1', add support for older clamds (bb #1429, thanks for the suggestion to Fabrio Pedretti <pedretti*eco.unibs.it>).

git-svn: trunk@4876

Török Edvin authored on 2009/03/01 19:15:27
Showing 2 changed files
... ...
@@ -1,3 +1,9 @@
1
+Sun Mar  1 11:29:32 EET 2009 (edwin)
2
+------------------------------------
3
+ * clamdtop/clamdtop.c: Use 'H' instead of 'F1', add support for
4
+ older clamds (bb #1429, thanks for the suggestion to Fabrio Pedretti
5
+ <pedretti*eco.unibs.it>).
6
+
1 7
 Sun Mar  1 11:03:40 EET 2009 (edwin)
2 8
 ------------------------------------
3 9
  * clamdtop/clamdtop.c: Fix gccism, thanks Ed <lunytun*gmail.com>.
... ...
@@ -96,7 +96,7 @@ struct stats {
96 96
 static void cleanup(void);
97 97
 static int send_string_noreconn(conn_t *conn, const char *cmd);
98 98
 static void send_string(conn_t *conn, const char *cmd);
99
-static void read_version(conn_t *conn);
99
+static int read_version(conn_t *conn);
100 100
 
101 101
 enum exit_reason {
102 102
         FAIL_CMDLINE=1,
... ...
@@ -253,7 +253,7 @@ static void init_windows(int num_clamd)
253 253
 	werase(stdscr);
254 254
 	refresh();
255 255
 	memset(status_bar_keys, 0, sizeof(status_bar_keys));
256
-	status_bar_keys[0] = "F1 - help";
256
+	status_bar_keys[0] = "H - help";
257 257
 	status_bar_keys[1] = "Q - quit";
258 258
 	status_bar_keys[2] = "R - reset maximums";
259 259
 	if (num_clamd > 1) {
... ...
@@ -494,7 +494,7 @@ static void print_con_info(conn_t *conn, const char *fmt, ...)
494 494
 }
495 495
 
496 496
 
497
-static int make_connection(const char *soname, conn_t *conn)
497
+static int make_connection_real(const char *soname, conn_t *conn)
498 498
 {
499 499
 	int s;
500 500
 	struct timeval tv;
... ...
@@ -558,13 +558,31 @@ static int make_connection(const char *soname, conn_t *conn)
558 558
 	tv.tv_sec = 4;
559 559
 	tv.tv_usec = 0;
560 560
 	setsockopt(conn->sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
561
-	send_string(conn, "nIDSESSION\nnVERSION\n");
562
-	free(conn->version);
563
-	conn->version = NULL;
564
-	read_version(conn);
565 561
 	return 0;
566 562
 }
567 563
 
564
+static int make_connection(const char *soname, conn_t *conn)
565
+{
566
+    int rc;
567
+
568
+    if ((rc = make_connection_real(soname, conn)))
569
+	return rc;
570
+    send_string(conn, "nIDSESSION\nnVERSION\n");
571
+    free(conn->version);
572
+    conn->version = NULL;
573
+    if (!read_version(conn))
574
+	return 0;
575
+
576
+    /* clamd < 0.95 */
577
+    if ((rc = make_connection_real(soname, conn)))
578
+	return rc;
579
+    send_string(conn, "nSESSION\nnVERSION\n");
580
+    conn->version = NULL;
581
+    if (!read_version(conn))
582
+	return 0;
583
+    return -1;
584
+}
585
+
568 586
 static void reconnect(conn_t *conn);
569 587
 
570 588
 static int send_string_noreconn(conn_t *conn, const char *cmd)
... ...
@@ -587,6 +605,8 @@ static void reconnect(conn_t *conn)
587 587
 	if(++tries > 3) {
588 588
 		EXIT_PROGRAM(RECONNECT_FAIL);
589 589
 	}
590
+	if (conn->sd != -1)
591
+	    close(conn->sd);
590 592
 	if (make_connection(conn->remote, conn) < 0) {
591 593
 		print_con_info(conn, "Unable to reconnect to %s: %s", conn->remote, strerror(errno));
592 594
 		EXIT_PROGRAM(RECONNECT_FAIL);
... ...
@@ -777,7 +797,7 @@ static int output_stats(struct stats *stats, unsigned idx)
777 777
 
778 778
 	OOM_CHECK(line);
779 779
 
780
-	if (stats->mem >= 0 || stats->stats_unsupp)
780
+	if (stats->mem <= 0 || stats->stats_unsupp)
781 781
 		strncpy(mem, "N/A", sizeof(mem));
782 782
 	else {
783 783
 		char c;
... ...
@@ -802,7 +822,7 @@ static int output_stats(struct stats *stats, unsigned idx)
802 802
 	else
803 803
 		snprintf(timbuf, sizeof(timbuf), "%04u-%02u-%02u %02uh",
804 804
 				1900 + stats->db_time.tm_year,
805
-				stats->db_time.tm_mon,
805
+				stats->db_time.tm_mon+1,
806 806
 				stats->db_time.tm_mday,
807 807
 				stats->db_time.tm_hour);
808 808
 
... ...
@@ -840,7 +860,6 @@ static int output_stats(struct stats *stats, unsigned idx)
840 840
 		snprintf(buf, sizeof(buf), "live %3u idle %3u max %3u", stats->prim_live, stats->prim_idle, stats->prim_max);
841 841
 		print_colored(win, buf);
842 842
 		show_bar(win, i++, stats->prim_live, stats->prim_idle, stats->prim_max, 0);
843
-
844 843
 /*		mvwprintw(win, i++, 0, "Multiscan pool : ");
845 844
 		snprintf(buf, sizeof(buf), "live %3u idle %3u max %3u", stats->live, stats->idle, stats->max);
846 845
 		print_colored(win, buf);
... ...
@@ -855,6 +874,7 @@ static int output_stats(struct stats *stats, unsigned idx)
855 855
 		snprintf(buf, sizeof(buf), "%6u items %6u max", stats->current_q, stats->biggest_queue);
856 856
 		print_colored(win, buf);
857 857
 		show_bar(win, i++, stats->current_q, 0, stats->biggest_queue, blink);
858
+		i += 2;
858 859
 		werase(mem_window);
859 860
 		output_memstats(stats);
860 861
 	}
... ...
@@ -1015,19 +1035,21 @@ static void parse_stats(conn_t *conn, struct stats *stats, unsigned idx)
1015 1015
 	}
1016 1016
 }
1017 1017
 
1018
-static void read_version(conn_t *conn)
1018
+static int read_version(conn_t *conn)
1019 1019
 {
1020 1020
 	char buf[1024];
1021
-	if(recv_line(conn, buf, sizeof(buf))) {
1022
-		unsigned i;
1023
-		conn->version = strdup(buf);
1024
-		OOM_CHECK(conn->version);
1025
-		for (i=0;i<strlen(conn->version);i++)
1026
-			if (conn->version[i] == '\n')
1027
-				conn->version[i] = ' ';
1028
-	} else {
1029
-		reconnect(conn);
1030
-	}
1021
+	unsigned i;
1022
+	if(!recv_line(conn, buf, sizeof(buf)))
1023
+	    return -1;
1024
+	if (!strcmp(buf, "UNKNOWN COMMAND\n"))
1025
+	    return -2;
1026
+
1027
+	conn->version = strdup(buf);
1028
+	OOM_CHECK(conn->version);
1029
+	for (i=0;i<strlen(conn->version);i++)
1030
+		    if (conn->version[i] == '\n')
1031
+			conn->version[i] = ' ';
1032
+	return 0;
1031 1033
 }
1032 1034
 
1033 1035
 static void sigint(int a)
... ...
@@ -1226,7 +1248,7 @@ int main(int argc, char *argv[])
1226 1226
 
1227 1227
 	memset(&tv_last, 0, sizeof(tv_last));
1228 1228
 	do {
1229
-		if (ch == KEY_F(1)) {
1229
+		if (toupper(ch) == 'H') {
1230 1230
 			ch = show_help();
1231 1231
 		}
1232 1232
 		switch(ch) {
... ...
@@ -1237,6 +1259,7 @@ int main(int argc, char *argv[])
1237 1237
 				init_windows(global.num_clamd);
1238 1238
 				break;
1239 1239
 			case 'R':
1240
+			case 'r':
1240 1241
 				for (i=0;i<global.num_clamd;i++)
1241 1242
 					global.all_stats[i].biggest_queue = 1;
1242 1243
 				biggest_mem = 0;