summaryrefslogtreecommitdiff
path: root/boost/process/detail/windows/job_workaround.hpp
blob: 574e4066098e15f2187ebca3142afb2b82ecac94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2016 Klemens D. Morgenstern
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_PROCESS_DETAIL_WINDOWS_JOB_WORKAROUND_HPP_
#define BOOST_PROCESS_DETAIL_WINDOWS_JOB_WORKAROUND_HPP_

#include <boost/winapi/config.hpp>
#include <boost/winapi/basic_types.hpp>
#include <boost/winapi/dll.hpp>

namespace boost { namespace process { namespace detail { namespace windows { namespace workaround {

//this import workaround is to keep it a header-only library. and enums cannot be imported from the winapi.

extern "C"
{

typedef enum _JOBOBJECTINFOCLASS_ {
      JobObjectBasicAccountingInformation_ = 1, JobObjectBasicLimitInformation_,
      JobObjectBasicProcessIdList_, JobObjectBasicUIRestrictions_,
      JobObjectSecurityLimitInformation_, JobObjectEndOfJobTimeInformation_,
      JobObjectAssociateCompletionPortInformation_, JobObjectBasicAndIoAccountingInformation_,
      JobObjectExtendedLimitInformation_, JobObjectJobSetInformation_,
      JobObjectGroupInformation_,
      JobObjectNotificationLimitInformation_,
      JobObjectLimitViolationInformation_,
      JobObjectGroupInformationEx_,
      JobObjectCpuRateControlInformation_,
      JobObjectCompletionFilter_,
      JobObjectCompletionCounter_,
      JobObjectReserved1Information_ = 18,
      JobObjectReserved2Information_,
      JobObjectReserved3Information_,
      JobObjectReserved4Information_,
      JobObjectReserved5Information_,
      JobObjectReserved6Information_,
      JobObjectReserved7Information_,
      JobObjectReserved8Information_,
      MaxJobObjectInfoClass_
    } JOBOBJECTINFOCLASS_;

typedef struct _JOBOBJECT_BASIC_LIMIT_INFORMATION_ {
  ::boost::winapi::LARGE_INTEGER_ PerProcessUserTimeLimit;
  ::boost::winapi::LARGE_INTEGER_ PerJobUserTimeLimit;
  ::boost::winapi::DWORD_         LimitFlags;
  ::boost::winapi::SIZE_T_        MinimumWorkingSetSize;
  ::boost::winapi::SIZE_T_        MaximumWorkingSetSize;
  ::boost::winapi::DWORD_         ActiveProcessLimit;
  ::boost::winapi::ULONG_PTR_     Affinity;
  ::boost::winapi::DWORD_         PriorityClass;
  ::boost::winapi::DWORD_         SchedulingClass;
} JOBOBJECT_BASIC_LIMIT_INFORMATION_;


typedef struct _IO_COUNTERS_ {
  ::boost::winapi::ULONGLONG_ ReadOperationCount;
  ::boost::winapi::ULONGLONG_ WriteOperationCount;
  ::boost::winapi::ULONGLONG_ OtherOperationCount;
  ::boost::winapi::ULONGLONG_ ReadTransferCount;
  ::boost::winapi::ULONGLONG_ WriteTransferCount;
  ::boost::winapi::ULONGLONG_ OtherTransferCount;
} IO_COUNTERS_;


typedef struct _JOBOBJECT_EXTENDED_LIMIT_INFORMATION_ {
  JOBOBJECT_BASIC_LIMIT_INFORMATION_ BasicLimitInformation;
  IO_COUNTERS_                       IoInfo;
  ::boost::winapi::SIZE_T_   ProcessMemoryLimit;
  ::boost::winapi::SIZE_T_   JobMemoryLimit;
  ::boost::winapi::SIZE_T_   PeakProcessMemoryUsed;
  ::boost::winapi::SIZE_T_   PeakJobMemoryUsed;
} JOBOBJECT_EXTENDED_LIMIT_INFORMATION_;


/*BOOL WINAPI QueryInformationJobObject(
  _In_opt_  HANDLE             hJob,
  _In_      JOBOBJECTINFOCLASS JobObjectInfoClass,
  _Out_     LPVOID             lpJobObjectInfo,
  _In_      DWORD              cbJobObjectInfoLength,
  _Out_opt_ LPDWORD            lpReturnLength
);
 */
typedef ::boost::winapi::BOOL_  (*query_information_job_object_p)(
        ::boost::winapi::HANDLE_,
        JOBOBJECTINFOCLASS_,
        void *,
        ::boost::winapi::DWORD_,
        ::boost::winapi::DWORD_ *);


inline ::boost::winapi::BOOL_ query_information_job_object(
        ::boost::winapi::HANDLE_ hJob,
        JOBOBJECTINFOCLASS_ JobObjectInfoClass,
        void * lpJobObjectInfo,
        ::boost::winapi::DWORD_ cbJobObjectInfoLength,
        ::boost::winapi::DWORD_ *lpReturnLength)
{
    static ::boost::winapi::HMODULE_ h = ::boost::winapi::get_module_handle(L"Kernel32.dll");
    static query_information_job_object_p f = reinterpret_cast<query_information_job_object_p>(::boost::winapi::get_proc_address(h, "QueryInformationJobObject"));

    return (*f)(hJob, JobObjectInfoClass, lpJobObjectInfo, cbJobObjectInfoLength, lpReturnLength);
}

/*BOOL WINAPI SetInformationJobObject(
  _In_ HANDLE             hJob,
  _In_ JOBOBJECTINFOCLASS JobObjectInfoClass,
  _In_ LPVOID             lpJobObjectInfo,
  _In_ DWORD              cbJobObjectInfoLength
);*/

typedef ::boost::winapi::BOOL_  (*set_information_job_object_p)(
        ::boost::winapi::HANDLE_,
        JOBOBJECTINFOCLASS_,
        void *,
        ::boost::winapi::DWORD_);

}

inline ::boost::winapi::BOOL_ set_information_job_object(
        ::boost::winapi::HANDLE_ hJob,
        JOBOBJECTINFOCLASS_ JobObjectInfoClass,
        void * lpJobObjectInfo,
        ::boost::winapi::DWORD_ cbJobObjectInfoLength)
{
    static ::boost::winapi::HMODULE_ h = ::boost::winapi::get_module_handle(L"Kernel32.dll");
    static set_information_job_object_p f = reinterpret_cast<set_information_job_object_p>(::boost::winapi::get_proc_address(h, "SetInformationJobObject"));

    return (*f)(hJob, JobObjectInfoClass, lpJobObjectInfo, cbJobObjectInfoLength);
}

constexpr static ::boost::winapi::DWORD_ JOB_OBJECT_LIMIT_BREAKAWAY_OK_ = 0x00000800;

}}}}}



#endif /* BOOST_PROCESS_DETAIL_WINDOWS_JOB_WORKAROUND_HPP_ */