method.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but 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 <dpkg/command.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 == NULL)
  94. methodlockfile = dpkg_db_get_path(METHLOCKFILE);
  95. if (methlockfd == -1) {
  96. methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  97. if (methlockfd == -1) {
  98. if ((errno == EPERM) || (errno == EACCES)) {
  99. sthfailed("requested operation requires superuser privilege");
  100. return urqr_fail;
  101. }
  102. sthfailed("unable to open/create access method lockfile");
  103. return urqr_fail;
  104. }
  105. }
  106. fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
  107. if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
  108. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  109. sthfailed("the access method area is already locked");
  110. return urqr_fail;
  111. }
  112. sthfailed("unable to lock access method area");
  113. return urqr_fail;
  114. }
  115. push_cleanup(cu_unlockmethod,~0, 0,0, 0);
  116. return urqr_normal;
  117. }
  118. static urqresult
  119. falliblesubprocess(struct command *cmd)
  120. {
  121. pid_t pid;
  122. int status, i, c;
  123. cursesoff();
  124. subproc_signals_setup(cmd->name);
  125. pid = subproc_fork();
  126. if (pid == 0) {
  127. subproc_signals_cleanup(0, 0);
  128. command_exec(cmd);
  129. }
  130. status = subproc_wait(pid, cmd->name);
  131. pop_cleanup(ehflag_normaltidy);
  132. fprintf(stderr, "\n");
  133. i = subproc_check(status, cmd->name, PROCWARN);
  134. if (i == 0) {
  135. sleep(1);
  136. return urqr_normal;
  137. }
  138. fprintf(stderr,_("Press <enter> to continue.\n"));
  139. m_output(stderr, _("<standard error>"));
  140. do { c= fgetc(stdin); } while ((c == ERR && errno==EINTR) || ((c != '\n') && c != EOF));
  141. if ((c == ERR) || (c == EOF))
  142. ohshite(_("error reading acknowledgement of program failure message"));
  143. return urqr_fail;
  144. }
  145. static urqresult runscript(const char *exepath, const char *name) {
  146. urqresult ur;
  147. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  148. ur=lockmethod(); if (ur != urqr_normal) return ur;
  149. getcurrentopt();
  150. if (coption) {
  151. struct command cmd;
  152. strcpy(coption->meth->pathinmeth,exepath);
  153. command_init(&cmd, coption->meth->path, name);
  154. command_add_args(&cmd, exepath, admindir, coption->meth->name,
  155. coption->name, NULL);
  156. ur = falliblesubprocess(&cmd);
  157. command_destroy(&cmd);
  158. } else {
  159. sthfailed("no access method is selected/configured");
  160. ur= urqr_fail;
  161. }
  162. pop_cleanup(ehflag_normaltidy);
  163. return ur;
  164. }
  165. urqresult urq_update(void) {
  166. return runscript(METHODUPDATESCRIPT,_("update available list script"));
  167. }
  168. urqresult urq_install(void) {
  169. return runscript(METHODINSTALLSCRIPT,_("installation script"));
  170. }
  171. static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
  172. urqresult ur;
  173. struct command cmd;
  174. command_init(&cmd, DPKG, name);
  175. command_add_args(&cmd, DPKG, "--admindir", admindir, "--pending",
  176. dpkgmode, NULL);
  177. cursesoff();
  178. printf("running dpkg --pending %s ...\n",dpkgmode);
  179. fflush(stdout);
  180. ur = falliblesubprocess(&cmd);
  181. command_destroy(&cmd);
  182. return ur;
  183. }
  184. urqresult urq_remove(void) {
  185. return rundpkgauto("dpkg --remove","--remove");
  186. }
  187. urqresult urq_config(void) {
  188. return rundpkgauto("dpkg --configure","--configure");
  189. }
  190. urqresult urq_setup(void) {
  191. quitaction qa;
  192. urqresult ur;
  193. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  194. ur=lockmethod(); if (ur != urqr_normal) return ur;
  195. getcurrentopt();
  196. curseson();
  197. methodlist *l= new methodlist();
  198. qa= l->display();
  199. delete l;
  200. if (qa == qa_quitchecksave) {
  201. struct command cmd;
  202. strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
  203. command_init(&cmd, coption->meth->path, _("query/setup script"));
  204. command_add_args(&cmd, METHODSETUPSCRIPT, admindir, coption->meth->name,
  205. coption->name, NULL);
  206. ur = falliblesubprocess(&cmd);
  207. command_destroy(&cmd);
  208. if (ur == urqr_normal) writecurrentopt();
  209. } else {
  210. ur= urqr_fail;
  211. }
  212. pop_cleanup(ehflag_normaltidy);
  213. return ur;
  214. }