execl崩溃C ++ node.js-addon

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了execl崩溃C ++ node.js-addon相关的知识,希望对你有一定的参考价值。

正常C ++ execl工作正常(与g++ ok.cc -o ok.elf编译)

#include <unistd.h>
int main(){
  execl("/usr/bin/python", "/usr/bin/python", nullptr);
}

但崩溃时,作为node.js C ++插件工作

#include <node.h>
#include <unistd.h>

namespace bug{
  void wtf(const v8::FunctionCallbackInfo<v8::Value>& args){
    execl("/usr/bin/python", "/usr/bin/python", nullptr);
  }

  void init(v8::Local<v8::Object> exports){
    NODE_SET_METHOD(exports, "wtf", bug::wtf);
  }
  NODE_MODULE(NODE_GYP_MODULE_NAME, init)
}

Crash of node.js extension

node.js v8.9.1 node-gyp v3.6.2 gcc版本6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2)

答案

Node不支持所有posix系统调用。

看到这个帖子

A way to call execl, execle, execlp, execv, execvP or execvp from Node.js

当您使用无法使用的东西时,预计会发生崩溃。正如上面的线程中所讨论的,您需要创建自己的exec

index.曹操

#include <nan.h>
#include <fcntl.h>
#include <unistd.h>

int doNotCloseStreamsOnExit(int desc) {
  int flags = fcntl(desc, F_GETFD, 0);
  if (flags < 0) return flags;
  flags &= ~FD_CLOEXEC; //clear FD_CLOEXEC bit
  return fcntl(desc, F_SETFD, flags);
}

void copyArray(char* dest[], unsigned int offset, v8::Local<v8::Array> src) {
  unsigned int length = src->Length();
  for (unsigned int i = 0; i < length; i++) {
    v8::String::Utf8Value arrayElem(Nan::Get(src, i).ToLocalChecked()->ToString());
    std::string arrayElemStr (*arrayElem);
    char* tmp = new char[arrayElemStr.length() +1];
    strcpy(tmp, arrayElemStr.c_str());
    dest[i + offset] = tmp;
  }
}

void setEnv(v8::Local<v8::Array> src) {
  unsigned int length = src->Length();
  v8::Local<v8::String> keyProp = Nan::New<v8::String>("key").ToLocalChecked();
  v8::Local<v8::String> valueProp = Nan::New<v8::String>("value").ToLocalChecked();
  for (unsigned int i = 0; i < length; i++) {
    v8::Local<v8::Object> obj = Nan::Get(src, i).ToLocalChecked()->ToObject();

    v8::String::Utf8Value objKey(Nan::Get(obj, keyProp).ToLocalChecked()->ToString());
    v8::String::Utf8Value objValue(Nan::Get(obj, valueProp).ToLocalChecked()->ToString());

    std::string objKeyStr (*objKey);
    char *key = const_cast<char*> ( objKeyStr.c_str() );
    std::string objValueStr (*objValue);
    char *value = const_cast<char*> ( objValueStr.c_str() );

    setenv(key, value, 1);
  }
}

void Method(const Nan::FunctionCallbackInfo<v8::Value>& info) {
  if (info.Length() < 3) {
    return;
  }
  if (!info[0]->IsString()) {
    return;
  }

  // get command
  v8::String::Utf8Value val(info[0]->ToString());
  std::string str (*val);
  char *command = const_cast<char*> ( str.c_str() );

  // set env on the current process
  v8::Local<v8::Array> envArr = v8::Local<v8::Array>::Cast(info[1]);
  setEnv(envArr);

  // build args: command, ...args, NULL
  v8::Local<v8::Array> argsArr = v8::Local<v8::Array>::Cast(info[2]);
  char* args[argsArr->Length() + 2];
  args[0] = command;
  copyArray(args, 1, argsArr);
  args[argsArr->Length() + 1] = NULL;

  // fix stream flags
  doNotCloseStreamsOnExit(0); //stdin
  doNotCloseStreamsOnExit(1); //stdout
  doNotCloseStreamsOnExit(2); //stderr

  execvp(command, args);
}

void Init(v8::Local<v8::Object> exports) {
  exports->Set(Nan::New("exec").ToLocalChecked(),
               Nan::New<v8::FunctionTemplate>(Method)->GetFunction());
}

NODE_MODULE(exec, Init)

index.js

'use strict';

var addon = require('bindings')('addon');
var path = require('path');
var fs = require('fs');

module.exports = function(cmd, env, args) {
  if (!cmd) {
    throw new Error('Command is required');
  }

  var envArr = Object.keys(env || {}).map(key => {
    return {
      key,
      value: env[key],
    };
  });

  addon.exec(cmd, envArr, args || []);
};

PS:代码从https://github.com/OrKoN/native-exec发布,以防链接在将来无效

另外你不应该尝试做的是执行需要获取TTY的东西,在这种情况下你会增加很多复杂性。所以运行python需要控制你的TTY。

以上是关于execl崩溃C ++ node.js-addon的主要内容,如果未能解决你的问题,请参考以下文章

C语言中 使用execl函数创建一个文件

C#NPOI execl下载

C++ dup2 和 execl

execl vs execv,参数大小未知

Unix上用C程序实现pipe管道命令“ | “(pipe,fork,dup2,close,execl,重定向)

如何在 execl() 函数中更改时区(TZ=UTC)输出。