configure.c 20 KB

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