process-cpp 3.0.0
A simple convenience library for handling processes in C++11.
process.cpp
Go to the documentation of this file.
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
28namespace core
29{
30namespace posix
31{
32
34{
35 pid_t pid;
36};
37
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
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
56{
57}
58
59pid_t Process::pid() const
60{
61 return d->pid;
62}
63
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
74ProcessGroup 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}
The ProcessGroup class models a signalable group of process.
Definition: process_group.h:44
The Process class models a process and possible operations on it.
Definition: process.h:45
Process(pid_t pid)
Creates a process instance wrapping an existing process.
Definition: process.cpp:47
virtual ProcessGroup process_group(std::error_code &se) const noexcept(true)
Queries the id of the process group this process belongs to.
Definition: process.cpp:74
virtual ProcessGroup process_group_or_throw() const
Queries the id of the process group this process belongs to.
Definition: process.cpp:64
static Process invalid()
Returns an invalid instance for testing purposes.
Definition: process.cpp:38
virtual ~Process() noexcept
Frees resources associated with the process.
Definition: process.cpp:55
virtual pid_t pid() const
Query the pid of the process.
Definition: process.cpp:59
The Signalable class abstracts the ability of an entity to be delivered a posix signal.
Definition: signalable.h:36