help.c 15 KB

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