]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.15 1999/12/10 06:30:42 jgg Exp $
4 /* ######################################################################
6 DPKG Package Manager - Provide an interface to dpkg
8 ##################################################################### */
12 #pragma implementation "apt-pkg/dpkgpm.h"
14 #include <apt-pkg/dpkgpm.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
21 #include <sys/types.h>
28 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
29 // ---------------------------------------------------------------------
31 pkgDPkgPM::pkgDPkgPM(pkgDepCache
&Cache
) : pkgPackageManager(Cache
)
35 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
36 // ---------------------------------------------------------------------
38 pkgDPkgPM::~pkgDPkgPM()
42 // DPkgPM::Install - Install a package /*{{{*/
43 // ---------------------------------------------------------------------
44 /* Add an install operation to the sequence list */
45 bool pkgDPkgPM::Install(PkgIterator Pkg
,string File
)
47 if (File
.empty() == true || Pkg
.end() == true)
48 return _error
->Error("Internal Error, No file name for %s",Pkg
.Name());
50 List
.push_back(Item(Item::Install
,Pkg
,File
));
54 // DPkgPM::Configure - Configure a package /*{{{*/
55 // ---------------------------------------------------------------------
56 /* Add a configure operation to the sequence list */
57 bool pkgDPkgPM::Configure(PkgIterator Pkg
)
59 if (Pkg
.end() == true)
62 List
.push_back(Item(Item::Configure
,Pkg
));
66 // DPkgPM::Remove - Remove a package /*{{{*/
67 // ---------------------------------------------------------------------
68 /* Add a remove operation to the sequence list */
69 bool pkgDPkgPM::Remove(PkgIterator Pkg
,bool Purge
)
71 if (Pkg
.end() == true)
75 List
.push_back(Item(Item::Purge
,Pkg
));
77 List
.push_back(Item(Item::Remove
,Pkg
));
81 // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
82 // ---------------------------------------------------------------------
83 /* This looks for a list of script sto run from the configuration file,
84 each one is run with system from a forked child. */
85 bool pkgDPkgPM::RunScripts(const char *Cnf
)
87 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
88 if (Opts
== 0 || Opts
->Child
== 0)
92 // Fork for running the system calls
93 pid_t Child
= ExecFork();
98 if (chdir("/tmp/") != 0)
101 unsigned int Count
= 1;
102 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
104 if (Opts
->Value
.empty() == true)
107 if (system(Opts
->Value
.c_str()) != 0)
113 // Wait for the child
115 while (waitpid(Child
,&Status
,0) != Child
)
119 return _error
->Errno("waitpid","Couldn't wait for subprocess");
122 // Restore sig int/quit
123 signal(SIGQUIT
,SIG_DFL
);
124 signal(SIGINT
,SIG_DFL
);
126 // Check for an error code.
127 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
129 unsigned int Count
= WEXITSTATUS(Status
);
133 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
134 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
137 return _error
->Error("Sub-process returned an error code");
144 // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
145 // ---------------------------------------------------------------------
146 /* This looks for a list of scripts to run from the configuration file
147 each one is run and is fed on standard input a list of all .deb files
148 that are due to be installed. */
149 bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf
)
151 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
152 if (Opts
== 0 || Opts
->Child
== 0)
156 unsigned int Count
= 1;
157 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
159 if (Opts
->Value
.empty() == true)
164 if (pipe(Pipes
) != 0)
165 return _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
166 SetCloseExec(Pipes
[0],true);
167 SetCloseExec(Pipes
[1],true);
169 // Purified Fork for running the script
170 pid_t Process
= ExecFork();
174 dup2(Pipes
[0],STDIN_FILENO
);
175 SetCloseExec(STDOUT_FILENO
,false);
176 SetCloseExec(STDIN_FILENO
,false);
177 SetCloseExec(STDERR_FILENO
,false);
182 Args
[2] = Opts
->Value
.c_str();
184 execv(Args
[0],(char **)Args
);
190 // Feed it the filenames.
191 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
193 // Only deal with packages to be installed from .deb
194 if (I
->Op
!= Item::Install
)
198 if (I
->File
[0] != '/')
201 /* Feed the filename of each package that is pending install
203 if (Fd
.Write(I
->File
.begin(),I
->File
.length()) == false ||
204 Fd
.Write("\n",1) == false)
206 kill(Process
,SIGINT
);
208 ExecWait(Process
,Opts
->Value
.c_str(),true);
209 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
214 // Clean up the sub process
215 if (ExecWait(Process
,Opts
->Value
.c_str()) == false)
216 return _error
->Error("Failure running script %s",Opts
->Value
.c_str());
223 // DPkgPM::Go - Run the sequence /*{{{*/
224 // ---------------------------------------------------------------------
225 /* This globs the operations and calls dpkg */
228 if (RunScripts("DPkg::Pre-Invoke") == false)
231 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
234 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
236 vector
<Item
>::iterator J
= I
;
237 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
239 // Generate the argument list
240 const char *Args
[400];
245 unsigned long Size
= 0;
246 Args
[n
++] = _config
->Find("Dir::Bin::dpkg","dpkg").c_str();
247 Size
+= strlen(Args
[n
-1]);
249 // Stick in any custom dpkg options
250 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
254 for (; Opts
!= 0; Opts
= Opts
->Next
)
256 if (Opts
->Value
.empty() == true)
258 Args
[n
++] = Opts
->Value
.c_str();
259 Size
+= Opts
->Value
.length();
266 Args
[n
++] = "--force-depends";
267 Size
+= strlen(Args
[n
-1]);
268 Args
[n
++] = "--force-remove-essential";
269 Size
+= strlen(Args
[n
-1]);
270 Args
[n
++] = "--remove";
271 Size
+= strlen(Args
[n
-1]);
275 Args
[n
++] = "--force-depends";
276 Size
+= strlen(Args
[n
-1]);
277 Args
[n
++] = "--force-remove-essential";
278 Size
+= strlen(Args
[n
-1]);
279 Args
[n
++] = "--purge";
280 Size
+= strlen(Args
[n
-1]);
283 case Item::Configure
:
284 Args
[n
++] = "--configure";
285 Size
+= strlen(Args
[n
-1]);
289 Args
[n
++] = "--unpack";
290 Size
+= strlen(Args
[n
-1]);
294 // Write in the file or package names
295 if (I
->Op
== Item::Install
)
297 for (;I
!= J
&& Size
< 1024; I
++)
299 if (I
->File
[0] != '/')
300 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
301 Args
[n
++] = I
->File
.c_str();
302 Size
+= strlen(Args
[n
-1]);
307 for (;I
!= J
&& Size
< 1024; I
++)
309 Args
[n
++] = I
->Pkg
.Name();
310 Size
+= strlen(Args
[n
-1]);
316 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
318 for (unsigned int k
= 0; k
!= n
; k
++)
319 clog
<< Args
[k
] << ' ';
328 /* Mask off sig int/quit. We do this because dpkg also does when
329 it forks scripts. What happens is that when you hit ctrl-c it sends
330 it to all processes in the group. Since dpkg ignores the signal
331 it doesn't die but we do! So we must also ignore it */
332 signal(SIGQUIT
,SIG_IGN
);
333 signal(SIGINT
,SIG_IGN
);
336 pid_t Child
= ExecFork();
341 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
345 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
348 // Discard everything in stdin before forking dpkg
349 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
352 while (read(STDIN_FILENO
,&dummy
,1) == 1);
354 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
357 /* No Job Control Stop Env is a magic dpkg var that prevents it
358 from using sigstop */
359 putenv("DPKG_NO_TSTP=yes");
360 execvp(Args
[0],(char **)Args
);
361 cerr
<< "Could not exec dpkg!" << endl
;
367 while (waitpid(Child
,&Status
,0) != Child
)
371 RunScripts("DPkg::Post-Invoke");
372 return _error
->Errno("waitpid","Couldn't wait for subprocess");
375 // Restore sig int/quit
376 signal(SIGQUIT
,SIG_DFL
);
377 signal(SIGINT
,SIG_DFL
);
379 // Check for an error code.
380 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
382 RunScripts("DPkg::Post-Invoke");
383 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
384 return _error
->Error("Sub-process recieved a segmentation fault.");
386 if (WIFEXITED(Status
) != 0)
387 return _error
->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status
));
389 return _error
->Error("Sub-process exited unexpectedly");
393 if (RunScripts("DPkg::Post-Invoke") == false)
398 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
399 // ---------------------------------------------------------------------
401 void pkgDPkgPM::Reset()
403 List
.erase(List
.begin(),List
.end());