dbmodify.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * dpkg - main program for package management
  3. * dbmodify.c - routines for managing dpkg database updates
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 2001 Wichert Akkerman <wichert@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <signal.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <errno.h>
  31. #include <unistd.h>
  32. #include <dirent.h>
  33. #include <limits.h>
  34. #include <ctype.h>
  35. #include <time.h>
  36. #include <assert.h>
  37. #include <dpkg.h>
  38. #include <dpkg-db.h>
  39. char *statusfile=NULL, *availablefile=NULL;
  40. static enum modstatdb_rw cstatus=-1, cflags=0;
  41. static char *importanttmpfile=NULL;
  42. static FILE *importanttmp;
  43. static int nextupdate;
  44. static int updateslength;
  45. static char *updatefnbuf, *updatefnrest;
  46. static const char *admindir;
  47. static struct varbuf uvb;
  48. static int ulist_select(const struct dirent *de) {
  49. const char *p;
  50. int l;
  51. for (p= de->d_name, l=0; *p; p++, l++)
  52. if (!cisdigit(*p)) return 0;
  53. if (l > IMPORTANTMAXLEN)
  54. ohshit(_("updates directory contains file `%.250s' whose name is too long "
  55. "(length=%d, max=%d)"), de->d_name, l, IMPORTANTMAXLEN);
  56. if (updateslength == -1) updateslength= l;
  57. else if (l != updateslength)
  58. ohshit(_("updates directory contains files with different length names "
  59. "(both %d and %d)"), l, updateslength);
  60. return 1;
  61. }
  62. static void cleanupdates(void) {
  63. struct dirent **cdlist;
  64. int cdn, i;
  65. parsedb(statusfile, pdb_weakclassification, NULL,NULL,NULL);
  66. *updatefnrest= 0;
  67. updateslength= -1;
  68. cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort);
  69. if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf);
  70. if (cdn) {
  71. for (i=0; i<cdn; i++) {
  72. strcpy(updatefnrest, cdlist[i]->d_name);
  73. parsedb(updatefnbuf, pdb_weakclassification, NULL,NULL,NULL);
  74. if (cstatus < msdbrw_write) free(cdlist[i]);
  75. }
  76. if (cstatus >= msdbrw_write) {
  77. writedb(statusfile,0,1);
  78. for (i=0; i<cdn; i++) {
  79. strcpy(updatefnrest, cdlist[i]->d_name);
  80. if (unlink(updatefnbuf))
  81. ohshite(_("failed to remove incorporated update file %.255s"),updatefnbuf);
  82. free(cdlist[i]);
  83. }
  84. }
  85. }
  86. free(cdlist);
  87. nextupdate= 0;
  88. }
  89. static void createimptmp(void) {
  90. int i;
  91. onerr_abort++;
  92. importanttmp= fopen(importanttmpfile,"w");
  93. if (!importanttmp)
  94. ohshite(_("unable to create `%.255s'"), importanttmpfile);
  95. setcloexec(fileno(importanttmp),importanttmpfile);
  96. for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
  97. if (ferror(importanttmp))
  98. ohshite(_("unable to fill %.250s with padding"),importanttmpfile);
  99. if (fflush(importanttmp))
  100. ohshite(_("unable to flush %.250s after padding"), importanttmpfile);
  101. if (fseek(importanttmp,0,SEEK_SET))
  102. ohshite(_("unable to seek to start of %.250s after padding"),
  103. importanttmpfile);
  104. onerr_abort--;
  105. }
  106. static const struct fni {
  107. const char *suffix;
  108. char **store;
  109. } fnis[] = {
  110. { STATUSFILE, &statusfile },
  111. { AVAILFILE, &availablefile },
  112. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  113. { NULL, NULL }
  114. };
  115. enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritereq) {
  116. const struct fni *fnip;
  117. admindir= adir;
  118. for (fnip=fnis; fnip->suffix; fnip++) {
  119. free(*fnip->store);
  120. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  121. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  122. }
  123. cflags= readwritereq & msdbrw_flagsmask;
  124. readwritereq &= ~msdbrw_flagsmask;
  125. switch (readwritereq) {
  126. case msdbrw_needsuperuser:
  127. case msdbrw_needsuperuserlockonly:
  128. if (getuid() || geteuid())
  129. ohshit(_("requested operation requires superuser privilege"));
  130. /* fall through */
  131. case msdbrw_write: case msdbrw_writeifposs:
  132. if (access(adir,W_OK)) {
  133. if (errno != EACCES)
  134. ohshite(_("unable to access dpkg status area"));
  135. else if (readwritereq == msdbrw_write)
  136. ohshit(_("operation requires read/write access to dpkg status area"));
  137. cstatus= msdbrw_readonly;
  138. } else {
  139. lockdatabase(adir);
  140. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  141. msdbrw_needsuperuserlockonly :
  142. msdbrw_write);
  143. }
  144. break;
  145. case msdbrw_readonly:
  146. cstatus= msdbrw_readonly; break;
  147. default:
  148. internerr("unknown readwritereq");
  149. }
  150. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  151. strcpy(updatefnbuf,adir);
  152. strcat(updatefnbuf,"/" UPDATESDIR);
  153. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  154. if (cstatus != msdbrw_needsuperuserlockonly) {
  155. cleanupdates();
  156. if(!(cflags & msdbrw_noavail))
  157. parsedb(availablefile,
  158. pdb_recordavailable|pdb_rejectstatus,
  159. NULL,NULL,NULL);
  160. }
  161. if (cstatus >= msdbrw_write) {
  162. createimptmp();
  163. uvb.used= 0;
  164. uvb.size= 10240;
  165. uvb.buf= m_malloc(uvb.size);
  166. }
  167. return cstatus;
  168. }
  169. void modstatdb_checkpoint(void) {
  170. int i;
  171. assert(cstatus >= msdbrw_write);
  172. writedb(statusfile,0,1);
  173. for (i=0; i<nextupdate; i++) {
  174. sprintf(updatefnrest, IMPORTANTFMT, i);
  175. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  176. if (unlink(updatefnbuf))
  177. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  178. }
  179. nextupdate= 0;
  180. }
  181. void modstatdb_shutdown(void) {
  182. const struct fni *fnip;
  183. switch (cstatus) {
  184. case msdbrw_write:
  185. modstatdb_checkpoint();
  186. writedb(availablefile,1,0);
  187. /* tidy up a bit, but don't worry too much about failure */
  188. fclose(importanttmp);
  189. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  190. varbuffree(&uvb);
  191. /* fall through */
  192. case msdbrw_needsuperuserlockonly:
  193. unlockdatabase();
  194. default:
  195. break;
  196. }
  197. for (fnip=fnis; fnip->suffix; fnip++) {
  198. free(*fnip->store);
  199. *fnip->store= NULL;
  200. }
  201. free(updatefnbuf);
  202. }
  203. void modstatdb_note(struct pkginfo *pkg) {
  204. assert(cstatus >= msdbrw_write);
  205. onerr_abort++;
  206. log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->name,
  207. versiondescribe(&pkg->installed.version, vdew_nonambig));
  208. statusfd_send("status: %s: %s", pkg->name, statusinfos[pkg->status].name);
  209. varbufreset(&uvb);
  210. varbufrecord(&uvb, pkg, &pkg->installed);
  211. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  212. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  213. if (fflush(importanttmp))
  214. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  215. if (ftruncate(fileno(importanttmp), uvb.used))
  216. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  217. if (fsync(fileno(importanttmp)))
  218. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  219. if (fclose(importanttmp))
  220. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  221. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  222. if (rename(importanttmpfile, updatefnbuf))
  223. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  224. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  225. nextupdate++;
  226. if (nextupdate > MAXUPDATES) {
  227. modstatdb_checkpoint();
  228. nextupdate= 0;
  229. }
  230. createimptmp();
  231. onerr_abort--;
  232. }
  233. void
  234. modstatdb_note_ifwrite(struct pkginfo *pkg)
  235. {
  236. if (cstatus >= msdbrw_write)
  237. modstatdb_note(pkg);
  238. }
  239. const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile) {
  240. static struct varbuf vb;
  241. varbufreset(&vb);
  242. varbufaddstr(&vb,admindir);
  243. varbufaddstr(&vb,"/" INFODIR);
  244. varbufaddstr(&vb,pkg->name);
  245. varbufaddc(&vb,'.');
  246. varbufaddstr(&vb,whichfile);
  247. varbufaddc(&vb,0);
  248. return vb.buf;
  249. }