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

Commit

Permalink
Only use numeric UIDs and GIDs in spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 30, 2012
1 parent 85873cf commit 76c7d93
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/utils/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var log = require("./log.js")
, myGID = process.getgid ? process.getgid() : null
, isRoot = process.getuid && myUID === 0
, constants = require("constants")
, uidNumber = require("uid-number")

function exec (cmd, args, env, takeOver, cwd, uid, gid, cb) {
if (typeof cb !== "function") cb = gid, gid = null
Expand All @@ -34,6 +35,15 @@ function exec (cmd, args, env, takeOver, cwd, uid, gid, cb) {
log.verbose(uid, "Setting uid from "+myUID)
log.verbose(new Error().stack, "stack at uid setting")
}

if (isNaN(uid) || isNaN(gid)) {
// get the numeric values
return uidNumber(uid, gid, function (er, uid, gid) {
if (er) return cb(er)
exec(cmd, args, env, takeOver, cwd, uid, gid, cb)
})
}

log.silly(cmd+" "+args.map(JSON.stringify).join(" "), "exec")
var stdout = ""
, stderr = ""
Expand Down Expand Up @@ -77,6 +87,7 @@ function pipe (cp1, cp2, cb) {
cb(errState = new Error(
"Failed "+(cp1.name || "<unknown>")+"\nexited with "+code))
})

cp2.on("exit", function (code) {
cp2._exited = true
if (errState) return
Expand All @@ -94,10 +105,14 @@ function spawn (c, a, env, takeOver, cwd, uid, gid) {
, env : env || process.env
, cwd : cwd || null }
, cp
if (uid != null) opts.uid = uid
if (gid != null) opts.gid = gid

if (!isNaN(uid)) opts.uid = uid
if (!isNaN(gid)) opts.gid = gid

if (!isNaN(opts.uid)) opts.uid = +opts.uid

if (!isNaN(opts.gid)) opts.gid = +opts.gid

var name = c +" "+ a.map(JSON.stringify).join(" ")
log.silly([c, a, opts.cwd], "spawning")
cp = child_process.spawn(c, a, opts)
Expand Down

0 comments on commit 76c7d93

Please sign in to comment.