dbmodify.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <assert.h>
  36. #include <dpkg.h>
  37. #include <dpkg-db.h>
  38. char *statusfile=NULL, *availablefile=NULL;
  39. static enum modstatdb_rw cstatus=-1, cflags=0;
  40. static char *importanttmpfile=NULL;
  41. static FILE *importanttmp;
  42. static int nextupdate;
  43. static int updateslength;
  44. static char *updatefnbuf, *updatefnrest;
  45. static const char *admindir;
  46. static struct varbuf uvb;
  47. static int ulist_select(const struct dirent *de) {
  48. const char *p;
  49. int l;
  50. for (p= de->d_name, l=0; *p; p++, l++)
  51. if (!cisdigit(*p)) return 0;
  52. if (l > IMPORTANTMAXLEN)
  53. ohshit(_("updates directory contains file `%.250s' whose name is too long "
  54. "(length=%d, max=%d)"), de->d_name, l, IMPORTANTMAXLEN);
  55. if (updateslength == -1) updateslength= l;
  56. else if (l != updateslength)
  57. ohshit(_("updates directory contains files with different length names "
  58. "(both %d and %d)"), l, updateslength);
  59. return 1;
  60. }
  61. static void cleanupdates(void) {
  62. struct dirent **cdlist;
  63. int cdn, i;
  64. parsedb(statusfile, pdb_weakclassification, NULL,NULL,NULL);
  65. *updatefnrest= 0;
  66. updateslength= -1;
  67. cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort);
  68. if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf);
  69. if (cdn) {
  70. for (i=0; i<cdn; i++) {
  71. strcpy(updatefnrest, cdlist[i]->d_name);
  72. parsedb(updatefnbuf, pdb_weakclassification, NULL,NULL,NULL);
  73. if (cstatus < msdbrw_write) free(cdlist[i]);
  74. }
  75. if (cstatus >= msdbrw_write) {
  76. writedb(statusfile,0,1);
  77. for (i=0; i<cdn; i++) {
  78. strcpy(updatefnrest, cdlist[i]->d_name);
  79. if (unlink(updatefnbuf))
  80. ohshite(_("failed to remove incorporated update file %.255s"),updatefnbuf);
  81. free(cdlist[i]);
  82. }
  83. }
  84. }
  85. free(cdlist);
  86. nextupdate= 0;
  87. }
  88. static void createimptmp(void) {
  89. int i;
  90. onerr_abort++;
  91. importanttmp= fopen(importanttmpfile,"w");
  92. if (!importanttmp) ohshite(_("unable to create %.250s"),importanttmpfile);
  93. setcloexec(fileno(importanttmp),importanttmpfile);
  94. for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
  95. if (ferror(importanttmp))
  96. ohshite(_("unable to fill %.250s with padding"),importanttmpfile);
  97. if (fflush(importanttmp))
  98. ohshite(_("unable flush %.250s after padding"),importanttmpfile);
  99. if (fseek(importanttmp,0,SEEK_SET))
  100. ohshite(_("unable seek to start of %.250s after padding"),importanttmpfile);
  101. onerr_abort--;
  102. }
  103. const struct fni { const char *suffix; char **store; } fnis[]= {
  104. { STATUSFILE, &statusfile },
  105. { AVAILFILE, &availablefile },
  106. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  107. { NULL, NULL }
  108. };
  109. enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritereq) {
  110. const struct fni *fnip;
  111. admindir= adir;
  112. for (fnip=fnis; fnip->suffix; fnip++) {
  113. free(*fnip->store);
  114. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  115. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  116. }
  117. cflags= readwritereq & msdbrw_flagsmask;
  118. readwritereq &= ~msdbrw_flagsmask;
  119. switch (readwritereq) {
  120. case msdbrw_needsuperuser:
  121. case msdbrw_needsuperuserlockonly:
  122. if (getuid() || geteuid())
  123. ohshit(_("requested operation requires superuser privilege"));
  124. /* fall through */
  125. case msdbrw_write: case msdbrw_writeifposs:
  126. if (access(adir,W_OK)) {
  127. if (errno != EACCES)
  128. ohshite(_("unable to access dpkg status area"));
  129. else if (readwritereq == msdbrw_write)
  130. ohshit(_("operation requires read/write access to dpkg status area"));
  131. cstatus= msdbrw_readonly;
  132. } else {
  133. lockdatabase(adir);
  134. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  135. msdbrw_needsuperuserlockonly :
  136. msdbrw_write);
  137. }
  138. break;
  139. case msdbrw_readonly:
  140. cstatus= msdbrw_readonly; break;
  141. default:
  142. internerr("unknown readwritereq");
  143. }
  144. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  145. strcpy(updatefnbuf,adir);
  146. strcat(updatefnbuf,"/" UPDATESDIR);
  147. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  148. if (cstatus != msdbrw_needsuperuserlockonly) {
  149. cleanupdates();
  150. if(!(cflags & msdbrw_noavail))
  151. parsedb(availablefile,
  152. pdb_recordavailable|pdb_rejectstatus,
  153. NULL,NULL,NULL);
  154. }
  155. if (cstatus >= msdbrw_write) {
  156. createimptmp();
  157. uvb.used= 0;
  158. uvb.size= 10240;
  159. uvb.buf= m_malloc(uvb.size);
  160. }
  161. return cstatus;
  162. }
  163. static void checkpoint(void) {
  164. int i;
  165. assert(cstatus >= msdbrw_write);
  166. writedb(statusfile,0,1);
  167. for (i=0; i<nextupdate; i++) {
  168. sprintf(updatefnrest, IMPORTANTFMT, i);
  169. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  170. if (unlink(updatefnbuf))
  171. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  172. }
  173. nextupdate= 0;
  174. }
  175. void modstatdb_shutdown(void) {
  176. const struct fni *fnip;
  177. switch (cstatus) {
  178. case msdbrw_write:
  179. checkpoint();
  180. writedb(availablefile,1,0);
  181. /* tidy up a bit, but don't worry too much about failure */
  182. fclose(importanttmp);
  183. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  184. varbuffree(&uvb);
  185. /* fall through */
  186. case msdbrw_needsuperuserlockonly:
  187. unlockdatabase(admindir);
  188. default:
  189. break;
  190. }
  191. for (fnip=fnis; fnip->suffix; fnip++) {
  192. free(*fnip->store);
  193. *fnip->store= NULL;
  194. }
  195. free(updatefnbuf);
  196. }
  197. struct pipef *status_pipes= NULL;
  198. void modstatdb_note(struct pkginfo *pkg) {
  199. assert(cstatus >= msdbrw_write);
  200. onerr_abort++;
  201. if (status_pipes) {
  202. static struct varbuf *status= NULL;
  203. struct pipef *pipef= status_pipes;
  204. int r;
  205. if (status == NULL) {
  206. status = nfmalloc(sizeof(struct varbuf));
  207. varbufinit(status);
  208. } else
  209. varbufreset(status);
  210. r= varbufprintf(status, "status: %s: %s\n", pkg->name, statusinfos[pkg->status].name);
  211. while (pipef) {
  212. write(pipef->fd, status->buf, r);
  213. pipef= pipef->next;
  214. }
  215. }
  216. varbufreset(&uvb);
  217. varbufrecord(&uvb, pkg, &pkg->installed);
  218. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  219. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  220. if (fflush(importanttmp))
  221. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  222. if (ftruncate(fileno(importanttmp), uvb.used))
  223. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  224. if (fsync(fileno(importanttmp)))
  225. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  226. if (fclose(importanttmp))
  227. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  228. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  229. if (rename(importanttmpfile, updatefnbuf))
  230. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  231. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  232. nextupdate++;
  233. if (nextupdate > MAXUPDATES) {
  234. checkpoint();
  235. nextupdate= 0;
  236. }
  237. createimptmp();
  238. onerr_abort--;
  239. }