method.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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
  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 <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/file.h>
  27. #include <sys/wait.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <limits.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <fcntl.h>
  34. #include <dirent.h>
  35. #include <signal.h>
  36. #include <unistd.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <dpkg/i18n.h>
  40. #include <dpkg/dpkg.h>
  41. #include <dpkg/dpkg-db.h>
  42. #include <dpkg/subproc.h>
  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
  53. sthfailed(const char * reasoning)
  54. {
  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. urqresult falliblesubprocess(const char *exepath, const char *name,
  124. const char *const *args) {
  125. pid_t c1, cr;
  126. int status, i, c;
  127. cursesoff();
  128. setup_subproc_signals(name);
  129. if (!(c1= m_fork())) {
  130. cu_subproc_signals(0, 0);
  131. execvp(exepath,(char* const*) args);
  132. ohshite(_("unable to run %.250s process `%.250s'"),name,exepath);
  133. }
  134. while ((cr= waitpid(c1,&status,0)) == -1)
  135. if (errno != EINTR) ohshite(_("unable to wait for %.250s"),name);
  136. if (cr != c1)
  137. ohshit(_("got wrong child's status - asked for %ld, got %ld"),(long)c1,(long)cr);
  138. pop_cleanup(ehflag_normaltidy);
  139. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  140. sleep(1);
  141. return urqr_normal;
  142. }
  143. fprintf(stderr,"\n%s ",name);
  144. if (WIFEXITED(status)) {
  145. i= WEXITSTATUS(status);
  146. fprintf(stderr,_("returned error exit status %d.\n"),i);
  147. } else if (WIFSIGNALED(status)) {
  148. i= WTERMSIG(status);
  149. if (i == SIGINT) {
  150. fprintf(stderr,_("was interrupted.\n"));
  151. } else {
  152. fprintf(stderr,_("was terminated by a signal: %s.\n"),strsignal(i));
  153. }
  154. if (WCOREDUMP(status))
  155. fprintf(stderr,_("(It left a coredump.)\n"));
  156. } else {
  157. fprintf(stderr,_("failed with an unknown wait return code %d.\n"),status);
  158. }
  159. fprintf(stderr,_("Press <enter> to continue.\n"));
  160. m_output(stderr, _("<standard error>"));
  161. do { c= fgetc(stdin); } while ((c == ERR && errno==EINTR) || ((c != '\n') && c != EOF));
  162. if ((c == ERR) || (c == EOF))
  163. ohshite(_("error reading acknowledgement of program failure message"));
  164. return urqr_fail;
  165. }
  166. static urqresult runscript(const char *exepath, const char *name) {
  167. urqresult ur;
  168. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  169. ur=lockmethod(); if (ur != urqr_normal) return ur;
  170. getcurrentopt();
  171. if (coption) {
  172. strcpy(coption->meth->pathinmeth,exepath);
  173. const char *fallibleargs[] = {
  174. exepath,
  175. admindir,
  176. coption->meth->name,
  177. coption->name,
  178. 0
  179. };
  180. ur= falliblesubprocess(coption->meth->path,name,fallibleargs);
  181. } else {
  182. sthfailed("no access method is selected/configured");
  183. ur= urqr_fail;
  184. }
  185. pop_cleanup(ehflag_normaltidy);
  186. return ur;
  187. }
  188. urqresult urq_update(void) {
  189. return runscript(METHODUPDATESCRIPT,_("update available list script"));
  190. }
  191. urqresult urq_install(void) {
  192. return runscript(METHODINSTALLSCRIPT,_("installation script"));
  193. }
  194. static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
  195. const char *fallibleargs[] = {
  196. DPKG,
  197. "--admindir",
  198. admindir,
  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. ur=lockmethod(); if (ur != urqr_normal) return ur;
  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. }