Apache Mesos
exists.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_POSIX_EXISTS_HPP__
14 #define __STOUT_OS_POSIX_EXISTS_HPP__
15 
16 #include <errno.h>
17 #include <signal.h>
18 
19 #include <sys/stat.h>
20 
21 #include <string>
22 
23 namespace os {
24 
25 
26 inline bool exists(const std::string& path)
27 {
28  struct stat s;
29 
30  if (::lstat(path.c_str(), &s) < 0) {
31  return false;
32  }
33  return true;
34 }
35 
36 
37 // Determine if the process identified by pid exists.
38 // NOTE: Zombie processes have a pid and therefore exist. See os::process(pid)
39 // to get details of a process.
40 inline bool exists(pid_t pid)
41 {
42  // The special signal 0 is used to check if the process exists; see kill(2).
43  // If the current user does not have permission to signal pid, but it does
44  // exist, then ::kill will return -1 and set errno == EPERM.
45  if (::kill(pid, 0) == 0 || errno == EPERM) {
46  return true;
47  }
48 
49  return false;
50 }
51 
52 
53 } // namespace os {
54 
55 #endif // __STOUT_OS_POSIX_EXISTS_HPP__
Definition: path.hpp:29
bool exists(const std::string &path)
Definition: exists.hpp:26
Definition: posix_signalhandler.hpp:23
DWORD pid_t
Definition: windows.hpp:181
Try< hashmap< std::string, uint64_t > > stat(const std::string &hierarchy, const std::string &cgroup, const std::string &file)
int kill(pid_t pid, int sig)
Definition: kill.hpp:21