]> git.saurik.com Git - apt.git/blame - apt-pkg/deb/dpkgpm.cc
Hopefully fixed the scan bug
[apt.git] / apt-pkg / deb / dpkgpm.cc
CommitLineData
03e39e59
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
0dbb95d8 3// $Id: dpkgpm.cc,v 1.5 1998/12/14 06:54:44 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>
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/* */
30pkgDPkgPM::pkgDPkgPM(pkgDepCache &Cache) : pkgPackageManager(Cache)
31{
32}
33 /*}}}*/
34// DPkgPM::pkgDPkgPM - Destructor /*{{{*/
35// ---------------------------------------------------------------------
36/* */
37pkgDPkgPM::~pkgDPkgPM()
38{
39}
40 /*}}}*/
41// DPkgPM::Install - Install a package /*{{{*/
42// ---------------------------------------------------------------------
43/* Add an install operation to the sequence list */
44bool 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 */
56bool 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 */
68bool pkgDPkgPM::Remove(PkgIterator Pkg)
69{
70 if (Pkg.end() == true)
71 return false;
72
73 List.push_back(Item(Item::Remove,Pkg));
74 return true;
75}
76 /*}}}*/
77// DPkgPM::Go - Run the sequence /*{{{*/
78// ---------------------------------------------------------------------
79/* This globs the operations and calls dpkg */
80bool pkgDPkgPM::Go()
81{
82 for (vector<Item>::iterator I = List.begin(); I != List.end();)
83 {
84 vector<Item>::iterator J = I;
85 for (; J != List.end() && J->Op == I->Op; J++);
30e1eab5 86
03e39e59
AL
87 // Generate the argument list
88 const char *Args[400];
89 if (J - I > 350)
90 J = I + 350;
91
30e1eab5
AL
92 unsigned int n = 0;
93 unsigned long Size = 0;
94 Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str();
95 Size += strlen(Args[n-1]);
03e39e59
AL
96
97 switch (I->Op)
98 {
99 case Item::Remove:
100 Args[n++] = "--force-depends";
30e1eab5 101 Size += strlen(Args[n-1]);
03e39e59 102 Args[n++] = "--force-remove-essential";
30e1eab5 103 Size += strlen(Args[n-1]);
03e39e59 104 Args[n++] = "--remove";
30e1eab5 105 Size += strlen(Args[n-1]);
03e39e59
AL
106 break;
107
108 case Item::Configure:
109 Args[n++] = "--configure";
30e1eab5 110 Size += strlen(Args[n-1]);
03e39e59
AL
111 break;
112
113 case Item::Install:
114 Args[n++] = "--unpack";
30e1eab5 115 Size += strlen(Args[n-1]);
03e39e59
AL
116 break;
117 }
118
119 // Write in the file or package names
120 if (I->Op == Item::Install)
30e1eab5
AL
121 {
122 for (;I != J && Size < 1024; I++)
123 {
03e39e59 124 Args[n++] = I->File.c_str();
30e1eab5
AL
125 Size += strlen(Args[n-1]);
126 }
127 }
03e39e59 128 else
30e1eab5
AL
129 {
130 for (;I != J && Size < 1024; I++)
131 {
03e39e59 132 Args[n++] = I->Pkg.Name();
30e1eab5
AL
133 Size += strlen(Args[n-1]);
134 }
135 }
03e39e59 136 Args[n] = 0;
30e1eab5
AL
137 J = I;
138
139 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
140 {
141 for (unsigned int k = 0; k != n; k++)
142 clog << Args[k] << ' ';
143 clog << endl;
144 continue;
145 }
03e39e59 146
03e39e59
AL
147 cout << flush;
148 clog << flush;
149 cerr << flush;
150
151 /* Mask off sig int/quit. We do this because dpkg also does when
152 it forks scripts. What happens is that when you hit ctrl-c it sends
153 it to all processes in the group. Since dpkg ignores the signal
154 it doesn't die but we do! So we must also ignore it */
155 signal(SIGQUIT,SIG_IGN);
156 signal(SIGINT,SIG_IGN);
157
158 // Fork dpkg
159 pid_t Child = fork();
160 if (Child < 0)
161 return _error->Errno("fork","Could't fork");
162
163 // This is the child
164 if (Child == 0)
165 {
d38b7b3d 166 signal(SIGPIPE,SIG_DFL);
03e39e59
AL
167 signal(SIGQUIT,SIG_DFL);
168 signal(SIGINT,SIG_DFL);
169 signal(SIGWINCH,SIG_DFL);
170 signal(SIGCONT,SIG_DFL);
171 signal(SIGTSTP,SIG_DFL);
172
173 if (chdir(_config->FindDir("Dir::Cache::Archives").c_str()) != 0)
0dbb95d8 174 _exit(100);
03e39e59
AL
175
176 // Close all of our FDs - just in case
177 for (int K = 3; K != 40; K++)
178 fcntl(K,F_SETFD,FD_CLOEXEC);
179
180 int Flags,dummy;
181 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
0dbb95d8 182 _exit(100);
03e39e59
AL
183
184 // Discard everything in stdin before forking dpkg
185 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
0dbb95d8 186 _exit(100);
03e39e59
AL
187
188 while (read(STDIN_FILENO,&dummy,1) == 1);
189
190 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
0dbb95d8 191 _exit(100);
03e39e59
AL
192
193 /* No Job Control Stop Env is a magic dpkg var that prevents it
194 from using sigstop */
195 setenv("DPKG_NO_TSTP","yes",1);
d568ed2d 196 execvp(Args[0],(char **)Args);
03e39e59 197 cerr << "Could not exec dpkg!" << endl;
0dbb95d8 198 _exit(100);
03e39e59
AL
199 }
200
201 // Wait for dpkg
202 int Status = 0;
203 while (waitpid(Child,&Status,0) != Child)
204 {
205 if (errno == EINTR)
206 continue;
207 return _error->Errno("waitpid","Couldn't wait for subprocess");
208 }
209
210 // Check for an error code.
211 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
212 return _error->Error("Sub-process returned an error code");
213
214 // Restore sig int/quit
215 signal(SIGQUIT,SIG_DFL);
216 signal(SIGINT,SIG_DFL);
217 }
218 return true;
219}
220 /*}}}*/