configure.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * dpkg - main program for package management
  3. * configure.c - configure packages
  4. *
  5. * Copyright 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. * Copyright 1999 Wichert Akkerman <wichert@deephackmode.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 <errno.h>
  23. #include <signal.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include <dirent.h>
  32. #include <ctype.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <assert.h>
  36. #include <sys/wait.h>
  37. #include <config.h>
  38. #include <dpkg.h>
  39. #include <dpkg-db.h>
  40. #include "filesdb.h"
  41. #include "main.h"
  42. int conffoptcells[2][2]= { CONFFOPTCELLS };
  43. static void md5hash(struct pkginfo *pkg, char **hashbuf, const char *fn);
  44. void deferred_configure(struct pkginfo *pkg) {
  45. /* The algorithm for deciding what to configure first is as follows:
  46. * Loop through all packages doing a `try 1' until we've been round
  47. * and nothing has been done, then do `try 2' and `try 3' likewise.
  48. * The incrementing of `dependtry' is done by process_queue().
  49. * Try 1:
  50. * Are all dependencies of this package done ? If so, do it.
  51. * Are any of the dependencies missing or the wrong version ?
  52. * If so, abort (unless --force-depends, in which case defer)
  53. * Will we need to configure a package we weren't given as an
  54. * argument ? If so, abort - except if --force-configure-any,
  55. * in which case we add the package to the argument list.
  56. * If none of the above, defer the package.
  57. * Try 2:
  58. * Find a cycle and break it (see above).
  59. * Do as for try 1.
  60. * Try 3 (only if --force-depends-version).
  61. * Same as for try 2, but don't mind version number in dependencies.
  62. * Try 4 (only if --force-depends).
  63. * Do anyway.
  64. */
  65. struct varbuf aemsgs, cdr, cdr2;
  66. char *cdr2rest;
  67. int ok, r, useredited, distedited, c, cc, status, c1;
  68. struct conffile *conff;
  69. char *currenthash= 0, *newdisthash= 0;
  70. struct stat stab;
  71. enum conffopt what;
  72. const char *s;
  73. if (pkg->status == stat_notinstalled)
  74. ohshit(_("no package named `%s' is installed, cannot configure"),pkg->name);
  75. if (pkg->status == stat_installed)
  76. ohshit(_("package %.250s is already installed and configured"), pkg->name);
  77. if (pkg->status != stat_unpacked && pkg->status != stat_halfconfigured)
  78. ohshit(_("package %.250s is not ready for configuration\n"
  79. " cannot configure (current status `%.250s')"),
  80. pkg->name, statusinfos[pkg->status].name);
  81. if (dependtry > 1) { if (findbreakcycle(pkg,0)) sincenothing= 0; }
  82. varbufinit(&aemsgs);
  83. ok= dependencies_ok(pkg,0,&aemsgs);
  84. if (ok == 1) {
  85. varbuffree(&aemsgs);
  86. pkg->clientdata->istobe= itb_installnew;
  87. add_to_queue(pkg);
  88. return;
  89. } else if (ok == 0) {
  90. sincenothing= 0;
  91. varbufaddc(&aemsgs,0);
  92. fprintf(stderr,
  93. _("dpkg: dependency problems prevent configuration of %s:\n%s"),
  94. pkg->name, aemsgs.buf);
  95. varbuffree(&aemsgs);
  96. ohshit(_("dependency problems - leaving unconfigured"));
  97. } else if (aemsgs.used) {
  98. varbufaddc(&aemsgs,0);
  99. fprintf(stderr,
  100. _("dpkg: %s: dependency problems, but configuring anyway as you request:\n%s"),
  101. pkg->name, aemsgs.buf);
  102. }
  103. varbuffree(&aemsgs);
  104. sincenothing= 0;
  105. if (pkg->eflag & eflagf_reinstreq)
  106. forcibleerr(fc_removereinstreq,
  107. _("Package is in a very bad inconsistent state - you should\n"
  108. " reinstall it before attempting configuration."));
  109. printf(_("Setting up %s (%s) ...\n"),pkg->name,
  110. versiondescribe(&pkg->installed.version,vdew_never));
  111. if (f_noact) {
  112. pkg->status= stat_installed;
  113. pkg->clientdata->istobe= itb_normal;
  114. return;
  115. }
  116. if (pkg->status == stat_unpacked) {
  117. debug(dbg_general,"deferred_configure updating conffiles");
  118. /* This will not do at all the right thing with overridden conffiles
  119. * or conffiles that are the `target' of an override; all the references
  120. * here would be to the `contested' filename, and in any case there'd
  121. * only be one hash for both `versions' of the conffile.
  122. *
  123. * Overriding conffiles is a silly thing to do anyway :-).
  124. */
  125. modstatdb_note(pkg);
  126. /* On entry, the `new' version of each conffile has been
  127. * unpacked as *.dpkg-new, and the `installed' version is
  128. * as-yet untouched in `*'. The hash of the `old distributed'
  129. * version is in the conffiles data for the package.
  130. * If `*.dpkg-new' no longer exists we assume that we've already
  131. * processed this one.
  132. */
  133. varbufinit(&cdr);
  134. varbufinit(&cdr2);
  135. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  136. r= conffderef(pkg, &cdr, conff->name);
  137. if (r == -1) {
  138. conff->hash= nfstrsave("-");
  139. continue;
  140. }
  141. md5hash(pkg,&currenthash,cdr.buf);
  142. varbufreset(&cdr2);
  143. varbufaddstr(&cdr2,cdr.buf);
  144. cdr2.used+=50; varbufaddc(&cdr2,0); cdr2rest= cdr2.buf+strlen(cdr.buf);
  145. /* From now on we can just strcpy(cdr2rest,extension); */
  146. strcpy(cdr2rest,DPKGNEWEXT);
  147. /* If the .dpkg-new file is no longer there, ignore this one. */
  148. if (lstat(cdr2.buf,&stab)) {
  149. if (errno == ENOENT) continue;
  150. ohshite(_("unable to stat new dist conffile `%.250s'"),cdr2.buf);
  151. }
  152. md5hash(pkg,&newdisthash,cdr2.buf);
  153. /* Copy the permissions from the installed version to the new
  154. * distributed version.
  155. */
  156. if (!stat(cdr.buf,&stab)) {
  157. if (chown(cdr2.buf,stab.st_uid,stab.st_gid))
  158. ohshite(_("unable to change ownership of new dist conffile `%.250s'"),cdr2.buf);
  159. if (chmod(cdr2.buf,stab.st_mode & 07777))
  160. if (errno != ENOENT)
  161. ohshite(_("unable to set mode of new dist conffile `%.250s'"),cdr2.buf);
  162. } else {
  163. if (errno != ENOENT)
  164. ohshite(_("unable to stat current installed conffile `%.250s'"),cdr.buf);
  165. }
  166. if (!strcmp(currenthash,newdisthash)) {
  167. /* They're both the same so there's no point asking silly questions. */
  168. useredited= -1;
  169. distedited= -1;
  170. what= cfo_identical;
  171. } else if (!strcmp(currenthash,NONEXISTENTFLAG) && fc_conff_miss) {
  172. fprintf(stderr, _("\nConfiguration file `%s', does not exist on system.\n"
  173. "Installing new config file as you request.\n"), conff->name);
  174. what= cfo_newconff;
  175. useredited= -1;
  176. distedited= -1;
  177. } else if (!strcmp(conff->hash,NEWCONFFILEFLAG)) {
  178. if (!strcmp(currenthash,NONEXISTENTFLAG)) {
  179. what= cfo_newconff;
  180. useredited= -1;
  181. distedited= -1;
  182. } else {
  183. useredited= 1;
  184. distedited= 1;
  185. what= conffoptcells[useredited][distedited] | cfof_isnew;
  186. }
  187. } else {
  188. useredited= strcmp(conff->hash,currenthash) != 0;
  189. distedited= strcmp(conff->hash,newdisthash) != 0;
  190. what= conffoptcells[useredited][distedited];
  191. }
  192. debug(dbg_conff,
  193. "deferred_configure `%s' (= `%s') useredited=%d distedited=%d what=%o",
  194. conff->name, cdr.buf, useredited, distedited, what);
  195. /* When prompting we check for some of the force options. There are
  196. * several cases in which these occur.
  197. * - We have --force-confnew, in which we always use the new config file
  198. * - We have --force-confold, in which we always use the old config file
  199. * - We have --force-confdef, in which we always use the default action
  200. * if there is one. Note, there are cases where we will still prompt
  201. * - If we have --force-confdef and one of the others (new/old) then we
  202. * always use the default where we can. If there is no default, then we
  203. * use the new or old, depending on which --force-conf{old,new} was used.
  204. */
  205. if (what & cfof_prompt) {
  206. do {
  207. fprintf(stderr, _("\nConfiguration file `%s'"), conff->name);
  208. if (strcmp(conff->name,cdr.buf))
  209. fprintf(stderr,_(" (actually `%s')"),cdr.buf);
  210. if (cfof_isnew) {
  211. fprintf(stderr,
  212. _("\n"
  213. " ==> File on system created by you or by a script.\n"
  214. " ==> File also in package provided by package maintainer.\n"));
  215. } else {
  216. fprintf(stderr, useredited ?
  217. _("\n ==> Modified (by you or by a script) since installation.\n") :
  218. _("\n Not modified since installation.\n"));
  219. fprintf(stderr, distedited ?
  220. _(" ==> Package distributor has shipped an updated version.\n") :
  221. _(" Version in package is the same as at last installation.\n"));
  222. }
  223. if (!(fc_conff_def && (what & (cfof_install|cfof_keep)))) {
  224. if (fc_conff_new) {
  225. fprintf(stderr, _(" ==> Using new file as you requested.\n"));
  226. cc = 'y';
  227. break;
  228. } else if (fc_conff_old) {
  229. fprintf(stderr, _(" ==> Using current old file as you requested.\n"));
  230. cc = 'n';
  231. break;
  232. }
  233. }
  234. if (what & cfof_keep && fc_conff_def) {
  235. fprintf(stderr, _(" ==> Keeping old config file as default.\n"));
  236. cc = 'n';
  237. break;
  238. } else if (what & cfof_install && fc_conff_def) {
  239. fprintf(stderr, _(" ==> Using new config file as default.\n"));
  240. cc = 'y';
  241. break;
  242. }
  243. fprintf(stderr,
  244. _(" What would you like to do about it ? Your options are:\n"
  245. " Y or I : install the package maintainer's version\n"
  246. " N or O : keep your currently-installed version\n"
  247. " D : show the differences between the versions\n"
  248. " Z : background this process to examine the situation\n"));
  249. if (what & cfof_keep)
  250. fprintf(stderr, _(" The default action is to keep your current version.\n"));
  251. else if (what & cfof_install)
  252. fprintf(stderr, _(" The default action is to install the new version.\n"));
  253. s= strrchr(conff->name,'/');
  254. if (!s || !*++s) s= conff->name;
  255. fprintf(stderr, "*** %s (Y/I/N/O/D/Z) %s ? ",
  256. s,
  257. (what & cfof_keep) ? _("[default=N]") :
  258. (what & cfof_install) ? _("[default=Y]") : _("[no default]"));
  259. if (ferror(stderr))
  260. ohshite(_("error writing to stderr, discovered before conffile prompt"));
  261. cc= 0;
  262. while ((c= getchar()) != EOF && c != '\n')
  263. if (!isspace(c) && !cc) cc= tolower(c);
  264. if (c == EOF) {
  265. if (ferror(stdin)) ohshite(_("read error on stdin at conffile prompt"));
  266. ohshit(_("EOF on stdin at conffile prompt"));
  267. }
  268. if (!cc) {
  269. if (what & cfof_keep) { cc= 'n'; break; }
  270. else if (what & cfof_install) { cc= 'y'; break; }
  271. }
  272. /* fixme: say something if silently not install */
  273. if (cc == 'd') {
  274. if (!(c1= m_fork())) {
  275. const char* p;
  276. char cmdbuf[1024];
  277. p= getenv(PAGERENV);
  278. if (!p || !*p) p= DEFAULTPAGER;
  279. sprintf(cmdbuf, DIFF " -Nu %.250s %.250s | %.250s", cdr.buf, cdr2.buf, p);
  280. s= getenv(SHELLENV);
  281. if (!s || !*s) s= DEFAULTSHELL;
  282. execlp(s,s,"-c", cmdbuf, NULL);
  283. ohshite(_("failed to run %s (%.250s)"), DIFF, cmdbuf);
  284. }
  285. while ((r= waitpid(c1,&status,0)) == -1 && errno == EINTR);
  286. if (r != c1) { onerr_abort++; ohshite(_("wait for shell failed")); }
  287. }
  288. if (cc == 'z') {
  289. strcpy(cdr2rest, DPKGNEWEXT);
  290. fprintf(stderr,
  291. _("Your currently installed version of the file is in:\n"
  292. " %s\n"
  293. "The version contained in the new version of the package is in:\n"
  294. " %s\n"
  295. "If you decide to take care of the update yourself, perhaps by editing\n"
  296. " the installed version, you should choose `N' when you return, so that\n"
  297. " I do not mess up your careful work.\n"),
  298. cdr.buf, cdr2.buf);
  299. s= getenv(NOJOBCTRLSTOPENV);
  300. if (s && *s) {
  301. fputs(_("Type `exit' when you're done.\n"),stderr);
  302. if (!(c1= m_fork())) {
  303. s= getenv(SHELLENV);
  304. if (!s || !*s) s= DEFAULTSHELL;
  305. execlp(s,s,"-i",(char*)0);
  306. ohshite(_("failed to exec shell (%.250s)"),s);
  307. }
  308. while ((r= waitpid(c1,&status,0)) == -1 && errno == EINTR);
  309. if (r != c1) { onerr_abort++; ohshite(_("wait for shell failed")); }
  310. } else {
  311. fputs(_("Don't forget to foreground (`fg') this "
  312. "process when you're done !\n"),stderr);
  313. kill(-getpgid(0),SIGTSTP);
  314. }
  315. }
  316. } while (!strchr("yino",cc));
  317. switch (cc) {
  318. case 'i': case 'y': what= cfof_install | cfof_backup; break;
  319. case 'n': case 'o': what= cfof_keep | cfof_backup; break;
  320. default: internerr("unknown response");
  321. }
  322. }
  323. switch (what & ~cfof_isnew) {
  324. case cfo_keep | cfof_backup:
  325. strcpy(cdr2rest,DPKGOLDEXT);
  326. if (unlink(cdr2.buf) && errno != ENOENT)
  327. fprintf(stderr,
  328. _("dpkg: %s: warning - failed to remove old backup `%.250s': %s\n"),
  329. pkg->name, cdr2.buf, strerror(errno));
  330. cdr.used--;
  331. varbufaddstr(&cdr,DPKGDISTEXT);
  332. varbufaddc(&cdr,0);
  333. strcpy(cdr2rest,DPKGNEWEXT);
  334. if (rename(cdr2.buf,cdr.buf))
  335. fprintf(stderr,
  336. _("dpkg: %s: warning - failed to rename `%.250s' to `%.250s': %s\n"),
  337. pkg->name, cdr2.buf, cdr.buf, strerror(errno));
  338. break;
  339. case cfo_keep:
  340. strcpy(cdr2rest,DPKGNEWEXT);
  341. if (unlink(cdr2.buf))
  342. fprintf(stderr,
  343. _("dpkg: %s: warning - failed to remove `%.250s': %s\n"),
  344. pkg->name, cdr2.buf, strerror(errno));
  345. break;
  346. case cfo_install | cfof_backup:
  347. strcpy(cdr2rest,DPKGDISTEXT);
  348. if (unlink(cdr2.buf) && errno != ENOENT)
  349. fprintf(stderr,
  350. _("dpkg: %s: warning - failed to remove old distrib version `%.250s': %s\n"),
  351. pkg->name, cdr2.buf, strerror(errno));
  352. strcpy(cdr2rest,DPKGOLDEXT);
  353. if (unlink(cdr2.buf) && errno != ENOENT)
  354. fprintf(stderr,
  355. _("dpkg: %s: warning - failed to remove `%.250s' (before overwrite): %s\n"),
  356. pkg->name, cdr2.buf, strerror(errno));
  357. if (link(cdr.buf,cdr2.buf))
  358. fprintf(stderr,
  359. _("dpkg: %s: warning - failed to link `%.250s' to `%.250s': %s\n"),
  360. pkg->name, cdr.buf, cdr2.buf, strerror(errno));
  361. /* fall through */
  362. case cfo_install:
  363. printf(_("Installing new version of config file %s ...\n"),conff->name);
  364. case cfo_newconff:
  365. strcpy(cdr2rest,DPKGNEWEXT);
  366. if (rename(cdr2.buf,cdr.buf))
  367. ohshite(_("unable to install `%.250s' as `%.250s'"),cdr2.buf,cdr.buf);
  368. break;
  369. default:
  370. internerr("unknown what");
  371. }
  372. conff->hash= nfstrsave(newdisthash);
  373. modstatdb_note(pkg);
  374. free(newdisthash);
  375. free(currenthash);
  376. } /* for (conff= ... */
  377. varbuffree(&cdr);
  378. varbuffree(&cdr2);
  379. pkg->status= stat_halfconfigured;
  380. }
  381. assert(pkg->status == stat_halfconfigured);
  382. modstatdb_note(pkg);
  383. if (maintainer_script_installed(pkg, POSTINSTFILE, "post-installation",
  384. "configure",
  385. informativeversion(&pkg->configversion)
  386. ? versiondescribe(&pkg->configversion,
  387. vdew_nonambig)
  388. : "",
  389. (char*)0))
  390. putchar('\n');
  391. pkg->status= stat_installed;
  392. pkg->eflag= eflagv_ok;
  393. modstatdb_note(pkg);
  394. }
  395. int conffderef(struct pkginfo *pkg, struct varbuf *result, const char *in) {
  396. /* returns 0 if all OK, -1 if some kind of error. */
  397. static char *linkreadbuf= 0;
  398. static int linkreadbufsize= 0;
  399. struct stat stab;
  400. int r, need;
  401. int loopprotect;
  402. varbufreset(result);
  403. varbufaddstr(result,instdir);
  404. if (*in != '/') varbufaddc(result,'/');
  405. varbufaddstr(result,in);
  406. varbufaddc(result,0);
  407. loopprotect= 0;
  408. for (;;) {
  409. debug(dbg_conffdetail,"conffderef in=`%s' current working=`%s'", in, result->buf);
  410. if (lstat(result->buf,&stab)) {
  411. if (errno != ENOENT)
  412. fprintf(stderr, _("dpkg: %s: warning - unable to stat config file `%s'\n"
  413. " (= `%s'): %s\n"),
  414. pkg->name, in, result->buf, strerror(errno));
  415. debug(dbg_conffdetail,"conffderef nonexistent");
  416. return 0;
  417. } else if (S_ISREG(stab.st_mode)) {
  418. debug(dbg_conff,"conffderef in=`%s' result=`%s'", in, result->buf);
  419. return 0;
  420. } else if (S_ISLNK(stab.st_mode)) {
  421. debug(dbg_conffdetail,"conffderef symlink loopprotect=%d",loopprotect);
  422. if (loopprotect++ >= 25) {
  423. fprintf(stderr, _("dpkg: %s: warning - config file `%s' is a circular link\n"
  424. " (= `%s')\n"), pkg->name, in, result->buf);
  425. return -1;
  426. }
  427. need= 255;
  428. for (;;) {
  429. if (need > linkreadbufsize) {
  430. linkreadbuf= m_realloc(linkreadbuf,need);
  431. linkreadbufsize= need;
  432. debug(dbg_conffdetail,"conffderef readlink realloc(%d)=%p",need,linkreadbuf);
  433. }
  434. r= readlink(result->buf,linkreadbuf,linkreadbufsize-1);
  435. if (r < 0) {
  436. fprintf(stderr, _("dpkg: %s: warning - unable to readlink conffile `%s'\n"
  437. " (= `%s'): %s\n"),
  438. pkg->name, in, result->buf, strerror(errno));
  439. return -1;
  440. }
  441. debug(dbg_conffdetail,"conffderef readlink gave %d, `%.*s'",
  442. r, r>0 ? r : 0, linkreadbuf);
  443. if (r < linkreadbufsize-1) break;
  444. need= r<<2;
  445. }
  446. linkreadbuf[r]= 0;
  447. if (linkreadbuf[0] == '/') {
  448. varbufreset(result);
  449. varbufaddstr(result,instdir);
  450. debug(dbg_conffdetail,"conffderef readlink absolute");
  451. } else {
  452. for (r=result->used-2; r>0 && result->buf[r] != '/'; r--);
  453. if (r < 0) {
  454. fprintf(stderr,
  455. _("dpkg: %s: warning - conffile `%.250s' resolves to degenerate filename\n"
  456. " (`%s' is a symlink to `%s')\n"),
  457. pkg->name, in, result->buf, linkreadbuf);
  458. return -1;
  459. }
  460. if (result->buf[r] == '/') r++;
  461. result->used= r;
  462. debug(dbg_conffdetail,"conffderef readlink relative to `%.*s'",
  463. (int)result->used, result->buf);
  464. }
  465. varbufaddstr(result,linkreadbuf);
  466. varbufaddc(result,0);
  467. } else {
  468. fprintf(stderr, _("dpkg: %s: warning - conffile `%.250s' is not a plain"
  469. " file or symlink (= `%s')\n"),
  470. pkg->name, in, result->buf);
  471. return -1;
  472. }
  473. }
  474. }
  475. static void md5hash(struct pkginfo *pkg, char **hashbuf, const char *fn) {
  476. static int fd;
  477. fd= open(fn,O_RDONLY);
  478. if (fd >= 0) {
  479. push_cleanup(cu_closefd,ehflag_bombout, 0,0, 1,&fd);
  480. fd_md5(fd, hashbuf, -1, _("md5hash"));
  481. pop_cleanup(ehflag_normaltidy); /* fd= open(cdr.buf) */
  482. close(fd);
  483. } else if (errno == ENOENT) {
  484. *hashbuf= strdup(NONEXISTENTFLAG);
  485. } else {
  486. fprintf(stderr, _("dpkg: %s: warning - unable to open conffile %s for hash: %s\n"),
  487. pkg->name, fn, strerror(errno));
  488. *hashbuf= strdup("-");
  489. }
  490. }