Ejemplo n.º 1
0
Archivo: sexe.c Proyecto: buzz26/toyBox
//
//	サービスメイン処理
//
void service_main(void)
{
	PROCESS_INFORMATION pi;

	service_handle = RegisterServiceCtrlHandler(service_name,
	                               (LPHANDLER_FUNCTION)service_control_handler);
	if(service_handle != 0) {
		send_status_to_scm(SERVICE_START_PENDING, 0, 1);

		send_status_to_scm(SERVICE_RUNNING, 0, 0);
		// プログラムを起動する。
		if(execute_process(exe_name, option_name, &pi)) {
			// 2009/3/26
			shutdown_flag = 0;

			process_id = pi.dwProcessId;
			send_status_to_scm(SERVICE_RUNNING, 0, 0);

			// サービスの終了で抜ける
			while(1) {
		    	if(WaitForSingleObject(pi.hProcess, 0) != WAIT_TIMEOUT) {
					// 2001/11/9
					CloseHandle(pi.hThread);
					CloseHandle(pi.hProcess);
					// 2009/3/26
					if(!retry_flag || shutdown_flag) {
						break;
					} else {
						if(execute_process(exe_name, option_name, &pi)) {
							process_id = pi.dwProcessId;
						} else {
							break;
						}
					}
				}
				Sleep(100);
			}
		}
// 2011/9/1 kimukou.buzz
		//if(end_pattern == END_CTRL_BREAK) {
		if(end_pattern == END_CTRL_BREAK || end_pattern == END_CTRL_C) {
// 2011/9/1 kimukou.buzz
			FreeConsole();
		}
		CloseHandle(mutex);
	}
	send_status_to_scm(SERVICE_STOPPED, 0, 0);
}
Ejemplo n.º 2
0
void scanner()
{
	int i,num_args=0;
	char *command,*argm[MAX]={NULL},str[MAX],ch = gc();
	if(ch!=-1 && ch!=3)
	{
		while(ch != '\n')
		{
			while(ch == ' ' || ch == '\t')
				ch = gc();
			int j=0;
			if(ch=='\n')
				break;
			while(1)
			{
				if( ch == ' ' || ch == '\n' || ch == '\t')
					break;
				str[j++] = ch;
				ch = gc();
			}
			str[j] = '\0';
			if(!strcmp("~",str))
				strcpy(str,getenv("HOME"));
			argm[num_args++] = strdup(str);
		}
		argm[num_args++] = NULL;
		if(argm[0])
		{
			command = strdup(argm[0]);
			execute_process(command,argm,num_args);
		}
	}
	else
		printf("\n");
}
Ejemplo n.º 3
0
//------------------------------------------------------------------------------
// Name: open
// Desc:
//------------------------------------------------------------------------------
bool DebuggerCore::open(const QString &path, const QString &cwd, const QList<QByteArray> &args, const QString &tty) {
	detach();
	pid_t pid;

	switch(pid = fork()) {
	case 0:
		// we are in the child now...

		// set ourselves (the child proc) up to be traced
		ptrace(PT_TRACE_ME, 0, 0, 0);

		// redirect it's I/O
		if(!tty.isEmpty()) {
			FILE *const std_out = freopen(qPrintable(tty), "r+b", stdout);
			FILE *const std_in  = freopen(qPrintable(tty), "r+b", stdin);
			FILE *const std_err = freopen(qPrintable(tty), "r+b", stderr);

			Q_UNUSED(std_out);
			Q_UNUSED(std_in);
			Q_UNUSED(std_err);
		}

		// do the actual exec
		execute_process(path, cwd, args);

		// we should never get here!
		abort();
		break;
	case -1:
		// error!
		pid_ = 0;
		return false;
	default:
		// parent
		do {
			threads_.clear();

			int status;
			if(native::waitpid(pid, &status, 0) == -1) {
				return false;
			}

			// the very first event should be a STOP of type SIGTRAP
			if(!WIFSTOPPED(status) || WSTOPSIG(status) != SIGTRAP) {
				detach();
				return false;
			}

			// setup the first event data for the primary thread
			threads_.insert(pid, thread_info());
			pid_                 = pid;
			active_thread_       = pid;
			threads_[pid].status = status;
			return true;
		} while(0);
		break;
	}
}
Ejemplo n.º 4
0
void not_hist_n(int n)
{
	if(n>num_proc-1)
		return;
	char *c = strdup(proc[n-1].proc_name),*c1[MAX];
	int i;
	for(i=0;i<proc[n-1].proc_num_args-1;i++)
		c1[i] = strdup(proc[n-1].proc_args[i]);
	execute_process(c,c1,proc[n-1].proc_num_args);
}
Ejemplo n.º 5
0
void process_command ()
{ char action;
  char fname[100];
  int pid, time, ret;

  printf ("command> ");
  scanf ("%c", &action);
  while (action != 'T')
  { switch (action)
    { case 's':   // submit
        scanf ("%s", &fname);
        if (Debug) printf ("File name: %s is submitted\n", fname);
        submit_process (fname);
        break;
      case 'x':  // execute
        execute_process ();
        break;
      case 'r':  // dump register
        dump_registers ();
        break;
      case 'q':  // dump ready queue and list of processes completed IO
        dump_ready_queue ();
        dump_doneWait_list ();
        break;
      case 'p':   // dump PCB
        printf ("PCB Dump Starts: Checks from 0 to %d\n", currentPid);
        for (pid=1; pid<currentPid; pid++)
          if (PCB[pid] != NULL) dump_PCB (pid);
        break;
      case 'e':   // dump events in timer
        dump_events ();
        break;
      case 'm':   // dump Memory
        for (pid=1; pid<currentPid; pid++)
          if (PCB[pid] != NULL) dump_memory (pid);
        break;
      case 'w':   // dump Swap Space
         for (pid=1; pid<currentPid; pid++)
            if (PCB[pid] != NULL) dump_swap_memory (pid);
        break;
      case 'l':   // dump Spool
        for (pid=1; pid<currentPid; pid++)
          if (PCB[pid] != NULL) dump_spool (pid);
        break;
      case 'T':  // Terminate, do nothing, terminate in while loop
        break;
      default: 
        printf ("Incorrect command!!!\n");
    }
    printf ("\ncommand> ");
    scanf ("\n%c", &action);
    if (Debug) printf ("Next command is %c\n", action);
  }
}
Ejemplo n.º 6
0
Archivo: sexe.c Proyecto: buzz26/toyBox
//
//	テスト起動
//	in: exe_name .. プログラムファイルパス
//	    option_name .. プログラムコマンドラインオプション
//	    pattern .. 終了パターン
//
void execute_program(HWND hDlg, TCHAR *exe_name, TCHAR *option_name, int pattern)
{
	PROCESS_INFORMATION pi;
	DWORD count;

	// プログラムを起動する。
	if(execute_process(exe_name, option_name, &pi)) {
		WaitForInputIdle(pi.hProcess,INFINITE);
		SetForegroundWindow(hDlg);
		// プログラムを起動しました。\nOKをクリックするとプログラムを終了します。\n終了しない場合、手動でプログラムを終了させてください。\n\n※これは sexe からプログラムを終了されられるかどうかのテストで、\n 実際にサービスでの正常動作が保証されるわけではありません\n のでご注意ください。
		MessageBoxResourceText(hDlg, IDS_TEST_EXECUTE, NULL, HEADER, MB_OK);
		process_id = pi.dwProcessId;
// 2011/9/19 kimukou.buzz
		if(pattern == END_CTRL_BREAK) {
			GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, process_id);
		}else if(pattern == END_CTRL_C) {
			GenerateConsoleCtrlEvent(CTRL_BREAK_C, process_id);
// 2011/9/19 kimukou.buzz
		} else {
			EnumWindows(EnumWindowsProc, pattern);
		}
		count = GetTickCount() + 30 * 1000;
		// プログラム終了もしくタイムアウト(30s)で抜ける
		while(count > GetTickCount()) {
	    	if(WaitForSingleObject(pi.hProcess, 0) != WAIT_TIMEOUT) {
				break;
			}
			Sleep(100);
		}
		// 2001/11/9
		CloseHandle(pi.hThread);
		CloseHandle(pi.hProcess);
	}
