help.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * dpkg - main program for package management
  3. * help.c - various helper routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.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 <config.h>
  22. #include <compat.h>
  23. #include <dpkg-i18n.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <dirent.h>
  28. #include <assert.h>
  29. #include <string.h>
  30. #include <sys/stat.h>
  31. #include <sys/types.h>
  32. #include <sys/wait.h>
  33. #include <time.h>
  34. #include <dpkg.h>
  35. #include <dpkg-db.h>
  36. #include <dpkg-priv.h>
  37. #include "filesdb.h"
  38. #include "main.h"
  39. const char *const statusstrings[]= {
  40. N_("not installed"),
  41. N_("not installed but configs remain"),
  42. N_("broken due to failed removal or installation"),
  43. N_("unpacked but not configured"),
  44. N_("broken due to postinst failure"),
  45. N_("awaiting trigger processing by another package"),
  46. N_("triggered"),
  47. N_("installed")
  48. };
  49. struct filenamenode *namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg) {
  50. struct filenamenode *r;
  51. if (!namenode->divert) {
  52. r = namenode;
  53. return r;
  54. }
  55. debug(dbg_eachfile,"namenodetouse namenode=`%s' pkg=%s",
  56. namenode->name,pkg->name);
  57. r=
  58. (namenode->divert->useinstead && namenode->divert->pkg != pkg)
  59. ? namenode->divert->useinstead : namenode;
  60. debug(dbg_eachfile,
  61. "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
  62. namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
  63. namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
  64. namenode->divert->pkg ? namenode->divert->pkg->name : "<none>",
  65. r->name);
  66. return r;
  67. }
  68. void checkpath(void) {
  69. /* Verify that some programs can be found in the PATH. */
  70. static const char *const checklist[]= { "ldconfig",
  71. #if WITH_START_STOP_DAEMON
  72. "start-stop-daemon",
  73. #endif
  74. "install-info",
  75. "update-rc.d",
  76. NULL
  77. };
  78. struct stat stab;
  79. const char *const *clp;
  80. const char *path, *s, *p;
  81. char* buf;
  82. int warned= 0;
  83. long l;
  84. path= getenv("PATH");
  85. if (!path) ohshit(_("dpkg - error: PATH is not set.\n"));
  86. buf=(char*)m_malloc(strlen(path)+2+strlen("start-stop-daemon"));
  87. for (clp=checklist; *clp; clp++) {
  88. s= path;
  89. while (s) {
  90. p= strchr(s,':');
  91. l= p ? p-s : (long)strlen(s);
  92. memcpy(buf,s,l);
  93. if (l) buf[l++]= '/';
  94. strcpy(buf+l,*clp);
  95. if (stat(buf,&stab) == 0 && (stab.st_mode & 0111)) break;
  96. s= p; if (s) s++;
  97. }
  98. if (!s) {
  99. fprintf(stderr,_("dpkg: `%s' not found on PATH.\n"),*clp);
  100. warned++;
  101. }
  102. }
  103. free(buf);
  104. if (warned)
  105. forcibleerr(fc_badpath,_("%d expected program(s) not found on PATH.\nNB: root's "
  106. "PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin."),
  107. warned);
  108. }
  109. int ignore_depends(struct pkginfo *pkg) {
  110. struct pkginqueue *id;
  111. for (id= ignoredependss; id; id= id->next)
  112. if (id->pkg == pkg) return 1;
  113. return 0;
  114. }
  115. int force_depends(struct deppossi *possi) {
  116. return fc_depends ||
  117. ignore_depends(possi->ed) ||
  118. ignore_depends(possi->up->up);
  119. }
  120. int force_breaks(struct deppossi *possi) {
  121. return fc_breaks ||
  122. ignore_depends(possi->ed) ||
  123. ignore_depends(possi->up->up);
  124. }
  125. int force_conflicts(struct deppossi *possi) {
  126. return fc_conflicts;
  127. }
  128. static const char* preexecscript(const char *path, char *const *argv) {
  129. /* returns the path to the script inside the chroot
  130. * FIXME: none of the stuff here will work if admindir isn't inside
  131. * instdir as expected.
  132. */
  133. size_t instdirl;
  134. if (*instdir) {
  135. if (chroot(instdir)) ohshite(_("failed to chroot to `%.250s'"),instdir);
  136. if (chdir("/"))
  137. ohshite(_("failed to chdir to `%.255s'"), "/");
  138. }
  139. if (f_debug & dbg_scripts) {
  140. struct varbuf args = VARBUF_INIT;
  141. while (*++argv) {
  142. varbufaddc(&args, ' ');
  143. varbufaddstr(&args, *argv);
  144. }
  145. varbufaddc(&args, '\0');
  146. debug(dbg_scripts, "fork/exec %s (%s )", path, args.buf);
  147. varbuffree(&args);
  148. }
  149. instdirl= strlen(instdir);
  150. if (!instdirl) return path;
  151. assert (strlen(path)>=instdirl);
  152. return path+instdirl;
  153. }
  154. static char *const *vbuildarglist(const char *scriptname, va_list ap) {
  155. static char *bufs[PKGSCRIPTMAXARGS+1];
  156. char *nextarg;
  157. int i;
  158. i=0;
  159. if(bufs[0]) free(bufs[0]);
  160. /* Yes, cast away const because exec wants it that way */
  161. bufs[i++] = m_strdup(scriptname);
  162. for (;;) {
  163. assert(i < PKGSCRIPTMAXARGS);
  164. nextarg= va_arg(ap,char*);
  165. if (!nextarg) break;
  166. bufs[i++]= nextarg;
  167. }
  168. bufs[i] = NULL;
  169. return bufs;
  170. }
  171. static char *const *buildarglist(const char *scriptname, ...) {
  172. char *const *arglist;
  173. va_list ap;
  174. va_start(ap,scriptname);
  175. arglist= vbuildarglist(scriptname,ap);
  176. va_end(ap);
  177. return arglist;
  178. }
  179. void
  180. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  181. {
  182. pkg->trigpend_head = NULL;
  183. pkg->status = pkg->trigaw.head ? stat_triggersawaited : new_status;
  184. post_postinst_tasks_core(pkg);
  185. }
  186. void
  187. post_postinst_tasks_core(struct pkginfo *pkg)
  188. {
  189. modstatdb_note(pkg);
  190. if (!f_noact) {
  191. debug(dbg_triggersdetail, "post_postinst_tasks_core - trig_incorporate");
  192. trig_incorporate(msdbrw_write, admindir);
  193. }
  194. }
  195. static void
  196. post_script_tasks(void)
  197. {
  198. ensure_diversions();
  199. debug(dbg_triggersdetail,
  200. "post_script_tasks - ensure_diversions; trig_incorporate");
  201. trig_incorporate(msdbrw_write, admindir);
  202. }
  203. static void
  204. cu_post_script_tasks(int argc, void **argv)
  205. {
  206. post_script_tasks();
  207. }
  208. static void setexecute(const char *path, struct stat *stab) {
  209. if ((stab->st_mode & 0555) == 0555) return;
  210. if (!chmod(path,0755)) return;
  211. ohshite(_("unable to set execute permissions on `%.250s'"),path);
  212. }
  213. static int do_script(const char *pkg, const char *scriptname, const char *scriptpath, struct stat *stab, char *const arglist[], const char *desc, const char *name, int warn) {
  214. const char *scriptexec;
  215. int c1, r;
  216. setexecute(scriptpath,stab);
  217. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  218. c1= m_fork();
  219. if (!c1) {
  220. const char **narglist;
  221. for (r=0; arglist[r]; r++) ;
  222. narglist=nfmalloc((r+1)*sizeof(char*));
  223. for (r=1; arglist[r-1]; r++)
  224. narglist[r]= arglist[r];
  225. scriptexec= preexecscript(scriptpath,(char * const *)narglist);
  226. narglist[0]= scriptexec;
  227. if (setenv(MAINTSCRIPTPKGENVVAR, pkg, 1) ||
  228. setenv(MAINTSCRIPTDPKGENVVAR, PACKAGE_VERSION, 1))
  229. ohshite(_("unable to setenv for maintainer script"));
  230. execv(scriptexec,(char * const *)narglist);
  231. ohshite(desc,name);
  232. }
  233. setup_subproc_signals(name); /* This does a push_cleanup() */
  234. r= waitsubproc(c1,name,warn);
  235. pop_cleanup(ehflag_normaltidy);
  236. pop_cleanup(ehflag_normaltidy);
  237. return r;
  238. }
  239. static int
  240. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  241. const char *description, va_list ap)
  242. {
  243. const char *scriptpath;
  244. char *const *arglist;
  245. struct stat stab;
  246. char buf[100];
  247. scriptpath= pkgadminfile(pkg,scriptname);
  248. arglist= vbuildarglist(scriptname,ap);
  249. sprintf(buf, _("installed %s script"), description);
  250. if (stat(scriptpath,&stab)) {
  251. if (errno == ENOENT) {
  252. debug(dbg_scripts, "vmaintainer_script_installed nonexistent %s",
  253. scriptname);
  254. return 0;
  255. }
  256. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  257. }
  258. do_script(pkg->name, scriptname, scriptpath, &stab, arglist, _("unable to execute %s"), buf, 0);
  259. return 1;
  260. }
  261. /* All ...'s are const char*'s. */
  262. int
  263. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  264. const char *description, ...)
  265. {
  266. int r;
  267. va_list ap;
  268. va_start(ap, description);
  269. r = vmaintainer_script_installed(pkg, scriptname, description, ap);
  270. va_end(ap);
  271. if (r)
  272. post_script_tasks();
  273. return r;
  274. }
  275. int
  276. maintainer_script_postinst(struct pkginfo *pkg, ...)
  277. {
  278. int r;
  279. va_list ap;
  280. va_start(ap, pkg);
  281. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation", ap);
  282. va_end(ap);
  283. if (r)
  284. ensure_diversions();
  285. return r;
  286. }
  287. int maintainer_script_new(const char *pkgname,
  288. const char *scriptname, const char *description,
  289. const char *cidir, char *cidirrest, ...) {
  290. char *const *arglist;
  291. struct stat stab;
  292. va_list ap;
  293. char buf[100];
  294. va_start(ap,cidirrest);
  295. arglist= vbuildarglist(scriptname,ap);
  296. va_end(ap);
  297. sprintf(buf, _("new %s script"), description);
  298. strcpy(cidirrest,scriptname);
  299. if (stat(cidir,&stab)) {
  300. if (errno == ENOENT) {
  301. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  302. return 0;
  303. }
  304. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  305. }
  306. do_script(pkgname, scriptname, cidir, &stab, arglist,
  307. _("unable to execute %s"), buf, 0);
  308. post_script_tasks();
  309. return 1;
  310. }
  311. int maintainer_script_alternative(struct pkginfo *pkg,
  312. const char *scriptname, const char *description,
  313. const char *cidir, char *cidirrest,
  314. const char *ifok, const char *iffallback) {
  315. const char *oldscriptpath;
  316. char *const *arglist;
  317. struct stat stab;
  318. char buf[100];
  319. oldscriptpath= pkgadminfile(pkg,scriptname);
  320. arglist= buildarglist(scriptname,
  321. ifok,versiondescribe(&pkg->available.version,
  322. vdew_nonambig),
  323. NULL);
  324. sprintf(buf,_("old %s script"),description);
  325. if (stat(oldscriptpath,&stab)) {
  326. if (errno == ENOENT) {
  327. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  328. scriptname,oldscriptpath);
  329. return 0;
  330. }
  331. warning(_("unable to stat %s '%.250s': %s"),
  332. buf,oldscriptpath,strerror(errno));
  333. } else {
  334. if (!do_script(pkg->name, scriptname, oldscriptpath, &stab, arglist,
  335. _("unable to execute %s"), buf, PROCWARN)) {
  336. post_script_tasks();
  337. return 1;
  338. }
  339. }
  340. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  341. arglist= buildarglist(scriptname,
  342. iffallback,versiondescribe(&pkg->installed.version,
  343. vdew_nonambig),
  344. NULL);
  345. strcpy(cidirrest,scriptname);
  346. sprintf(buf,_("new %s script"),description);
  347. if (stat(cidir,&stab)) {
  348. if (errno == ENOENT)
  349. ohshit(_("there is no script in the new version of the package - giving up"));
  350. else
  351. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  352. }
  353. do_script(pkg->name, scriptname, cidir, &stab, arglist, _("unable to execute %s"), buf, 0);
  354. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  355. post_script_tasks();
  356. return 1;
  357. }
  358. void clear_istobes(void) {
  359. struct pkgiterator *it;
  360. struct pkginfo *pkg;
  361. it= iterpkgstart();
  362. while ((pkg = iterpkgnext(it)) != NULL) {
  363. ensure_package_clientdata(pkg);
  364. pkg->clientdata->istobe= itb_normal;
  365. pkg->clientdata->replacingfilesandsaid= 0;
  366. }
  367. iterpkgend(it);
  368. }
  369. void debug(int which, const char *fmt, ...) {
  370. va_list ap;
  371. if (!(f_debug & which)) return;
  372. fprintf(stderr,"D0%05o: ",which);
  373. va_start(ap,fmt);
  374. vfprintf(stderr,fmt,ap);
  375. va_end(ap);
  376. putc('\n',stderr);
  377. }
  378. int hasdirectoryconffiles(struct filenamenode *file, struct pkginfo *pkg) {
  379. /* Returns 1 if the directory contains conffiles belonging to pkg, 0 otherwise. */
  380. struct conffile *conff;
  381. size_t namelen;
  382. debug(dbg_veryverbose, "hasdirectoryconffiles `%s' (from %s)", file->name,
  383. pkg->name);
  384. namelen = strlen(file->name);
  385. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  386. if (!strncmp(file->name,conff->name,namelen)) {
  387. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  388. file->name, conff->name, pkg->name);
  389. return 1;
  390. }
  391. }
  392. debug(dbg_veryverbose, "hasdirectoryconffiles no");
  393. return 0;
  394. }
  395. int isdirectoryinuse(struct filenamenode *file, struct pkginfo *pkg) {
  396. /* Returns 1 if the file is used by packages other than pkg, 0 otherwise. */
  397. struct filepackages *packageslump;
  398. int i;
  399. debug(dbg_veryverbose, "isdirectoryinuse `%s' (except %s)", file->name,
  400. pkg ? pkg->name : "<none>");
  401. for (packageslump= file->packages; packageslump; packageslump= packageslump->more) {
  402. debug(dbg_veryverbose, "isdirectoryinuse packageslump %s ...",
  403. packageslump->pkgs[0] ? packageslump->pkgs[0]->name : "<none>");
  404. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  405. debug(dbg_veryverbose, "isdirectoryinuse considering [%d] %s ...", i,
  406. packageslump->pkgs[i]->name);
  407. if (packageslump->pkgs[i] == pkg) continue;
  408. return 1;
  409. }
  410. }
  411. debug(dbg_veryverbose, "isdirectoryinuse no");
  412. return 0;
  413. }
  414. void oldconffsetflags(const struct conffile *searchconff) {
  415. struct filenamenode *namenode;
  416. while (searchconff) {
  417. namenode= findnamenode(searchconff->name, 0); /* XXX */
  418. namenode->flags |= fnnf_old_conff;
  419. if (!namenode->oldhash)
  420. namenode->oldhash= searchconff->hash;
  421. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  422. searchconff->name, namenode, namenode->flags);
  423. searchconff= searchconff->next;
  424. }
  425. }
  426. int chmodsafe_unlink(const char *pathname, const char **failed) {
  427. /* Sets *failed to `chmod' or `unlink' if those calls fail (which is
  428. * always unexpected). If stat fails it leaves *failed alone. */
  429. struct stat stab;
  430. if (lstat(pathname,&stab)) return -1;
  431. *failed= N_("unlink");
  432. return chmodsafe_unlink_statted(pathname, &stab, failed);
  433. }
  434. int chmodsafe_unlink_statted(const char *pathname, const struct stat *stab,
  435. const char **failed) {
  436. /* Sets *failed to `chmod'' if that call fails (which is always
  437. * unexpected). If unlink fails it leaves *failed alone. */
  438. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  439. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  440. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  441. /* We chmod it if it is 1. a sticky or set-id file, or 2. an unrecognised
  442. * object (ie, not a file, link, directory, fifo or socket)
  443. */
  444. if (chmod(pathname,0600)) { *failed= N_("chmod"); return -1; }
  445. }
  446. if (unlink(pathname)) return -1;
  447. return 0;
  448. }
  449. void ensure_pathname_nonexisting(const char *pathname) {
  450. int c1;
  451. const char *u, *failed;
  452. u= skip_slash_dotslash(pathname);
  453. assert(*u);
  454. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  455. if (!rmdir(pathname)) return; /* Deleted it OK, it was a directory. */
  456. if (errno == ENOENT || errno == ELOOP) return;
  457. failed= N_("delete");
  458. if (errno == ENOTDIR) {
  459. /* Either it's a file, or one of the path components is. If one
  460. * of the path components is this will fail again ...
  461. */
  462. if (!chmodsafe_unlink(pathname, &failed)) return; /* OK, it was */
  463. if (errno == ENOTDIR) return;
  464. }
  465. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh ? */
  466. const char *failed_local = gettext(failed);
  467. ohshite(_("failed to %s '%.255s'"), failed_local, pathname);
  468. }
  469. c1= m_fork();
  470. if (!c1) {
  471. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  472. ohshite(_("failed to exec rm for cleanup"));
  473. }
  474. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  475. waitsubproc(c1,"rm cleanup",0);
  476. }
  477. void log_action(const char *action, struct pkginfo *pkg) {
  478. log_message("%s %s %s %s", action, pkg->name,
  479. versiondescribe(&pkg->installed.version, vdew_nonambig),
  480. versiondescribe(&pkg->available.version, vdew_nonambig));
  481. statusfd_send("processing: %s: %s", action, pkg->name);
  482. }