├── demo ├── demo.js ├── demovp.js └── demovp-pids.js ├── test ├── mocha.opts ├── files │ ├── echo.sh │ ├── echovp-empty.sh │ └── echovp.sh ├── kexec.test.js ├── kexec.testvp.js └── kexec.testvp-empty.js ├── index.js ├── .gitignore ├── .travis.yml ├── circle.yml ├── binding.gyp ├── Makefile ├── package.json ├── LICENSE ├── README.md ├── CHANGELOG.md └── src └── kexec.cc /demo/demo.js: -------------------------------------------------------------------------------- 1 | var kexec = require('../') 2 | kexec('top') 3 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter spec 2 | --ui qunit 3 | --timeout 2000 -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./build/Release/kexec.node').kexec 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .lock-wscript 3 | node_modules 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /demo/demovp.js: -------------------------------------------------------------------------------- 1 | var kexec = require('../') 2 | kexec('less', [ '/etc/fstab' ]) 3 | -------------------------------------------------------------------------------- /test/files/echo.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var kexec = require('../../'); 4 | 5 | kexec('echo "hello world"'); 6 | -------------------------------------------------------------------------------- /test/files/echovp-empty.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var kexec = require('../../'); 4 | 5 | kexec('echo', [ ]); 6 | -------------------------------------------------------------------------------- /test/files/echovp.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var kexec = require('../../'); 4 | 5 | kexec('echo', [ "hello", "world" ]); 6 | -------------------------------------------------------------------------------- /demo/demovp-pids.js: -------------------------------------------------------------------------------- 1 | var kexec = require('../') 2 | 3 | console.log(process.pid + ' - PID before exec') 4 | 5 | kexec('sh', [ '-c', 'echo "$$ - PID after exec"' ]) 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "6" 5 | - "5" 6 | - "4" 7 | - "0.12" 8 | env: 9 | - CXX=g++-4.8 10 | addons: 11 | apt: 12 | sources: 13 | - ubuntu-toolchain-r-test 14 | packages: 15 | - gcc-4.8 16 | - g++-4.8 17 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | machine: 2 | node: 3 | version: 4.1.0 4 | machine: 5 | pre: 6 | - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 10 7 | - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 10 8 | - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20 9 | - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20 10 | -------------------------------------------------------------------------------- /test/kexec.test.js: -------------------------------------------------------------------------------- 1 | var exec = require('child_process').exec 2 | var path = require('path') 3 | var assert = require('assert') 4 | 5 | /* global suite, test */ 6 | 7 | suite('kexec') 8 | 9 | test('+ kexec() - kexec echo - single argument', function (done) { 10 | var echoFile = path.join(__dirname, './files/echo.sh') 11 | 12 | exec(echoFile, function (error, stdout, stderr) { 13 | assert(stdout.trim() === 'hello world') 14 | assert(stderr.trim() === '') 15 | assert(error === null) 16 | done() 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/kexec.testvp.js: -------------------------------------------------------------------------------- 1 | var exec = require('child_process').exec 2 | var path = require('path') 3 | var assert = require('assert') 4 | 5 | /* global suite test */ 6 | 7 | suite('kexec') 8 | 9 | test('+ kexec() - kexec echovp - two arguments', function (done) { 10 | var echoFile = path.join(__dirname, './files/echovp.sh') 11 | 12 | exec(echoFile, function (error, stdout, stderr) { 13 | assert(stdout.trim() === 'hello world') 14 | assert(stderr.trim() === '') 15 | assert(error === null) 16 | done() 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /test/kexec.testvp-empty.js: -------------------------------------------------------------------------------- 1 | var exec = require('child_process').exec 2 | var path = require('path') 3 | var assert = require('assert') 4 | 5 | /* global suite test */ 6 | 7 | suite('kexec') 8 | 9 | test('+ kexec() - kexec echovp - two arguments, empty list', function (done) { 10 | var echoFile = path.join(__dirname, './files/echovp-empty.sh') 11 | 12 | exec(echoFile, function (error, stdout, stderr) { 13 | assert(stdout.trim() === '') 14 | assert(stderr.trim() === '') 15 | assert(error === null) 16 | done() 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | 'targets': [ 3 | { 4 | 'target_name': 'kexec', 5 | 'sources': [ 'src/kexec.cc' ], 6 | 'defines': [ 7 | ' /dev/null && echo "__NODE_GE_V4__" || true)', 8 | ' /dev/null && echo "__NODE_V0_11_OR_12__" || true)', 9 | ' /dev/null && echo "__NODE_V0_11_OR_12__" || true)', 10 | ' /dev/null && echo "__NODE_V0_10__" || true)', 11 | ], 12 | "include_dirs": ["", 22 | "license": "MIT", 23 | "main": "./index", 24 | "engines": { 25 | "node": ">=0.10" 26 | }, 27 | "devDependencies": { 28 | "mocha": "^2.3.3", 29 | "standard": "^8.0.0" 30 | }, 31 | "dependencies": { 32 | "nan": "^2.4.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2011-2012 JP Richardson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 6 | (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, 7 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 13 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 14 | OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 15 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Node.js - kexec 2 | =============== 3 | 4 | [![npm Package](https://img.shields.io/npm/v/kexec.svg?style=flat-square)](https://www.npmjs.org/package/kexec) 5 | [![build status](https://api.travis-ci.org/jprichardson/node-kexec.svg)](http://travis-ci.org/jprichardson/node-kexec) 6 | [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) 7 | 8 | This module causes your current Node.js process to be replaced by the process invoked by the parameter of this function. It's like the Ruby exec function. It currently does not work on Windows. 9 | 10 | Fully compatible with Node.js version v0.10 and v0.11. 11 | 12 | 13 | Usage 14 | ----- 15 | 16 | ```js 17 | var kexec = require('kexec') 18 | 19 | kexec('top') //your process now becomes top, can also accept parameters in one string 20 | ``` 21 | 22 | ```javascript 23 | var kexec = require('kexec') 24 | 25 | kexec('du', [ '-sh', '/etc/fstab' ]) //your process now becomes du, with the arguments indicated 26 | ``` 27 | 28 | 29 | Details 30 | ------- 31 | 32 | `kexec` can be called in either of two ways, as indicated by the examples, above. 33 | 34 | With one argument `arg`, that argument must be a string. The resulting system 35 | call is: 36 | 37 | execvp("/bin/sh", [ "/bin/sh", "-c", arg, 0 ]); 38 | 39 | With two arguments, the first (`cmd`) must be a string, and the second (`args`) an array of strings. The resulting 40 | system call is: 41 | 42 | execvp(cmd, [ cmd, args[0], args[1], ..., 0 ]); 43 | 44 | In the first case, the command is subject to shell parsing, and shell meta 45 | characters retain their special meanings. In the second case, the arguments 46 | are passed directly to `execvp`, without an intervening shell. 47 | 48 | 49 | License 50 | ------- 51 | 52 | (The MIT License) 53 | 54 | Copyright (c) 2011-2015 JP Richardson 55 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 3.0.0 / 2016-09-11 2 | ------------------ 3 | - port to NaN [#31] 4 | - change JS style to Standard 5 | - dropped official support for Node v0.10 6 | 7 | 2.0.2 / 2015-11-27 8 | ------------------ 9 | - fix build in Make: See: https://github.com/jprichardson/node-kexec/pull/28 10 | - compatible for Node v5 See: https://github.com/jprichardson/node-kexec/pull/29 11 | 12 | 2.0.1 / 2015-11-25 13 | ------------------ 14 | - removed incorrect references to `node-gyp`: See: https://github.com/jprichardson/node-kexec/pull/27 15 | 16 | 2.0.0 / 2015-11-24 17 | ------------------ 18 | - removed `node-gyp` dep. In case this breaks anything, bumped major. See: https://github.com/jprichardson/node-kexec/pull/24 19 | 20 | 1.3.0 / 2015-10-20 21 | ------------------ 22 | - Node.js v4 support https://github.com/jprichardson/node-kexec/pull/21 23 | 24 | 1.2.0 / 2015-02-09 25 | ------------------ 26 | - Node.js v0.12 support https://github.com/jprichardson/node-kexec/pull/18 27 | 28 | 1.1.1 / 2015-01-26 29 | ------------------ 30 | - move `pangyp` to dependencies 31 | 32 | 1.1.0 / 20145-01-20 33 | ------------------ 34 | - dropped `node-gyp` for `pangyp` 35 | - `io.js` support https://github.com/jprichardson/node-kexec/issues/13 36 | 37 | 1.0.0 / 2014-12-29 38 | ------------------ 39 | - added Node 0.11 support https://github.com/jprichardson/node-kexec/pull/11 40 | 41 | 0.2.0 / 2014-01-25 42 | ------------------ 43 | * API extensions, kexec(cmnd,[args]) now execvps cmd with args directly. 44 | 45 | 0.1.1 / 2012-09-19 46 | ------------------ 47 | * kexec now returns `exevp` error code if it fails. 48 | 49 | 0.1.0 / 2012-09-19 50 | ------------------ 51 | * Made Node v0.8 compatible. 52 | 53 | 0.0.3 / 2012-01-09 54 | ------------------ 55 | * Fixed typo in index.js (Matt Insler) 56 | 57 | 0.0.2 / 2012-01-06 58 | ------------------ 59 | * Node.js v0.6 support (Matt Insler) 60 | 61 | 0.0.1 / 2011-12-05 62 | ------------------ 63 | * Initial release 64 | -------------------------------------------------------------------------------- /src/kexec.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #if defined(__NODE_V0_11_OR_12__) || defined(__NODE_GE_V4__) 5 | #include 6 | #endif 7 | 8 | //#ifdef __POSIX__ 9 | #include 10 | /*#else 11 | #include 12 | #endif*/ 13 | 14 | #include 15 | 16 | 17 | using v8::Array; 18 | using v8::FunctionTemplate; 19 | using v8::Handle; 20 | using v8::Integer; 21 | using v8::Local; 22 | using v8::Object; 23 | using v8::String; 24 | 25 | 26 | static int clear_cloexec (int desc) 27 | { 28 | int flags = fcntl (desc, F_GETFD, 0); 29 | if (flags < 0) 30 | return flags; //return if reading failed 31 | 32 | flags &= ~FD_CLOEXEC; //clear FD_CLOEXEC bit 33 | return fcntl (desc, F_SETFD, flags); 34 | } 35 | 36 | static int do_exec(char *argv[]) 37 | { 38 | clear_cloexec(0); //stdin 39 | clear_cloexec(1); //stdout 40 | clear_cloexec(2); //stderr 41 | return execvp(argv[0], argv); 42 | } 43 | 44 | NAN_METHOD(kexec) { 45 | /* 46 | * Steve Blott: 17 Jan, 2014 47 | * Temporary comment by way of explanation... 48 | * To be deleted. 49 | * 50 | * With a single argument: 51 | * - pass it to execvp as "sh -c 'args[0]'" 52 | * - this is the existing usage 53 | * 54 | * With exactly two arguments: 55 | * - the first is the command name 56 | * - the second is an array of arguments 57 | * ...as in process.child_process.spawn() 58 | * 59 | * This approach is not great, but it allows the established usage to 60 | * coexist with direct execvp-usage, and avoids making any changes to the 61 | * established API. 62 | */ 63 | 64 | if ( 1 == info.Length() && info[0]->IsString() ) 65 | { 66 | String::Utf8Value str(info[0]); 67 | char* argv[] = { const_cast("/bin/sh"), const_cast("-c"), *str, NULL}; 68 | 69 | int err = do_exec(argv); 70 | 71 | info.GetReturnValue().Set(Nan::New(err)); 72 | } 73 | 74 | if ( 2 == info.Length() && info[0]->IsString() && info[1]->IsArray() ) 75 | { 76 | String::Utf8Value str(info[0]); 77 | 78 | // Substantially copied from: 79 | // https://github.com/joyent/node/blob/2944e03/src/node_child_process.cc#L92-104 80 | Local argv_handle = Local::Cast(info[1]); 81 | int argc = argv_handle->Length(); 82 | 83 | int argv_length = argc + 1 + 1; 84 | char **argv = new char*[argv_length]; 85 | 86 | argv[0] = *str; 87 | argv[argv_length-1] = NULL; 88 | for (int i = 0; i < argc; i++) { 89 | String::Utf8Value arg(argv_handle->Get(Nan::New(i))->ToString()); 90 | argv[i+1] = strdup(*arg); 91 | } 92 | 93 | int err = do_exec(argv); 94 | 95 | // Failed...! 96 | // FIXME: It might be better to raise an exception here. 97 | for (int i = 0; i < argc; i++) 98 | free(argv[i+1]); 99 | delete [] argv; 100 | 101 | info.GetReturnValue().Set(Nan::New(err)); 102 | } 103 | 104 | return Nan::ThrowTypeError("kexec: invalid arguments"); 105 | } 106 | 107 | 108 | #define EXPORT(name, symbol) exports->Set( \ 109 | Nan::New(name).ToLocalChecked(), \ 110 | Nan::New(symbol)->GetFunction() \ 111 | ) 112 | 113 | void init (Handle exports) { 114 | EXPORT("kexec", kexec); 115 | } 116 | 117 | NODE_MODULE(kexec, init); 118 | --------------------------------------------------------------------------------