FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

OpenSSL and libssh2 thread safety by srajko · Pull Request #912 · nodegit/nodegit · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cc  (2) .gyp  (1) .h  (1) .js  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 6 additions & 0 deletions generate/templates/manual/include/init_ssh2.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef INIT_SSH2
#define INIT_SSH2

void init_ssh2();

#endif
12 changes: 12 additions & 0 deletions generate/templates/manual/src/init_ssh2.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// We are initializing libssh2 from a separate .cc file to avoid ssize_t
// redefinition conflicts caused by incliding both node.h and libssh2.h from
// the same file (e.g. nodegit.cc)
//
// The redefinition can also be avoided by #defines but that is risky in case
// the libraries depend on the different definitions.

#include <libssh2.h>

void init_ssh2() {
libssh2_init(0);
}
3 changes: 3 additions & 0 deletions generate/templates/templates/binding.gyp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"sources": [
"src/lock_master.cc",
"src/nodegit.cc",
"src/init_ssh2.cc",
"src/promise_completion.cc",
"src/wrapper.cc",
"src/functions/copy.cc",
Expand All @@ -32,6 +33,8 @@

"include_dirs": [
"vendor/libv8-convert",
"vendor/libssh2/include",
"vendor/openssl/openssl/include",
"<!(node -e \"require('nan')\")"
],

Expand Down
32 changes: 32 additions & 0 deletions generate/templates/templates/nodegit.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// This is a generated file, modify: generate/templates/nodegit.cc.
#include <v8.h>

#include <node.h>
#include <git2.h>
#include <map>
#include <algorithm>
#include <set>

#include <openssl/crypto.h>

#include "../include/init_ssh2.h"
#include "../include/lock_master.h"
#include "../include/wrapper.h"
#include "../include/promise_completion.h"
Expand Down Expand Up @@ -39,7 +43,35 @@ void LockMasterGetDiagnostics(const FunctionCallbackInfo<Value>& info) {
info.GetReturnValue().Set(result);
}

static uv_mutex_t *opensslMutexes;

void OpenSSL_LockingCallback(int mode, int type, const char *, int) {
if (mode & CRYPTO_LOCK) {
uv_mutex_lock(&opensslMutexes[type]);
} else {
uv_mutex_unlock(&opensslMutexes[type]);
}
}

unsigned long OpenSSL_IDCallback() {
return (unsigned long)uv_thread_self();
}

void OpenSSL_ThreadSetup() {
opensslMutexes=(uv_mutex_t *)malloc(CRYPTO_num_locks() * sizeof(uv_mutex_t));

for (int i=0; i<CRYPTO_num_locks(); i++) {
uv_mutex_init(&opensslMutexes[i]);
}

CRYPTO_set_locking_callback(OpenSSL_LockingCallback);
CRYPTO_set_id_callback(OpenSSL_IDCallback);
}

extern "C" void init(Local<v8::Object> target) {
// Initialize thread safety in openssl and libssh2
OpenSSL_ThreadSetup();
init_ssh2();
// Initialize libgit2.
git_libgit2_init();

Expand Down
16 changes: 13 additions & 3 deletions lifecycleScripts/configureLibssh2.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ module.exports = function retrieveExternalDependencies() {

return new Promise(function(resolve, reject) {
console.info("[nodegit] Configuring libssh2.");
cp.execFile(
rooted("vendor/libssh2/") + "configure",
{cwd: rooted("vendor/libssh2/")},
var opensslDir = rooted("vendor/openssl/openssl");
var newEnv = {};
Object.keys(process.env).forEach(function(key) {
newEnv[key] = process.env[key];
});
newEnv.CPPFLAGS = newEnv.CPPFLAGS || "";
newEnv.CPPFLAGS += " -I" + path.join(opensslDir, "include");
newEnv.CPPFLAGS = newEnv.CPPFLAGS.trim();

cp.exec(
rooted("vendor/libssh2/configure") +
" --with-libssl-prefix=" + opensslDir,
{cwd: rooted("vendor/libssh2/"), env: newEnv},
function(err, stdout, stderr) {
if (err) {
console.error(err);
Expand Down
40 changes: 22 additions & 18 deletions lifecycleScripts/install.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var promisify = require("promisify-node");
var path = require("path");
var fs = require("fs");

var cp = require("child_process");
var prepareForBuild = require("./prepareForBuild");

var exec = promisify(function(command, opts, callback) {
return require("child_process").exec(command, opts, callback);
return cp.exec(command, opts, callback);
});

var fromRegistry;
Expand Down Expand Up @@ -52,7 +52,9 @@ function installPrebuilt() {

function pathForTool(name) {
var toolPath = path.resolve(".", "node_modules", ".bin", name);
toolPath = "\"" + toolPath + "\"";
if (process.platform == "win32") {
toolPath += ".cmd";
}
return toolPath;
}

Expand All @@ -72,7 +74,8 @@ function build() {
var opts = {
cwd: ".",
maxBuffer: Number.MAX_VALUE,
env: process.env
env: process.env,
stdio: "inherit"
};

var builder = "node-gyp";
Expand Down Expand Up @@ -104,26 +107,27 @@ function build() {

opts.env.HOME = path.join(home, ".nodegit-gyp");

var cmd = [
pathForTool(builder),
var cmd = pathForTool(builder);
var args = [
"rebuild",
debug,
target,
distUrl
]
.join(" ")
.trim();

return exec(cmd, opts)
.then(function() {
console.info("[nodegit] Compilation complete.");
console.info("[nodegit] Completed installation successfully.");
process.exitCode = 0;
},
function(err, stderr) {
console.error(err);
console.error(stderr);
.trim()
.split(" ");
return new Promise(function(resolve, reject) {
var child = cp.spawn(cmd, args, opts);
child.on("close", function(code) {
console.log(code);
if (code) {
reject(code);
process.exitCode = 13;
}
);
else {
resolve();
}
});
});
}

Back | FazBrowse Home | New Git URL