method.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <curses.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 <enter> 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 lockfailed(const char * reasoning) {
  80. char buf[2048];
  81. curseson();
  82. clear();
  83. sprintf(buf,_("\n\n%s: %s\n"),DSELECT,reasoning);
  84. addstr(buf);
  85. attrset(A_BOLD);
  86. addstr(_("\nPress <enter> to continue."));
  87. attrset(A_NORMAL);
  88. refresh(); getch();
  89. }
  90. static enum urqresult lockmethod(void) {
  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. lockfailed("requested operation requires superuser privilege");
  103. return urqr_fail;
  104. }
  105. lockfailed("unable to open/create access method lockfile");
  106. return urqr_fail;
  107. }
  108. }
  109. if (flock(methlockfd,LOCK_EX|LOCK_NB)) {
  110. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  111. lockfailed("the access method area is already locked");
  112. return urqr_fail;
  113. }
  114. lockfailed("unable to lock access method area");
  115. return urqr_fail;
  116. }
  117. push_cleanup(cu_unlockmethod,~0, 0,0, 0);
  118. return urqr_normal;
  119. }
  120. static int catchsignals[]= { SIGQUIT, SIGINT, 0 };
  121. #define NCATCHSIGNALS ((signed)(sizeof(catchsignals)/sizeof(int))-1)
  122. static struct sigaction uncatchsignal[NCATCHSIGNALS];
  123. void cu_restoresignals(int, void**) {
  124. int i;
  125. for (i=0; i<NCATCHSIGNALS; i++)
  126. if (sigaction(catchsignals[i],&uncatchsignal[i],0))
  127. fprintf(stderr,_("error un-catching signal %d: %s\n"),
  128. catchsignals[i],strerror(errno));
  129. }
  130. urqresult falliblesubprocess(const char *exepath, const char *name,
  131. const char *const *args) {
  132. pid_t c1, cr;
  133. int status, i, c;
  134. struct sigaction catchsig;
  135. cursesoff();
  136. memset(&catchsig,0,sizeof(catchsig));
  137. catchsig.sa_handler= SIG_IGN;
  138. sigemptyset(&catchsig.sa_mask);
  139. catchsig.sa_flags= 0;
  140. for (i=0; i<NCATCHSIGNALS; i++)
  141. if (sigaction(catchsignals[i],&catchsig,&uncatchsignal[i]))
  142. ohshite(_("unable to ignore signal %d before running %.250s"),
  143. catchsignals[i], name);
  144. push_cleanup(cu_restoresignals,~0, 0,0, 0);
  145. if (!(c1= m_fork())) {
  146. cu_restoresignals(0,0);
  147. execvp(exepath,(char* const*) args);
  148. ohshite(_("unable to run %.250s process `%.250s'"),name,exepath);
  149. }
  150. while ((cr= waitpid(c1,&status,0)) == -1)
  151. if (errno != EINTR) ohshite(_("unable to wait for %.250s"),name);
  152. if (cr != c1)
  153. ohshit(_("got wrong child's status - asked for %ld, got %ld"),(long)c1,(long)cr);
  154. pop_cleanup(ehflag_normaltidy);
  155. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  156. sleep(1);
  157. return urqr_normal;
  158. }
  159. fprintf(stderr,"\n%s ",name);
  160. if (WIFEXITED(status)) {
  161. i= WEXITSTATUS(status);
  162. fprintf(stderr,_("returned error exit status %d.\n"),i);
  163. } else if (WIFSIGNALED(status)) {
  164. i= WTERMSIG(status);
  165. if (i == SIGINT) {
  166. fprintf(stderr,_("was interrupted.\n"));
  167. } else {
  168. fprintf(stderr,_("was terminated by a signal: %s.\n"),strsignal(i));
  169. }
  170. if (WCOREDUMP(status))
  171. fprintf(stderr,_("(It left a coredump.)\n"));
  172. } else {
  173. fprintf(stderr,_("failed with an unknown wait return code %d.\n"),status);
  174. }
  175. fprintf(stderr,_("Press <enter> to continue.\n"));
  176. if (ferror(stderr))
  177. ohshite(_("write error on standard error"));
  178. do { c= fgetc(stdin); } while (c != EOF && c != '\n');
  179. if (c == EOF)
  180. ohshite(_("error reading acknowledgement of program failure message"));
  181. return urqr_fail;
  182. }
  183. static urqresult runscript(const char *exepath, const char *name) {
  184. urqresult ur;
  185. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  186. ur=lockmethod(); if (ur != urqr_normal) return ur;
  187. getcurrentopt();
  188. if (coption) {
  189. strcpy(coption->meth->pathinmeth,exepath);
  190. const char *fallibleargs[] = {
  191. exepath,
  192. admindir,
  193. coption->meth->name,
  194. coption->name,
  195. 0
  196. };
  197. ur= falliblesubprocess(coption->meth->path,name,fallibleargs);
  198. } else {
  199. curseson();
  200. addstr(_("No access method is selected/configured.\n\n"
  201. "Press <enter> to continue."));
  202. refresh(); getch();
  203. ur= urqr_fail;
  204. }
  205. pop_cleanup(ehflag_normaltidy);
  206. return ur;
  207. }
  208. urqresult urq_update(void) {
  209. return runscript(METHODUPDATESCRIPT,_("update available list script"));
  210. }
  211. urqresult urq_install(void) {
  212. return runscript(METHODINSTALLSCRIPT,_("installation script"));
  213. }
  214. static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
  215. const char *fallibleargs[] = {
  216. DPKG,
  217. "--admindir",
  218. admindir,
  219. "--pending",
  220. dpkgmode,
  221. 0
  222. };
  223. cursesoff();
  224. printf("running dpkg --pending %s ...\n",dpkgmode);
  225. fflush(stdout);
  226. return falliblesubprocess(DPKG,name,fallibleargs);
  227. }
  228. urqresult urq_remove(void) {
  229. return rundpkgauto("dpkg --remove","--remove");
  230. }
  231. urqresult urq_config(void) {
  232. return rundpkgauto("dpkg --configure","--configure");
  233. }
  234. urqresult urq_setup(void) {
  235. quitaction qa;
  236. urqresult ur;
  237. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  238. ur=lockmethod(); if (ur != urqr_normal) return ur;
  239. getcurrentopt();
  240. curseson();
  241. methodlist *l= new methodlist();
  242. qa= l->display();
  243. delete l;
  244. if (qa == qa_quitchecksave) {
  245. strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
  246. const char *fallibleargs[] = {
  247. METHODSETUPSCRIPT,
  248. admindir,
  249. coption->meth->name,
  250. coption->name,
  251. 0
  252. };
  253. ur= falliblesubprocess(coption->meth->path,_("query/setup script"),fallibleargs);
  254. if (ur == urqr_normal) writecurrentopt();
  255. } else {
  256. ur= urqr_fail;
  257. }
  258. pop_cleanup(ehflag_normaltidy);
  259. return ur;
  260. }