Call commonChildInit() before doing chroot init

This ensures that daemon clients see error messages from the chroot
setup.
This commit is contained in:
Eelco Dolstra 2014-08-01 19:29:03 +02:00 committed by Ludovic Courtès
parent c51374c128
commit b732ffd28d
2 changed files with 16 additions and 12 deletions

View file

@ -374,8 +374,6 @@ void Goal::trace(const format & f)
/* Common initialisation performed in child processes. */ /* Common initialisation performed in child processes. */
static void commonChildInit(Pipe & logPipe) static void commonChildInit(Pipe & logPipe)
{ {
restoreAffinity();
/* Put the child in a separate session (and thus a separate /* Put the child in a separate session (and thus a separate
process group) so that it has no controlling terminal (meaning process group) so that it has no controlling terminal (meaning
that e.g. ssh cannot open /dev/tty) and it doesn't receive that e.g. ssh cannot open /dev/tty) and it doesn't receive
@ -1974,6 +1972,12 @@ void DerivationGoal::initChild()
try { /* child */ try { /* child */
_writeToStderr = 0;
restoreAffinity();
commonChildInit(builderOut);
#if CHROOT_ENABLED #if CHROOT_ENABLED
if (useChroot) { if (useChroot) {
/* Initialise the loopback interface. */ /* Initialise the loopback interface. */
@ -2092,8 +2096,6 @@ void DerivationGoal::initChild()
} }
#endif #endif
commonChildInit(builderOut);
if (chdir(tmpDir.c_str()) == -1) if (chdir(tmpDir.c_str()) == -1)
throw SysError(format("changing into `%1%'") % tmpDir); throw SysError(format("changing into `%1%'") % tmpDir);

View file

@ -466,10 +466,18 @@ void warnOnce(bool & haveWarned, const FormatOrString & fs)
} }
static void defaultWriteToStderr(const unsigned char * buf, size_t count)
{
writeFull(STDERR_FILENO, buf, count);
}
void writeToStderr(const string & s) void writeToStderr(const string & s)
{ {
try { try {
_writeToStderr((const unsigned char *) s.data(), s.size()); auto p = _writeToStderr;
if (!p) p = defaultWriteToStderr;
p((const unsigned char *) s.data(), s.size());
} catch (SysError & e) { } catch (SysError & e) {
/* Ignore failing writes to stderr if we're in an exception /* Ignore failing writes to stderr if we're in an exception
handler, otherwise throw an exception. We need to ignore handler, otherwise throw an exception. We need to ignore
@ -481,12 +489,6 @@ void writeToStderr(const string & s)
} }
static void defaultWriteToStderr(const unsigned char * buf, size_t count)
{
writeFull(STDERR_FILENO, buf, count);
}
void (*_writeToStderr) (const unsigned char * buf, size_t count) = defaultWriteToStderr; void (*_writeToStderr) (const unsigned char * buf, size_t count) = defaultWriteToStderr;
@ -849,7 +851,7 @@ pid_t startProcess(std::function<void()> fun, const string & errorPrefix)
if (pid == -1) throw SysError("unable to fork"); if (pid == -1) throw SysError("unable to fork");
if (pid == 0) { if (pid == 0) {
_writeToStderr = defaultWriteToStderr; _writeToStderr = 0;
try { try {
restoreAffinity(); restoreAffinity();
fun(); fun();