method.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * dselect - Debian GNU/Linux package maintenance user interface
  3. * method.cc - access method handling
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. * Copyright (C) 2001 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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <dirent.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. #include <sys/file.h>
  38. extern "C" {
  39. #include <config.h>
  40. #include <dpkg.h>
  41. #include <dpkg-db.h>
  42. }
  43. #include "dselect.h"
  44. #include "method.h"
  45. static const char *const methoddirectories[]= {
  46. LIBDIR "/" METHODSDIR,
  47. LOCALLIBDIR "/" METHODSDIR,
  48. 0
  49. };
  50. static char *methodlockfile= 0;
  51. static int methlockfd= -1;
  52. void sthfailed(const char * reasoning) {
  53. char buf[2048];
  54. curseson();
  55. clear();
  56. sprintf(buf,_("\n\n%s: %s\n"),DSELECT,reasoning);
  57. addstr(buf);
  58. attrset(A_BOLD);
  59. addstr(_("\nPress <enter> to continue."));
  60. attrset(A_NORMAL);
  61. refresh(); getch();
  62. }
  63. static void cu_unlockmethod(int, void**) {
  64. struct flock fl;
  65. assert(methodlockfile);
  66. assert(methlockfd);
  67. fl.l_type=F_UNLCK; fl.l_whence= SEEK_SET; fl.l_start=fl.l_len=0;
  68. if (fcntl(methlockfd,F_SETLK,&fl) == -1)
  69. sthfailed("unable to unlock access method area");
  70. }
  71. static enum urqresult ensureoptions(void) {
  72. const char *const *ccpp;
  73. dselect_option *newoptions;
  74. int nread;
  75. if (!options) {
  76. newoptions= 0;
  77. nread= 0;
  78. for (ccpp= methoddirectories; *ccpp; ccpp++)
  79. readmethods(*ccpp, &newoptions, &nread);
  80. if (!newoptions) {
  81. sthfailed("no access methods are available");
  82. return urqr_fail;
  83. }
  84. options= newoptions;
  85. noptions= nread;
  86. }
  87. return urqr_normal;
  88. }
  89. static enum urqresult lockmethod(void) {
  90. struct flock fl;
  91. if (!methodlockfile) {
  92. int l;
  93. l= strlen(admindir);
  94. methodlockfile= new char[l+sizeof(METHLOCKFILE)+2];
  95. strcpy(methodlockfile,admindir);
  96. strcpy(methodlockfile+l, "/" METHLOCKFILE);
  97. }
  98. if (methlockfd == -1) {
  99. methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  100. if (methlockfd == -1) {
  101. if ((errno == EPERM) || (errno == EACCES)) {
  102. sthfailed("requested operation requires superuser privilege");
  103. return urqr_fail;
  104. }
  105. sthfailed("unable to open/create access method lockfile");
  106. return urqr_fail;
  107. }
  108. }
  109. fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
  110. if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
  111. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  112. sthfailed("the access method area is already locked");
  113. return urqr_fail;
  114. }
  115. sthfailed("unable to lock access method area");
  116. return urqr_fail;
  117. }
  118. push_cleanup(cu_unlockmethod,~0, 0,0, 0);
  119. return urqr_normal;
  120. }
  121. static int catchsignals[]= { SIGQUIT, SIGINT, 0 };
  122. #define NCATCHSIGNALS ((signed)(sizeof(catchsignals)/sizeof(int))-1)
  123. static struct sigaction uncatchsignal[NCATCHSIGNALS];
  124. void cu_restoresignals(int, void**) {
  125. int i;
  126. for (i=0; i<NCATCHSIGNALS; i++)
  127. if (sigaction(catchsignals[i],&uncatchsignal[i],0))
  128. fprintf(stderr,_("error un-catching signal %d: %s\n"),
  129. catchsignals[i],strerror(errno));
  130. }
  131. urqresult falliblesubprocess(const char *exepath, const char *name,
  132. const char *const *args) {
  133. pid_t c1, cr;
  134. int status, i, c;
  135. struct sigaction catchsig;
  136. cursesoff();
  137. memset(&catchsig,0,sizeof(catchsig));
  138. catchsig.sa_handler= SIG_IGN;
  139. sigemptyset(&catchsig.sa_mask);
  140. catchsig.sa_flags= 0;
  141. for (i=0; i<NCATCHSIGNALS; i++)
  142. if (sigaction(catchsignals[i],&catchsig,&uncatchsignal[i]))
  143. ohshite(_("unable to ignore signal %d before running %.250s"),
  144. catchsignals[i], name);
  145. push_cleanup(cu_restoresignals,~0, 0,0, 0);
  146. if (!(c1= m_fork())) {
  147. cu_restoresignals(0,0);
  148. execvp(exepath,(char* const*) args);
  149. ohshite(_("unable to run %.250s process `%.250s'"),name,exepath);
  150. }
  151. while ((cr= waitpid(c1,&status,0)) == -1)
  152. if (errno != EINTR) ohshite(_("unable to wait for %.250s"),name);
  153. if (cr != c1)
  154. ohshit(_("got wrong child's status - asked for %ld, got %ld"),(long)c1,(long)cr);
  155. pop_cleanup(ehflag_normaltidy);
  156. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  157. sleep(1);
  158. return urqr_normal;
  159. }
  160. fprintf(stderr,"\n%s ",name);
  161. if (WIFEXITED(status)) {
  162. i= WEXITSTATUS(status);
  163. fprintf(stderr,_("returned error exit status %d.\n"),i);
  164. } else if (WIFSIGNALED(status)) {
  165. i= WTERMSIG(status);
  166. if (i == SIGINT) {
  167. fprintf(stderr,_("was interrupted.\n"));
  168. } else {
  169. fprintf(stderr,_("was terminated by a signal: %s.\n"),strsignal(i));
  170. }
  171. if (WCOREDUMP(status))
  172. fprintf(stderr,_("(It left a coredump.)\n"));
  173. } else {
  174. fprintf(stderr,_("failed with an unknown wait return code %d.\n"),status);
  175. }
  176. fprintf(stderr,_("Press <enter> to continue.\n"));
  177. if (ferror(stderr))
  178. ohshite(_("write error on standard error"));
  179. do { c= fgetc(stdin); } while ((c == ERR && errno==EINTR) || (c != '\n'));
  180. if (c == ERR)
  181. ohshite(_("error reading acknowledgement of program failure message"));
  182. return urqr_fail;
  183. }
  184. static urqresult runscript(const char *exepath, const char *name) {
  185. urqresult ur;
  186. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  187. ur=lockmethod(); if (ur != urqr_normal) return ur;
  188. getcurrentopt();
  189. if (coption) {
  190. strcpy(coption->meth->pathinmeth,exepath);
  191. const char *fallibleargs[] = {
  192. exepath,
  193. admindir,
  194. coption->meth->name,
  195. coption->name,
  196. 0
  197. };
  198. ur= falliblesubprocess(coption->meth->path,name,fallibleargs);
  199. } else {
  200. sthfailed("no access method is selected/configured");
  201. ur= urqr_fail;
  202. }
  203. pop_cleanup(ehflag_normaltidy);
  204. return ur;
  205. }
  206. urqresult urq_update(void) {
  207. return runscript(METHODUPDATESCRIPT,_("update available list script"));
  208. }
  209. urqresult urq_install(void) {
  210. return runscript(METHODINSTALLSCRIPT,_("installation script"));
  211. }
  212. static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
  213. const char *fallibleargs[] = {
  214. DPKG,
  215. "--admindir",
  216. admindir,
  217. "--pending",
  218. dpkgmode,
  219. 0
  220. };
  221. cursesoff();
  222. printf("running dpkg --pending %s ...\n",dpkgmode);
  223. fflush(stdout);
  224. return falliblesubprocess(DPKG,name,fallibleargs);
  225. }
  226. urqresult urq_remove(void) {
  227. return rundpkgauto("dpkg --remove","--remove");
  228. }
  229. urqresult urq_config(void) {
  230. return rundpkgauto("dpkg --configure","--configure");
  231. }
  232. urqresult urq_setup(void) {
  233. quitaction qa;
  234. urqresult ur;
  235. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  236. ur=lockmethod(); if (ur != urqr_normal) return ur;
  237. getcurrentopt();
  238. curseson();
  239. methodlist *l= new methodlist();
  240. qa= l->display();
  241. delete l;
  242. if (qa == qa_quitchecksave) {
  243. strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
  244. const char *fallibleargs[] = {
  245. METHODSETUPSCRIPT,
  246. admindir,
  247. coption->meth->name,
  248. coption->name,
  249. 0
  250. };
  251. ur= falliblesubprocess(coption->meth->path,_("query/setup script"),fallibleargs);
  252. if (ur == urqr_normal) writecurrentopt();
  253. } else {
  254. ur= urqr_fail;
  255. }
  256. pop_cleanup(ehflag_normaltidy);
  257. return ur;
  258. }