Ptrace /等待非孩子

时间:2013-12-04 02:23:09

标签: c ptrace waitpid

int Enable ( int pid) 
{
int status;
#if 1 
    {
    printf ( "child pid = %d \n", pid );
long ret = ptrace (PTRACE_ATTACH, pid, NULL, NULL);


        do {
            int w = waitpid(-1, &status, 0);
            if (w == -1) {
                perror("waitpid error :");
                exit(EXIT_FAILURE);
            }

            if (WIFEXITED(status)) {
                printf("exited, status=%d\n", WEXITSTATUS(status));
            } else if (WIFSIGNALED(status)) {
                printf("killed by signal %d\n", WTERMSIG(status));
            } else if (WIFSTOPPED(status)) {
                printf("stopped by signal %d\n", WSTOPSIG(status));
            } else if (WIFCONTINUED(status)) {
                printf("continued\n");
            }
        } while (!WIFEXITED(status) && !WIFSIGNALED(status));

        exit(EXIT_SUCCESS);
    }
#endif
//  while ((result = wait(&status)) != -1 && result != pid){ printf (" this is not my child go back \n"); };
}
int main(int arg, char*argv[])
{

    Enable(atoi(argv[1]));
    sleep(125);
}

- 我用pid 6841运行了一个守护进程,并尝试在ptrace-attach之后等待它

./ptrace 6841
child pid = 6841 
waitpid error :: No child processes

简而言之,我希望能够等待非子流程 - 欢迎任何其他计划。

2 个答案:

答案 0 :(得分:0)

  

糟糕。如果我不是root,那就不行了。)

这是记录在案的行为;见ptrace() - Unix, Linux System Call e。克。

  

非root进程无法跟踪无法发送的进程   发信号到

答案 1 :(得分:-1)

public void getLabel(){
    labelTempoGara = new TimeRefreshRace();
    labelTempoGara.setForeground(Color.white);
    labelTempoGara.start();
}
    class TimeRefreshRace extends JLabel implements Runnable {

        private boolean isAlive = false;

        public void start() {
            threadTempoCorsa = new Thread(this);
            isAlive = true;
            threadTempoCorsa.start();
        }

        public void run() {
            int tempoInSecondi = programma.getTempoGara() % 60;
            int minuti = programma.getTempoGara() / 60;
            while (isAlive) {
                try {
                    if (minuti <= 0 && tempoInSecondi<=1) {
                        isRun=false;
                        this.setText("00:00");
                        isAlive = false;
                        salvaDatiCorsa();
                        break;
                    }
                    if(tempoInSecondi<=1){
                        minuti--;
                        tempoInSecondi=60;
                    }
                    --tempoInSecondi;
                    this.setText(minuti+":"+ tempoInSecondi);
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    log.logStackTrace(e);
                }
            }
        }

        public void kill() {
            isAlive = false;
            isRun=false;
        }

    }//fine autoclass