/* ------------------------------------------------------------------- * Any necesary cleanup before Iperf quits. Called at program exit, * either by exit() or terminating main(). * ------------------------------------------------------------------- */ void cleanup( void ) { #ifdef WIN32 // Shutdown Winsock WSACleanup(); #endif /* WIN32 */ // clean up the list of clients Iperf_destroy ( &clients ); // shutdown the thread subsystem IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Deinitializing the thread subsystem.\n" ) ); thread_destroy( ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report condition.\n" ) ); Condition_Destroy( &ReportCond ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report done condition.\n" ) ); Condition_Destroy( &ReportDoneCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying group condition mutex.\n" ) ); Mutex_Destroy( &groupCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying clients mutex.\n" ) ); Mutex_Destroy( &clients_mutex ); #ifdef IPERF_DEBUG debug_init(); #endif /* IPERF_DEBUG */ } // end cleanup
static int TestSuite_RunSerial (TestSuite *suite) /* IN */ { Test *test; Mutex mutex; int count = 0; int status = 0; Mutex_Init (&mutex); for (test = suite->tests; test; test = test->next) { count++; } for (test = suite->tests; test; test = test->next) { status += TestSuite_RunTest (suite, test, &mutex, &count); count--; } TestSuite_PrintJsonFooter (stdout); if (suite->outfile) { TestSuite_PrintJsonFooter (suite->outfile); } Mutex_Destroy (&mutex); return status; }
static void TestSuite_RunNamed (TestSuite *suite, /* IN */ const char *testname) /* IN */ { Mutex mutex; char name[128]; Test *test; int count = 1; ASSERT (suite); ASSERT (testname); Mutex_Init (&mutex); for (test = suite->tests; test; test = test->next) { snprintf (name, sizeof name, "%s%s", suite->name, test->name); name [sizeof name - 1] = '\0'; if (0 == strcmp (name, testname)) { TestSuite_RunTest (suite, test, &mutex, &count); } } TestSuite_PrintJsonFooter (stdout); if (suite->outfile) { TestSuite_PrintJsonFooter (suite->outfile); } Mutex_Destroy (&mutex); }
static void TestSuite_RunSerial (TestSuite *suite) /* IN */ { Test *test; Mutex mutex; int count = 0; Mutex_Init (&mutex); for (test = suite->tests; test; test = test->next) { count++; } for (test = suite->tests; test; test = test->next) { TestSuite_RunTest (suite, test, &mutex, &count); count--; } TestSuite_PrintJsonFooter (stdout); if (suite->outfile && !suite->generatexmlreport) { TestSuite_PrintJsonFooter (suite->outfile); } Mutex_Destroy (&mutex); }
static int TestSuite_RunNamed (TestSuite *suite, /* IN */ const char *testname) /* IN */ { Mutex mutex; char name[128]; Test *test; int count = 1; bool star = strlen (testname) && testname[strlen (testname) - 1] == '*'; bool match; int status = 0; ASSERT (suite); ASSERT (testname); Mutex_Init (&mutex); for (test = suite->tests; test; test = test->next) { snprintf (name, sizeof name, "%s%s", suite->name, test->name); name [sizeof name - 1] = '\0'; if (star) { /* e.g. testname is "/Client*" and name is "/Client/authenticate" */ match = (0 == strncmp (name, testname, strlen (testname) - 1)); } else { match = (0 == strcmp (name, testname)); } if (match) { status += TestSuite_RunTest (suite, test, &mutex, &count); } } TestSuite_PrintJsonFooter (stdout); if (suite->outfile) { TestSuite_PrintJsonFooter (suite->outfile); } Mutex_Destroy (&mutex); return status; }
/** * @brief close muxer * * @author dengzhou * @date 2013-04-07 * @param[in] * @return T_S32 * @retval if return 0 success, otherwise failed */ int mux_close(void) { T_CHR strFile[MAX_PATH]; printf("%s\n", __func__); bzero(strFile, sizeof(strFile)); sprintf(strFile, "/mnt/index" ); MediaLib_Mux_Stop(hMedia); ak_rec_cb_unload(pfile1); fsync(fid); close(fid); MediaLib_Mux_Close(hMedia); hMedia = AK_NULL; ak_rec_cb_unload(pfile2); fsync(index_fid); close(index_fid); remove(strFile); Mutex_Destroy(&muxMutex); return 0; }
SO_PUBLIC void Thread_Destroy (struct Thread *p_pThread) { List_Lock(sg_threadList); Thread_Lock(p_pThread); // rzb_log(LOG_ERR, "%s: Thread ref count: %d", __func__, p_pThread->refs); // Referece count should not drop below 1 as the list holds a ref. ASSERT(p_pThread->refs >= 1); if (p_pThread->refs > 1) { p_pThread->refs--; Thread_Unlock(p_pThread); List_Unlock(sg_threadList); return; } List_Remove(sg_threadList, p_pThread); // destroy running mutex Thread_Unlock(p_pThread); List_Unlock(sg_threadList); Mutex_Destroy (p_pThread->mMutex); free (p_pThread); }
void debug_destroy() { #if IPERF_DEBUG Mutex_Destroy(&debugprint_mutex); debugprint_mutex_init = 0; #endif /* IPERF_DEBUG */ }
/* ------------------------------------------------------------------- * main() * Entry point into Iperf * * sets up signal handlers * initialize global locks and conditions * parses settings from environment and command line * starts up server or client thread * waits for all threads to complete * ------------------------------------------------------------------- */ #ifdef __cplusplus extern "C" #endif /* __cplusplus */ int IPERF_MAIN( int argc, char **argv ) { #ifdef NO_EXIT should_exit = 0; #endif /* NO_EXIT */ #ifdef IPERF_DEBUG debug_init(); #endif /* IPERF_DEBUG */ #ifndef NO_INTERRUPTS #ifdef WIN32 setsigalrmfunc(call_sigalrm); #endif /* WIN32 */ // Set SIGTERM and SIGINT to call our user interrupt function my_signal( SIGTERM, Sig_Interupt ); my_signal( SIGINT, Sig_Interupt ); my_signal( SIGALRM, Sig_Interupt ); #ifndef WIN32 // Ignore broken pipes signal(SIGPIPE,SIG_IGN); #else // Start winsock WORD wVersionRequested; WSADATA wsaData; // Using MAKEWORD macro, Winsock version request 2.2 wVersionRequested = MAKEWORD(2, 2); int rc = WSAStartup( wVersionRequested, &wsaData ); WARN_errno( rc == SOCKET_ERROR, ( "WSAStartup failed.\n" ) ); if (rc != 0) { fprintf(stderr, "The Winsock DLL was not found!\n"); return 1; } /* * Confirm that the WinSock DLL supports 2.2. Note that if the DLL supports * versions greater than 2.2 in addition to 2.2, it will still return 2.2 in * wVersion since that is the version we requested. */ if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 ) { /* Tell the user that we could not find a usable WinSock DLL. */ fprintf(stderr, "The DLL does not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion)); WSACleanup(); return 1; } // Tell windows we want to handle our own signals SetConsoleCtrlHandler( sig_dispatcher, true ); #endif /* WIN32 */ #endif /* NO_INTERRUPTS */ // Initialize global mutexes and conditions IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Initializing report condition.\n" ) ); Condition_Initialize ( &ReportCond ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Initializing report done condition.\n" ) ); Condition_Initialize ( &ReportDoneCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Initializing group condition mutex.\n" ) ); Mutex_Initialize( &groupCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Initializing clients mutex.\n" ) ); Mutex_Initialize( &clients_mutex ); // Initialize the thread subsystem IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Initializing the thread subsystem.\n" ) ); thread_init( ); // Initialize the interrupt handling thread to 0 sThread = thread_zeroid(); #ifndef NO_EXIT // perform any cleanup when quitting Iperf atexit( cleanup ); #endif /* NO_EXIT */ // Allocate the "global" settings thread_Settings *ext_gSettings = (thread_Settings*) malloc( sizeof( thread_Settings ) ); FAIL( ext_gSettings == NULL, ( "Unable to allocate memory for thread_Settings ext_gSettings.\n" ), NULL ); IPERF_DEBUGF( MEMALLOC_DEBUG, IPERF_MEMALLOC_MSG( ext_gSettings, sizeof( thread_Settings ) ) ); // Initialize settings to defaults Settings_Initialize( ext_gSettings ); #ifndef NO_ENVIRONMENT // read settings from environment variables Settings_ParseEnvironment( ext_gSettings ); #endif /* NO_ENVIORNMENT */ // read settings from command-line parameters Settings_ParseCommandLine( argc, argv, ext_gSettings ); #ifdef NO_EXIT if (should_exit) { IPERF_DEBUGF( MEMFREE_DEBUG | IPERF_DBG_TRACE, IPERF_MEMFREE_MSG( ext_gSettings ) ); FREE_PTR( ext_gSettings ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report condition.\n" ) ); Condition_Destroy( &ReportCond ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report done condition.\n" ) ); Condition_Destroy( &ReportDoneCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying group condition mutex.\n" ) ); Mutex_Destroy( &groupCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying clients mutex.\n" ) ); Mutex_Destroy( &clients_mutex ); return 0; } #endif /* NO_EXIT */ // Check for either having specified client or server if ( ext_gSettings->mThreadMode == kMode_Client || ext_gSettings->mThreadMode == kMode_Listener ) { #ifdef WIN32 #ifndef NO_DAEMON // Start the server as a daemon // Daemon mode for non-windows in handled // in the listener_spawn function if ( isDaemon( ext_gSettings ) ) { CmdInstallService(argc, argv); return 0; } #endif /* NO_DAEMON */ #ifndef NO_SERVICE // Remove the Windows service if requested if ( isRemoveService( ext_gSettings ) ) { // remove the service if ( CmdRemoveService() ) { fprintf(stderr, "IPerf Service is removed.\n"); return 0; } } #endif /* NO_SERVICE */ #endif /* WIN32 */ // initialize client(s) if ( ext_gSettings->mThreadMode == kMode_Client ) { IPERF_DEBUGF( CLIENT_DEBUG | LISTENER_DEBUG | IPERF_DBG_TRACE, ( "Initializing client(s)...\n" ) ); client_init( ext_gSettings ); } #ifdef HAVE_THREAD // start up the reporter and client(s) or listener thread_Settings *into = NULL; // Create the settings structure for the reporter thread IPERF_DEBUGF( CLIENT_DEBUG | LISTENER_DEBUG | REPORTER_DEBUG | IPERF_DBG_TRACE, ( "Creating the settings structure for the reporter thread.\n" ) ); Settings_Copy( ext_gSettings, &into ); into->mThreadMode = kMode_Reporter; // Have the reporter launch the client or listener IPERF_DEBUGF( CLIENT_DEBUG | LISTENER_DEBUG | IPERF_DBG_TRACE, ( "Setting the reporter to launch the client or listener before launching itself.\n" ) ); into->runNow = ext_gSettings; // Start all the threads that are ready to go IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Starting all the threads...\n" ) ); thread_start( into ); #else // No need to make a reporter thread because we don't have threads IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Starting iperf in a single thread...\n" ) ); thread_start( ext_gSettings ); #endif /* HAVE_THREAD */ } else { // neither server nor client mode was specified // print usage and exit #ifdef WIN32 // In Win32 we also attempt to start a previously defined service // Starting in 2.0 to restart a previously defined service // you must call iperf with "iperf -D" or using the environment variable SERVICE_TABLE_ENTRY dispatchTable[] = { { TEXT((char *) SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main}, { NULL, NULL} }; #ifndef NO_DAEMON // Only attempt to start the service if "-D" was specified if ( !isDaemon(ext_gSettings) || // starting the service by SCM, there is no arguments will be passed in. // the arguments will pass into Service_Main entry. !StartServiceCtrlDispatcher(dispatchTable) ) // If the service failed to start then print usage #endif /* NO_DAEMON */ #endif /* WIN32 */ fprintf( stderr, usage_short, argv[0], argv[0] ); return 0; } // wait for other (client, server) threads to complete // IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Waiting for other (client, server) threads to complete...\n" ) ); // thread_joinall(); #ifdef NO_EXIT /* We can't run the atexit function */ #ifdef WIN32 // Shutdown Winsock WSACleanup(); #endif /* WIN32 */ // clean up the list of clients Iperf_destroy ( &clients ); // shutdown the thread subsystem IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Deinitializing the thread subsystem.\n" ) ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report condition.\n" ) ); Condition_Destroy( &ReportCond ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report done condition.\n" ) ); Condition_Destroy( &ReportDoneCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying group condition mutex.\n" ) ); Mutex_Destroy( &groupCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying clients mutex.\n" ) ); Mutex_Destroy( &clients_mutex ); #ifdef IPERF_DEBUG debug_init(); #endif /* IPERF_DEBUG */ #endif /* NO_EXIT */ // all done! IPERF_DEBUGF( IPERF_DBG_TRACE | IPERF_DBG_STATE, ( "Done!\n" ) ); return 0; } // end main
int main(int argc, char ** argv) { mutex = Mutex_Create(MUTEX_MODE_NORMAL); uuid_t uuid; struct FileDefinition * f; int c, daemon = 0; uuid_clear(uuid); int prev = 0; int limit = 0; while (true) { /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long (argc, argv, "c:f:w:t:u:d", options, &option_index); /* Detect the end of the options. */ if (c != -1 && optind > prev) { limit = optind - 1; } else { limit = argc; } for (prev++; prev < limit; prev++) { printf("val = %s, len = %lu\n", argv[prev], strlen(argv[prev])); if (strlen(argv[prev]) == 0) continue; rzb_log (LOG_DEBUG,"Positional argument %d %s",prev, argv[prev]); f = createFileDefinition(argv[prev],uuid); Mutex_Lock(mutex); pushFileList(f); Mutex_Unlock(mutex); } if (c == -1) break; switch (c) { case 'c': rzb_log (LOG_DEBUG,"option -c with value `%s'", optarg); break; case 'f': f = createFileDefinition(optarg,uuid); Mutex_Lock(mutex); pushFileList(f); Mutex_Unlock(mutex); rzb_log (LOG_DEBUG,"option -f with value `%s'", optarg); break; case 'w': rzb_log (LOG_DEBUG,"option -w with value `%s'", optarg); break; case 'd': daemon = 1; break; case 'u': rzb_log (LOG_DEBUG,"option -u with value `%s'", optarg); uuid_parse(optarg,uuid); break; case 't': rzb_log (LOG_DEBUG,"option -t with value `%s'", optarg); UUID_Get_UUID(optarg,UUID_TYPE_DATA_TYPE,uuid); break; case '?': rzb_log (LOG_DEBUG,"no option with value `%s'", optarg); /* getopt_long already printed an error message. */ break; default: rzb_log (LOG_DEBUG,"no option with value `%s'", optarg); //abort (); } } if (daemon) initListenerThread(); //RZB_Init_API(); if (!initNug()) { rzb_log(LOG_ERR, "initNug failed"); exit(-1); } rzb_log(LOG_INFO, "Done initNug"); struct FileDefinition * tmp; while (!endScan) { if (fileList == NULL) { if (daemon == 0) break; usleep(500000); continue; } clearTmpDir(); Mutex_Lock(mutex); tmp = shiftFileList(); if (strlen(tmp->path) == 0) { destroyFileDefinition(tmp); continue; } fprintf(stderr, "value = '%s'\n", tmp == NULL ? "NULL" : tmp->path); Mutex_Unlock(mutex); rzb_log(LOG_NOTICE, "Inspecting %s",tmp->path); if (testFile(tmp->path)) { inspectFile(tmp->path,tmp->uuid); } rzb_log(LOG_NOTICE, "Done inspecting %s",tmp->path); destroyFileDefinition(tmp); rzb_log(LOG_INFO, "FINISHED"); } shutdownNug(); Mutex_Destroy(mutex); rzb_log(LOG_INFO, "Done shutdownNug"); }