about summary refs log blame commit diff
path: root/src/eval.cc
blob: be8b70a039becd1e00370623314bec942d72a3e2 (plain) (tree)
1
2
3
4
5
6
7
8






                      
                  











                                                              
                                    





                                  
                                                               



                 

                                                               
 
                            
                                                


                                                                                            
                                                                              
 


                                            
            

                                         
 
            
 
                         

     









                                                                                      
                
                                                                         
 




                                                                                                
 

                         

      














                                                                              
            


                                                                
 


                                                                 
 

                                                                          
 
                                                                      
            
                                 
                                                            
          
                 
 

     
                
 











                                                                 




                                                                      
                                                  

                               

                                                                   


 
                         
 
                                  



             

                                                                
                                               
 
                                                                   


 
                      
 
                                    


 
              
 



                                                                    

 






















                                                                     
                                                  
 
                          
                    





















                                                                                 
    
                                                                                  
                        
 


                                                        
                                                                           
 
                                       
                                  
                                  
                                                                       

                                   
                                 
 
                                                  
                                                    


                                  
                                                        
                                          
 





                                                             




                                                                     
                          
             
 


                                                                             
 


                                                                

                  
     
 




                                                                           


                                                             
                                
        
























                                                                        
        


















                                                                                

                                                              
                                                               



                                                         
     
 
                                               


 
                               

                   
                               
 
#include <map>
#include <iostream>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>

#include "eval.hh"
#include "globals.hh"
#include "values.hh"
#include "db.hh"


/* A Unix environment is a mapping from strings to strings. */
typedef map<string, string> Environment;


/* Return true iff the given path exists. */
bool pathExists(const string & path)
{
    int res;
    struct stat st;
    res = stat(path.c_str(), &st);
    if (!res) return true;
    if (errno != ENOENT)
        throw SysError(format("getting status of %1%") % path);
    return false;
}


/* Run a program. */
static void runProgram(const string & program, Environment env)
{
    /* Create a log file. */
    string logFileName = nixLogDir + "/run.log";
    /* !!! auto-pclose on exit */
    FILE * logFile = popen(("tee " + logFileName + " >&2").c_str(), "w"); /* !!! escaping */
    if (!logFile)
        throw SysError(format("unable to create log file %1%") % logFileName);

    /* Fork a child to build the package. */
    pid_t pid;
    switch (pid = fork()) {
            
    case -1:
        throw SysError("unable to fork");

    case 0: 

        try { /* child */

#if 0
            /* Try to use a prebuilt. */
            string prebuiltHashS, prebuiltFile;
            if (queryDB(nixDB, dbPrebuilts, hash, prebuiltHashS)) {

                try {
                    prebuiltFile = getFile(parseHash(prebuiltHashS));
                } catch (Error e) {
                    cerr << "cannot obtain prebuilt (ignoring): " << e.what() << endl;
                    goto build;
                }
                
                cerr << "substituting prebuilt " << prebuiltFile << endl;

                int res = system(("tar xfj " + prebuiltFile + " 1>&2").c_str()); // !!! escaping
                if (WEXITSTATUS(res) != 0)
                    /* This is a fatal error, because path may now
                       have clobbered. */
                    throw Error("cannot unpack " + prebuiltFile);

                _exit(0);
            }
#endif

            //             build:

            /* Fill in the environment.  We don't bother freeing
               the strings, since we'll exec or die soon
               anyway. */
            const char * env2[env.size() + 1];
            int i = 0;
            for (Environment::iterator it = env.begin();
                 it != env.end(); it++, i++)
                env2[i] = (new string(it->first + "=" + it->second))->c_str();
            env2[i] = 0;

            /* Dup the log handle into stderr. */
            if (dup2(fileno(logFile), STDERR_FILENO) == -1)
                throw SysError("cannot pipe standard error into log file");
            
            /* Dup stderr to stdin. */
            if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
                throw SysError("cannot dup stderr into stdout");

            /* Make the program executable.  !!! hack. */
            if (chmod(program.c_str(), 0755))
                throw SysError("cannot make program executable");

            /* Execute the program.  This should not return. */
            execle(program.c_str(), baseNameOf(program).c_str(), 0, env2);

            throw SysError(format("unable to execute %1%") % program);
            
        } catch (exception & e) {
            cerr << format("build error: %1%\n") % e.what();