28 #ifndef NEW_HTTP_SERVER_DISABLE
32 #include "hostIf_tr69ReqHandler.h"
35 #ifndef NEW_HTTP_SERVER_DISABLE
37 #include "http_server.h"
38 #include "hostIf_utils.h"
41 #include "hostIf_updateHandler.h"
42 #include "XrdkCentralComBSStore.h"
44 #if defined(USE_WIFI_PROFILE)
49 #include <semaphore.h>
53 #define LOG_FILE "./tr69hostIflog.txt"
54 #ifdef WEBPA_RFC_ENABLED
55 #define RFC_BUFFER_SIZE 256
58 #ifdef ENABLE_SD_NOTIFY
59 #include <systemd/sd-daemon.h>
62 #include <condition_variable>
64 #ifdef WEB_CONFIG_ENABLED
74 #ifdef WEBCONFIG_LITE_ENABLE
75 #include<webconfig_lite.h>
80 #include "safec_lib.h"
85 GThread *hostIf_JsonIfThread = NULL;
86 GThread *updateHandler_runThread = NULL;
87 #ifndef NEW_HTTP_SERVER_DISABLE
88 GThread *HTTPServerThread = NULL;
89 #define LEGACY_RFC_ENABLED_PATH "/opt/RFC/.RFC_LegacyRFCEnabled.ini"
92 static GMainLoop *main_loop = NULL;
93 gchar *date_str = NULL;
94 const gchar* logfilename = NULL;
96 int rdk_logger_enabled = 0;
97 GHashTable* paramMgrhash = NULL;
100 static int isShutdownTriggered = 0;
102 std::mutex mtx_httpServerThreadDone;
103 std::condition_variable cv_httpServerThreadDone;
104 bool httpServerThreadDone =
false;
107 pthread_t parodus_init_tid;
109 #ifdef WEBCONFIG_LITE_ENABLE
110 pthread_t webconfig_threadId;
112 char *hostIf_JsonIfMsg = (
char *)
"hostIf_JsonIfThread";
113 #ifndef NEW_HTTP_SERVER_DISABLE
114 char *HTTPServerName = (
char *)
"HTTPServerThread";
115 GError *httpError = NULL;
117 GError *err1 = NULL ;
121 static pthread_t shutdown_thread = 0;
122 static void *shutdown_thread_entry(
void *arg);
123 static sem_t shutdown_thread_sem;
124 static int shutdown_sig_received = 0;
125 pthread_mutex_t graceful_exit_mutex;
126 #define UNUSED(x) ((void)(x))
132 static void *shutdown_thread_entry(
void *arg)
137 while ((sem_wait(&shutdown_thread_sem) == -1) && (errno == EINTR))
142 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"[%s:%s] TR69 Host-If received signal handler with sigNum : %d \n",__FILE__, __FUNCTION__, shutdown_sig_received);
144 switch (shutdown_sig_received)
147 RDK_LOG(RDK_LOG_NOTICE,LOG_TR69HOSTIF,
"SIGINT received (CTRL+C was pressed) \n");
150 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"SIGTERM received\n");
153 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"SIGQUIT received\n");
156 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"SIGSEGV received (Segmentation fault was detected)\n");
159 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Signal %d received\n", shutdown_sig_received);
163 exit_gracefully(shutdown_sig_received);
167 #ifdef WEBPA_RFC_ENABLED
169 int GetFeatureEnabled(
char *cmd)
171 FILE * pipeStream = NULL;
172 char buffer[RFC_BUFFER_SIZE];
173 int isFeatureEnabled = 0;
175 memset(buffer, 0, RFC_BUFFER_SIZE);
176 pipeStream = popen(cmd,
"r");
177 if (pipeStream != NULL)
179 if (fgets(buffer, RFC_BUFFER_SIZE, pipeStream) != NULL)
180 sscanf(buffer,
"%d",&isFeatureEnabled);
182 RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF,
"[%s] %s End of stream.\n", __FUNCTION__, cmd);
185 return isFeatureEnabled;
195 int main(
int argc,
char *argv[])
199 #ifdef WEBPA_RFC_ENABLED
202 const char* debugConfigFile = NULL;
203 const char* webpaNotifyConfigFile = NULL;
207 struct sigaction sigact;
212 static struct option long_options[] =
216 {
"help", no_argument, 0,
'h'},
217 {
"logfile", required_argument, 0,
'l'},
218 {
"conffile", required_argument, 0,
'c'},
219 {
"port", required_argument, 0,
'p'},
220 #ifndef NEW_HTTP_SERVER_DISABLE
221 {
"httpserverport", required_argument, 0,
's'},
223 {
"debugconfig", required_argument, 0,
'd'},
224 {
"notifyconfig", required_argument, 0,
'w'},
229 int option_index = 0;
230 #ifndef NEW_HTTP_SERVER_DISABLE
231 ch = getopt_long (argc, argv,
"hHl:c:p:s:d:w:",
232 long_options, &option_index);
234 ch = getopt_long (argc, argv,
"hHl:c:p:d:w:",
235 long_options, &option_index);
246 memset(argList.confFile,
'\0', sizeof (argList.confFile));
247 rc=strcpy_s (argList.confFile,
sizeof(argList.confFile),optarg);
259 debugConfigFile = optarg;
265 webpaNotifyConfigFile = optarg;
272 argList.httpPort = atoi(optarg);
275 #ifndef NEW_HTTP_SERVER_DISABLE
279 argList.httpServerPort = atoi(optarg);
297 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"non-option ARGV-elements: ");
298 while (optind < argc)
299 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"%s ", argv[optind++]);
305 #ifdef WEBPA_RFC_ENABLED
306 retVal = GetFeatureEnabled(
". /lib/rdk/isFeatureEnabled.sh WEBPAXG");
307 RDK_LOG(RDK_LOG_INFO, LOG_TR69HOSTIF,
"[%s] WEBPAXG returns %d\n", __FUNCTION__, retVal);
310 system(
"systemctl stop tr69hostif.service");
315 if (sem_init(&shutdown_thread_sem, 0, 0) == -1)
317 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%s] sem_init() failed\n", __FUNCTION__, __FILE__);
321 if (pthread_create(&shutdown_thread, NULL, shutdown_thread_entry, NULL) != 0)
323 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s:%s] pthread_create() failed\n", __FUNCTION__, __FILE__);
328 sigemptyset(&sigact.sa_mask);
329 sigact.sa_handler = quit_handler;
330 sigact.sa_flags = SA_ONSTACK;
332 sigaction (SIGINT, &sigact, NULL);
333 sigaction (SIGTERM, &sigact, NULL);
334 sigaction (SIGHUP, &sigact, NULL);
335 #ifndef RDK_DEVICE_CISCO_XI4
337 sigaction (SIGSEGV, &sigact, NULL);
338 sigaction (SIGILL, &sigact, NULL);
339 sigaction (SIGFPE, &sigact, NULL);
340 sigaction (SIGABRT, &sigact, NULL);
341 sigaction (SIGQUIT, &sigact, NULL);
344 signal (SIGPIPE, SIG_IGN);
346 setvbuf(stdout, NULL, _IOLBF, 0);
351 #if GLIB_VERSION_CUR_STABLE <= GLIB_VERSION_2_32
352 if(!g_thread_supported())
355 RDK_LOG(RDK_LOG_NOTICE,LOG_TR69HOSTIF,
"g_thread supported\n");
359 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"g_thread NOT supported\n");
365 #if defined(USE_WIFI_PROFILE)
367 (void)WiFiDevice::init();
374 char* logoutfile = (
char *)argList.logFileName;
377 g_log_set_handler(G_LOG_DOMAIN, (GLogLevelFlags)(G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE | \
378 G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | \
379 G_LOG_LEVEL_ERROR), tr69hostIf_logger, (
void *)logoutfile);
381 RDK_LOG(RDK_LOG_NOTICE,LOG_TR69HOSTIF,
"Starting tr69HostIf Service\n");
391 if(
false == hostIf_initalize_ConfigManger())
393 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Failed to hostIf_initalize_ConfigManger()\n");
396 #ifndef NEW_HTTP_SERVER_DISABLE
397 ifstream ifs_legacyEnabled(LEGACY_RFC_ENABLED_PATH);
398 if(!ifs_legacyEnabled.is_open())
400 setLegacyRFCEnabled(
false);
404 setLegacyRFCEnabled(
true);
405 ifs_legacyEnabled.close();
409 if(
false == hostIf_IARM_IF_Start() )
411 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Failed to start hostIf_IARM_IF_Start()\n");
415 DB_STATUS status = loadDataModel();
416 if(status != DB_SUCCESS)
418 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Error in Data Model Initialization\n");
423 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Successfully initialize Data Model.\n");
429 if( (hostIf_JsonIfThread = g_thread_create( (GThreadFunc)jsonIfHandlerThread,
430 (
void *)hostIf_JsonIfMsg,
434 g_critical(
"Thread create failed: %s!!\n", err1->message );
439 #ifndef NEW_HTTP_SERVER_DISABLE
440 if(!legacyRFCEnabled())
442 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"legacyRFC Set to False, Starting New HTTP Server\n");
443 if((HTTPServerThread = g_thread_create((GThreadFunc)HTTPServerStartThread,
444 (
void *)HTTPServerName,
446 &httpError)) == NULL)
448 g_critical(
"New HTTP Server Thread Create failed: %s!!\n", httpError->message );
449 g_error_free (httpError);
454 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"legacyRFC Set to True, New HTTP Server is not started\n");
459 #define xstr(s) str(s)
468 updateHandler::Init();
474 RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,
"Starting WEBPA Parodus Connections\n");
478 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"Initiating Connection with PARODUS success.. \n");
482 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Parodus init thread create failed\n");
486 #if defined(WEB_CONFIG_ENABLED)
487 initWebConfigMultipartTask(0);
488 #elif defined(WEBCONFIG_LITE_ENABLE)
489 if(0 == pthread_create(&webconfig_threadId, NULL, initWebConfigTask, NULL))
491 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"webconfig thread created success.. \n");
495 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"webconfig thread created failed.. \n");
500 init_rbus_dml_provider();
503 if (httpServerThreadDone ==
false)
505 std::unique_lock<std::mutex> lck(mtx_httpServerThreadDone);
506 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"Waiting(max 10 sec) for http server thread to be complete...\n");
507 auto sec = chrono::seconds(1);
508 cv_httpServerThreadDone.wait_for(lck, 10*sec, [] {
return httpServerThreadDone;});
510 #ifdef ENABLE_SD_NOTIFY
511 sd_notifyf(0,
"READY=1\n"
512 "STATUS=tr69hostif is Successfully Initialized\n"
513 "MAINPID=%lu", (
unsigned long) getpid());
514 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"tr69hostif sd notify envent is sent Successfully, httpServerThreadDone=%d\n", httpServerThreadDone);
517 hostIf_DeviceInfo::send_DeviceManageableNotification();
519 main_loop = g_main_loop_new (NULL, FALSE);
522 g_main_loop_run(main_loop);
523 g_main_loop_unref (main_loop);
526 RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,
"[%s]Fails to Create a main loop.", __FUNCTION__);
529 if(hostIf_JsonIfThread)
530 g_thread_join(hostIf_JsonIfThread);
532 #ifndef NEW_HTTP_SERVER_DISABLE
534 g_thread_join(HTTPServerThread);
537 pthread_join(parodus_init_tid,NULL);
539 RDK_LOG(RDK_LOG_INFO,LOG_TR69HOSTIF,
"\n\n----------------------EXITING MAIN PROGRAM----------------------\n");
548 return (pid_t)syscall(__NR_gettid);
560 void quit_handler (
int sig_received)
564 shutdown_sig_received = sig_received;
565 (void)sem_post(&shutdown_thread_sem);
572 void exit_gracefully (
int sig_received)
575 if((pthread_mutex_trylock(&graceful_exit_mutex) == 0) && (isShutdownTriggered == 0)) {
576 RDK_LOG(RDK_LOG_NOTICE,LOG_TR69HOSTIF,
"[%s:%s] Entering..\n", __FUNCTION__, __FILE__);
577 isShutdownTriggered = 1;
579 #if defined(USE_WIFI_PROFILE)
581 WiFiDevice::shutdown();
590 updateHandler::stop();
591 XBSStore::getInstance()->stop();
593 if(logfile) fclose (logfile);
596 g_hash_table_destroy(paramMgrhash);
599 hostIf_IARM_IF_Stop();
601 RDK_LOG(RDK_LOG_NOTICE,LOG_TR69HOSTIF,
"[%s:%s] Exiting program gracefully..\n", __FUNCTION__, __FILE__);
602 if (g_main_loop_is_running(main_loop)) {
603 g_main_loop_quit(main_loop);
604 #ifndef NEW_HTTP_SERVER_DISABLE
609 pthread_mutex_unlock(&graceful_exit_mutex);
619 #ifndef NEW_HTTP_SERVER_DISABLE
620 cout <<
"\nUsage: tr69hostIf -p[http port] [-l][LogFileName]\n\
621 \nDisplay tr69hostIf options:\n\
622 -p HTTP PORT HTTP Server port number.\n\
623 -s HTTP PORT New HTTP Server Port. \n\
624 -l LOG FILE NAME Output log file name with path.\n\
625 -c CONFIG FILE NAME Configure file contains Manager list .\n\
626 -d DEBUG CONFIG FILE NAME Config file contain debug configurations. \n\
629 ================================================================================\n\
630 tr69hostIf -l /opt/logs/tr69hostIf.log -c mgrlist.conf -p 5001 -d /etc/debug.ini\n\
631 ================================================================================\n\
634 cout <<
"\nUsage: tr69hostIf -p[http port] [-l][LogFileName]\n\
635 \nDisplay tr69hostIf options:\n\
636 -p HTTP PORT HTTP Server port number.\n\
637 -l LOG FILE NAME Output log file name with path.\n\
638 -c CONFIG FILE NAME Configure file contains Manager list .\n\
639 -d DEBUG CONFIG FILE NAME Config file contain debug configurations. \n\
642 ================================================================================\n\
643 tr69hostIf -l /opt/logs/tr69hostIf.log -c mgrlist.conf -p 5001 -d /etc/debug.ini\n\
644 ================================================================================\n\