help.c 16 KB

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