// 2011/9/19 kimukou.buzz
	//if(pattern == END_CTRL_BREAK) {
	if(pattern == END_CTRL_BREAK || pattern == END_CTRL_C) {
// 2011/9/19 kimukou.buzz
		FreeConsole();
	}
}
Ejemplo n.º 7
0
int exec_with_redirect_from_stdout(char *command_line, BOOL use_cmd_exe, char *** results)
{
	execution_details * details = (execution_details *)palloc(sizeof(execution_details));

	if (!initialize_files(details)) return FALSE;

	// use the current OS comspec for DOS commands, i.e., DIR
	char cmd[MAX_PATH] = { 0 };

	prepare_command_line(cmd, command_line, use_cmd_exe);

	if (!execute_process(details, cmd)) return FALSE;

	int result_count = extract_result(details, results);

	close_handles(details);

	DeleteFile(details->temp_filename);

	pfree(details);

	return result_count;

}
Ejemplo n.º 8
0
Archivo: botox.c Proyecto: stezqy/botox
int main(int argc, char *argv[])
{
	useconds_t ts = 0;
	char *target = NULL;
	pid_t min_pid = getpid();
	int c = 0, pid = 0, gdb = 0, ret_val = EXIT_FAILURE;
	int long_opt_index = 0;
	char *short_options = "p:t:gh";
	struct option long_options[] = {
			{ "pid", required_argument, NULL, 'p' },
			{ "time", required_argument, NULL, 't' },
			{ "gdb", no_argument, NULL, 'g' },
			{ "help", no_argument, NULL, 'h' },
			{ 0, 0, 0, 0 }
        };

	if(argc < 2)
	{
		usage(argv[0]);
		goto end;
	}

	while((c = getopt_long(argc, argv, short_options, long_options, &long_opt_index)) != -1)
	{
		switch(c)
		{
			case 'p':
				min_pid = atoi(optarg);
				break;
			case 't':
				ts = atoi(optarg);
				break;
			case 'g':
				gdb = 1;
				break;
			default:
				usage(argv[0]);
				goto end;
		}
	}

	target = argv[argc-1];

	printf("Waiting for process '%s' with a pid greater than %d\n", target, min_pid);

	while((pid = get_process_pid(target, min_pid)) == -1) { }

	usleep(ts);

	if(kill(pid, SIGSTOP) == -1)
	{
		perror("kill");
	}
	else
	{
		printf("%s (pid %d) has been paused.\n", target, pid);
		if(gdb)
		{
			execute_process(pid);
		}
		ret_val = EXIT_SUCCESS;
	}

end:
	return ret_val;
}
Ejemplo n.º 9
0
//------------------------------------------------------------------------------
// Name: open
// Desc:
//------------------------------------------------------------------------------
bool DebuggerCore::open(const QString &path, const QString &cwd, const QList<QByteArray> &args, const QString &tty) {
	detach();

	switch(pid_t pid = fork()) {
	case 0:
		// we are in the child now...

		// set ourselves (the child proc) up to be traced
		ptrace_traceme();

		// redirect it's I/O
		if(!tty.isEmpty()) {
			FILE *const std_out = freopen(qPrintable(tty), "r+b", stdout);
			FILE *const std_in  = freopen(qPrintable(tty), "r+b", stdin);
			FILE *const std_err = freopen(qPrintable(tty), "r+b", stderr);

			Q_UNUSED(std_out);
			Q_UNUSED(std_in);
			Q_UNUSED(std_err);
		}

		// do the actual exec
		execute_process(path, cwd, args);

		// we should never get here!
		abort();
		break;
	case -1:
		// error! for some reason we couldn't fork
		reset();
		return false;
	default:
		// parent
		do {
			reset();

			int status;
			if(native::waitpid(pid, &status, __WALL) == -1) {
				return false;
			}

			// the very first event should be a STOP of type SIGTRAP
			if(!WIFSTOPPED(status) || WSTOPSIG(status) != SIGTRAP) {
				detach();
				return false;
			}

			waited_threads_.insert(pid);

			// enable following clones (threads)
			if(ptrace_set_options(pid, PTRACE_O_TRACECLONE) == -1) {
				qDebug("[DebuggerCore] failed to set PTRACE_SETOPTIONS: %s", strerror(errno));
				detach();
				return false;
			}
			
#ifdef PTRACE_O_EXITKILL
			if(ptrace_set_options(pid, PTRACE_O_EXITKILL) == -1) {
				qDebug("[DebuggerCore] failed to set PTRACE_SETOPTIONS: %s", strerror(errno));
				detach();
				return false;
			}
#endif

			// setup the first event data for the primary thread
			waited_threads_.insert(pid);
			
			// create the process
			process_ = new PlatformProcess(this, pid);
			

			// the PID == primary TID
			auto newThread     = std::make_shared<PlatformThread>(this, process_, pid);
			newThread->status_ = status;
			newThread->state_  = PlatformThread::Stopped;
			
			threads_[pid]   = newThread;

			pid_            = pid;
			active_thread_   = pid;
			binary_info_    = edb::v1::get_binary_info(edb::v1::primary_code_region());

			detectDebuggeeBitness();

			return true;
		} while(0);
		break;
	}
}