Test whether a process left any orphans behind

M.K.

I don't need to wait for them, I just need a yes or no.

I'm executing a program that I know is going to fork (and the children are likely to fork too). If run correctly, it should take care of all the its children and exit them when it finishes. I need to write a test that checks if this is true. Orphaned processes get assigned to the init process, so I cannot see them in pstree anymore.

Bonus points if I can get a list of their PIDs so I can send SIGKILL to them. Ideally if it's something POSIX-y, so it would work on *BSD.

Gilles 'SO- stop being evil'

When a process terminates, the PPID of its children is set to 1 (adoption by init), but the PGID (process group identifier) and the SID (session identifier) don't change.

The process's children probably don't change their process group, unless they're intended to be daemons. Assuming they don't, start the process to be tested in its own process group. Call setpgid(getpid(), getpid()) from your test framework, after forking and before calling execve to execute the program being tested. Call kill(-test_program_pid, 0) (kill with a negative pid argument and the signal value 0) to test whether there is a running process with the PGID test_program_pid. Pass SIGKILL as the signal argument to kill them all.

test_program_pid = fork();
if (test_program_pid) {
    waitpid(test_program_pid, &status, 0);
    if (kill(-test_program_pid, 0)) {
        record_failue("some child processes were not terminated properly");
    }
    kill(-test_program_pid, SIGKILL);
} else {
    setpgid(getpid(), getpid());
    execve("/program/to/test", …);
}

An alternative method would be to create a temporary file and open it in the program that you're testing and nowhere else. If the program calls execve, make sure that the file descriptor is opened without the O_CLOEXEC flag (or call fcntl(fd, FD_CLOEXEC, 0)). This method assumes that the program doesn't go and close the file descriptors that it doesn't use explicitly. You can then run fuser /temp/file to list the processes that have this file open, and fuser -k /temp/file to kill them. A variant of this approach that works even with programs that close file descriptors they don't use, but assumes that the program doesn't change its current directory, is to create a temporary directory and change to that directory to run the program.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel function to test whether string contains ANY of $THESE CHARACTERS

From Dev

How do i kill a process left behind after an aborted Jenkins job, using the groovy script console?

From Dev

Handlebarsjs left behind expression

From Dev

What is the process behind this output?

From Dev

How can I test in RSpec whether code I've loaded has any Ruby warnings?

From Dev

Check whether there are any updates

From Dev

Test whether globbing is enabled

From Dev

Why is a process suspended by another process behind it?

From Dev

Determine whether the parent process is a terminal

From Dev

How to check whether a process is daemon or not?

From Dev

Is there any hidden functionality behind AsNoTracking()?

From Dev

In Python is there any way to append behind?

From Dev

How to determine whether a process handle is of the current process?

From Dev

Java process execute "tail -f test.log | grep abc" can't not get any output

From Dev

Test whether a value is a string in Coldfusion

From Dev

Test whether a Sass function is defined

From Dev

Test whether a property name exists

From Dev

Test whether fortran is installed on OSX

From Dev

Test whether a Sass function is defined

From Dev

QThread always one thread left behind

From Dev

Swift - garbage collection - something gets left behind

From Dev

Deciding whether a test is a Unit or Integration test

From Dev

Detect whether button was right or left clicked

From Dev

Detect whether button was right or left clicked

From Dev

python kill parent process but child process left

From Dev

Finding whether a row in a table is visible or not in code behind in asp.net

From Dev

Tell whether route is behind firewall in Symfony2

From Dev

Determine whether iPhone is really connected to the internet or just behind a restricted hotspot

From Dev

Predicate to check whether an element is behind another element in a list

Related Related

  1. 1

    Excel function to test whether string contains ANY of $THESE CHARACTERS

  2. 2

    How do i kill a process left behind after an aborted Jenkins job, using the groovy script console?

  3. 3

    Handlebarsjs left behind expression

  4. 4

    What is the process behind this output?

  5. 5

    How can I test in RSpec whether code I've loaded has any Ruby warnings?

  6. 6

    Check whether there are any updates

  7. 7

    Test whether globbing is enabled

  8. 8

    Why is a process suspended by another process behind it?

  9. 9

    Determine whether the parent process is a terminal

  10. 10

    How to check whether a process is daemon or not?

  11. 11

    Is there any hidden functionality behind AsNoTracking()?

  12. 12

    In Python is there any way to append behind?

  13. 13

    How to determine whether a process handle is of the current process?

  14. 14

    Java process execute "tail -f test.log | grep abc" can't not get any output

  15. 15

    Test whether a value is a string in Coldfusion

  16. 16

    Test whether a Sass function is defined

  17. 17

    Test whether a property name exists

  18. 18

    Test whether fortran is installed on OSX

  19. 19

    Test whether a Sass function is defined

  20. 20

    QThread always one thread left behind

  21. 21

    Swift - garbage collection - something gets left behind

  22. 22

    Deciding whether a test is a Unit or Integration test

  23. 23

    Detect whether button was right or left clicked

  24. 24

    Detect whether button was right or left clicked

  25. 25

    python kill parent process but child process left

  26. 26

    Finding whether a row in a table is visible or not in code behind in asp.net

  27. 27

    Tell whether route is behind firewall in Symfony2

  28. 28

    Determine whether iPhone is really connected to the internet or just behind a restricted hotspot

  29. 29

    Predicate to check whether an element is behind another element in a list

HotTag

Archive