http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / userapps / opensource / busybox / sysklogd / syslogd.c
index 4102182..420c31b 100755 (executable)
@@ -57,6 +57,11 @@ int localLogLevel=-1;
 int remoteLogLevel=-1;
 /*BRCM end*/
 
+/*==== Add by Andrew ====*/
+/*==== Porting by Andy ====*/
+int isWanUp();
+/*==== end ====*/
+
 /* Path for the file where all log messages are written */
 #define __LOG_FILE "/var/log/messages"
 
@@ -483,6 +488,66 @@ static void logMessage(int pri, char *msg)
 /*BRCM end*/
 }
 
+/*==== add by Andrew ====*/
+/*==== Porting by Andy ====*/
+int isWanUp()
+{
+   char data[10];
+   int res = 0;
+
+   FILE* fp = fopen("/var/wanStatus", "r");
+   if ( fp != NULL ) {
+      fgets(data, 10, fp);
+      fclose(fp);
+   }
+
+   if(data[0] == '6') res = 1;
+   return res;
+}
+
+static void Connect2RemoteServ(int sig)
+{
+  struct sockaddr_in remoteaddr;
+  struct hostent *hostinfo=NULL;
+  int len = sizeof(remoteaddr);
+
+  if(remotefd>0) {
+     close(remotefd);
+     remotefd = -1;
+  }
+
+  if(!RemoteHost || !strcmp(RemoteHost, "")) 
+     return;
+
+  hostinfo = xgethostbyname(RemoteHost);
+
+  if(!hostinfo) 
+     return;
+
+  memset(&remoteaddr, 0, len);
+  remotefd = socket(AF_INET, SOCK_DGRAM, 0);
+
+  if (remotefd < 0) {
+    bb_perror_msg("cannot create socket");
+       return;
+  }
+
+  remoteaddr.sin_family = AF_INET;
+  remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
+  remoteaddr.sin_port = htons(RemotePort);
+
+  /*
+     Since we are using UDP sockets, connect just sets the default host and port
+     for future operations
+  */
+  if((connect(remotefd, (struct sockaddr *) &remoteaddr, len)) != 0){
+     close(remotefd);
+     remotefd = -1;
+  }
+  return;
+}
+/*==== end ====*/
+
 static void quit_signal(int sig)
 {
 /*BRCM begin */
@@ -561,6 +626,7 @@ static void init_RemoteLog(void)
 {
        memset(&remoteaddr, 0, sizeof(remoteaddr));
        remotefd = socket(AF_INET, SOCK_DGRAM, 0);
+       int len = sizeof(remoteaddr);
 
        if (remotefd < 0) {
           /* BRCM begin */
@@ -574,6 +640,22 @@ static void init_RemoteLog(void)
        remoteaddr.sin_family = AF_INET;
        remoteaddr.sin_addr = *(struct in_addr *) *(xgethostbyname(RemoteHost))->h_addr_list;
        remoteaddr.sin_port = htons(RemotePort);
+/*==== Modified by Andrew ====*/
+/*==== Porting by Wilson ====*/
+  //if(!isWanUp()) {
+  //   close(remotefd);
+  //   remotefd = -1;
+  //   return;
+  //}
+
+  //if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){
+    //error_msg_and_die("cannot connect to remote host %s:%d", RemoteHost, RemotePort);
+  //}
+  if((connect(remotefd, (struct sockaddr *) &remoteaddr, len)) != 0){
+     close(remotefd);
+     remotefd = -1;
+  }
+/*==== end ====*/      
 }
 #endif
 
@@ -598,7 +680,11 @@ static void doSyslogd(void)
        signal(SIGINT, quit_signal);
        signal(SIGTERM, quit_signal);
        signal(SIGQUIT, quit_signal);
-       signal(SIGHUP, SIG_IGN);
+/*==== Modified by Andrew ====*/
+/*==== Porting by Andy ====*/
+       //signal (SIGHUP,  SIG_IGN);
+       signal (SIGHUP,  Connect2RemoteServ);
+/*==== end ====*/
        signal(SIGCHLD, SIG_IGN);
 #ifdef SIGCLD
        signal(SIGCLD, SIG_IGN);
@@ -664,7 +750,7 @@ static void doSyslogd(void)
 
        /* change to priority emerg to be consistent with klogd */
         /* logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER); */
-       logMessage (LOG_SYSLOG | LOG_EMERG, "BCM96345  started: " BB_BANNER);
+       logMessage (LOG_SYSLOG | LOG_EMERG, "BCM63XX  started: " BB_BANNER);
 
        sprintf (pidfilename, "%s%s.pid", _PATH_VARRUN, "syslogd");
        if ((pidfile = fopen (pidfilename, "w")) != NULL) {