int main(int argc, char *argv[]) { int thr_id1, thr_id2; atomic_set(0,&flag); setup(); pass_criteria = THRESHOLD; rt_init("l:h", parse_args, argc, argv); /* we need the buffered print system */ printf("-------------------------------\n"); printf("pthread_kill Latency\n"); printf("-------------------------------\n\n"); printf("Iterations: %d\n", ITERATIONS); debug(DBG_DEBUG, "Main creating threads\n"); fflush(stdout); thr_id1 = create_fifo_thread(signal_receiving_thread, (void*)0, PRIO); thr_id2 = create_fifo_thread(signal_sending_thread, (void*)(intptr_t)thr_id1, PRIO-1); // thr_id2 = create_other_thread(signal_sending_thread, (void*)(intptr_t)thr_id1); debug(DBG_DEBUG, "Main joining threads debug\n"); join_thread(thr_id1); join_thread(thr_id2); buffer_print(); return fail; }
int main() { #ifdef USE_DYNAMO dynamorio_app_init(); dynamorio_app_start(); #endif INIT(); print("calling via IAT-style call\n"); import_me1(57); print("calling in a thread\n"); join_thread(create_thread((fptr)import_me1)); print("calling in a thread that dies\n"); join_thread(create_thread((fptr)import_me_die)); print("case 5455 regression passed\n"); print("all done\n"); #ifdef USE_DYNAMO dynamorio_app_stop(); dynamorio_app_exit(); #endif return 0; }
int main(void) { parent_ready = create_cond_var(); int i; thread_t threads[NUM_PARENT_THREADS]; for (i = 0; i < NUM_PARENT_THREADS; ++i) threads[i] = create_thread(parent_func, NULL); /* We setup and start at once to avoid process memory changing much between * the two. */ dr_app_setup_and_start(); wait_cond_var(parent_ready); thread_sleep(50); dr_app_stop_and_cleanup(); parent_exit = true; for (i = 0; i < NUM_PARENT_THREADS; ++i) join_thread(threads[i]); destroy_cond_var(parent_ready); print("all done\n"); return 0; }
void x11c_clear(X11Common* x11Common) { x11Common->stopped = 1; join_thread(&x11Common->processEventThreadId, NULL, NULL); PTHREAD_MUTEX_LOCK(&x11Common->eventMutex) /* don't allow events to be processed */ kic_free_keyboard_connect(&x11Common->keyboardConnect); pic_free_progress_bar_connect(&x11Common->progressBarConnect); mic_free_mouse_connect(&x11Common->mouseConnect); if (x11Common->createdWindowInfo) { x11c_close_window(&x11Common->windowInfo); x11Common->createdWindowInfo = 0; } SAFE_FREE(&x11Common->windowName); x11Common->osd = NULL; PTHREAD_MUTEX_UNLOCK(&x11Common->eventMutex) destroy_mutex(&x11Common->eventMutex); memset(x11Common, 0, sizeof(x11Common)); }
void init_data(t_shared shared) { int k; int i; t_data data[7]; pthread_t philosophe[7]; i = 0; while (i < 7) { data[i].i = i; data[i].shared = &shared; data[i].life = MAX_LIFE; data[i].etat = 0; data[i].prior = 0; data[i].shared->pos = 7; if ((k = pthread_create(&philosophe[i], NULL, thread_1, &data[i])) != 0) { ft_putstr("Thread creation error \n"); exit(1); } i++; } init_time(data); join_thread(philosophe); }
int rumpuser_thread_join(void *p) { join_thread(p); return 0; }
void simple_test(func_args* args) { THREAD_TYPE serverThread; func_args svrArgs; char *svrArgv[NUMARGS]; char argc0s[32]; char argc1s[32]; char argc2s[32]; func_args cliArgs; char *cliArgv[NUMARGS]; char argc0c[32]; char argc1c[32]; char argc2c[32]; svrArgv[0] = argc0s; svrArgv[1] = argc1s; svrArgv[2] = argc2s; cliArgv[0] = argc0c; cliArgv[1] = argc1c; cliArgv[2] = argc2c; svrArgs.argc = 1; svrArgs.argv = svrArgv; svrArgs.return_code = 0; cliArgs.argc = 1; cliArgs.argv = cliArgv; cliArgs.return_code = 0; strcpy(svrArgs.argv[0], "SimpleServer"); #if !defined(USE_WINDOWS_API) && !defined(CYASSL_SNIFFER) svrArgs.argc = NUMARGS; strcpy(svrArgs.argv[1], "-p"); strcpy(svrArgs.argv[2], "0"); #endif /* Set the last arg later, when it is known. */ args->return_code = 0; svrArgs.signal = args->signal; start_thread(server_test, &svrArgs, &serverThread); wait_tcp_ready(&svrArgs); /* Setting the actual port number. */ strcpy(cliArgs.argv[0], "SimpleClient"); #ifndef USE_WINDOWS_API cliArgs.argc = NUMARGS; strcpy(cliArgs.argv[1], "-p"); snprintf(cliArgs.argv[2], sizeof(argc2c), "%d", svrArgs.signal->port); #endif client_test(&cliArgs); if (cliArgs.return_code != 0) { args->return_code = cliArgs.return_code; return; } join_thread(serverThread); if (svrArgs.return_code != 0) args->return_code = svrArgs.return_code; }
static void term_sockdev ( void* arg ) { UNREFERENCED( arg ); if (!init_done) init_sockdev(); SIGNAL_SOCKDEV_THREAD(); join_thread ( sysblk.socktid, NULL ); detach_thread ( sysblk.socktid ); }
int main(int argc, char *argv[]) { int worker, interrupter; setup(); rt_init("h",parse_args,argc,argv); interrupter = create_fifo_thread(thread_interrupter, NULL, 80); sleep(1); worker = create_fifo_thread(thread_worker, NULL, 10); join_thread(worker); flag = 1; join_thread(interrupter); return 0; }
ReplayAudioIngest::~ReplayAudioIngest( ) { stop = true; join_thread( ); /* clean up per-channel buffers */ for (channel_entry &e : channel_map) { delete [] e.current_frame; delete [] e.last_frame; delete e.fifo; } }
void main_move() { PCZ_TITLE("std::move operation: threads"); std::function<void(int, int)> fun = &print_add; std::thread thread(fun, 1, 1); #if METHOD_NR == 1 join_thread(std::move(thread)); #elif METHOD_NR == 2 join_thread(thread); #elif METHOD_NR == 3 std::thread thread2 (std::move(thread)); // std::thread thread2 (thread); // error: use of deleted function ‘std::thread::thread(std::thread&)’ PCZ_DEBUG("thread.joinable() = %d", thread.joinable()); // -> 0 PCZ_DEBUG("thread2.joinable() = %d", thread2.joinable()); // -> 1 thread_joiner(std::move(thread2)); #endif PCZ_TITLE("std::move operation: vector"); // move a vector std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; PCZ_DEBUG("vector.size() = %d, pointer to data = %x", v.size(), v.data()); std::vector<int> v2 = std::move(v); PCZ_DEBUG("vector_2.size() = %d, vector.size() = %d, pointer_2 = %x, pointer_1 = %x", v2.size(), v.size(), v2.data(), v.data()); PCZ_TITLE("std::move operation: return by value"); simple_object s = return_simple_object(); // here s is not a new instance, but is the same as in the function PCZ_DEBUG("simple_object is moved to this variable - pointer = %x", &s); }
static void test_CyaSSL_read_write(void) { #ifdef HAVE_IO_TESTS_DEPENDENCIES /* The unit testing for read and write shall happen simutaneously, since * one can't do anything with one without the other. (Except for a failure * test case.) This function will call all the others that will set up, * execute, and report their test findings. * * Set up the success case first. This function will become the template * for the other tests. This should eventually be renamed * * The success case isn't interesting, how can this fail? * - Do not give the client context a CA certificate. The connect should * fail. Do not need server for this? * - Using NULL for the ssl object on server. Do not need client for this. * - Using NULL for the ssl object on client. Do not need server for this. * - Good ssl objects for client and server. Client write() without server * read(). * - Good ssl objects for client and server. Server write() without client * read(). * - Forgetting the password callback? */ tcp_ready ready; func_args client_args; func_args server_args; THREAD_TYPE serverThread; #ifdef CYASSL_TIRTOS fdOpenSession(Task_self()); #endif StartTCP(); InitTcpReady(&ready); server_args.signal = &ready; client_args.signal = &ready; start_thread(test_server_nofail, &server_args, &serverThread); wait_tcp_ready(&server_args); test_client_nofail(&client_args); join_thread(serverThread); AssertTrue(client_args.return_code); AssertTrue(server_args.return_code); FreeTcpReady(&ready); #ifdef CYASSL_TIRTOS fdOpenSession(Task_self()); #endif #endif }
static int test_CyaSSL_read_write(void) { /* The unit testing for read and write shall happen simutaneously, since * one can't do anything with one without the other. (Except for a failure * test case.) This function will call all the others that will set up, * execute, and report their test findings. * * Set up the success case first. This function will become the template * for the other tests. This should eventually be renamed * * The success case isn't interesting, how can this fail? * - Do not give the client context a CA certificate. The connect should * fail. Do not need server for this? * - Using NULL for the ssl object on server. Do not need client for this. * - Using NULL for the ssl object on client. Do not need server for this. * - Good ssl objects for client and server. Client write() without server * read(). * - Good ssl objects for client and server. Server write() without client * read(). * - Forgetting the password callback? */ int test_result = TEST_SUCCESS; tcp_ready ready; func_args client_args; func_args server_args; THREAD_TYPE serverThread; StartTCP(); InitTcpReady(&ready); server_args.signal = &ready; start_thread(test_server_nofail, &server_args, &serverThread); wait_tcp_ready(&server_args); test_client_nofail(&client_args); join_thread(serverThread); if (client_args.return_code != TEST_SUCCESS) { printf(resultFmt, "client failure"); test_result = TEST_FAIL; } if (server_args.return_code != TEST_SUCCESS) { printf(resultFmt, "server failure"); test_result = TEST_FAIL; } FreeTcpReady(&ready); return test_result; };
static void execute_test_case(int svr_argc, char** svr_argv, int cli_argc, char** cli_argv) { func_args cliArgs = {cli_argc, cli_argv, 0, NULL}; func_args svrArgs = {svr_argc, svr_argv, 0, NULL}; tcp_ready ready; THREAD_TYPE serverThread; char commandLine[MAX_COMMAND_SZ]; int i; static int tests = 1; commandLine[0] = '\0'; for (i = 0; i < svr_argc; i++) { strcat(commandLine, svr_argv[i]); strcat(commandLine, " "); } printf("trying server command line[%d]: %s\n", tests, commandLine); commandLine[0] = '\0'; for (i = 0; i < cli_argc; i++) { strcat(commandLine, cli_argv[i]); strcat(commandLine, " "); } printf("trying client command line[%d]: %s\n", tests++, commandLine); InitTcpReady(&ready); /* start server */ svrArgs.signal = &ready; start_thread(server_test, &svrArgs, &serverThread); wait_tcp_ready(&svrArgs); /* start client */ client_test(&cliArgs); /* verify results */ if (cliArgs.return_code != 0) { printf("client_test failed\n"); exit(EXIT_FAILURE); } join_thread(serverThread); if (svrArgs.return_code != 0) { printf("server_test failed\n"); exit(EXIT_FAILURE); } FreeTcpReady(&ready); }
int main(int argc, char** argv) { if (2 > argc) { printf ("Syntax: %s [conf_file_path]\n", argv[0]); return 1; } if( access( argv[1], F_OK ) == -1 ) { log_err("Specify correct path"); return 1; } if (populate_blacklist(get_filename_from_path(argv[1])) == -1) { log_err("Failed to read %s", get_filename_from_path(argv[1])); return 1; } if (pthread_mutex_init(&lock, NULL) || pthread_mutex_init(&udev_lock, NULL)) { log_err("\n mutex init failed\n"); return 1; } create_thread(&config_file_watcher, watcher, argv[1]); create_thread(&udev_sniffer_thread, do_udev_sniff, NULL); /* Meat */ serve("Echo"); /* End Meat*/ thread_run = 0; join_thread(config_file_watcher); join_thread(udev_sniffer_thread); pthread_mutex_destroy(&lock); return 0; }
void nn_servicelease_free (struct nn_servicelease *sl) { if (sl->keepgoing != -1) { os_mutexLock (&sl->lock); sl->keepgoing = 0; os_condSignal (&sl->cond); os_mutexUnlock (&sl->lock); join_thread (sl->ts, (void **) 0); } os_condDestroy (&sl->cond); os_mutexDestroy (&sl->lock); os_free (sl->av_ary); os_free (sl); }
void hac_free_http_access(HTTPAccess** access) { if (*access == NULL) { return; } (*access)->stopped = 1; join_thread(&(*access)->httpThreadId, NULL, NULL); har_free_resources(&(*access)->resources); destroy_mutex(&(*access)->playerStateMutex); SAFE_FREE(access); }
THREAD_FUNC_RETURN_TYPE parent_func(void *arg) { signal_cond_var(parent_ready); while (!parent_exit) { int i; thread_t threads[NUM_CHILD_THREADS]; for (i = 0; i < NUM_CHILD_THREADS; ++i) { print("."); threads[i] = create_thread(child_func, NULL); } for (i = 0; i < NUM_CHILD_THREADS; ++i) join_thread(threads[i]); } return THREAD_FUNC_RETURN_ZERO; }
int main() { int i; pthread_t child[NUM_CHILDREN]; Shared *shared = make_shared(); child[0] = make_thread(producer_entry, shared); child[1] = make_thread(consumer_entry, shared); for (i=0; i<NUM_CHILDREN; i++) { join_thread(child[i]); } return 0; }
int main () { int i; pthread_t child[NUM_CHILDREN]; Shared *shared = make_shared (100000000); for (i=0; i<NUM_CHILDREN; i++) { child[i] = make_thread (entry, shared); } for (i=0; i<NUM_CHILDREN; i++) { join_thread (child[i]); } check_array (shared); return 0; }
/** * \brief Waits for an Async object to finish up (joins all the threads * in the thread pool) and DESTROYS the Async object (frees its * memory and resources). * * This function will wait forever or until a signal is received and all * the tasks in the queue have been processed. */ static void async_wait(async_p async) { if (!async) return; /* wake threads (just in case) by sending `async->count` * number of wakeups */ if (async->pipe.out) write(async->pipe.out, async, async->count); /* join threads */ for (int i = 0; i < async->count; i++) { join_thread(async->threads[i]); } /* perform any pending tasks */ perform_tasks(async); /* release queue memory and resources */ async_destroy(async); }
int main() { TEST_THREAD_DATA threads_data[NUMBER_OF_TEST_THREADS]; THR_HANDLE thread_handle[NUMBER_OF_TEST_THREADS]; unsigned i; int found_error = 0; srand( (unsigned)time( NULL ) ); memset( threads_data, 0, sizeof(threads_data) ); for ( i = 0; i < NUMBER_OF_TEST_THREADS; i++ ) { threads_data[i].index = i; threads_data[i].sleep_interval_millis = get_random_value( MAX_THREAD_SLEEP_MILLIS ); thread_handle[i] = launch_thread( &threads_data[i] ); if ( !thread_handle[i] ) { fprintf( stderr, "failed to launch worker thread, error code is %d\n", LAST_ERROR() ); return EXIT_FAILURE; } } for ( i = 0; i < NUMBER_OF_TEST_THREADS; i++ ) if ( join_thread( thread_handle[i] ) != 0 ) { fprintf( stderr, "failed to join thread %d, error code is %d\n", i, LAST_ERROR() ); return EXIT_FAILURE; } for ( i = 0; i < NUMBER_OF_TEST_THREADS; i++ ) { if( threads_data[i].error_occured ) { fprintf( stderr, "error occured at thread %d: %s\n", i, threads_data[i].error_message ); found_error = 1; } } if ( found_error ) return EXIT_FAILURE; // printf( "test ended successfully\n"); return EXIT_SUCCESS; }
int CTCI_Close( DEVBLK* pDEVBLK ) { /* DEVBLK* pDEVBLK2; */ PCTCBLK pCTCBLK = (PCTCBLK)pDEVBLK->dev_data; // Close the device file (if not already closed) if( pCTCBLK->fd >= 0 ) { // PROGRAMMING NOTE: there's currently no way to interrupt // the "CTCI_ReadThread"s TUNTAP_Read of the adapter. Thus // we must simply wait for CTCI_ReadThread to eventually // notice that we're doing a close (via our setting of the // fCloseInProgress flag). Its TUNTAP_Read will eventually // timeout after a few seconds (currently 5, which is dif- // ferent than the CTC_READ_TIMEOUT_SECS timeout value the // CTCI_Read function uses) and will then do the close of // the adapter for us (TUNTAP_Close) so we don't have to. // All we need to do is ask it to exit (via our setting of // the fCloseInProgress flag) and then wait for it to exit // (which, as stated, could take up to a max of 5 seconds). // All of this is simply because it's poor form to close a // device from one thread while another thread is reading // from it. Attempting to do so could trip a race condition // wherein the internal i/o buffers used to process the // read request could have been freed (by the close call) // by the time the read request eventually gets serviced. TID tid = pCTCBLK->tid; pCTCBLK->fCloseInProgress = 1; // (ask read thread to exit) signal_thread( tid, SIGUSR2 ); // (for non-Win32 platforms) //FIXME signal_thread not working for non-MSVC platforms #if defined(_MSVC_) join_thread( tid, NULL ); // (wait for thread to end) #endif detach_thread( tid ); // (wait for thread to end) } pDEVBLK->fd = -1; // indicate we're now closed return 0; }
int free_iothread(iothread_t* iothr) { int err; if (iothr->thread) { requeststop_iothread(iothr); (void) join_thread(iothr->thread); cancelall_iolist(&iothr->iolist); err = delete_thread(&iothr->thread); (void) PROCESS_testerrortimer(&s_iothread_errtimer, &err); if (err) goto ONERR; } return 0; ONERR: TRACEEXITFREE_ERRLOG(err); return err; }
static void test_CyaSSL_client_server(callback_functions* client_callbacks, callback_functions* server_callbacks) { #ifdef HAVE_IO_TESTS_DEPENDENCIES tcp_ready ready; func_args client_args; func_args server_args; THREAD_TYPE serverThread; StartTCP(); client_args.callbacks = client_callbacks; server_args.callbacks = server_callbacks; #ifdef CYASSL_TIRTOS fdOpenSession(Task_self()); #endif /* RUN Server side */ InitTcpReady(&ready); server_args.signal = &ready; client_args.signal = &ready; start_thread(run_cyassl_server, &server_args, &serverThread); wait_tcp_ready(&server_args); /* RUN Client side */ run_cyassl_client(&client_args); join_thread(serverThread); FreeTcpReady(&ready); #ifdef CYASSL_TIRTOS fdCloseSession(Task_self()); #endif #else (void)client_callbacks; (void)server_callbacks; #endif }
static void ddc_close(void* data) { DVDecodeStreamConnect* connect = (DVDecodeStreamConnect*)data; if (data == NULL) { return; } if (connect->useWorkerThread) { connect->stopped = 1; /* wake up threads - this is to avoid valgrind saying the mutx is still in use when pthread_mutex_destroy is called below */ PTHREAD_MUTEX_LOCK(&connect->workerMutex); pthread_cond_broadcast(&connect->frameIsReadyCond); pthread_cond_broadcast(&connect->workerIsBusyCond); PTHREAD_MUTEX_UNLOCK(&connect->workerMutex); join_thread(&connect->workerThreadId, NULL, NULL); } free_dv_decoder(&connect->decoder); free_dv_decoder_resources(); SAFE_FREE(&connect->dvData); if (connect->useWorkerThread) { destroy_cond_var(&connect->workerIsBusyCond); destroy_cond_var(&connect->frameIsReadyCond); destroy_mutex(&connect->workerMutex); } SAFE_FREE(&connect); }
static void logger_term(void *arg) { char *lmsbuf = NULL; /* xxx */ int lmsnum = -1; /* xxx */ int lmscnt = -1; /* xxx */ char* term_msg = MSG(HHC02103, "I"); UNREFERENCED(arg); log_wakeup(NULL); usleep(1000); /* Flush all pending logger o/p before redirecting?? */ fflush(stdout); if(logger_active) { /* Redirect all output to stderr */ dup2(STDERR_FILENO, STDOUT_FILENO); /* Tell logger thread we want it to exit */ logger_active = 0; log_wakeup(NULL); usleep(1000); if ( logger_tid != 0 && !sysblk.shutdown ) { sleep(2); /* Logger is now terminating */ obtain_lock(&logger_lock); /* Wait for the logger to terminate */ join_thread( logger_tid, NULL ); detach_thread( logger_tid ); release_lock(&logger_lock); } if ( fwrite("\n",1,1,stderr) ) { perror(QLINE "fwrite failure/HHC02102 "); } /* Read and display any msgs still remaining in the system log */ lmsnum = log_line(-1); while((lmscnt = log_read(&lmsbuf, &lmsnum, LOG_BLOCK))) { char *p = NULL; char *strtok_str = NULL; lmsbuf[lmscnt-1] = '\0'; p = strtok_r( lmsbuf, "\n", &strtok_str ); while ( (p = strtok_r(NULL, "\n", &strtok_str ) ) != NULL ) { char* pLeft = p; int nLeft = (int)strlen(p); #if defined( OPTION_MSGCLR ) /* Remove "<pnl,..." color string if it exists */ if (1 && nLeft > 5 && strncasecmp( pLeft, "<pnl", 4 ) == 0 && (pLeft = memchr( pLeft+4, '>', nLeft-4 )) != NULL ) { pLeft++; nLeft -= (int)(pLeft - p); } #endif // defined( OPTION_MSGCLR ) if (nLeft) { if ( fwrite(pLeft,nLeft,1,stderr) ) { perror(QLINE "fwrite failure/HHC02102 "); } if ( fwrite("\n",1,1,stderr) ) { perror(QLINE "fwrite failure/HHC02102 "); } } } } if ( fwrite( term_msg, strlen(term_msg), 1, stderr ) ) { perror(QLINE "fwrite failure/HHC02102 "); } fflush(stderr); } }
int main(int argc, char** argv) { size_t _i = 0, _j = 0; size_t _read_len = 0; char *_file_in = NULL, *_file_out = NULL, *_file_k = NULL; int _op = 0; FILE *_f_in = stdin, *_f_out = stdout, *_f_k = NULL; do_block _b[OAES_THREADS]; fprintf( stderr, "\n" "*******************************************************************************\n" "* OpenAES %-10s *\n" "* Copyright (c) 2013, Nabil S. Al Ramli, www.nalramli.com *\n" "*******************************************************************************\n\n", OAES_VERSION ); // pad the key for( _j = 0; _j < 32; _j++ ) _key_data[_j] = _j + 1; if( argc < 2 ) { usage( argv[0] ); return EXIT_FAILURE; } if( 0 == strcmp( argv[1], "gen-key" ) ) { OAES_CTX *_oaes = NULL; uint8_t _buf[16384]; size_t _buf_len = sizeof(_buf); OAES_RET _rc = OAES_RET_SUCCESS; _i++; _i++; // key_length _i++; // key_file if( _i >= argc ) { fprintf( stderr, "Error: No value specified for '%s'.\n", argv[1] ); usage( argv[0] ); return EXIT_FAILURE; } _key_data_len = atoi( argv[_i - 1] ); _file_k = argv[_i]; _oaes = oaes_alloc(); if( NULL == _oaes ) { fprintf(stderr, "Error: Failed to initialize OAES.\n"); return OAES_RET_MEM; } switch( _key_data_len ) { case 128: _rc = oaes_key_gen_128(_oaes); break; case 192: _rc = oaes_key_gen_192(_oaes); break; case 256: _rc = oaes_key_gen_256(_oaes); break; default: fprintf( stderr, "Error: Invalid value [%s] specified for '%s'.\n", argv[_i - 1], argv[_i - 2] ); oaes_free(&_oaes); return EXIT_FAILURE; } if( OAES_RET_SUCCESS != _rc ) { fprintf( stderr, "Error: Failed to generate OAES %lu bit key.\n", _key_data_len ); oaes_free(&_oaes); return EXIT_FAILURE; } if( OAES_RET_SUCCESS != oaes_key_export(_oaes, _buf, &_buf_len) ) { fprintf( stderr, "Error: Failed to retrieve key length %lu.\n", _key_data_len ); oaes_free(&_oaes); return EXIT_FAILURE; } oaes_free(&_oaes); if( 0 == access(_file_k, 00) ) { fprintf(stderr, "Error: '%s' already exists.\n", _file_k); return EXIT_FAILURE; } _f_k = fopen(_file_k, "wb"); if( NULL == _f_k ) { fprintf(stderr, "Error: Failed to open '%s' for writing.\n", _file_k); return EXIT_FAILURE; } fwrite(_buf, sizeof(uint8_t), _buf_len, _f_k); fclose(_f_k); return EXIT_SUCCESS; } else if( 0 == strcmp( argv[1], "base64-enc" ) ) { _op = 0; _read_len = OAES_BASE64_LEN_ENC; } else if( 0 == strcmp( argv[1], "base64-dec" ) ) { _op = 1; _read_len = OAES_BASE64_LEN_DEC; } else if( 0 == strcmp( argv[1], "aes-enc" ) ) { _op = 2; _read_len = OAES_AES_LEN_ENC; } else if( 0 == strcmp( argv[1], "aes-dec" ) ) { _op = 3; _read_len = OAES_AES_LEN_DEC; } else { fprintf(stderr, "Error: Unknown command '%s'.", argv[1]); usage( argv[0] ); return EXIT_FAILURE; } for( _i = 2; _i < argc; _i++ ) { int _found = 0; if( 0 == strcmp( argv[_i], "--ecb" ) ) { _found = 1; _is_ecb = 1; } if( 0 == strcmp( argv[_i], "--key" ) ) { uint8_t *_buf = NULL; size_t _buf_len = 0; _found = 1; _i++; // base64_encoded_key_data if( _i >= argc ) { fprintf(stderr, "Error: No value specified for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } if( oaes_base64_decode(argv[_i], strlen(argv[_i]), NULL, &_buf_len) ) { fprintf(stderr, "Error: Invalid value for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } _buf = (uint8_t *) calloc(_buf_len, sizeof(uint8_t)); if( NULL == _buf ) { fprintf(stderr, "Error: Failed to allocate memory.\n"); return EXIT_FAILURE; } if( oaes_base64_decode(argv[_i], strlen(argv[_i]), _buf, &_buf_len) ) { free(_buf); fprintf(stderr, "Error: Invalid value for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } _key_data_len = _buf_len; if( 16 >= _key_data_len ) _key_data_len = 16; else if( 24 >= _key_data_len ) _key_data_len = 24; else _key_data_len = 32; memcpy(_key_data, _buf, __min(32, _buf_len)); for( _j = 0; _j < _buf_len; _j++ ) { _key_data[_j % 32] ^= _buf[_j]; } free(_buf); } if( 0 == strcmp( argv[_i], "--key-file" ) ) { OAES_CTX *_ctx = NULL; uint8_t _buf[16384]; size_t _read = 0; _found = 1; _i++; // key_file if( _i >= argc ) { fprintf(stderr, "Error: No value specified for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } _file_k = argv[_i]; _ctx = oaes_alloc(); if( NULL == _ctx ) { fprintf(stderr, "Error: Failed to initialize OAES.\n"); return OAES_RET_MEM; } _f_k = fopen(_file_k, "rb"); if( NULL == _f_k ) { fprintf(stderr, "Error: Failed to open '%s' for reading.\n", _file_k); oaes_free(&_ctx); return EXIT_FAILURE; } _read = fread(_buf, sizeof(uint8_t), sizeof(_buf), _f_k); fclose(_f_k); if( OAES_RET_SUCCESS != oaes_key_import(_ctx, _buf, _read) ) { fprintf(stderr, "Error: Failed to import '%s'.\n", _file_k); oaes_free(&_ctx); return EXIT_FAILURE; } _key_data_len = sizeof(_key_data); if( OAES_RET_SUCCESS != oaes_key_export_data(_ctx, _key_data, &_key_data_len) ) { fprintf(stderr, "Error: Failed to export '%s' data.\n", _file_k); oaes_free(&_ctx); return EXIT_FAILURE; } oaes_free(&_ctx); } if( 0 == strcmp( argv[_i], "--in" ) ) { _found = 1; _i++; // path_in if( _i >= argc ) { fprintf(stderr, "Error: No value specified for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } _file_in = argv[_i]; } if( 0 == strcmp( argv[_i], "--out" ) ) { _found = 1; _i++; // path_out if( _i >= argc ) { fprintf(stderr, "Error: No value specified for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } _file_out = argv[_i]; } if( 0 == _found ) { fprintf(stderr, "Error: Invalid option '%s'.\n", argv[_i]); usage( argv[0] ); return EXIT_FAILURE; } } switch(_op) { case 0: case 1: break; case 2: case 3: if( 0 == _key_data_len ) { char _key_data_ent[8193] = ""; uint8_t *_buf = NULL; size_t _buf_len = 0; fprintf(stderr, "Enter base64-encoded key: "); scanf("%8192s", _key_data_ent); if( oaes_base64_decode( _key_data_ent, strlen(_key_data_ent), NULL, &_buf_len ) ) { fprintf(stderr, "Error: Invalid value for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } _buf = (uint8_t *) calloc(_buf_len, sizeof(uint8_t)); if( NULL == _buf ) { fprintf(stderr, "Error: Failed to allocate memory.\n"); return EXIT_FAILURE; } if( oaes_base64_decode( _key_data_ent, strlen(_key_data_ent), _buf, &_buf_len ) ) { free(_buf); fprintf(stderr, "Error: Invalid value for '%s'.\n", argv[_i - 1]); usage( argv[0] ); return EXIT_FAILURE; } _key_data_len = _buf_len; if( 16 >= _key_data_len ) _key_data_len = 16; else if( 24 >= _key_data_len ) _key_data_len = 24; else _key_data_len = 32; memcpy(_key_data, _buf, __min(32, _buf_len)); for( _j = 0; _j < _buf_len; _j++ ) { _key_data[_j % 32] ^= _buf[_j]; } free(_buf); } break; default: break; } if( _file_in ) { _f_in = fopen(_file_in, "rb"); if( NULL == _f_in ) { fprintf(stderr, "Error: Failed to open '%s' for reading.\n", _file_in); return EXIT_FAILURE; } } else { if( setmode(fileno(stdin), 0x8000) < 0 ) fprintf(stderr,"Error: Failed in setmode().\n"); _f_in = stdin; } if( _file_out ) { if( 0 == access(_file_out, 00) ) { fprintf(stderr, "Error: '%s' already exists.\n", _file_out); return EXIT_FAILURE; } _f_out = fopen(_file_out, "wb"); if( NULL == _f_out ) { fprintf(stderr, "Error: Failed to open '%s' for writing.\n", _file_out); if( _file_in ) fclose(_f_in); return EXIT_FAILURE; } } else { if( setmode(fileno(stdout), 0x8000) < 0 ) fprintf(stderr, "Error: Failed in setmode().\n"); _f_out = stdout; } _i = 0; while( _b[_i].in_len = fread(_b[_i].in, sizeof(uint8_t), _read_len, _f_in) ) { switch(_op) { case 0: _b[_i].id = start_thread(_do_base64_encode, &(_b[_i])); if( NULL == _b[_i].id ) fprintf(stderr, "Error: Failed to start encryption.\n"); break; case 1: _b[_i].id = start_thread(_do_base64_decode, &(_b[_i])); if( NULL == _b[_i].id ) fprintf(stderr, "Error: Failed to start decryption.\n"); break; case 2: _b[_i].id = start_thread(_do_aes_encrypt, &(_b[_i])); if( NULL == _b[_i].id ) fprintf(stderr, "Error: Failed to start encryption.\n"); break; case 3: _b[_i].id = start_thread(_do_aes_decrypt, &(_b[_i])); if( NULL == _b[_i].id ) fprintf(stderr, "Error: Failed to start decryption.\n"); break; default: break; } if( OAES_THREADS == _i + 1 ) { for( _j = 0; _j < OAES_THREADS; _j++ ) { if( _b[_j].id ) { join_thread(_b[_j].id); _b[_j].id = 0; if( _b[_j].out ) { fwrite(_b[_j].out, sizeof(uint8_t), _b[_j].out_len, _f_out); free(_b[_j].out); _b[_j].out = NULL; } } } } _i = (_i + 1) % OAES_THREADS; } for( _j = 0; _j < _i; _j++ ) { if( _b[_j].id ) { join_thread(_b[_j].id); _b[_j].id = 0; if( _b[_j].out ) { fwrite(_b[_j].out, sizeof(uint8_t), _b[_j].out_len, _f_out); free(_b[_j].out); _b[_j].out = NULL; } } } if( _file_in ) fclose(_f_in); if( _file_out ) fclose(_f_out); fprintf(stderr, "done.\n"); return (EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int per_id; setup(); pass_criteria = PASS_US; rt_init("d:l:ht:i:", parse_args, argc, argv); printf("-------------------------------\n"); printf("Scheduling Latency\n"); printf("-------------------------------\n\n"); if (load_ms*NS_PER_MS >= period-OVERHEAD) { printf("ERROR: load must be < period - %d us\n", OVERHEAD/NS_PER_US); exit(1); } if (iterations == 0) iterations = DEFAULT_ITERATIONS; if (iterations < MIN_ITERATIONS) { printf("Too few iterations (%d), use min iteration instead (%d)\n", iterations, MIN_ITERATIONS); iterations = MIN_ITERATIONS; } printf("Running %d iterations with a period of %llu ms\n", iterations, period/NS_PER_MS); printf("Periodic load duration: %d ms\n", load_ms); printf("Expected running time: %d s\n", (int)(iterations*((float)period / NS_PER_SEC))); if (stats_container_init(&dat, iterations)) exit(1); if (stats_container_init(&hist, HIST_BUCKETS)) { stats_container_free(&dat); exit(1); } /* use the highest value for the quantiles */ if (stats_quantiles_init(&quantiles, (int)log10(iterations))) { stats_container_free(&hist); stats_container_free(&dat); exit(1); } /* wait one quarter second to execute */ start = rt_gettime() + 250 * NS_PER_MS; per_id = create_fifo_thread(periodic_thread, (void*)0, PRIO); join_thread(per_id); join_threads(); printf("\nCriteria: latencies < %d us\n", (int)pass_criteria); printf("Result: %s\n", ret ? "FAIL" : "PASS"); stats_container_free(&dat); stats_container_free(&hist); stats_quantiles_free(&quantiles); return ret; }
static int execute_test_case(int svr_argc, char** svr_argv, int cli_argc, char** cli_argv, int addNoVerify, int addNonBlocking) { #ifdef WOLFSSL_TIRTOS func_args cliArgs = {0}; func_args svrArgs = {0}; cliArgs.argc = cli_argc; cliArgs.argv = cli_argv; svrArgs.argc = svr_argc; svrArgs.argv = svr_argv; #else func_args cliArgs = {cli_argc, cli_argv, 0, NULL, NULL}; func_args svrArgs = {svr_argc, svr_argv, 0, NULL, NULL}; #endif tcp_ready ready; THREAD_TYPE serverThread; char commandLine[MAX_COMMAND_SZ]; char cipherSuite[MAX_SUITE_SZ+1]; int i; size_t added = 0; static int tests = 1; commandLine[0] = '\0'; for (i = 0; i < svr_argc; i++) { added += strlen(svr_argv[i]) + 2; if (added >= MAX_COMMAND_SZ) { printf("server command line too long\n"); break; } strcat(commandLine, svr_argv[i]); strcat(commandLine, flagSep); } if (IsValidCipherSuite(commandLine, cipherSuite) == 0) { #ifdef DEBUG_SUITE_TESTS printf("cipher suite %s not supported in build\n", cipherSuite); #endif return NOT_BUILT_IN; } #ifndef WOLFSSL_ALLOW_SSLV3 if (IsSslVersion(commandLine) == 1) { #ifdef DEBUG_SUITE_TESTS printf("protocol version on line %s is too old\n", commandLine); #endif return VERSION_TOO_OLD; } #endif #ifdef NO_OLD_TLS if (IsOldTlsVersion(commandLine) == 1) { #ifdef DEBUG_SUITE_TESTS printf("protocol version on line %s is too old\n", commandLine); #endif return VERSION_TOO_OLD; } #endif if (addNoVerify) { printf("repeating test with client cert request off\n"); added += 4; /* -d plus space plus terminator */ if (added >= MAX_COMMAND_SZ || svr_argc >= MAX_ARGS) printf("server command line too long\n"); else { svr_argv[svr_argc++] = noVerifyFlag; svrArgs.argc = svr_argc; strcat(commandLine, noVerifyFlag); strcat(commandLine, flagSep); } } if (addNonBlocking) { printf("repeating test with non blocking on\n"); added += 4; /* -N plus terminator */ if (added >= MAX_COMMAND_SZ || svr_argc >= MAX_ARGS) printf("server command line too long\n"); else { svr_argv[svr_argc++] = nonblockFlag; svrArgs.argc = svr_argc; strcat(commandLine, nonblockFlag); strcat(commandLine, flagSep); } } #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) /* add port 0 */ if (svr_argc + 2 > MAX_ARGS) printf("cannot add the magic port number flag to server\n"); else { svr_argv[svr_argc++] = portFlag; svr_argv[svr_argc++] = svrPort; svrArgs.argc = svr_argc; } #endif printf("trying server command line[%d]: %s\n", tests, commandLine); commandLine[0] = '\0'; added = 0; for (i = 0; i < cli_argc; i++) { added += strlen(cli_argv[i]) + 2; if (added >= MAX_COMMAND_SZ) { printf("client command line too long\n"); break; } strcat(commandLine, cli_argv[i]); strcat(commandLine, flagSep); } if (addNonBlocking) { added += 4; /* -N plus space plus terminator */ if (added >= MAX_COMMAND_SZ) printf("client command line too long\n"); else { cli_argv[cli_argc++] = nonblockFlag; strcat(commandLine, nonblockFlag); strcat(commandLine, flagSep); cliArgs.argc = cli_argc; } } printf("trying client command line[%d]: %s\n", tests++, commandLine); InitTcpReady(&ready); #ifdef WOLFSSL_TIRTOS fdOpenSession(Task_self()); #endif /* start server */ svrArgs.signal = &ready; start_thread(server_test, &svrArgs, &serverThread); wait_tcp_ready(&svrArgs); #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) if (ready.port != 0) { if (cli_argc + 2 > MAX_ARGS) printf("cannot add the magic port number flag to client\n"); else { char portNumber[8]; snprintf(portNumber, sizeof(portNumber), "%d", ready.port); cli_argv[cli_argc++] = portFlag; cli_argv[cli_argc++] = portNumber; cliArgs.argc = cli_argc; } } #endif /* start client */ client_test(&cliArgs); /* verify results */ if (cliArgs.return_code != 0) { printf("client_test failed\n"); exit(EXIT_FAILURE); } join_thread(serverThread); if (svrArgs.return_code != 0) { printf("server_test failed\n"); exit(EXIT_FAILURE); } #ifdef WOLFSSL_TIRTOS fdCloseSession(Task_self()); #endif FreeTcpReady(&ready); return 0; }