Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

rpmdb/legacy.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #if HAVE_GELF_H
00008 
00009 #include <gelf.h>
00010 
00011 #if !defined(DT_GNU_PRELINKED)
00012 #define DT_GNU_PRELINKED        0x6ffffdf5
00013 #endif
00014 #if !defined(DT_GNU_LIBLIST)
00015 #define DT_GNU_LIBLIST          0x6ffffef9
00016 #endif
00017 
00018 #endif
00019 
00020 #include "rpmio_internal.h"
00021 #include <rpmlib.h>
00022 #include <rpmmacro.h>
00023 #include "misc.h"
00024 #include "legacy.h"
00025 #include "debug.h"
00026 
00027 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
00028 
00036 static int open_dso(const char * path, /*@null@*/ pid_t * pidp, /*@null@*/ size_t *fsizep)
00037         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
00038         /*@modifies *pidp, *fsizep, rpmGlobalMacroContext,
00039                 fileSystem, internalState @*/
00040 {
00041 /*@only@*/
00042     static const char * cmd = NULL;
00043     static int initted = 0;
00044     int fdno;
00045 
00046     if (!initted) {
00047         cmd = rpmExpand("%{?__prelink_undo_cmd}", NULL);
00048         initted++;
00049     }
00050 
00051 /*@-boundswrite@*/
00052     if (pidp) *pidp = 0;
00053 
00054     if (fsizep) {
00055         struct stat sb, * st = &sb;
00056         if (stat(path, st) < 0)
00057             return -1;
00058         *fsizep = st->st_size;
00059     }
00060 /*@=boundswrite@*/
00061 
00062     fdno = open(path, O_RDONLY);
00063     if (fdno < 0)
00064         return fdno;
00065 
00066 /*@-boundsread@*/
00067     if (!(cmd && *cmd))
00068         return fdno;
00069 /*@=boundsread@*/
00070 
00071 #if HAVE_GELF_H && HAVE_LIBELF
00072  {  Elf *elf = NULL;
00073     Elf_Scn *scn = NULL;
00074     Elf_Data *data = NULL;
00075     GElf_Ehdr ehdr;
00076     GElf_Shdr shdr;
00077     GElf_Dyn dyn;
00078     int bingo;
00079 
00080     (void) elf_version(EV_CURRENT);
00081 
00082 /*@-evalorder@*/
00083     if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL
00084      || elf_kind(elf) != ELF_K_ELF
00085      || gelf_getehdr(elf, &ehdr) == NULL
00086      || !(ehdr.e_type == ET_DYN || ehdr.e_type == ET_EXEC))
00087         goto exit;
00088 /*@=evalorder@*/
00089 
00090     bingo = 0;
00091     /*@-branchstate -uniondef @*/
00092     while (!bingo && (scn = elf_nextscn(elf, scn)) != NULL) {
00093         (void) gelf_getshdr(scn, &shdr);
00094         if (shdr.sh_type != SHT_DYNAMIC)
00095             continue;
00096         while (!bingo && (data = elf_getdata (scn, data)) != NULL) {
00097             int maxndx = data->d_size / shdr.sh_entsize;
00098             int ndx;
00099 
00100             for (ndx = 0; ndx < maxndx; ++ndx) {
00101                 (void) gelf_getdyn (data, ndx, &dyn);
00102                 if (!(dyn.d_tag == DT_GNU_PRELINKED || dyn.d_tag == DT_GNU_LIBLIST))
00103                     /*@innercontinue@*/ continue;
00104                 bingo = 1;
00105                 /*@innerbreak@*/ break;
00106             }
00107         }
00108     }
00109     /*@=branchstate =uniondef @*/
00110 
00111 /*@-boundswrite@*/
00112     if (pidp != NULL && bingo) {
00113         int pipes[2];
00114         pid_t pid;
00115         int xx;
00116 
00117         xx = close(fdno);
00118         pipes[0] = pipes[1] = -1;
00119         xx = pipe(pipes);
00120         if (!(pid = fork())) {
00121             const char ** av;
00122             int ac;
00123             xx = close(pipes[0]);
00124             xx = dup2(pipes[1], STDOUT_FILENO);
00125             xx = close(pipes[1]);
00126             if (!poptParseArgvString(cmd, &ac, &av)) {
00127                 av[ac-1] = path;
00128                 av[ac] = NULL;
00129                 unsetenv("MALLOC_CHECK_");
00130                 xx = execve(av[0], (char *const *)av+1, environ);
00131             }
00132             _exit(127);
00133         }
00134         *pidp = pid;
00135         fdno = pipes[0];
00136         xx = close(pipes[1]);
00137     }
00138 /*@=boundswrite@*/
00139 
00140 exit:
00141     if (elf) (void) elf_end(elf);
00142  }
00143 #endif
00144 
00145     return fdno;
00146 }
00147 
00148 int domd5(const char * fn, unsigned char * digest, int asAscii, size_t *fsizep)
00149 {
00150     const char * path;
00151     urltype ut = urlPath(fn, &path);
00152     unsigned char * md5sum = NULL;
00153     size_t md5len;
00154     unsigned char buf[32*BUFSIZ];
00155     FD_t fd;
00156     size_t fsize = 0;
00157     pid_t pid = 0;
00158     int rc = 0;
00159     int fdno;
00160     int xx;
00161 
00162 /*@-globs -internalglobs -mods @*/
00163     fdno = open_dso(path, &pid, &fsize);
00164 /*@=globs =internalglobs =mods @*/
00165     if (fdno < 0) {
00166         rc = 1;
00167         goto exit;
00168     }
00169 
00170     switch(ut) {
00171     case URL_IS_PATH:
00172     case URL_IS_UNKNOWN:
00173 #if HAVE_MMAP
00174       if (pid == 0) {
00175         DIGEST_CTX ctx;
00176         void * mapped;
00177 
00178         mapped = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fdno, 0);
00179         if (mapped == (void *)-1) {
00180             xx = close(fdno);
00181             rc = 1;
00182             break;
00183         }
00184 
00185 #ifdef  MADV_SEQUENTIAL
00186         xx = madvise(mapped, fsize, MADV_SEQUENTIAL);
00187 #endif
00188 
00189         ctx = rpmDigestInit(PGPHASHALGO_MD5, RPMDIGEST_NONE);
00190         xx = rpmDigestUpdate(ctx, mapped, fsize);
00191         xx = rpmDigestFinal(ctx, (void **)&md5sum, &md5len, asAscii);
00192         xx = munmap(mapped, fsize);
00193         xx = close(fdno);
00194         break;
00195       } /*@fallthrough@*/
00196 #endif
00197     case URL_IS_FTP:
00198     case URL_IS_HTTP:
00199     case URL_IS_DASH:
00200     default:
00201         /* Either use the pipe to prelink -y or open the URL. */
00202         fd = (pid != 0) ? fdDup(fdno) : Fopen(fn, "r.ufdio");
00203         (void) close(fdno);
00204         if (fd == NULL || Ferror(fd)) {
00205             rc = 1;
00206             if (fd != NULL)
00207                 (void) Fclose(fd);
00208             break;
00209         }
00210         
00211         fdInitDigest(fd, PGPHASHALGO_MD5, 0);
00212         fsize = 0;
00213         while ((rc = Fread(buf, sizeof(buf[0]), sizeof(buf), fd)) > 0)
00214             fsize += rc;
00215         fdFiniDigest(fd, PGPHASHALGO_MD5, (void **)&md5sum, &md5len, asAscii);
00216         if (Ferror(fd))
00217             rc = 1;
00218 
00219         (void) Fclose(fd);
00220         break;
00221     }
00222 
00223     /* Reap the prelink -y helper. */
00224     if (pid) {
00225         int status;
00226         (void) waitpid(pid, &status, 0);
00227         if (!WIFEXITED(status) || WEXITSTATUS(status))
00228             rc = 1;
00229     }
00230 
00231 exit:
00232 /*@-boundswrite@*/
00233     if (fsizep)
00234         *fsizep = fsize;
00235     if (!rc)
00236         memcpy(digest, md5sum, md5len);
00237 /*@=boundswrite@*/
00238     md5sum = _free(md5sum);
00239 
00240     return rc;
00241 }
00242 
00243 /*@-exportheadervar@*/
00244 /*@unchecked@*/
00245 int _noDirTokens = 0;
00246 /*@=exportheadervar@*/
00247 
00248 /*@-boundsread@*/
00249 static int dncmp(const void * a, const void * b)
00250         /*@*/
00251 {
00252     const char *const * first = a;
00253     const char *const * second = b;
00254     return strcmp(*first, *second);
00255 }
00256 /*@=boundsread@*/
00257 
00258 /*@-bounds@*/
00259 void compressFilelist(Header h)
00260 {
00261     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00262     HAE_t hae = (HAE_t)headerAddEntry;
00263     HRE_t hre = (HRE_t)headerRemoveEntry;
00264     HFD_t hfd = headerFreeData;
00265     char ** fileNames;
00266     const char ** dirNames;
00267     const char ** baseNames;
00268     int_32 * dirIndexes;
00269     rpmTagType fnt;
00270     int count;
00271     int i, xx;
00272     int dirIndex = -1;
00273 
00274     /*
00275      * This assumes the file list is already sorted, and begins with a
00276      * single '/'. That assumption isn't critical, but it makes things go
00277      * a bit faster.
00278      */
00279 
00280     if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
00281         xx = hre(h, RPMTAG_OLDFILENAMES);
00282         return;         /* Already converted. */
00283     }
00284 
00285     if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (void **) &fileNames, &count))
00286         return;         /* no file list */
00287     if (fileNames == NULL || count <= 0)
00288         return;
00289 
00290     dirNames = alloca(sizeof(*dirNames) * count);       /* worst case */
00291     baseNames = alloca(sizeof(*dirNames) * count);
00292     dirIndexes = alloca(sizeof(*dirIndexes) * count);
00293 
00294     if (fileNames[0][0] != '/') {
00295         /* HACK. Source RPM, so just do things differently */
00296         dirIndex = 0;
00297         dirNames[dirIndex] = "";
00298         for (i = 0; i < count; i++) {
00299             dirIndexes[i] = dirIndex;
00300             baseNames[i] = fileNames[i];
00301         }
00302         goto exit;
00303     }
00304 
00305     /*@-branchstate@*/
00306     for (i = 0; i < count; i++) {
00307         const char ** needle;
00308         char savechar;
00309         char * baseName;
00310         int len;
00311 
00312         if (fileNames[i] == NULL)       /* XXX can't happen */
00313             continue;
00314         baseName = strrchr(fileNames[i], '/') + 1;
00315         len = baseName - fileNames[i];
00316         needle = dirNames;
00317         savechar = *baseName;
00318         *baseName = '\0';
00319 /*@-compdef@*/
00320         if (dirIndex < 0 ||
00321             (needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
00322             char *s = alloca(len + 1);
00323             memcpy(s, fileNames[i], len + 1);
00324             s[len] = '\0';
00325             dirIndexes[i] = ++dirIndex;
00326             dirNames[dirIndex] = s;
00327         } else
00328             dirIndexes[i] = needle - dirNames;
00329 /*@=compdef@*/
00330 
00331         *baseName = savechar;
00332         baseNames[i] = baseName;
00333     }
00334     /*@=branchstate@*/
00335 
00336 exit:
00337     if (count > 0) {
00338         xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
00339         xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
00340                         baseNames, count);
00341         xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
00342                         dirNames, dirIndex + 1);
00343     }
00344 
00345     fileNames = hfd(fileNames, fnt);
00346 
00347     xx = hre(h, RPMTAG_OLDFILENAMES);
00348 }
00349 /*@=bounds@*/
00350 
00351 void rpmfiBuildFNames(Header h, rpmTag tagN,
00352         /*@out@*/ const char *** fnp, /*@out@*/ int * fcp)
00353 {
00354     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00355     HFD_t hfd = headerFreeData;
00356     const char ** baseNames;
00357     const char ** dirNames;
00358     int * dirIndexes;
00359     int count;
00360     const char ** fileNames;
00361     int size;
00362     rpmTag dirNameTag = 0;
00363     rpmTag dirIndexesTag = 0;
00364     rpmTagType bnt, dnt;
00365     char * t;
00366     int i, xx;
00367 
00368     if (tagN == RPMTAG_BASENAMES) {
00369         dirNameTag = RPMTAG_DIRNAMES;
00370         dirIndexesTag = RPMTAG_DIRINDEXES;
00371     } else if (tagN == RPMTAG_ORIGBASENAMES) {
00372         dirNameTag = RPMTAG_ORIGDIRNAMES;
00373         dirIndexesTag = RPMTAG_ORIGDIRINDEXES;
00374     }
00375 
00376     if (!hge(h, tagN, &bnt, (void **) &baseNames, &count)) {
00377         if (fnp) *fnp = NULL;
00378         if (fcp) *fcp = 0;
00379         return;         /* no file list */
00380     }
00381 
00382     xx = hge(h, dirNameTag, &dnt, (void **) &dirNames, NULL);
00383     xx = hge(h, dirIndexesTag, NULL, (void **) &dirIndexes, &count);
00384 
00385     size = sizeof(*fileNames) * count;
00386     for (i = 0; i < count; i++)
00387         size += strlen(baseNames[i]) + strlen(dirNames[dirIndexes[i]]) + 1;
00388 
00389     fileNames = xmalloc(size);
00390     t = ((char *) fileNames) + (sizeof(*fileNames) * count);
00391     /*@-branchstate@*/
00392     for (i = 0; i < count; i++) {
00393         fileNames[i] = t;
00394         t = stpcpy( stpcpy(t, dirNames[dirIndexes[i]]), baseNames[i]);
00395         *t++ = '\0';
00396     }
00397     /*@=branchstate@*/
00398     baseNames = hfd(baseNames, bnt);
00399     dirNames = hfd(dirNames, dnt);
00400 
00401     /*@-branchstate@*/
00402     if (fnp)
00403         *fnp = fileNames;
00404     else
00405         fileNames = _free(fileNames);
00406     /*@=branchstate@*/
00407     if (fcp) *fcp = count;
00408 }
00409 
00410 void expandFilelist(Header h)
00411 {
00412     HAE_t hae = (HAE_t)headerAddEntry;
00413     HRE_t hre = (HRE_t)headerRemoveEntry;
00414     const char ** fileNames = NULL;
00415     int count = 0;
00416     int xx;
00417 
00418     /*@-branchstate@*/
00419     if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
00420         rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fileNames, &count);
00421         if (fileNames == NULL || count <= 0)
00422             return;
00423         xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
00424                         fileNames, count);
00425         fileNames = _free(fileNames);
00426     }
00427     /*@=branchstate@*/
00428 
00429     xx = hre(h, RPMTAG_DIRNAMES);
00430     xx = hre(h, RPMTAG_BASENAMES);
00431     xx = hre(h, RPMTAG_DIRINDEXES);
00432 }
00433 
00434 /*
00435  * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
00436  * Retrofit an explicit "Provides: name = epoch:version-release.
00437  */
00438 void providePackageNVR(Header h)
00439 {
00440     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00441     HFD_t hfd = headerFreeData;
00442     const char *name, *version, *release;
00443     int_32 * epoch;
00444     const char *pEVR;
00445     char *p;
00446     int_32 pFlags = RPMSENSE_EQUAL;
00447     const char ** provides = NULL;
00448     const char ** providesEVR = NULL;
00449     rpmTagType pnt, pvt;
00450     int_32 * provideFlags = NULL;
00451     int providesCount;
00452     int i, xx;
00453     int bingo = 1;
00454 
00455     /* Generate provides for this package name-version-release. */
00456     xx = headerNVR(h, &name, &version, &release);
00457     if (!(name && version && release))
00458         return;
00459     pEVR = p = alloca(21 + strlen(version) + 1 + strlen(release) + 1);
00460     *p = '\0';
00461     if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
00462         sprintf(p, "%d:", *epoch);
00463         while (*p != '\0')
00464             p++;
00465     }
00466     (void) stpcpy( stpcpy( stpcpy(p, version) , "-") , release);
00467 
00468     /*
00469      * Rpm prior to 3.0.3 does not have versioned provides.
00470      * If no provides at all are available, we can just add.
00471      */
00472     if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides, &providesCount))
00473         goto exit;
00474 
00475     /*
00476      * Otherwise, fill in entries on legacy packages.
00477      */
00478     if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (void **) &providesEVR, NULL)) {
00479         for (i = 0; i < providesCount; i++) {
00480             char * vdummy = "";
00481             int_32 fdummy = RPMSENSE_ANY;
00482             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00483                         &vdummy, 1);
00484             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00485                         &fdummy, 1);
00486         }
00487         goto exit;
00488     }
00489 
00490     xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (void **) &provideFlags, NULL);
00491 
00492     /*@-nullderef@*/    /* LCL: providesEVR is not NULL */
00493     if (provides && providesEVR && provideFlags)
00494     for (i = 0; i < providesCount; i++) {
00495         if (!(provides[i] && providesEVR[i]))
00496             continue;
00497         if (!(provideFlags[i] == RPMSENSE_EQUAL &&
00498             !strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
00499             continue;
00500         bingo = 0;
00501         break;
00502     }
00503     /*@=nullderef@*/
00504 
00505 exit:
00506     provides = hfd(provides, pnt);
00507     providesEVR = hfd(providesEVR, pvt);
00508 
00509     if (bingo) {
00510         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
00511                 &name, 1);
00512         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00513                 &pFlags, 1);
00514         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00515                 &pEVR, 1);
00516     }
00517 }
00518 
00519 void legacyRetrofit(Header h, const struct rpmlead * lead)
00520 {
00521     const char * prefix;
00522 
00523     /*
00524      * We don't use these entries (and rpm >= 2 never has) and they are
00525      * pretty misleading. Let's just get rid of them so they don't confuse
00526      * anyone.
00527      */
00528     if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
00529         (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
00530     if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
00531         (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
00532 
00533     /*
00534      * We switched the way we do relocateable packages. We fix some of
00535      * it up here, though the install code still has to be a bit 
00536      * careful. This fixup makes queries give the new values though,
00537      * which is quite handy.
00538      */
00539     /*@=branchstate@*/
00540     if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &prefix, NULL))
00541     {
00542         const char * nprefix = stripTrailingChar(alloca_strdup(prefix), '/');
00543         (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
00544                 &nprefix, 1); 
00545     }
00546     /*@=branchstate@*/
00547 
00548     /*
00549      * The file list was moved to a more compressed format which not
00550      * only saves memory (nice), but gives fingerprinting a nice, fat
00551      * speed boost (very nice). Go ahead and convert old headers to
00552      * the new style (this is a noop for new headers).
00553      */
00554     if (lead->major < 4)
00555         compressFilelist(h);
00556 
00557     /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
00558     if (lead->type == RPMLEAD_SOURCE) {
00559         int_32 one = 1;
00560         if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
00561             (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
00562                                 &one, 1);
00563     } else if (lead->major < 4) {
00564         /* Retrofit "Provide: name = EVR" for binary packages. */
00565         providePackageNVR(h);
00566     }
00567 }

Generated on Sun Oct 26 13:02:02 2003 for rpm by doxygen1.2.18