dbmodify.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <dirent.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <assert.h>
  35. #include <config.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. enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritereq) {
  104. static const struct fni { const char *suffix; char **store; } fnis[]= {
  105. { STATUSFILE, &statusfile },
  106. { AVAILFILE, &availablefile },
  107. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  108. { NULL, NULL }
  109. }, *fnip;
  110. admindir= adir;
  111. for (fnip=fnis; fnip->suffix; fnip++) {
  112. free(*fnip->store);
  113. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  114. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  115. }
  116. cflags= readwritereq & msdbrw_flagsmask;
  117. readwritereq &= ~msdbrw_flagsmask;
  118. switch (readwritereq) {
  119. case msdbrw_needsuperuser:
  120. case msdbrw_needsuperuserlockonly:
  121. if (getuid() || geteuid())
  122. ohshit(_("requested operation requires superuser privilege"));
  123. /* fall through */
  124. case msdbrw_write: case msdbrw_writeifposs:
  125. if (access(adir,W_OK)) {
  126. if (errno != EACCES)
  127. ohshite(_("unable to access dpkg status area"));
  128. else if (readwritereq == msdbrw_write)
  129. ohshit(_("operation requires read/write access to dpkg status area"));
  130. cstatus= msdbrw_readonly;
  131. } else {
  132. lockdatabase(adir);
  133. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  134. msdbrw_needsuperuserlockonly :
  135. msdbrw_write);
  136. }
  137. break;
  138. case msdbrw_readonly:
  139. cstatus= msdbrw_readonly; break;
  140. default:
  141. internerr("unknown readwritereq");
  142. }
  143. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  144. strcpy(updatefnbuf,adir);
  145. strcat(updatefnbuf,"/" UPDATESDIR);
  146. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  147. if (cstatus != msdbrw_needsuperuserlockonly) {
  148. cleanupdates();
  149. if(!(cflags & msdbrw_noavail))
  150. parsedb(availablefile,
  151. pdb_recordavailable|pdb_rejectstatus,
  152. NULL,NULL,NULL);
  153. }
  154. if (cstatus >= msdbrw_write) {
  155. createimptmp();
  156. uvb.used= 0;
  157. uvb.size= 10240;
  158. uvb.buf= m_malloc(uvb.size);
  159. }
  160. return cstatus;
  161. }
  162. static void checkpoint(void) {
  163. int i;
  164. assert(cstatus >= msdbrw_write);
  165. writedb(statusfile,0,1);
  166. for (i=0; i<nextupdate; i++) {
  167. sprintf(updatefnrest, IMPORTANTFMT, i);
  168. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  169. if (unlink(updatefnbuf))
  170. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  171. }
  172. nextupdate= 0;
  173. }
  174. void modstatdb_shutdown(void) {
  175. switch (cstatus) {
  176. case msdbrw_write:
  177. checkpoint();
  178. writedb(availablefile,1,0);
  179. /* tidy up a bit, but don't worry too much about failure */
  180. fclose(importanttmp);
  181. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  182. varbuffree(&uvb);
  183. /* fall through */
  184. case msdbrw_needsuperuserlockonly:
  185. unlockdatabase(admindir);
  186. default:
  187. break;
  188. }
  189. free(updatefnbuf);
  190. }
  191. struct pipef *status_pipes= NULL;
  192. void modstatdb_note(struct pkginfo *pkg) {
  193. assert(cstatus >= msdbrw_write);
  194. onerr_abort++;
  195. if (status_pipes) {
  196. static struct varbuf *status= NULL;
  197. struct pipef *pipef= status_pipes;
  198. int r;
  199. if (status == NULL) {
  200. status = nfmalloc(sizeof(struct varbuf));
  201. varbufinit(status);
  202. } else
  203. varbufreset(status);
  204. r= varbufprintf(status, "status: %s: %s\n", pkg->name, statusinfos[pkg->status].name);
  205. while (pipef) {
  206. write(pipef->fd, status->buf, r);
  207. pipef= pipef->next;
  208. }
  209. }
  210. varbufreset(&uvb);
  211. varbufrecord(&uvb, pkg, &pkg->installed);
  212. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  213. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  214. if (fflush(importanttmp))
  215. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  216. if (ftruncate(fileno(importanttmp), uvb.used))
  217. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  218. if (fsync(fileno(importanttmp)))
  219. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  220. if (fclose(importanttmp))
  221. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  222. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  223. if (rename(importanttmpfile, updatefnbuf))
  224. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  225. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  226. nextupdate++;
  227. if (nextupdate > MAXUPDATES) {
  228. checkpoint();
  229. nextupdate= 0;
  230. }
  231. createimptmp();
  232. onerr_abort--;
  233. }