method.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * method.cc - access method handling
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@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 <https://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 <string.h>
  31. #include <fcntl.h>
  32. #include <dirent.h>
  33. #include <signal.h>
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/dpkg-db.h>
  40. #include <dpkg/subproc.h>
  41. #include <dpkg/command.h>
  42. #include "dselect.h"
  43. #include "method.h"
  44. static const char *const methoddirectories[]= {
  45. LIBDIR "/" METHODSDIR,
  46. LOCALLIBDIR "/" METHODSDIR,
  47. nullptr
  48. };
  49. static char *methodlockfile = nullptr;
  50. static int methlockfd= -1;
  51. static void
  52. sthfailed(const char * reasoning)
  53. {
  54. curseson();
  55. clear();
  56. printw(_("\n\n%s: %s\n"), DSELECT, reasoning);
  57. attrset(A_BOLD);
  58. addstr(_("\nPress <enter> to continue."));
  59. attrset(A_NORMAL);
  60. refresh(); getch();
  61. }
  62. static void cu_unlockmethod(int, void**) {
  63. struct flock fl;
  64. assert(methodlockfile);
  65. assert(methlockfd);
  66. fl.l_type=F_UNLCK; fl.l_whence= SEEK_SET; fl.l_start=fl.l_len=0;
  67. if (fcntl(methlockfd,F_SETLK,&fl) == -1)
  68. sthfailed(_("cannot unlock access method area"));
  69. }
  70. static enum urqresult ensureoptions(void) {
  71. const char *const *ccpp;
  72. dselect_option *newoptions;
  73. int nread;
  74. if (!options) {
  75. newoptions = nullptr;
  76. nread= 0;
  77. for (ccpp= methoddirectories; *ccpp; ccpp++)
  78. readmethods(*ccpp, &newoptions, &nread);
  79. if (!newoptions) {
  80. sthfailed(_("no access methods are available"));
  81. return urqr_fail;
  82. }
  83. options= newoptions;
  84. noptions= nread;
  85. }
  86. return urqr_normal;
  87. }
  88. static enum urqresult lockmethod(void) {
  89. struct flock fl;
  90. if (methodlockfile == nullptr)
  91. methodlockfile = dpkg_db_get_path(METHLOCKFILE);
  92. if (methlockfd == -1) {
  93. methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
  94. if (methlockfd == -1) {
  95. if ((errno == EPERM) || (errno == EACCES)) {
  96. sthfailed(_("requested operation requires superuser privilege"));
  97. return urqr_fail;
  98. }
  99. sthfailed(_("cannot open or create access method lockfile"));
  100. return urqr_fail;
  101. }
  102. }
  103. fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
  104. if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
  105. if (errno == EACCES || errno == EAGAIN) {
  106. sthfailed(_("the access method area is already locked"));
  107. return urqr_fail;
  108. }
  109. sthfailed(_("cannot lock access method area"));
  110. return urqr_fail;
  111. }
  112. push_cleanup(cu_unlockmethod, ~0, nullptr, 0, 0);
  113. return urqr_normal;
  114. }
  115. static urqresult
  116. falliblesubprocess(struct command *cmd)
  117. {
  118. pid_t pid;
  119. int i, c;
  120. cursesoff();
  121. subproc_signals_ignore(cmd->name);
  122. pid = subproc_fork();
  123. if (pid == 0) {
  124. subproc_signals_cleanup(0, nullptr);
  125. command_exec(cmd);
  126. }
  127. fprintf(stderr, "\n");
  128. i = subproc_reap(pid, cmd->name, SUBPROC_WARN);
  129. subproc_signals_restore();
  130. if (i == 0) {
  131. sleep(1);
  132. return urqr_normal;
  133. }
  134. fprintf(stderr,_("Press <enter> to continue.\n"));
  135. m_output(stderr, _("<standard error>"));
  136. do {
  137. c = fgetc(stdin);
  138. } while ((c == EOF && errno == EINTR) || (c != '\n' && c != EOF));
  139. if (c == EOF)
  140. ohshite(_("error reading acknowledgement of program failure message"));
  141. return urqr_fail;
  142. }
  143. static urqresult runscript(const char *exepath, const char *name) {
  144. urqresult ur;
  145. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  146. ur=lockmethod(); if (ur != urqr_normal) return ur;
  147. getcurrentopt();
  148. if (coption) {
  149. struct command cmd;
  150. strcpy(coption->meth->pathinmeth,exepath);
  151. command_init(&cmd, coption->meth->path, name);
  152. command_add_args(&cmd, exepath, dpkg_db_get_dir(),
  153. coption->meth->name, coption->name, nullptr);
  154. ur = falliblesubprocess(&cmd);
  155. command_destroy(&cmd);
  156. } else {
  157. sthfailed(_("no access method is selected or configured"));
  158. ur= urqr_fail;
  159. }
  160. pop_cleanup(ehflag_normaltidy);
  161. return ur;
  162. }
  163. urqresult urq_update(void) {
  164. return runscript(METHODUPDATESCRIPT,_("update available list script"));
  165. }
  166. urqresult urq_install(void) {
  167. return runscript(METHODINSTALLSCRIPT,_("installation script"));
  168. }
  169. static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
  170. urqresult ur;
  171. struct command cmd;
  172. command_init(&cmd, DPKG, name);
  173. command_add_args(&cmd, DPKG, "--admindir", dpkg_db_get_dir(), "--pending",
  174. dpkgmode, nullptr);
  175. cursesoff();
  176. printf("running dpkg --pending %s ...\n",dpkgmode);
  177. fflush(stdout);
  178. ur = falliblesubprocess(&cmd);
  179. command_destroy(&cmd);
  180. return ur;
  181. }
  182. urqresult urq_remove(void) {
  183. return rundpkgauto("dpkg --remove","--remove");
  184. }
  185. urqresult urq_config(void) {
  186. return rundpkgauto("dpkg --configure","--configure");
  187. }
  188. urqresult urq_setup(void) {
  189. quitaction qa;
  190. urqresult ur;
  191. ur= ensureoptions(); if (ur != urqr_normal) return ur;
  192. ur=lockmethod(); if (ur != urqr_normal) return ur;
  193. getcurrentopt();
  194. curseson();
  195. methodlist *l= new methodlist();
  196. qa= l->display();
  197. delete l;
  198. if (qa == qa_quitchecksave) {
  199. struct command cmd;
  200. strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
  201. command_init(&cmd, coption->meth->path, _("query/setup script"));
  202. command_add_args(&cmd, METHODSETUPSCRIPT, dpkg_db_get_dir(),
  203. coption->meth->name, coption->name, nullptr);
  204. ur = falliblesubprocess(&cmd);
  205. command_destroy(&cmd);
  206. if (ur == urqr_normal) writecurrentopt();
  207. } else {
  208. ur= urqr_fail;
  209. }
  210. pop_cleanup(ehflag_normaltidy);
  211. return ur;
  212. }