]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
Fixed up the docs a bit
[apt.git] / apt-pkg / deb / dpkgpm.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dpkgpm.cc,v 1.2 1998/11/22 03:20:35 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
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 /* */
30 pkgDPkgPM::pkgDPkgPM(pkgDepCache &Cache) : pkgPackageManager(Cache)
31 {
32 }
33 /*}}}*/
34 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* */
37 pkgDPkgPM::~pkgDPkgPM()
38 {
39 }
40 /*}}}*/
41 // DPkgPM::Install - Install a package /*{{{*/
42 // ---------------------------------------------------------------------
43 /* Add an install operation to the sequence list */
44 bool 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 */
56 bool 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 */
68 bool 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 */
80 bool 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++);
86
87 // Generate the argument list
88 const char *Args[400];
89 if (J - I > 350)
90 J = I + 350;
91
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]);
96
97 switch (I->Op)
98 {
99 case Item::Remove:
100 Args[n++] = "--force-depends";
101 Size += strlen(Args[n-1]);
102 Args[n++] = "--force-remove-essential";
103 Size += strlen(Args[n-1]);
104 Args[n++] = "--remove";
105 Size += strlen(Args[n-1]);
106 break;
107
108 case Item::Configure:
109 Args[n++] = "--configure";
110 Size += strlen(Args[n-1]);
111 break;
112
113 case Item::Install:
114 Args[n++] = "--unpack";
115 Size += strlen(Args[n-1]);
116 break;
117 }
118
119 // Write in the file or package names
120 if (I->Op == Item::Install)
121 {
122 for (;I != J && Size < 1024; I++)
123 {
124 Args[n++] = I->File.c_str();
125 Size += strlen(Args[n-1]);
126 }
127 }
128 else
129 {
130 for (;I != J && Size < 1024; I++)
131 {
132 Args[n++] = I->Pkg.Name();
133 Size += strlen(Args[n-1]);
134 }
135 }
136 Args[n] = 0;
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 }
146
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 {
166 signal(SIGQUIT,SIG_DFL);
167 signal(SIGINT,SIG_DFL);
168 signal(SIGWINCH,SIG_DFL);
169 signal(SIGCONT,SIG_DFL);
170 signal(SIGTSTP,SIG_DFL);
171
172 if (chdir(_config->FindDir("Dir::Cache::Archives").c_str()) != 0)
173 exit(100);
174
175 // Close all of our FDs - just in case
176 for (int K = 3; K != 40; K++)
177 fcntl(K,F_SETFD,FD_CLOEXEC);
178
179 int Flags,dummy;
180 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
181 exit(100);
182
183 // Discard everything in stdin before forking dpkg
184 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
185 exit(100);
186
187 while (read(STDIN_FILENO,&dummy,1) == 1);
188
189 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
190 exit(100);
191
192 /* No Job Control Stop Env is a magic dpkg var that prevents it
193 from using sigstop */
194 setenv("DPKG_NO_TSTP","yes",1);
195 execvp("dpkg",(char **)Args);
196 cerr << "Could not exec dpkg!" << endl;
197 exit(100);
198 }
199
200 // Wait for dpkg
201 int Status = 0;
202 while (waitpid(Child,&Status,0) != Child)
203 {
204 if (errno == EINTR)
205 continue;
206 return _error->Errno("waitpid","Couldn't wait for subprocess");
207 }
208
209 // Check for an error code.
210 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
211 return _error->Error("Sub-process returned an error code");
212
213 // Restore sig int/quit
214 signal(SIGQUIT,SIG_DFL);
215 signal(SIGINT,SIG_DFL);
216 }
217 return true;
218 }
219 /*}}}*/