]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
new change log
[apt.git] / apt-pkg / deb / dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dpkgpm.cc,v 1.12 1999/07/26 17:46:08 jgg Exp $
4 /* ######################################################################
5
6 DPKG Package Manager - Provide an interface to dpkg
7
8 ##################################################################### */
9 /*}}}*/
10 // Includes /*{{{*/
11 #ifdef __GNUG__
12 #pragma implementation "apt-pkg/dpkgpm.h"
13 #endif
14 #include <apt-pkg/dpkgpm.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
17
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <signal.h>
24 #include <errno.h>
25 /*}}}*/
26
27 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 pkgDPkgPM::pkgDPkgPM(pkgDepCache &Cache) : pkgPackageManager(Cache)
31 {
32 }
33 /*}}}*/
34 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* */
37 pkgDPkgPM::~pkgDPkgPM()
38 {
39 }
40 /*}}}*/
41 // DPkgPM::Install - Install a package /*{{{*/
42 // ---------------------------------------------------------------------
43 /* Add an install operation to the sequence list */
44 bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
45 {
46 if (File.empty() == true || Pkg.end() == true)
47 return _error->Error("Internal Error, No file name for %s",Pkg.Name());
48
49 List.push_back(Item(Item::Install,Pkg,File));
50 return true;
51 }
52 /*}}}*/
53 // DPkgPM::Configure - Configure a package /*{{{*/
54 // ---------------------------------------------------------------------
55 /* Add a configure operation to the sequence list */
56 bool pkgDPkgPM::Configure(PkgIterator Pkg)
57 {
58 if (Pkg.end() == true)
59 return false;
60
61 List.push_back(Item(Item::Configure,Pkg));
62 return true;
63 }
64 /*}}}*/
65 // DPkgPM::Remove - Remove a package /*{{{*/
66 // ---------------------------------------------------------------------
67 /* Add a remove operation to the sequence list */
68 bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
69 {
70 if (Pkg.end() == true)
71 return false;
72
73 if (Purge == true)
74 List.push_back(Item(Item::Purge,Pkg));
75 else
76 List.push_back(Item(Item::Remove,Pkg));
77 return true;
78 }
79 /*}}}*/
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)
85 {
86 Configuration::Item const *Opts = _config->Tree(Cnf);
87 if (Opts == 0 || Opts->Child == 0)
88 return true;
89 Opts = Opts->Child;
90
91 // Fork for running the system calls
92 pid_t Child = ExecFork();
93
94 // This is the child
95 if (Child == 0)
96 {
97 if (chdir("/tmp/") != 0)
98 _exit(100);
99
100 unsigned int Count = 1;
101 for (; Opts != 0; Opts = Opts->Next, Count++)
102 {
103 if (Opts->Value.empty() == true)
104 continue;
105
106 if (system(Opts->Value.c_str()) != 0)
107 _exit(100+Count);
108 }
109 _exit(0);
110 }
111
112 // Wait for the child
113 int Status = 0;
114 while (waitpid(Child,&Status,0) != Child)
115 {
116 if (errno == EINTR)
117 continue;
118 return _error->Errno("waitpid","Couldn't wait for subprocess");
119 }
120
121 // Restore sig int/quit
122 signal(SIGQUIT,SIG_DFL);
123 signal(SIGINT,SIG_DFL);
124
125 // Check for an error code.
126 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
127 {
128 unsigned int Count = WEXITSTATUS(Status);
129 if (Count > 100)
130 {
131 Count -= 100;
132 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
133 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
134 }
135
136 return _error->Error("Sub-process returned an error code");
137 }
138
139 return true;
140 }
141 /*}}}*/
142 // DPkgPM::Go - Run the sequence /*{{{*/
143 // ---------------------------------------------------------------------
144 /* This globs the operations and calls dpkg */
145 bool pkgDPkgPM::Go()
146 {
147 if (RunScripts("DPkg::Pre-Invoke") == false)
148 return false;
149
150 for (vector<Item>::iterator I = List.begin(); I != List.end();)
151 {
152 vector<Item>::iterator J = I;
153 for (; J != List.end() && J->Op == I->Op; J++);
154
155 // Generate the argument list
156 const char *Args[400];
157 if (J - I > 350)
158 J = I + 350;
159
160 unsigned int n = 0;
161 unsigned long Size = 0;
162 Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str();
163 Size += strlen(Args[n-1]);
164
165 // Stick in any custom dpkg options
166 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
167 if (Opts != 0)
168 {
169 Opts = Opts->Child;
170 for (; Opts != 0; Opts = Opts->Next)
171 {
172 if (Opts->Value.empty() == true)
173 continue;
174 Args[n++] = Opts->Value.c_str();
175 Size += Opts->Value.length();
176 }
177 }
178
179 switch (I->Op)
180 {
181 case Item::Remove:
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]);
188 break;
189
190 case Item::Purge:
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]);
197 break;
198
199 case Item::Configure:
200 Args[n++] = "--configure";
201 Size += strlen(Args[n-1]);
202 break;
203
204 case Item::Install:
205 Args[n++] = "--unpack";
206 Size += strlen(Args[n-1]);
207 break;
208 }
209
210 // Write in the file or package names
211 if (I->Op == Item::Install)
212 {
213 for (;I != J && Size < 1024; I++)
214 {
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]);
219 }
220 }
221 else
222 {
223 for (;I != J && Size < 1024; I++)
224 {
225 Args[n++] = I->Pkg.Name();
226 Size += strlen(Args[n-1]);
227 }
228 }
229 Args[n] = 0;
230 J = I;
231
232 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
233 {
234 for (unsigned int k = 0; k != n; k++)
235 clog << Args[k] << ' ';
236 clog << endl;
237 continue;
238 }
239
240 cout << flush;
241 clog << flush;
242 cerr << flush;
243
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);
250
251 // Fork dpkg
252 pid_t Child = ExecFork();
253
254 // This is the child
255 if (Child == 0)
256 {
257 if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
258 _exit(100);
259
260 int Flags,dummy;
261 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
262 _exit(100);
263
264 // Discard everything in stdin before forking dpkg
265 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
266 _exit(100);
267
268 while (read(STDIN_FILENO,&dummy,1) == 1);
269
270 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
271 _exit(100);
272
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;
278 _exit(100);
279 }
280
281 // Wait for dpkg
282 int Status = 0;
283 while (waitpid(Child,&Status,0) != Child)
284 {
285 if (errno == EINTR)
286 continue;
287 RunScripts("DPkg::Post-Invoke");
288 return _error->Errno("waitpid","Couldn't wait for subprocess");
289 }
290
291 // Restore sig int/quit
292 signal(SIGQUIT,SIG_DFL);
293 signal(SIGINT,SIG_DFL);
294
295 // Check for an error code.
296 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
297 {
298 RunScripts("DPkg::Post-Invoke");
299 if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
300 return _error->Error("Sub-process recieved a segmentation fault.");
301
302 if (WIFEXITED(Status) != 0)
303 return _error->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status));
304
305 return _error->Error("Sub-process exited unexpectedly");
306 }
307 }
308
309 if (RunScripts("DPkg::Post-Invoke") == false)
310 return false;
311 return true;
312 }
313 /*}}}*/
314 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
315 // ---------------------------------------------------------------------
316 /* */
317 void pkgDPkgPM::Reset()
318 {
319 List.erase(List.begin(),List.end());
320 }
321 /*}}}*/