This file is indexed.

/usr/lib/python3/dist-packages/os_win/utils/processutils.py is in python3-os-win 3.0.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# Copyright 2017 Cloudbase Solutions Srl
#
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import ctypes

from oslo_log import log as logging

from os_win.utils import win32utils
from os_win.utils.winapi import constants as w_const
from os_win.utils.winapi import libs as w_lib
from os_win.utils.winapi.libs import kernel32 as kernel32_struct

kernel32 = w_lib.get_shared_lib_handle(w_lib.KERNEL32)

LOG = logging.getLogger(__name__)


class ProcessUtils(object):
    def __init__(self):
        self._win32_utils = win32utils.Win32Utils()

    def _run_and_check_output(self, *args, **kwargs):
        kwargs.update(kernel32_lib_func=True)
        return self._win32_utils.run_and_check_output(*args, **kwargs)

    def create_job_object(self, name=None):
        """Create or open a job object.

        :param name: (Optional) the job name.
        :returns: a handle of the created job.
        """
        pname = None if name is None else ctypes.c_wchar_p(name)
        return self._run_and_check_output(kernel32.CreateJobObjectW,
                                          None,  # job security attributes
                                          pname,
                                          error_ret_vals=[None])

    def set_information_job_object(self, job_handle, job_object_info_class,
                                   job_object_info):
        self._run_and_check_output(kernel32.SetInformationJobObject,
                                   job_handle,
                                   job_object_info_class,
                                   ctypes.byref(job_object_info),
                                   ctypes.sizeof(job_object_info))

    def assign_process_to_job_object(self, job_handle, process_handle):
        self._run_and_check_output(kernel32.AssignProcessToJobObject,
                                   job_handle, process_handle)

    def open_process(self, pid, desired_access, inherit_handle=False):
        """Open an existing process."""
        return self._run_and_check_output(kernel32.OpenProcess,
                                          desired_access,
                                          inherit_handle,
                                          pid,
                                          error_ret_vals=[None])

    def kill_process_on_job_close(self, pid):
        """Associates a new job to the specified process.

        The process is immediately killed when the last job handle is closed.
        This mechanism can be useful when ensuring that child processes get
        killed along with a parent process.

        This method does not check if the specified process is already part of
        a job. Starting with WS 2012, nested jobs are available.

        :returns: the job handle, if a job was successfully created and
                  associated with the process, otherwise "None".
        """

        process_handle = None
        job_handle = None
        job_associated = False

        try:
            desired_process_access = (w_const.PROCESS_SET_QUOTA |
                                      w_const.PROCESS_TERMINATE)
            process_handle = self.open_process(pid, desired_process_access)
            job_handle = self.create_job_object()

            job_info = kernel32_struct.JOBOBJECT_EXTENDED_LIMIT_INFORMATION()
            job_info.BasicLimitInformation.LimitFlags = (
                w_const.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)
            job_info_class = w_const.JobObjectExtendedLimitInformation

            self.set_information_job_object(job_handle,
                                            job_info_class,
                                            job_info)

            self.assign_process_to_job_object(job_handle, process_handle)
            job_associated = True
        finally:
            if process_handle:
                self._win32_utils.close_handle(process_handle)

            if not job_associated and job_handle:
                # We have an unassociated job object. Closing the handle
                # will also destroy the job object.
                self._win32_utils.close_handle(job_handle)

        return job_handle

    def wait_for_multiple_processes(self, pids, wait_all=True,
                                    milliseconds=w_const.INFINITE):
        handles = []
        try:
            for pid in pids:
                handle = self.open_process(pid,
                                           desired_access=w_const.SYNCHRONIZE)
                handles.append(handle)

            return self._win32_utils.wait_for_multiple_objects(
                handles, wait_all, milliseconds)
        finally:
            for handle in handles:
                self._win32_utils.close_handle(handle)