OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / JobObject_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 //Internal
24 #include <MUtils/JobObject.h>
25
26 //Qt
27 #include <QProcess>
28
29 //Windows includes
30 #define NOMINMAX
31 #define WIN32_LEAN_AND_MEAN 1
32 #include <Windows.h>
33
34 //Utilities
35 #define PTR2HANDLE(X) reinterpret_cast<HANDLE>((X))
36 #define HANDLE2PTR(X) reinterpret_cast<uintptr_t>((X))
37
38 MUtils::JobObject::JobObject(void)
39 :
40         m_jobPtr(NULL)
41 {
42         const HANDLE jobObject = CreateJobObject(NULL, NULL);
43         if((jobObject != NULL) && (jobObject != INVALID_HANDLE_VALUE))
44         {
45                 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
46                 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
47                 jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
48                 if(SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)))
49                 {
50                         m_jobPtr = HANDLE2PTR(jobObject);
51                 }
52                 else
53                 {
54                         qWarning("Failed to set up job object limit information!");
55                         CloseHandle(jobObject);
56                 }
57         }
58         else
59         {
60                 qWarning("Failed to create the job object!");
61         }
62 }
63
64 MUtils::JobObject::~JobObject(void)
65 {
66         if(m_jobPtr)
67         {
68                 CloseHandle(PTR2HANDLE(m_jobPtr));
69                 m_jobPtr = NULL;
70         }
71 }
72
73 bool MUtils::JobObject::isObjectCreated(void)
74 {
75         return (bool) m_jobPtr;
76 }
77
78 bool MUtils::JobObject::addProcessToJob(const QProcess *const process)
79 {
80         if(!m_jobPtr)
81         {
82                 qWarning("Cannot assign process to job: No job bject available!");
83                 return false;
84         }
85
86         if(const Q_PID pid = process->pid())
87         {
88                 DWORD exitCode;
89                 if(!GetExitCodeProcess(pid->hProcess, &exitCode))
90                 {
91                         qWarning("Cannot assign process to job: Failed to query process status!");
92                         return false;
93                 }
94                 if(exitCode != STILL_ACTIVE)
95                 {
96                         qWarning("Cannot assign process to job: Process is not running anymore!");
97                         return false;
98                 }
99                 if(!AssignProcessToJobObject(PTR2HANDLE(m_jobPtr), pid->hProcess))
100                 {
101                         qWarning("Failed to assign process to job object!");
102                         return false;
103                 }
104                 return true;
105         }
106         else
107         {
108                 qWarning("Cannot assign process to job: Process handle not available!");
109                 return false;
110         }
111 }
112
113 bool MUtils::JobObject::terminateJob(const quint32 &exitCode)
114 {
115         if(m_jobPtr)
116         {
117                 if(TerminateJobObject(PTR2HANDLE(m_jobPtr), exitCode))
118                 {
119                         return true;
120                 }
121                 else
122                 {
123                         qWarning("Failed to terminate job object!");
124                         return false;
125                 }
126         }
127         else
128         {
129                 qWarning("Cannot assign process to job: No job bject available!");
130                 return false;
131         }
132 }