+ /*}}}*/
+// DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
+// ---------------------------------------------------------------------
+/* This looks for a list of scripts to run from the configuration file
+ each one is run and is fed on standard input a list of all .deb files
+ that are due to be installed. */
+bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
+{
+ Configuration::Item const *Opts = _config->Tree(Cnf);
+ if (Opts == 0 || Opts->Child == 0)
+ return true;
+ Opts = Opts->Child;
+
+ unsigned int Count = 1;
+ for (; Opts != 0; Opts = Opts->Next, Count++)
+ {
+ if (Opts->Value.empty() == true)
+ continue;
+
+ // Determine the protocol version
+ string OptSec = Opts->Value;
+ string::size_type Pos;
+ if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
+ Pos = OptSec.length();
+ else
+ Pos--;
+ OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
+
+ unsigned int Version = _config->FindI(OptSec+"::Version",1);
+
+ // Create the pipes
+ int Pipes[2];
+ if (pipe(Pipes) != 0)
+ return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
+ SetCloseExec(Pipes[0],true);
+ SetCloseExec(Pipes[1],true);
+
+ // Purified Fork for running the script
+ pid_t Process = ExecFork();
+ if (Process == 0)
+ {
+ // Setup the FDs
+ dup2(Pipes[0],STDIN_FILENO);
+ SetCloseExec(STDOUT_FILENO,false);
+ SetCloseExec(STDIN_FILENO,false);
+ SetCloseExec(STDERR_FILENO,false);
+
+ const char *Args[4];
+ Args[0] = "/bin/sh";
+ Args[1] = "-c";
+ Args[2] = Opts->Value.c_str();
+ Args[3] = 0;
+ execv(Args[0],(char **)Args);
+ _exit(100);
+ }
+ close(Pipes[0]);
+ FILE *F = fdopen(Pipes[1],"w");
+ if (F == 0)
+ return _error->Errno("fdopen","Faild to open new FD");
+
+ // Feed it the filenames.
+ bool Die = false;
+ if (Version <= 1)
+ {
+ for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
+ {
+ // Only deal with packages to be installed from .deb
+ if (I->Op != Item::Install)
+ continue;
+
+ // No errors here..
+ if (I->File[0] != '/')
+ continue;
+
+ /* Feed the filename of each package that is pending install
+ into the pipe. */
+ fprintf(F,"%s\n",I->File.c_str());
+ if (ferror(F) != 0)
+ {
+ Die = true;
+ break;
+ }
+ }
+ }
+ else
+ Die = !SendV2Pkgs(F);
+
+ fclose(F);
+ if (Die == true)
+ {
+ kill(Process,SIGINT);
+ ExecWait(Process,Opts->Value.c_str(),true);
+ return _error->Error("Failure running script %s",Opts->Value.c_str());
+ }
+
+ // Clean up the sub process
+ if (ExecWait(Process,Opts->Value.c_str()) == false)
+ return _error->Error("Failure running script %s",Opts->Value.c_str());
+ }
+
+ return true;
+}
+