Search code examples
cwindowsprocesshandlejobs

Is it possible to manage JobObjects by name only, without keeping an open HANDLE?


With processes, I can do the following:

int pid;
::CreateProcess(NULL, cmd, ..., &pi);
pid = pi.dwProcessId;
::CloseHandle(pi.hProcess);
// then later...
HANDLE proc = ::OpenProcess(PROCESS_TERMINATE, FALSE, pid);
::TerminateProcess(proc, 1);
::CloseHandle(proc);

In this fashion, I can manage windows processes by pid only, which is nice for multi-platform code.

I would like to extend this pattern to job objects:

HANDLE job = ::CreateJobObject(NULL, name);
::AssignProcessToJobObject(job, proc); // proc from above
::CloseHandle(job);
// then later...
job = ::OpenJobObject(JOB_OBJECT_TERMINATE, FALSE, name);
::TerminateJobObject(job, 1);

To be clear, I am not in the job, but my child process is. MSDN says "The job is destroyed when its last handle has been closed and all associated processes have exited". In this case, my main process has closed his last handle, but the child is still running, and still has a handle. A call to IsProcessInJob(proc, NULL, &isit) confirms the job still exists. However, my call to open the job returns NULL, and the error code is ERROR_FILE_NOT_FOUND.

So the question: does my main process have any way to get a handle to the job after closing it?


Solution

  • Yes, it is possible. However, observe:

    // This example will fail to reopen and terminate the job.
    // Processes cmd and ping stay running
    STARTUPINFO si;
    memset(&si, 0, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    PROCESS_INFORMATION pi;
    memset(&pi, 0, sizeof(PROCESS_INFORMATION));
    
    // BEGIN BLOCK A
    ::CreateProcess(NULL, "cmd /c ping 127.0.0.1 -n 10 > nul",
        NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, ".", &si, &pi);
    HANDLE job = ::CeateJobObject(NULL, "myjob");
    ::AssignProcessToJobObject(job, pi.hProcess);
    // END BLOCK A
    
    ::ResumeThread(pi.hThread);
    ::CloseHandle(pi.hProcess);
    ::CloseHandle(pi.hThread);
    ::CloseHandle(job);
    HANDLE reopenJob = ::OpenJobObject(JOB_OBJECT_TERMINATE, FALSE, "myjob");
    if(reopenJob != NULL)
        ::TerminateJobObject(reopenJob, 1);
    

    The reopened job in this case is NULL, and the error code is ERROR_FILE_NOT_FOUND.

    The job must be created before the process, and be inherited, for the job to be reopenable.

    // NEW BLOCK A
    HANDLE job = ::CreateJobObject(NULL, "myjob");
    ::SetHandleInformation(job, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
    ::CreateProcess(NULL, "cmd /c ping 127.0.0.1 -n 10 > nul",
        NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, ".", &si, &pi);
    ::AssignProcessToJobObject(job, pi.hProcess);
    

    If your goal is to name the job using the PID, note that this creation order prevents such a convention. This still means that some piece of information must be tracked in addition to the PID in order to reopen the job.