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