help.c 15 KB

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