00001 #include "system.h"
00002
00003 #include <signal.h>
00004
00005 #include <rpmbuild.h>
00006 #include <argv.h>
00007 #include <rpmfc.h>
00008
00009 #define _RPMDS_INTERNAL
00010 #include <rpmds.h>
00011 #include <rpmfi.h>
00012
00013 #if HAVE_GELF_H
00014 #include <gelf.h>
00015
00016 #if !defined(DT_GNU_HASH)
00017 #define DT_GNU_HASH 0x6ffffef5
00018 #endif
00019
00020 #endif
00021
00022 #include "debug.h"
00023
00024
00025
00028 static int rpmfcExpandAppend( ARGV_t * argvp, const ARGV_t av)
00029
00030
00031
00032 {
00033 ARGV_t argv = *argvp;
00034 int argc = argvCount(argv);
00035 int ac = argvCount(av);
00036 int i;
00037
00038
00039 argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv));
00040
00041 for (i = 0; i < ac; i++)
00042 argv[argc + i] = rpmExpand(av[i], NULL);
00043 argv[argc + ac] = NULL;
00044 *argvp = argv;
00045 return 0;
00046 }
00047
00058
00059 static StringBuf getOutputFrom( const char * dir, ARGV_t argv,
00060 const char * writePtr, int writeBytesLeft,
00061 int failNonZero)
00062
00063
00064 {
00065 pid_t child, reaped;
00066 int toProg[2];
00067 int fromProg[2];
00068 int status;
00069 void *oldhandler;
00070 StringBuf readBuff;
00071 int done;
00072
00073
00074 oldhandler = signal(SIGPIPE, SIG_IGN);
00075
00076
00077 toProg[0] = toProg[1] = 0;
00078 (void) pipe(toProg);
00079 fromProg[0] = fromProg[1] = 0;
00080 (void) pipe(fromProg);
00081
00082 if (!(child = fork())) {
00083 (void) close(toProg[1]);
00084 (void) close(fromProg[0]);
00085
00086 (void) dup2(toProg[0], STDIN_FILENO);
00087 (void) dup2(fromProg[1], STDOUT_FILENO);
00088
00089 (void) close(toProg[0]);
00090 (void) close(fromProg[1]);
00091
00092 if (dir) {
00093 (void) chdir(dir);
00094 }
00095
00096 rpmMessage(RPMMESS_DEBUG, _("\texecv(%s) pid %d\n"),
00097 argv[0], (unsigned)getpid());
00098
00099 unsetenv("MALLOC_CHECK_");
00100 (void) execvp(argv[0], (char *const *)argv);
00101
00102 rpmError(RPMERR_EXEC, _("Couldn't exec %s: %s\n"),
00103 argv[0], strerror(errno));
00104 _exit(RPMERR_EXEC);
00105 }
00106 if (child < 0) {
00107 rpmError(RPMERR_FORK, _("Couldn't fork %s: %s\n"),
00108 argv[0], strerror(errno));
00109 return NULL;
00110 }
00111
00112 (void) close(toProg[0]);
00113 (void) close(fromProg[1]);
00114
00115
00116 (void) fcntl(fromProg[0], F_SETFL, O_NONBLOCK);
00117 (void) fcntl(toProg[1], F_SETFL, O_NONBLOCK);
00118
00119 readBuff = newStringBuf();
00120
00121 do {
00122 fd_set ibits, obits;
00123 struct timeval tv;
00124 int nfd, nbw, nbr;
00125 int rc;
00126
00127 done = 0;
00128 top:
00129 FD_ZERO(&ibits);
00130 FD_ZERO(&obits);
00131 if (fromProg[0] >= 0) {
00132 FD_SET(fromProg[0], &ibits);
00133 }
00134 if (toProg[1] >= 0) {
00135 FD_SET(toProg[1], &obits);
00136 }
00137
00138 tv.tv_sec = 0;
00139 tv.tv_usec = 10000;
00140 nfd = ((fromProg[0] > toProg[1]) ? fromProg[0] : toProg[1]);
00141 if ((rc = select(nfd, &ibits, &obits, NULL, &tv)) < 0) {
00142 if (errno == EINTR)
00143 goto top;
00144 break;
00145 }
00146
00147
00148 if (toProg[1] >= 0 && FD_ISSET(toProg[1], &obits)) {
00149 if (writePtr && writeBytesLeft > 0) {
00150 if ((nbw = write(toProg[1], writePtr,
00151 (1024<writeBytesLeft) ? 1024 : writeBytesLeft)) < 0) {
00152 if (errno != EAGAIN) {
00153 perror("getOutputFrom()");
00154 exit(EXIT_FAILURE);
00155 }
00156 nbw = 0;
00157 }
00158 writeBytesLeft -= nbw;
00159 writePtr += nbw;
00160 } else if (toProg[1] >= 0) {
00161 (void) close(toProg[1]);
00162 toProg[1] = -1;
00163 }
00164 }
00165
00166
00167
00168 { char buf[BUFSIZ+1];
00169 while ((nbr = read(fromProg[0], buf, sizeof(buf)-1)) > 0) {
00170 buf[nbr] = '\0';
00171 appendStringBuf(readBuff, buf);
00172 }
00173 }
00174
00175
00176
00177 done = (nbr == 0 || (nbr < 0 && errno != EAGAIN));
00178
00179 } while (!done);
00180
00181
00182 if (toProg[1] >= 0)
00183 (void) close(toProg[1]);
00184 if (fromProg[0] >= 0)
00185 (void) close(fromProg[0]);
00186
00187 (void) signal(SIGPIPE, oldhandler);
00188
00189
00190
00191 reaped = waitpid(child, &status, 0);
00192 rpmMessage(RPMMESS_DEBUG, _("\twaitpid(%d) rc %d status %x\n"),
00193 (unsigned)child, (unsigned)reaped, status);
00194
00195 if (failNonZero && (!WIFEXITED(status) || WEXITSTATUS(status))) {
00196 rpmError(RPMERR_EXEC, _("%s failed\n"), argv[0]);
00197 return NULL;
00198 }
00199 if (writeBytesLeft) {
00200 rpmError(RPMERR_EXEC, _("failed to write all data to %s\n"), argv[0]);
00201 return NULL;
00202 }
00203 return readBuff;
00204 }
00205
00206 int rpmfcExec(ARGV_t av, StringBuf sb_stdin, StringBuf * sb_stdoutp,
00207 int failnonzero)
00208 {
00209 const char * s = NULL;
00210 ARGV_t xav = NULL;
00211 ARGV_t pav = NULL;
00212 int pac = 0;
00213 int ec = -1;
00214 StringBuf sb = NULL;
00215 const char * buf_stdin = NULL;
00216 int buf_stdin_len = 0;
00217 int xx;
00218
00219 if (sb_stdoutp)
00220 *sb_stdoutp = NULL;
00221 if (!(av && *av))
00222 goto exit;
00223
00224
00225 s = rpmExpand(av[0], NULL);
00226 if (!(s && *s))
00227 goto exit;
00228
00229
00230 pac = 0;
00231 xx = poptParseArgvString(s, &pac, (const char ***)&pav);
00232 if (!(xx == 0 && pac > 0 && pav != NULL))
00233 goto exit;
00234
00235
00236 xav = NULL;
00237
00238 xx = argvAppend(&xav, pav);
00239 if (av[1])
00240 xx = rpmfcExpandAppend(&xav, av + 1);
00241
00242
00243 if (sb_stdin != NULL) {
00244 buf_stdin = getStringBuf(sb_stdin);
00245 buf_stdin_len = strlen(buf_stdin);
00246 }
00247
00248
00249 sb = getOutputFrom(NULL, xav, buf_stdin, buf_stdin_len, failnonzero);
00250
00251
00252 if (sb_stdoutp != NULL) {
00253 *sb_stdoutp = sb;
00254 sb = NULL;
00255 }
00256
00257
00258 ec = 0;
00259
00260 exit:
00261 sb = freeStringBuf(sb);
00262 xav = argvFree(xav);
00263 pav = _free(pav);
00264 s = _free(s);
00265 return ec;
00266 }
00267
00270 static int rpmfcSaveArg( ARGV_t * argvp, const char * key)
00271
00272
00273 {
00274 int rc = 0;
00275
00276 if (argvSearch(*argvp, key, NULL) == NULL) {
00277 rc = argvAdd(argvp, key);
00278 rc = argvSort(*argvp, NULL);
00279 }
00280 return rc;
00281 }
00282
00283 static char * rpmfcFileDep( char * buf, int ix,
00284 rpmds ds)
00285
00286
00287
00288 {
00289 int_32 tagN = rpmdsTagN(ds);
00290 char deptype = 'X';
00291
00292 buf[0] = '\0';
00293 switch (tagN) {
00294 case RPMTAG_PROVIDENAME:
00295 deptype = 'P';
00296 break;
00297 case RPMTAG_REQUIRENAME:
00298 deptype = 'R';
00299 break;
00300 }
00301
00302 if (ds != NULL)
00303 sprintf(buf, "%08d%c %s %s 0x%08x", ix, deptype,
00304 rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
00305
00306 return buf;
00307 };
00308
00316 static int rpmfcHelper(rpmfc fc, unsigned char deptype, const char * nsdep)
00317
00318
00319 {
00320 const char * fn = fc->fn[fc->ix];
00321 char buf[BUFSIZ];
00322 StringBuf sb_stdout = NULL;
00323 StringBuf sb_stdin;
00324 const char *av[2];
00325 rpmds * depsp, ds;
00326 const char * N;
00327 const char * EVR;
00328 int_32 Flags, dsContext, tagN;
00329 ARGV_t pav;
00330 const char * s;
00331 int pac;
00332 int xx;
00333 int i;
00334
00335 switch (deptype) {
00336 default:
00337 return -1;
00338 break;
00339 case 'P':
00340 if (fc->skipProv)
00341 return 0;
00342 xx = snprintf(buf, sizeof(buf), "%%{?__%s_provides}", nsdep);
00343 depsp = &fc->provides;
00344 dsContext = RPMSENSE_FIND_PROVIDES;
00345 tagN = RPMTAG_PROVIDENAME;
00346 break;
00347 case 'R':
00348 if (fc->skipReq)
00349 return 0;
00350 xx = snprintf(buf, sizeof(buf), "%%{?__%s_requires}", nsdep);
00351 depsp = &fc->requires;
00352 dsContext = RPMSENSE_FIND_REQUIRES;
00353 tagN = RPMTAG_REQUIRENAME;
00354 break;
00355 }
00356 buf[sizeof(buf)-1] = '\0';
00357 av[0] = buf;
00358 av[1] = NULL;
00359
00360 sb_stdin = newStringBuf();
00361 appendLineStringBuf(sb_stdin, fn);
00362 sb_stdout = NULL;
00363
00364 xx = rpmfcExec(av, sb_stdin, &sb_stdout, 0);
00365
00366 sb_stdin = freeStringBuf(sb_stdin);
00367
00368 if (xx == 0 && sb_stdout != NULL) {
00369 pav = NULL;
00370 xx = argvSplit(&pav, getStringBuf(sb_stdout), " \t\n\r");
00371 pac = argvCount(pav);
00372 if (pav)
00373 for (i = 0; i < pac; i++) {
00374 N = pav[i];
00375 EVR = "";
00376 Flags = dsContext;
00377
00378 if (pav[i+1] && strchr("=<>", *pav[i+1])) {
00379 i++;
00380 for (s = pav[i]; *s; s++) {
00381 switch(*s) {
00382 default:
00383 assert(*s != '\0');
00384 break;
00385 case '=':
00386 Flags |= RPMSENSE_EQUAL;
00387 break;
00388 case '<':
00389 Flags |= RPMSENSE_LESS;
00390 break;
00391 case '>':
00392 Flags |= RPMSENSE_GREATER;
00393 break;
00394 }
00395 }
00396 i++;
00397 EVR = pav[i];
00398 assert(EVR != NULL);
00399 }
00400
00401
00402
00403
00404 if (!fc->tracked && deptype == 'P' && *EVR != '\0') {
00405 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
00406 "rpmlib(VersionedDependencies)", "3.0.3-1",
00407 RPMSENSE_RPMLIB|(RPMSENSE_LESS|RPMSENSE_EQUAL));
00408 xx = rpmdsMerge(&fc->requires, ds);
00409 ds = rpmdsFree(ds);
00410 fc->tracked = 1;
00411 }
00412
00413 ds = rpmdsSingle(tagN, N, EVR, Flags);
00414
00415
00416 xx = rpmdsMerge(depsp, ds);
00417
00418
00419
00420 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(buf, fc->ix, ds));
00421
00422
00423 ds = rpmdsFree(ds);
00424 }
00425
00426 pav = argvFree(pav);
00427 }
00428 sb_stdout = freeStringBuf(sb_stdout);
00429
00430 return 0;
00431 }
00432
00435
00436 static struct rpmfcTokens_s rpmfcTokens[] = {
00437 { "directory", RPMFC_DIRECTORY|RPMFC_INCLUDE },
00438
00439 { " shared object", RPMFC_LIBRARY },
00440 { " executable", RPMFC_EXECUTABLE },
00441 { " statically linked", RPMFC_STATIC },
00442 { " not stripped", RPMFC_NOTSTRIPPED },
00443 { " archive", RPMFC_ARCHIVE },
00444
00445 { "ELF 32-bit", RPMFC_ELF32|RPMFC_INCLUDE },
00446 { "ELF 64-bit", RPMFC_ELF64|RPMFC_INCLUDE },
00447
00448 { " script", RPMFC_SCRIPT },
00449 { " text", RPMFC_TEXT },
00450 { " document", RPMFC_DOCUMENT },
00451
00452 { " compressed", RPMFC_COMPRESSED },
00453
00454 { "troff or preprocessor input", RPMFC_MANPAGE|RPMFC_INCLUDE },
00455 { "GNU Info", RPMFC_MANPAGE|RPMFC_INCLUDE },
00456
00457 { "perl script text", RPMFC_PERL|RPMFC_INCLUDE },
00458 { "Perl5 module source text", RPMFC_PERL|RPMFC_MODULE|RPMFC_INCLUDE },
00459
00460 { " /usr/bin/python", RPMFC_PYTHON|RPMFC_INCLUDE },
00461
00462
00463
00464 { "python ", RPMFC_PYTHON|RPMFC_INCLUDE },
00465
00466
00467
00468 { "PE executable", RPMFC_MONO|RPMFC_INCLUDE },
00469
00470 { "current ar archive", RPMFC_STATIC|RPMFC_LIBRARY|RPMFC_ARCHIVE|RPMFC_INCLUDE },
00471
00472 { "Zip archive data", RPMFC_COMPRESSED|RPMFC_ARCHIVE|RPMFC_INCLUDE },
00473 { "tar archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00474 { "cpio archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00475 { "RPM v3", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00476 { "RPM v4", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00477
00478 { " image", RPMFC_IMAGE|RPMFC_INCLUDE },
00479 { " font", RPMFC_FONT|RPMFC_INCLUDE },
00480 { " Font", RPMFC_FONT|RPMFC_INCLUDE },
00481
00482 { " commands", RPMFC_SCRIPT|RPMFC_INCLUDE },
00483 { " script", RPMFC_SCRIPT|RPMFC_INCLUDE },
00484
00485 { "empty", RPMFC_WHITE|RPMFC_INCLUDE },
00486
00487 { "HTML", RPMFC_WHITE|RPMFC_INCLUDE },
00488 { "SGML", RPMFC_WHITE|RPMFC_INCLUDE },
00489 { "XML", RPMFC_WHITE|RPMFC_INCLUDE },
00490
00491 { " program text", RPMFC_WHITE|RPMFC_INCLUDE },
00492 { " source", RPMFC_WHITE|RPMFC_INCLUDE },
00493 { "GLS_BINARY_LSB_FIRST", RPMFC_WHITE|RPMFC_INCLUDE },
00494 { " DB ", RPMFC_WHITE|RPMFC_INCLUDE },
00495
00496 { "ASCII English text", RPMFC_WHITE|RPMFC_INCLUDE },
00497 { "ASCII text", RPMFC_WHITE|RPMFC_INCLUDE },
00498 { "ISO-8859 text", RPMFC_WHITE|RPMFC_INCLUDE },
00499
00500 { "symbolic link to", RPMFC_SYMLINK|RPMFC_INCLUDE },
00501 { "socket", RPMFC_DEVICE },
00502 { "special", RPMFC_DEVICE },
00503
00504 { "ASCII", RPMFC_WHITE },
00505 { "ISO-8859", RPMFC_WHITE },
00506
00507 { "data", RPMFC_WHITE },
00508
00509 { "application", RPMFC_WHITE },
00510 { "boot", RPMFC_WHITE },
00511 { "catalog", RPMFC_WHITE },
00512 { "code", RPMFC_WHITE },
00513 { "file", RPMFC_WHITE },
00514 { "format", RPMFC_WHITE },
00515 { "message", RPMFC_WHITE },
00516 { "program", RPMFC_WHITE },
00517
00518 { "broken symbolic link to ", RPMFC_WHITE|RPMFC_ERROR },
00519 { "can't read", RPMFC_WHITE|RPMFC_ERROR },
00520 { "can't stat", RPMFC_WHITE|RPMFC_ERROR },
00521 { "executable, can't read", RPMFC_WHITE|RPMFC_ERROR },
00522 { "core file", RPMFC_WHITE|RPMFC_ERROR },
00523
00524 { NULL, RPMFC_BLACK }
00525 };
00526
00527 int rpmfcColoring(const char * fmstr)
00528 {
00529 rpmfcToken fct;
00530 int fcolor = RPMFC_BLACK;
00531
00532 for (fct = rpmfcTokens; fct->token != NULL; fct++) {
00533 if (strstr(fmstr, fct->token) == NULL)
00534 continue;
00535 fcolor |= fct->colors;
00536 if (fcolor & RPMFC_INCLUDE)
00537 return fcolor;
00538 }
00539 return fcolor;
00540 }
00541
00542 void rpmfcPrint(const char * msg, rpmfc fc, FILE * fp)
00543 {
00544 int fcolor;
00545 int ndx;
00546 int cx;
00547 int dx;
00548 int fx;
00549
00550 int nprovides;
00551 int nrequires;
00552
00553 if (fp == NULL) fp = stderr;
00554
00555 if (msg)
00556 fprintf(fp, "===================================== %s\n", msg);
00557
00558 nprovides = rpmdsCount(fc->provides);
00559 nrequires = rpmdsCount(fc->requires);
00560
00561 if (fc)
00562 for (fx = 0; fx < fc->nfiles; fx++) {
00563 assert(fx < fc->fcdictx->nvals);
00564 cx = fc->fcdictx->vals[fx];
00565 assert(fx < fc->fcolor->nvals);
00566 fcolor = fc->fcolor->vals[fx];
00567
00568 fprintf(fp, "%3d %s", fx, fc->fn[fx]);
00569 if (fcolor != RPMFC_BLACK)
00570 fprintf(fp, "\t0x%x", fc->fcolor->vals[fx]);
00571 else
00572 fprintf(fp, "\t%s", fc->cdict[cx]);
00573 fprintf(fp, "\n");
00574
00575 if (fc->fddictx == NULL || fc->fddictn == NULL)
00576 continue;
00577
00578 assert(fx < fc->fddictx->nvals);
00579 dx = fc->fddictx->vals[fx];
00580 assert(fx < fc->fddictn->nvals);
00581 ndx = fc->fddictn->vals[fx];
00582
00583 while (ndx-- > 0) {
00584 const char * depval;
00585 unsigned char deptype;
00586 unsigned ix;
00587
00588 ix = fc->ddictx->vals[dx++];
00589 deptype = ((ix >> 24) & 0xff);
00590 ix &= 0x00ffffff;
00591 depval = NULL;
00592 switch (deptype) {
00593 default:
00594 assert(depval != NULL);
00595 break;
00596 case 'P':
00597 if (nprovides > 0) {
00598 assert(ix < nprovides);
00599 (void) rpmdsSetIx(fc->provides, ix-1);
00600 if (rpmdsNext(fc->provides) >= 0)
00601 depval = rpmdsDNEVR(fc->provides);
00602 }
00603 break;
00604 case 'R':
00605 if (nrequires > 0) {
00606 assert(ix < nrequires);
00607 (void) rpmdsSetIx(fc->requires, ix-1);
00608 if (rpmdsNext(fc->requires) >= 0)
00609 depval = rpmdsDNEVR(fc->requires);
00610 }
00611 break;
00612 }
00613 if (depval)
00614 fprintf(fp, "\t%s\n", depval);
00615 }
00616 }
00617 }
00618
00619 rpmfc rpmfcFree(rpmfc fc)
00620 {
00621 if (fc) {
00622 fc->fn = argvFree(fc->fn);
00623 fc->fcolor = argiFree(fc->fcolor);
00624 fc->fcdictx = argiFree(fc->fcdictx);
00625 fc->fddictx = argiFree(fc->fddictx);
00626 fc->fddictn = argiFree(fc->fddictn);
00627 fc->cdict = argvFree(fc->cdict);
00628 fc->ddict = argvFree(fc->ddict);
00629 fc->ddictx = argiFree(fc->ddictx);
00630
00631 fc->provides = rpmdsFree(fc->provides);
00632 fc->requires = rpmdsFree(fc->requires);
00633
00634 fc->sb_java = freeStringBuf(fc->sb_java);
00635 fc->sb_perl = freeStringBuf(fc->sb_perl);
00636 fc->sb_python = freeStringBuf(fc->sb_python);
00637
00638 }
00639 fc = _free(fc);
00640 return NULL;
00641 }
00642
00643 rpmfc rpmfcNew(void)
00644 {
00645 rpmfc fc = xcalloc(1, sizeof(*fc));
00646 return fc;
00647 }
00648
00654 static int rpmfcSYMLINK(rpmfc fc)
00655 {
00656 const char * fn = fc->fn[fc->ix];
00657 struct stat sb;
00658 int fdno;
00659
00660 if (stat(fn, &sb) < 0)
00661 return -1;
00662 if (S_ISLNK(sb.st_mode))
00663 return -1;
00664
00665 fdno = open(fn, O_RDONLY);
00666 if (fdno < 0) {
00667 return fdno;
00668 }
00669
00670 #if HAVE_GELF_H && HAVE_LIBELF
00671 Elf * elf = NULL;
00672 GElf_Ehdr ehdr_mem, * ehdr;
00673 int isElf64;
00674 int i, cnt;
00675 char * soname = NULL;
00676 char buf[BUFSIZ];
00677 char * t;
00678 rpmds ds;
00679
00680 (void) elf_version(EV_CURRENT);
00681 elf = NULL;
00682 if ((elf = elf_begin (fdno, ELF_C_READ_MMAP, NULL)) == NULL
00683 || elf_kind(elf) != ELF_K_ELF
00684 || (ehdr = gelf_getehdr(elf, &ehdr_mem)) == NULL
00685 || ehdr->e_type != ET_DYN)
00686 goto exit;
00687
00688 isElf64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
00689
00690 for (i = 0; i < ehdr->e_phnum; ++i) {
00691 GElf_Phdr phdr_mem;
00692 GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
00693 GElf_Shdr shdr_mem;
00694 Elf_Data * data = NULL;
00695 Elf_Scn * scn;
00696 GElf_Shdr *shdr;
00697
00698 if (phdr == NULL || phdr->p_type != PT_DYNAMIC)
00699 continue;
00700
00701 scn = gelf_offscn(elf, phdr->p_offset);
00702 shdr = gelf_getshdr(scn, &shdr_mem);
00703
00704 if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
00705 data = elf_getdata (scn, NULL);
00706 if (data == NULL)
00707 continue;
00708
00709 for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt) {
00710 GElf_Dyn dynmem;
00711 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
00712 if (dyn == NULL)
00713 break;
00714 if (dyn->d_tag != DT_SONAME)
00715 continue;
00716
00717
00718 soname = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
00719 if (soname == NULL)
00720 break;
00721 buf[0] = '\0';
00722 t = buf;
00723 t = stpcpy(t, soname);
00724 #if !defined(__alpha__)
00725 if (isElf64)
00726 t = stpcpy(t, "()(64bit)");
00727 #endif
00728 t++;
00729
00730 if (!fc->skipReq) {
00731 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
00732 buf, "", RPMSENSE_FIND_REQUIRES);
00733 rpmdsMerge(&fc->requires, ds);
00734 rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
00735 ds = rpmdsFree(ds);
00736 }
00737 break;
00738 }
00739 }
00740 exit:
00741 if (elf) (void) elf_end(elf);
00742 close(fdno);
00743 return 0;
00744 #endif
00745 return -1;
00746 }
00747
00753 static int rpmfcSCRIPT(rpmfc fc)
00754
00755
00756 {
00757 const char * fn = fc->fn[fc->ix];
00758 const char * bn;
00759 rpmds ds;
00760 char buf[BUFSIZ];
00761 FILE * fp;
00762 char * s, * se;
00763 int i;
00764 struct stat sb, * st = &sb;
00765 int is_executable;
00766 int xx;
00767
00768
00769 if (stat(fn, st) < 0)
00770 return -1;
00771 is_executable = (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH));
00772
00773 fp = fopen(fn, "r");
00774 if (fp == NULL || ferror(fp)) {
00775 if (fp) (void) fclose(fp);
00776 return -1;
00777 }
00778
00779
00780
00781 for (i = 0; i < 10; i++) {
00782
00783 s = fgets(buf, sizeof(buf) - 1, fp);
00784 if (s == NULL || ferror(fp) || feof(fp))
00785 break;
00786 s[sizeof(buf)-1] = '\0';
00787 if (!(s[0] == '#' && s[1] == '!'))
00788 continue;
00789 s += 2;
00790
00791 while (*s && strchr(" \t\n\r", *s) != NULL)
00792 s++;
00793 if (*s == '\0')
00794 continue;
00795 if (*s != '/')
00796 continue;
00797
00798 for (se = s+1; *se; se++) {
00799 if (strchr(" \t\n\r", *se) != NULL)
00800 break;
00801 }
00802 *se = '\0';
00803 se++;
00804
00805 if (is_executable) {
00806
00807 ds = rpmdsSingle(RPMTAG_REQUIRENAME, s, "", RPMSENSE_FIND_REQUIRES);
00808 xx = rpmdsMerge(&fc->requires, ds);
00809
00810
00811 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(se, fc->ix, ds));
00812
00813 ds = rpmdsFree(ds);
00814 }
00815
00816
00817 bn = basename(s);
00818 if (!strcmp(bn, "perl"))
00819 fc->fcolor->vals[fc->ix] |= RPMFC_PERL;
00820 else if (!strncmp(bn, "python", sizeof("python")-1))
00821 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
00822
00823 break;
00824 }
00825
00826
00827 (void) fclose(fp);
00828
00829 if (fc->fcolor->vals[fc->ix] & RPMFC_PERL) {
00830 if (fc->fcolor->vals[fc->ix] & RPMFC_MODULE)
00831 xx = rpmfcHelper(fc, 'P', "perl");
00832 if (is_executable || (fc->fcolor->vals[fc->ix] & RPMFC_MODULE))
00833 xx = rpmfcHelper(fc, 'R', "perl");
00834 }
00835 if (fc->fcolor->vals[fc->ix] & RPMFC_PYTHON) {
00836 xx = rpmfcHelper(fc, 'P', "python");
00837 #ifdef NOTYET
00838 if (is_executable)
00839 #endif
00840 xx = rpmfcHelper(fc, 'R', "python");
00841 }
00842 if (fc->fcolor->vals[fc->ix] & RPMFC_MONO) {
00843 xx = rpmfcHelper(fc, 'P', "mono");
00844 if (is_executable)
00845 xx = rpmfcHelper(fc, 'R', "mono");
00846 }
00847
00848 return 0;
00849 }
00850
00856 static int rpmfcELF(rpmfc fc)
00857
00858
00859 {
00860 #if HAVE_GELF_H && HAVE_LIBELF
00861 const char * fn = fc->fn[fc->ix];
00862 Elf * elf;
00863 Elf_Scn * scn;
00864 Elf_Data * data;
00865 GElf_Ehdr ehdr_mem, * ehdr;
00866 GElf_Shdr shdr_mem, * shdr;
00867 GElf_Verdef def_mem, * def;
00868 GElf_Verneed need_mem, * need;
00869 GElf_Dyn dyn_mem, * dyn;
00870 unsigned int auxoffset;
00871 unsigned int offset;
00872 int fdno;
00873 int cnt2;
00874 int cnt;
00875 char buf[BUFSIZ];
00876 const char * s;
00877 struct stat sb, * st = &sb;
00878 const char * soname = NULL;
00879 rpmds * depsp, ds;
00880 int_32 tagN, dsContext;
00881 char * t;
00882 int xx;
00883 int isElf64;
00884 int isDSO;
00885 int gotSONAME = 0;
00886 int gotDEBUG = 0;
00887 int gotHASH = 0;
00888 int gotGNUHASH = 0;
00889 static int filter_GLIBC_PRIVATE = 0;
00890 static int oneshot = 0;
00891
00892 if (oneshot == 0) {
00893 oneshot = 1;
00894 filter_GLIBC_PRIVATE = rpmExpandNumeric("%{?_filter_GLIBC_PRIVATE}");
00895 }
00896
00897
00898 if (stat(fn, st) != 0)
00899 return(-1);
00900
00901 fdno = open(fn, O_RDONLY);
00902 if (fdno < 0)
00903 return fdno;
00904
00905 (void) elf_version(EV_CURRENT);
00906
00907
00908 elf = NULL;
00909 if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL
00910 || elf_kind(elf) != ELF_K_ELF
00911 || (ehdr = gelf_getehdr(elf, &ehdr_mem)) == NULL
00912 || !(ehdr->e_type == ET_DYN || ehdr->e_type == ET_EXEC))
00913 goto exit;
00914
00915
00916 isElf64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
00917 isDSO = ehdr->e_type == ET_DYN;
00918
00919
00920 scn = NULL;
00921 while ((scn = elf_nextscn(elf, scn)) != NULL) {
00922 shdr = gelf_getshdr(scn, &shdr_mem);
00923 if (shdr == NULL)
00924 break;
00925
00926 soname = _free(soname);
00927 switch (shdr->sh_type) {
00928 default:
00929 continue;
00930 break;
00931 case SHT_GNU_verdef:
00932 data = NULL;
00933 if (!fc->skipProv)
00934 while ((data = elf_getdata (scn, data)) != NULL) {
00935 offset = 0;
00936 for (cnt = shdr->sh_info; --cnt >= 0; ) {
00937
00938 def = gelf_getverdef (data, offset, &def_mem);
00939 if (def == NULL)
00940 break;
00941 auxoffset = offset + def->vd_aux;
00942 for (cnt2 = def->vd_cnt; --cnt2 >= 0; ) {
00943 GElf_Verdaux aux_mem, * aux;
00944
00945 aux = gelf_getverdaux (data, auxoffset, &aux_mem);
00946 if (aux == NULL)
00947 break;
00948
00949 s = elf_strptr(elf, shdr->sh_link, aux->vda_name);
00950 if (s == NULL)
00951 break;
00952 if (def->vd_flags & VER_FLG_BASE) {
00953 soname = _free(soname);
00954 soname = xstrdup(s);
00955 auxoffset += aux->vda_next;
00956 continue;
00957 } else
00958 if (soname != NULL
00959 && !(filter_GLIBC_PRIVATE != 0
00960 && !strcmp(s, "GLIBC_PRIVATE")))
00961 {
00962 buf[0] = '\0';
00963 t = buf;
00964 t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
00965
00966 #if !defined(__alpha__)
00967 if (isElf64)
00968 t = stpcpy(t, "(64bit)");
00969 #endif
00970 t++;
00971
00972
00973 ds = rpmdsSingle(RPMTAG_PROVIDES,
00974 buf, "", RPMSENSE_FIND_PROVIDES);
00975 xx = rpmdsMerge(&fc->provides, ds);
00976
00977
00978 xx = rpmfcSaveArg(&fc->ddict,
00979 rpmfcFileDep(t, fc->ix, ds));
00980
00981 ds = rpmdsFree(ds);
00982 }
00983 auxoffset += aux->vda_next;
00984 }
00985 offset += def->vd_next;
00986 }
00987 }
00988 break;
00989 case SHT_GNU_verneed:
00990 data = NULL;
00991
00992 if (!fc->skipReq && (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
00993 while ((data = elf_getdata (scn, data)) != NULL) {
00994 offset = 0;
00995 for (cnt = shdr->sh_info; --cnt >= 0; ) {
00996 need = gelf_getverneed (data, offset, &need_mem);
00997 if (need == NULL)
00998 break;
00999
01000 s = elf_strptr(elf, shdr->sh_link, need->vn_file);
01001 if (s == NULL)
01002 break;
01003 soname = _free(soname);
01004 soname = xstrdup(s);
01005 auxoffset = offset + need->vn_aux;
01006 for (cnt2 = need->vn_cnt; --cnt2 >= 0; ) {
01007 GElf_Vernaux aux_mem, * aux;
01008
01009 aux = gelf_getvernaux (data, auxoffset, &aux_mem);
01010 if (aux == NULL)
01011 break;
01012
01013 s = elf_strptr(elf, shdr->sh_link, aux->vna_name);
01014 if (s == NULL)
01015 break;
01016
01017
01018 if (soname != NULL
01019 && !(filter_GLIBC_PRIVATE != 0
01020 && !strcmp(s, "GLIBC_PRIVATE")))
01021 {
01022 buf[0] = '\0';
01023 t = buf;
01024 t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
01025
01026 #if !defined(__alpha__)
01027 if (isElf64)
01028 t = stpcpy(t, "(64bit)");
01029 #endif
01030 t++;
01031
01032
01033 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
01034 buf, "", RPMSENSE_FIND_REQUIRES);
01035 xx = rpmdsMerge(&fc->requires, ds);
01036
01037
01038 xx = rpmfcSaveArg(&fc->ddict,
01039 rpmfcFileDep(t, fc->ix, ds));
01040 ds = rpmdsFree(ds);
01041 }
01042 auxoffset += aux->vna_next;
01043 }
01044 offset += need->vn_next;
01045 }
01046 }
01047 break;
01048 case SHT_DYNAMIC:
01049 data = NULL;
01050 while ((data = elf_getdata (scn, data)) != NULL) {
01051
01052 for (cnt = 0; cnt < (shdr->sh_size / shdr->sh_entsize); ++cnt) {
01053 dyn = gelf_getdyn (data, cnt, &dyn_mem);
01054 if (dyn == NULL)
01055 break;
01056 s = NULL;
01057 switch (dyn->d_tag) {
01058 default:
01059 continue;
01060 break;
01061 case DT_HASH:
01062 gotHASH= 1;
01063 continue;
01064 case DT_GNU_HASH:
01065 gotGNUHASH= 1;
01066 continue;
01067 case DT_DEBUG:
01068 gotDEBUG = 1;
01069 continue;
01070 case DT_NEEDED:
01071
01072 if (fc->skipReq || !(st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
01073 continue;
01074
01075 depsp = &fc->requires;
01076 tagN = RPMTAG_REQUIRENAME;
01077 dsContext = RPMSENSE_FIND_REQUIRES;
01078 s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
01079 assert(s != NULL);
01080 break;
01081 case DT_SONAME:
01082 gotSONAME = 1;
01083
01084 if (fc->skipProv)
01085 continue;
01086 depsp = &fc->provides;
01087 tagN = RPMTAG_PROVIDENAME;
01088 dsContext = RPMSENSE_FIND_PROVIDES;
01089 s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
01090 assert(s != NULL);
01091 break;
01092 }
01093 if (s == NULL)
01094 continue;
01095
01096 buf[0] = '\0';
01097 t = buf;
01098 t = stpcpy(t, s);
01099
01100 #if !defined(__alpha__)
01101 if (isElf64)
01102 t = stpcpy(t, "()(64bit)");
01103 #endif
01104 t++;
01105
01106
01107 ds = rpmdsSingle(tagN, buf, "", dsContext);
01108 xx = rpmdsMerge(depsp, ds);
01109
01110
01111 xx = rpmfcSaveArg(&fc->ddict,
01112 rpmfcFileDep(t, fc->ix, ds));
01113
01114 ds = rpmdsFree(ds);
01115 }
01116
01117 }
01118 break;
01119 }
01120 }
01121
01122
01123
01124
01125 if (gotGNUHASH && !gotHASH) {
01126 ds = rpmdsSingle(RPMTAG_REQUIRENAME, "rtld(GNU_HASH)", "",
01127 RPMSENSE_FIND_REQUIRES);
01128 rpmdsMerge(&fc->requires, ds);
01129 buf[0] = '\0';
01130 t = buf;
01131 rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
01132 ds = rpmdsFree(ds);
01133 }
01134
01135
01136 if (!fc->skipProv && isDSO && !gotDEBUG && !gotSONAME) {
01137 depsp = &fc->provides;
01138 tagN = RPMTAG_PROVIDENAME;
01139 dsContext = RPMSENSE_FIND_PROVIDES;
01140
01141 s = strrchr(fn, '/');
01142 if (s)
01143 s++;
01144 else
01145 s = fn;
01146
01147
01148 buf[0] = '\0';
01149 t = buf;
01150
01151 t = stpcpy(t, s);
01152
01153
01154 #if !defined(__alpha__)
01155 if (isElf64)
01156 t = stpcpy(t, "()(64bit)");
01157 #endif
01158
01159 t++;
01160
01161
01162 ds = rpmdsSingle(tagN, buf, "", dsContext);
01163 xx = rpmdsMerge(depsp, ds);
01164
01165
01166
01167 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
01168
01169
01170 ds = rpmdsFree(ds);
01171 }
01172
01173 exit:
01174 soname = _free(soname);
01175 if (elf) (void) elf_end(elf);
01176 xx = close(fdno);
01177 return 0;
01178 #else
01179 return -1;
01180 #endif
01181 }
01182
01183 typedef struct rpmfcApplyTbl_s {
01184 int (*func) (rpmfc fc);
01185 int colormask;
01186 } * rpmfcApplyTbl;
01187
01190
01191 static struct rpmfcApplyTbl_s rpmfcApplyTable[] = {
01192 { rpmfcELF, RPMFC_ELF },
01193 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PERL) },
01194 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PYTHON) },
01195 { rpmfcSCRIPT, RPMFC_MONO },
01196 { rpmfcSYMLINK, RPMFC_SYMLINK },
01197 { NULL, 0 }
01198 };
01199
01200 int rpmfcApply(rpmfc fc)
01201 {
01202 rpmfcApplyTbl fcat;
01203 const char * s;
01204 char * se;
01205 rpmds ds;
01206 const char * N;
01207 const char * EVR;
01208 int_32 Flags;
01209 unsigned char deptype;
01210 int nddict;
01211 int previx;
01212 unsigned int val;
01213 int dix;
01214 int ix;
01215 int i;
01216 int xx;
01217 int skipping;
01218
01219
01220 for (fc->ix = 0; fc->fn[fc->ix] != NULL; fc->ix++) {
01221
01222
01223
01224 { const char *fn = strstr(fc->fn[fc->ix], "/usr/lib");
01225 if (fn) {
01226 fn += sizeof("/usr/lib")-1;
01227 if (fn[0] == '6' && fn[1] == '4')
01228 fn += 2;
01229 if (!strncmp(fn, "/python", sizeof("/python")-1))
01230 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
01231 }
01232 }
01233
01234 if (fc->fcolor->vals[fc->ix])
01235 for (fcat = rpmfcApplyTable; fcat->func != NULL; fcat++) {
01236 if (!(fc->fcolor->vals[fc->ix] & fcat->colormask))
01237 continue;
01238 xx = (*fcat->func) (fc);
01239 }
01240 }
01241
01242
01243
01244 nddict = argvCount(fc->ddict);
01245 previx = -1;
01246 for (i = 0; i < nddict; i++) {
01247 s = fc->ddict[i];
01248
01249
01250 ix = strtol(s, &se, 10);
01251 assert(se != NULL);
01252 deptype = *se++;
01253 se++;
01254 N = se;
01255 while (*se && *se != ' ')
01256 se++;
01257 *se++ = '\0';
01258 EVR = se;
01259 while (*se && *se != ' ')
01260 se++;
01261 *se++ = '\0';
01262 Flags = strtol(se, NULL, 16);
01263
01264 dix = -1;
01265 skipping = 0;
01266 switch (deptype) {
01267 default:
01268 break;
01269 case 'P':
01270 skipping = fc->skipProv;
01271 ds = rpmdsSingle(RPMTAG_PROVIDENAME, N, EVR, Flags);
01272 dix = rpmdsFind(fc->provides, ds);
01273 ds = rpmdsFree(ds);
01274 break;
01275 case 'R':
01276 skipping = fc->skipReq;
01277 ds = rpmdsSingle(RPMTAG_REQUIRENAME, N, EVR, Flags);
01278 dix = rpmdsFind(fc->requires, ds);
01279 ds = rpmdsFree(ds);
01280 break;
01281 }
01282
01283
01284 #if 0
01285 assert(dix >= 0);
01286 #else
01287 if (dix < 0)
01288 continue;
01289 #endif
01290
01291 val = (deptype << 24) | (dix & 0x00ffffff);
01292 xx = argiAdd(&fc->ddictx, -1, val);
01293
01294 if (previx != ix) {
01295 previx = ix;
01296 xx = argiAdd(&fc->fddictx, ix, argiCount(fc->ddictx)-1);
01297 }
01298 if (fc->fddictn && fc->fddictn->vals && !skipping)
01299 fc->fddictn->vals[ix]++;
01300 }
01301
01302
01303 return 0;
01304 }
01305
01306 int rpmfcClassify(rpmfc fc, ARGV_t argv, int_16 * fmode)
01307 {
01308 ARGV_t fcav = NULL;
01309 ARGV_t dav;
01310 const char * s, * se;
01311 size_t slen;
01312 int fcolor;
01313 int xx;
01314
01315 static const char * magicfile = "/usr/lib/rpm/magic";
01316 int msflags = MAGIC_CHECK;
01317 magic_t ms = NULL;
01318
01319 if (fc == NULL || argv == NULL)
01320 return 0;
01321
01322 fc->nfiles = argvCount(argv);
01323
01324
01325 xx = argiAdd(&fc->fddictx, fc->nfiles-1, 0);
01326 xx = argiAdd(&fc->fddictn, fc->nfiles-1, 0);
01327
01328
01329 xx = argvAdd(&fc->cdict, "");
01330 xx = argvAdd(&fc->cdict, "directory");
01331
01332 ms = magic_open(msflags);
01333 if (ms == NULL) {
01334 xx = RPMERR_EXEC;
01335 rpmError(xx, _("magic_open(0x%x) failed: %s\n"),
01336 msflags, strerror(errno));
01337 assert(ms != NULL);
01338 }
01339
01340 xx = magic_load(ms, magicfile);
01341 if (xx == -1) {
01342 xx = RPMERR_EXEC;
01343 rpmError(xx, _("magic_load(ms, \"%s\") failed: %s\n"),
01344 magicfile, magic_error(ms));
01345 assert(xx != -1);
01346 }
01347
01348 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
01349 const char * ftype;
01350 int_16 mode = (fmode ? fmode[fc->ix] : 0);
01351 char dbuf[1024];
01352
01353 s = argv[fc->ix];
01354 assert(s != NULL);
01355 slen = strlen(s);
01356
01357 switch (mode & S_IFMT) {
01358 case S_IFCHR: ftype = "character special"; break;
01359 case S_IFBLK: ftype = "block special"; break;
01360 case S_IFIFO: ftype = "fifo (named pipe)"; break;
01361 case S_IFSOCK: ftype = "socket"; break;
01362 case S_IFDIR:
01363 case S_IFLNK:
01364 case S_IFREG:
01365 default:
01366
01367
01368 if (slen >= sizeof(".pm") && !strcmp(s+slen-(sizeof(".pm")-1), ".pm"))
01369 ftype = "Perl5 module source text";
01370
01371 else if (slen >= fc->brlen+sizeof("/dev/") && !strncmp(s+fc->brlen, "/dev/", sizeof("/dev/")-1))
01372 ftype = "";
01373 else
01374 ftype = magic_file(ms, s);
01375
01376 if (ftype == NULL) {
01377 xx = RPMERR_EXEC;
01378 rpmError(xx, _("magic_file(ms, \"%s\") failed: mode %06o %s\n"),
01379 s, mode, magic_error(ms));
01380 assert(ftype != NULL);
01381 }
01382 }
01383
01384
01385 se = ftype;
01386 rpmMessage(RPMMESS_DEBUG, "%s: %s\n", s, se);
01387
01388
01389 xx = argvAdd(&fc->fn, s);
01390
01391
01392 xx = argvAdd(&fcav, se);
01393
01394
01395 fcolor = rpmfcColoring(se);
01396 xx = argiAdd(&fc->fcolor, fc->ix, fcolor);
01397
01398
01399 if (fcolor != RPMFC_WHITE && (fcolor & RPMFC_INCLUDE))
01400 xx = rpmfcSaveArg(&fc->cdict, se);
01401
01402 }
01403
01404
01405 fc->fknown = 0;
01406 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
01407 se = fcav[fc->ix];
01408 assert(se != NULL);
01409
01410 dav = argvSearch(fc->cdict, se, NULL);
01411 if (dav) {
01412 xx = argiAdd(&fc->fcdictx, fc->ix, (dav - fc->cdict));
01413 fc->fknown++;
01414 } else {
01415 xx = argiAdd(&fc->fcdictx, fc->ix, 0);
01416 fc->fwhite++;
01417 }
01418 }
01419
01420 fcav = argvFree(fcav);
01421
01422 if (ms != NULL)
01423 magic_close(ms);
01424
01425 return 0;
01426 }
01427
01430 typedef struct DepMsg_s * DepMsg_t;
01431
01434 struct DepMsg_s {
01435
01436 const char * msg;
01437
01438 const char * argv[4];
01439 rpmTag ntag;
01440 rpmTag vtag;
01441 rpmTag ftag;
01442 int mask;
01443 int xor;
01444 };
01445
01448
01449 static struct DepMsg_s depMsgs[] = {
01450 { "Provides", { "%{?__find_provides}", NULL, NULL, NULL },
01451 RPMTAG_PROVIDENAME, RPMTAG_PROVIDEVERSION, RPMTAG_PROVIDEFLAGS,
01452 0, -1 },
01453 #ifdef DYING
01454 { "PreReq", { NULL, NULL, NULL, NULL },
01455 RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
01456 RPMSENSE_PREREQ, 0 },
01457 { "Requires(interp)", { NULL, "interp", NULL, NULL },
01458 -1, -1, RPMTAG_REQUIREFLAGS,
01459 _notpre(RPMSENSE_INTERP), 0 },
01460 #else
01461 { "Requires(interp)", { NULL, "interp", NULL, NULL },
01462 RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
01463 _notpre(RPMSENSE_INTERP), 0 },
01464 #endif
01465 { "Requires(rpmlib)", { NULL, "rpmlib", NULL, NULL },
01466 -1, -1, RPMTAG_REQUIREFLAGS,
01467 _notpre(RPMSENSE_RPMLIB), 0 },
01468 { "Requires(verify)", { NULL, "verify", NULL, NULL },
01469 -1, -1, RPMTAG_REQUIREFLAGS,
01470 RPMSENSE_SCRIPT_VERIFY, 0 },
01471 { "Requires(pre)", { NULL, "pre", NULL, NULL },
01472 -1, -1, RPMTAG_REQUIREFLAGS,
01473 _notpre(RPMSENSE_SCRIPT_PRE), 0 },
01474 { "Requires(post)", { NULL, "post", NULL, NULL },
01475 -1, -1, RPMTAG_REQUIREFLAGS,
01476 _notpre(RPMSENSE_SCRIPT_POST), 0 },
01477 { "Requires(preun)", { NULL, "preun", NULL, NULL },
01478 -1, -1, RPMTAG_REQUIREFLAGS,
01479 _notpre(RPMSENSE_SCRIPT_PREUN), 0 },
01480 { "Requires(postun)", { NULL, "postun", NULL, NULL },
01481 -1, -1, RPMTAG_REQUIREFLAGS,
01482 _notpre(RPMSENSE_SCRIPT_POSTUN), 0 },
01483 { "Requires", { "%{?__find_requires}", NULL, NULL, NULL },
01484 -1, -1, RPMTAG_REQUIREFLAGS,
01485 RPMSENSE_PREREQ, RPMSENSE_PREREQ },
01486 { "Conflicts", { "%{?__find_conflicts}", NULL, NULL, NULL },
01487 RPMTAG_CONFLICTNAME, RPMTAG_CONFLICTVERSION, RPMTAG_CONFLICTFLAGS,
01488 0, -1 },
01489 { "Obsoletes", { "%{?__find_obsoletes}", NULL, NULL, NULL },
01490 RPMTAG_OBSOLETENAME, RPMTAG_OBSOLETEVERSION, RPMTAG_OBSOLETEFLAGS,
01491 0, -1 },
01492 { NULL, { NULL, NULL, NULL, NULL }, 0, 0, 0, 0, 0 }
01493 };
01494
01495
01496 static DepMsg_t DepMsgs = depMsgs;
01497
01500 static void printDeps(Header h)
01501
01502
01503 {
01504 DepMsg_t dm;
01505 rpmds ds = NULL;
01506 int flags = 0x2;
01507 const char * DNEVR;
01508 int_32 Flags;
01509 int bingo = 0;
01510
01511 for (dm = DepMsgs; dm->msg != NULL; dm++) {
01512 if (dm->ntag != -1) {
01513 ds = rpmdsFree(ds);
01514 ds = rpmdsNew(h, dm->ntag, flags);
01515 }
01516 if (dm->ftag == 0)
01517 continue;
01518
01519 ds = rpmdsInit(ds);
01520 if (ds == NULL)
01521 continue;
01522
01523 bingo = 0;
01524 while (rpmdsNext(ds) >= 0) {
01525
01526 Flags = rpmdsFlags(ds);
01527
01528 if (!((Flags & dm->mask) ^ dm->xor))
01529 continue;
01530 if (bingo == 0) {
01531 rpmMessage(RPMMESS_NORMAL, "%s:", (dm->msg ? dm->msg : ""));
01532 bingo = 1;
01533 }
01534 if ((DNEVR = rpmdsDNEVR(ds)) == NULL)
01535 continue;
01536 rpmMessage(RPMMESS_NORMAL, " %s", DNEVR+2);
01537 }
01538 if (bingo)
01539 rpmMessage(RPMMESS_NORMAL, "\n");
01540 }
01541 ds = rpmdsFree(ds);
01542 }
01543
01546 static int rpmfcGenerateDependsHelper(const Spec spec, Package pkg, rpmfi fi)
01547
01548
01549 {
01550 StringBuf sb_stdin;
01551 StringBuf sb_stdout;
01552 DepMsg_t dm;
01553 int failnonzero = 0;
01554 int rc = 0;
01555
01556
01557
01558
01559 sb_stdin = newStringBuf();
01560 fi = rpmfiInit(fi, 0);
01561 if (fi != NULL)
01562 while (rpmfiNext(fi) >= 0)
01563 appendLineStringBuf(sb_stdin, rpmfiFN(fi));
01564
01565 for (dm = DepMsgs; dm->msg != NULL; dm++) {
01566 int tag, tagflags;
01567 char * s;
01568 int xx;
01569
01570 tag = (dm->ftag > 0) ? dm->ftag : dm->ntag;
01571 tagflags = 0;
01572 s = NULL;
01573
01574 switch(tag) {
01575 case RPMTAG_PROVIDEFLAGS:
01576 if (!pkg->autoProv)
01577 continue;
01578 failnonzero = 1;
01579 tagflags = RPMSENSE_FIND_PROVIDES;
01580 break;
01581 case RPMTAG_REQUIREFLAGS:
01582 if (!pkg->autoReq)
01583 continue;
01584 failnonzero = 0;
01585 tagflags = RPMSENSE_FIND_REQUIRES;
01586 break;
01587 default:
01588 continue;
01589 break;
01590 }
01591
01592
01593 xx = rpmfcExec(dm->argv, sb_stdin, &sb_stdout, failnonzero);
01594
01595 if (xx == -1)
01596 continue;
01597
01598 s = rpmExpand(dm->argv[0], NULL);
01599 rpmMessage(RPMMESS_NORMAL, _("Finding %s: %s\n"), dm->msg,
01600 (s ? s : ""));
01601 s = _free(s);
01602
01603 if (sb_stdout == NULL) {
01604 rc = RPMERR_EXEC;
01605 rpmError(rc, _("Failed to find %s:\n"), dm->msg);
01606 break;
01607 }
01608
01609
01610 rc = parseRCPOT(spec, pkg, getStringBuf(sb_stdout), tag, 0, tagflags);
01611 sb_stdout = freeStringBuf(sb_stdout);
01612
01613 if (rc) {
01614 rpmError(rc, _("Failed to find %s:\n"), dm->msg);
01615 break;
01616 }
01617 }
01618
01619 sb_stdin = freeStringBuf(sb_stdin);
01620
01621 return rc;
01622 }
01623
01624 int rpmfcGenerateDepends(const Spec spec, Package pkg)
01625 {
01626 rpmfi fi = pkg->cpioList;
01627 rpmfc fc = NULL;
01628 rpmds ds;
01629 int flags = 0x2;
01630 ARGV_t av;
01631 int_16 * fmode;
01632 int ac = rpmfiFC(fi);
01633 const void ** p;
01634 char buf[BUFSIZ];
01635 const char * N;
01636 const char * EVR;
01637 int genConfigDeps;
01638 int c;
01639 int rc = 0;
01640 int xx;
01641
01642
01643 if (ac <= 0)
01644 return 0;
01645
01646
01647 if (! (pkg->autoReq || pkg->autoProv))
01648 return 0;
01649
01650
01651 if (!rpmExpandNumeric("%{?_use_internal_dependency_generator}")) {
01652
01653 rc = rpmfcGenerateDependsHelper(spec, pkg, fi);
01654 printDeps(pkg->header);
01655 return rc;
01656 }
01657
01658
01659 av = xcalloc(ac+1, sizeof(*av));
01660 fmode = xcalloc(ac+1, sizeof(*fmode));
01661
01662
01663 genConfigDeps = 0;
01664 fi = rpmfiInit(fi, 0);
01665 if (fi != NULL)
01666 while ((c = rpmfiNext(fi)) >= 0) {
01667 rpmfileAttrs fileAttrs;
01668
01669
01670 fileAttrs = rpmfiFFlags(fi);
01671 genConfigDeps |= (fileAttrs & RPMFILE_CONFIG);
01672
01673 av[c] = xstrdup(rpmfiFN(fi));
01674 fmode[c] = rpmfiFMode(fi);
01675 }
01676 av[ac] = NULL;
01677
01678
01679 fc = rpmfcNew();
01680 fc->skipProv = !pkg->autoProv;
01681 fc->skipReq = !pkg->autoReq;
01682 fc->tracked = 0;
01683 fc->brlen = (spec->buildRootURL ? strlen(spec->buildRootURL) : 0);
01684
01685
01686 if (!fc->skipProv) {
01687 ds = rpmdsNew(pkg->header, RPMTAG_PROVIDENAME, flags);
01688 xx = rpmdsMerge(&fc->provides, ds);
01689 ds = rpmdsFree(ds);
01690 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDENAME);
01691 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEVERSION);
01692 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEFLAGS);
01693
01694
01695 if (genConfigDeps) {
01696 N = rpmdsN(pkg->ds);
01697 assert(N != NULL);
01698 EVR = rpmdsEVR(pkg->ds);
01699 assert(EVR != NULL);
01700 sprintf(buf, "config(%s)", N);
01701 ds = rpmdsSingle(RPMTAG_PROVIDENAME, buf, EVR,
01702 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
01703 xx = rpmdsMerge(&fc->provides, ds);
01704 ds = rpmdsFree(ds);
01705 }
01706 }
01707
01708 if (!fc->skipReq) {
01709 ds = rpmdsNew(pkg->header, RPMTAG_REQUIRENAME, flags);
01710 xx = rpmdsMerge(&fc->requires, ds);
01711 ds = rpmdsFree(ds);
01712 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIRENAME);
01713 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREVERSION);
01714 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREFLAGS);
01715
01716
01717 if (genConfigDeps) {
01718 N = rpmdsN(pkg->ds);
01719 assert(N != NULL);
01720 EVR = rpmdsEVR(pkg->ds);
01721 assert(EVR != NULL);
01722 sprintf(buf, "config(%s)", N);
01723 ds = rpmdsSingle(RPMTAG_REQUIRENAME, buf, EVR,
01724 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
01725 xx = rpmdsMerge(&fc->requires, ds);
01726 ds = rpmdsFree(ds);
01727 }
01728 }
01729
01730
01731 xx = rpmfcClassify(fc, av, fmode);
01732
01733
01734 xx = rpmfcApply(fc);
01735
01736
01737 p = (const void **) argiData(fc->fcolor);
01738 c = argiCount(fc->fcolor);
01739 assert(ac == c);
01740 if (p != NULL && c > 0) {
01741 int_32 * fcolors = (int_32 *)p;
01742 int i;
01743
01744
01745 for (i = 0; i < c; i++)
01746 fcolors[i] &= 0x0f;
01747 xx = headerAddEntry(pkg->header, RPMTAG_FILECOLORS, RPM_INT32_TYPE,
01748 p, c);
01749 }
01750
01751
01752 p = (const void **) argvData(fc->cdict);
01753 c = argvCount(fc->cdict);
01754 if (p != NULL && c > 0)
01755 xx = headerAddEntry(pkg->header, RPMTAG_CLASSDICT, RPM_STRING_ARRAY_TYPE,
01756 p, c);
01757
01758
01759 p = (const void **) argiData(fc->fcdictx);
01760 c = argiCount(fc->fcdictx);
01761 assert(ac == c);
01762 if (p != NULL && c > 0)
01763 xx = headerAddEntry(pkg->header, RPMTAG_FILECLASS, RPM_INT32_TYPE,
01764 p, c);
01765
01766
01767
01768 if (fc->provides != NULL && (c = rpmdsCount(fc->provides)) > 0 && !fc->skipProv) {
01769 p = (const void **) fc->provides->N;
01770 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
01771 p, c);
01772
01773
01774 p = (const void **) fc->provides->EVR;
01775 assert(p != NULL);
01776 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
01777 p, c);
01778 p = (const void **) fc->provides->Flags;
01779 assert(p != NULL);
01780 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
01781 p, c);
01782
01783 }
01784
01785
01786
01787
01788 if (fc->requires != NULL && (c = rpmdsCount(fc->requires)) > 0 && !fc->skipReq) {
01789 p = (const void **) fc->requires->N;
01790 xx = headerAddEntry(pkg->header, RPMTAG_REQUIRENAME, RPM_STRING_ARRAY_TYPE,
01791 p, c);
01792
01793
01794 p = (const void **) fc->requires->EVR;
01795 assert(p != NULL);
01796 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREVERSION, RPM_STRING_ARRAY_TYPE,
01797 p, c);
01798 p = (const void **) fc->requires->Flags;
01799 assert(p != NULL);
01800 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREFLAGS, RPM_INT32_TYPE,
01801 p, c);
01802
01803 }
01804
01805
01806
01807 p = (const void **) argiData(fc->ddictx);
01808 c = argiCount(fc->ddictx);
01809 if (p != NULL)
01810 xx = headerAddEntry(pkg->header, RPMTAG_DEPENDSDICT, RPM_INT32_TYPE,
01811 p, c);
01812
01813
01814 p = (const void **) argiData(fc->fddictx);
01815 c = argiCount(fc->fddictx);
01816 assert(ac == c);
01817 if (p != NULL)
01818 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSX, RPM_INT32_TYPE,
01819 p, c);
01820
01821 p = (const void **) argiData(fc->fddictn);
01822 c = argiCount(fc->fddictn);
01823 assert(ac == c);
01824 if (p != NULL)
01825 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSN, RPM_INT32_TYPE,
01826 p, c);
01827
01828 printDeps(pkg->header);
01829
01830 if (fc != NULL && _rpmfc_debug) {
01831 char msg[BUFSIZ];
01832 sprintf(msg, "final: files %d cdict[%d] %d%% ddictx[%d]", fc->nfiles, argvCount(fc->cdict), ((100 * fc->fknown)/fc->nfiles), argiCount(fc->ddictx));
01833 rpmfcPrint(msg, fc, NULL);
01834 }
01835
01836
01837 fmode = _free(fmode);
01838 fc = rpmfcFree(fc);
01839 av = argvFree(av);
01840
01841 return rc;
01842 }