help.c 16 KB

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