parity_daemonize/
error.rs

1// Copyright 2015-2018 Parity Technologies (UK) Ltd.
2// This file is part of Parity.
3
4// Parity is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity.  If not, see <http://www.gnu.org/licenses/>.
16use failure::{Backtrace, Context, Fail};
17use std::fmt::{self, Display};
18use std::io;
19
20/// Error type
21#[derive(Debug)]
22pub struct Error {
23	inner: Context<ErrorKind>,
24}
25
26/// Possible errors encountered while creating daemon
27#[derive(Fail, Debug)]
28pub enum ErrorKind {
29	/// Call to pipe failed
30	#[fail(display = "Call to pipe failed: {}", _0)]
31	Pipe(io::Error),
32
33	/// Couldn't fork daemon process
34	#[fail(display = "Couldn't fork daemon process: {}", _0)]
35	Fork(io::Error),
36
37	/// Couldn't redirect stdio streams
38	#[fail(display = "Couldn't redirect stdio streams: {}", _0)]
39	Dup2(io::Error),
40
41	/// Unable to create new session
42	#[fail(display = "Unable to create new session: {}", _0)]
43	DetachSession(io::Error),
44
45	/// Unable to change directory
46	#[fail(display = "Unable to change directory")]
47	ChangeDirectory,
48
49	/// pid_file option contains NUL
50	#[fail(display = "pid_file option contains NUL")]
51	PathContainsNul,
52
53	/// Unable to open pid file
54	#[fail(display = "Unable to open pid file, {}", _0)]
55	OpenPidfile(io::Error),
56
57	/// Unable to write self pid to pid file
58	#[fail(display = "Unable to write self pid to pid file {}", _0)]
59	WritePid(io::Error),
60
61	/// failed to register pipe fd's with mio
62	#[fail(display = "Unable to register pipe with mio: {}", _0)]
63	RegisterationError(io::Error),
64
65	/// splice returned an error
66	#[fail(display = "Unable to splice streams: {}", _0)]
67	SpliceError(io::Error),
68
69	/// couldn't get the pending datasize from ioctl
70	#[fail(display = "Unable to fetch pending datasize from ioctl: {}", _0)]
71	Ioctl(io::Error),
72
73	/// fnctl operations failed
74	#[fail(display = "{}", _0)]
75	Fnctl(io::Error),
76
77	/// attempted to daemonize on an unsupported platform
78	#[fail(display = "Daemonize on the current platform is not supported")]
79	UnsupportedPlatform
80}
81
82impl Fail for Error {
83	fn cause(&self) -> Option<&Fail> {
84		self.inner.cause()
85	}
86
87	fn backtrace(&self) -> Option<&Backtrace> {
88		self.inner.backtrace()
89	}
90}
91
92impl Display for Error {
93	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94		Display::fmt(&self.inner, f)
95	}
96}
97
98impl Error {
99	/// extract the error kind
100	pub fn kind(&self) -> &ErrorKind {
101		self.inner.get_context()
102	}
103}
104
105impl From<ErrorKind> for Error {
106	fn from(kind: ErrorKind) -> Error {
107		Error {
108			inner: Context::new(kind),
109		}
110	}
111}
112
113impl From<Context<ErrorKind>> for Error {
114	fn from(inner: Context<ErrorKind>) -> Error {
115		Error { inner }
116	}
117}