]>
Commit | Line | Data |
---|---|---|
03e39e59 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b2e465d6 | 3 | // $Id: dpkgpm.cc,v 1.18 2001/02/20 07:03:17 jgg Exp $ |
03e39e59 AL |
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> | |
b2e465d6 AL |
17 | #include <apt-pkg/depcache.h> |
18 | #include <apt-pkg/strutl.h> | |
19 | ||
03e39e59 AL |
20 | #include <unistd.h> |
21 | #include <stdlib.h> | |
22 | #include <fcntl.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/wait.h> | |
25 | #include <signal.h> | |
26 | #include <errno.h> | |
db0c350f | 27 | #include <stdio.h> |
03e39e59 AL |
28 | /*}}}*/ |
29 | ||
30 | // DPkgPM::pkgDPkgPM - Constructor /*{{{*/ | |
31 | // --------------------------------------------------------------------- | |
32 | /* */ | |
b2e465d6 | 33 | pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache) |
03e39e59 AL |
34 | { |
35 | } | |
36 | /*}}}*/ | |
37 | // DPkgPM::pkgDPkgPM - Destructor /*{{{*/ | |
38 | // --------------------------------------------------------------------- | |
39 | /* */ | |
40 | pkgDPkgPM::~pkgDPkgPM() | |
41 | { | |
42 | } | |
43 | /*}}}*/ | |
44 | // DPkgPM::Install - Install a package /*{{{*/ | |
45 | // --------------------------------------------------------------------- | |
46 | /* Add an install operation to the sequence list */ | |
47 | bool pkgDPkgPM::Install(PkgIterator Pkg,string File) | |
48 | { | |
49 | if (File.empty() == true || Pkg.end() == true) | |
50 | return _error->Error("Internal Error, No file name for %s",Pkg.Name()); | |
51 | ||
52 | List.push_back(Item(Item::Install,Pkg,File)); | |
53 | return true; | |
54 | } | |
55 | /*}}}*/ | |
56 | // DPkgPM::Configure - Configure a package /*{{{*/ | |
57 | // --------------------------------------------------------------------- | |
58 | /* Add a configure operation to the sequence list */ | |
59 | bool pkgDPkgPM::Configure(PkgIterator Pkg) | |
60 | { | |
61 | if (Pkg.end() == true) | |
62 | return false; | |
63 | ||
64 | List.push_back(Item(Item::Configure,Pkg)); | |
65 | return true; | |
66 | } | |
67 | /*}}}*/ | |
68 | // DPkgPM::Remove - Remove a package /*{{{*/ | |
69 | // --------------------------------------------------------------------- | |
70 | /* Add a remove operation to the sequence list */ | |
fc4b5c9f | 71 | bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge) |
03e39e59 AL |
72 | { |
73 | if (Pkg.end() == true) | |
74 | return false; | |
75 | ||
fc4b5c9f AL |
76 | if (Purge == true) |
77 | List.push_back(Item(Item::Purge,Pkg)); | |
78 | else | |
79 | List.push_back(Item(Item::Remove,Pkg)); | |
6dd55be7 AL |
80 | return true; |
81 | } | |
82 | /*}}}*/ | |
83 | // DPkgPM::RunScripts - Run a set of scripts /*{{{*/ | |
84 | // --------------------------------------------------------------------- | |
85 | /* This looks for a list of script sto run from the configuration file, | |
86 | each one is run with system from a forked child. */ | |
87 | bool pkgDPkgPM::RunScripts(const char *Cnf) | |
88 | { | |
89 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
90 | if (Opts == 0 || Opts->Child == 0) | |
91 | return true; | |
92 | Opts = Opts->Child; | |
93 | ||
94 | // Fork for running the system calls | |
54676e1a | 95 | pid_t Child = ExecFork(); |
6dd55be7 AL |
96 | |
97 | // This is the child | |
98 | if (Child == 0) | |
99 | { | |
6dd55be7 AL |
100 | if (chdir("/tmp/") != 0) |
101 | _exit(100); | |
102 | ||
6dd55be7 AL |
103 | unsigned int Count = 1; |
104 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
105 | { | |
106 | if (Opts->Value.empty() == true) | |
107 | continue; | |
108 | ||
109 | if (system(Opts->Value.c_str()) != 0) | |
110 | _exit(100+Count); | |
111 | } | |
112 | _exit(0); | |
113 | } | |
114 | ||
115 | // Wait for the child | |
116 | int Status = 0; | |
117 | while (waitpid(Child,&Status,0) != Child) | |
118 | { | |
119 | if (errno == EINTR) | |
120 | continue; | |
121 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
122 | } | |
123 | ||
124 | // Restore sig int/quit | |
125 | signal(SIGQUIT,SIG_DFL); | |
126 | signal(SIGINT,SIG_DFL); | |
ddc1d8d0 | 127 | |
6dd55be7 AL |
128 | // Check for an error code. |
129 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
130 | { | |
131 | unsigned int Count = WEXITSTATUS(Status); | |
132 | if (Count > 100) | |
133 | { | |
134 | Count -= 100; | |
135 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
cf544e14 | 136 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); |
6dd55be7 AL |
137 | } |
138 | ||
139 | return _error->Error("Sub-process returned an error code"); | |
140 | } | |
141 | ||
03e39e59 AL |
142 | return true; |
143 | } | |
db0c350f AL |
144 | |
145 | /*}}}*/ | |
b2e465d6 AL |
146 | // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/ |
147 | // --------------------------------------------------------------------- | |
148 | /* This is part of the helper script communication interface, it sends | |
149 | very complete information down to the other end of the pipe.*/ | |
150 | bool pkgDPkgPM::SendV2Pkgs(FILE *F) | |
151 | { | |
152 | fprintf(F,"VERSION 2\n"); | |
153 | ||
154 | /* Write out all of the configuration directives by walking the | |
155 | configuration tree */ | |
156 | const Configuration::Item *Top = _config->Tree(0); | |
157 | for (; Top != 0;) | |
158 | { | |
159 | if (Top->Value.empty() == false) | |
160 | { | |
161 | fprintf(F,"%s=%s\n", | |
162 | QuoteString(Top->FullTag(),"=\"\n").c_str(), | |
163 | QuoteString(Top->Value,"\n").c_str()); | |
164 | } | |
165 | ||
166 | if (Top->Child != 0) | |
167 | { | |
168 | Top = Top->Child; | |
169 | continue; | |
170 | } | |
171 | ||
172 | while (Top != 0 && Top->Next == 0) | |
173 | Top = Top->Parent; | |
174 | if (Top != 0) | |
175 | Top = Top->Next; | |
176 | } | |
177 | fprintf(F,"\n"); | |
178 | ||
179 | // Write out the package actions in order. | |
180 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) | |
181 | { | |
182 | pkgDepCache::StateCache &S = Cache[I->Pkg]; | |
183 | ||
184 | fprintf(F,"%s ",I->Pkg.Name()); | |
185 | // Current version | |
186 | if (I->Pkg->CurrentVer == 0) | |
187 | fprintf(F,"- "); | |
188 | else | |
189 | fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr()); | |
190 | ||
191 | // Show the compare operator | |
192 | // Target version | |
193 | if (S.InstallVer != 0) | |
194 | { | |
195 | int Comp = 2; | |
196 | if (I->Pkg->CurrentVer != 0) | |
197 | Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer()); | |
198 | if (Comp < 0) | |
199 | fprintf(F,"> "); | |
200 | if (Comp == 0) | |
201 | fprintf(F,"= "); | |
202 | if (Comp > 0) | |
203 | fprintf(F,"< "); | |
204 | fprintf(F,"%s ",S.InstVerIter(Cache).VerStr()); | |
205 | } | |
206 | else | |
207 | fprintf(F,"> - "); | |
208 | ||
209 | // Show the filename/operation | |
210 | if (I->Op == Item::Install) | |
211 | { | |
212 | // No errors here.. | |
213 | if (I->File[0] != '/') | |
214 | fprintf(F,"**ERROR**\n"); | |
215 | else | |
216 | fprintf(F,"%s\n",I->File.c_str()); | |
217 | } | |
218 | if (I->Op == Item::Configure) | |
219 | fprintf(F,"**CONFIGURE**\n"); | |
220 | if (I->Op == Item::Remove || | |
221 | I->Op == Item::Purge) | |
222 | fprintf(F,"**REMOVE**\n"); | |
223 | ||
224 | if (ferror(F) != 0) | |
225 | return false; | |
226 | } | |
227 | return true; | |
228 | } | |
229 | /*}}}*/ | |
db0c350f AL |
230 | // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/ |
231 | // --------------------------------------------------------------------- | |
232 | /* This looks for a list of scripts to run from the configuration file | |
233 | each one is run and is fed on standard input a list of all .deb files | |
234 | that are due to be installed. */ | |
235 | bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf) | |
236 | { | |
237 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
238 | if (Opts == 0 || Opts->Child == 0) | |
239 | return true; | |
240 | Opts = Opts->Child; | |
241 | ||
242 | unsigned int Count = 1; | |
243 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
244 | { | |
245 | if (Opts->Value.empty() == true) | |
246 | continue; | |
b2e465d6 AL |
247 | |
248 | // Determine the protocol version | |
249 | string OptSec = Opts->Value; | |
250 | string::size_type Pos; | |
251 | if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0) | |
252 | Pos = OptSec.length(); | |
253 | else | |
254 | Pos--; | |
255 | OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos); | |
256 | ||
257 | unsigned int Version = _config->FindI(OptSec+"::Version",1); | |
258 | ||
db0c350f AL |
259 | // Create the pipes |
260 | int Pipes[2]; | |
261 | if (pipe(Pipes) != 0) | |
262 | return _error->Errno("pipe","Failed to create IPC pipe to subprocess"); | |
263 | SetCloseExec(Pipes[0],true); | |
264 | SetCloseExec(Pipes[1],true); | |
265 | ||
266 | // Purified Fork for running the script | |
267 | pid_t Process = ExecFork(); | |
268 | if (Process == 0) | |
269 | { | |
270 | // Setup the FDs | |
271 | dup2(Pipes[0],STDIN_FILENO); | |
272 | SetCloseExec(STDOUT_FILENO,false); | |
273 | SetCloseExec(STDIN_FILENO,false); | |
274 | SetCloseExec(STDERR_FILENO,false); | |
90ecbd7d AL |
275 | |
276 | const char *Args[4]; | |
db0c350f | 277 | Args[0] = "/bin/sh"; |
90ecbd7d AL |
278 | Args[1] = "-c"; |
279 | Args[2] = Opts->Value.c_str(); | |
280 | Args[3] = 0; | |
db0c350f AL |
281 | execv(Args[0],(char **)Args); |
282 | _exit(100); | |
283 | } | |
284 | close(Pipes[0]); | |
b2e465d6 AL |
285 | FILE *F = fdopen(Pipes[1],"w"); |
286 | if (F == 0) | |
287 | return _error->Errno("fdopen","Faild to open new FD"); | |
288 | ||
db0c350f | 289 | // Feed it the filenames. |
b2e465d6 AL |
290 | bool Die = false; |
291 | if (Version <= 1) | |
db0c350f | 292 | { |
b2e465d6 | 293 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) |
db0c350f | 294 | { |
b2e465d6 AL |
295 | // Only deal with packages to be installed from .deb |
296 | if (I->Op != Item::Install) | |
297 | continue; | |
298 | ||
299 | // No errors here.. | |
300 | if (I->File[0] != '/') | |
301 | continue; | |
302 | ||
303 | /* Feed the filename of each package that is pending install | |
304 | into the pipe. */ | |
305 | fprintf(F,"%s\n",I->File.c_str()); | |
306 | if (ferror(F) != 0) | |
307 | { | |
308 | Die = true; | |
309 | break; | |
310 | } | |
90ecbd7d | 311 | } |
db0c350f | 312 | } |
b2e465d6 AL |
313 | else |
314 | Die = !SendV2Pkgs(F); | |
315 | ||
316 | fclose(F); | |
317 | if (Die == true) | |
318 | { | |
319 | kill(Process,SIGINT); | |
320 | ExecWait(Process,Opts->Value.c_str(),true); | |
321 | return _error->Error("Failure running script %s",Opts->Value.c_str()); | |
322 | } | |
db0c350f AL |
323 | |
324 | // Clean up the sub process | |
325 | if (ExecWait(Process,Opts->Value.c_str()) == false) | |
90ecbd7d | 326 | return _error->Error("Failure running script %s",Opts->Value.c_str()); |
db0c350f AL |
327 | } |
328 | ||
329 | return true; | |
330 | } | |
331 | ||
03e39e59 AL |
332 | /*}}}*/ |
333 | // DPkgPM::Go - Run the sequence /*{{{*/ | |
334 | // --------------------------------------------------------------------- | |
335 | /* This globs the operations and calls dpkg */ | |
336 | bool pkgDPkgPM::Go() | |
337 | { | |
6dd55be7 AL |
338 | if (RunScripts("DPkg::Pre-Invoke") == false) |
339 | return false; | |
db0c350f AL |
340 | |
341 | if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false) | |
342 | return false; | |
343 | ||
03e39e59 AL |
344 | for (vector<Item>::iterator I = List.begin(); I != List.end();) |
345 | { | |
346 | vector<Item>::iterator J = I; | |
347 | for (; J != List.end() && J->Op == I->Op; J++); | |
30e1eab5 | 348 | |
03e39e59 AL |
349 | // Generate the argument list |
350 | const char *Args[400]; | |
351 | if (J - I > 350) | |
352 | J = I + 350; | |
353 | ||
30e1eab5 AL |
354 | unsigned int n = 0; |
355 | unsigned long Size = 0; | |
356 | Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str(); | |
357 | Size += strlen(Args[n-1]); | |
03e39e59 | 358 | |
6dd55be7 AL |
359 | // Stick in any custom dpkg options |
360 | Configuration::Item const *Opts = _config->Tree("DPkg::Options"); | |
361 | if (Opts != 0) | |
362 | { | |
363 | Opts = Opts->Child; | |
364 | for (; Opts != 0; Opts = Opts->Next) | |
365 | { | |
366 | if (Opts->Value.empty() == true) | |
367 | continue; | |
368 | Args[n++] = Opts->Value.c_str(); | |
369 | Size += Opts->Value.length(); | |
370 | } | |
371 | } | |
372 | ||
03e39e59 AL |
373 | switch (I->Op) |
374 | { | |
375 | case Item::Remove: | |
376 | Args[n++] = "--force-depends"; | |
30e1eab5 | 377 | Size += strlen(Args[n-1]); |
03e39e59 | 378 | Args[n++] = "--force-remove-essential"; |
30e1eab5 | 379 | Size += strlen(Args[n-1]); |
03e39e59 | 380 | Args[n++] = "--remove"; |
30e1eab5 | 381 | Size += strlen(Args[n-1]); |
03e39e59 AL |
382 | break; |
383 | ||
fc4b5c9f AL |
384 | case Item::Purge: |
385 | Args[n++] = "--force-depends"; | |
386 | Size += strlen(Args[n-1]); | |
387 | Args[n++] = "--force-remove-essential"; | |
388 | Size += strlen(Args[n-1]); | |
389 | Args[n++] = "--purge"; | |
390 | Size += strlen(Args[n-1]); | |
391 | break; | |
392 | ||
03e39e59 AL |
393 | case Item::Configure: |
394 | Args[n++] = "--configure"; | |
30e1eab5 | 395 | Size += strlen(Args[n-1]); |
03e39e59 AL |
396 | break; |
397 | ||
398 | case Item::Install: | |
399 | Args[n++] = "--unpack"; | |
30e1eab5 | 400 | Size += strlen(Args[n-1]); |
03e39e59 AL |
401 | break; |
402 | } | |
403 | ||
404 | // Write in the file or package names | |
405 | if (I->Op == Item::Install) | |
30e1eab5 AL |
406 | { |
407 | for (;I != J && Size < 1024; I++) | |
408 | { | |
cf544e14 AL |
409 | if (I->File[0] != '/') |
410 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
03e39e59 | 411 | Args[n++] = I->File.c_str(); |
30e1eab5 AL |
412 | Size += strlen(Args[n-1]); |
413 | } | |
414 | } | |
03e39e59 | 415 | else |
30e1eab5 AL |
416 | { |
417 | for (;I != J && Size < 1024; I++) | |
418 | { | |
03e39e59 | 419 | Args[n++] = I->Pkg.Name(); |
30e1eab5 AL |
420 | Size += strlen(Args[n-1]); |
421 | } | |
422 | } | |
03e39e59 | 423 | Args[n] = 0; |
30e1eab5 AL |
424 | J = I; |
425 | ||
426 | if (_config->FindB("Debug::pkgDPkgPM",false) == true) | |
427 | { | |
428 | for (unsigned int k = 0; k != n; k++) | |
429 | clog << Args[k] << ' '; | |
430 | clog << endl; | |
431 | continue; | |
432 | } | |
03e39e59 | 433 | |
03e39e59 AL |
434 | cout << flush; |
435 | clog << flush; | |
436 | cerr << flush; | |
437 | ||
438 | /* Mask off sig int/quit. We do this because dpkg also does when | |
439 | it forks scripts. What happens is that when you hit ctrl-c it sends | |
440 | it to all processes in the group. Since dpkg ignores the signal | |
441 | it doesn't die but we do! So we must also ignore it */ | |
442 | signal(SIGQUIT,SIG_IGN); | |
443 | signal(SIGINT,SIG_IGN); | |
6dd55be7 | 444 | |
03e39e59 | 445 | // Fork dpkg |
54676e1a | 446 | pid_t Child = ExecFork(); |
6dd55be7 | 447 | |
03e39e59 AL |
448 | // This is the child |
449 | if (Child == 0) | |
450 | { | |
cf544e14 | 451 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 452 | _exit(100); |
03e39e59 | 453 | |
8b5fe26c AL |
454 | if (_config->FindB("DPkg::FlushSTDIN",true) == true) |
455 | { | |
456 | int Flags,dummy; | |
457 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
458 | _exit(100); | |
459 | ||
460 | // Discard everything in stdin before forking dpkg | |
461 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
462 | _exit(100); | |
463 | ||
464 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
465 | ||
466 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
467 | _exit(100); | |
468 | } | |
03e39e59 | 469 | |
03e39e59 AL |
470 | /* No Job Control Stop Env is a magic dpkg var that prevents it |
471 | from using sigstop */ | |
101030ab | 472 | putenv("DPKG_NO_TSTP=yes"); |
d568ed2d | 473 | execvp(Args[0],(char **)Args); |
03e39e59 | 474 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 475 | _exit(100); |
03e39e59 AL |
476 | } |
477 | ||
478 | // Wait for dpkg | |
479 | int Status = 0; | |
480 | while (waitpid(Child,&Status,0) != Child) | |
481 | { | |
482 | if (errno == EINTR) | |
483 | continue; | |
6dd55be7 | 484 | RunScripts("DPkg::Post-Invoke"); |
03e39e59 AL |
485 | return _error->Errno("waitpid","Couldn't wait for subprocess"); |
486 | } | |
03e39e59 AL |
487 | |
488 | // Restore sig int/quit | |
489 | signal(SIGQUIT,SIG_DFL); | |
490 | signal(SIGINT,SIG_DFL); | |
6dd55be7 AL |
491 | |
492 | // Check for an error code. | |
493 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
494 | { | |
495 | RunScripts("DPkg::Post-Invoke"); | |
f956efb4 | 496 | if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) |
b2e465d6 AL |
497 | return _error->Error("Sub-process %s received a segmentation fault.",Args[0]); |
498 | ||
f956efb4 | 499 | if (WIFEXITED(Status) != 0) |
0410b3d5 | 500 | return _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status)); |
f956efb4 | 501 | |
0410b3d5 | 502 | return _error->Error("Sub-process %s exited unexpectedly",Args[0]); |
6dd55be7 | 503 | } |
03e39e59 | 504 | } |
6dd55be7 AL |
505 | |
506 | if (RunScripts("DPkg::Post-Invoke") == false) | |
507 | return false; | |
03e39e59 AL |
508 | return true; |
509 | } | |
510 | /*}}}*/ | |
281daf46 AL |
511 | // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/ |
512 | // --------------------------------------------------------------------- | |
513 | /* */ | |
514 | void pkgDPkgPM::Reset() | |
515 | { | |
516 | List.erase(List.begin(),List.end()); | |
517 | } | |
518 | /*}}}*/ |