Giter Site home page Giter Site logo

msantos / prx Goto Github PK

View Code? Open in Web Editor NEW
36.0 5.0 6.0 618 KB

an Erlang library for interacting with Unix processes

License: ISC License

Erlang 99.72% Shell 0.28%
exec fork linux-namespaces signal supervisor system-programming capsicum pledge seccomp prctl

prx's Introduction

prx

Package Version Hex Docs

An Erlang library for Unix process management and system programming tasks.

prx provides:

  • a safe, beam-friendly interface to system calls and other POSIX operations

  • simple, reliable OS process management by mapping Erlang processes to a hierarchy of system processes

  • an interface for privilege separation operations to restrict processes

  • operations to isolate processes like containers and jails

Build

$ rebar3 compile

Quick Start

prx has two basic operations: fork and exec.

% Spawn a new system process
{ok, Task} = prx:fork(),

% And a child of the process
{ok, Child} = prx:fork(Task).

After fork()'ing, other calls can be made. For example:

UID = prx:getuid(Task),
PID = prx:getpid(Child).

Calling exec() causes the process I/O to be treated as streams of data:

ok = prx:execvp(Child, ["/bin/cat", "-n"]),
prx:stdin(Child, "test\n"),
receive
    {stdout,Child,Stdout} ->
        Stdout
end.

Usage

  • fork and exec /bin/cat

      {ok, Task} = prx:fork(),
      ok = prx:execvp(Task, ["/bin/cat", "-n"],
      prx:stdin(Task, "test\n"),
      receive {stdout, Task, _} = Out -> Out end.
    
  • creating a pipeline of child processes

    prx processes can fork child prx processes:

beam
  |-erl_child_setup
  |   `-prx
  |       `-prx

After calling exec, the process tree looks like:

beam
  |-erl_child_setup
  |   `-prx
  |       `-cat
    {ok, Task} = prx:fork(),
    {ok, Child} = prx:fork(Task),
    OSPid = prx:getpid(Child),
    ok = prx:execvp(Child, ["/bin/cat", "-n"],
    prx:stdin(Child, "test\n"),
    receive {stdout, Child, _} = Out -> Out end.
  • running cat within a containerized namespace

      application:set_env(prx, options, [{exec, "sudo -n"}]),
      {ok, Task} = prx:fork(),
      {ok, Child} = prx:clone(Task, [clone_newnet, clone_newpid, clone_newipc,
          clone_newuts, clone_newns]),
      OSPid = prx:getpid(Child),
      ok = prx:execvp(Child, ["/bin/cat", "-n"],
      prx:stdin(Child, "test\n"),
      receive {stdout, Child, _} = Out -> Out end.
    

Documentation

https://hexdocs.pm/prx/

See also: alcove

prx's People

Contributors

msantos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

prx's Issues

Can I set beam's own proctitle with this library?

Hello :)

I read the docs but didn't understand if I can set current process title. Sorry, I'm new to Erlang.

We run Erlang 19 and 21 on CentOS 6 and 7 and I would like a simple way to set nice OS process titles. Right now they are just very long beam.smp invokations with a lot of arguments :)

Thanks!

Fail to build

I want to give a try to prx, it seems very interesting. Unfortunately I run into the following error during the build:

[..]/deps/alcove/c_src/sys/clone.c:21:9: note: #pragma message: Support for namespaces using clone(2) enabled
 #pragma message "Support for namespaces using clone(2) enabled"
         ^~~~~~~
[..]/deps/alcove/c_src/sys/setns.c:30:12: error: static declaration of ‘setns’ follows non-static declaration
 static int setns(int fd, int nstype);
            ^~~~~
In file included from /usr/include/sched.h:43,
                 from [..]/deps/alcove/c_src/sys/setns.c:17:
/usr/include/bits/sched.h:94:12: note: previous declaration of ‘setns’ was here
 extern int setns (int __fd, int __nstype) __THROW;
            ^~~~~
make[3]: *** [Makefile:59: [..]/deps/alcove/c_src/../priv/alcove] Error 1
make[3]: Leaving directory '[..]/deps/alcove/c_src'
make[2]: *** [Makefile:37: pre-app] Error 2
make[2]: Leaving directory '[..]/deps/alcove'
make[1]: *** [[..]/erlang.mk:4339: deps] Error 2
make[1]: Leaving directory '[..]/deps/prx'
make: *** [erlang.mk:4339: deps] Error 2

I am running Fedora 28 and OTP-21.0-rc1. I am using erlang.mk to manage my project.
Am i missing any dependency or anything?

Send Stdout after each carriage retrun

I execute a c program with prx which log some useful outputs, I want to receive my outputs line by line (not accumulated in a buffer) after each carriage return .
How can I do it ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.