script.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * dpkg - main program for package management
  3. * script.c - maintainer script routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2007-2012 Guillem Jover <guillem@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 <assert.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/dpkg-db.h>
  33. #include <dpkg/pkg.h>
  34. #include <dpkg/subproc.h>
  35. #include <dpkg/command.h>
  36. #include <dpkg/triglib.h>
  37. #include "filesdb.h"
  38. #include "infodb.h"
  39. #include "main.h"
  40. void
  41. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  42. {
  43. if (new_status < stat_triggersawaited)
  44. pkg_set_status(pkg, new_status);
  45. else if (pkg->trigaw.head)
  46. pkg_set_status(pkg, stat_triggersawaited);
  47. else if (pkg->trigpend_head)
  48. pkg_set_status(pkg, stat_triggerspending);
  49. else
  50. pkg_set_status(pkg, stat_installed);
  51. post_postinst_tasks_core(pkg);
  52. }
  53. void
  54. post_postinst_tasks_core(struct pkginfo *pkg)
  55. {
  56. modstatdb_note(pkg);
  57. if (!f_noact) {
  58. debug(dbg_triggersdetail,
  59. "post_postinst_tasks_core - trig_incorporate");
  60. trig_incorporate(msdbrw_write);
  61. }
  62. }
  63. static void
  64. post_script_tasks(void)
  65. {
  66. ensure_diversions();
  67. debug(dbg_triggersdetail,
  68. "post_script_tasks - ensure_diversions; trig_incorporate");
  69. trig_incorporate(msdbrw_write);
  70. }
  71. static void
  72. cu_post_script_tasks(int argc, void **argv)
  73. {
  74. post_script_tasks();
  75. }
  76. static void
  77. setexecute(const char *path, struct stat *stab)
  78. {
  79. if ((stab->st_mode & 0555) == 0555)
  80. return;
  81. if (!chmod(path, 0755))
  82. return;
  83. ohshite(_("unable to set execute permissions on `%.250s'"), path);
  84. }
  85. /**
  86. * Returns the path to the script inside the chroot.
  87. */
  88. static const char *
  89. preexecscript(struct command *cmd)
  90. {
  91. const char *admindir = dpkg_db_get_dir();
  92. size_t instdirl = strlen(instdir);
  93. if (*instdir) {
  94. if (strncmp(admindir, instdir, instdirl) != 0)
  95. ohshit(_("admindir must be inside instdir for dpkg to work properly"));
  96. if (setenv("DPKG_ADMINDIR", admindir + instdirl, 1) < 0)
  97. ohshite(_("unable to setenv for subprocesses"));
  98. if (chroot(instdir))
  99. ohshite(_("failed to chroot to `%.250s'"), instdir);
  100. if (chdir("/"))
  101. ohshite(_("failed to chdir to `%.255s'"), "/");
  102. }
  103. if (debug_has_flag(dbg_scripts)) {
  104. struct varbuf args = VARBUF_INIT;
  105. const char **argv = cmd->argv;
  106. while (*++argv) {
  107. varbuf_add_char(&args, ' ');
  108. varbuf_add_str(&args, *argv);
  109. }
  110. varbuf_end_str(&args);
  111. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename,
  112. args.buf);
  113. varbuf_destroy(&args);
  114. }
  115. if (!instdirl)
  116. return cmd->filename;
  117. assert(strlen(cmd->filename) >= instdirl);
  118. return cmd->filename + instdirl;
  119. }
  120. static int
  121. do_script(struct pkginfo *pkg, struct pkgbin *pkgbin,
  122. struct command *cmd, struct stat *stab, int warn)
  123. {
  124. pid_t pid;
  125. int r;
  126. setexecute(cmd->filename, stab);
  127. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  128. pid = subproc_fork();
  129. if (pid == 0) {
  130. if (setenv("DPKG_MAINTSCRIPT_PACKAGE", pkg->set->name, 1) ||
  131. setenv("DPKG_MAINTSCRIPT_ARCH", pkgbin->arch->name, 1) ||
  132. setenv("DPKG_MAINTSCRIPT_NAME", cmd->argv[0], 1) ||
  133. setenv("DPKG_RUNNING_VERSION", PACKAGE_VERSION, 1))
  134. ohshite(_("unable to setenv for maintainer script"));
  135. cmd->filename = cmd->argv[0] = preexecscript(cmd);
  136. command_exec(cmd);
  137. }
  138. subproc_signals_setup(cmd->name); /* This does a push_cleanup(). */
  139. r = subproc_wait_check(pid, cmd->name, warn);
  140. pop_cleanup(ehflag_normaltidy);
  141. pop_cleanup(ehflag_normaltidy);
  142. return r;
  143. }
  144. static int
  145. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  146. const char *desc, va_list args)
  147. {
  148. struct command cmd;
  149. const char *scriptpath;
  150. struct stat stab;
  151. char buf[100];
  152. scriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  153. sprintf(buf, _("installed %s script"), desc);
  154. command_init(&cmd, scriptpath, buf);
  155. command_add_arg(&cmd, scriptname);
  156. command_add_argv(&cmd, args);
  157. if (stat(scriptpath, &stab)) {
  158. command_destroy(&cmd);
  159. if (errno == ENOENT) {
  160. debug(dbg_scripts,
  161. "vmaintainer_script_installed nonexistent %s",
  162. scriptname);
  163. return 0;
  164. }
  165. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  166. }
  167. do_script(pkg, &pkg->installed, &cmd, &stab, 0);
  168. command_destroy(&cmd);
  169. return 1;
  170. }
  171. /*
  172. * All ...'s in maintainer_script_* are const char *'s.
  173. */
  174. int
  175. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  176. const char *desc, ...)
  177. {
  178. va_list args;
  179. int r;
  180. va_start(args, desc);
  181. r = vmaintainer_script_installed(pkg, scriptname, desc, args);
  182. va_end(args);
  183. if (r)
  184. post_script_tasks();
  185. return r;
  186. }
  187. int
  188. maintainer_script_postinst(struct pkginfo *pkg, ...)
  189. {
  190. va_list args;
  191. int r;
  192. va_start(args, pkg);
  193. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation",
  194. args);
  195. va_end(args);
  196. if (r)
  197. ensure_diversions();
  198. return r;
  199. }
  200. int
  201. maintainer_script_new(struct pkginfo *pkg,
  202. const char *scriptname, const char *desc,
  203. const char *cidir, char *cidirrest, ...)
  204. {
  205. struct command cmd;
  206. struct stat stab;
  207. va_list args;
  208. char buf[100];
  209. strcpy(cidirrest, scriptname);
  210. sprintf(buf, _("new %s script"), desc);
  211. va_start(args, cidirrest);
  212. command_init(&cmd, cidir, buf);
  213. command_add_arg(&cmd, scriptname);
  214. command_add_argv(&cmd, args);
  215. va_end(args);
  216. if (stat(cidir, &stab)) {
  217. command_destroy(&cmd);
  218. if (errno == ENOENT) {
  219. debug(dbg_scripts,
  220. "maintainer_script_new nonexistent %s '%s'",
  221. scriptname, cidir);
  222. return 0;
  223. }
  224. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  225. }
  226. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  227. command_destroy(&cmd);
  228. post_script_tasks();
  229. return 1;
  230. }
  231. int
  232. maintainer_script_alternative(struct pkginfo *pkg,
  233. const char *scriptname, const char *desc,
  234. const char *cidir, char *cidirrest,
  235. const char *ifok, const char *iffallback)
  236. {
  237. struct command cmd;
  238. const char *oldscriptpath;
  239. struct stat stab;
  240. char buf[100];
  241. oldscriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  242. sprintf(buf, _("old %s script"), desc);
  243. command_init(&cmd, oldscriptpath, buf);
  244. command_add_args(&cmd, scriptname, ifok,
  245. versiondescribe(&pkg->available.version, vdew_nonambig),
  246. NULL);
  247. if (stat(oldscriptpath, &stab)) {
  248. if (errno == ENOENT) {
  249. debug(dbg_scripts,
  250. "maintainer_script_alternative nonexistent %s '%s'",
  251. scriptname, oldscriptpath);
  252. command_destroy(&cmd);
  253. return 0;
  254. }
  255. warning(_("unable to stat %s '%.250s': %s"),
  256. cmd.name, oldscriptpath, strerror(errno));
  257. } else {
  258. if (!do_script(pkg, &pkg->installed, &cmd, &stab, PROCWARN)) {
  259. command_destroy(&cmd);
  260. post_script_tasks();
  261. return 1;
  262. }
  263. }
  264. notice(_("trying script from the new package instead ..."));
  265. strcpy(cidirrest, scriptname);
  266. sprintf(buf, _("new %s script"), desc);
  267. command_destroy(&cmd);
  268. command_init(&cmd, cidir, buf);
  269. command_add_args(&cmd, scriptname, iffallback,
  270. versiondescribe(&pkg->installed.version, vdew_nonambig),
  271. NULL);
  272. if (stat(cidir, &stab)) {
  273. command_destroy(&cmd);
  274. if (errno == ENOENT)
  275. ohshit(_("there is no script in the new version of the package - giving up"));
  276. else
  277. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  278. }
  279. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  280. notice(_("... it looks like that went OK"));
  281. command_destroy(&cmd);
  282. post_script_tasks();
  283. return 1;
  284. }