configure.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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_unpacked && pkg->status != stat_halfconfigured)
  75. ohshit(_("package %.250s is not ready for configuration\n"
  76. " cannot configure (current status `%.250s')"),
  77. pkg->name, statusinfos[pkg->status].name);
  78. if (dependtry > 1) { if (findbreakcycle(pkg,0)) sincenothing= 0; }
  79. varbufinit(&aemsgs);
  80. ok= dependencies_ok(pkg,0,&aemsgs);
  81. if (ok == 1) {
  82. varbuffree(&aemsgs);
  83. pkg->clientdata->istobe= itb_installnew;
  84. add_to_queue(pkg);
  85. return;
  86. } else if (ok == 0) {
  87. sincenothing= 0;
  88. varbufaddc(&aemsgs,0);
  89. fprintf(stderr,
  90. _("dpkg: dependency problems prevent configuration of %s:\n%s"),
  91. pkg->name, aemsgs.buf);
  92. varbuffree(&aemsgs);
  93. ohshit(_("dependency problems - leaving unconfigured"));
  94. } else if (aemsgs.used) {
  95. varbufaddc(&aemsgs,0);
  96. fprintf(stderr,
  97. _("dpkg: %s: dependency problems, but configuring anyway as you request:\n%s"),
  98. pkg->name, aemsgs.buf);
  99. }
  100. varbuffree(&aemsgs);
  101. sincenothing= 0;
  102. if (pkg->eflag & eflagf_reinstreq)
  103. forcibleerr(fc_removereinstreq,
  104. _("Package is in a very bad inconsistent state - you should\n"
  105. " reinstall it before attempting configuration."));
  106. printf(_("Setting up %s (%s) ...\n"),pkg->name,
  107. versiondescribe(&pkg->installed.version,vdew_never));
  108. if (f_noact) {
  109. pkg->status= stat_installed;
  110. pkg->clientdata->istobe= itb_normal;
  111. return;
  112. }
  113. if (pkg->status == stat_unpacked) {
  114. debug(dbg_general,"deferred_configure updating conffiles");
  115. /* This will not do at all the right thing with overridden conffiles
  116. * or conffiles that are the `target' of an override; all the references
  117. * here would be to the `contested' filename, and in any case there'd
  118. * only be one hash for both `versions' of the conffile.
  119. *
  120. * Overriding conffiles is a silly thing to do anyway :-).
  121. */
  122. modstatdb_note(pkg);
  123. /* On entry, the `new' version of each conffile has been
  124. * unpacked as *.dpkg-new, and the `installed' version is
  125. * as-yet untouched in `*'. The hash of the `old distributed'
  126. * version is in the conffiles data for the package.
  127. * If `*.dpkg-new' no longer exists we assume that we've already
  128. * processed this one.
  129. */
  130. varbufinit(&cdr);
  131. varbufinit(&cdr2);
  132. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  133. r= conffderef(pkg, &cdr, conff->name);
  134. if (r == -1) {
  135. conff->hash= nfstrsave("-");
  136. continue;
  137. }
  138. md5hash(pkg,currenthash,cdr.buf);
  139. varbufreset(&cdr2);
  140. varbufaddstr(&cdr2,cdr.buf);
  141. cdr2.used+=50; varbufaddc(&cdr2,0); cdr2rest= cdr2.buf+strlen(cdr.buf);
  142. /* From now on we can just strcpy(cdr2rest,extension); */
  143. strcpy(cdr2rest,DPKGNEWEXT);
  144. /* If the .dpkg-new file is no longer there, ignore this one. */
  145. if (lstat(cdr2.buf,&stab)) {
  146. if (errno == ENOENT) continue;
  147. ohshite(_("unable to stat new dist conffile `%.250s'"),cdr2.buf);
  148. }
  149. md5hash(pkg,newdisthash,cdr2.buf);
  150. /* Copy the permissions from the installed version to the new
  151. * distributed version.
  152. */
  153. if (!stat(cdr.buf,&stab)) {
  154. if (chown(cdr2.buf,stab.st_uid,stab.st_gid))
  155. ohshite(_("unable to change ownership of new dist conffile `%.250s'"),cdr2.buf);
  156. if (chmod(cdr2.buf,stab.st_mode & 07777))
  157. if (errno != ENOENT)
  158. ohshite(_("unable to set mode of new dist conffile `%.250s'"),cdr2.buf);
  159. } else {
  160. if (errno != ENOENT)
  161. ohshite(_("unable to stat current installed conffile `%.250s'"),cdr.buf);
  162. }
  163. if (!strcmp(currenthash,newdisthash)) {
  164. /* They're both the same so there's no point asking silly questions. */
  165. useredited= -1;
  166. distedited= -1;
  167. what= cfo_identical;
  168. } else if (!strcmp(conff->hash,NEWCONFFILEFLAG)) {
  169. if (!strcmp(currenthash,NONEXISTENTFLAG)) {
  170. what= cfo_newconff;
  171. useredited= -1;
  172. distedited= -1;
  173. } else {
  174. useredited= 1;
  175. distedited= 1;
  176. what= conffoptcells[useredited][distedited] | cfof_isnew;
  177. }
  178. } else {
  179. useredited= strcmp(conff->hash,currenthash) != 0;
  180. distedited= strcmp(conff->hash,newdisthash) != 0;
  181. what= conffoptcells[useredited][distedited];
  182. }
  183. debug(dbg_conff,
  184. "deferred_configure `%s' (= `%s') useredited=%d distedited=%d what=%o",
  185. conff->name, cdr.buf, useredited, distedited, what);
  186. if (what & cfof_prompt) {
  187. do {
  188. fprintf(stderr, _("\nConfiguration file `%s'"), conff->name);
  189. if (strcmp(conff->name,cdr.buf))
  190. fprintf(stderr,_(" (actually `%s')"),cdr.buf);
  191. if (cfof_isnew) {
  192. fprintf(stderr,
  193. _("\n"
  194. " ==> File on system created by you or by a script.\n"
  195. " ==> File also in package provided by package maintainer.\n"));
  196. } else {
  197. fprintf(stderr, useredited ?
  198. _("\n ==> Modified (by you or by a script) since installation.\n") :
  199. _("\n Not modified since installation.\n"));
  200. fprintf(stderr, distedited ?
  201. _(" ==> Package distributor has shipped an updated version.\n") :
  202. _(" Version in package is the same as at last installation.\n"));
  203. }
  204. fprintf(stderr,
  205. _(" What would you like to do about it ? Your options are:\n"
  206. " Y or I : install the package maintainer's version\n"
  207. " N or O : keep your currently-installed version\n"
  208. " Z : background this process to examine the situation\n"));
  209. if (what & cfof_keep)
  210. fprintf(stderr, _(" The default action is to keep your current version.\n"));
  211. else if (what & cfof_install)
  212. fprintf(stderr, _(" The default action is to install the new version.\n"));
  213. s= strrchr(conff->name,'/');
  214. if (!s || !*++s) s= conff->name;
  215. fprintf(stderr, "*** %s (Y/I/N/O/Z) %s ? ",
  216. s,
  217. (what & cfof_keep) ? _("[default=N]") :
  218. (what & cfof_install) ? _("[default=Y]") : _("[no default]"));
  219. if (ferror(stderr))
  220. ohshite(_("error writing to stderr, discovered before conffile prompt"));
  221. cc= 0;
  222. while ((c= getchar()) != EOF && c != '\n')
  223. if (!isspace(c) && !cc) cc= tolower(c);
  224. if (c == EOF) {
  225. if (ferror(stdin)) ohshite(_("read error on stdin at conffile prompt"));
  226. ohshit(_("EOF on stdin at conffile prompt"));
  227. }
  228. if (!cc) {
  229. if (what & cfof_keep) { cc= 'n'; break; }
  230. else if (what & cfof_install) { cc= 'y'; break; }
  231. }
  232. /* fixme: say something if silently not install */
  233. if (cc == 'z') {
  234. strcpy(cdr2rest, DPKGNEWEXT);
  235. fprintf(stderr,
  236. _("Your currently installed version of the file is in:\n"
  237. " %s\n"
  238. "The version contained in the new version of the package is in:\n"
  239. " %s\n"
  240. "If you decide to take care of the update yourself, perhaps by editing\n"
  241. " the installed version, you should choose `N' when you return, so that\n"
  242. " I do not mess up your careful work.\n"),
  243. cdr.buf, cdr2.buf);
  244. s= getenv(NOJOBCTRLSTOPENV);
  245. if (s && *s) {
  246. fputs(_("Type `exit' when you're done.\n"),stderr);
  247. if (!(c1= m_fork())) {
  248. s= getenv(SHELLENV);
  249. if (!s || !*s) s= DEFAULTSHELL;
  250. execlp(s,s,"-i",(char*)0);
  251. ohshite(_("failed to exec shell (%.250s)"),s);
  252. }
  253. while ((r= waitpid(c1,&status,0)) == -1 && errno == EINTR);
  254. if (r != c1) { onerr_abort++; ohshite(_("wait for shell failed")); }
  255. } else {
  256. fputs(_("Don't forget to foreground (`fg') this "
  257. "process when you're done !\n"),stderr);
  258. kill(-getpgid(0),SIGTSTP);
  259. }
  260. }
  261. } while (!strchr("yino",cc));
  262. switch (cc) {
  263. case 'i': case 'y': what= cfof_install | cfof_backup; break;
  264. case 'n': case 'o': what= cfof_keep | cfof_backup; break;
  265. default: internerr("unknown response");
  266. }
  267. }
  268. switch (what & ~cfof_isnew) {
  269. case cfo_keep | cfof_backup:
  270. strcpy(cdr2rest,DPKGOLDEXT);
  271. if (unlink(cdr2.buf) && errno != ENOENT)
  272. fprintf(stderr,
  273. _("dpkg: %s: warning - failed to remove old backup `%.250s': %s\n"),
  274. pkg->name, cdr2.buf, strerror(errno));
  275. cdr.used--;
  276. varbufaddstr(&cdr,DPKGDISTEXT);
  277. varbufaddc(&cdr,0);
  278. strcpy(cdr2rest,DPKGNEWEXT);
  279. if (rename(cdr2.buf,cdr.buf))
  280. fprintf(stderr,
  281. _("dpkg: %s: warning - failed to rename `%.250s' to `%.250s': %s\n"),
  282. pkg->name, cdr2.buf, cdr.buf, strerror(errno));
  283. break;
  284. case cfo_keep:
  285. strcpy(cdr2rest,DPKGNEWEXT);
  286. if (unlink(cdr2.buf))
  287. fprintf(stderr,
  288. _("dpkg: %s: warning - failed to remove `%.250s': %s\n"),
  289. pkg->name, cdr2.buf, strerror(errno));
  290. break;
  291. case cfo_install | cfof_backup:
  292. strcpy(cdr2rest,DPKGDISTEXT);
  293. if (unlink(cdr2.buf) && errno != ENOENT)
  294. fprintf(stderr,
  295. _("dpkg: %s: warning - failed to remove old distrib version `%.250s': %s\n"),
  296. pkg->name, cdr2.buf, strerror(errno));
  297. strcpy(cdr2rest,DPKGOLDEXT);
  298. if (unlink(cdr2.buf) && errno != ENOENT)
  299. fprintf(stderr,
  300. _("dpkg: %s: warning - failed to remove `%.250s' (before overwrite): %s\n"),
  301. pkg->name, cdr2.buf, strerror(errno));
  302. if (link(cdr.buf,cdr2.buf))
  303. fprintf(stderr,
  304. _("dpkg: %s: warning - failed to link `%.250s' to `%.250s': %s\n"),
  305. pkg->name, cdr.buf, cdr2.buf, strerror(errno));
  306. /* fall through */
  307. case cfo_install:
  308. printf(_("Installing new version of config file %s ...\n"),conff->name);
  309. case cfo_newconff:
  310. strcpy(cdr2rest,DPKGNEWEXT);
  311. if (rename(cdr2.buf,cdr.buf))
  312. ohshite(_("unable to install `%.250s' as `%.250s'"),cdr2.buf,cdr.buf);
  313. break;
  314. default:
  315. internerr("unknown what");
  316. }
  317. conff->hash= nfstrsave(newdisthash);
  318. modstatdb_note(pkg);
  319. } /* for (conff= ... */
  320. varbuffree(&cdr);
  321. varbuffree(&cdr2);
  322. pkg->status= stat_halfconfigured;
  323. }
  324. assert(pkg->status == stat_halfconfigured);
  325. modstatdb_note(pkg);
  326. if (maintainer_script_installed(pkg, POSTINSTFILE, "post-installation",
  327. "configure",
  328. informativeversion(&pkg->configversion)
  329. ? versiondescribe(&pkg->configversion,
  330. vdew_nonambig)
  331. : "",
  332. (char*)0))
  333. putchar('\n');
  334. pkg->status= stat_installed;
  335. pkg->eflag= eflagv_ok;
  336. modstatdb_note(pkg);
  337. }
  338. int conffderef(struct pkginfo *pkg, struct varbuf *result, const char *in) {
  339. /* returns 0 if all OK, -1 if some kind of error. */
  340. static char *linkreadbuf= 0;
  341. static int linkreadbufsize= 0;
  342. struct stat stab;
  343. int r, need;
  344. int loopprotect;
  345. varbufreset(result);
  346. varbufaddstr(result,instdir);
  347. if (*in != '/') varbufaddc(result,'/');
  348. varbufaddstr(result,in);
  349. varbufaddc(result,0);
  350. loopprotect= 0;
  351. for (;;) {
  352. debug(dbg_conffdetail,"conffderef in=`%s' current working=`%s'", in, result->buf);
  353. if (lstat(result->buf,&stab)) {
  354. if (errno != ENOENT)
  355. fprintf(stderr, _("dpkg: %s: warning - unable to stat config file `%s'\n"
  356. " (= `%s'): %s\n"),
  357. pkg->name, in, result->buf, strerror(errno));
  358. debug(dbg_conffdetail,"conffderef nonexistent");
  359. return 0;
  360. } else if (S_ISREG(stab.st_mode)) {
  361. debug(dbg_conff,"conffderef in=`%s' result=`%s'", in, result->buf);
  362. return 0;
  363. } else if (S_ISLNK(stab.st_mode)) {
  364. debug(dbg_conffdetail,"conffderef symlink loopprotect=%d",loopprotect);
  365. if (loopprotect++ >= 25) {
  366. fprintf(stderr, _("dpkg: %s: warning - config file `%s' is a circular link\n"
  367. " (= `%s')\n"), pkg->name, in, result->buf);
  368. return -1;
  369. }
  370. need= 255;
  371. for (;;) {
  372. if (need > linkreadbufsize) {
  373. linkreadbuf= m_realloc(linkreadbuf,need);
  374. linkreadbufsize= need;
  375. debug(dbg_conffdetail,"conffderef readlink realloc(%d)=%p",need,linkreadbuf);
  376. }
  377. r= readlink(result->buf,linkreadbuf,linkreadbufsize-1);
  378. if (r < 0) {
  379. fprintf(stderr, _("dpkg: %s: warning - unable to readlink conffile `%s'\n"
  380. " (= `%s'): %s\n"),
  381. pkg->name, in, result->buf, strerror(errno));
  382. return -1;
  383. }
  384. debug(dbg_conffdetail,"conffderef readlink gave %d, `%.*s'",
  385. r, r>0 ? r : 0, linkreadbuf);
  386. if (r < linkreadbufsize-1) break;
  387. need= r<<2;
  388. }
  389. linkreadbuf[r]= 0;
  390. if (linkreadbuf[0] == '/') {
  391. varbufreset(result);
  392. varbufaddstr(result,instdir);
  393. debug(dbg_conffdetail,"conffderef readlink absolute");
  394. } else {
  395. for (r=result->used-2; r>0 && result->buf[r] != '/'; r--);
  396. if (r < 0) {
  397. fprintf(stderr,
  398. _("dpkg: %s: warning - conffile `%.250s' resolves to degenerate filename\n"
  399. " (`%s' is a symlink to `%s')\n"),
  400. pkg->name, in, result->buf, linkreadbuf);
  401. return -1;
  402. }
  403. if (result->buf[r] == '/') r++;
  404. result->used= r;
  405. debug(dbg_conffdetail,"conffderef readlink relative to `%.*s'",
  406. result->used, result->buf);
  407. }
  408. varbufaddstr(result,linkreadbuf);
  409. varbufaddc(result,0);
  410. } else {
  411. fprintf(stderr, _("dpkg: %s: warning - conffile `%.250s' is not a plain"
  412. " file or symlink (= `%s')\n"),
  413. pkg->name, in, result->buf);
  414. return -1;
  415. }
  416. }
  417. }
  418. static void md5hash(struct pkginfo *pkg, char hashbuf[33], const char *fn) {
  419. static int fd, p1[2];
  420. FILE *file;
  421. pid_t c1;
  422. int ok;
  423. fd= open(fn,O_RDONLY);
  424. if (fd >= 0) {
  425. push_cleanup(cu_closefd,ehflag_bombout, 0,0, 1,(void*)&fd);
  426. m_pipe(p1);
  427. push_cleanup(cu_closepipe,ehflag_bombout, 0,0, 1,(void*)&p1[0]);
  428. if (!(c1= m_fork())) {
  429. m_dup2(fd,0); m_dup2(p1[1],1); close(p1[0]);
  430. execlp(MD5SUM,MD5SUM,(char*)0);
  431. ohshite(_("failed to exec md5sum"));
  432. }
  433. close(p1[1]); close(fd);
  434. file= fdopen(p1[0],"r");
  435. push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)file);
  436. if (!file) ohshite(_("unable to fdopen for md5sum of `%.250s'"),fn);
  437. ok= 1;
  438. memset(hashbuf,0,33);
  439. if (fread(hashbuf,1,32,file) != 32) ok=0;
  440. if (getc(file) != '\n') ok=0;
  441. if (getc(file) != EOF) ok=0;
  442. waitsubproc(c1,"md5sum",0);
  443. if (strspn(hashbuf,"0123456789abcdef") != 32) ok=0;
  444. if (ferror(file)) ohshite(_("error reading pipe from md5sum"));
  445. if (fclose(file)) ohshite(_("error closing pipe from md5sum"));
  446. pop_cleanup(ehflag_normaltidy); /* file= fdopen(p1[0]) */
  447. pop_cleanup(ehflag_normaltidy); /* m_pipe() */
  448. pop_cleanup(ehflag_normaltidy); /* fd= open(cdr.buf) */
  449. if (!ok) ohshit(_("md5sum gave malformatted output `%.250s'"),hashbuf);
  450. } else if (errno == ENOENT) {
  451. strcpy(hashbuf,NONEXISTENTFLAG);
  452. } else {
  453. fprintf(stderr, _("dpkg: %s: warning - unable to open conffile %s for hash: %s\n"),
  454. pkg->name, fn, strerror(errno));
  455. strcpy(hashbuf,"-");
  456. }
  457. }