help.c 16 KB

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