1//===-- HostProcessPosix.cpp ----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Host/Host.h"
10#include "lldb/Host/FileSystem.h"
11#include "lldb/Host/posix/HostProcessPosix.h"
12
13#include "llvm/ADT/STLExtras.h"
14
15#include <climits>
16#include <csignal>
17#include <unistd.h>
18
19using namespace lldb_private;
20
21static const int kInvalidPosixProcess = 0;
22
23HostProcessPosix::HostProcessPosix()
24    : HostNativeProcessBase(kInvalidPosixProcess) {}
25
26HostProcessPosix::HostProcessPosix(lldb::process_t process)
27    : HostNativeProcessBase(process) {}
28
29HostProcessPosix::~HostProcessPosix() = default;
30
31Status HostProcessPosix::Signal(int signo) const {
32  if (m_process == kInvalidPosixProcess) {
33    Status error;
34    error.SetErrorString("HostProcessPosix refers to an invalid process");
35    return error;
36  }
37
38  return HostProcessPosix::Signal(m_process, signo);
39}
40
41Status HostProcessPosix::Signal(lldb::process_t process, int signo) {
42  Status error;
43
44  if (-1 == ::kill(process, signo))
45    error.SetErrorToErrno();
46
47  return error;
48}
49
50Status HostProcessPosix::Terminate() { return Signal(SIGKILL); }
51
52lldb::pid_t HostProcessPosix::GetProcessId() const { return m_process; }
53
54bool HostProcessPosix::IsRunning() const {
55  if (m_process == kInvalidPosixProcess)
56    return false;
57
58  // Send this process the null signal.  If it succeeds the process is running.
59  Status error = Signal(0);
60  return error.Success();
61}
62
63llvm::Expected<HostThread> HostProcessPosix::StartMonitoring(
64    const Host::MonitorChildProcessCallback &callback) {
65  return Host::StartMonitoringChildProcess(callback, m_process);
66}
67