Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Specify uid and gid to script runner
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 8, 2011
1 parent 3ac6312 commit a9cf93f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
37 changes: 16 additions & 21 deletions bin/npm-script-runner.js
@@ -1,31 +1,26 @@
#!/usr/bin/env/ node
#!/usr/bin/env node

// Run a command as nobody
// Run a command as a specific user.
// env is passed through untouched.
// usage: npm-script-runner $folder $uid "make test"
// usage: npm-script-runner $folder $uid $gid "make test"
// This is internal plumbing

console.error(process.argv)

var argv = process.argv.slice(2)
, cwd = argv.shift()
, cmd = argv.shift()
, uid = argv.shift()
, gid = argv.shift()
, cmd = argv.shift()
, stdio = process.binding("stdio")
, cp = require("child_process")

try {
process.setuid(uid)
} catch (ex) {
console.error("Could not setuid to "+uid)
throw ex
}

process.setgid(gid)
process.setuid(uid)

require("child_process").spawn( "sh", ["-c", cmd]
, { env : process.env
, cwd : cwd
, customFDs: [ stdio.stdinFD
, stdio.stdoutFD
, stdio.stderrFD ]
})
.on("exit", function (c) { process.exit(c) })
cp.spawn( "sh", ["-c", cmd]
, { env : process.env
, cwd : cwd
, customFds: [ stdio.stdinFD
, stdio.stdoutFD
, stdio.stderrFD ]
} )
.on("exit", function (c) { process.exit(c) })
6 changes: 4 additions & 2 deletions lib/utils/lifecycle.js
Expand Up @@ -120,9 +120,10 @@ function runPackageLifecycle (pkg, env, wd, cb) {
, node = process.execPath
, runner = path.join(__dirname, "../../bin/npm-script-runner.js")
, user = npm.config.get("user")
, group = npm.config.get("group")
, cmd = env.npm_lifecycle_script

exec(node, [runner, wd, cmd, user], env, true, wd,
exec(node, [runner, wd, user, group, cmd], env, true, wd,
function (er, code, stdout, stderr) {
if (er && !npm.ROLLBACK) {
log("Failed to exec "+stage+" script", pkg._id)
Expand Down Expand Up @@ -151,11 +152,12 @@ function runHookLifecycle (pkg, env, wd, cb) {
var node = process.execPath
, runner = path.join(__dirname, "../../bin/npm-script-runner.js")
, user = npm.config.get("user")
, group = npm.config.get("group")
, cmd = hook
fs.stat(hook, function (er) {
if (er) return cb()

exec(node, [runner, wd, cmd, user], env, true, wd, function (er) {
exec(node, [runner, wd, user, group, cmd], env, true, wd, function (er) {
if (er) {
er.message += "\nFailed to exec "+stage+" hook script"
log(er, pkg._id)
Expand Down

0 comments on commit a9cf93f

Please sign in to comment.