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

Merge pull request #912 from srajko/openssl-thread-safety · nodegit/nodegit@f6cbd36 · GitHub

Repository navigation

Commit f6cbd36

Browse files
committed
Merge pull request #912 from srajko/openssl-thread-safety
OpenSSL and libssh2 thread safety
2 parents d9f6147 + 6a16e67 commit f6cbd36

6 files changed

Lines changed: 88 additions & 21 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef INIT_SSH2
2+
#define INIT_SSH2
3+
4+
void init_ssh2();
5+
6+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// We are initializing libssh2 from a separate .cc file to avoid ssize_t
2+
// redefinition conflicts caused by incliding both node.h and libssh2.h from
3+
// the same file (e.g. nodegit.cc)
4+
//
5+
// The redefinition can also be avoided by #defines but that is risky in case
6+
// the libraries depend on the different definitions.
7+
8+
#include <libssh2.h>
9+
10+
void init_ssh2() {
11+
libssh2_init(0);
12+
}

‎generate/templates/templates/binding.gyp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"sources": [
1717
"src/lock_master.cc",
1818
"src/nodegit.cc",
19+
"src/init_ssh2.cc",
1920
"src/promise_completion.cc",
2021
"src/wrapper.cc",
2122
"src/functions/copy.cc",
@@ -32,6 +33,8 @@
3233

3334
"include_dirs": [
3435
"vendor/libv8-convert",
36+
"vendor/libssh2/include",
37+
"vendor/openssl/openssl/include",
3538
"<!(node -e \"require('nan')\")"
3639
],
3740

‎generate/templates/templates/nodegit.cc‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// This is a generated file, modify: generate/templates/nodegit.cc.
22
#include <v8.h>
3+
34
#include <node.h>
45
#include <git2.h>
56
#include <map>
67
#include <algorithm>
78
#include <set>
89

10+
#include <openssl/crypto.h>
11+
12+
#include "../include/init_ssh2.h"
913
#include "../include/lock_master.h"
1014
#include "../include/wrapper.h"
1115
#include "../include/promise_completion.h"
@@ -39,7 +43,35 @@ void LockMasterGetDiagnostics(const FunctionCallbackInfo<Value>& info) {
3943
info.GetReturnValue().Set(result);
4044
}
4145

46+
static uv_mutex_t *opensslMutexes;
47+
48+
void OpenSSL_LockingCallback(int mode, int type, const char *, int) {
49+
if (mode & CRYPTO_LOCK) {
50+
uv_mutex_lock(&opensslMutexes[type]);
51+
} else {
52+
uv_mutex_unlock(&opensslMutexes[type]);
53+
}
54+
}
55+
56+
unsigned long OpenSSL_IDCallback() {
57+
return (unsigned long)uv_thread_self();
58+
}
59+
60+
void OpenSSL_ThreadSetup() {
61+
opensslMutexes=(uv_mutex_t *)malloc(CRYPTO_num_locks() * sizeof(uv_mutex_t));
62+
63+
for (int i=0; i<CRYPTO_num_locks(); i++) {
64+
uv_mutex_init(&opensslMutexes[i]);
65+
}
66+
67+
CRYPTO_set_locking_callback(OpenSSL_LockingCallback);
68+
CRYPTO_set_id_callback(OpenSSL_IDCallback);
69+
}
70+
4271
extern "C" void init(Local<v8::Object> target) {
72+
// Initialize thread safety in openssl and libssh2
73+
OpenSSL_ThreadSetup();
74+
init_ssh2();
4375
// Initialize libgit2.
4476
git_libgit2_init();
4577

‎lifecycleScripts/configureLibssh2.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@ module.exports = function retrieveExternalDependencies() {
99

1010
return new Promise(function(resolve, reject) {
1111
console.info("[nodegit] Configuring libssh2.");
12-
cp.execFile(
13-
rooted("vendor/libssh2/") + "configure",
14-
{cwd: rooted("vendor/libssh2/")},
12+
var opensslDir = rooted("vendor/openssl/openssl");
13+
var newEnv = {};
14+
Object.keys(process.env).forEach(function(key) {
15+
newEnv[key] = process.env[key];
16+
});
17+
newEnv.CPPFLAGS = newEnv.CPPFLAGS || "";
18+
newEnv.CPPFLAGS += " -I" + path.join(opensslDir, "include");
19+
newEnv.CPPFLAGS = newEnv.CPPFLAGS.trim();
20+
21+
cp.exec(
22+
rooted("vendor/libssh2/configure") +
23+
" --with-libssl-prefix=" + opensslDir,
24+
{cwd: rooted("vendor/libssh2/"), env: newEnv},
1525
function(err, stdout, stderr) {
1626
if (err) {
1727
console.error(err);

‎lifecycleScripts/install.js‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var promisify = require("promisify-node");
22
var path = require("path");
33
var fs = require("fs");
4-
4+
var cp = require("child_process");
55
var prepareForBuild = require("./prepareForBuild");
66

77
var exec = promisify(function(command, opts, callback) {
8-
return require("child_process").exec(command, opts, callback);
8+
return cp.exec(command, opts, callback);
99
});
1010

1111
var fromRegistry;
@@ -52,7 +52,9 @@ function installPrebuilt() {
5252

5353
function pathForTool(name) {
5454
var toolPath = path.resolve(".", "node_modules", ".bin", name);
55-
toolPath = "\"" + toolPath + "\"";
55+
if (process.platform == "win32") {
56+
toolPath += ".cmd";
57+
}
5658
return toolPath;
5759
}
5860

@@ -72,7 +74,8 @@ function build() {
7274
var opts = {
7375
cwd: ".",
7476
maxBuffer: Number.MAX_VALUE,
75-
env: process.env
77+
env: process.env,
78+
stdio: "inherit"
7679
};
7780

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

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

107-
var cmd = [
108-
pathForTool(builder),
110+
var cmd = pathForTool(builder);
111+
var args = [
109112
"rebuild",
110113
debug,
111114
target,
112115
distUrl
113116
]
114117
.join(" ")
115-
.trim();
116-
117-
return exec(cmd, opts)
118-
.then(function() {
119-
console.info("[nodegit] Compilation complete.");
120-
console.info("[nodegit] Completed installation successfully.");
121-
process.exitCode = 0;
122-
},
123-
function(err, stderr) {
124-
console.error(err);
125-
console.error(stderr);
118+
.trim()
119+
.split(" ");
120+
return new Promise(function(resolve, reject) {
121+
var child = cp.spawn(cmd, args, opts);
122+
child.on("close", function(code) {
123+
console.log(code);
124+
if (code) {
125+
reject(code);
126126
process.exitCode = 13;
127127
}
128-
);
128+
else {
129+
resolve();
130+
}
131+
});
132+
});
129133
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL