help.c 16 KB

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