Пример #1
0
Файл: main.c Проект: Prajna/xnu
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 */
Пример #2
0
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 */