help.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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[]= { "ldconfig",
  63. #ifdef USE_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) fputs(_("dpkg - warning: PATH is not set.\n"), stderr);
  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 : 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 pkginfoperfile));
  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_conflicts(struct deppossi *possi) {
  134. return fc_conflicts;
  135. }
  136. const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile) {
  137. static struct varbuf vb;
  138. varbufreset(&vb);
  139. varbufaddstr(&vb,admindir);
  140. varbufaddstr(&vb,"/" INFODIR);
  141. varbufaddstr(&vb,pkg->name);
  142. varbufaddc(&vb,'.');
  143. varbufaddstr(&vb,whichfile);
  144. varbufaddc(&vb,0);
  145. return vb.buf;
  146. }
  147. static const char* preexecscript(const char *path, char *const *argv) {
  148. /* returns the path to the script inside the chroot
  149. * none of the stuff here will work if admindir isn't inside instdir
  150. * as expected. - fixme
  151. */
  152. int instdirl;
  153. if (*instdir) {
  154. if (chroot(instdir)) ohshite(_("failed to chroot to `%.250s'"),instdir);
  155. }
  156. if (f_debug & dbg_scripts) {
  157. fprintf(stderr,"D0%05o: fork/exec %s (",dbg_scripts,path);
  158. while (*argv) fprintf(stderr," %s",*argv++);
  159. fputs(" )\n",stderr);
  160. }
  161. instdirl= strlen(instdir);
  162. if (!instdirl) return path;
  163. assert (strlen(path)>=instdirl);
  164. return path+instdirl;
  165. }
  166. static char *const *vbuildarglist(const char *scriptname, va_list ap) {
  167. static char *bufs[PKGSCRIPTMAXARGS+1];
  168. char *nextarg;
  169. int i;
  170. i=0;
  171. bufs[i++]= (char*)scriptname; /* yes, cast away const because exec wants it that way */
  172. for (;;) {
  173. assert(i < PKGSCRIPTMAXARGS);
  174. nextarg= va_arg(ap,char*);
  175. if (!nextarg) break;
  176. bufs[i++]= nextarg;
  177. }
  178. bufs[i]= 0;
  179. return bufs;
  180. }
  181. static char *const *buildarglist(const char *scriptname, ...) {
  182. char *const *arglist;
  183. va_list ap;
  184. va_start(ap,scriptname);
  185. arglist= vbuildarglist(scriptname,ap);
  186. va_end(ap);
  187. return arglist;
  188. }
  189. #define NSCRIPTCATCHSIGNALS sizeof(script_catchsignallist)/sizeof(int)-1
  190. static int script_catchsignallist[]= { SIGQUIT, SIGINT, 0 };
  191. static struct sigaction script_uncatchsignal[NSCRIPTCATCHSIGNALS];
  192. static void cu_restorescriptsignals(int argc, void **argv) {
  193. int i;
  194. for (i=0; i<NSCRIPTCATCHSIGNALS; i++) {
  195. if (sigaction(script_catchsignallist[i],&script_uncatchsignal[i],0)) {
  196. fprintf(stderr,_("error un-catching signal %s: %s\n"),
  197. strsignal(script_catchsignallist[i]),strerror(errno));
  198. onerr_abort++;
  199. }
  200. }
  201. }
  202. static void script_catchsignals(void) {
  203. int i;
  204. struct sigaction catchsig;
  205. onerr_abort++;
  206. memset(&catchsig,0,sizeof(catchsig));
  207. catchsig.sa_handler= SIG_IGN;
  208. sigemptyset(&catchsig.sa_mask);
  209. catchsig.sa_flags= 0;
  210. for (i=0; i<NSCRIPTCATCHSIGNALS; i++)
  211. if (sigaction(script_catchsignallist[i],&catchsig,&script_uncatchsignal[i]))
  212. ohshite(_("unable to ignore signal %s before running script"),
  213. strsignal(script_catchsignallist[i]));
  214. push_cleanup(cu_restorescriptsignals,~0, 0,0, 0);
  215. onerr_abort--;
  216. }
  217. static void setexecute(const char *path, struct stat *stab) {
  218. if ((stab->st_mode & 0555) == 0555) return;
  219. if (!chmod(path,0755)) return;
  220. ohshite(_("unable to set execute permissions on `%.250s'"),path);
  221. }
  222. int maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  223. const char *description, ...) {
  224. /* all ...'s are const char*'s */
  225. const char *scriptpath, *scriptexec;
  226. char *const *arglist;
  227. struct stat stab;
  228. va_list ap;
  229. int c1;
  230. char buf[100];
  231. scriptpath= pkgadminfile(pkg,scriptname);
  232. va_start(ap,description);
  233. arglist= vbuildarglist(scriptname,ap);
  234. va_end(ap);
  235. sprintf(buf,"%s script",description);
  236. if (stat(scriptpath,&stab)) {
  237. if (errno == ENOENT) {
  238. debug(dbg_scripts,"maintainer_script_installed nonexistent %s",scriptname);
  239. return 0;
  240. }
  241. ohshite(_("unable to stat installed %s script `%.250s'"),description,scriptpath);
  242. }
  243. setexecute(scriptpath,&stab);
  244. c1= m_fork();
  245. if (!c1) {
  246. scriptexec= preexecscript(scriptpath,arglist);
  247. execv(scriptexec,arglist);
  248. ohshite(_("unable to execute %s"),buf);
  249. }
  250. script_catchsignals(); /* This does a push_cleanup() */
  251. waitsubproc(c1,buf,0);
  252. pop_cleanup(ehflag_normaltidy);
  253. ensure_diversions();
  254. return 1;
  255. }
  256. int maintainer_script_new(const char *scriptname, const char *description,
  257. const char *cidir, char *cidirrest, ...) {
  258. char *const *arglist;
  259. const char *scriptexec;
  260. struct stat stab;
  261. va_list ap;
  262. char buf[100];
  263. int c1;
  264. va_start(ap,cidirrest);
  265. arglist= vbuildarglist(scriptname,ap);
  266. va_end(ap);
  267. sprintf(buf,"%s script",description);
  268. strcpy(cidirrest,scriptname);
  269. if (stat(cidir,&stab)) {
  270. if (errno == ENOENT) {
  271. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  272. return 0;
  273. }
  274. ohshite(_("unable to stat new %s script `%.250s'"),description,cidir);
  275. }
  276. setexecute(cidir,&stab);
  277. c1= m_fork();
  278. if (!c1) {
  279. scriptexec= preexecscript(cidir,arglist);
  280. execv(scriptexec,arglist);
  281. ohshite(_("unable to execute new %s"),buf);
  282. }
  283. script_catchsignals(); /* This does a push_cleanup() */
  284. waitsubproc(c1,buf,0);
  285. pop_cleanup(ehflag_normaltidy);
  286. ensure_diversions();
  287. return 1;
  288. }
  289. int maintainer_script_alternative(struct pkginfo *pkg,
  290. const char *scriptname, const char *description,
  291. const char *cidir, char *cidirrest,
  292. const char *ifok, const char *iffallback) {
  293. const char *oldscriptpath, *scriptexec;
  294. char *const *arglist;
  295. struct stat stab;
  296. int c1, n, status;
  297. char buf[100];
  298. pid_t r;
  299. oldscriptpath= pkgadminfile(pkg,scriptname);
  300. arglist= buildarglist(scriptname,
  301. ifok,versiondescribe(&pkg->available.version,
  302. vdew_nonambig),
  303. (char*)0);
  304. sprintf(buf,_("old %s script"),description);
  305. if (stat(oldscriptpath,&stab)) {
  306. if (errno == ENOENT) {
  307. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  308. scriptname,oldscriptpath);
  309. return 0;
  310. }
  311. fprintf(stderr,
  312. _("dpkg: warning - unable to stat %s `%.250s': %s\n"),
  313. buf,oldscriptpath,strerror(errno));
  314. } else {
  315. setexecute(oldscriptpath,&stab);
  316. c1= m_fork();
  317. if (!c1) {
  318. scriptexec= preexecscript(oldscriptpath,arglist);
  319. execv(scriptexec, arglist);
  320. ohshite(_("unable to execute %s"),buf);
  321. }
  322. script_catchsignals(); /* This does a push_cleanup() */
  323. while ((r= waitpid(c1,&status,0)) == -1 && errno == EINTR);
  324. if (r != c1) ohshite(_("wait for %s failed"),buf);
  325. pop_cleanup(ehflag_normaltidy);
  326. if (WIFEXITED(status)) {
  327. n= WEXITSTATUS(status); if (!n) return 1;
  328. fprintf(stderr, _("dpkg: warning - %s returned error exit status %d\n"),buf,n);
  329. } else if (WIFSIGNALED(status)) {
  330. n= WTERMSIG(status);
  331. fprintf(stderr, _("dpkg: warning - %s killed by signal (%s)%s\n"),
  332. buf, strsignal(n), WCOREDUMP(status) ? ", core dumped" : "");
  333. } else {
  334. ohshit(_("%s failed with unknown wait status code %d"),buf,status);
  335. }
  336. ensure_diversions();
  337. }
  338. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  339. arglist= buildarglist(scriptname,
  340. iffallback,versiondescribe(&pkg->installed.version,
  341. vdew_nonambig),
  342. (char*)0);
  343. strcpy(cidirrest,scriptname);
  344. sprintf(buf,_("new %s script"),description);
  345. if (stat(cidir,&stab)) {
  346. if (errno == ENOENT)
  347. ohshit(_("there is no script in the new version of the package - giving up"));
  348. else
  349. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  350. }
  351. setexecute(cidir,&stab);
  352. c1= m_fork();
  353. if (!c1) {
  354. scriptexec= preexecscript(cidir,arglist);
  355. execv(scriptexec, arglist);
  356. ohshite(_("unable to execute %s"),buf);
  357. }
  358. script_catchsignals(); /* This does a push_cleanup() */
  359. waitsubproc(c1,buf,0);
  360. pop_cleanup(ehflag_normaltidy);
  361. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  362. ensure_diversions();
  363. return 1;
  364. }
  365. void clear_istobes(void) {
  366. struct pkgiterator *it;
  367. struct pkginfo *pkg;
  368. it= iterpkgstart();
  369. while ((pkg= iterpkgnext(it)) != 0) {
  370. ensure_package_clientdata(pkg);
  371. pkg->clientdata->istobe= itb_normal;
  372. pkg->clientdata->replacingfilesandsaid= 0;
  373. }
  374. }
  375. void debug(int which, const char *fmt, ...) {
  376. va_list ap;
  377. if (!(f_debug & which)) return;
  378. fprintf(stderr,"D0%05o: ",which);
  379. va_start(ap,fmt);
  380. vfprintf(stderr,fmt,ap);
  381. va_end(ap);
  382. putc('\n',stderr);
  383. }
  384. int isdirectoryinuse(struct filenamenode *file, struct pkginfo *pkg) {
  385. /* Returns 1 if the file is used by packages other than pkg, 0 otherwise. */
  386. struct filepackages *packageslump;
  387. int i;
  388. debug(dbg_veryverbose, "isdirectoryinuse `%s' (except %s)", file->name,
  389. pkg ? pkg->name : "<none>");
  390. for (packageslump= file->packages; packageslump; packageslump= packageslump->more) {
  391. debug(dbg_veryverbose, "isdirectoryinuse packageslump %s ...",
  392. packageslump->pkgs[0] ? packageslump->pkgs[0]->name : "<none>");
  393. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  394. debug(dbg_veryverbose, "isdirectoryinuse considering [%d] %s ...", i,
  395. packageslump->pkgs[i]->name);
  396. if (packageslump->pkgs[i] == pkg) continue;
  397. return 1;
  398. }
  399. }
  400. debug(dbg_veryverbose, "isdirectoryinuse no");
  401. return 0;
  402. }
  403. void oldconffsetflags(struct conffile *searchconff) {
  404. struct filenamenode *namenode;
  405. while (searchconff) {
  406. namenode= findnamenode(searchconff->name, 0); /* XXX */
  407. namenode->flags |= fnnf_old_conff;
  408. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  409. searchconff->name, namenode, namenode->flags);
  410. searchconff= searchconff->next;
  411. }
  412. }
  413. int chmodsafe_unlink(const char *pathname) {
  414. struct stat stab;
  415. if (lstat(pathname,&stab)) return -1;
  416. if (S_ISREG(stab.st_mode) ? (stab.st_mode & 07000) :
  417. !(S_ISLNK(stab.st_mode) || S_ISDIR(stab.st_mode) ||
  418. S_ISFIFO(stab.st_mode) || S_ISSOCK(stab.st_mode))) {
  419. /* We chmod it if it is 1. a sticky or set-id file, or 2. an unrecognised
  420. * object (ie, not a file, link, directory, fifo or socket
  421. */
  422. if (chmod(pathname,0600)) return -1;
  423. }
  424. if (unlink(pathname)) return -1;
  425. return 0;
  426. }
  427. void ensure_pathname_nonexisting(const char *pathname) {
  428. int c1;
  429. const char *u;
  430. u= skip_slash_dotslash(pathname);
  431. assert(*u);
  432. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  433. if (!rmdir(pathname)) return; /* Deleted it OK, it was a directory. */
  434. if (errno == ENOENT || errno == ELOOP) return;
  435. if (errno == ENOTDIR) {
  436. /* Either it's a file, or one of the path components is. If one
  437. * of the path components is this will fail again ...
  438. */
  439. if (!chmodsafe_unlink(pathname)) return; /* OK, it was */
  440. if (errno == ENOTDIR) return;
  441. }
  442. if (errno != ENOTEMPTY) /* Huh ? */
  443. ohshite(_("failed to rmdir/unlink `%.255s'"),pathname);
  444. c1= m_fork();
  445. if (!c1) {
  446. execlp(RM,"rm","-rf","--",pathname,(char*)0);
  447. ohshite(_("failed to exec rm for cleanup"));
  448. }
  449. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  450. waitsubproc(c1,"rm cleanup",0);
  451. }