method.cc 6.8 KB

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