├── .drone.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── rust-toolchain └── src └── main.rs /.drone.yml: -------------------------------------------------------------------------------- 1 | pipeline: 2 | build: 3 | image: mesalocklinux/build-mesalock-linux 4 | commands: 5 | - rustup show 6 | - cargo build 7 | notify: 8 | image: drillster/drone-email 9 | secrets: [ plugin_host, plugin_username, plugin_password, plugin_from ] 10 | when: 11 | status: [ changed, failure ] 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "bitflags" 3 | version = "0.9.1" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | 6 | [[package]] 7 | name = "cfg-if" 8 | version = "0.1.2" 9 | source = "registry+https://github.com/rust-lang/crates.io-index" 10 | 11 | [[package]] 12 | name = "libc" 13 | version = "0.2.33" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | 16 | [[package]] 17 | name = "minit" 18 | version = "0.1.0" 19 | dependencies = [ 20 | "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", 21 | "nix 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", 22 | ] 23 | 24 | [[package]] 25 | name = "nix" 26 | version = "0.9.0" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | dependencies = [ 29 | "bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", 30 | "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 31 | "libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", 32 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 33 | ] 34 | 35 | [[package]] 36 | name = "void" 37 | version = "1.0.2" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | 40 | [metadata] 41 | "checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" 42 | "checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" 43 | "checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2" 44 | "checksum nix 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a2c5afeb0198ec7be8569d666644b574345aad2e95a53baf3a532da3e0f3fb32" 45 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 46 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minit" 3 | version = "0.1.0" 4 | authors = ["Mingshen Sun "] 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | nix = "0.9.0" 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, The MesaLock Linux Project Contributors 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # init for MesaLock Linux 2 | 3 | [![Build Status](https://ci.mesalock-linux.org/api/badges/mesalock-linux/minit/status.svg?branch=master)](https://ci.mesalock-linux.org/mesalock-linux/minit) 4 | 5 | An init utility purely written in Rust. `minit` provides basic function to 6 | bootstrap MesaLock Linux. `minit` will mount essential device files, setup 7 | signal handler properly and then invoke `mgetty`. 8 | 9 | ## Maintainer 10 | 11 | - Mingshen Sun `` [@mssun](https://github.com/mssun) 12 | 13 | ## License 14 | 15 | Minit is provided under the BSD license. 16 | -------------------------------------------------------------------------------- /rust-toolchain: -------------------------------------------------------------------------------- 1 | stable 2 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2017, The MesaLock Linux Contributors 2 | // All rights reserved. 3 | // 4 | // This work is licensed under the terms of the BSD 3-Clause License. 5 | // For a copy, see the LICENSE file. 6 | 7 | extern crate libc; 8 | extern crate nix; 9 | use nix::unistd; 10 | use nix::mount; 11 | 12 | use libc::{waitpid, sigprocmask, sigfillset, sigset_t, signal}; 13 | use std::os::unix::process::CommandExt; 14 | use std::process::Command; 15 | use std::mem; 16 | use std::ptr; 17 | use std::ffi::CString; 18 | 19 | fn run(line: &str) { 20 | println!("[+] init: run {}", line); 21 | let mut args = line.split(' ').map(|arg| {arg.to_string()}); 22 | 23 | if let Some(cmd) = args.next() { 24 | match cmd.as_str() { 25 | _ => { 26 | let mut command = Command::new(cmd); 27 | for arg in args { 28 | command.arg(arg); 29 | } 30 | 31 | match command.before_exec(|| { 32 | unsafe { reset_sighandlers_and_unblock_sigs() } 33 | // TODO: Open the new terminal device 34 | Ok(()) 35 | }).spawn() { 36 | Ok(mut child) => match child.wait() { 37 | Ok(_status) => { 38 | println!("[+] init: {} exit", line); 39 | unsafe { sigprocmask_allsigs(libc::SIG_UNBLOCK); } 40 | }, 41 | Err(err) => println!("[-] init: failed to wait: {}", err) 42 | }, 43 | Err(err) => println!("[-] init: failed to execute: {}", err) 44 | } 45 | } 46 | } 47 | 48 | } 49 | } 50 | 51 | unsafe fn sigprocmask_allsigs(how: libc::c_int) { 52 | let mut sigset = mem::uninitialized::(); 53 | sigfillset(&mut sigset as *mut sigset_t); 54 | sigprocmask(how, &sigset as *const sigset_t, ptr::null_mut() as *mut sigset_t); 55 | } 56 | 57 | 58 | unsafe fn reset_sighandlers_and_unblock_sigs() { 59 | signal(libc::SIGUSR1, libc::SIG_DFL); 60 | signal(libc::SIGUSR2, libc::SIG_DFL); 61 | signal(libc::SIGTERM, libc::SIG_DFL); 62 | signal(libc::SIGQUIT, libc::SIG_DFL); 63 | signal(libc::SIGINT, libc::SIG_DFL); 64 | signal(libc::SIGHUP, libc::SIG_DFL); 65 | signal(libc::SIGTSTP, libc::SIG_DFL); 66 | signal(libc::SIGSTOP, libc::SIG_DFL); 67 | sigprocmask_allsigs(libc::SIG_UNBLOCK); 68 | } 69 | 70 | fn main() { 71 | println!("init"); 72 | unistd::setsid().expect("setsid failed"); 73 | unsafe { 74 | libc::putenv(CString::new("HOME=/").unwrap().into_raw()); 75 | libc::putenv(CString::new("PATH=/sbin:/bin:/usr/sbin:/usr/bin").unwrap().into_raw()); 76 | libc::putenv(CString::new("SHELL=/bin/sh").unwrap().into_raw()); 77 | } 78 | 79 | // TODO: setup signal handler 80 | 81 | // mount -n -t proc proc /proc 82 | let proc_mount_flags = mount::MS_NOSUID | mount::MS_NODEV | mount::MS_NOEXEC | mount::MS_RELATIME; 83 | let _ = mount::mount(Some("proc"), "/proc", Some("proc"), proc_mount_flags, Some("mode=0555")).expect("mount proc failed"); 84 | 85 | // mount -n -t devtmpfs devtmpfs /dev 86 | let dev_mount_flags = mount::MS_NOSUID | mount::MS_RELATIME; 87 | let _ = mount::mount(Some("dev"), "/dev", Some("devtmpfs"), dev_mount_flags, Some("mode=0755")).expect("mount tmp failed"); 88 | 89 | // mount -n -t sysfs sysfs /sys 90 | let sys_mount_flags = mount::MS_NOSUID | mount::MS_NODEV | mount::MS_NOEXEC | mount::MS_RELATIME; 91 | let _ = mount::mount(Some("sysfs"), "/sys", Some("sysfs"), sys_mount_flags, Some("mode=0555")).expect("mount sys failed"); 92 | 93 | run("mknod -m 600 /dev/console c 5 1"); 94 | run("mknod -m 620 /dev/tty1 c 4 1"); 95 | run("mknod -m 666 /dev/tty c 5 0"); 96 | run("mknod -m 666 /dev/null c 1 3"); 97 | run("mknod -m 660 /dev/kmsg c 1 11"); 98 | 99 | run("/bin/mgetty"); 100 | loop { 101 | let mut status = 0; 102 | unsafe { 103 | waitpid(0, &mut status, 0); 104 | } 105 | } 106 | } 107 | --------------------------------------------------------------------------------