049e7b199c36b2936ca68e18a10de29554ff5b2a
[iec.git] / src / type3_AndroidCloud / anbox-master / external / process-cpp-minimal / src / core / posix / process.cpp
1 /*
2  * Copyright © 2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18
19 #include <core/posix/process.h>
20
21 #include <core/posix/signal.h>
22
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include <iostream>
27
28 namespace core
29 {
30 namespace posix
31 {
32
33 struct Process::Private
34 {
35     pid_t pid;
36 };
37
38 Process Process::invalid()
39 {
40     static const pid_t invalid_pid = 0;
41     Process p(invalid_pid);
42     p.d->pid = -1;
43
44     return p;
45 }
46
47 Process::Process(pid_t pid)
48     : Signalable(pid),
49       d(new Private{pid})
50 {
51     if (pid < 0)
52         throw std::runtime_error("Cannot construct instance for invalid pid.");
53 }
54
55 Process::~Process() noexcept
56 {
57 }
58
59 pid_t Process::pid() const
60 {
61     return d->pid;
62 }
63
64 ProcessGroup Process::process_group_or_throw() const
65 {
66     pid_t pgid = ::getpgid(pid());
67
68     if (pgid == -1)
69         throw std::system_error(errno, std::system_category());
70
71     return ProcessGroup(pgid);
72 }
73
74 ProcessGroup Process::process_group(std::error_code& se) const noexcept(true)
75 {
76     pid_t pgid = ::getpgid(pid());
77
78     if (pgid == -1)
79     {
80         se = std::error_code(errno, std::system_category());
81     }
82
83     return ProcessGroup(pgid);
84 }
85 }
86 }