method.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. #include <dirent.h>
  31. #include <limits.h>
  32. #include <ctype.h>
  33. #include <assert.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <sys/file.h>
  37. #include <ncurses.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. static void cu_unlockmethod(int, void**) {
  53. assert(methodlockfile);
  54. assert(methlockfd);
  55. if (flock(methlockfd,LOCK_UN))
  56. ohshite("unable to unlock access method area");
  57. }
  58. static enum urqresult ensureoptions(void) {
  59. const char *const *ccpp;
  60. option *newoptions;
  61. int nread;
  62. if (!options) {
  63. newoptions= 0;
  64. nread= 0;
  65. for (ccpp= methoddirectories; *ccpp; ccpp++)
  66. readmethods(*ccpp, &newoptions, &nread);
  67. if (!newoptions) {
  68. curseson();
  69. addstr("No access methods are available.\n\n"
  70. "Press RETURN to continue.");
  71. refresh(); getch();
  72. return urqr_fail;
  73. }
  74. options= newoptions;
  75. noptions= nread;
  76. }
  77. return urqr_normal;
  78. }
  79. static void lockmethod(void) {
  80. if (!methodlockfile) {
  81. int l;
  82. l= strlen(admindir);
  83. methodlockfile= new char[l+sizeof(METHLOCKFILE)+2];
  84. strcpy(methodlockfile,admindir);
  85. strcpy(methodlockfile+l, "/" METHLOCKFILE);
  86. }
  87. if (methlockfd == -1) {
  88. methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  89. if (methlockfd == -1) {
  90. if (errno == EPERM)
  91. ohshit("you do not have permission to change the access method");
  92. ohshite("unable to open/create access method lockfile");
  93. }
  94. }
  95. if (flock(methlockfd,LOCK_EX|LOCK_NB)) {
  96. if (errno == EWOULDBLOCK || errno == EAGAIN)
  97. ohshit("the access method area is already locked");
  98. ohshite("unable to lock access method area");
  99. }
  100. push_cleanup(cu_unlockmethod,~0, 0,0, 0);
  101. }
  102. static int catchsignals[]= { SIGQUIT, SIGINT, 0 };
  103. #define NCATCHSIGNALS ((signed)(sizeof(catchsignals)/sizeof(int))-1)
  104. static struct sigaction uncatchsignal[NCATCHSIGNALS];
  105. void cu_restoresignals(int, void**) {
  106. int i;
  107. for (i=0; i<NCATCHSIGNALS; i++)
  108. if (sigaction(catchsignals[i],&uncatchsignal[i],0))
  109. fprintf(stderr,"error un-catching signal %d: %s\n",
  110. catchsignals[i],strerror(errno));
  111. }
  112. urqresult falliblesubprocess(const char *exepath, const char *name,
  113. const char *const *args) {
  114. pid_t c1, cr;
  115. int status, i, c;
  116. struct sigaction catchsig;
  117. cursesoff();
  118. memset(&catchsig,0,sizeof(catchsig));
  119. catchsig.sa_handler= SIG_IGN;
  120. sigemptyset(&catchsig.sa_mask);
  121. catchsig.sa_flags= 0;
  122. for (i=0; i<NCATCHSIGNALS; i++)
  123. if (sigaction(catchsignals[i],&catchsig,&uncatchsignal[i]))
  124. ohshite("unable to ignore signal %d before running %.250s",
  125. catchsignals[i], name);
  126. push_cleanup(cu_restoresignals,~0, 0,0, 0);
  127. if (!(c1= m_fork())) {
  128. cu_restoresignals(0,0);
  129. execvp(exepath,(char* const*) args);
  130. ohshite("unable to run %.250s process `%.250s'",name,exepath);
  131. }
  132. while ((cr= waitpid(c1,&status,0)) == -1)
  133. if (errno != EINTR) ohshite("unable to wait for %.250s",name);
  134. if (cr != c1)
  135. ohshit("got wrong child's status - asked for %ld, got %ld",(long)c1,(long)cr);
  136. pop_cleanup(ehflag_normaltidy);
  137. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  138. sleep(1);
  139. return urqr_normal;
  140. }
  141. fprintf(stderr,"\n%s ",name);
  142. if (WIFEXITED(status)) {
  143. i= WEXITSTATUS(status);
  144. fprintf(stderr,"returned error exit status %d.\n",i);
  145. } else if (WIFSIGNALED(status)) {
  146. i= WTERMSIG(status);
  147. if (i == SIGINT) {
  148. fprintf(stderr,"was interrupted.\n");
  149. } else {
  150. fprintf(stderr,"was terminated by a signal: %s.\n",strsignal(i));
  151. }
  152. if (WCOREDUMP(status))
  153. fprintf(stderr,"(It left a coredump.)\n");
  154. } else {
  155. fprintf(stderr,"failed with an unknown wait return code %d.\n",status);
  156. }
  157. fprintf(stderr,"Press RETURN to continue.\n");
  158. if (ferror(stderr))
  159. ohshite("write error on standard error");
  160. do { c= fgetc(stdin); } while (c != EOF && c != '\n');
  161. if (c == EOF)
  162. ohshite("error reading acknowledgement of program failure message");
  163. return urqr_fail;
  164. }
  165. static urqresult runscript(const char *exepath, const char *name) {
  166. urqresult ur;
  167. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  168. lockmethod();
  169. getcurrentopt();
  170. if (coption) {
  171. strcpy(coption->meth->pathinmeth,exepath);
  172. const char *fallibleargs[] = {
  173. exepath,
  174. admindir,
  175. coption->meth->name,
  176. coption->name,
  177. 0
  178. };
  179. ur= falliblesubprocess(coption->meth->path,name,fallibleargs);
  180. } else {
  181. curseson();
  182. addstr("No access method is selected/configured.\n\n"
  183. "Press RETURN to continue.");
  184. refresh(); getch();
  185. ur= urqr_fail;
  186. }
  187. pop_cleanup(ehflag_normaltidy);
  188. return ur;
  189. }
  190. urqresult urq_update(void) {
  191. return runscript(METHODUPDATESCRIPT,"update available list script");
  192. }
  193. urqresult urq_install(void) {
  194. return runscript(METHODINSTALLSCRIPT,"installation script");
  195. }
  196. static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
  197. const char *fallibleargs[] = {
  198. DPKG,
  199. "--pending",
  200. dpkgmode,
  201. 0
  202. };
  203. cursesoff();
  204. printf("running dpkg --pending %s ...\n",dpkgmode);
  205. fflush(stdout);
  206. return falliblesubprocess(DPKG,name,fallibleargs);
  207. }
  208. urqresult urq_remove(void) {
  209. return rundpkgauto("dpkg --remove","--remove");
  210. }
  211. urqresult urq_config(void) {
  212. return rundpkgauto("dpkg --configure","--configure");
  213. }
  214. urqresult urq_setup(void) {
  215. quitaction qa;
  216. urqresult ur;
  217. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  218. lockmethod();
  219. getcurrentopt();
  220. curseson();
  221. methodlist *l= new methodlist();
  222. qa= l->display();
  223. delete l;
  224. if (qa == qa_quitchecksave) {
  225. strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
  226. const char *fallibleargs[] = {
  227. METHODSETUPSCRIPT,
  228. admindir,
  229. coption->meth->name,
  230. coption->name,
  231. 0
  232. };
  233. ur= falliblesubprocess(coption->meth->path,"query/setup script",fallibleargs);
  234. if (ur == urqr_normal) writecurrentopt();
  235. } else {
  236. ur= urqr_fail;
  237. }
  238. pop_cleanup(ehflag_normaltidy);
  239. return ur;
  240. }