Code Monkey home page Code Monkey logo

ptyme's Introduction

ptyme - daemonize but keep terminal alive

Simple demonstration of Linux PTY capabilities. PTY is an ancient yet ubiquitous technology. It powers SSH, docker, kubernetes, etc.

This project contains trivial implementation of attach/exec (or kubectl attach/exec) feature.

Server:
    +-----------+                                      +----------------+
    |  shim.c   | <-- [pty] -- read/write -- [pts] --> |   ping ya.ru   |
    +-----------+                                      +----------------+
          |
          |
      [network]
          |
Client:   |
    +-----------+
    | attach.go | <-- [terminal in RAW mode] --> user via xterm (iterm2, etc).
    +-----------+

The idea is simple: we want to start an arbitrary executable in background (i.e. as a daemon), but keep its STDIN/STDOUT bound to a controlling terminal to be able to connect to it later on. For that we need a tiny piece of software called a shim (shim.c). The shim creates a pseudoterminal and fork/exec-s a given executable binding its standard streams to the slave side of the pseudoterminal pair. At the same time the parent process keeps reading and writing the master end of the pair. The parent process also starts listening on TCP port. Each byte read from an incomming connection is then forwarded to a master side of the terminal. And other way around - each byte read from the master end of the pseudoterminal has to be written to each incomming connection (i.e. broadcasted).

Simplistic client can be found in attach.go. Since the controlling (i.e. escape sequence handling, etc) of user interaction is done by the pseudoterminal on the server side, the client sets its controlling terminal to RAW mode and then just blindly forwards bytes from its STDIN to a socket connection and from the connection to its STDOUT.

Try it out:

$ make shim
$ ./build/shim 43210 /usr/bin/ping ya.ru

$ make attach SOCK=localhost:43210

> 64 bytes from ya.ru (87.250.250.242): icmp_seq=154 ttl=63 time=36.4 ms
> 64 bytes from ya.ru (87.250.250.242): icmp_seq=155 ttl=63 time=38.4 ms
> 64 bytes from ya.ru (87.250.250.242): icmp_seq=156 ttl=63 time=41.7 ms
> ^C
> --- ya.ru ping statistics ---
> 187 packets transmitted, 187 received, 0% packet loss, time 187058ms
> rtt min/avg/max/mdev = 35.013/39.285/93.060/6.794 ms

ptyme's People

Contributors

fsmiamoto avatar iximiuz 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

Watchers

 avatar  avatar  avatar  avatar

ptyme's Issues

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.