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

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, h_errno, 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 
00131 #if defined(__GLIBC__)
00132 
00136                 {
00137                    char* bypassVar = (char*) malloc(1024*sizeof(char));
00138                    if (bypassVar != NULL)
00139                    {
00140                       snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00141                       bypassVar[1023] = '\0';
00142                       if (getenv(bypassVar) != NULL)
00143                       {
00144                          char* bypassVal = (char*) malloc(1024*sizeof(char));
00145                          if (bypassVal != NULL)
00146                          {
00147                             snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00148                             unsetenv(bypassVar);
00149                             snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00150                             bypassVar[1023] = '\0';
00151                             putenv(bypassVar);
00152                             free(bypassVal);
00153                          }
00154                          else
00155                          {
00156                             free(bypassVar);
00157                          }
00158                       }
00159                    }
00160                 }
00161 #endif
00162                 xx = execve(av[0], (char *const *)av+1, environ);
00163             }
00164             _exit(127);
00165         }
00166         *pidp = pid;
00167         fdno = pipes[0];
00168         xx = close(pipes[1]);
00169     }
00170 /*@=boundswrite@*/
00171 
00172 exit:
00173     if (elf) (void) elf_end(elf);
00174  }
00175 #endif
00176 
00177     return fdno;
00178 }
00179 
00180 int domd5(const char * fn, unsigned char * digest, int asAscii, size_t *fsizep)
00181 {
00182     const char * path;
00183     urltype ut = urlPath(fn, &path);
00184     unsigned char * md5sum = NULL;
00185     size_t md5len;
00186     unsigned char buf[32*BUFSIZ];
00187     FD_t fd;
00188     size_t fsize = 0;
00189     pid_t pid = 0;
00190     int rc = 0;
00191     int fdno;
00192     int xx;
00193 
00194 /*@-globs -internalglobs -mods @*/
00195     fdno = open_dso(path, &pid, &fsize);
00196 /*@=globs =internalglobs =mods @*/
00197     if (fdno < 0) {
00198         rc = 1;
00199         goto exit;
00200     }
00201 
00202     switch(ut) {
00203     case URL_IS_PATH:
00204     case URL_IS_UNKNOWN:
00205 #if HAVE_MMAP
00206       if (pid == 0) {
00207         DIGEST_CTX ctx;
00208         void * mapped;
00209 
00210         mapped = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fdno, 0);
00211         if (mapped == (void *)-1) {
00212             xx = close(fdno);
00213             rc = 1;
00214             break;
00215         }
00216 
00217 #ifdef  MADV_SEQUENTIAL
00218         xx = madvise(mapped, fsize, MADV_SEQUENTIAL);
00219 #endif
00220 
00221         ctx = rpmDigestInit(PGPHASHALGO_MD5, RPMDIGEST_NONE);
00222         xx = rpmDigestUpdate(ctx, mapped, fsize);
00223         xx = rpmDigestFinal(ctx, (void **)&md5sum, &md5len, asAscii);
00224         xx = munmap(mapped, fsize);
00225         xx = close(fdno);
00226         break;
00227       } /*@fallthrough@*/
00228 #endif
00229     case URL_IS_FTP:
00230     case URL_IS_HTTP:
00231     case URL_IS_DASH:
00232     default:
00233         /* Either use the pipe to prelink -y or open the URL. */
00234         fd = (pid != 0) ? fdDup(fdno) : Fopen(fn, "r.ufdio");
00235         (void) close(fdno);
00236         if (fd == NULL || Ferror(fd)) {
00237             rc = 1;
00238             if (fd != NULL)
00239                 (void) Fclose(fd);
00240             break;
00241         }
00242         
00243         fdInitDigest(fd, PGPHASHALGO_MD5, 0);
00244         fsize = 0;
00245         while ((rc = Fread(buf, sizeof(buf[0]), sizeof(buf), fd)) > 0)
00246             fsize += rc;
00247         fdFiniDigest(fd, PGPHASHALGO_MD5, (void **)&md5sum, &md5len, asAscii);
00248         if (Ferror(fd))
00249             rc = 1;
00250 
00251         (void) Fclose(fd);
00252         break;
00253     }
00254 
00255     /* Reap the prelink -y helper. */
00256     if (pid) {
00257         int status;
00258         (void) waitpid(pid, &status, 0);
00259         if (!WIFEXITED(status) || WEXITSTATUS(status))
00260             rc = 1;
00261     }
00262 
00263 exit:
00264 /*@-boundswrite@*/
00265     if (fsizep)
00266         *fsizep = fsize;
00267     if (!rc)
00268         memcpy(digest, md5sum, md5len);
00269 /*@=boundswrite@*/
00270     md5sum = _free(md5sum);
00271 
00272     return rc;
00273 }
00274 
00275 /*@-exportheadervar@*/
00276 /*@unchecked@*/
00277 int _noDirTokens = 0;
00278 /*@=exportheadervar@*/
00279 
00280 /*@-boundsread@*/
00281 static int dncmp(const void * a, const void * b)
00282         /*@*/
00283 {
00284     const char *const * first = a;
00285     const char *const * second = b;
00286     return strcmp(*first, *second);
00287 }
00288 /*@=boundsread@*/
00289 
00290 /*@-bounds@*/
00291 void compressFilelist(Header h)
00292 {
00293     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00294     HAE_t hae = (HAE_t)headerAddEntry;
00295     HRE_t hre = (HRE_t)headerRemoveEntry;
00296     HFD_t hfd = headerFreeData;
00297     char ** fileNames;
00298     const char ** dirNames;
00299     const char ** baseNames;
00300     int_32 * dirIndexes;
00301     rpmTagType fnt;
00302     int count;
00303     int i, xx;
00304     int dirIndex = -1;
00305 
00306     /*
00307      * This assumes the file list is already sorted, and begins with a
00308      * single '/'. That assumption isn't critical, but it makes things go
00309      * a bit faster.
00310      */
00311 
00312     if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
00313         xx = hre(h, RPMTAG_OLDFILENAMES);
00314         return;         /* Already converted. */
00315     }
00316 
00317     if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (void **) &fileNames, &count))
00318         return;         /* no file list */
00319     if (fileNames == NULL || count <= 0)
00320         return;
00321 
00322     dirNames = alloca(sizeof(*dirNames) * count);       /* worst case */
00323     baseNames = alloca(sizeof(*dirNames) * count);
00324     dirIndexes = alloca(sizeof(*dirIndexes) * count);
00325 
00326     if (fileNames[0][0] != '/') {
00327         /* HACK. Source RPM, so just do things differently */
00328         dirIndex = 0;
00329         dirNames[dirIndex] = "";
00330         for (i = 0; i < count; i++) {
00331             dirIndexes[i] = dirIndex;
00332             baseNames[i] = fileNames[i];
00333         }
00334         goto exit;
00335     }
00336 
00337     /*@-branchstate@*/
00338     for (i = 0; i < count; i++) {
00339         const char ** needle;
00340         char savechar;
00341         char * baseName;
00342         int len;
00343 
00344         if (fileNames[i] == NULL)       /* XXX can't happen */
00345             continue;
00346         baseName = strrchr(fileNames[i], '/') + 1;
00347         len = baseName - fileNames[i];
00348         needle = dirNames;
00349         savechar = *baseName;
00350         *baseName = '\0';
00351 /*@-compdef@*/
00352         if (dirIndex < 0 ||
00353             (needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
00354             char *s = alloca(len + 1);
00355             memcpy(s, fileNames[i], len + 1);
00356             s[len] = '\0';
00357             dirIndexes[i] = ++dirIndex;
00358             dirNames[dirIndex] = s;
00359         } else
00360             dirIndexes[i] = needle - dirNames;
00361 /*@=compdef@*/
00362 
00363         *baseName = savechar;
00364         baseNames[i] = baseName;
00365     }
00366     /*@=branchstate@*/
00367 
00368 exit:
00369     if (count > 0) {
00370         xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
00371         xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
00372                         baseNames, count);
00373         xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
00374                         dirNames, dirIndex + 1);
00375     }
00376 
00377     fileNames = hfd(fileNames, fnt);
00378 
00379     xx = hre(h, RPMTAG_OLDFILENAMES);
00380 }
00381 /*@=bounds@*/
00382 
00383 void rpmfiBuildFNames(Header h, rpmTag tagN,
00384         /*@out@*/ const char *** fnp, /*@out@*/ int * fcp)
00385 {
00386     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00387     HFD_t hfd = headerFreeData;
00388     const char ** baseNames;
00389     const char ** dirNames;
00390     int * dirIndexes;
00391     int count;
00392     const char ** fileNames;
00393     int size;
00394     rpmTag dirNameTag = 0;
00395     rpmTag dirIndexesTag = 0;
00396     rpmTagType bnt, dnt;
00397     char * t;
00398     int i, xx;
00399 
00400     if (tagN == RPMTAG_BASENAMES) {
00401         dirNameTag = RPMTAG_DIRNAMES;
00402         dirIndexesTag = RPMTAG_DIRINDEXES;
00403     } else if (tagN == RPMTAG_ORIGBASENAMES) {
00404         dirNameTag = RPMTAG_ORIGDIRNAMES;
00405         dirIndexesTag = RPMTAG_ORIGDIRINDEXES;
00406     }
00407 
00408     if (!hge(h, tagN, &bnt, (void **) &baseNames, &count)) {
00409         if (fnp) *fnp = NULL;
00410         if (fcp) *fcp = 0;
00411         return;         /* no file list */
00412     }
00413 
00414     xx = hge(h, dirNameTag, &dnt, (void **) &dirNames, NULL);
00415     xx = hge(h, dirIndexesTag, NULL, (void **) &dirIndexes, &count);
00416 
00417     size = sizeof(*fileNames) * count;
00418     for (i = 0; i < count; i++)
00419         size += strlen(baseNames[i]) + strlen(dirNames[dirIndexes[i]]) + 1;
00420 
00421     fileNames = xmalloc(size);
00422     t = ((char *) fileNames) + (sizeof(*fileNames) * count);
00423     /*@-branchstate@*/
00424     for (i = 0; i < count; i++) {
00425         fileNames[i] = t;
00426         t = stpcpy( stpcpy(t, dirNames[dirIndexes[i]]), baseNames[i]);
00427         *t++ = '\0';
00428     }
00429     /*@=branchstate@*/
00430     baseNames = hfd(baseNames, bnt);
00431     dirNames = hfd(dirNames, dnt);
00432 
00433     /*@-branchstate@*/
00434     if (fnp)
00435         *fnp = fileNames;
00436     else
00437         fileNames = _free(fileNames);
00438     /*@=branchstate@*/
00439     if (fcp) *fcp = count;
00440 }
00441 
00442 void expandFilelist(Header h)
00443 {
00444     HAE_t hae = (HAE_t)headerAddEntry;
00445     HRE_t hre = (HRE_t)headerRemoveEntry;
00446     const char ** fileNames = NULL;
00447     int count = 0;
00448     int xx;
00449 
00450     /*@-branchstate@*/
00451     if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
00452         rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fileNames, &count);
00453         if (fileNames == NULL || count <= 0)
00454             return;
00455         xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
00456                         fileNames, count);
00457         fileNames = _free(fileNames);
00458     }
00459     /*@=branchstate@*/
00460 
00461     xx = hre(h, RPMTAG_DIRNAMES);
00462     xx = hre(h, RPMTAG_BASENAMES);
00463     xx = hre(h, RPMTAG_DIRINDEXES);
00464 }
00465 
00466 /*
00467  * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
00468  * Retrofit an explicit "Provides: name = epoch:version-release.
00469  */
00470 void providePackageNVR(Header h)
00471 {
00472     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00473     HFD_t hfd = headerFreeData;
00474     const char *name, *version, *release;
00475     int_32 * epoch;
00476     const char *pEVR;
00477     char *p;
00478     int_32 pFlags = RPMSENSE_EQUAL;
00479     const char ** provides = NULL;
00480     const char ** providesEVR = NULL;
00481     rpmTagType pnt, pvt;
00482     int_32 * provideFlags = NULL;
00483     int providesCount;
00484     int i, xx;
00485     int bingo = 1;
00486 
00487     /* Generate provides for this package name-version-release. */
00488     xx = headerNVR(h, &name, &version, &release);
00489     if (!(name && version && release))
00490         return;
00491     pEVR = p = alloca(21 + strlen(version) + 1 + strlen(release) + 1);
00492     *p = '\0';
00493     if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
00494         sprintf(p, "%d:", *epoch);
00495         while (*p != '\0')
00496             p++;
00497     }
00498     (void) stpcpy( stpcpy( stpcpy(p, version) , "-") , release);
00499 
00500     /*
00501      * Rpm prior to 3.0.3 does not have versioned provides.
00502      * If no provides at all are available, we can just add.
00503      */
00504     if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides, &providesCount))
00505         goto exit;
00506 
00507     /*
00508      * Otherwise, fill in entries on legacy packages.
00509      */
00510     if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (void **) &providesEVR, NULL)) {
00511         for (i = 0; i < providesCount; i++) {
00512             char * vdummy = "";
00513             int_32 fdummy = RPMSENSE_ANY;
00514             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00515                         &vdummy, 1);
00516             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00517                         &fdummy, 1);
00518         }
00519         goto exit;
00520     }
00521 
00522     xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (void **) &provideFlags, NULL);
00523 
00524     /*@-nullderef@*/    /* LCL: providesEVR is not NULL */
00525     if (provides && providesEVR && provideFlags)
00526     for (i = 0; i < providesCount; i++) {
00527         if (!(provides[i] && providesEVR[i]))
00528             continue;
00529         if (!(provideFlags[i] == RPMSENSE_EQUAL &&
00530             !strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
00531             continue;
00532         bingo = 0;
00533         break;
00534     }
00535     /*@=nullderef@*/
00536 
00537 exit:
00538     provides = hfd(provides, pnt);
00539     providesEVR = hfd(providesEVR, pvt);
00540 
00541     if (bingo) {
00542         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
00543                 &name, 1);
00544         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00545                 &pFlags, 1);
00546         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00547                 &pEVR, 1);
00548     }
00549 }
00550 
00551 void legacyRetrofit(Header h, const struct rpmlead * lead)
00552 {
00553     const char * prefix;
00554 
00555     /*
00556      * We don't use these entries (and rpm >= 2 never has) and they are
00557      * pretty misleading. Let's just get rid of them so they don't confuse
00558      * anyone.
00559      */
00560     if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
00561         (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
00562     if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
00563         (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
00564 
00565     /*
00566      * We switched the way we do relocatable packages. We fix some of
00567      * it up here, though the install code still has to be a bit 
00568      * careful. This fixup makes queries give the new values though,
00569      * which is quite handy.
00570      */
00571     /*@=branchstate@*/
00572     if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &prefix, NULL))
00573     {
00574         const char * nprefix = stripTrailingChar(alloca_strdup(prefix), '/');
00575         (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
00576                 &nprefix, 1); 
00577     }
00578     /*@=branchstate@*/
00579 
00580     /*
00581      * The file list was moved to a more compressed format which not
00582      * only saves memory (nice), but gives fingerprinting a nice, fat
00583      * speed boost (very nice). Go ahead and convert old headers to
00584      * the new style (this is a noop for new headers).
00585      */
00586     if (lead->major < 4)
00587         compressFilelist(h);
00588 
00589     /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
00590     if (lead->type == RPMLEAD_SOURCE) {
00591         int_32 one = 1;
00592         if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
00593             (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
00594                                 &one, 1);
00595     } else if (lead->major < 4) {
00596         /* Retrofit "Provide: name = EVR" for binary packages. */
00597         providePackageNVR(h);
00598     }
00599 }

Generated on Fri Feb 18 20:26:12 2005 for rpm by  doxygen 1.3.9.1