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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*
* Copyright (C) 2020-2023 Fanout, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pub mod app;
pub mod arena;
pub mod buffer;
pub mod channel;
pub mod client;
pub mod connection;
pub mod event;
pub mod executor;
pub mod future;
pub mod http1;
pub mod list;
pub mod listener;
pub mod net;
pub mod pool;
pub mod reactor;
pub mod resolver;
pub mod server;
pub mod shuffle;
pub mod timer;
pub mod tls;
pub mod tnetstring;
pub mod track;
pub mod waker;
pub mod websocket;
pub mod zhttppacket;
pub mod zhttpsocket;
pub mod zmq;
use app::Config;
use log::info;
use std::error::Error;
use std::ffi::CString;
use std::future::Future;
use std::io;
use std::mem;
use std::ops::Deref;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::pin::Pin;
use std::ptr;
use std::task::{Context, Poll};
pub struct Defer<T: FnOnce()> {
f: Option<T>,
}
impl<T: FnOnce()> Defer<T> {
pub fn new(f: T) -> Self {
Self { f: Some(f) }
}
}
impl<T: FnOnce()> Drop for Defer<T> {
fn drop(&mut self) {
let f = self.f.take().unwrap();
f();
}
}
pub struct Pinner<'a, T> {
pub unsafe_pointer: &'a mut T,
}
impl<'a, T> Pinner<'a, T> {
pub fn as_mut(&mut self) -> Pin<&mut T> {
// SAFETY: as long as Pinner is only ever constructed via the pin!()
// macro and the unsafe_pointer field is never directly accessed,
// then the value is safe to pin here. this is because the macro
// ensures the input is turned into a borrowed anonymous temporary,
// preventing any further access to the original value after Pinner
// is constructed, and Pinner has no methods that enable moving out
// of the reference. the word "unsafe" is used in the field name to
// discourage direct access. this is the best we can do, since the
// field must be public for the macro to work.
unsafe { Pin::new_unchecked(self.unsafe_pointer) }
}
pub fn set(&mut self, value: T) {
self.as_mut().set(value)
}
}
impl<'a, T> Pinner<'a, Option<T>> {
pub fn as_pin_mut(&mut self) -> Option<Pin<&mut T>> {
self.as_mut().as_pin_mut()
}
}
impl<'a, T> Deref for Pinner<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.unsafe_pointer
}
}
impl<'a, T> Future for Pinner<'a, T>
where
T: Future,
{
type Output = T::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
T::poll(Pin::into_inner(self).as_mut(), cx)
}
}
// NOTE: replace with std::pin::pin someday
#[macro_export]
macro_rules! pin {
($x:expr) => {
$crate::Pinner {
unsafe_pointer: &mut { $x },
}
};
}
fn try_with_increasing_buffer<T, U>(starting_size: usize, f: T) -> Result<U, io::Error>
where
T: Fn(&mut [u8]) -> Result<U, io::Error>,
{
let mut buf = Vec::new();
buf.resize(starting_size, 0);
loop {
match f(&mut buf) {
Ok(v) => return Ok(v),
Err(e) if e.raw_os_error() == Some(libc::ERANGE) => buf.resize(buf.len() * 2, 0),
Err(e) => return Err(e),
}
}
}
fn get_user_uid(name: &str) -> Result<libc::gid_t, io::Error> {
let name = CString::new(name).unwrap();
try_with_increasing_buffer(1024, |buf| unsafe {
let mut pwd = mem::MaybeUninit::uninit();
let mut passwd = ptr::null_mut();
if libc::getpwnam_r(
name.as_ptr(),
pwd.as_mut_ptr(),
buf.as_mut_ptr() as *mut libc::c_char,
buf.len(),
&mut passwd,
) != 0
{
return Err(io::Error::last_os_error());
}
let passwd = match passwd.as_ref() {
Some(r) => r,
None => return Err(io::Error::from(io::ErrorKind::NotFound)),
};
Ok(passwd.pw_uid)
})
}
fn get_group_gid(name: &str) -> Result<libc::gid_t, io::Error> {
let name = CString::new(name).unwrap();
try_with_increasing_buffer(1024, |buf| unsafe {
let mut grp = mem::MaybeUninit::uninit();
let mut group = ptr::null_mut();
if libc::getgrnam_r(
name.as_ptr(),
grp.as_mut_ptr(),
buf.as_mut_ptr() as *mut libc::c_char,
buf.len(),
&mut group,
) != 0
{
return Err(io::Error::last_os_error());
}
let group = match group.as_ref() {
Some(r) => r,
None => return Err(io::Error::from(io::ErrorKind::NotFound)),
};
Ok(group.gr_gid)
})
}
pub fn set_user(path: &Path, user: &str) -> Result<(), io::Error> {
let uid = get_user_uid(user)?;
unsafe {
let path = CString::new(path.as_os_str().as_bytes()).unwrap();
if libc::chown(path.as_ptr(), uid, u32::MAX) != 0 {
return Err(io::Error::last_os_error());
}
}
Ok(())
}
pub fn set_group(path: &Path, group: &str) -> Result<(), io::Error> {
let gid = get_group_gid(group)?;
unsafe {
let path = CString::new(path.as_os_str().as_bytes()).unwrap();
if libc::chown(path.as_ptr(), u32::MAX, gid) != 0 {
return Err(io::Error::last_os_error());
}
}
Ok(())
}
pub fn can_move_mio_sockets_between_threads() -> bool {
// on unix platforms, mio always uses epoll or kqueue, which support
// this. mio makes no guarantee about supporting this on non-unix
// platforms
cfg!(unix)
}
pub fn run(config: &Config) -> Result<(), Box<dyn Error>> {
info!("starting...");
{
let a = match app::App::new(config) {
Ok(a) => a,
Err(e) => {
return Err(e.into());
}
};
info!("started");
a.wait_for_term();
info!("stopping...");
}
info!("stopped");
Ok(())
}
|