1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
// Copyright Pit Kleyersburg <pitkley@googlemail.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified or distributed
// except according to those terms.
#![forbid(missing_docs, unsafe_code)]
//! # in-container
//!
//! `in-container` is a binary and a library that can be used to detect if you are running inside a
//! container. Executing the binary will by default return exit-code 0 if it was run inside a
//! container and exit-code 1 if it wasn't. The library can be included in an application of your
//! choice, allowing you to determine whether your application is running inside a container or not.
//!
//! (Please note that some of the detection mechanisms only work if `in-container` is executed in a
//! privileged context.)
//!
//! ## Supported operating systems/containerization solutions
//!
//! * FreeBSD
//! * [Jails](https://www.freebsd.org/doc/handbook/jails.html)
//! * Linux
//! * [Docker](https://docs.docker.com/engine/)
//! * [LXC](https://linuxcontainers.org/)
//! * [systemd-nspawn](https://www.freedesktop.org/software/systemd/man/systemd-nspawn.html)
//! * Windows
//! * [Docker](https://docs.docker.com/docker-for-windows/install/)
//!
//! If you are missing support for an operating system or container runtime, feel free to [open a
//! feature request](https://github.com/pitkley/in-container/issues/new) or
//! [open a pull request](https://github.com/pitkley/in-container/pull/compare).
//!
//! ## Usage as a library
//!
//! Add `in-container` as a dependency to your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! in-container = "^1"
//! ```
//!
//! You can then use `in_container::in_container()` which will return `true` if you are running
//! inside a container and `false` otherwise. In case you are interested in the container-runtime
//! that was detected, you can call `in_container::get_container_runtime()` instead, which will
//! return an `Option<ContainerRuntime>`. The `Option` is `None` when not running in a container,
//! otherwise it will contain the detected runtime.
//!
//! ## <a name="license"></a> License
//!
//! This project is licensed under either of
//!
//! * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
//! <https://www.apache.org/licenses/LICENSE-2.0>)
//! * MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/licenses/MIT>)
//!
//! at your option.
//!
//! ### <a name="license-contribution"></a> Contribution
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in
//! this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above,
//! without any additional terms or conditions.
use std::{fmt::Display, str::FromStr};
/// Returns `true` if called from inside a container, `false` otherwise.
pub fn in_container() -> bool {
get_container_runtime().is_some()
}
/// Optionally returns the detected [`ContainerRuntime`] if called from inside a container.
///
/// [`ContainerRuntime`]: enum.ContainerRuntime.html
pub fn get_container_runtime() -> Option<ContainerRuntime> {
#[cfg(target_os = "freebsd")]
return freebsd::get_container_runtime();
#[cfg(target_os = "linux")]
return linux::get_container_runtime();
#[cfg(target_os = "windows")]
return windows::get_container_runtime();
#[cfg(not(any(target_os = "freebsd", target_os = "linux", target_os = "windows")))]
return None;
}
/// The detected container runtime.
#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum ContainerRuntime {
/// Docker container runtime
Docker,
/// BSD jail
Jail,
/// Linux Containers
Lxc,
/// systemd-nspawn
SystemdNspawn,
/// The detected container runtime is unknown
Unknown(String),
}
impl Display for ContainerRuntime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ContainerRuntime::Docker => write!(f, "docker"),
ContainerRuntime::Jail => write!(f, "jail"),
ContainerRuntime::Lxc => write!(f, "lxc"),
ContainerRuntime::SystemdNspawn => write!(f, "systemd-nspawn"),
ContainerRuntime::Unknown(name) => write!(f, "unknown({})", name),
}
}
}
impl From<&str> for ContainerRuntime {
fn from(s: &str) -> Self {
match s {
"docker" => Self::Docker,
"jail" => Self::Jail,
"lxc" => Self::Lxc,
"systemd-nspawn" => Self::SystemdNspawn,
name => Self::Unknown(name.to_owned()),
}
}
}
impl FromStr for ContainerRuntime {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.into())
}
}
macro_rules! chain {
( $fn:path $(,)+ $( $tail:path $(,)* )* ) => {
$fn().or_else(|| chain!( $($tail , )* ))
};
( $fn:path ) => {
$fn()
};
() => { None};
}
#[cfg(target_os = "freebsd")]
mod freebsd;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn containerruntime_from_str() {
assert_eq!(ContainerRuntime::Docker, "docker".into());
assert_eq!(ContainerRuntime::Jail, "jail".into());
assert_eq!(ContainerRuntime::Lxc, "lxc".into());
assert_eq!(ContainerRuntime::SystemdNspawn, "systemd-nspawn".into());
assert_eq!(
ContainerRuntime::Unknown("garbage".to_owned()),
"garbage".into()
);
assert_eq!(ContainerRuntime::Unknown("".to_owned()), "".into());
}
}