method.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <dpkg/i18n.h>
  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. #include <dpkg.h>
  42. #include <dpkg-db.h>
  43. #include <dpkg-priv.h>
  44. #include "dselect.h"
  45. #include "method.h"
  46. static const char *const methoddirectories[]= {
  47. LIBDIR "/" METHODSDIR,
  48. LOCALLIBDIR "/" METHODSDIR,
  49. 0
  50. };
  51. static char *methodlockfile= 0;
  52. static int methlockfd= -1;
  53. static void
  54. sthfailed(const char * reasoning)
  55. {
  56. char buf[2048];
  57. curseson();
  58. clear();
  59. sprintf(buf,_("\n\n%s: %s\n"),DSELECT,reasoning);
  60. addstr(buf);
  61. attrset(A_BOLD);
  62. addstr(_("\nPress <enter> to continue."));
  63. attrset(A_NORMAL);
  64. refresh(); getch();
  65. }
  66. static void cu_unlockmethod(int, void**) {
  67. struct flock fl;
  68. assert(methodlockfile);
  69. assert(methlockfd);
  70. fl.l_type=F_UNLCK; fl.l_whence= SEEK_SET; fl.l_start=fl.l_len=0;
  71. if (fcntl(methlockfd,F_SETLK,&fl) == -1)
  72. sthfailed("unable to unlock access method area");
  73. }
  74. static enum urqresult ensureoptions(void) {
  75. const char *const *ccpp;
  76. dselect_option *newoptions;
  77. int nread;
  78. if (!options) {
  79. newoptions= 0;
  80. nread= 0;
  81. for (ccpp= methoddirectories; *ccpp; ccpp++)
  82. readmethods(*ccpp, &newoptions, &nread);
  83. if (!newoptions) {
  84. sthfailed("no access methods are available");
  85. return urqr_fail;
  86. }
  87. options= newoptions;
  88. noptions= nread;
  89. }
  90. return urqr_normal;
  91. }
  92. static enum urqresult lockmethod(void) {
  93. struct flock fl;
  94. if (!methodlockfile) {
  95. int l;
  96. l= strlen(admindir);
  97. methodlockfile= new char[l+sizeof(METHLOCKFILE)+2];
  98. strcpy(methodlockfile,admindir);
  99. strcpy(methodlockfile+l, "/" METHLOCKFILE);
  100. }
  101. if (methlockfd == -1) {
  102. methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  103. if (methlockfd == -1) {
  104. if ((errno == EPERM) || (errno == EACCES)) {
  105. sthfailed("requested operation requires superuser privilege");
  106. return urqr_fail;
  107. }
  108. sthfailed("unable to open/create access method lockfile");
  109. return urqr_fail;
  110. }
  111. }
  112. fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
  113. if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
  114. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  115. sthfailed("the access method area is already locked");
  116. return urqr_fail;
  117. }
  118. sthfailed("unable to lock access method area");
  119. return urqr_fail;
  120. }
  121. push_cleanup(cu_unlockmethod,~0, 0,0, 0);
  122. return urqr_normal;
  123. }
  124. urqresult falliblesubprocess(const char *exepath, const char *name,
  125. const char *const *args) {
  126. pid_t c1, cr;
  127. int status, i, c;
  128. cursesoff();
  129. setup_subproc_signals(name);
  130. if (!(c1= m_fork())) {
  131. cu_subproc_signals(0, 0);
  132. execvp(exepath,(char* const*) args);
  133. ohshite(_("unable to run %.250s process `%.250s'"),name,exepath);
  134. }
  135. while ((cr= waitpid(c1,&status,0)) == -1)
  136. if (errno != EINTR) ohshite(_("unable to wait for %.250s"),name);
  137. if (cr != c1)
  138. ohshit(_("got wrong child's status - asked for %ld, got %ld"),(long)c1,(long)cr);
  139. pop_cleanup(ehflag_normaltidy);
  140. if (WIFEXITED(status) && !WEXITSTATUS(status)) {
  141. sleep(1);
  142. return urqr_normal;
  143. }
  144. fprintf(stderr,"\n%s ",name);
  145. if (WIFEXITED(status)) {
  146. i= WEXITSTATUS(status);
  147. fprintf(stderr,_("returned error exit status %d.\n"),i);
  148. } else if (WIFSIGNALED(status)) {
  149. i= WTERMSIG(status);
  150. if (i == SIGINT) {
  151. fprintf(stderr,_("was interrupted.\n"));
  152. } else {
  153. fprintf(stderr,_("was terminated by a signal: %s.\n"),strsignal(i));
  154. }
  155. if (WCOREDUMP(status))
  156. fprintf(stderr,_("(It left a coredump.)\n"));
  157. } else {
  158. fprintf(stderr,_("failed with an unknown wait return code %d.\n"),status);
  159. }
  160. fprintf(stderr,_("Press <enter> to continue.\n"));
  161. if (ferror(stderr))
  162. ohshite(_("write error on standard error"));
  163. do { c= fgetc(stdin); } while ((c == ERR && errno==EINTR) || ((c != '\n') && c != EOF));
  164. if ((c == ERR) || (c == EOF))
  165. ohshite(_("error reading acknowledgement of program failure message"));
  166. return urqr_fail;
  167. }
  168. static urqresult runscript(const char *exepath, const char *name) {
  169. urqresult ur;
  170. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  171. ur=lockmethod(); if (ur != urqr_normal) return ur;
  172. getcurrentopt();
  173. if (coption) {
  174. strcpy(coption->meth->pathinmeth,exepath);
  175. const char *fallibleargs[] = {
  176. exepath,
  177. admindir,
  178. coption->meth->name,
  179. coption->name,
  180. 0
  181. };
  182. ur= falliblesubprocess(coption->meth->path,name,fallibleargs);
  183. } else {
  184. sthfailed("no access method is selected/configured");
  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. "--admindir",
  200. admindir,
  201. "--pending",
  202. dpkgmode,
  203. 0
  204. };
  205. cursesoff();
  206. printf("running dpkg --pending %s ...\n",dpkgmode);
  207. fflush(stdout);
  208. return falliblesubprocess(DPKG,name,fallibleargs);
  209. }
  210. urqresult urq_remove(void) {
  211. return rundpkgauto("dpkg --remove","--remove");
  212. }
  213. urqresult urq_config(void) {
  214. return rundpkgauto("dpkg --configure","--configure");
  215. }
  216. urqresult urq_setup(void) {
  217. quitaction qa;
  218. urqresult ur;
  219. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  220. ur=lockmethod(); if (ur != urqr_normal) return ur;
  221. getcurrentopt();
  222. curseson();
  223. methodlist *l= new methodlist();
  224. qa= l->display();
  225. delete l;
  226. if (qa == qa_quitchecksave) {
  227. strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
  228. const char *fallibleargs[] = {
  229. METHODSETUPSCRIPT,
  230. admindir,
  231. coption->meth->name,
  232. coption->name,
  233. 0
  234. };
  235. ur= falliblesubprocess(coption->meth->path,_("query/setup script"),fallibleargs);
  236. if (ur == urqr_normal) writecurrentopt();
  237. } else {
  238. ur= urqr_fail;
  239. }
  240. pop_cleanup(ehflag_normaltidy);
  241. return ur;
  242. }