Giter Site home page Giter Site logo

sockproc's Introduction

Introduction

Sockproc daemon is a simple server for executing shell commands or processes. It can be useful in the situations, where a typical system call to launch a child process and wait for its completion is unacceptable, due to its blocking nature. Instead, a socket can be opened to sockproc, a command written to it, and then once child process completes, its exit code, output stream data and error stream data can be read back from the socket.

Examples

With UNIX domain socket

Launch sockproc on a UNIX domain socket:

$ ./sockproc /tmp/shell.sock

On Mac telnet works with both tcp sockets and unix-domain sockets, but on most Linux distributions, the telnet command is not as versatile. So we can employ a socat utility instead, using "crlf" flag to enforce the '\r\n' line-endings for standard input.

Connect to socket and type in a command line to execute, followed by a line that contains the number 0:

$ socat - /tmp/shell.sock,crlf
uname -a
0
status:0
98
Linux a569cf4d3a74 4.9.27-moby #1 SMP Thu May 11 04:01:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
0

With TCP socket

Launch sockproc on a TCP socket:

$ ./sockproc 13000

Connect to socket and type in a command line to execute, followed by a line that contains the number 0:

$ telnet 127.0.0.1 13000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
uname -a
0
status:0
98
Linux a569cf4d3a74 4.9.27-moby #1 SMP Thu May 11 04:01:18 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
0
Connection closed by foreign host.

Execute a bad command:

$ telnet 127.0.0.1 13000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
foobar
0
status:32512
0
37
/bin/bash: foobar: command not found
Connection closed by foreign host.

Wire protocol

The protocol is very simple, similar somewhat to HTTP:

Request format:

<command-line>\r\n
<stdin-byte-count>\r\n
<stdin-data>

The <command-line> length cannot exceed 2040 characters.

Response format:

status:<process-exit-code>\r\n
<stdout-byte-count>\r\n
<stdout-data>
<stderr-byte-count>\r\n
<stderr-data>

License

The MIT License (MIT)

sockproc's People

Contributors

gui avatar hongliang5316 avatar irr avatar juce avatar wuhao5 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sockproc's Issues

sockproc process is still running after failed tests

