]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
Newer sub/guess scripts
[apt.git] / apt-pkg / deb / dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dpkgpm.cc,v 1.19 2001/02/22 06:26:27 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 #include <apt-pkg/depcache.h>
18 #include <apt-pkg/strutl.h>
19
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>
27 #include <stdio.h>
28 /*}}}*/
29
30 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
32 /* */
33 pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache)
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 */
71 bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
72 {
73 if (Pkg.end() == true)
74 return false;
75
76 if (Purge == true)
77 List.push_back(Item(Item::Purge,Pkg));
78 else
79 List.push_back(Item(Item::Remove,Pkg));
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
95 pid_t Child = ExecFork();
96
97 // This is the child
98 if (Child == 0)
99 {
100 if (chdir("/tmp/") != 0)
101 _exit(100);
102
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);
127
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--);
136 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
137 }
138
139 return _error->Error("Sub-process returned an error code");
140 }
141
142 return true;
143 }
144
145 /*}}}*/
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 /*}}}*/
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;
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 OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
254
255 unsigned int Version = _config->FindI(OptSec+"::Version",1);
256
257 // Create the pipes
258 int Pipes[2];
259 if (pipe(Pipes) != 0)
260 return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
261 SetCloseExec(Pipes[0],true);
262 SetCloseExec(Pipes[1],true);
263
264 // Purified Fork for running the script
265 pid_t Process = ExecFork();
266 if (Process == 0)
267 {
268 // Setup the FDs
269 dup2(Pipes[0],STDIN_FILENO);
270 SetCloseExec(STDOUT_FILENO,false);
271 SetCloseExec(STDIN_FILENO,false);
272 SetCloseExec(STDERR_FILENO,false);
273
274 const char *Args[4];
275 Args[0] = "/bin/sh";
276 Args[1] = "-c";
277 Args[2] = Opts->Value.c_str();
278 Args[3] = 0;
279 execv(Args[0],(char **)Args);
280 _exit(100);
281 }
282 close(Pipes[0]);
283 FILE *F = fdopen(Pipes[1],"w");
284 if (F == 0)
285 return _error->Errno("fdopen","Faild to open new FD");
286
287 // Feed it the filenames.
288 bool Die = false;
289 if (Version <= 1)
290 {
291 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
292 {
293 // Only deal with packages to be installed from .deb
294 if (I->Op != Item::Install)
295 continue;
296
297 // No errors here..
298 if (I->File[0] != '/')
299 continue;
300
301 /* Feed the filename of each package that is pending install
302 into the pipe. */
303 fprintf(F,"%s\n",I->File.c_str());
304 if (ferror(F) != 0)
305 {
306 Die = true;
307 break;
308 }
309 }
310 }
311 else
312 Die = !SendV2Pkgs(F);
313
314 fclose(F);
315 if (Die == true)
316 {
317 kill(Process,SIGINT);
318 ExecWait(Process,Opts->Value.c_str(),true);
319 return _error->Error("Failure running script %s",Opts->Value.c_str());
320 }
321
322 // Clean up the sub process
323 if (ExecWait(Process,Opts->Value.c_str()) == false)
324 return _error->Error("Failure running script %s",Opts->Value.c_str());
325 }
326
327 return true;
328 }
329
330 /*}}}*/
331 // DPkgPM::Go - Run the sequence /*{{{*/
332 // ---------------------------------------------------------------------
333 /* This globs the operations and calls dpkg */
334 bool pkgDPkgPM::Go()
335 {
336 if (RunScripts("DPkg::Pre-Invoke") == false)
337 return false;
338
339 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
340 return false;
341
342 for (vector<Item>::iterator I = List.begin(); I != List.end();)
343 {
344 vector<Item>::iterator J = I;
345 for (; J != List.end() && J->Op == I->Op; J++);
346
347 // Generate the argument list
348 const char *Args[400];
349 if (J - I > 350)
350 J = I + 350;
351
352 unsigned int n = 0;
353 unsigned long Size = 0;
354 Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str();
355 Size += strlen(Args[n-1]);
356
357 // Stick in any custom dpkg options
358 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
359 if (Opts != 0)
360 {
361 Opts = Opts->Child;
362 for (; Opts != 0; Opts = Opts->Next)
363 {
364 if (Opts->Value.empty() == true)
365 continue;
366 Args[n++] = Opts->Value.c_str();
367 Size += Opts->Value.length();
368 }
369 }
370
371 switch (I->Op)
372 {
373 case Item::Remove:
374 Args[n++] = "--force-depends";
375 Size += strlen(Args[n-1]);
376 Args[n++] = "--force-remove-essential";
377 Size += strlen(Args[n-1]);
378 Args[n++] = "--remove";
379 Size += strlen(Args[n-1]);
380 break;
381
382 case Item::Purge:
383 Args[n++] = "--force-depends";
384 Size += strlen(Args[n-1]);
385 Args[n++] = "--force-remove-essential";
386 Size += strlen(Args[n-1]);
387 Args[n++] = "--purge";
388 Size += strlen(Args[n-1]);
389 break;
390
391 case Item::Configure:
392 Args[n++] = "--configure";
393 Size += strlen(Args[n-1]);
394 break;
395
396 case Item::Install:
397 Args[n++] = "--unpack";
398 Size += strlen(Args[n-1]);
399 break;
400 }
401
402 // Write in the file or package names
403 if (I->Op == Item::Install)
404 {
405 for (;I != J && Size < 1024; I++)
406 {
407 if (I->File[0] != '/')
408 return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
409 Args[n++] = I->File.c_str();
410 Size += strlen(Args[n-1]);
411 }
412 }
413 else
414 {
415 for (;I != J && Size < 1024; I++)
416 {
417 Args[n++] = I->Pkg.Name();
418 Size += strlen(Args[n-1]);
419 }
420 }
421 Args[n] = 0;
422 J = I;
423
424 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
425 {
426 for (unsigned int k = 0; k != n; k++)
427 clog << Args[k] << ' ';
428 clog << endl;
429 continue;
430 }
431
432 cout << flush;
433 clog << flush;
434 cerr << flush;
435
436 /* Mask off sig int/quit. We do this because dpkg also does when
437 it forks scripts. What happens is that when you hit ctrl-c it sends
438 it to all processes in the group. Since dpkg ignores the signal
439 it doesn't die but we do! So we must also ignore it */
440 signal(SIGQUIT,SIG_IGN);
441 signal(SIGINT,SIG_IGN);
442
443 // Fork dpkg
444 pid_t Child = ExecFork();
445
446 // This is the child
447 if (Child == 0)
448 {
449 if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
450 _exit(100);
451
452 if (_config->FindB("DPkg::FlushSTDIN",true) == true)
453 {
454 int Flags,dummy;
455 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
456 _exit(100);
457
458 // Discard everything in stdin before forking dpkg
459 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
460 _exit(100);
461
462 while (read(STDIN_FILENO,&dummy,1) == 1);
463
464 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
465 _exit(100);
466 }
467
468 /* No Job Control Stop Env is a magic dpkg var that prevents it
469 from using sigstop */
470 putenv("DPKG_NO_TSTP=yes");
471 execvp(Args[0],(char **)Args);
472 cerr << "Could not exec dpkg!" << endl;
473 _exit(100);
474 }
475
476 // Wait for dpkg
477 int Status = 0;
478 while (waitpid(Child,&Status,0) != Child)
479 {
480 if (errno == EINTR)
481 continue;
482 RunScripts("DPkg::Post-Invoke");
483 return _error->Errno("waitpid","Couldn't wait for subprocess");
484 }
485
486 // Restore sig int/quit
487 signal(SIGQUIT,SIG_DFL);
488 signal(SIGINT,SIG_DFL);
489
490 // Check for an error code.
491 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
492 {
493 RunScripts("DPkg::Post-Invoke");
494 if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
495 return _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
496
497 if (WIFEXITED(Status) != 0)
498 return _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
499
500 return _error->Error("Sub-process %s exited unexpectedly",Args[0]);
501 }
502 }
503
504 if (RunScripts("DPkg::Post-Invoke") == false)
505 return false;
506 return true;
507 }
508 /*}}}*/
509 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
510 // ---------------------------------------------------------------------
511 /* */
512 void pkgDPkgPM::Reset()
513 {
514 List.erase(List.begin(),List.end());
515 }
516 /*}}}*/