help.c 14 KB

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