int main() { sranddev(); Task tasks[1024]; int i; for (i=0; i < 1024; i++) task_new(&tasks[i], &clear_task, (void*)i); pthread_exit(NULL); return 0; }
bool init_random(t_zopt *opt) { if (!opt) return (false); #if defined(_Darwin_) if ((int)opt->seed == -1) sranddev(); else #endif srand(opt->seed); return (true); }
/* * In the event that we don't get a root password, we try to * randomize the master key the best we can */ static void randomize(des_block *master) { #ifdef KEYSERV_RANDOM master->key.low = arc4random(); master->key.high = arc4random(); #else /* use stupid dangerous bad rand() */ sranddev(); master->key.low = rand(); master->key.high = rand(); #endif }
void C_random_seed(void) { #ifdef THREADED_LIBRARY /* no-op */ #else #ifdef HAVE_SRANDDEV sranddev(); #else srand((uint_t)time(NULL) + (uint_t)getpid()); #endif #endif /* THREADED_LIBRARY */ }
int main() { int i, j; sranddev(); lnp_routing_table_initialize(); for (j=0; j<id_n; j++) { for (i=0; i<ID_LENGTH; i++) { id[j][i] = (u_char)(((double)rand()/RAND_MAX)*26)+'A'; } } test_1(); printf("--------\n"); test_3(); printf("--------\n"); test_4(); //test_2(); }
TexOGL* Textures::createGrayNoise() { // ONLY FOR GL_FLOAT datatype //struct timeval *tp; //struct timezone *tzp; //int timeday = gettimeofday(tp, tzp); #ifndef LINUX sranddev(); #else srand(100); #endif if (internal_format != GL_RGBA) { printf("createGrayNoise: internal format should be GL_RGBA\n"); //exit(0); } if (data_type != GL_FLOAT) { printf("createBWNoise: data_type should be GL_FLOAT\n"); return 0; } float val, p; TexOGL* t = new TexOGL(); TexOGL& input = *t; Array3D tex(nb_internal_channels, nx, ny); for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { float p = u.rand_float(); for (int k=0; k < nb_internal_channels; k++) { tex(k, i, j) = p; } if (nb_internal_channels == 4) { tex(3, i, j) = 1.0; } }} input.init_targ(nx, ny, target); input.load(internal_format, format, data_type, tex.getDataPtr()); input.repeat(); input.point(); return t; }
int main(int argc, char *argv[]) { int success = 0; int iterations = 0; sranddev(); init_board(' '); draw_board(); while (!success) { iterations++; success = place_five(); } printf("\n\niterations = %d\n", iterations); draw_board(); return 0; }
int main(const int argc, const char** argv) { #define INPUT_ARRAY_SIZE 1000000 // YES! 1 Million int i; int *arr; arr = malloc(INPUT_ARRAY_SIZE * sizeof(int)); int sup_arr[INPUT_ARRAY_SIZE]; sranddev(); for ( i = 0; i < INPUT_ARRAY_SIZE; ++i ) { arr[i] = rand(); } polite_merge_sort(arr, 0, INPUT_ARRAY_SIZE-1, sup_arr); printf("----- SOLUTION -----\n"); for ( i = 0; i < INPUT_ARRAY_SIZE; ++i ) { printf("#%d: %d\n", i, arr[i]); } printf("\n------- END --------\n"); }
int main(int argc, char **argv) { uint8_t success; int i; intset *is; sranddev(); printf("Value encodings: "); { assert(_intsetValueEncoding(-32768) == INTSET_ENC_INT16); assert(_intsetValueEncoding(+32767) == INTSET_ENC_INT16); assert(_intsetValueEncoding(-32769) == INTSET_ENC_INT32); assert(_intsetValueEncoding(+32768) == INTSET_ENC_INT32); assert(_intsetValueEncoding(-2147483648) == INTSET_ENC_INT32); assert(_intsetValueEncoding(+2147483647) == INTSET_ENC_INT32); assert(_intsetValueEncoding(-2147483649) == INTSET_ENC_INT64); assert(_intsetValueEncoding(+2147483648) == INTSET_ENC_INT64); assert(_intsetValueEncoding(-9223372036854775808ull) == INTSET_ENC_INT64); assert(_intsetValueEncoding(+9223372036854775807ull) == INTSET_ENC_INT64); ok(); } printf("Basic adding: "); { is = intsetNew(); is = intsetAdd(is,5,&success); assert(success); is = intsetAdd(is,6,&success); assert(success); is = intsetAdd(is,4,&success); assert(success); is = intsetAdd(is,4,&success); assert(!success); ok(); } printf("Large number of random adds: "); { int inserts = 0; is = intsetNew(); for (i = 0; i < 1024; i++) { is = intsetAdd(is,rand()%0x800,&success); if (success) inserts++; } assert(is->length == inserts); checkConsistency(is); ok(); } printf("Upgrade from int16 to int32: "); { is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,65535,NULL); assert(is->encoding == INTSET_ENC_INT32); assert(intsetFind(is,32)); assert(intsetFind(is,65535)); checkConsistency(is); is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,-65535,NULL); assert(is->encoding == INTSET_ENC_INT32); assert(intsetFind(is,32)); assert(intsetFind(is,-65535)); checkConsistency(is); ok(); } printf("Upgrade from int16 to int64: "); { is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,32)); assert(intsetFind(is,4294967295)); checkConsistency(is); is = intsetNew(); is = intsetAdd(is,32,NULL); assert(is->encoding == INTSET_ENC_INT16); is = intsetAdd(is,-4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,32)); assert(intsetFind(is,-4294967295)); checkConsistency(is); ok(); } printf("Upgrade from int32 to int64: "); { is = intsetNew(); is = intsetAdd(is,65535,NULL); assert(is->encoding == INTSET_ENC_INT32); is = intsetAdd(is,4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,65535)); assert(intsetFind(is,4294967295)); checkConsistency(is); is = intsetNew(); is = intsetAdd(is,65535,NULL); assert(is->encoding == INTSET_ENC_INT32); is = intsetAdd(is,-4294967295,NULL); assert(is->encoding == INTSET_ENC_INT64); assert(intsetFind(is,65535)); assert(intsetFind(is,-4294967295)); checkConsistency(is); ok(); } printf("Stress lookups: "); { long num = 100000, size = 10000; int i, bits = 20; long long start; is = createSet(bits,size); checkConsistency(is); start = usec(); for (i = 0; i < num; i++) intsetSearch(is,rand() % ((1<<bits)-1),NULL); printf("%ld lookups, %ld element set, %lldusec\n",num,size,usec()-start); } printf("Stress add+delete: "); { int i, v1, v2; is = intsetNew(); for (i = 0; i < 0xffff; i++) { v1 = rand() % 0xfff; is = intsetAdd(is,v1,NULL); assert(intsetFind(is,v1)); v2 = rand() % 0xfff; is = intsetRemove(is,v2,NULL); assert(!intsetFind(is,v2)); } checkConsistency(is); ok(); } }
/* Create new UDP connection to the server */ struct VDgramConn *vc_create_client_dgram_conn(struct vContext *C) { struct VSession *vsession = CTX_current_session(C); struct VDgramConn *dgram_conn = NULL; struct addrinfo hints, *result, *rp; int sockfd = -1; int flag, ret; struct VURL url; /* Seed random number generator, */ #ifdef __APPLE__ sranddev(); /* Other BSD based systems probably support this or similar function too. */ #else /* Other systems have to use this evil trick */ struct timeval tv; gettimeofday(&tv, NULL); srand(tv.tv_sec - tv.tv_usec); #endif if (v_url_parse(vsession->host_url, &url) != 1) { goto end; } else { /* v_print_url(VRS_PRINT_DEBUG_MSG, &url); */ } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Allow datagram protocol */ hints.ai_flags = 0; /* No flags required */ hints.ai_protocol = IPPROTO_UDP; /* Allow UDP protocol only */ if(is_log_level(VRS_PRINT_DEBUG_MSG)) { if(url.ip_ver==IPV6) { v_print_log(VRS_PRINT_DEBUG_MSG, "Try to connect to: [%s]:%s\n", url.node, url.service); } else { v_print_log(VRS_PRINT_DEBUG_MSG, "Try to connect to: %s:%s\n", url.node, url.service); } } if( (ret = getaddrinfo(url.node, url.service, &hints, &result)) !=0 ) { v_print_log(VRS_PRINT_ERROR, "getaddrinfo(): %s\n", gai_strerror(ret)); goto end; } /* Try to use addrinfo from getaddrinfo() */ for(rp=result; rp!=NULL; rp=rp->ai_next) { if( (sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1) { v_print_log(VRS_PRINT_ERROR, "socket(): %s\n", strerror(errno)); continue; } else { /* Try to "connect" to this address ... the client will be able to send and * receive packets only from this address. */ if(connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1) break; close(sockfd); sockfd = -1; } } if(rp == NULL) { if(is_log_level(VRS_PRINT_ERROR)) { if(url.ip_ver==IPV6) { v_print_log(VRS_PRINT_ERROR, "Could not connect to the [%s]:%s\n", url.node, url.service); } else { v_print_log(VRS_PRINT_ERROR, "Could not connect to the %s:%s\n", url.node, url.service); } } freeaddrinfo(result); goto end; } if( (dgram_conn = (struct VDgramConn*)calloc(1, sizeof(struct VDgramConn))) == NULL) { v_print_log(VRS_PRINT_ERROR, "calloc(): %s\n", strerror(errno)); freeaddrinfo(result); if(sockfd != -1) { close(sockfd); } goto end; } /* Initialize datagram connection */ v_conn_dgram_init(dgram_conn); /* Use first successfully assigned socket */ dgram_conn->io_ctx.sockfd = sockfd; /* Set socket non-blocking */ flag = fcntl(dgram_conn->io_ctx.sockfd, F_GETFL, 0); if( (fcntl(dgram_conn->io_ctx.sockfd, F_SETFL, flag | O_NONBLOCK)) == -1) { v_print_log(VRS_PRINT_ERROR, "fcntl(): %s\n", strerror(errno)); free(dgram_conn); dgram_conn = NULL; if(sockfd != -1) { close(sockfd); } freeaddrinfo(result); goto end; } /* Set socket to reuse address */ flag = 1; if( setsockopt(dgram_conn->io_ctx.sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) == -1) { v_print_log(VRS_PRINT_ERROR, "setsockopt(): %s\n", strerror(errno)); free(dgram_conn); if(sockfd != -1) { close(sockfd); } dgram_conn = NULL; goto end; } /* Set address of peer and host */ if(rp->ai_family==AF_INET) { /* Address type of client */ dgram_conn->io_ctx.host_addr.ip_ver = IPV4; dgram_conn->io_ctx.host_addr.protocol = UDP; /* Address of peer */ dgram_conn->io_ctx.peer_addr.ip_ver = IPV4; dgram_conn->io_ctx.peer_addr.protocol = UDP; dgram_conn->io_ctx.peer_addr.port = ntohs(((struct sockaddr_in*)rp->ai_addr)->sin_port); memcpy(&dgram_conn->io_ctx.peer_addr.addr.ipv4, rp->ai_addr, rp->ai_addrlen); /* Address of peer (reference in connection) */ dgram_conn->peer_address.ip_ver = IPV4; dgram_conn->peer_address.protocol = UDP; dgram_conn->peer_address.port = ntohs(((struct sockaddr_in*)rp->ai_addr)->sin_port); memcpy(&dgram_conn->peer_address.addr.ipv4, rp->ai_addr, rp->ai_addrlen); } else if(rp->ai_family==AF_INET6) { /* Address type of client */ dgram_conn->io_ctx.host_addr.ip_ver = IPV6; dgram_conn->io_ctx.host_addr.protocol = UDP; /* Address of peer */ dgram_conn->io_ctx.peer_addr.ip_ver = IPV6; dgram_conn->io_ctx.peer_addr.protocol = UDP; dgram_conn->io_ctx.peer_addr.port = ntohs(((struct sockaddr_in6*)rp->ai_addr)->sin6_port); memcpy(&dgram_conn->io_ctx.peer_addr.addr.ipv6, rp->ai_addr, rp->ai_addrlen); /* Address of peer (reference in connection) */ dgram_conn->peer_address.ip_ver = IPV6; dgram_conn->peer_address.protocol = UDP; dgram_conn->peer_address.port = ntohs(((struct sockaddr_in6*)rp->ai_addr)->sin6_port); memcpy(&dgram_conn->peer_address.addr.ipv6, rp->ai_addr, rp->ai_addrlen); } freeaddrinfo(result); /* When DTLS was negotiated, then set flag */ if(url.security_protocol == VRS_SEC_DATA_TLS) { #if (defined WITH_OPENSSL) && OPENSSL_VERSION_NUMBER>=0x10000000 dgram_conn->io_ctx.flags |= SOCKET_SECURED; #else v_print_log(VRS_PRINT_ERROR, "Server tries to force client to use secured connection, but it is not supported\n"); goto end; #endif } #ifdef WITH_OPENSSL /* Create BIO, connect and set to already connected */ if( (dgram_conn->io_ctx.bio = BIO_new_dgram(dgram_conn->io_ctx.sockfd, BIO_CLOSE)) == NULL) { v_print_log(VRS_PRINT_ERROR, "BIO_new_dgram()\n"); goto end; } /* Try to do PMTU discovery */ if( BIO_ctrl(dgram_conn->io_ctx.bio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL) < 0) { v_print_log(VRS_PRINT_ERROR, "BIO_ctrl()\n"); goto end; } /* Try to get MTU from the bio */ ret = BIO_ctrl(dgram_conn->io_ctx.bio, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); if(ret > 0) { dgram_conn->io_ctx.mtu = ret; v_print_log(VRS_PRINT_DEBUG_MSG, "PMTU: %d\n", dgram_conn->io_ctx.mtu); } else { dgram_conn->io_ctx.mtu = DEFAULT_MTU; v_print_log(VRS_PRINT_DEBUG_MSG, "Default MTU: %d\n", dgram_conn->io_ctx.mtu); } #else dgram_conn->io_ctx.mtu = DEFAULT_MTU; #endif /* Set up necessary flag for V_CTX (client will be able to send and receive packets only to/from server) */ dgram_conn->io_ctx.flags |= SOCKET_CONNECTED; dgram_conn->host_id = (unsigned int)rand(); end: v_url_clear(&url); return dgram_conn; }
int main( int argc, const char * argv[] ) { #pragma unused(argc) #pragma unused(argv) int my_tests_count, i; int err; int my_failures = 0; int list_the_tests = 0; const char * my_targetp; time_t my_start_time, my_end_time; struct stat my_stat_buf; char my_buffer[64]; /* vars for XILog */ #if !TARGET_OS_EMBEDDED XILogRef logRef; char *logPath = ""; char *config = NULL; int echo = 0; int xml = 0; #endif sranddev( ); /* set up seed for our random name generator */ g_cmd_namep = argv[0]; /* NOTE - code in create_target_directory will append '/' if it is necessary */ my_targetp = getenv("TMPDIR"); if ( my_targetp == NULL ) my_targetp = "/tmp"; /* make sure our executable is owned by root and has set uid bit */ err = stat( g_cmd_namep, &my_stat_buf ); if ( err != 0 ) { err = errno; printf( "stat call on our executable failed - \"%s\" \n", g_cmd_namep ); printf( " failed with error %d - \"%s\" \n", err, strerror( err) ); exit( -1 ); } if ( my_stat_buf.st_uid != 0 || (my_stat_buf.st_mode & S_ISUID) == 0 ) { printf( "executable file - \"%s\" \n", g_cmd_namep ); printf( "does not have correct owner (must be root) or setuid bit is not set \n" ); exit( -1 ); } /* parse input parameters */ for ( i = 1; i < argc; i++ ) { if ( strcmp( argv[i], "-u" ) == 0 ) { usage( ); } if ( strcmp( argv[i], "-t" ) == 0 || strcmp( argv[i], "-target" ) == 0 ) { if ( ++i >= argc ) { printf( "invalid target parameter \n" ); usage( ); } /* verify our target directory exists */ my_targetp = argv[i]; err = stat( my_targetp, &my_stat_buf ); if ( err != 0 || S_ISDIR(my_stat_buf.st_mode) == 0 ) { printf( "invalid target path \n" ); if ( err != 0 ) { printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) ); } usage( ); } continue; } if ( strcmp( argv[i], "-f" ) == 0 || strcmp( argv[i], "-failures" ) == 0 ) { if ( ++i >= argc ) { printf( "invalid failures parameter \n" ); usage( ); } /* get our max number of failures */ g_max_failures = strtol( argv[i], NULL, 10 ); continue; } if ( strcmp( argv[i], "-l" ) == 0 || strcmp( argv[i], "-list" ) == 0 ) { /* list all the tests this tool will do. */ list_the_tests = 1; continue; } if ( strcmp( argv[i], "-r" ) == 0 || strcmp( argv[i], "-run" ) == 0 ) { if ( ++i >= argc ) { printf( "invalid run tests parameter \n" ); usage( ); } /* get which tests to run */ if ( parse_tests_to_run( argc, argv, &i ) != 0 ) { printf( "invalid run tests parameter \n" ); usage( ); } continue; } if ( strcmp( argv[i], "-s" ) == 0 || strcmp( argv[i], "-skip" ) == 0 ) { /* set that want to skip the setuid related tests - this is useful for debgugging since since I can't * get setuid tests to work while in gdb. */ g_skip_setuid_tests = 1; continue; } #if !TARGET_OS_EMBEDDED if ( strcmp( argv[i], "-x" ) == 0 || strcmp( argv[i], "-xilog" ) == 0 ) { g_xilog_active = 1; continue; } #endif printf( "invalid argument \"%s\" \n", argv[i] ); usage( ); } /* done parsing. */ /* Check if we are running under testbots */ #if RUN_UNDER_TESTBOTS g_testbots_active = 1; #endif /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[TEST] xnu_quick_test \n"); /* Declare the beginning of test suite */ } /* Populate groups list if we're in single user mode */ if (setgroups_if_single_user()) { return 1; } if ( list_the_tests != 0 ) { list_all_tests( ); return 0; } #if !TARGET_OS_EMBEDDED if (g_xilog_active == 1) { logRef = XILogOpenLogExtended( logPath, "xnu_quick_test", "com.apple.coreos", config, xml, echo, NULL, "ResultOwner", "com.apple.coreos", NULL ); if( logRef == NULL ) { fprintf(stderr,"Couldn't create log: %s",logPath); exit(-1); } } #endif /* build a test target directory that we use as our path to create any test * files and directories. */ create_target_directory( my_targetp ); printf( "Will allow %ld failures before testing is aborted \n", g_max_failures ); my_start_time = time( NULL ); printf( "\nBegin testing - %s \n", ctime_r( &my_start_time, &my_buffer[0] ) ); printf( "Current architecture is %s\n", current_arch() ); /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[PASS] xnu_quick_test started\n"); } /* run each test that is marked to run in our table until we complete all of them or * hit the maximum number of failures. */ my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] )); for ( i = 0; i < (my_tests_count - 1); i++ ) { int my_err; test_entryp my_testp; my_testp = &g_tests[i]; if ( my_testp->test_run_it == 0 || my_testp->test_routine == NULL ) continue; #if !TARGET_OS_EMBEDDED if (g_xilog_active == 1) { XILogBeginTestCase( logRef, my_testp->test_infop, my_testp->test_infop ); XILogMsg( "test #%d - %s \n", (i + 1), my_testp->test_infop ); } #endif printf( "test #%d - %s \n", (i + 1), my_testp->test_infop ); fflush(stdout); my_err = my_testp->test_routine( my_testp->test_input ); if ( my_err != 0 ) { printf("\t--> FAILED \n"); #if !TARGET_OS_EMBEDDED if (g_xilog_active == 1) { XILogMsg("SysCall %s failed", my_testp->test_infop); XILogErr("Result %d", my_err); } #endif my_failures++; if ( my_failures > g_max_failures ) { #if !TARGET_OS_EMBEDDED if (g_xilog_active == 1) { XILogMsg("Reached the maximum number of failures - Aborting xnu_quick_test."); XILogEndTestCase( logRef, kXILogTestPassOnErrorLevel ); } #endif printf( "\n Reached the maximum number of failures - Aborting xnu_quick_test. \n" ); /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[FAIL] %s \n", my_testp->test_infop); } goto exit_this_routine; } /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[FAIL] %s \n", my_testp->test_infop); } #if !TARGET_OS_EMBEDDED if (g_xilog_active == 1) { XILogEndTestCase( logRef, kXILogTestPassOnErrorLevel ); } #endif continue; } #if !TARGET_OS_EMBEDDED if (g_xilog_active == 1) { XILogEndTestCase(logRef, kXILogTestPassOnErrorLevel); } #endif /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[PASS] %s \n", my_testp->test_infop); } } exit_this_routine: my_end_time = time( NULL ); printf( "\nEnd testing - %s \n", ctime_r( &my_end_time, &my_buffer[0] ) ); /* clean up our test directory */ rmdir( &g_target_path[0] ); #if !TARGET_OS_EMBEDDED if (g_xilog_active == 1) { XILogCloseLog(logRef); } #endif return 0; } /* main */
/** * \brief Main Verse server loop. Server waits for connect attempts, responds to attempts * and creates per connection threads */ int vs_main_listen_loop(VS_CTX *vs_ctx) { struct vContext *C; struct timeval start, tv; fd_set set; int count, tmp, i, ret; int sockfd; /* Allocate context for server */ C = (struct vContext*)calloc(1, sizeof(struct vContext)); /* Set up client context, connection context and IO context */ CTX_server_ctx_set(C, vs_ctx); CTX_client_ctx_set(C, NULL); CTX_current_dgram_conn_set(C, NULL); /* Get time of the start of the server */ gettimeofday(&start, NULL); /* Seed random number generator */ #ifdef __APPLE__ sranddev(); /* Other BSD based systems probably support this or similar function too. */ #else /* Other systems have to use this evil trick */ srand(start.tv_sec - start.tv_usec); #endif /* "Never ending" listen loop */ while(vs_ctx->state == SERVER_STATE_READY) { /* Debug print */ gettimeofday(&tv, NULL); if(is_log_level(VRS_PRINT_DEBUG_MSG)) { if(tv.tv_sec==start.tv_sec) v_print_log(VRS_PRINT_DEBUG_MSG, "\t+0s"); else v_print_log(VRS_PRINT_DEBUG_MSG, "\t+%lds", (long int)(tv.tv_sec - start.tv_sec)); #ifdef WSLAY v_print_log_simple(VRS_PRINT_DEBUG_MSG, "\tServer listen on (TCP port: %d, WebSocket port: %d)\n", vs_ctx->tcp_io_ctx.host_addr.port, vs_ctx->ws_io_ctx.host_addr.port); #else v_print_log_simple(VRS_PRINT_DEBUG_MSG, "\tServer listen on TCP port: %d\n", vs_ctx->tcp_io_ctx.host_addr.port); #endif } /* Set up set of sockets */ FD_ZERO(&set); FD_SET(vs_ctx->tcp_io_ctx.sockfd, &set); /* When Verse is compiled with support of WebSocket, then listen on * WebSocket port too */ #ifdef WSLAY FD_SET(vs_ctx->ws_io_ctx.sockfd, &set); sockfd = (vs_ctx->tcp_io_ctx.sockfd > vs_ctx->ws_io_ctx.sockfd) ? vs_ctx->tcp_io_ctx.sockfd : vs_ctx->ws_io_ctx.sockfd; #else sockfd = vs_ctx->tcp_io_ctx.sockfd; #endif /* We will wait one second for connect attempt, then debug print will * be print again */ tv.tv_sec = 1; tv.tv_usec = 0; /* Wait for event on listening sockets */ if( (ret = select(sockfd+1, &set, NULL, NULL, &tv)) == -1 ) { int err = errno; if(err==EINTR) { break; } else { if(is_log_level(VRS_PRINT_ERROR)) v_print_log(VRS_PRINT_ERROR, "%s:%s():%d select(): %s\n", __FILE__, __FUNCTION__, __LINE__, strerror(err)); return -1; } /* Was event on the listen socket */ } else if(ret>0) { if (FD_ISSET(vs_ctx->tcp_io_ctx.sockfd, &set)) { v_print_log(VRS_PRINT_DEBUG_MSG, "TCP Connection attempt\n"); CTX_io_ctx_set(C, &vs_ctx->tcp_io_ctx); vs_new_stream_conn(C, vs_tcp_conn_loop); #ifdef WSLAY } else if(FD_ISSET(vs_ctx->ws_io_ctx.sockfd, &set)) { v_print_log(VRS_PRINT_DEBUG_MSG, "WebSocket Connection attempt\n"); CTX_io_ctx_set(C, &vs_ctx->ws_io_ctx); vs_new_stream_conn(C, vs_websocket_loop); #endif } } } count = 0; while( (vs_ctx->state == SERVER_STATE_CLOSING) && (count < VRS_TIMEOUT) ) { v_print_log(VRS_PRINT_DEBUG_MSG, "Wait for Server state CLOSED\n"); /* Check if there are still some pending connection */ tmp = 0; for(i=0; i<vs_ctx->max_sessions; i++) { if(vs_ctx->vsessions[i] != NULL) { if(vs_ctx->vsessions[i]->stream_conn != NULL && vs_ctx->vsessions[i]->stream_conn->host_state != TCP_SERVER_STATE_LISTEN ) { tmp++; } /* TODO: cancel thread with closed connection to speed up server exit pthread_kill(vs_ctx->vsessions[i]->tcp_thread, SIGALRM); pthread_join(vs_ctx->vsessions[i]->tcp_thread, NULL); */ } } if(tmp==0) { vs_ctx->state = SERVER_STATE_CLOSED; } else { sleep(1); } } free(C); return 1; }
int main( int argc, const char * argv[] ) { #pragma unused(argc) #pragma unused(argv) int my_tests_count, i; int err; int my_failures = 0; int list_the_tests = 0; const char * my_targetp; time_t my_start_time, my_end_time; struct stat my_stat_buf; char my_buffer[64]; uid_t sudo_uid = 0; const char * sudo_uid_env; gid_t sudo_gid; const char * sudo_gid_env; sranddev( ); /* set up seed for our random name generator */ g_cmd_namep = argv[0]; /* make sure SIGCHLD is not ignored, so wait4 calls work */ signal(SIGCHLD, SIG_DFL); /* NOTE - code in create_target_directory will append '/' if it is necessary */ my_targetp = getenv("TMPDIR"); if ( my_targetp == NULL ) my_targetp = "/tmp"; /* make sure we are running as root */ if ( ( getuid() != 0 ) || ( geteuid() != 0 ) ) { printf( "Test must be run as root\n", g_cmd_namep ); exit( -1 ); } sudo_uid_env = getenv("SUDO_UID"); if ( sudo_uid_env ) { sudo_uid = strtol(sudo_uid_env, NULL, 10); } /* switch real uid to a non_root user, while keeping effective uid as root */ if ( sudo_uid != 0 ) { setreuid( sudo_uid, 0 ); } else { /* Default to 501 if no sudo uid found */ setreuid( 501, 0 ); } /* restore the gid if run through sudo */ sudo_gid_env = getenv("SUDO_GID"); if ( sudo_gid_env ) { sudo_gid = strtol(sudo_gid_env, NULL, 10); } if ( getgid() == 0 ) { if ( sudo_gid != 0 ) { setgid( sudo_gid ); } else { /* Default to 20 if no sudo gid found */ setgid( 20 ); } } /* parse input parameters */ for ( i = 1; i < argc; i++ ) { if ( strcmp( argv[i], "-u" ) == 0 ) { usage( ); } if ( strcmp( argv[i], "-t" ) == 0 || strcmp( argv[i], "-target" ) == 0 ) { if ( ++i >= argc ) { printf( "invalid target parameter \n" ); usage( ); } /* verify our target directory exists */ my_targetp = argv[i]; err = stat( my_targetp, &my_stat_buf ); if ( err != 0 || S_ISDIR(my_stat_buf.st_mode) == 0 ) { printf( "invalid target path \n" ); if ( err != 0 ) { printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) ); } usage( ); } continue; } if ( strcmp( argv[i], "-f" ) == 0 || strcmp( argv[i], "-failures" ) == 0 ) { if ( ++i >= argc ) { printf( "invalid failures parameter \n" ); usage( ); } /* get our max number of failures */ g_max_failures = strtol( argv[i], NULL, 10 ); continue; } if ( strcmp( argv[i], "-l" ) == 0 || strcmp( argv[i], "-list" ) == 0 ) { /* list all the tests this tool will do. */ list_the_tests = 1; continue; } if ( strcmp( argv[i], "-r" ) == 0 || strcmp( argv[i], "-run" ) == 0 ) { if ( ++i >= argc ) { printf( "invalid run tests parameter \n" ); usage( ); } /* get which tests to run */ if ( parse_tests_to_run( argc, argv, &i ) != 0 ) { printf( "invalid run tests parameter \n" ); usage( ); } continue; } if ( strcmp( argv[i], "-s" ) == 0 || strcmp( argv[i], "-skip" ) == 0 ) { /* set that want to skip the setuid related tests - this is useful for debgugging since since I can't * get setuid tests to work while in gdb. */ g_skip_setuid_tests = 1; continue; } if ( strcmp( argv[i], "-testbot" ) == 0 ) { g_testbots_active = 1; continue; } printf( "invalid argument \"%s\" \n", argv[i] ); usage( ); } /* done parsing. */ /* Check if we are running under testbots */ #if RUN_UNDER_TESTBOTS g_testbots_active = 1; #endif /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[TEST] xnu_quick_test \n"); /* Declare the beginning of test suite */ } /* Populate groups list if we're in single user mode */ if (setgroups_if_single_user()) { return 1; } if ( list_the_tests != 0 ) { list_all_tests( ); return 0; } /* build a test target directory that we use as our path to create any test * files and directories. */ create_target_directory( my_targetp ); printf( "Will allow %ld failures before testing is aborted \n", g_max_failures ); my_start_time = time( NULL ); printf( "\nBegin testing - %s \n", ctime_r( &my_start_time, &my_buffer[0] ) ); printf( "Current architecture is %s\n", current_arch() ); /* Code added to run xnu_quick_test under testbots */ /* run each test that is marked to run in our table until we complete all of them or * hit the maximum number of failures. */ my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] )); for ( i = 0; i < (my_tests_count - 1); i++ ) { int my_err; test_entryp my_testp; my_testp = &g_tests[i]; if ( my_testp->test_run_it == 0 || my_testp->test_routine == NULL ) continue; if ( g_testbots_active == 1 ) { printf("[BEGIN] %s \n", my_testp->test_infop); } printf( "test #%d - %s \n", (i + 1), my_testp->test_infop ); fflush(stdout); my_err = my_testp->test_routine( my_testp->test_input ); if ( my_err != 0 ) { printf("\t--> FAILED \n"); printf("SysCall %s failed", my_testp->test_infop); printf("Result %d", my_err); my_failures++; if ( my_failures > g_max_failures ) { printf( "\n Reached the maximum number of failures - Aborting xnu_quick_test. \n" ); /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[FAIL] %s \n", my_testp->test_infop); } goto exit_this_routine; } /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("\n[FAIL] %s \n", my_testp->test_infop); } continue; } /* Code added to run xnu_quick_test under testbots */ if ( g_testbots_active == 1 ) { printf("[PASS] %s \n", my_testp->test_infop); } } exit_this_routine: my_end_time = time( NULL ); printf( "\nEnd testing - %s \n", ctime_r( &my_end_time, &my_buffer[0] ) ); /* clean up our test directory */ rmdir( &g_target_path[0] ); /* exit non zero if there are any failures */ return my_failures != 0; } /* main */