method.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * method.cc - access method handling
  4. *
  5. * Copyright (C) 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 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
  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. extern "C" {
  23. #include <config.h>
  24. }
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <signal.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #include <dirent.h>
  35. #include <limits.h>
  36. #include <ctype.h>
  37. #include <assert.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40. #include <sys/file.h>
  41. extern "C" {
  42. #include <dpkg.h>
  43. #include <dpkg-db.h>
  44. }
  45. #include "dselect.h"
  46. #include "method.h"
  47. static const char *const methoddirectories[]= {
  48. LIBDIR "/" METHODSDIR,
  49. LOCALLIBDIR "/" METHODSDIR,
  50. 0
  51. };
  52. static char *methodlockfile= 0;
  53. static int methlockfd= -1;
  54. void sthfailed(const char * reasoning) {
  55. char buf[2048];
  56. curseson();
  57. clear();
  58. sprintf(buf,_("\n\n%s: %s\n"),DSELECT,reasoning);
  59. addstr(buf);
  60. attrset(A_BOLD);
  61. addstr(_("\nPress <enter> to continue."));
  62. attrset(A_NORMAL);
  63. refresh(); getch();
  64. }
  65. static void cu_unlockmethod(int, void**) {
  66. struct flock fl;
  67. assert(methodlockfile);
  68. assert(methlockfd);
  69. fl.l_type=F_UNLCK; fl.l_whence= SEEK_SET; fl.l_start=fl.l_len=0;
  70. if (fcntl(methlockfd,F_SETLK,&fl) == -1)
  71. sthfailed("unable to unlock access method area");
  72. }
  73. static enum urqresult ensureoptions(void) {
  74. const char *const *ccpp;
  75. dselect_option *newoptions;
  76. int nread;
  77. if (!options) {
  78. newoptions= 0;
  79. nread= 0;
  80. for (ccpp= methoddirectories; *ccpp; ccpp++)
  81. readmethods(*ccpp, &newoptions, &nread);
  82. if (!newoptions) {
  83. sthfailed("no access methods are available");
  84. return urqr_fail;
  85. }
  86. options= newoptions;
  87. noptions= nread;
  88. }
  89. return urqr_normal;
  90. }
  91. static enum urqresult lockmethod(void) {
  92. struct flock fl;
  93. if (!methodlockfile) {
  94. int l;
  95. l= strlen(admindir);
  96. methodlockfile= new char[l+sizeof(METHLOCKFILE)+2];
  97. strcpy(methodlockfile,admindir);
  98. strcpy(methodlockfile+l, "/" METHLOCKFILE);
  99. }
  100. if (methlockfd == -1) {
  101. methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  102. if (methlockfd == -1) {
  103. if ((errno == EPERM) || (errno == EACCES)) {
  104. sthfailed("requested operation requires superuser privilege");
  105. return urqr_fail;
  106. }
  107. sthfailed("unable to open/create access method lockfile");
  108. return urqr_fail;
  109. }
  110. }
  111. fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
  112. if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
  113. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  114. sthfailed("the access method area is already locked");
  115. return urqr_fail;
  116. }
  117. sthfailed("unable to lock access method area");
  118. return urqr_fail;
  119. }
  120. push_cleanup(cu_unlockmethod,~0, 0,0, 0);
  121. return urqr_normal;
  122. }
  123. static int catchsignals[]= { SIGQUIT, SIGINT, 0 };
  124. #define NCATCHSIGNALS ((signed)(sizeof(catchsignals)/sizeof(int))-1)
  125. static struct sigaction uncatchsignal[NCATCHSIGNALS];
  126. void cu_restoresignals(int, void**) {
  127. int i;
  128. for (i=0; i<NCATCHSIGNALS; i++)
  129. if (sigaction(catchsignals[i],&uncatchsignal[i],0))
  130. fprintf(stderr,_("error un-catching signal %d: %s\n"),
  131. catchsignals[i],strerror(errno));
  132. }
  133. urqresult falliblesubprocess(const char *exepath, const char *name,
  134. const char *const *args) {
  135. pid_t c1, cr;
  136. int status, i, c;
  137. struct sigaction catchsig;
  138. cursesoff();
  139. memset(&catchsig,0,sizeof(catchsig));
  140. catchsig.sa_handler= SIG_IGN;
  141. sigemptyset(&catchsig.sa_mask);
  142. catchsig.sa_flags= 0;
  143. for (i=0; i<NCATCHSIGNALS; i++)
  144. if (sigaction(catchsignals[i],&catchsig,&uncatchsignal[i]))
  145. ohshite(_("unable to ignore signal %d before running %.250s"),
  146. catchsignals[i], name);
  147. push_cleanup(cu_restoresignals,~0, 0,0, 0);
  148. if (!(c1= m_fork())) {
  149. cu_restoresignals(0,0);
  150. execvp(exepath,(char* const*) args);
  151. ohshite(_("unable to run %.250s process `%.250s'"),name,exepath);
  152. }
  153. while ((cr= waitpid(c1,&status,0)) == -1)
  154. if (errno != EINTR) ohshite(_("unable to wait for %.250s"),name);
  155. if (cr != c1)
  156. ohshit(_("got wrong child's status - asked for %ld, got %ld"),(long)c1,(long)cr);
  157. pop_cleanup(ehflag_normaltidy);
  158. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  159. sleep(1);
  160. return urqr_normal;
  161. }
  162. fprintf(stderr,"\n%s ",name);
  163. if (WIFEXITED(status)) {
  164. i= WEXITSTATUS(status);
  165. fprintf(stderr,_("returned error exit status %d.\n"),i);
  166. } else if (WIFSIGNALED(status)) {
  167. i= WTERMSIG(status);
  168. if (i == SIGINT) {
  169. fprintf(stderr,_("was interrupted.\n"));
  170. } else {
  171. fprintf(stderr,_("was terminated by a signal: %s.\n"),strsignal(i));
  172. }
  173. if (WCOREDUMP(status))
  174. fprintf(stderr,_("(It left a coredump.)\n"));
  175. } else {
  176. fprintf(stderr,_("failed with an unknown wait return code %d.\n"),status);
  177. }
  178. fprintf(stderr,_("Press <enter> to continue.\n"));
  179. if (ferror(stderr))
  180. ohshite(_("write error on standard error"));
  181. do { c= fgetc(stdin); } while ((c == ERR && errno==EINTR) || ((c != '\n') && c != EOF));
  182. if ((c == ERR) || (c == EOF))
  183. ohshite(_("error reading acknowledgement of program failure message"));
  184. return urqr_fail;
  185. }
  186. static urqresult runscript(const char *exepath, const char *name) {
  187. urqresult ur;
  188. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  189. ur=lockmethod(); if (ur != urqr_normal) return ur;
  190. getcurrentopt();
  191. if (coption) {
  192. strcpy(coption->meth->pathinmeth,exepath);
  193. const char *fallibleargs[] = {
  194. exepath,
  195. admindir,
  196. coption->meth->name,
  197. coption->name,
  198. 0
  199. };
  200. ur= falliblesubprocess(coption->meth->path,name,fallibleargs);
  201. } else {
  202. sthfailed("no access method is selected/configured");
  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. }