make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/temp/rcv-nginx/.build/sockproc/build/sockproc-master'
make: *** [/root/temp/rcv-nginx//.build/sockproc/build/sockproc-master/sockproc] Error 2
[root@rnd01-k01-jnk01 rcv-nginx]# netstat -lptn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      974/sshd            
tcp        0      0 127.0.0.1:12345         0.0.0.0:*               LISTEN      11583/./sockproc    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1544/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      3581/zabbix_agentd  
tcp        0      0 0.0.0.0:514             0.0.0.0:*               LISTEN      731/rsyslogd        
tcp        0      0 127.0.0.1:199           0.0.0.0:*               LISTEN      970/snmpd           
tcp        0      0 0.0.0.0:9001            0.0.0.0:*               LISTEN      1185/python         
tcp6       0      0 :::10050                :::*                    LISTEN      3581/zabbix_agentd  

This could prevent further builds from succeeding.
To avoid this, you should probably wrap test.sh script and kill pid from this wrapper.

chunking

Have you considered reinventing the sockproc protocol to serve data in chunks? Say for example
for generating an archive dynamically, seems to me you need to allocate the size of the output, then
write everything to the socket?

Cannot build on CentOS7

Tests failing

make[1]: Entering directory `/root/temp/rcv-nginx/.build/sockproc/build/sockproc-master'
cc -Wall -Werror -o sockproc sockproc.c
./tests.sh 12345
+ port=12345
+ pidfile=/tmp/sockproc-test.pid
+ ./sockproc 12345 /tmp/sockproc-test.pid
+ echo -e =========================
=========================
+ echo -e 'uname -a\r\n0\r\n'
+ nc 127.0.0.1 12345
status:0
115
Linux rnd01-k01-jnk01 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
0
Ncat: Connection reset by peer.
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/temp/rcv-nginx/.build/sockproc/build/sockproc-master'

Update
Tried to use the built sockproc.
It is not working with Lua Resty Shell

2018-11-01T09:54:31+00:00 rnd01-t13-vgw01 ERR nginx_error: 2018/11/01 09:54:31 [error] 22443#22443: *486 connect() to unix:/var/run/ringcentral/vgw/vgw-shell.sock failed (111: Connection refused) while sending to client, client: 10.62.19.12, server: vi6-1-vgw-maintenance.*, request: "GET /localapi/vgw/v1/healthcheck HTTP/1.1", host: "rnd01-t13-vgw01.lab.nordigy.ru:9301"
2018-11-01T09:54:31+00:00 rnd01-t13-vgw01 ERR nginx_error: 2018/11/01 09:54:31 [error] 22443#22443: *486 [lua] vgw-healthcheck.lua:61: Error during calculating VGW uptime, exit-code -2 while sending to client, client: 10.62.19.12, server: vi6-1-vgw-maintenance.*, request: "GET /localapi/vgw/v1/healthcheck HTTP/1.1", host: "rnd01-t13-vgw01.lab.nordigy.ru:9301"
2018-11-01T09:54:31+00:00 rnd01-t13-vgw01 ERR nginx_error: 2018/11/01 09:54:31 [error] 22443#22443: *486 [lua] vgw-healthcheck.lua:62: StdOut: '' while sending to client, client: 10.62.19.12, server: vi6-1-vgw-maintenance.*, request: "GET /localapi/vgw/v1/healthcheck HTTP/1.1", host: "rnd01-t13-vgw01.lab.nordigy.ru:9301"
2018-11-01T09:54:31+00:00 rnd01-t13-vgw01 ERR nginx_error: 2018/11/01 09:54:31 [error] 22443#22443: *486 [lua] vgw-healthcheck.lua:63: StdErr: 'connection refused' while sending to client, client: 10.62.19.12, server: vi6-1-vgw-maintenance.*, request: "GET /localapi/vgw/v1/healthcheck HTTP/1.1", host: "rnd01-t13-vgw01.lab.nordigy.ru:9301"

Failed installing in FreeBSD 11

make

gcc49 -Wall -Werror -o sockproc sockproc.c
sockproc.c: In function 'main':
sockproc.c:304:35: error: invalid application of 'sizeof' to incomplete type 'struct sockaddr_in'
memset(&addr_in, 0, sizeof(addr_in));
^
sockproc.c:305:9: error: invalid use of undefined type 'struct sockaddr_in'
addr_in.sin_family = AF_INET;
^
sockproc.c:306:9: error: invalid use of undefined type 'struct sockaddr_in'
addr_in.sin_port = htons(port);
^
sockproc.c:307:9: error: invalid use of undefined type 'struct sockaddr_in'
addr_in.sin_addr.s_addr = inet_addr("127.0.0.1");
^
sockproc.c:308:56: error: invalid application of 'sizeof' to incomplete type 'struct sockaddr_in'
if (bind(fd, (struct sockaddr*)&addr_in, sizeof(addr_in)) == -1) {
^
*** Error code 1

Stop.

docker - can't connect to socket

I'm having trouble making sockproc work in a docker container. I tested it ok on my mac. Then I built it in an ubuntu docker container. The process appears to start ok, but I can't telnet to the socket.

Here's the command output:

root@311718b99310:/usr/local/bin# ./sockproc /tmp/foo
root@311718b99310:/usr/local/bin# ps aux |grep sock
root        33  0.0  0.0   4364    84 ?        Ss   03:44   0:00 ./sockproc /tmp/foo
root@311718b99310:/usr/local/bin# telnet /tmp/foo
telnet: could not resolve /tmp/foo/telnet: Name or service not known

Ideas or things I can do to help debug? Thanks.

user git clone

user git clone no get the progress util wait git clone over

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.