Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/ptrace.h>
- #include <errno.h>
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <sys/wait.h>
- int main(int argc, char** argv) {
- int pid = atoi(argv[1]);
- int status;
- siginfo_t si;
- switch (ptrace(PTRACE_ATTACH, pid, NULL)) {
- case 0:
- break;
- case -ESRCH:
- case -EPERM:
- return 0;
- default:
- fprintf(stderr, "Failed to attach childn");
- return 1;
- }
- if (pid != wait(&status)) {
- fprintf(stderr, "wrong wait signaln");
- return 1;
- }
- if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP)) {
- /* The pid might not be running */
- if (!kill(pid, 0)) {
- fprintf(stderr, "SIGSTOP didn't stop childn");
- return 1;
- } else {
- return 0;
- }
- }
- if (ptrace(PTRACE_CONT, pid, 0, 0)) {
- fprintf(stderr, "Failed to restart childn");
- return 1;
- }
- while (1) {
- if (waitid(P_PID, pid, &si, WSTOPPED | WEXITED)) {
- // an error occurred.
- if (errno == ECHILD)
- return 0;
- return 1;
- }
- errno = 0;
- if (si.si_code & (CLD_STOPPED | CLD_TRAPPED)) {
- /* If the child gets stopped, we have to PTRACE_CONT it
- * this will happen when the child has a child that exits.
- **/
- if (ptrace(PTRACE_CONT, pid, 1, si.si_status)) {
- if (errno == ENOSYS) {
- /* Wow, we're stuffed. Stop and return */
- return 0;
- }
- }
- continue;
- }
- if (si.si_code & (CLD_EXITED | CLD_KILLED | CLD_DUMPED)) {
- return si.si_status;
- }
- // Fall through to exiting.
- return 1;
- }
- }
- use POSIX qw(:sys_wait_h);
- sub child_handler {
- while ((my $child = waitpid(-1, WNOHANG)) > 0) {
- # We've caught a process dying, its PID is now in $child.
- # The exit value and other information is in $?
- }
- $SIG{CHLD} &child_handler; # SysV systems clear handlers when called,
- # so we need to re-instate it.
- }
- # This establishes our handler.
- $SIG{CHLD} = &child_handler;
- bool WaitForProcessExit( DWORD _dwPID )
- {
- HANDLE hProc = NULL;
- bool bReturn = false;
- hProc = OpenProcess(SYNCHRONIZE, FALSE, _dwPID);
- if(hProc != NULL)
- {
- if ( WAIT_OBJECT_0 == WaitForSingleObject(hProc, INFINITE) )
- {
- bReturn = true;
- }
- }
- CloseHandle(hProc) ;
- }
- return bReturn;
- }
- Foo {
- func Stops(foo_sub) { run foo_sub; return 1; }
- }
Add Comment
Please, Sign In to add comment