]>
git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.13 1999/07/30 06:15:14 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>
28 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
29 // ---------------------------------------------------------------------
31 pkgDPkgPM::pkgDPkgPM(pkgDepCache
&Cache
) : pkgPackageManager(Cache
)
35 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
36 // ---------------------------------------------------------------------
38 pkgDPkgPM::~pkgDPkgPM()
42 // DPkgPM::Install - Install a package /*{{{*/
43 // ---------------------------------------------------------------------
44 /* Add an install operation to the sequence list */
45 bool pkgDPkgPM::Install(PkgIterator Pkg
,string File
)
47 if (File
.empty() == true || Pkg
.end() == true)
48 return _error
->Error("Internal Error, No file name for %s",Pkg
.Name());
50 List
.push_back(Item(Item::Install
,Pkg
,File
));
54 // DPkgPM::Configure - Configure a package /*{{{*/
55 // ---------------------------------------------------------------------
56 /* Add a configure operation to the sequence list */
57 bool pkgDPkgPM::Configure(PkgIterator Pkg
)
59 if (Pkg
.end() == true)
62 List
.push_back(Item(Item::Configure
,Pkg
));
66 // DPkgPM::Remove - Remove a package /*{{{*/
67 // ---------------------------------------------------------------------
68 /* Add a remove operation to the sequence list */
69 bool pkgDPkgPM::Remove(PkgIterator Pkg
,bool Purge
)
71 if (Pkg
.end() == true)
75 List
.push_back(Item(Item::Purge
,Pkg
));
77 List
.push_back(Item(Item::Remove
,Pkg
));
81 // DPkgPM::RunScripts - Run a set of scripts /*{{{*/
82 // ---------------------------------------------------------------------
83 /* This looks for a list of script sto run from the configuration file,
84 each one is run with system from a forked child. */
85 bool pkgDPkgPM::RunScripts(const char *Cnf
)
87 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
88 if (Opts
== 0 || Opts
->Child
== 0)
92 // Fork for running the system calls
93 pid_t Child
= ExecFork();
98 if (chdir("/tmp/") != 0)
101 unsigned int Count
= 1;
102 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
104 if (Opts
->Value
.empty() == true)
107 if (system(Opts
->Value
.c_str()) != 0)
113 // Wait for the child
115 while (waitpid(Child
,&Status
,0) != Child
)
119 return _error
->Errno("waitpid","Couldn't wait for subprocess");
122 // Restore sig int/quit
123 signal(SIGQUIT
,SIG_DFL
);
124 signal(SIGINT
,SIG_DFL
);
126 // Check for an error code.
127 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
129 unsigned int Count
= WEXITSTATUS(Status
);
133 for (; Opts
!= 0 && Count
!= 1; Opts
= Opts
->Next
, Count
--);
134 _error
->Error("Problem executing scripts %s '%s'",Cnf
,Opts
->Value
.c_str());
137 return _error
->Error("Sub-process returned an error code");
144 // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
145 // ---------------------------------------------------------------------
146 /* This looks for a list of scripts to run from the configuration file
147 each one is run and is fed on standard input a list of all .deb files
148 that are due to be installed. */
149 bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf
)
151 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
152 if (Opts
== 0 || Opts
->Child
== 0)
156 unsigned int Count
= 1;
157 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
159 if (Opts
->Value
.empty() == true)
164 if (pipe(Pipes
) != 0)
165 return _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
166 SetCloseExec(Pipes
[0],true);
167 SetCloseExec(Pipes
[1],true);
169 // Purified Fork for running the script
170 pid_t Process
= ExecFork();
174 dup2(Pipes
[0],STDIN_FILENO
);
175 SetCloseExec(STDOUT_FILENO
,false);
176 SetCloseExec(STDIN_FILENO
,false);
177 SetCloseExec(STDERR_FILENO
,false);
183 Args
[3] = Opts
->Value
.c_str();
185 execv(Args
[0],(char **)Args
);
191 // Feed it the filenames.
192 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
194 // Only deal with packages to be installed from .deb
195 if (I
->Op
!= Item::Install
)
199 if (I
->File
[0] != '/')
202 /* Feed the filename of each package that is pending install
204 if (Fd
.Write(I
->File
.begin(),I
->File
.length()) == false ||
205 Fd
.Write("\n",1) == false)
207 kill(Process
,SIGINT
);
209 ExecWait(Process
,Opts
->Value
.c_str(),true);
215 // Clean up the sub process
216 if (ExecWait(Process
,Opts
->Value
.c_str()) == false)
224 // DPkgPM::Go - Run the sequence /*{{{*/
225 // ---------------------------------------------------------------------
226 /* This globs the operations and calls dpkg */
229 if (RunScripts("DPkg::Pre-Invoke") == false)
232 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
235 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end();)
237 vector
<Item
>::iterator J
= I
;
238 for (; J
!= List
.end() && J
->Op
== I
->Op
; J
++);
240 // Generate the argument list
241 const char *Args
[400];
246 unsigned long Size
= 0;
247 Args
[n
++] = _config
->Find("Dir::Bin::dpkg","dpkg").c_str();
248 Size
+= strlen(Args
[n
-1]);
250 // Stick in any custom dpkg options
251 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
255 for (; Opts
!= 0; Opts
= Opts
->Next
)
257 if (Opts
->Value
.empty() == true)
259 Args
[n
++] = Opts
->Value
.c_str();
260 Size
+= Opts
->Value
.length();
267 Args
[n
++] = "--force-depends";
268 Size
+= strlen(Args
[n
-1]);
269 Args
[n
++] = "--force-remove-essential";
270 Size
+= strlen(Args
[n
-1]);
271 Args
[n
++] = "--remove";
272 Size
+= strlen(Args
[n
-1]);
276 Args
[n
++] = "--force-depends";
277 Size
+= strlen(Args
[n
-1]);
278 Args
[n
++] = "--force-remove-essential";
279 Size
+= strlen(Args
[n
-1]);
280 Args
[n
++] = "--purge";
281 Size
+= strlen(Args
[n
-1]);
284 case Item::Configure
:
285 Args
[n
++] = "--configure";
286 Size
+= strlen(Args
[n
-1]);
290 Args
[n
++] = "--unpack";
291 Size
+= strlen(Args
[n
-1]);
295 // Write in the file or package names
296 if (I
->Op
== Item::Install
)
298 for (;I
!= J
&& Size
< 1024; I
++)
300 if (I
->File
[0] != '/')
301 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
302 Args
[n
++] = I
->File
.c_str();
303 Size
+= strlen(Args
[n
-1]);
308 for (;I
!= J
&& Size
< 1024; I
++)
310 Args
[n
++] = I
->Pkg
.Name();
311 Size
+= strlen(Args
[n
-1]);
317 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
319 for (unsigned int k
= 0; k
!= n
; k
++)
320 clog
<< Args
[k
] << ' ';
329 /* Mask off sig int/quit. We do this because dpkg also does when
330 it forks scripts. What happens is that when you hit ctrl-c it sends
331 it to all processes in the group. Since dpkg ignores the signal
332 it doesn't die but we do! So we must also ignore it */
333 signal(SIGQUIT
,SIG_IGN
);
334 signal(SIGINT
,SIG_IGN
);
337 pid_t Child
= ExecFork();
342 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
346 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
349 // Discard everything in stdin before forking dpkg
350 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
353 while (read(STDIN_FILENO
,&dummy
,1) == 1);
355 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
358 /* No Job Control Stop Env is a magic dpkg var that prevents it
359 from using sigstop */
360 setenv("DPKG_NO_TSTP","yes",1);
361 execvp(Args
[0],(char **)Args
);
362 cerr
<< "Could not exec dpkg!" << endl
;
368 while (waitpid(Child
,&Status
,0) != Child
)
372 RunScripts("DPkg::Post-Invoke");
373 return _error
->Errno("waitpid","Couldn't wait for subprocess");
376 // Restore sig int/quit
377 signal(SIGQUIT
,SIG_DFL
);
378 signal(SIGINT
,SIG_DFL
);
380 // Check for an error code.
381 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
383 RunScripts("DPkg::Post-Invoke");
384 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
385 return _error
->Error("Sub-process recieved a segmentation fault.");
387 if (WIFEXITED(Status
) != 0)
388 return _error
->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status
));
390 return _error
->Error("Sub-process exited unexpectedly");
394 if (RunScripts("DPkg::Post-Invoke") == false)
399 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
400 // ---------------------------------------------------------------------
402 void pkgDPkgPM::Reset()
404 List
.erase(List
.begin(),List
.end());