help.c 17 KB

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