3 #include <signal.h> /* getOutputFrom() */
9 #define _RPMDS_INTERNAL
16 #if !defined(DT_GNU_HASH)
17 #define DT_GNU_HASH 0x6ffffef5
27 int nfiles; /*!< no. of files */
28 int fknown; /*!< no. of classified files */
29 int fwhite; /*!< no. of "white" files */
30 int ix; /*!< current file index */
31 int skipProv; /*!< Don't auto-generate Provides:? */
32 int skipReq; /*!< Don't auto-generate Requires:? */
33 int tracked; /*!< Versioned Provides: tracking dependency added? */
34 size_t brlen; /*!< strlen(spec->buildRoot) */
36 ARGV_t fn; /*!< (no. files) file names */
37 ARGI_t fcolor; /*!< (no. files) file colors */
38 ARGI_t fcdictx; /*!< (no. files) file class dictionary indices */
39 ARGI_t fddictx; /*!< (no. files) file depends dictionary start */
40 ARGI_t fddictn; /*!< (no. files) file depends dictionary no. entries */
41 ARGV_t cdict; /*!< (no. classes) file class dictionary */
42 ARGV_t ddict; /*!< (no. dependencies) file depends dictionary */
43 ARGI_t ddictx; /*!< (no. dependencies) file->dependency mapping */
45 rpmds provides; /*!< (no. provides) package provides */
46 rpmds requires; /*!< (no. requires) package requires */
48 StringBuf sb_java; /*!< concatenated list of java colored files. */
49 StringBuf sb_perl; /*!< concatenated list of perl colored files. */
50 StringBuf sb_python;/*!< concatenated list of python colored files. */
57 static int rpmfcExpandAppend(ARGV_t * argvp, const ARGV_t av)
60 int argc = argvCount(argv);
61 int ac = argvCount(av);
64 argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv));
65 for (i = 0; i < ac; i++)
66 argv[argc + i] = rpmExpand(av[i], NULL);
67 argv[argc + ac] = NULL;
73 * Return output from helper script.
74 * @todo Use poll(2) rather than select(2), if available.
75 * @param dir directory to run in (or NULL)
76 * @param argv program and arguments to run
77 * @param writePtr bytes to feed to script on stdin (or NULL)
78 * @param writeBytesLeft no. of bytes to feed to script on stdin
79 * @param failNonZero is script failure an error?
80 * @return buffered stdout from script, NULL on error
82 static StringBuf getOutputFrom(const char * dir, ARGV_t argv,
83 const char * writePtr, int writeBytesLeft,
95 oldhandler = signal(SIGPIPE, SIG_IGN);
97 toProg[0] = toProg[1] = 0;
99 fromProg[0] = fromProg[1] = 0;
100 (void) pipe(fromProg);
102 if (!(child = fork())) {
103 (void) close(toProg[1]);
104 (void) close(fromProg[0]);
106 (void) dup2(toProg[0], STDIN_FILENO); /* Make stdin the in pipe */
107 (void) dup2(fromProg[1], STDOUT_FILENO); /* Make stdout the out pipe */
109 (void) close(toProg[0]);
110 (void) close(fromProg[1]);
116 rpmlog(RPMLOG_DEBUG, _("\texecv(%s) pid %d\n"),
117 argv[0], (unsigned)getpid());
119 unsetenv("MALLOC_CHECK_");
120 (void) execvp(argv[0], (char *const *)argv);
121 /* XXX this error message is probably not seen. */
122 rpmlog(RPMERR_EXEC, _("Couldn't exec %s: %s\n"),
123 argv[0], strerror(errno));
127 rpmlog(RPMERR_FORK, _("Couldn't fork %s: %s\n"),
128 argv[0], strerror(errno));
132 (void) close(toProg[0]);
133 (void) close(fromProg[1]);
135 /* Do not block reading or writing from/to prog. */
136 (void) fcntl(fromProg[0], F_SETFL, O_NONBLOCK);
137 (void) fcntl(toProg[1], F_SETFL, O_NONBLOCK);
139 readBuff = newStringBuf();
151 if (fromProg[0] >= 0) {
152 FD_SET(fromProg[0], &ibits);
154 if (toProg[1] >= 0) {
155 FD_SET(toProg[1], &obits);
157 /* XXX values set to limit spinning with perl doing ~100 forks/sec. */
160 nfd = ((fromProg[0] > toProg[1]) ? fromProg[0] : toProg[1]);
161 if ((rc = select(nfd, &ibits, &obits, NULL, &tv)) < 0) {
167 /* Write any data to program */
168 if (toProg[1] >= 0 && FD_ISSET(toProg[1], &obits)) {
169 if (writePtr && writeBytesLeft > 0) {
170 if ((nbw = write(toProg[1], writePtr,
171 (1024<writeBytesLeft) ? 1024 : writeBytesLeft)) < 0) {
172 if (errno != EAGAIN) {
173 perror("getOutputFrom()");
178 writeBytesLeft -= nbw;
180 } else if (toProg[1] >= 0) { /* close write fd */
181 (void) close(toProg[1]);
186 /* Read any data from prog */
187 { char buf[BUFSIZ+1];
188 while ((nbr = read(fromProg[0], buf, sizeof(buf)-1)) > 0) {
190 appendStringBuf(readBuff, buf);
194 /* terminate on (non-blocking) EOF or error */
195 done = (nbr == 0 || (nbr < 0 && errno != EAGAIN));
201 (void) close(toProg[1]);
202 if (fromProg[0] >= 0)
203 (void) close(fromProg[0]);
205 (void) signal(SIGPIPE, oldhandler);
207 /* Collect status from prog */
208 reaped = waitpid(child, &status, 0);
209 rpmlog(RPMLOG_DEBUG, _("\twaitpid(%d) rc %d status %x\n"),
210 (unsigned)child, (unsigned)reaped, status);
212 if (failNonZero && (!WIFEXITED(status) || WEXITSTATUS(status))) {
213 rpmlog(RPMERR_EXEC, _("%s failed\n"), argv[0]);
216 if (writeBytesLeft) {
217 rpmlog(RPMERR_EXEC, _("failed to write all data to %s\n"), argv[0]);
223 int rpmfcExec(ARGV_t av, StringBuf sb_stdin, StringBuf * sb_stdoutp,
226 const char * s = NULL;
232 const char * buf_stdin = NULL;
233 int buf_stdin_len = 0;
241 /* Find path to executable with (possible) args. */
242 s = rpmExpand(av[0], NULL);
246 /* Parse args buried within expanded exacutable. */
248 xx = poptParseArgvString(s, &pac, (const char ***)&pav);
249 if (!(xx == 0 && pac > 0 && pav != NULL))
252 /* Build argv, appending args to the executable args. */
254 xx = argvAppend(&xav, pav);
256 xx = rpmfcExpandAppend(&xav, av + 1);
258 if (sb_stdin != NULL) {
259 buf_stdin = getStringBuf(sb_stdin);
260 buf_stdin_len = strlen(buf_stdin);
263 /* Read output from exec'd helper. */
264 sb = getOutputFrom(NULL, xav, buf_stdin, buf_stdin_len, failnonzero);
266 if (sb_stdoutp != NULL) {
268 sb = NULL; /* XXX don't free */
274 sb = freeStringBuf(sb);
276 pav = _free(pav); /* XXX popt mallocs in single blob. */
283 static int rpmfcSaveArg(ARGV_t * argvp, const char * key)
287 if (argvSearch(*argvp, key, NULL) == NULL) {
288 rc = argvAdd(argvp, key);
289 rc = argvSort(*argvp, NULL);
294 static char * rpmfcFileDep(char * buf, int ix,
297 int_32 tagN = rpmdsTagN(ds);
302 case RPMTAG_PROVIDENAME:
305 case RPMTAG_REQUIRENAME:
310 sprintf(buf, "%08d%c %s %s 0x%08x", ix, deptype,
311 rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
316 * Run per-interpreter dependency helper.
317 * @param fc file classifier
318 * @param deptype 'P' == Provides:, 'R' == Requires:, helper
319 * @param nsdep class name for interpreter (e.g. "perl")
320 * @return 0 on success
322 static int rpmfcHelper(rpmfc fc, unsigned char deptype, const char * nsdep)
324 const char * fn = fc->fn[fc->ix];
326 StringBuf sb_stdout = NULL;
332 int_32 Flags, dsContext, tagN;
346 xx = snprintf(buf, sizeof(buf), "%%{?__%s_provides}", nsdep);
347 depsp = &fc->provides;
348 dsContext = RPMSENSE_FIND_PROVIDES;
349 tagN = RPMTAG_PROVIDENAME;
354 xx = snprintf(buf, sizeof(buf), "%%{?__%s_requires}", nsdep);
355 depsp = &fc->requires;
356 dsContext = RPMSENSE_FIND_REQUIRES;
357 tagN = RPMTAG_REQUIRENAME;
360 buf[sizeof(buf)-1] = '\0';
364 sb_stdin = newStringBuf();
365 appendLineStringBuf(sb_stdin, fn);
367 xx = rpmfcExec(av, sb_stdin, &sb_stdout, 0);
368 sb_stdin = freeStringBuf(sb_stdin);
370 if (xx == 0 && sb_stdout != NULL) {
372 xx = argvSplit(&pav, getStringBuf(sb_stdout), " \t\n\r");
373 pac = argvCount(pav);
375 for (i = 0; i < pac; i++) {
379 if (pav[i+1] && strchr("=<>", *pav[i+1])) {
381 for (s = pav[i]; *s; s++) {
387 Flags |= RPMSENSE_EQUAL;
390 Flags |= RPMSENSE_LESS;
393 Flags |= RPMSENSE_GREATER;
403 /* Add tracking dependency for versioned Provides: */
404 if (!fc->tracked && deptype == 'P' && *EVR != '\0') {
405 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
406 "rpmlib(VersionedDependencies)", "3.0.3-1",
407 RPMSENSE_RPMLIB|(RPMSENSE_LESS|RPMSENSE_EQUAL));
408 xx = rpmdsMerge(&fc->requires, ds);
413 ds = rpmdsSingle(tagN, N, EVR, Flags);
415 /* Add to package dependencies. */
416 xx = rpmdsMerge(depsp, ds);
418 /* Add to file dependencies. */
419 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(buf, fc->ix, ds));
426 sb_stdout = freeStringBuf(sb_stdout);
433 static struct rpmfcTokens_s rpmfcTokens[] = {
434 { "directory", RPMFC_DIRECTORY|RPMFC_INCLUDE },
436 { " shared object", RPMFC_LIBRARY },
437 { " executable", RPMFC_EXECUTABLE },
438 { " statically linked", RPMFC_STATIC },
439 { " not stripped", RPMFC_NOTSTRIPPED },
440 { " archive", RPMFC_ARCHIVE },
442 { "ELF 32-bit", RPMFC_ELF32|RPMFC_INCLUDE },
443 { "ELF 64-bit", RPMFC_ELF64|RPMFC_INCLUDE },
445 { " script", RPMFC_SCRIPT },
446 { " text", RPMFC_TEXT },
447 { " document", RPMFC_DOCUMENT },
449 { " compressed", RPMFC_COMPRESSED },
451 { "troff or preprocessor input", RPMFC_MANPAGE|RPMFC_INCLUDE },
452 { "GNU Info", RPMFC_MANPAGE|RPMFC_INCLUDE },
454 { "perl script text", RPMFC_PERL|RPMFC_INCLUDE },
455 { "Perl5 module source text", RPMFC_PERL|RPMFC_MODULE|RPMFC_INCLUDE },
457 { " /usr/bin/python", RPMFC_PYTHON|RPMFC_INCLUDE },
459 /* XXX "a /usr/bin/python -t script text executable" */
460 /* XXX "python 2.3 byte-compiled" */
461 { "python ", RPMFC_PYTHON|RPMFC_INCLUDE },
463 { "libtool library ", RPMFC_LIBTOOL|RPMFC_INCLUDE },
464 { "pkgconfig ", RPMFC_PKGCONFIG|RPMFC_INCLUDE },
466 /* XXX .NET executables and libraries. file(1) cannot differ from win32
467 * executables unfortunately :( */
468 { "PE executable", RPMFC_MONO|RPMFC_INCLUDE },
470 { "current ar archive", RPMFC_STATIC|RPMFC_LIBRARY|RPMFC_ARCHIVE|RPMFC_INCLUDE },
472 { "Zip archive data", RPMFC_COMPRESSED|RPMFC_ARCHIVE|RPMFC_INCLUDE },
473 { "tar archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
474 { "cpio archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
475 { "RPM v3", RPMFC_ARCHIVE|RPMFC_INCLUDE },
476 { "RPM v4", RPMFC_ARCHIVE|RPMFC_INCLUDE },
478 { " image", RPMFC_IMAGE|RPMFC_INCLUDE },
479 { " font", RPMFC_FONT|RPMFC_INCLUDE },
480 { " Font", RPMFC_FONT|RPMFC_INCLUDE },
482 { " commands", RPMFC_SCRIPT|RPMFC_INCLUDE },
483 { " script", RPMFC_SCRIPT|RPMFC_INCLUDE },
485 { "empty", RPMFC_WHITE|RPMFC_INCLUDE },
487 { "HTML", RPMFC_WHITE|RPMFC_INCLUDE },
488 { "SGML", RPMFC_WHITE|RPMFC_INCLUDE },
489 { "XML", RPMFC_WHITE|RPMFC_INCLUDE },
491 { " program text", RPMFC_WHITE|RPMFC_INCLUDE },
492 { " source", RPMFC_WHITE|RPMFC_INCLUDE },
493 { "GLS_BINARY_LSB_FIRST", RPMFC_WHITE|RPMFC_INCLUDE },
494 { " DB ", RPMFC_WHITE|RPMFC_INCLUDE },
496 { "ASCII English text", RPMFC_WHITE|RPMFC_INCLUDE },
497 { "ASCII text", RPMFC_WHITE|RPMFC_INCLUDE },
498 { "ISO-8859 text", RPMFC_WHITE|RPMFC_INCLUDE },
500 { "symbolic link to", RPMFC_SYMLINK },
501 { "socket", RPMFC_DEVICE },
502 { "special", RPMFC_DEVICE },
504 { "ASCII", RPMFC_WHITE },
505 { "ISO-8859", RPMFC_WHITE },
507 { "data", RPMFC_WHITE },
509 { "application", RPMFC_WHITE },
510 { "boot", RPMFC_WHITE },
511 { "catalog", RPMFC_WHITE },
512 { "code", RPMFC_WHITE },
513 { "file", RPMFC_WHITE },
514 { "format", RPMFC_WHITE },
515 { "message", RPMFC_WHITE },
516 { "program", RPMFC_WHITE },
518 { "broken symbolic link to ", RPMFC_WHITE|RPMFC_ERROR },
519 { "can't read", RPMFC_WHITE|RPMFC_ERROR },
520 { "can't stat", RPMFC_WHITE|RPMFC_ERROR },
521 { "executable, can't read", RPMFC_WHITE|RPMFC_ERROR },
522 { "core file", RPMFC_WHITE|RPMFC_ERROR },
524 { NULL, RPMFC_BLACK }
527 int rpmfcColoring(const char * fmstr)
530 int fcolor = RPMFC_BLACK;
532 for (fct = rpmfcTokens; fct->token != NULL; fct++) {
533 if (strstr(fmstr, fct->token) == NULL)
535 fcolor |= fct->colors;
536 if (fcolor & RPMFC_INCLUDE)
542 void rpmfcPrint(const char * msg, rpmfc fc, FILE * fp)
553 if (fp == NULL) fp = stderr;
556 fprintf(fp, "===================================== %s\n", msg);
558 nprovides = rpmdsCount(fc->provides);
559 nrequires = rpmdsCount(fc->requires);
562 for (fx = 0; fx < fc->nfiles; fx++) {
563 assert(fx < fc->fcdictx->nvals);
564 cx = fc->fcdictx->vals[fx];
565 assert(fx < fc->fcolor->nvals);
566 fcolor = fc->fcolor->vals[fx];
568 fprintf(fp, "%3d %s", fx, fc->fn[fx]);
569 if (fcolor != RPMFC_BLACK)
570 fprintf(fp, "\t0x%x", fc->fcolor->vals[fx]);
572 fprintf(fp, "\t%s", fc->cdict[cx]);
575 if (fc->fddictx == NULL || fc->fddictn == NULL)
578 assert(fx < fc->fddictx->nvals);
579 dx = fc->fddictx->vals[fx];
580 assert(fx < fc->fddictn->nvals);
581 ndx = fc->fddictn->vals[fx];
585 unsigned char deptype;
588 ix = fc->ddictx->vals[dx++];
589 deptype = ((ix >> 24) & 0xff);
594 assert(depval != NULL);
598 assert(ix < nprovides);
599 (void) rpmdsSetIx(fc->provides, ix-1);
600 if (rpmdsNext(fc->provides) >= 0)
601 depval = rpmdsDNEVR(fc->provides);
606 assert(ix < nrequires);
607 (void) rpmdsSetIx(fc->requires, ix-1);
608 if (rpmdsNext(fc->requires) >= 0)
609 depval = rpmdsDNEVR(fc->requires);
614 fprintf(fp, "\t%s\n", depval);
619 rpmfc rpmfcFree(rpmfc fc)
622 fc->fn = argvFree(fc->fn);
623 fc->fcolor = argiFree(fc->fcolor);
624 fc->fcdictx = argiFree(fc->fcdictx);
625 fc->fddictx = argiFree(fc->fddictx);
626 fc->fddictn = argiFree(fc->fddictn);
627 fc->cdict = argvFree(fc->cdict);
628 fc->ddict = argvFree(fc->ddict);
629 fc->ddictx = argiFree(fc->ddictx);
631 fc->provides = rpmdsFree(fc->provides);
632 fc->requires = rpmdsFree(fc->requires);
634 fc->sb_java = freeStringBuf(fc->sb_java);
635 fc->sb_perl = freeStringBuf(fc->sb_perl);
636 fc->sb_python = freeStringBuf(fc->sb_python);
645 rpmfc fc = xcalloc(1, sizeof(*fc));
649 rpmds rpmfcProvides(rpmfc fc)
651 return (fc != NULL ? fc->provides : NULL);
654 rpmds rpmfcRequires(rpmfc fc)
656 return (fc != NULL ? fc->requires : NULL);
661 * Extract script dependencies.
662 * @param fc file classifier
663 * @return 0 on success
665 static int rpmfcSCRIPT(rpmfc fc)
667 const char * fn = fc->fn[fc->ix];
674 struct stat sb, * st = &sb;
678 /* Only executable scripts are searched. */
679 if (stat(fn, st) < 0)
681 is_executable = (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH));
684 if (fp == NULL || ferror(fp)) {
685 if (fp) (void) fclose(fp);
689 /* Look for #! interpreter in first 10 lines. */
690 for (i = 0; i < 10; i++) {
692 s = fgets(buf, sizeof(buf) - 1, fp);
693 if (s == NULL || ferror(fp) || feof(fp))
695 s[sizeof(buf)-1] = '\0';
696 if (!(s[0] == '#' && s[1] == '!'))
700 while (*s && strchr(" \t\n\r", *s) != NULL)
707 for (se = s+1; *se; se++) {
708 if (strchr(" \t\n\r", *se) != NULL)
715 /* Add to package requires. */
716 ds = rpmdsSingle(RPMTAG_REQUIRENAME, s, "", RPMSENSE_FIND_REQUIRES);
717 xx = rpmdsMerge(&fc->requires, ds);
719 /* Add to file requires. */
720 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(se, fc->ix, ds));
725 /* Set color based on interpreter name. */
727 if (!strcmp(bn, "perl"))
728 fc->fcolor->vals[fc->ix] |= RPMFC_PERL;
729 else if (!strncmp(bn, "python", sizeof("python")-1))
730 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
737 if (fc->fcolor->vals[fc->ix] & RPMFC_PERL) {
738 if (fc->fcolor->vals[fc->ix] & RPMFC_MODULE)
739 xx = rpmfcHelper(fc, 'P', "perl");
740 if (is_executable || (fc->fcolor->vals[fc->ix] & RPMFC_MODULE))
741 xx = rpmfcHelper(fc, 'R', "perl");
743 if (fc->fcolor->vals[fc->ix] & RPMFC_PYTHON) {
744 xx = rpmfcHelper(fc, 'P', "python");
748 xx = rpmfcHelper(fc, 'R', "python");
750 if (fc->fcolor->vals[fc->ix] & RPMFC_LIBTOOL) {
751 xx = rpmfcHelper(fc, 'P', "libtool");
755 xx = rpmfcHelper(fc, 'R', "libtool");
757 if (fc->fcolor->vals[fc->ix] & RPMFC_PKGCONFIG) {
758 xx = rpmfcHelper(fc, 'P', "pkgconfig");
762 xx = rpmfcHelper(fc, 'R', "pkgconfig");
764 if (fc->fcolor->vals[fc->ix] & RPMFC_MONO) {
765 xx = rpmfcHelper(fc, 'P', "mono");
767 xx = rpmfcHelper(fc, 'R', "mono");
774 * Extract Elf dependencies.
775 * @param fc file classifier
776 * @return 0 on success
778 static int rpmfcELF(rpmfc fc)
780 #if HAVE_GELF_H && HAVE_LIBELF
781 const char * fn = fc->fn[fc->ix];
785 GElf_Ehdr ehdr_mem, * ehdr;
786 GElf_Shdr shdr_mem, * shdr;
787 GElf_Verdef def_mem, * def;
788 GElf_Verneed need_mem, * need;
789 GElf_Dyn dyn_mem, * dyn;
790 unsigned int auxoffset;
797 struct stat sb, * st = &sb;
798 const char * soname = NULL;
800 int_32 tagN, dsContext;
809 static int filter_GLIBC_PRIVATE = 0;
810 static int oneshot = 0;
814 filter_GLIBC_PRIVATE = rpmExpandNumeric("%{?_filter_GLIBC_PRIVATE}");
817 /* Files with executable bit set only. */
818 if (stat(fn, st) != 0)
821 fdno = open(fn, O_RDONLY);
825 (void) elf_version(EV_CURRENT);
828 if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL
829 || elf_kind(elf) != ELF_K_ELF
830 || (ehdr = gelf_getehdr(elf, &ehdr_mem)) == NULL
831 || !(ehdr->e_type == ET_DYN || ehdr->e_type == ET_EXEC))
834 isElf64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
835 isDSO = ehdr->e_type == ET_DYN;
838 while ((scn = elf_nextscn(elf, scn)) != NULL) {
839 shdr = gelf_getshdr(scn, &shdr_mem);
843 soname = _free(soname);
844 switch (shdr->sh_type) {
851 while ((data = elf_getdata (scn, data)) != NULL) {
853 for (cnt = shdr->sh_info; --cnt >= 0; ) {
855 def = gelf_getverdef (data, offset, &def_mem);
858 auxoffset = offset + def->vd_aux;
859 for (cnt2 = def->vd_cnt; --cnt2 >= 0; ) {
860 GElf_Verdaux aux_mem, * aux;
862 aux = gelf_getverdaux (data, auxoffset, &aux_mem);
866 s = elf_strptr(elf, shdr->sh_link, aux->vda_name);
869 if (def->vd_flags & VER_FLG_BASE) {
870 soname = _free(soname);
872 auxoffset += aux->vda_next;
876 && !(filter_GLIBC_PRIVATE != 0
877 && !strcmp(s, "GLIBC_PRIVATE")))
881 t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
883 #if !defined(__alpha__)
885 t = stpcpy(t, "(64bit)");
889 /* Add to package provides. */
890 ds = rpmdsSingle(RPMTAG_PROVIDES,
891 buf, "", RPMSENSE_FIND_PROVIDES);
892 xx = rpmdsMerge(&fc->provides, ds);
894 /* Add to file dependencies. */
895 xx = rpmfcSaveArg(&fc->ddict,
896 rpmfcFileDep(t, fc->ix, ds));
900 auxoffset += aux->vda_next;
902 offset += def->vd_next;
906 case SHT_GNU_verneed:
908 /* Files with executable bit set only. */
909 if (!fc->skipReq && (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
910 while ((data = elf_getdata (scn, data)) != NULL) {
912 for (cnt = shdr->sh_info; --cnt >= 0; ) {
913 need = gelf_getverneed (data, offset, &need_mem);
917 s = elf_strptr(elf, shdr->sh_link, need->vn_file);
920 soname = _free(soname);
922 auxoffset = offset + need->vn_aux;
923 for (cnt2 = need->vn_cnt; --cnt2 >= 0; ) {
924 GElf_Vernaux aux_mem, * aux;
926 aux = gelf_getvernaux (data, auxoffset, &aux_mem);
930 s = elf_strptr(elf, shdr->sh_link, aux->vna_name);
934 /* Filter dependencies that contain GLIBC_PRIVATE */
936 && !(filter_GLIBC_PRIVATE != 0
937 && !strcmp(s, "GLIBC_PRIVATE")))
941 t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
943 #if !defined(__alpha__)
945 t = stpcpy(t, "(64bit)");
949 /* Add to package dependencies. */
950 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
951 buf, "", RPMSENSE_FIND_REQUIRES);
952 xx = rpmdsMerge(&fc->requires, ds);
954 /* Add to file dependencies. */
955 xx = rpmfcSaveArg(&fc->ddict,
956 rpmfcFileDep(t, fc->ix, ds));
959 auxoffset += aux->vna_next;
961 offset += need->vn_next;
967 while ((data = elf_getdata (scn, data)) != NULL) {
968 for (cnt = 0; cnt < (shdr->sh_size / shdr->sh_entsize); ++cnt) {
969 dyn = gelf_getdyn (data, cnt, &dyn_mem);
973 switch (dyn->d_tag) {
987 /* Files with executable bit set only. */
988 if (fc->skipReq || !(st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
990 /* Add to package requires. */
991 depsp = &fc->requires;
992 tagN = RPMTAG_REQUIRENAME;
993 dsContext = RPMSENSE_FIND_REQUIRES;
994 s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
999 /* Add to package provides. */
1002 depsp = &fc->provides;
1003 tagN = RPMTAG_PROVIDENAME;
1004 dsContext = RPMSENSE_FIND_PROVIDES;
1005 s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
1016 #if !defined(__alpha__)
1018 t = stpcpy(t, "()(64bit)");
1022 /* Add to package dependencies. */
1023 ds = rpmdsSingle(tagN, buf, "", dsContext);
1024 xx = rpmdsMerge(depsp, ds);
1026 /* Add to file dependencies. */
1027 xx = rpmfcSaveArg(&fc->ddict,
1028 rpmfcFileDep(t, fc->ix, ds));
1037 /* For DSOs which use the .gnu_hash section and don't have a .hash
1038 * section, we need to ensure that we have a new enough glibc. */
1039 if (gotGNUHASH && !gotHASH) {
1040 ds = rpmdsSingle(RPMTAG_REQUIRENAME, "rtld(GNU_HASH)", "",
1041 RPMSENSE_FIND_REQUIRES);
1042 rpmdsMerge(&fc->requires, ds);
1045 rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
1049 /* For DSO's, provide the basename of the file if DT_SONAME not found. */
1050 if (!fc->skipProv && isDSO && !gotDEBUG && !gotSONAME) {
1051 depsp = &fc->provides;
1052 tagN = RPMTAG_PROVIDENAME;
1053 dsContext = RPMSENSE_FIND_PROVIDES;
1055 s = strrchr(fn, '/');
1063 /* LCL: s is not null. */
1066 #if !defined(__alpha__)
1068 t = stpcpy(t, "()(64bit)");
1072 /* Add to package dependencies. */
1073 ds = rpmdsSingle(tagN, buf, "", dsContext);
1074 xx = rpmdsMerge(depsp, ds);
1076 /* Add to file dependencies. */
1077 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
1083 soname = _free(soname);
1084 if (elf) (void) elf_end(elf);
1092 typedef struct rpmfcApplyTbl_s {
1093 int (*func) (rpmfc fc);
1099 static struct rpmfcApplyTbl_s rpmfcApplyTable[] = {
1100 { rpmfcELF, RPMFC_ELF },
1101 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PERL) },
1102 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PYTHON) },
1103 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PKGCONFIG) },
1104 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_LIBTOOL) },
1105 { rpmfcSCRIPT, RPMFC_MONO },
1109 int rpmfcApply(rpmfc fc)
1118 unsigned char deptype;
1127 /* Generate package and per-file dependencies. */
1128 for (fc->ix = 0; fc->fn[fc->ix] != NULL; fc->ix++) {
1130 /* XXX Insure that /usr/lib{,64}/python files are marked RPMFC_PYTHON */
1131 /* XXX HACK: classification by path is intrinsically stupid. */
1132 { const char *fn = strstr(fc->fn[fc->ix], "/usr/lib");
1134 fn += sizeof("/usr/lib")-1;
1135 if (fn[0] == '6' && fn[1] == '4')
1137 if (!strncmp(fn, "/python", sizeof("/python")-1))
1138 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
1142 if (fc->fcolor->vals[fc->ix])
1143 for (fcat = rpmfcApplyTable; fcat->func != NULL; fcat++) {
1144 if (!(fc->fcolor->vals[fc->ix] & fcat->colormask))
1146 xx = (*fcat->func) (fc);
1150 /* Generate per-file indices into package dependencies. */
1151 nddict = argvCount(fc->ddict);
1153 for (i = 0; i < nddict; i++) {
1156 /* Parse out (file#,deptype,N,EVR,Flags) */
1157 ix = strtol(s, &se, 10);
1162 while (*se && *se != ' ')
1166 while (*se && *se != ' ')
1169 Flags = strtol(se, NULL, 16);
1176 ds = rpmdsSingle(RPMTAG_PROVIDENAME, N, EVR, Flags);
1177 dix = rpmdsFind(fc->provides, ds);
1181 ds = rpmdsSingle(RPMTAG_REQUIRENAME, N, EVR, Flags);
1182 dix = rpmdsFind(fc->requires, ds);
1187 /* XXX assertion incorrect while generating -debuginfo deps. */
1195 val = (deptype << 24) | (dix & 0x00ffffff);
1196 xx = argiAdd(&fc->ddictx, -1, val);
1200 xx = argiAdd(&fc->fddictx, ix, argiCount(fc->ddictx)-1);
1202 if (fc->fddictn && fc->fddictn->vals)
1203 fc->fddictn->vals[ix]++;
1209 int rpmfcClassify(rpmfc fc, ARGV_t argv, int_16 * fmode)
1213 const char * s, * se;
1217 int msflags = MAGIC_CHECK; /* XXX MAGIC_COMPRESS flag? */
1220 if (fc == NULL || argv == NULL)
1223 fc->nfiles = argvCount(argv);
1225 /* Initialize the per-file dictionary indices. */
1226 xx = argiAdd(&fc->fddictx, fc->nfiles-1, 0);
1227 xx = argiAdd(&fc->fddictn, fc->nfiles-1, 0);
1229 /* Build (sorted) file class dictionary. */
1230 xx = argvAdd(&fc->cdict, "");
1231 xx = argvAdd(&fc->cdict, "directory");
1233 ms = magic_open(msflags);
1236 rpmlog(xx, _("magic_open(0x%x) failed: %s\n"),
1237 msflags, strerror(errno));
1238 assert(ms != NULL); /* XXX figger a proper return path. */
1241 xx = magic_load(ms, NULL);
1244 rpmlog(xx, _("magic_load failed: %s\n"), magic_error(ms));
1245 assert(xx != -1); /* XXX figger a proper return path. */
1248 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
1250 int_16 mode = (fmode ? fmode[fc->ix] : 0);
1256 switch (mode & S_IFMT) {
1257 case S_IFCHR: ftype = "character special"; break;
1258 case S_IFBLK: ftype = "block special"; break;
1259 case S_IFIFO: ftype = "fifo (named pipe)"; break;
1260 case S_IFSOCK: ftype = "socket"; break;
1265 #define _suffix(_s, _x) \
1266 (slen >= sizeof(_x) && !strcmp((_s)+slen-(sizeof(_x)-1), (_x)))
1268 /* XXX all files with extension ".pm" are perl modules for now. */
1269 if (_suffix(s, ".pm"))
1270 ftype = "Perl5 module source text";
1272 /* XXX all files with extension ".la" are libtool for now. */
1273 else if (_suffix(s, ".la"))
1274 ftype = "libtool library file";
1276 /* XXX all files with extension ".pc" are pkgconfig for now. */
1277 else if (_suffix(s, ".pc"))
1278 ftype = "pkgconfig file";
1280 /* XXX skip all files in /dev/ which are (or should be) %dev dummies. */
1281 else if (slen >= fc->brlen+sizeof("/dev/") && !strncmp(s+fc->brlen, "/dev/", sizeof("/dev/")-1))
1284 ftype = magic_file(ms, s);
1286 if (ftype == NULL) {
1288 rpmlog(xx, _("magic_file(ms, \"%s\") failed: mode %06o %s\n"),
1289 s, mode, magic_error(ms));
1290 assert(ftype != NULL); /* XXX figger a proper return path. */
1295 rpmlog(RPMLOG_DEBUG, "%s: %s\n", s, se);
1297 /* Save the path. */
1298 xx = argvAdd(&fc->fn, s);
1300 /* Save the file type string. */
1301 xx = argvAdd(&fcav, se);
1303 /* Add (filtered) entry to sorted class dictionary. */
1304 fcolor = rpmfcColoring(se);
1305 xx = argiAdd(&fc->fcolor, fc->ix, fcolor);
1307 if (fcolor != RPMFC_WHITE && (fcolor & RPMFC_INCLUDE))
1308 xx = rpmfcSaveArg(&fc->cdict, se);
1311 /* Build per-file class index array. */
1313 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
1317 dav = argvSearch(fc->cdict, se, NULL);
1319 xx = argiAdd(&fc->fcdictx, fc->ix, (dav - fc->cdict));
1322 xx = argiAdd(&fc->fcdictx, fc->ix, 0);
1327 fcav = argvFree(fcav);
1337 typedef struct DepMsg_s * DepMsg_t;
1343 const char * argv[4];
1353 static struct DepMsg_s depMsgs[] = {
1354 { "Provides", { "%{?__find_provides}", NULL, NULL, NULL },
1355 RPMTAG_PROVIDENAME, RPMTAG_PROVIDEVERSION, RPMTAG_PROVIDEFLAGS,
1358 { "PreReq", { NULL, NULL, NULL, NULL },
1359 RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
1360 RPMSENSE_PREREQ, 0 },
1361 { "Requires(interp)", { NULL, "interp", NULL, NULL },
1362 -1, -1, RPMTAG_REQUIREFLAGS,
1363 _notpre(RPMSENSE_INTERP), 0 },
1365 { "Requires(interp)", { NULL, "interp", NULL, NULL },
1366 RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
1367 _notpre(RPMSENSE_INTERP), 0 },
1369 { "Requires(rpmlib)", { NULL, "rpmlib", NULL, NULL },
1370 -1, -1, RPMTAG_REQUIREFLAGS,
1371 _notpre(RPMSENSE_RPMLIB), 0 },
1372 { "Requires(verify)", { NULL, "verify", NULL, NULL },
1373 -1, -1, RPMTAG_REQUIREFLAGS,
1374 RPMSENSE_SCRIPT_VERIFY, 0 },
1375 { "Requires(pre)", { NULL, "pre", NULL, NULL },
1376 -1, -1, RPMTAG_REQUIREFLAGS,
1377 _notpre(RPMSENSE_SCRIPT_PRE), 0 },
1378 { "Requires(post)", { NULL, "post", NULL, NULL },
1379 -1, -1, RPMTAG_REQUIREFLAGS,
1380 _notpre(RPMSENSE_SCRIPT_POST), 0 },
1381 { "Requires(preun)", { NULL, "preun", NULL, NULL },
1382 -1, -1, RPMTAG_REQUIREFLAGS,
1383 _notpre(RPMSENSE_SCRIPT_PREUN), 0 },
1384 { "Requires(postun)", { NULL, "postun", NULL, NULL },
1385 -1, -1, RPMTAG_REQUIREFLAGS,
1386 _notpre(RPMSENSE_SCRIPT_POSTUN), 0 },
1387 { "Requires", { "%{?__find_requires}", NULL, NULL, NULL },
1388 -1, -1, RPMTAG_REQUIREFLAGS, /* XXX inherit name/version arrays */
1389 RPMSENSE_PREREQ, RPMSENSE_PREREQ },
1390 { "Conflicts", { "%{?__find_conflicts}", NULL, NULL, NULL },
1391 RPMTAG_CONFLICTNAME, RPMTAG_CONFLICTVERSION, RPMTAG_CONFLICTFLAGS,
1393 { "Obsoletes", { "%{?__find_obsoletes}", NULL, NULL, NULL },
1394 RPMTAG_OBSOLETENAME, RPMTAG_OBSOLETEVERSION, RPMTAG_OBSOLETEFLAGS,
1396 { NULL, { NULL, NULL, NULL, NULL }, 0, 0, 0, 0, 0 }
1399 static DepMsg_t DepMsgs = depMsgs;
1403 static void printDeps(Header h)
1407 int flags = 0; /* XXX !scareMem */
1412 for (dm = DepMsgs; dm->msg != NULL; dm++) {
1413 if (dm->ntag != -1) {
1415 ds = rpmdsNew(h, dm->ntag, flags);
1422 continue; /* XXX can't happen */
1425 while (rpmdsNext(ds) >= 0) {
1427 Flags = rpmdsFlags(ds);
1429 if (!((Flags & dm->mask) ^ dm->xor))
1432 rpmlog(RPMLOG_NOTICE, "%s:", (dm->msg ? dm->msg : ""));
1435 if ((DNEVR = rpmdsDNEVR(ds)) == NULL)
1436 continue; /* XXX can't happen */
1437 rpmlog(RPMLOG_NOTICE, " %s", DNEVR+2);
1440 rpmlog(RPMLOG_NOTICE, "\n");
1447 static int rpmfcGenerateDependsHelper(const rpmSpec spec, Package pkg, rpmfi fi)
1450 StringBuf sb_stdout;
1452 int failnonzero = 0;
1456 * Create file manifest buffer to deliver to dependency finder.
1458 sb_stdin = newStringBuf();
1459 fi = rpmfiInit(fi, 0);
1461 while (rpmfiNext(fi) >= 0)
1462 appendLineStringBuf(sb_stdin, rpmfiFN(fi));
1464 for (dm = DepMsgs; dm->msg != NULL; dm++) {
1469 tag = (dm->ftag > 0) ? dm->ftag : dm->ntag;
1474 case RPMTAG_PROVIDEFLAGS:
1478 tagflags = RPMSENSE_FIND_PROVIDES;
1480 case RPMTAG_REQUIREFLAGS:
1484 tagflags = RPMSENSE_FIND_REQUIRES;
1491 xx = rpmfcExec(dm->argv, sb_stdin, &sb_stdout, failnonzero);
1495 s = rpmExpand(dm->argv[0], NULL);
1496 rpmlog(RPMLOG_NOTICE, _("Finding %s: %s\n"), dm->msg,
1500 if (sb_stdout == NULL) {
1502 rpmlog(rc, _("Failed to find %s:\n"), dm->msg);
1506 /* Parse dependencies into header */
1507 rc = parseRCPOT(spec, pkg, getStringBuf(sb_stdout), tag, 0, tagflags);
1508 sb_stdout = freeStringBuf(sb_stdout);
1511 rpmlog(rc, _("Failed to find %s:\n"), dm->msg);
1516 sb_stdin = freeStringBuf(sb_stdin);
1521 int rpmfcGenerateDepends(const rpmSpec spec, Package pkg)
1523 rpmfi fi = pkg->cpioList;
1526 int flags = 0; /* XXX !scareMem */
1529 int ac = rpmfiFC(fi);
1539 /* Skip packages with no files. */
1543 /* Skip packages that have dependency generation disabled. */
1544 if (! (pkg->autoReq || pkg->autoProv))
1547 /* If new-fangled dependency generation is disabled ... */
1548 if (!rpmExpandNumeric("%{?_use_internal_dependency_generator}")) {
1549 /* ... then generate dependencies using %{__find_requires} et al. */
1550 rc = rpmfcGenerateDependsHelper(spec, pkg, fi);
1551 printDeps(pkg->header);
1555 /* Extract absolute file paths in argv format. */
1556 av = xcalloc(ac+1, sizeof(*av));
1557 fmode = xcalloc(ac+1, sizeof(*fmode));
1560 fi = rpmfiInit(fi, 0);
1562 while ((c = rpmfiNext(fi)) >= 0) {
1563 rpmfileAttrs fileAttrs;
1565 /* Does package have any %config files? */
1566 fileAttrs = rpmfiFFlags(fi);
1567 genConfigDeps |= (fileAttrs & RPMFILE_CONFIG);
1569 av[c] = xstrdup(rpmfiFN(fi));
1570 fmode[c] = rpmfiFMode(fi);
1575 fc->skipProv = !pkg->autoProv;
1576 fc->skipReq = !pkg->autoReq;
1578 fc->brlen = (spec->buildRootURL ? strlen(spec->buildRootURL) : 0);
1580 /* Copy (and delete) manually generated dependencies to dictionary. */
1581 if (!fc->skipProv) {
1582 ds = rpmdsNew(pkg->header, RPMTAG_PROVIDENAME, flags);
1583 xx = rpmdsMerge(&fc->provides, ds);
1585 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDENAME);
1586 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEVERSION);
1587 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEFLAGS);
1589 /* Add config dependency, Provides: config(N) = EVR */
1590 if (genConfigDeps) {
1591 N = rpmdsN(pkg->ds);
1593 EVR = rpmdsEVR(pkg->ds);
1594 assert(EVR != NULL);
1595 sprintf(buf, "config(%s)", N);
1596 ds = rpmdsSingle(RPMTAG_PROVIDENAME, buf, EVR,
1597 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
1598 xx = rpmdsMerge(&fc->provides, ds);
1604 ds = rpmdsNew(pkg->header, RPMTAG_REQUIRENAME, flags);
1605 xx = rpmdsMerge(&fc->requires, ds);
1607 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIRENAME);
1608 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREVERSION);
1609 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREFLAGS);
1611 /* Add config dependency, Requires: config(N) = EVR */
1612 if (genConfigDeps) {
1613 N = rpmdsN(pkg->ds);
1615 EVR = rpmdsEVR(pkg->ds);
1616 assert(EVR != NULL);
1617 sprintf(buf, "config(%s)", N);
1618 ds = rpmdsSingle(RPMTAG_REQUIRENAME, buf, EVR,
1619 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
1620 xx = rpmdsMerge(&fc->requires, ds);
1625 /* Build file class dictionary. */
1626 xx = rpmfcClassify(fc, av, fmode);
1628 /* Build file/package dependency dictionary. */
1629 xx = rpmfcApply(fc);
1631 /* Add per-file colors(#files) */
1632 p = (const void **) argiData(fc->fcolor);
1633 c = argiCount(fc->fcolor);
1635 if (p != NULL && c > 0) {
1636 int_32 * fcolors = (int_32 *)p;
1639 /* XXX Make sure only primary (i.e. Elf32/Elf64) colors are added. */
1640 for (i = 0; i < c; i++)
1642 xx = headerAddEntry(pkg->header, RPMTAG_FILECOLORS, RPM_INT32_TYPE,
1646 /* Add classes(#classes) */
1647 p = (const void **) argvData(fc->cdict);
1648 c = argvCount(fc->cdict);
1649 if (p != NULL && c > 0)
1650 xx = headerAddEntry(pkg->header, RPMTAG_CLASSDICT, RPM_STRING_ARRAY_TYPE,
1653 /* Add per-file classes(#files) */
1654 p = (const void **) argiData(fc->fcdictx);
1655 c = argiCount(fc->fcdictx);
1657 if (p != NULL && c > 0)
1658 xx = headerAddEntry(pkg->header, RPMTAG_FILECLASS, RPM_INT32_TYPE,
1662 if (fc->provides != NULL && (c = rpmdsCount(fc->provides)) > 0 && !fc->skipProv) {
1663 p = (const void **) fc->provides->N;
1664 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
1666 /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
1667 p = (const void **) fc->provides->EVR;
1669 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
1671 p = (const void **) fc->provides->Flags;
1673 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
1678 if (fc->requires != NULL && (c = rpmdsCount(fc->requires)) > 0 && !fc->skipReq) {
1679 p = (const void **) fc->requires->N;
1680 xx = headerAddEntry(pkg->header, RPMTAG_REQUIRENAME, RPM_STRING_ARRAY_TYPE,
1682 /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
1683 p = (const void **) fc->requires->EVR;
1685 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREVERSION, RPM_STRING_ARRAY_TYPE,
1687 p = (const void **) fc->requires->Flags;
1689 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREFLAGS, RPM_INT32_TYPE,
1693 /* Add dependency dictionary(#dependencies) */
1694 p = (const void **) argiData(fc->ddictx);
1695 c = argiCount(fc->ddictx);
1697 xx = headerAddEntry(pkg->header, RPMTAG_DEPENDSDICT, RPM_INT32_TYPE,
1700 /* Add per-file dependency (start,number) pairs (#files) */
1701 p = (const void **) argiData(fc->fddictx);
1702 c = argiCount(fc->fddictx);
1705 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSX, RPM_INT32_TYPE,
1708 p = (const void **) argiData(fc->fddictn);
1709 c = argiCount(fc->fddictn);
1712 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSN, RPM_INT32_TYPE,
1715 printDeps(pkg->header);
1717 if (fc != NULL && _rpmfc_debug) {
1719 sprintf(msg, "final: files %d cdict[%d] %d%% ddictx[%d]", fc->nfiles, argvCount(fc->cdict), ((100 * fc->fknown)/fc->nfiles), argiCount(fc->ddictx));
1720 rpmfcPrint(msg, fc, NULL);
1724 fmode = _free(fmode);