CreateProcess 使得当父进程被杀死时子进程也被杀死?

发布于 2024-11-14 07:43:23 字数 482 浏览 3 评论 0原文

有没有办法调用 CreateProcess这样杀死父进程会自动杀死子进程吗?

也许使用创建进程标志

编辑
解决方案是创建一个作业对象,将父对象和子对象都放置在作业对象中。当父母被杀时,孩子也被杀。我从这里得到了代码: 当父进程被终止时终止子进程 请注意 @wilx 关于继承句柄的评论。

Is there a way to call CreateProcess such that killing the parent process automatically kills the child process?

Perhaps using Create Process Flags?

Edit
The solution is to create a job object, place both parent and child in the job object. Whent he parent is killed the child is killed. I got the code from here:
Kill child process when parent process is killed
Take note of @wilx's comment about inherited handles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

此刻的回忆 2024-11-21 07:43:23

将作业用作 Neil 说< /a> 恕我直言,这是最好的方法。您可以通过设置 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 使用 SetInformationJobObject()。当您的父进程退出/终止时,作业对象句柄将关闭。为此,子进程继承作业句柄至关重要。如果您还想跟踪孙进程,那么您将必须创建挂起的子进程,将它们添加到您的作业对象中,然后才让它们运行。

Using jobs as Neil says is IMHO the best way. You can make the child processes get killed when the job owning process dies by setting JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE on the job object using SetInformationJobObject(). The job object handle will be closed when your parent process exits/dies. For this to work it is essential that the job handle is not inherited by the child processes. If you want to track also the grand-child processes then you will have to create your child processes suspended, add them to your job object and only then let them run.

眼趣 2024-11-21 07:43:23

您能做的最好的事情就是将两个进程放在同一个作业中,这样杀死该作业就会杀死这两个进程。

The best you can do is to put both processes in the same job, so that killing the job kills both processes.

踏雪无痕 2024-11-21 07:43:23

您是否需要杀死子进程,或者只是检测父进程退出以便它可以干净地终止?父进程可以为其自身创建一个可继承的句柄,然后子进程可以将其与其自己的对象一起传递给 WaitForMultipleObjects(Ex)

如果子进程不是专门为此编写的,您可以将其标准输入附加到管道,该管道的另一端由父进程保存。如果父级死亡,管道将自动关闭。

这与 Unix 行为非常相似,其中子级不会在其父级死亡时被杀死,它通常会响应 SIGHUP 而退出(但它可以处理该信号并实现任何行为)。在 Linux 上,孤儿的父 PID 更改为 1 (init)。

Do you need the child process to be killed, or merely detect the parent process exit so it can terminate cleanly? The parent process can create an inheritable handle to itself, which the child can then pass to WaitForMultipleObjects(Ex) along with its own objects.

If the child process isn't written specifically for this, you could attach its stdin to a pipe, the other end of which is held by the parent process. If the parent dies, the pipe is automatically closed.

This closely parallels the Unix behavior, in which a child isn't killed when its parent dies, it generally exits in response to SIGHUP (but it can handle that signal and implement any behavior). On Linux, the parent PID of an orphan is changed to 1 (init).

ぶ宁プ宁ぶ 2024-11-21 07:43:23

一种不同的方法

所有情况下都没有用,但是我有一个特定的场景,其中应用程序完全控制子进程。用于通讯和API 拦截,需要 DLL 注入,以便 DLL 在实际子进程中运行以向父进程报告。

注意:现在,这不是创意奖。 这里的背景是:需要包装的应用程序,但它是遗留应用程序,既不能以令人满意的方式修改,也不能重写。我们需要保持这个系统运行,同时仍然拦截它的输出。

此外,子进程编写得不好,会立即再次启动然后终止,因此父进程实际上不是我们的主应用程序。

如果您控制子进程并拥有注入的 DLL,则可以通过监视父进程来检查它是否正在运行来扩展此 DLL。如果没有,ExitProcess

如果子进程中不需要 DLL,其他解决方案很可能会更好。


例如,您可以通过应用程序已使用的注册表项来传输 parentProcessID。或者,如果这不会破坏子进程,您可以传递命令行。

这必须在自己的线程中运行。我的 DLL 有两个线程:这里的代码和控制 & 线程。通讯代码。

DLL

bool IsProcessRunning(HANDLE hProcess)
{
    DWORD exitCode;
    GetExitCodeProcess(hProcess, &exitCode);
    return exitCode == STILL_ACTIVE;
}

bool WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH)
    {
        int parentProcessID = [...]
        
        HANDLE parentProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, parentProcessID);
        while (IsProcessRunning(parentHandle)) Sleep(100);
        ExitProcess(0);
    }

    return true;
}

A different approach

Not useful in all situations, however I had one particular scenario where an application was controlling a child process completely. For communication & API interception, a DLL injection was required so a DLL is running in the actual child processes to report back to the parent.

Note: Now, this is not for the creativity award. Background here is: An application that needed to be wrapped, however that was a legacy application and could neither be modified, nor rewritten in a satisfying manner. We needed to keep this system running, while still intercepting outputs from it.

Also, the child process was poorly written, launching itself again immediately and then terminating, so the parent process is actually not our main application.

If you control the child processes and have an injected DLL anyway, you can extend this DLL by monitoring the parent process to check if it is running. If not, ExitProcess.

If you don't need a DLL in the child process, the other solutions are most likely far better.


You can transfer the parentProcessID through, for instance, a registry key that you use by your application already. Or you can pass a commandline, if this doesn't break the child process.

This has to run in its own thread. My DLL had two threads: This code here and the controlling & communication code.

DLL

bool IsProcessRunning(HANDLE hProcess)
{
    DWORD exitCode;
    GetExitCodeProcess(hProcess, &exitCode);
    return exitCode == STILL_ACTIVE;
}

bool WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH)
    {
        int parentProcessID = [...]
        
        HANDLE parentProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, parentProcessID);
        while (IsProcessRunning(parentHandle)) Sleep(100);
        ExitProcess(0);
    }

    return true;
}
半暖夏伤 2024-11-21 07:43:23

我想 DEBUG_PROCESSDEBUG_ONLY_THIS_PROCESS 会产生几乎偶然的副作用。 Windows 并不像类 Unix 系统那样在树中创建进程。

I suppose DEBUG_PROCESS or DEBUG_ONLY_THIS_PROCESS would do that as an almost-accidental side-effect. Windows doesn't create processes in a tree the way Unix-like systems do though.

醉态萌生 2024-11-21 07:43:23

不知道windows,但这可以在linux上运行:
prctl(PR_SET_PDEATHSIG, SIGHUP);

Dont know about windows, but this will work on linux:
prctl(PR_SET_PDEATHSIG, SIGHUP);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文