]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.12 1999/07/26 17:46:08 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>
27 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
30 pkgDPkgPM::pkgDPkgPM(pkgDepCache
&Cache
) : pkgPackageManager(Cache
)
34 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
35 // ---------------------------------------------------------------------
37 pkgDPkgPM::~pkgDPkgPM()
41 // DPkgPM::Install - Install a package /*{{{*/
42 // ---------------------------------------------------------------------
43 /* Add an install operation to the sequence list */
44 bool pkgDPkgPM::Install(PkgIterator Pkg
,string File
)
46 if (File
.empty() == true || Pkg
.end() == true)
47 return _error
->Error("Internal Error, No file name for %s",Pkg
.Name());
49 List
.push_back(Item(Item::Install
,Pkg
,File
));
53 // DPkgPM::Configure - Configure a package /*{{{*/
54 // ---------------------------------------------------------------------
55 /* Add a configure operation to the sequence list */
56 bool pkgDPkgPM::Configure(PkgIterator Pkg
)
58 if (Pkg
.end() == true)
61 List
.push_back(Item(Item::Configure
,Pkg
));
65 // DPkgPM::Remove - Remove a package /*{{{*/
66 // ---------------------------------------------------------------------
67 /* Add a remove operation to the sequence list */
68 bool pkgDPkgPM::Remove(PkgIterator Pkg
,bool Purge
)
70 if (Pkg
.end() == true)
74 List
.push_back(Item(Item::Purge
,Pkg
));
76 List
.push_back(Item(Item::Remove
,Pkg
));
80 // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
81 // ---------------------------------------------------------------------
82 /* This looks for a list of script sto run from the configuration file,
83 each one is run with system from a forked child. */
84 bool pkgDPkgPM::RunScripts(const char *Cnf
)
86 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
87 if (Opts
== 0 || Opts
->Child
== 0)
91 // Fork for running the system calls
92 pid_t Child
= ExecFork();
97 if (chdir("/tmp/") != 0)
100 unsigned int Count
= 1;
101 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
103 if (Opts
->Value
.empty() == true)
106 if (system(Opts
->Value
.c_str()) != 0)
112 // Wait for the child
114 while (waitpid(Child
,&Status
,0) != Child
)
118 return _error
->Errno("waitpid","Couldn't wait for subprocess");
121 // Restore sig int/quit
122 signal(SIGQUIT
,SIG_DFL
);
123 signal(SIGINT
,SIG_DFL
);
125 // Check for an error code.
126 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
128 unsigned int Count
= WEXITSTATUS(Status
);
132 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
133 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
136 return _error
->Error("Sub-process returned an error code");
142 // DPkgPM::Go - Run the sequence /*{{{*/
143 // ---------------------------------------------------------------------
144 /* This globs the operations and calls dpkg */
147 if (RunScripts("DPkg::Pre-Invoke") == false)
150 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
152 vector
<Item
>::iterator J
= I
;
153 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
155 // Generate the argument list
156 const char *Args
[400];
161 unsigned long Size
= 0;
162 Args
[n
++] = _config
->Find("Dir::Bin::dpkg","dpkg").c_str();
163 Size
+= strlen(Args
[n
-1]);
165 // Stick in any custom dpkg options
166 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
170 for (; Opts
!= 0; Opts
= Opts
->Next
)
172 if (Opts
->Value
.empty() == true)
174 Args
[n
++] = Opts
->Value
.c_str();
175 Size
+= Opts
->Value
.length();
182 Args
[n
++] = "--force-depends";
183 Size
+= strlen(Args
[n
-1]);
184 Args
[n
++] = "--force-remove-essential";
185 Size
+= strlen(Args
[n
-1]);
186 Args
[n
++] = "--remove";
187 Size
+= strlen(Args
[n
-1]);
191 Args
[n
++] = "--force-depends";
192 Size
+= strlen(Args
[n
-1]);
193 Args
[n
++] = "--force-remove-essential";
194 Size
+= strlen(Args
[n
-1]);
195 Args
[n
++] = "--purge";
196 Size
+= strlen(Args
[n
-1]);
199 case Item::Configure
:
200 Args
[n
++] = "--configure";
201 Size
+= strlen(Args
[n
-1]);
205 Args
[n
++] = "--unpack";
206 Size
+= strlen(Args
[n
-1]);
210 // Write in the file or package names
211 if (I
->Op
== Item::Install
)
213 for (;I
!= J
&& Size
< 1024; I
++)
215 if (I
->File
[0] != '/')
216 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
217 Args
[n
++] = I
->File
.c_str();
218 Size
+= strlen(Args
[n
-1]);
223 for (;I
!= J
&& Size
< 1024; I
++)
225 Args
[n
++] = I
->Pkg
.Name();
226 Size
+= strlen(Args
[n
-1]);
232 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
234 for (unsigned int k
= 0; k
!= n
; k
++)
235 clog
<< Args
[k
] << ' ';
244 /* Mask off sig int/quit. We do this because dpkg also does when
245 it forks scripts. What happens is that when you hit ctrl-c it sends
246 it to all processes in the group. Since dpkg ignores the signal
247 it doesn't die but we do! So we must also ignore it */
248 signal(SIGQUIT
,SIG_IGN
);
249 signal(SIGINT
,SIG_IGN
);
252 pid_t Child
= ExecFork();
257 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
261 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
264 // Discard everything in stdin before forking dpkg
265 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
268 while (read(STDIN_FILENO
,&dummy
,1) == 1);
270 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
273 /* No Job Control Stop Env is a magic dpkg var that prevents it
274 from using sigstop */
275 setenv("DPKG_NO_TSTP","yes",1);
276 execvp(Args
[0],(char **)Args
);
277 cerr
<< "Could not exec dpkg!" << endl
;
283 while (waitpid(Child
,&Status
,0) != Child
)
287 RunScripts("DPkg::Post-Invoke");
288 return _error
->Errno("waitpid","Couldn't wait for subprocess");
291 // Restore sig int/quit
292 signal(SIGQUIT
,SIG_DFL
);
293 signal(SIGINT
,SIG_DFL
);
295 // Check for an error code.
296 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
298 RunScripts("DPkg::Post-Invoke");
299 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
300 return _error
->Error("Sub-process recieved a segmentation fault.");
302 if (WIFEXITED(Status
) != 0)
303 return _error
->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status
));
305 return _error
->Error("Sub-process exited unexpectedly");
309 if (RunScripts("DPkg::Post-Invoke") == false)
314 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
315 // ---------------------------------------------------------------------
317 void pkgDPkgPM::Reset()
319 List
.erase(List
.begin(),List
.end());