Index: /branches/rel_apv_10_7/usr/click/bin/webui_agent/webui_agent.c
===================================================================
--- /branches/rel_apv_10_7/usr/click/bin/webui_agent/webui_agent.c	(revision 38526)
+++ /branches/rel_apv_10_7/usr/click/bin/webui_agent/webui_agent.c	(working copy)
@@ -38,6 +38,7 @@
 #include "englog.h"
 #include <click/sys/clickarray.h>
 #include <uinet_api.h>
+#include <syslog.h>
 
 #define BUFFER_SIZE 4096
 #define F_EOU 253         /*end of username */
@@ -92,6 +93,33 @@
 #include <time.h>
 extern char *program_invocation_short_name;
 static int debug_mode = 0;
+
+void delete_empty_node_by_seesion_id(char *sess_id)
+{
+    struct list_node *current_p = NULL;
+    struct list_node *previous_p = NULL;
+
+	previous_p = agent_list;
+	current_p = agent_list;
+
+
+	while (current_p != NULL) {
+		if (strcmp(current_p->sess_id, sess_id) != 0) {
+			previous_p = current_p;
+			current_p = current_p->next;
+		}
+		else {
+			previous_p->next = current_p->next;
+			free(current_p);
+			if (current_p == agent_list){
+				agent_list = NULL;
+			}
+			break;
+		}
+	}
+	return;
+}
+
 void debug1(const char *format, ...)
 {
     char buf1[4096];
@@ -284,6 +312,7 @@
 			englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "WebUI Agent: select -- %s\n", strerror(errno));
 			englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, 
 						"webui agent select error, errno = %d", errno);
+			syslog(LOG_ERR, "webui agent select error, errno = %d", errno);
 			if (errno == EINTR) {
 				englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "EINTR: %s\n", strerror(errno));
 				continue;
@@ -320,6 +349,7 @@
 		englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG,
                               "webui agent get command failed");      
 		close(web_fd);
+		syslog(LOG_ERR, "webui agent get command failed");
 		continue;
 	    }
 
@@ -329,6 +359,7 @@
 		englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, 
 						"webui agent cannot break cmd and sess_id");
 		close(web_fd);
+		syslog(LOG_ERR, "webui agent cannot break cmd and sess_id");
 		continue;
 	    }
 	    insert_sep(cmd);
@@ -337,6 +368,7 @@
 	    if (break_in_token(sess_id, username, buff, F_EOU) == 0){
 		englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "couldn't extract username");
 		close(web_fd);
+		syslog(LOG_ERR, "webui agent couldn't extract username");
 		continue;
 	    }
 
@@ -349,6 +381,7 @@
                 englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG,
                                                 "webui agent couldn't find F_EOS");
                 close(web_fd);
+				syslog(LOG_ERR, "webui agent couldn't find F_EOS");
                 continue;
 
 	    }
@@ -418,6 +451,11 @@
 			englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, 
 				"webui agent socketpair failed, errno = %d", errno);
 		    englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "WebUI Agent: pipe error");
+			close(web_fd);
+			web_fd = -1;
+			delete_empty_node_by_seesion_id(sess_id);
+					
+			syslog(LOG_ERR, "webui agent socketpair failed, errno = %d",errno);
 		    continue;
 		}
 
@@ -425,7 +463,10 @@
 			englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, 
 						"webui agent fork ca_shell error, errno = %d", errno);
 		    //continue;
+			close(web_fd);					
+			delete_empty_node_by_seesion_id(sess_id);
 		    web_fd = -1;
+			syslog(LOG_ERR, "webui agent fork ca_shell error, errno = %d",errno);
 		}
  
 		if (ca_pid > 0) {
@@ -510,6 +551,7 @@
 		    if (write(current_p->cli_fd, cmd, strlen(cmd)) != strlen(cmd)) {
 			englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, 
 					"webui agent write error to pipe, errno = %d", errno);
+				syslog(LOG_ERR, "webui agent write error to pipe, errno = %d",errno);
 		    }
 		    debug1("write1 to cli_fd(%d): [%s]", current_p->cli_fd, cmd);
 		}
@@ -629,7 +671,8 @@
 			if (read_write_until_token1(current_p->web_fd, -1, F_EOS, buff)<0) {
 			    englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG,
                            		  "webui agent read_write_until_token1 failed");      
-			    close(current_p->web_fd);
+				syslog(LOG_ERR, "webui agent read_write_until_token1 failed");
+                close(current_p->web_fd);
 			    FD_CLR(current_p->web_fd, &afds);
 			}
 			else {
@@ -637,12 +680,14 @@
 					englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, 
 					"webui agent break_in_toke returns 0");
 				englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "WebUI Agent: couldn't break the cmd and sess_id\n");
+					syslog(LOG_ERR, "WebUI Agent: couldn't break the cmd and sess_id");
 			    }
 			    insert_sep(cmd);
 			    /* here move user name to username from sess_id */
 			    if (break_in_token(sess_id, username, buff, F_EOU) == 0) {
 				englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "couldn't extract username");
-				close(web_fd);
+
+				syslog(LOG_ERR, "WebUI Agent: couldn't extract username");
 				continue;
 			    }
 			    englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "username = %s", username);
@@ -660,11 +705,13 @@
 					englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, 
 						"webui agent write error to pipe inside if "
 						"current_p->web_fd > 0, errno = %d", errno);
+					syslog(LOG_ERR, "webui agent write error to cli errno = %d",errno);
 				}
 		                debug1("write3 to cli_fd(%d): [%s]", current_p->cli_fd, cmd);
 			    }
 			    else {
 					englog(ENGLOG_WEBUI, WEBUI_AGENT_DEBUG, "WebUI Agent: couldn't break the cmd/sess_id or couldn't find F_EOS\n");
+					syslog(LOG_ERR, "WebUI Agent: couldn't break the cmd/sess_id or couldn't find F_EOS");
 			    }
 			}
 		    }
