Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bp::extend::handler in V2? #454

Open
db4 opened this issue Jan 18, 2025 · 3 comments
Open

bp::extend::handler in V2? #454

db4 opened this issue Jan 18, 2025 · 3 comments

Comments

@db4
Copy link

db4 commented Jan 18, 2025

For V1 I used the following trick to kill a child Linux process when its parent dies:

struct ExecHandler : bp::extend::handler
{
    template<typename Executor>
    void on_exec_setup (Executor &) const
    {
        (void)prctl(PR_SET_PDEATHSIG, SIGTERM);
    }
};
...
    bp::child p(
        ExecHandler(),
        ...
    );

Is there anything similar to bp::extend::handler in V2?

@klemens-morgenstern
Copy link
Collaborator

Similar code, just without the base-class:

struct ExecHandler 
{

    error_code on_exec_setup(posix::default_launcher & /*launcher*/, const filesystem::path & /* exe*/, const char * const * /* argv*/)
   {
              (void)prctl(PR_SET_PDEATHSIG, SIGTERM);
   }

};

process::v2::process p{..., ExecHandler{}};

@db4
Copy link
Author

db4 commented Jan 21, 2025

@klemens-morgenstern, thanks a lot! If you don't mind, I have a similar question regarding Windows. I used to achieve the same effect of killing a process when its parent dies with

            bp::group g;
            bp::child p(
                ...
                g,
                io_service
            );
#ifdef _WIN32
            namespace bpd = bp::detail;
            bpd::api::workaround::JOBOBJECT_EXTENDED_LIMIT_INFORMATION_ info;
            auto h = g.native_handle();
            if (!bpd::api::workaround::query_information_job_object(
                h,
                bpd::api::workaround::JobObjectExtendedLimitInformation_,
                static_cast<void*>(&info),
                sizeof(info),
                nullptr)) {
                bpd::throw_last_error("QueryInformationJobObject() failed");
            }
            info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
            if (!bpd::api::workaround::set_information_job_object(
                h,
                bpd::api::workaround::JobObjectExtendedLimitInformation_,
                static_cast<void*>(&info),
                sizeof(info))) {
                bpd::throw_last_error("SetInformationJobObject() failed");
            }
#endif

How to do this with V2? With bp::group no longer existing I don't see how to include a process into a group and get the group's handle.

@klemens-morgenstern
Copy link
Collaborator

I did not add the group to v2 because there are too many differences between the windows & linux implementation. And it's simple enough, you just got a bit of boilerplate re HANDLEs and error handling.

The whole api::workaround stuff is also moot, because the

You would need to use the job object directly:

#include <windows.h> 
#include <boost/process/windows/creation_flag.hpp>

auto h = ::CreateJobObjectW(nullptr, nullptr); // don't forget the CloseHandle


bp::process proc(..., boost::process::windows::create_breakaway_from_job);

AssignProcessToJobObject(h, proc.native_handle()); // error handling...

// now do with the job object `h` what you have above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants