help.c 15 KB

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