1*8f0ba417SAndroid Build Coastguard Worker /* 2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project 3*8f0ba417SAndroid Build Coastguard Worker * 4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*8f0ba417SAndroid Build Coastguard Worker * 8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*8f0ba417SAndroid Build Coastguard Worker * 10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License. 15*8f0ba417SAndroid Build Coastguard Worker */ 16*8f0ba417SAndroid Build Coastguard Worker 17*8f0ba417SAndroid Build Coastguard Worker #pragma once 18*8f0ba417SAndroid Build Coastguard Worker 19*8f0ba417SAndroid Build Coastguard Worker #include <dirent.h> 20*8f0ba417SAndroid Build Coastguard Worker #include <sys/types.h> 21*8f0ba417SAndroid Build Coastguard Worker 22*8f0ba417SAndroid Build Coastguard Worker #include <iterator> 23*8f0ba417SAndroid Build Coastguard Worker #include <memory> 24*8f0ba417SAndroid Build Coastguard Worker #include <vector> 25*8f0ba417SAndroid Build Coastguard Worker 26*8f0ba417SAndroid Build Coastguard Worker namespace android { 27*8f0ba417SAndroid Build Coastguard Worker namespace base { 28*8f0ba417SAndroid Build Coastguard Worker 29*8f0ba417SAndroid Build Coastguard Worker class AllPids { 30*8f0ba417SAndroid Build Coastguard Worker class PidIterator { 31*8f0ba417SAndroid Build Coastguard Worker public: PidIterator(DIR * dir)32*8f0ba417SAndroid Build Coastguard Worker PidIterator(DIR* dir) : dir_(dir, closedir) { Increment(); } 33*8f0ba417SAndroid Build Coastguard Worker PidIterator& operator++() { 34*8f0ba417SAndroid Build Coastguard Worker Increment(); 35*8f0ba417SAndroid Build Coastguard Worker return *this; 36*8f0ba417SAndroid Build Coastguard Worker } 37*8f0ba417SAndroid Build Coastguard Worker bool operator==(const PidIterator& other) const { return pid_ == other.pid_; } 38*8f0ba417SAndroid Build Coastguard Worker bool operator!=(const PidIterator& other) const { return !(*this == other); } 39*8f0ba417SAndroid Build Coastguard Worker long operator*() const { return pid_; } 40*8f0ba417SAndroid Build Coastguard Worker // iterator traits 41*8f0ba417SAndroid Build Coastguard Worker using difference_type = pid_t; 42*8f0ba417SAndroid Build Coastguard Worker using value_type = pid_t; 43*8f0ba417SAndroid Build Coastguard Worker using pointer = const pid_t*; 44*8f0ba417SAndroid Build Coastguard Worker using reference = const pid_t&; 45*8f0ba417SAndroid Build Coastguard Worker using iterator_category = std::input_iterator_tag; 46*8f0ba417SAndroid Build Coastguard Worker 47*8f0ba417SAndroid Build Coastguard Worker private: 48*8f0ba417SAndroid Build Coastguard Worker void Increment(); 49*8f0ba417SAndroid Build Coastguard Worker 50*8f0ba417SAndroid Build Coastguard Worker pid_t pid_ = -1; 51*8f0ba417SAndroid Build Coastguard Worker std::unique_ptr<DIR, decltype(&closedir)> dir_; 52*8f0ba417SAndroid Build Coastguard Worker }; 53*8f0ba417SAndroid Build Coastguard Worker 54*8f0ba417SAndroid Build Coastguard Worker public: begin()55*8f0ba417SAndroid Build Coastguard Worker PidIterator begin() { return opendir("/proc"); } end()56*8f0ba417SAndroid Build Coastguard Worker PidIterator end() { return nullptr; } 57*8f0ba417SAndroid Build Coastguard Worker }; 58*8f0ba417SAndroid Build Coastguard Worker 59*8f0ba417SAndroid Build Coastguard Worker } // namespace base 60*8f0ba417SAndroid Build Coastguard Worker } // namespace android 61