chiark / gitweb /
lib/dpkg/tarfn.c: Kludge `tar_header_decode' to handle spurious `errno'.
[dpkg] / dselect / method.cc
1 /*
2  * dselect - Debian package maintenance user interface
3  * method.cc - access method handling
4  *
5  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * Copyright © 2001,2002 Wichert Akkerman <wakkerma@debian.org>
7  *
8  * This is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <compat.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <sys/wait.h>
29
30 #include <assert.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <dirent.h>
36 #include <signal.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40
41 #include <dpkg/i18n.h>
42 #include <dpkg/dpkg.h>
43 #include <dpkg/dpkg-db.h>
44 #include <dpkg/subproc.h>
45 #include <dpkg/command.h>
46
47 #include "dselect.h"
48 #include "method.h"
49
50 static const char *const methoddirectories[]= {
51   LIBDIR "/" METHODSDIR,
52   LOCALLIBDIR "/" METHODSDIR,
53   nullptr
54 };
55
56 static char *methodlockfile = nullptr;
57 static int methlockfd= -1;
58
59 static void
60 sthfailed(const char * reasoning)
61 {
62   curseson();
63   clear();
64   printw(_("\n\n%s: %s\n"), DSELECT, reasoning);
65   attrset(A_BOLD);
66   addstr(_("\nPress <enter> to continue."));
67   attrset(A_NORMAL);
68   refresh(); getch();
69 }
70
71 static void cu_unlockmethod(int, void**) {
72   struct flock fl;
73
74   assert(methodlockfile);
75   assert(methlockfd);
76   fl.l_type=F_UNLCK; fl.l_whence= SEEK_SET; fl.l_start=fl.l_len=0;
77   if (fcntl(methlockfd,F_SETLK,&fl) == -1)
78     sthfailed(_("cannot unlock access method area"));
79 }
80
81 static enum urqresult ensureoptions(void) {
82   const char *const *ccpp;
83   dselect_option *newoptions;
84   int nread;
85
86   if (!options) {
87     newoptions = nullptr;
88     nread= 0;
89     for (ccpp= methoddirectories; *ccpp; ccpp++)
90       readmethods(*ccpp, &newoptions, &nread);
91     if (!newoptions) {
92       sthfailed(_("no access methods are available"));
93       return urqr_fail;
94     }
95     options= newoptions;
96     noptions= nread;
97   }
98   return urqr_normal;
99 }
100
101 static enum urqresult lockmethod(void) {
102   struct flock fl;
103
104   if (methodlockfile == nullptr)
105     methodlockfile = dpkg_db_get_path(METHLOCKFILE);
106
107   if (methlockfd == -1) {
108     methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
109     if (methlockfd == -1) {
110       if ((errno == EPERM) || (errno == EACCES)) {
111         sthfailed(_("requested operation requires superuser privilege"));
112         return urqr_fail;
113       }
114       sthfailed(_("cannot open or create access method lockfile"));
115       return urqr_fail;
116     }
117   }
118   fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
119   if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
120     if (errno == EACCES || errno == EAGAIN) {
121       sthfailed(_("the access method area is already locked"));
122       return urqr_fail;
123     }
124     sthfailed(_("cannot lock access method area"));
125     return urqr_fail;
126   }
127   push_cleanup(cu_unlockmethod, ~0, nullptr, 0, 0);
128   return urqr_normal;
129 }
130
131 static urqresult
132 falliblesubprocess(struct command *cmd)
133 {
134   pid_t pid;
135   int i, c;
136
137   cursesoff();
138
139   subproc_signals_ignore(cmd->name);
140
141   pid = subproc_fork();
142   if (pid == 0) {
143     subproc_signals_cleanup(0, nullptr);
144     command_exec(cmd);
145   }
146
147   fprintf(stderr, "\n");
148
149   i = subproc_reap(pid, cmd->name, SUBPROC_WARN);
150
151   subproc_signals_restore();
152
153   if (i == 0) {
154     sleep(1);
155     return urqr_normal;
156   }
157   fprintf(stderr,_("Press <enter> to continue.\n"));
158   m_output(stderr, _("<standard error>"));
159   do {
160     c = fgetc(stdin);
161   } while ((c == EOF && errno == EINTR) || (c != '\n' && c != EOF));
162   if (c == EOF)
163     ohshite(_("error reading acknowledgement of program failure message"));
164   return urqr_fail;
165 }
166
167 static urqresult runscript(const char *exepath, const char *name) {
168   urqresult ur;
169
170   ur= ensureoptions();  if (ur != urqr_normal) return ur;
171   ur=lockmethod();  if (ur != urqr_normal) return ur;
172   getcurrentopt();
173
174   if (coption) {
175     struct command cmd;
176
177     strcpy(coption->meth->pathinmeth,exepath);
178
179     command_init(&cmd, coption->meth->path, name);
180     command_add_args(&cmd, exepath, dpkg_db_get_dir(),
181                      coption->meth->name, coption->name, nullptr);
182     ur = falliblesubprocess(&cmd);
183     command_destroy(&cmd);
184   } else {
185     sthfailed(_("no access method is selected or configured"));
186     ur= urqr_fail;
187   }
188   pop_cleanup(ehflag_normaltidy);
189
190   return ur;
191 }
192
193 urqresult urq_update(void) {
194   return runscript(METHODUPDATESCRIPT,_("update available list script"));
195 }
196
197 urqresult urq_install(void) {
198   return runscript(METHODINSTALLSCRIPT,_("installation script"));
199 }
200
201 static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
202   urqresult ur;
203   struct command cmd;
204
205   command_init(&cmd, DPKG, name);
206   command_add_args(&cmd, DPKG, "--admindir", dpkg_db_get_dir(), "--pending",
207                    dpkgmode, nullptr);
208
209   cursesoff();
210   printf("running dpkg --pending %s ...\n",dpkgmode);
211   fflush(stdout);
212   ur = falliblesubprocess(&cmd);
213   command_destroy(&cmd);
214
215   return ur;
216 }
217
218 urqresult urq_remove(void) {
219   return rundpkgauto("dpkg --remove","--remove");
220 }
221
222 urqresult urq_config(void) {
223   return rundpkgauto("dpkg --configure","--configure");
224 }
225
226 urqresult urq_setup(void) {
227   quitaction qa;
228   urqresult ur;
229
230   ur= ensureoptions();  if (ur != urqr_normal) return ur;
231   ur=lockmethod();  if (ur != urqr_normal) return ur;
232   getcurrentopt();
233
234   curseson();
235   methodlist *l= new methodlist();
236   qa= l->display();
237   delete l;
238
239   if (qa == qa_quitchecksave) {
240     struct command cmd;
241
242     strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
243
244     command_init(&cmd, coption->meth->path, _("query/setup script"));
245     command_add_args(&cmd, METHODSETUPSCRIPT, dpkg_db_get_dir(),
246                      coption->meth->name, coption->name, nullptr);
247     ur = falliblesubprocess(&cmd);
248     command_destroy(&cmd);
249     if (ur == urqr_normal) writecurrentopt();
250   } else {
251     ur= urqr_fail;
252   }
253
254   pop_cleanup(ehflag_normaltidy);
255   return ur;
256 }