help.c 17 KB

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