help.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. }
  137. if (f_debug & dbg_scripts) {
  138. struct varbuf args = VARBUF_INIT;
  139. while (*++argv) {
  140. varbufaddc(&args, ' ');
  141. varbufaddstr(&args, *argv);
  142. }
  143. varbufaddc(&args, '\0');
  144. debug(dbg_scripts, "fork/exec %s (%s )", path, args.buf);
  145. varbuffree(&args);
  146. }
  147. instdirl= strlen(instdir);
  148. if (!instdirl) return path;
  149. assert (strlen(path)>=instdirl);
  150. return path+instdirl;
  151. }
  152. static char *const *vbuildarglist(const char *scriptname, va_list ap) {
  153. static char *bufs[PKGSCRIPTMAXARGS+1];
  154. char *nextarg;
  155. int i;
  156. i=0;
  157. if(bufs[0]) free(bufs[0]);
  158. /* Yes, cast away const because exec wants it that way */
  159. bufs[i++] = m_strdup(scriptname);
  160. for (;;) {
  161. assert(i < PKGSCRIPTMAXARGS);
  162. nextarg= va_arg(ap,char*);
  163. if (!nextarg) break;
  164. bufs[i++]= nextarg;
  165. }
  166. bufs[i] = NULL;
  167. return bufs;
  168. }
  169. static char *const *buildarglist(const char *scriptname, ...) {
  170. char *const *arglist;
  171. va_list ap;
  172. va_start(ap,scriptname);
  173. arglist= vbuildarglist(scriptname,ap);
  174. va_end(ap);
  175. return arglist;
  176. }
  177. void
  178. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  179. {
  180. pkg->trigpend_head = NULL;
  181. pkg->status = pkg->trigaw.head ? stat_triggersawaited : new_status;
  182. post_postinst_tasks_core(pkg);
  183. }
  184. void
  185. post_postinst_tasks_core(struct pkginfo *pkg)
  186. {
  187. modstatdb_note(pkg);
  188. if (!f_noact) {
  189. debug(dbg_triggersdetail, "post_postinst_tasks_core - trig_incorporate");
  190. trig_incorporate(msdbrw_write, admindir);
  191. }
  192. }
  193. static void
  194. post_script_tasks(void)
  195. {
  196. ensure_diversions();
  197. debug(dbg_triggersdetail,
  198. "post_script_tasks - ensure_diversions; trig_incorporate");
  199. trig_incorporate(msdbrw_write, admindir);
  200. }
  201. static void
  202. cu_post_script_tasks(int argc, void **argv)
  203. {
  204. post_script_tasks();
  205. }
  206. static void setexecute(const char *path, struct stat *stab) {
  207. if ((stab->st_mode & 0555) == 0555) return;
  208. if (!chmod(path,0755)) return;
  209. ohshite(_("unable to set execute permissions on `%.250s'"),path);
  210. }
  211. 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) {
  212. const char *scriptexec;
  213. int c1, r;
  214. setexecute(scriptpath,stab);
  215. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  216. c1= m_fork();
  217. if (!c1) {
  218. const char **narglist;
  219. for (r=0; arglist[r]; r++) ;
  220. narglist=nfmalloc((r+1)*sizeof(char*));
  221. for (r=1; arglist[r-1]; r++)
  222. narglist[r]= arglist[r];
  223. scriptexec= preexecscript(scriptpath,(char * const *)narglist);
  224. narglist[0]= scriptexec;
  225. if (setenv(MAINTSCRIPTPKGENVVAR, pkg, 1) ||
  226. setenv(MAINTSCRIPTDPKGENVVAR, PACKAGE_VERSION, 1))
  227. ohshite(_("unable to setenv for maintainer script"));
  228. execv(scriptexec,(char * const *)narglist);
  229. ohshite(desc,name);
  230. }
  231. setup_subproc_signals(name); /* This does a push_cleanup() */
  232. r= waitsubproc(c1,name,warn);
  233. pop_cleanup(ehflag_normaltidy);
  234. pop_cleanup(ehflag_normaltidy);
  235. return r;
  236. }
  237. static int
  238. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  239. const char *description, va_list ap)
  240. {
  241. const char *scriptpath;
  242. char *const *arglist;
  243. struct stat stab;
  244. char buf[100];
  245. scriptpath= pkgadminfile(pkg,scriptname);
  246. arglist= vbuildarglist(scriptname,ap);
  247. sprintf(buf, _("installed %s script"), description);
  248. if (stat(scriptpath,&stab)) {
  249. if (errno == ENOENT) {
  250. debug(dbg_scripts, "vmaintainer_script_installed nonexistent %s",
  251. scriptname);
  252. return 0;
  253. }
  254. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  255. }
  256. do_script(pkg->name, scriptname, scriptpath, &stab, arglist, _("unable to execute %s"), buf, 0);
  257. return 1;
  258. }
  259. /* All ...'s are const char*'s. */
  260. int
  261. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  262. const char *description, ...)
  263. {
  264. int r;
  265. va_list ap;
  266. va_start(ap, description);
  267. r = vmaintainer_script_installed(pkg, scriptname, description, ap);
  268. va_end(ap);
  269. if (r)
  270. post_script_tasks();
  271. return r;
  272. }
  273. int
  274. maintainer_script_postinst(struct pkginfo *pkg, ...)
  275. {
  276. int r;
  277. va_list ap;
  278. va_start(ap, pkg);
  279. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation", ap);
  280. va_end(ap);
  281. if (r)
  282. ensure_diversions();
  283. return r;
  284. }
  285. int maintainer_script_new(const char *pkgname,
  286. const char *scriptname, const char *description,
  287. const char *cidir, char *cidirrest, ...) {
  288. char *const *arglist;
  289. struct stat stab;
  290. va_list ap;
  291. char buf[100];
  292. va_start(ap,cidirrest);
  293. arglist= vbuildarglist(scriptname,ap);
  294. va_end(ap);
  295. sprintf(buf, _("new %s script"), description);
  296. strcpy(cidirrest,scriptname);
  297. if (stat(cidir,&stab)) {
  298. if (errno == ENOENT) {
  299. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  300. return 0;
  301. }
  302. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  303. }
  304. do_script(pkgname, scriptname, cidir, &stab, arglist,
  305. _("unable to execute %s"), buf, 0);
  306. post_script_tasks();
  307. return 1;
  308. }
  309. int maintainer_script_alternative(struct pkginfo *pkg,
  310. const char *scriptname, const char *description,
  311. const char *cidir, char *cidirrest,
  312. const char *ifok, const char *iffallback) {
  313. const char *oldscriptpath;
  314. char *const *arglist;
  315. struct stat stab;
  316. char buf[100];
  317. oldscriptpath= pkgadminfile(pkg,scriptname);
  318. arglist= buildarglist(scriptname,
  319. ifok,versiondescribe(&pkg->available.version,
  320. vdew_nonambig),
  321. NULL);
  322. sprintf(buf,_("old %s script"),description);
  323. if (stat(oldscriptpath,&stab)) {
  324. if (errno == ENOENT) {
  325. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  326. scriptname,oldscriptpath);
  327. return 0;
  328. }
  329. fprintf(stderr,
  330. _("dpkg: warning - unable to stat %s `%.250s': %s\n"),
  331. buf,oldscriptpath,strerror(errno));
  332. } else {
  333. if (!do_script(pkg->name, scriptname, oldscriptpath, &stab, arglist,
  334. _("unable to execute %s"), buf, PROCWARN)) {
  335. post_script_tasks();
  336. return 1;
  337. }
  338. }
  339. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  340. arglist= buildarglist(scriptname,
  341. iffallback,versiondescribe(&pkg->installed.version,
  342. vdew_nonambig),
  343. NULL);
  344. strcpy(cidirrest,scriptname);
  345. sprintf(buf,_("new %s script"),description);
  346. if (stat(cidir,&stab)) {
  347. if (errno == ENOENT)
  348. ohshit(_("there is no script in the new version of the package - giving up"));
  349. else
  350. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  351. }
  352. do_script(pkg->name, scriptname, cidir, &stab, arglist, _("unable to execute %s"), buf, 0);
  353. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  354. post_script_tasks();
  355. return 1;
  356. }
  357. void clear_istobes(void) {
  358. struct pkgiterator *it;
  359. struct pkginfo *pkg;
  360. it= iterpkgstart();
  361. while ((pkg = iterpkgnext(it)) != NULL) {
  362. ensure_package_clientdata(pkg);
  363. pkg->clientdata->istobe= itb_normal;
  364. pkg->clientdata->replacingfilesandsaid= 0;
  365. }
  366. iterpkgend(it);
  367. }
  368. void debug(int which, const char *fmt, ...) {
  369. va_list ap;
  370. if (!(f_debug & which)) return;
  371. fprintf(stderr,"D0%05o: ",which);
  372. va_start(ap,fmt);
  373. vfprintf(stderr,fmt,ap);
  374. va_end(ap);
  375. putc('\n',stderr);
  376. }
  377. int hasdirectoryconffiles(struct filenamenode *file, struct pkginfo *pkg) {
  378. /* Returns 1 if the directory contains conffiles belonging to pkg, 0 otherwise. */
  379. struct conffile *conff;
  380. size_t namelen;
  381. debug(dbg_veryverbose, "hasdirectoryconffiles `%s' (from %s)", file->name,
  382. pkg->name);
  383. namelen = strlen(file->name);
  384. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  385. if (!strncmp(file->name,conff->name,namelen)) {
  386. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  387. file->name, conff->name, pkg->name);
  388. return 1;
  389. }
  390. }
  391. debug(dbg_veryverbose, "hasdirectoryconffiles no");
  392. return 0;
  393. }
  394. int isdirectoryinuse(struct filenamenode *file, struct pkginfo *pkg) {
  395. /* Returns 1 if the file is used by packages other than pkg, 0 otherwise. */
  396. struct filepackages *packageslump;
  397. int i;
  398. debug(dbg_veryverbose, "isdirectoryinuse `%s' (except %s)", file->name,
  399. pkg ? pkg->name : "<none>");
  400. for (packageslump= file->packages; packageslump; packageslump= packageslump->more) {
  401. debug(dbg_veryverbose, "isdirectoryinuse packageslump %s ...",
  402. packageslump->pkgs[0] ? packageslump->pkgs[0]->name : "<none>");
  403. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  404. debug(dbg_veryverbose, "isdirectoryinuse considering [%d] %s ...", i,
  405. packageslump->pkgs[i]->name);
  406. if (packageslump->pkgs[i] == pkg) continue;
  407. return 1;
  408. }
  409. }
  410. debug(dbg_veryverbose, "isdirectoryinuse no");
  411. return 0;
  412. }
  413. void oldconffsetflags(const struct conffile *searchconff) {
  414. struct filenamenode *namenode;
  415. while (searchconff) {
  416. namenode= findnamenode(searchconff->name, 0); /* XXX */
  417. namenode->flags |= fnnf_old_conff;
  418. if (!namenode->oldhash)
  419. namenode->oldhash= searchconff->hash;
  420. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  421. searchconff->name, namenode, namenode->flags);
  422. searchconff= searchconff->next;
  423. }
  424. }
  425. int chmodsafe_unlink(const char *pathname, const char **failed) {
  426. /* Sets *failed to `chmod' or `unlink' if those calls fail (which is
  427. * always unexpected). If stat fails it leaves *failed alone. */
  428. struct stat stab;
  429. if (lstat(pathname,&stab)) return -1;
  430. *failed= N_("unlink");
  431. return chmodsafe_unlink_statted(pathname, &stab, failed);
  432. }
  433. int chmodsafe_unlink_statted(const char *pathname, const struct stat *stab,
  434. const char **failed) {
  435. /* Sets *failed to `chmod'' if that call fails (which is always
  436. * unexpected). If unlink fails it leaves *failed alone. */
  437. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  438. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  439. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  440. /* We chmod it if it is 1. a sticky or set-id file, or 2. an unrecognised
  441. * object (ie, not a file, link, directory, fifo or socket)
  442. */
  443. if (chmod(pathname,0600)) { *failed= N_("chmod"); return -1; }
  444. }
  445. if (unlink(pathname)) return -1;
  446. return 0;
  447. }
  448. void ensure_pathname_nonexisting(const char *pathname) {
  449. int c1;
  450. const char *u, *failed;
  451. u= skip_slash_dotslash(pathname);
  452. assert(*u);
  453. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  454. if (!rmdir(pathname)) return; /* Deleted it OK, it was a directory. */
  455. if (errno == ENOENT || errno == ELOOP) return;
  456. failed= N_("delete");
  457. if (errno == ENOTDIR) {
  458. /* Either it's a file, or one of the path components is. If one
  459. * of the path components is this will fail again ...
  460. */
  461. if (!chmodsafe_unlink(pathname, &failed)) return; /* OK, it was */
  462. if (errno == ENOTDIR) return;
  463. }
  464. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh ? */
  465. const char *failed_local = gettext(failed);
  466. ohshite(_("failed to %s '%.255s'"), failed_local, pathname);
  467. }
  468. c1= m_fork();
  469. if (!c1) {
  470. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  471. ohshite(_("failed to exec rm for cleanup"));
  472. }
  473. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  474. waitsubproc(c1,"rm cleanup",0);
  475. }
  476. void log_action(const char *action, struct pkginfo *pkg) {
  477. log_message("%s %s %s %s", action, pkg->name,
  478. versiondescribe(&pkg->installed.version, vdew_nonambig),
  479. versiondescribe(&pkg->available.version, vdew_nonambig));
  480. statusfd_send("processing: %s: %s", action, pkg->name);
  481. }