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

lib/psm.c

Go to the documentation of this file.
00001 
00006 #include "system.h"
00007 
00008 #include <signal.h>
00009 #include <sys/signal.h>
00010 
00011 #include <rpmio_internal.h>
00012 #include <rpmlib.h>
00013 #include <rpmmacro.h>
00014 #include <rpmurl.h>
00015 
00016 #include "cpio.h"
00017 #include "fsm.h"                /* XXX CPIO_FOO/FSM_FOO constants */
00018 #include "psm.h"
00019 
00020 #include "rpmds.h"
00021 
00022 #define _RPMFI_INTERNAL
00023 #include "rpmfi.h"
00024 
00025 #define _RPMTE_INTERNAL
00026 #include "rpmte.h"
00027 
00028 #define _RPMTS_INTERNAL         /* XXX ts->notify */
00029 #include "rpmts.h"
00030 
00031 #include "rpmlead.h"            /* writeLead proto */
00032 #include "signature.h"          /* signature constants */
00033 #include "legacy.h"             /* XXX rpmfiBuildFNames() */
00034 #include "ugid.h"               /* XXX unameToUid() and gnameToGid() */
00035 #include "misc.h"               /* XXX stripTrailingChar() */
00036 #include "rpmdb.h"              /* XXX for db_chrootDone */
00037 #include "debug.h"
00038 
00039 #define _PSM_DEBUG      0
00040 /*@unchecked@*/
00041 int _psm_debug = _PSM_DEBUG;
00042 
00043 /*@access FD_t @*/              /* XXX void ptr args */
00044 /*@access rpmpsm @*/
00045 
00046 /*@access rpmfi @*/
00047 /*@access rpmte @*/     /* XXX rpmInstallSourcePackage */
00048 /*@access rpmts @*/     /* XXX ts->notify */
00049 
00050 int rpmVersionCompare(Header first, Header second)
00051 {
00052     const char * one, * two;
00053     int_32 * epochOne, * epochTwo;
00054     int rc;
00055 
00056     if (!headerGetEntry(first, RPMTAG_EPOCH, NULL, (void **) &epochOne, NULL))
00057         epochOne = NULL;
00058     if (!headerGetEntry(second, RPMTAG_EPOCH, NULL, (void **) &epochTwo, NULL))
00059         epochTwo = NULL;
00060 
00061     if (epochOne != NULL && epochTwo == NULL)
00062         return 1;
00063     else if (epochOne == NULL && epochTwo != NULL)
00064         return -1;
00065     else if (epochOne != NULL && epochTwo != NULL) {
00066 /*@-boundsread@*/
00067         if (*epochOne < *epochTwo)
00068             return -1;
00069         else if (*epochOne > *epochTwo)
00070             return 1;
00071 /*@=boundsread@*/
00072     }
00073 
00074     rc = headerGetEntry(first, RPMTAG_VERSION, NULL, (void **) &one, NULL);
00075     rc = headerGetEntry(second, RPMTAG_VERSION, NULL, (void **) &two, NULL);
00076 
00077     rc = rpmvercmp(one, two);
00078     if (rc)
00079         return rc;
00080 
00081     rc = headerGetEntry(first, RPMTAG_RELEASE, NULL, (void **) &one, NULL);
00082     rc = headerGetEntry(second, RPMTAG_RELEASE, NULL, (void **) &two, NULL);
00083 
00084     return rpmvercmp(one, two);
00085 }
00086 
00091 /*@observer@*/ /*@unchecked@*/
00092 static struct tagMacro {
00093 /*@observer@*/ /*@null@*/ const char *  macroname; 
00094     rpmTag      tag;            
00095 } tagMacros[] = {
00096     { "name",           RPMTAG_NAME },
00097     { "version",        RPMTAG_VERSION },
00098     { "release",        RPMTAG_RELEASE },
00099     { "epoch",          RPMTAG_EPOCH },
00100     { NULL, 0 }
00101 };
00102 
00109 static int rpmInstallLoadMacros(rpmfi fi, Header h)
00110         /*@globals rpmGlobalMacroContext @*/
00111         /*@modifies rpmGlobalMacroContext @*/
00112 {
00113     HGE_t hge = (HGE_t) fi->hge;
00114     struct tagMacro * tagm;
00115     union {
00116 /*@unused@*/ void * ptr;
00117 /*@unused@*/ const char ** argv;
00118         const char * str;
00119         int_32 * i32p;
00120     } body;
00121     char numbuf[32];
00122     rpmTagType type;
00123 
00124     for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
00125         if (!hge(h, tagm->tag, &type, (void **) &body, NULL))
00126             continue;
00127         switch (type) {
00128         case RPM_INT32_TYPE:
00129 /*@-boundsread@*/
00130             sprintf(numbuf, "%d", *body.i32p);
00131 /*@=boundsread@*/
00132             addMacro(NULL, tagm->macroname, NULL, numbuf, -1);
00133             /*@switchbreak@*/ break;
00134         case RPM_STRING_TYPE:
00135             addMacro(NULL, tagm->macroname, NULL, body.str, -1);
00136             /*@switchbreak@*/ break;
00137         case RPM_NULL_TYPE:
00138         case RPM_CHAR_TYPE:
00139         case RPM_INT8_TYPE:
00140         case RPM_INT16_TYPE:
00141         case RPM_BIN_TYPE:
00142         case RPM_STRING_ARRAY_TYPE:
00143         case RPM_I18NSTRING_TYPE:
00144         default:
00145             /*@switchbreak@*/ break;
00146         }
00147     }
00148     return 0;
00149 }
00150 
00156 /*@-bounds@*/
00157 static rpmRC markReplacedFiles(const rpmpsm psm)
00158         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
00159         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00160 {
00161     const rpmts ts = psm->ts;
00162     rpmfi fi = psm->fi;
00163     HGE_t hge = (HGE_t)fi->hge;
00164     sharedFileInfo replaced = fi->replaced;
00165     sharedFileInfo sfi;
00166     rpmdbMatchIterator mi;
00167     Header h;
00168     unsigned int * offsets;
00169     unsigned int prev;
00170     int num, xx;
00171 
00172     if (!(rpmfiFC(fi) > 0 && fi->replaced))
00173         return RPMRC_OK;
00174 
00175     num = prev = 0;
00176     for (sfi = replaced; sfi->otherPkg; sfi++) {
00177         if (prev && prev == sfi->otherPkg)
00178             continue;
00179         prev = sfi->otherPkg;
00180         num++;
00181     }
00182     if (num == 0)
00183         return RPMRC_OK;
00184 
00185     offsets = alloca(num * sizeof(*offsets));
00186     offsets[0] = 0;
00187     num = prev = 0;
00188     for (sfi = replaced; sfi->otherPkg; sfi++) {
00189         if (prev && prev == sfi->otherPkg)
00190             continue;
00191         prev = sfi->otherPkg;
00192         offsets[num++] = sfi->otherPkg;
00193     }
00194 
00195     mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
00196     xx = rpmdbAppendIterator(mi, offsets, num);
00197     xx = rpmdbSetIteratorRewrite(mi, 1);
00198 
00199     sfi = replaced;
00200     while ((h = rpmdbNextIterator(mi)) != NULL) {
00201         char * secStates;
00202         int modified;
00203         int count;
00204 
00205         modified = 0;
00206 
00207         if (!hge(h, RPMTAG_FILESTATES, NULL, (void **)&secStates, &count))
00208             continue;
00209         
00210         prev = rpmdbGetIteratorOffset(mi);
00211         num = 0;
00212         while (sfi->otherPkg && sfi->otherPkg == prev) {
00213             assert(sfi->otherFileNum < count);
00214             if (secStates[sfi->otherFileNum] != RPMFILE_STATE_REPLACED) {
00215                 secStates[sfi->otherFileNum] = RPMFILE_STATE_REPLACED;
00216                 if (modified == 0) {
00217                     /* Modified header will be rewritten. */
00218                     modified = 1;
00219                     xx = rpmdbSetIteratorModified(mi, modified);
00220                 }
00221                 num++;
00222             }
00223             sfi++;
00224         }
00225     }
00226     mi = rpmdbFreeIterator(mi);
00227 
00228     return RPMRC_OK;
00229 }
00230 /*@=bounds@*/
00231 
00232 rpmRC rpmInstallSourcePackage(rpmts ts, FD_t fd,
00233                 const char ** specFilePtr, const char ** cookie)
00234 {
00235     int scareMem = 1;
00236     rpmfi fi = NULL;
00237     const char * _sourcedir = NULL;
00238     const char * _specdir = NULL;
00239     const char * specFile = NULL;
00240     HGE_t hge;
00241     HFD_t hfd;
00242     Header h = NULL;
00243     struct rpmpsm_s psmbuf;
00244     rpmpsm psm = &psmbuf;
00245     int isSource;
00246     rpmRC rpmrc;
00247     int i;
00248 
00249     memset(psm, 0, sizeof(*psm));
00250     psm->ts = rpmtsLink(ts, "InstallSourcePackage");
00251 
00252     rpmrc = rpmReadPackageFile(ts, fd, "InstallSourcePackage", &h);
00253     switch (rpmrc) {
00254     case RPMRC_NOTTRUSTED:
00255     case RPMRC_NOKEY:
00256     case RPMRC_OK:
00257         break;
00258     default:
00259         goto exit;
00260         /*@notreached@*/ break;
00261     }
00262     if (h == NULL)
00263         goto exit;
00264 
00265     rpmrc = RPMRC_OK;
00266 
00267     isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
00268 
00269     if (!isSource) {
00270         rpmError(RPMERR_NOTSRPM, _("source package expected, binary found\n"));
00271         rpmrc = RPMRC_FAIL;
00272         goto exit;
00273     }
00274 
00275     (void) rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
00276 
00277     fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
00278     h = headerFree(h);
00279 
00280     if (fi == NULL) {   /* XXX can't happen */
00281         rpmrc = RPMRC_FAIL;
00282         goto exit;
00283     }
00284 
00285 /*@-onlytrans@*/        /* FIX: te reference */
00286     fi->te = rpmtsElement(ts, 0);
00287 /*@=onlytrans@*/
00288 /*@-nullpass@*/         /* FIX fi->h may be null */
00289     fi->te->h = headerLink(fi->h);
00290 /*@=nullpass@*/
00291     fi->te->fd = fdLink(fd, "installSourcePackage");
00292     hge = fi->hge;
00293     hfd = fi->hfd;
00294 
00295 /*@i@*/ (void) rpmInstallLoadMacros(fi, fi->h);
00296 
00297     psm->fi = rpmfiLink(fi, NULL);
00298     /*@-assignexpose -usereleased @*/
00299     psm->te = fi->te;
00300     /*@=assignexpose =usereleased @*/
00301 
00302     if (cookie) {
00303         *cookie = NULL;
00304         if (hge(fi->h, RPMTAG_COOKIE, NULL, (void **) cookie, NULL))
00305             *cookie = xstrdup(*cookie);
00306     }
00307 
00308     /* XXX FIXME: can't do endian neutral MD5 verification yet. */
00309 /*@i@*/ fi->fmd5s = hfd(fi->fmd5s, -1);
00310 
00311     /* XXX FIXME: don't do per-file mapping, force global flags. */
00312     fi->fmapflags = _free(fi->fmapflags);
00313     fi->mapflags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
00314 
00315     fi->uid = getuid();
00316     fi->gid = getgid();
00317     fi->astriplen = 0;
00318     fi->striplen = 0;
00319 
00320     fi->fuids = xcalloc(sizeof(*fi->fuids), fi->fc);
00321     fi->fgids = xcalloc(sizeof(*fi->fgids), fi->fc);
00322     for (i = 0; i < fi->fc; i++) {
00323         fi->fuids[i] = fi->uid;
00324         fi->fgids[i] = fi->gid;
00325     }
00326 
00327     for (i = 0; i < fi->fc; i++)
00328         fi->actions[i] = FA_CREATE;
00329 
00330     i = fi->fc;
00331 
00332     if (fi->h != NULL) {        /* XXX can't happen */
00333         rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
00334 
00335         if (headerIsEntry(fi->h, RPMTAG_COOKIE))
00336             for (i = 0; i < fi->fc; i++)
00337                 if (fi->fflags[i] & RPMFILE_SPECFILE) break;
00338     }
00339 
00340     if (i == fi->fc) {
00341         /* Find the spec file by name. */
00342         for (i = 0; i < fi->fc; i++) {
00343             const char * t = fi->apath[i];
00344             t += strlen(fi->apath[i]) - 5;
00345             if (!strcmp(t, ".spec")) break;
00346         }
00347     }
00348 
00349     _sourcedir = rpmGenPath(rpmtsRootDir(ts), "%{_sourcedir}", "");
00350     rpmrc = rpmMkdirPath(_sourcedir, "sourcedir");
00351     if (rpmrc) {
00352         rpmrc = RPMRC_FAIL;
00353         goto exit;
00354     }
00355 
00356     _specdir = rpmGenPath(rpmtsRootDir(ts), "%{_specdir}", "");
00357     rpmrc = rpmMkdirPath(_specdir, "specdir");
00358     if (rpmrc) {
00359         rpmrc = RPMRC_FAIL;
00360         goto exit;
00361     }
00362 
00363     /* Build dnl/dil with {_sourcedir, _specdir} as values. */
00364     if (i < fi->fc) {
00365         int speclen = strlen(_specdir) + 2;
00366         int sourcelen = strlen(_sourcedir) + 2;
00367         char * t;
00368 
00369 /*@i@*/ fi->dnl = hfd(fi->dnl, -1);
00370 
00371         fi->dc = 2;
00372         fi->dnl = xmalloc(fi->dc * sizeof(*fi->dnl)
00373                         + fi->fc * sizeof(*fi->dil)
00374                         + speclen + sourcelen);
00375         /*@-dependenttrans@*/
00376         fi->dil = (int *)(fi->dnl + fi->dc);
00377         /*@=dependenttrans@*/
00378         memset(fi->dil, 0, fi->fc * sizeof(*fi->dil));
00379         fi->dil[i] = 1;
00380         /*@-dependenttrans@*/
00381         fi->dnl[0] = t = (char *)(fi->dil + fi->fc);
00382         fi->dnl[1] = t = stpcpy( stpcpy(t, _sourcedir), "/") + 1;
00383         /*@=dependenttrans@*/
00384         (void) stpcpy( stpcpy(t, _specdir), "/");
00385 
00386         t = xmalloc(speclen + strlen(fi->bnl[i]) + 1);
00387         (void) stpcpy( stpcpy( stpcpy(t, _specdir), "/"), fi->bnl[i]);
00388         specFile = t;
00389     } else {
00390         rpmError(RPMERR_NOSPEC, _("source package contains no .spec file\n"));
00391         rpmrc = RPMRC_FAIL;
00392         goto exit;
00393     }
00394 
00395     psm->goal = PSM_PKGINSTALL;
00396 
00397     /*@-compmempass@*/  /* FIX: psm->fi->dnl should be owned. */
00398     rpmrc = rpmpsmStage(psm, PSM_PROCESS);
00399 
00400     (void) rpmpsmStage(psm, PSM_FINI);
00401     /*@=compmempass@*/
00402 
00403     if (rpmrc) rpmrc = RPMRC_FAIL;
00404 
00405 exit:
00406     if (specFilePtr && specFile && rpmrc == RPMRC_OK)
00407         *specFilePtr = specFile;
00408     else
00409         specFile = _free(specFile);
00410 
00411     _specdir = _free(_specdir);
00412     _sourcedir = _free(_sourcedir);
00413 
00414     psm->fi = rpmfiFree(psm->fi);
00415     psm->te = NULL;
00416 
00417     if (h != NULL) h = headerFree(h);
00418 
00419     /*@-branchstate@*/
00420     if (fi != NULL) {
00421         fi->te->h = headerFree(fi->te->h);
00422         if (fi->te->fd != NULL)
00423             (void) Fclose(fi->te->fd);
00424         fi->te->fd = NULL;
00425         fi->te = NULL;
00426         fi = rpmfiFree(fi);
00427     }
00428     /*@=branchstate@*/
00429 
00430     /* XXX nuke the added package(s). */
00431     rpmtsClean(ts);
00432 
00433     psm->ts = rpmtsFree(psm->ts);
00434 
00435     return rpmrc;
00436 }
00437 
00438 /*@observer@*/ /*@unchecked@*/
00439 static char * SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin";
00440 
00446 static /*@observer@*/ const char * const tag2sln(int tag)
00447         /*@*/
00448 {
00449     switch (tag) {
00450     case RPMTAG_PREIN:          return "%pre";
00451     case RPMTAG_POSTIN:         return "%post";
00452     case RPMTAG_PREUN:          return "%preun";
00453     case RPMTAG_POSTUN:         return "%postun";
00454     case RPMTAG_VERIFYSCRIPT:   return "%verify";
00455     }
00456     return "%unknownscript";
00457 }
00458 
00461 /*@unchecked@*/
00462 static sigset_t caught;
00463 
00466 /*@unchecked@*/
00467 static struct psmtbl_s {
00468     int nalloced;
00469     int npsms;
00470 /*@null@*/
00471     rpmpsm * psms;
00472 } psmtbl = { 0, 0, NULL };
00473 
00474 /* forward ref */
00475 static void handler(int signum)
00476         /*@globals caught, psmtbl, fileSystem @*/
00477         /*@modifies caught, psmtbl, fileSystem @*/;
00478 
00481 /*@unchecked@*/
00482 /*@-fullinitblock@*/
00483 static struct sigtbl_s {
00484     int signum;
00485     int active;
00486     void (*handler) (int signum);
00487     struct sigaction oact;
00488 } satbl[] = {
00489     { SIGCHLD,  0, handler },
00490     { -1,       0, NULL },
00491 };
00492 /*@=fullinitblock@*/
00493 
00496 /*@-incondefs@*/
00497 static void handler(int signum)
00498 {
00499     struct sigtbl_s * tbl;
00500 
00501     for(tbl = satbl; tbl->signum >= 0; tbl++) {
00502         if (tbl->signum != signum)
00503             continue;
00504         if (!tbl->active)
00505             continue;
00506         (void) sigaddset(&caught, signum);
00507         switch (signum) {
00508         case SIGCHLD:
00509             while (1) {
00510                 int status = 0;
00511                 pid_t reaped = waitpid(0, &status, WNOHANG);
00512                 int i;
00513 
00514                 if (reaped <= 0)
00515                     /*@innerbreak@*/ break;
00516 
00517                 if (psmtbl.psms)
00518                 for (i = 0; i < psmtbl.npsms; i++) {
00519                     rpmpsm psm = psmtbl.psms[i];
00520                     if (psm->child != reaped)
00521                         /*@innercontinue@*/ continue;
00522 
00523 #if _PSM_DEBUG
00524 /*@-modfilesys@*/
00525 if (_psm_debug)
00526 fprintf(stderr, "      Reap: %p[%d:%d:%d] = %p child %d\n", psmtbl.psms, i, psmtbl.npsms, psmtbl.nalloced, psm, psm->child);
00527 /*@=modfilesys@*/
00528 #endif
00529 
00530                     psm->reaped = reaped;
00531                     psm->status = status;
00532                     /*@innerbreak@*/ break;
00533                 }
00534             }
00535             /*@switchbreak@*/ break;
00536         default:
00537             /*@switchbreak@*/ break;
00538         }
00539         break;
00540     }
00541 }
00542 /*@=incondefs@*/
00543 
00547 static int enableSignal(int signum)
00548         /*@globals caught, satbl, fileSystem @*/
00549         /*@modifies caught, satbl, fileSystem @*/
00550 {
00551     sigset_t newMask, oldMask;
00552     struct sigtbl_s * tbl;
00553     struct sigaction act;
00554 
00555     (void) sigfillset(&newMask);                /* block all signals */
00556     (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
00557     for (tbl = satbl; tbl->signum >= 0; tbl++) {
00558         if (signum >= 0 && signum != tbl->signum)
00559             continue;
00560 /*@-modfilesys@*/
00561 if (_psm_debug)
00562 fprintf(stderr, "    Enable: %p[0:%d:%d] active %d\n", psmtbl.psms, psmtbl.npsms, psmtbl.nalloced, tbl->active);
00563 /*@=modfilesys@*/
00564         if (tbl->active++ <= 0) {
00565             (void) sigdelset(&caught, tbl->signum);
00566             memset(&act, 0, sizeof(act));
00567             act.sa_handler = tbl->handler;
00568             (void) sigaction(tbl->signum, &act, &tbl->oact);
00569         }
00570         break;
00571     }
00572     return sigprocmask(SIG_SETMASK, &oldMask, NULL);
00573 }
00574 
00578 static int disableSignal(int signum)
00579         /*@globals satbl, fileSystem @*/
00580         /*@modifies satbl, fileSystem @*/
00581 {
00582     sigset_t newMask, oldMask;
00583     struct sigtbl_s * tbl;
00584 
00585     (void) sigfillset(&newMask);                /* block all signals */
00586     (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
00587     for (tbl = satbl; tbl->signum >= 0; tbl++) {
00588         if (signum >= 0 && signum != tbl->signum)
00589             continue;
00590 /*@-modfilesys@*/
00591 if (_psm_debug)
00592 fprintf(stderr, "   Disable: %p[0:%d:%d] active %d\n", psmtbl.psms, psmtbl.npsms, psmtbl.nalloced, tbl->active);
00593 /*@=modfilesys@*/
00594         if (--tbl->active <= 0) {
00595             tbl->active = 0;            /* XXX just in case */
00596             (void) sigaction(tbl->signum, &tbl->oact, NULL);
00597         }
00598         break;
00599     }
00600     return sigprocmask(SIG_SETMASK, &oldMask, NULL);
00601 }
00602 
00608 static pid_t psmRegisterFork(rpmpsm psm)
00609         /*@globals psmtbl, fileSystem, internalState @*/
00610         /*@modifies psm, psmtbl, fileSystem, internalState @*/
00611 {
00612     sigset_t newMask, oldMask;
00613     int empty = -1;
00614     int i = psmtbl.npsms;
00615 
00616     (void) sigfillset(&newMask);                /* block all signals */
00617     (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
00618 
00619     if (psmtbl.psms)
00620     for (i = 0; i < psmtbl.npsms; i++) {
00621         if (empty == -1 && psmtbl.psms[i] == NULL)
00622             empty = i;
00623         if (psm != psmtbl.psms[i])
00624             continue;
00625         break;
00626     }
00627     if (i == psmtbl.npsms) {
00628         if (i >= psmtbl.nalloced) {
00629             if (psmtbl.nalloced == 0) psmtbl.nalloced = 5;
00630             while (psmtbl.nalloced < i)
00631                 psmtbl.nalloced += psmtbl.nalloced;
00632             psmtbl.psms = xrealloc(psmtbl.psms,
00633                         psmtbl.nalloced * sizeof(*psmtbl.psms));
00634         }
00635         empty = psmtbl.npsms++;
00636     }
00637     if (psmtbl.psms)    /* XXX can't happen */
00638         psmtbl.psms[empty] = rpmpsmLink(psm, "psmRegister");
00639 /*@-modfilesys@*/
00640 if (_psm_debug)
00641 fprintf(stderr, "  Register: %p[%d:%d:%d] = %p\n", psmtbl.psms, empty, psmtbl.npsms, psmtbl.nalloced, psm);
00642 /*@=modfilesys@*/
00643 
00644     (void) enableSignal(SIGCHLD);
00645 
00646     psm->reaped = 0;
00647     if ((psm->child = fork()) != 0) {
00648 /*@-modfilesys@*/
00649 if (_psm_debug)
00650 fprintf(stderr, "      Fork: %p[%d:%d:%d] = %p child %d\n", psmtbl.psms, 0, psmtbl.npsms, psmtbl.nalloced, psm, psm->child);
00651 /*@=modfilesys@*/
00652     }
00653 
00654     (void) sigprocmask(SIG_SETMASK, &oldMask, NULL);
00655 
00656     return psm->child;
00657 }
00658 
00662 static int psmWaitUnregister(rpmpsm psm, pid_t child)
00663         /*@globals psmtbl, fileSystem, internalState @*/
00664         /*@modifies psmtbl, fileSystem, internalState @*/
00665 {
00666     sigset_t newMask, oldMask;
00667     int i = 0;
00668 
00669     (void) sigfillset(&newMask);                /* block all signals */
00670     (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
00671     
00672     /*@-infloops@*/
00673     while (psm->reaped != psm->child)
00674         (void) sigsuspend(&oldMask);
00675     /*@=infloops@*/
00676 
00677 /*@-modfilesys@*/
00678 if (_psm_debug)
00679 fprintf(stderr, "      Wait: %p[%d:%d:%d] = %p child %d\n", psmtbl.psms, 0, psmtbl.npsms, psmtbl.nalloced, psm, psm->child);
00680 /*@=modfilesys@*/
00681 
00682     if (psmtbl.psms)
00683     for (i = 0; i < psmtbl.npsms; i++) {
00684         if (psmtbl.psms[i] == NULL)
00685             continue;
00686         if (psm != psmtbl.psms[i])
00687             continue;
00688         if (child != psm->child)
00689             continue;
00690         break;
00691     }
00692 
00693     if (i < psmtbl.npsms) {
00694         (void) disableSignal(SIGCHLD);
00695 /*@-modfilesys@*/
00696 if (_psm_debug)
00697 fprintf(stderr, "Unregister: %p[%d:%d:%d] = %p child %d\n", psmtbl.psms, i, psmtbl.npsms, psmtbl.nalloced, psm, child);
00698 /*@=modfilesys@*/
00699         if (psmtbl.psms)        /* XXX can't happen */
00700             psmtbl.psms[i] = rpmpsmFree(psmtbl.psms[i]);
00701         if (psmtbl.npsms == (i+1))
00702             psmtbl.npsms--;
00703         if (psmtbl.npsms == 0) {
00704             psmtbl.psms = _free(psmtbl.psms);
00705             psmtbl.nalloced = 0;
00706         }
00707     }
00708 
00709     return sigprocmask(SIG_SETMASK, &oldMask, NULL);
00710 }
00711 
00717 static pid_t psmWait(rpmpsm psm)
00718         /*@globals fileSystem, internalState @*/
00719         /*@modifies psm, fileSystem, internalState @*/
00720 {
00721     if (psm->reaper) {
00722         (void) psmWaitUnregister(psm, psm->child);
00723     } else {
00724         do {
00725             psm->reaped = waitpid(psm->child, &psm->status, 0);
00726         } while (psm->reaped >= 0 && psm->reaped != psm->child);
00727     }
00728 
00729     rpmMessage(RPMMESS_DEBUG, _("%s: waitpid(%d) rc %d status %x\n"),
00730         psm->stepName, (unsigned)psm->child,
00731         (unsigned)psm->reaped, psm->status);
00732 
00733     return psm->reaped;
00734 }
00735 
00738 /*@unchecked@*/
00739 static int ldconfig_done = 0;
00740 
00741 /*@unchecked@*/ /*@observer@*/ /*@null@*/
00742 static const char * ldconfig_path = "/sbin/ldconfig";
00743 
00762 static rpmRC runScript(rpmpsm psm, Header h, const char * sln,
00763                 int progArgc, const char ** progArgv, 
00764                 const char * script, int arg1, int arg2)
00765         /*@globals ldconfig_done, rpmGlobalMacroContext,
00766                 fileSystem, internalState@*/
00767         /*@modifies psm, ldconfig_done, rpmGlobalMacroContext,
00768                 fileSystem, internalState @*/
00769 {
00770     const rpmts ts = psm->ts;
00771     rpmfi fi = psm->fi;
00772     HGE_t hge = fi->hge;
00773     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00774     const char ** argv = NULL;
00775     int argc = 0;
00776     const char ** prefixes = NULL;
00777     int numPrefixes;
00778     rpmTagType ipt;
00779     const char * oldPrefix;
00780     int maxPrefixLength;
00781     int len;
00782     char * prefixBuf = NULL;
00783     const char * fn = NULL;
00784     int i, xx;
00785     int freePrefixes = 0;
00786     FD_t scriptFd;
00787     FD_t out;
00788     rpmRC rc = RPMRC_OK;
00789     const char *n, *v, *r;
00790 
00791     if (progArgv == NULL && script == NULL)
00792         return rc;
00793 
00794     psm->child = 0;
00795     psm->reaped = 0;
00796     psm->status = 0;
00797     psm->reaper = 1;
00798 
00799     /* XXX FIXME: except for %verifyscript, rpmteNEVR can be used. */
00800     xx = headerNVR(h, &n, &v, &r);
00801 
00802     /* XXX bash must have functional libtermcap.so.2 */
00803     if (!strcmp(n, "libtermcap"))
00804         ldconfig_done = 0;
00805 
00806     /*
00807      * If a successor node, and ldconfig was just run, don't bother.
00808      */
00809     if (ldconfig_path && progArgv && psm->unorderedSuccessor) {
00810         if (ldconfig_done && !strcmp(progArgv[0], ldconfig_path)) {
00811             rpmMessage(RPMMESS_DEBUG,
00812                 _("%s: %s(%s-%s-%s) skipping redundant \"%s\".\n"),
00813                 psm->stepName, tag2sln(psm->scriptTag), n, v, r,
00814                 progArgv[0]);
00815             return rc;
00816         }
00817     }
00818 
00819     rpmMessage(RPMMESS_DEBUG,
00820                 _("%s: %s(%s-%s-%s) %ssynchronous scriptlet start\n"),
00821                 psm->stepName, tag2sln(psm->scriptTag), n, v, r,
00822                 (psm->unorderedSuccessor ? "a" : ""));
00823 
00824     if (!progArgv) {
00825         argv = alloca(5 * sizeof(*argv));
00826         argv[0] = "/bin/sh";
00827         argc = 1;
00828         ldconfig_done = 0;
00829     } else {
00830         argv = alloca((progArgc + 4) * sizeof(*argv));
00831         memcpy(argv, progArgv, progArgc * sizeof(*argv));
00832         argc = progArgc;
00833         ldconfig_done = (ldconfig_path && !strcmp(argv[0], ldconfig_path)
00834                 ? 1 : 0);
00835     }
00836 
00837     if (hge(h, RPMTAG_INSTPREFIXES, &ipt, (void **) &prefixes, &numPrefixes)) {
00838         freePrefixes = 1;
00839     } else if (hge(h, RPMTAG_INSTALLPREFIX, NULL, (void **) &oldPrefix, NULL)) {
00840         prefixes = &oldPrefix;
00841         numPrefixes = 1;
00842     } else {
00843         numPrefixes = 0;
00844     }
00845 
00846     maxPrefixLength = 0;
00847     if (prefixes != NULL)
00848     for (i = 0; i < numPrefixes; i++) {
00849         len = strlen(prefixes[i]);
00850         if (len > maxPrefixLength) maxPrefixLength = len;
00851     }
00852     prefixBuf = alloca(maxPrefixLength + 50);
00853 
00854     if (script) {
00855         const char * rootDir = rpmtsRootDir(ts);
00856         FD_t fd;
00857 
00858         /*@-branchstate@*/
00859         if (makeTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn, &fd)) {
00860             if (prefixes != NULL && freePrefixes) free(prefixes);
00861             return RPMRC_FAIL;
00862         }
00863         /*@=branchstate@*/
00864 
00865         if (rpmIsDebug() &&
00866             (!strcmp(argv[0], "/bin/sh") || !strcmp(argv[0], "/bin/bash")))
00867         {
00868             static const char set_x[] = "set -x\n";
00869             xx = Fwrite(set_x, sizeof(set_x[0]), sizeof(set_x)-1, fd);
00870         }
00871 
00872         if (ldconfig_path && strstr(script, ldconfig_path) != NULL)
00873             ldconfig_done = 1;
00874 
00875         xx = Fwrite(script, sizeof(script[0]), strlen(script), fd);
00876         xx = Fclose(fd);
00877 
00878         {   const char * sn = fn;
00879             if (!rpmtsChrootDone(ts) && rootDir != NULL &&
00880                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00881             {
00882                 sn += strlen(rootDir)-1;
00883             }
00884             argv[argc++] = sn;
00885         }
00886 
00887         if (arg1 >= 0) {
00888             char *av = alloca(20);
00889             sprintf(av, "%d", arg1);
00890             argv[argc++] = av;
00891         }
00892         if (arg2 >= 0) {
00893             char *av = alloca(20);
00894             sprintf(av, "%d", arg2);
00895             argv[argc++] = av;
00896         }
00897     }
00898 
00899     argv[argc] = NULL;
00900 
00901     scriptFd = rpmtsScriptFd(ts);
00902     if (scriptFd != NULL) {
00903         if (rpmIsVerbose()) {
00904             out = fdDup(Fileno(scriptFd));
00905         } else {
00906             out = Fopen("/dev/null", "w.fdio");
00907             if (Ferror(out)) {
00908                 out = fdDup(Fileno(scriptFd));
00909             }
00910         }
00911     } else {
00912         out = fdDup(STDOUT_FILENO);
00913     }
00914     if (out == NULL) return RPMRC_FAIL; /* XXX can't happen */
00915     
00916     /*@-branchstate@*/
00917     (void) psmRegisterFork(psm);
00918     if (psm->child == 0) {
00919         const char * rootDir;
00920         int pipes[2];
00921 
00922         pipes[0] = pipes[1] = 0;
00923         /* make stdin inaccessible */
00924         xx = pipe(pipes);
00925         xx = close(pipes[1]);
00926         xx = dup2(pipes[0], STDIN_FILENO);
00927         xx = close(pipes[0]);
00928 
00929         if (scriptFd != NULL) {
00930             int sfdno = Fileno(scriptFd);
00931             int ofdno = Fileno(out);
00932             if (sfdno != STDERR_FILENO)
00933                 xx = dup2(sfdno, STDERR_FILENO);
00934             if (ofdno != STDOUT_FILENO)
00935                 xx = dup2(ofdno, STDOUT_FILENO);
00936             /* make sure we don't close stdin/stderr/stdout by mistake! */
00937             if (ofdno > STDERR_FILENO && ofdno != sfdno) {
00938                 xx = Fclose (out);
00939             }
00940             if (sfdno > STDERR_FILENO) {
00941                 xx = Fclose (scriptFd);
00942             }
00943         }
00944 
00945         {   const char *ipath = rpmExpand("PATH=%{_install_script_path}", NULL);
00946             const char *path = SCRIPT_PATH;
00947 
00948             if (ipath && ipath[5] != '%')
00949                 path = ipath;
00950 
00951             xx = doputenv(path);
00952             /*@-modobserver@*/
00953             ipath = _free(ipath);
00954             /*@=modobserver@*/
00955         }
00956 
00957         if (prefixes != NULL)
00958         for (i = 0; i < numPrefixes; i++) {
00959             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00960             xx = doputenv(prefixBuf);
00961 
00962             /* backwards compatibility */
00963             if (i == 0) {
00964                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00965                 xx = doputenv(prefixBuf);
00966             }
00967         }
00968 
00969         rootDir = rpmtsRootDir(ts);
00970         if (rootDir  != NULL)   /* XXX can't happen */
00971         switch(urlIsURL(rootDir)) {
00972         case URL_IS_PATH:
00973             rootDir += sizeof("file://") - 1;
00974             rootDir = strchr(rootDir, '/');
00975             /*@fallthrough@*/
00976         case URL_IS_UNKNOWN:
00977             if (!rpmtsChrootDone(ts) &&
00978                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00979             {
00980                 /*@-superuser -noeffect @*/
00981                 xx = chroot(rootDir);
00982                 /*@=superuser =noeffect @*/
00983             }
00984             xx = chdir("/");
00985             rpmMessage(RPMMESS_DEBUG, _("%s: %s(%s-%s-%s)\texecv(%s) pid %d\n"),
00986                         psm->stepName, sln, n, v, r,
00987                         argv[0], (unsigned)getpid());
00988 /*@-modfilesys@*/
00989 if (_psm_debug)
00990 fprintf(stderr, "      Exec: %s \"%s\"\n", sln, argv[0]);
00991 /*@=modfilesys@*/
00992             unsetenv("MALLOC_CHECK_");
00993             xx = execv(argv[0], (char *const *)argv);
00994             break;
00995         default:
00996             break;
00997         }
00998 
00999         _exit(-1);
01000         /*@notreached@*/
01001     }
01002     /*@=branchstate@*/
01003 
01004     (void) psmWait(psm);
01005 
01006     if (psm->reaped < 0) {
01007         rpmError(RPMERR_SCRIPT,
01008                 _("%s(%s-%s-%s) scriptlet failed, waitpid(%d) rc %d: %s\n"),
01009                  sln, n, v, r, psm->child, psm->reaped, strerror(errno));
01010         rc = RPMRC_FAIL;
01011     } else
01012     if (!WIFEXITED(psm->status) || WEXITSTATUS(psm->status)) {
01013         rpmError(RPMERR_SCRIPT,
01014                 _("%s(%s-%s-%s) scriptlet failed, exit status %d\n"),
01015                 sln, n, v, r, WEXITSTATUS(psm->status));
01016         rc = RPMRC_FAIL;
01017     }
01018 
01019     if (freePrefixes) prefixes = hfd(prefixes, ipt);
01020 
01021     xx = Fclose(out);   /* XXX dup'd STDOUT_FILENO */
01022     
01023     /*@-branchstate@*/
01024     if (script) {
01025         if (!rpmIsDebug())
01026             xx = unlink(fn);
01027         fn = _free(fn);
01028     }
01029     /*@=branchstate@*/
01030 
01031     return rc;
01032 }
01033 
01039 static rpmRC runInstScript(rpmpsm psm)
01040         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
01041         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
01042 {
01043     rpmfi fi = psm->fi;
01044     HGE_t hge = fi->hge;
01045     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01046     void ** progArgv;
01047     int progArgc;
01048     const char ** argv;
01049     rpmTagType ptt, stt;
01050     const char * script;
01051     rpmRC rc = RPMRC_OK;
01052     int xx;
01053 
01054     /*
01055      * headerGetEntry() sets the data pointer to NULL if the entry does
01056      * not exist.
01057      */
01058     xx = hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
01059     xx = hge(fi->h, psm->progTag, &ptt, (void **) &progArgv, &progArgc);
01060     if (progArgv == NULL && script == NULL)
01061         goto exit;
01062 
01063     /*@-branchstate@*/
01064     if (progArgv && ptt == RPM_STRING_TYPE) {
01065         argv = alloca(sizeof(*argv));
01066         *argv = (const char *) progArgv;
01067     } else {
01068         argv = (const char **) progArgv;
01069     }
01070     /*@=branchstate@*/
01071 
01072     if (fi->h != NULL)  /* XXX can't happen */
01073     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), progArgc, argv,
01074                 script, psm->scriptArg, -1);
01075 
01076 exit:
01077     progArgv = hfd(progArgv, ptt);
01078     script = hfd(script, stt);
01079     return rc;
01080 }
01081 
01092 static rpmRC handleOneTrigger(const rpmpsm psm,
01093                         Header sourceH, Header triggeredH,
01094                         int arg2, unsigned char * triggersAlreadyRun)
01095         /*@globals rpmGlobalMacroContext, fileSystem, internalState@*/
01096         /*@modifies psm, sourceH, triggeredH, *triggersAlreadyRun,
01097                 rpmGlobalMacroContext, fileSystem, internalState @*/
01098 {
01099     int scareMem = 1;
01100     const rpmts ts = psm->ts;
01101     rpmfi fi = psm->fi;
01102     HGE_t hge = fi->hge;
01103     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01104     rpmds trigger = NULL;
01105     const char ** triggerScripts;
01106     const char ** triggerProgs;
01107     int_32 * triggerIndices;
01108     const char * sourceName;
01109     rpmRC rc = RPMRC_OK;
01110     int xx;
01111     int i;
01112 
01113     xx = headerNVR(sourceH, &sourceName, NULL, NULL);
01114 
01115     trigger = rpmdsInit(rpmdsNew(triggeredH, RPMTAG_TRIGGERNAME, scareMem));
01116     if (trigger == NULL)
01117         return rc;
01118 
01119     (void) rpmdsSetNoPromote(trigger, 1);
01120 
01121     while ((i = rpmdsNext(trigger)) >= 0) {
01122         rpmTagType tit, tst, tpt;
01123         const char * Name;
01124         int_32 Flags = rpmdsFlags(trigger);
01125 
01126         if ((Name = rpmdsN(trigger)) == NULL)
01127             continue;   /* XXX can't happen */
01128 
01129         if (strcmp(Name, sourceName))
01130             continue;
01131         if (!(Flags & psm->sense))
01132             continue;
01133 
01134         /*
01135          * XXX Trigger on any provided dependency, not just the package NEVR.
01136          */
01137         if (!rpmdsAnyMatchesDep(sourceH, trigger, 1))
01138             continue;
01139 
01140         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
01141                        (void **) &triggerIndices, NULL) &&
01142                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
01143                        (void **) &triggerScripts, NULL) &&
01144                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
01145                        (void **) &triggerProgs, NULL))
01146             )
01147             continue;
01148 
01149         {   int arg1;
01150             int index;
01151 
01152             arg1 = rpmdbCountPackages(rpmtsGetRdb(ts), Name);
01153             if (arg1 < 0) {
01154                 /* XXX W2DO? fails as "execution of script failed" */
01155                 rc = RPMRC_FAIL;
01156             } else {
01157                 arg1 += psm->countCorrection;
01158                 index = triggerIndices[i];
01159                 if (triggersAlreadyRun == NULL ||
01160                     triggersAlreadyRun[index] == 0)
01161                 {
01162                     rc = runScript(psm, triggeredH, "%trigger", 1,
01163                             triggerProgs + index, triggerScripts[index], 
01164                             arg1, arg2);
01165                     if (triggersAlreadyRun != NULL)
01166                         triggersAlreadyRun[index] = 1;
01167                 }
01168             }
01169         }
01170 
01171         triggerIndices = hfd(triggerIndices, tit);
01172         triggerScripts = hfd(triggerScripts, tst);
01173         triggerProgs = hfd(triggerProgs, tpt);
01174 
01175         /*
01176          * Each target/source header pair can only result in a single
01177          * script being run.
01178          */
01179         break;
01180     }
01181 
01182     trigger = rpmdsFree(trigger);
01183 
01184     return rc;
01185 }
01186 
01192 static rpmRC runTriggers(rpmpsm psm)
01193         /*@globals rpmGlobalMacroContext,
01194                 fileSystem, internalState @*/
01195         /*@modifies psm, rpmGlobalMacroContext,
01196                 fileSystem, internalState @*/
01197 {
01198     const rpmts ts = psm->ts;
01199     rpmfi fi = psm->fi;
01200     int numPackage = -1;
01201     rpmRC rc = RPMRC_OK;
01202     const char * N = NULL;
01203 
01204     if (psm->te)        /* XXX can't happen */
01205         N = rpmteN(psm->te);
01206     if (N)              /* XXX can't happen */
01207         numPackage = rpmdbCountPackages(rpmtsGetRdb(ts), N)
01208                                 + psm->countCorrection;
01209     if (numPackage < 0)
01210         return RPMRC_NOTFOUND;
01211 
01212     if (fi != NULL && fi->h != NULL)    /* XXX can't happen */
01213     {   Header triggeredH;
01214         rpmdbMatchIterator mi;
01215         int countCorrection = psm->countCorrection;
01216 
01217         psm->countCorrection = 0;
01218         mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, N, 0);
01219         while((triggeredH = rpmdbNextIterator(mi)) != NULL)
01220             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01221         mi = rpmdbFreeIterator(mi);
01222         psm->countCorrection = countCorrection;
01223     }
01224 
01225     return rc;
01226 }
01227 
01233 static rpmRC runImmedTriggers(rpmpsm psm)
01234         /*@globals rpmGlobalMacroContext,
01235                 fileSystem, internalState @*/
01236         /*@modifies psm, rpmGlobalMacroContext,
01237                 fileSystem, internalState @*/
01238 {
01239     const rpmts ts = psm->ts;
01240     rpmfi fi = psm->fi;
01241     HGE_t hge = fi->hge;
01242     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01243     const char ** triggerNames;
01244     int numTriggers;
01245     int_32 * triggerIndices;
01246     rpmTagType tnt, tit;
01247     int numTriggerIndices;
01248     unsigned char * triggersRun;
01249     rpmRC rc = RPMRC_OK;
01250 
01251     if (fi->h == NULL)  return rc;      /* XXX can't happen */
01252 
01253     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01254                         (void **) &triggerNames, &numTriggers) &&
01255                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01256                         (void **) &triggerIndices, &numTriggerIndices))
01257         )
01258         return rc;
01259 
01260     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01261     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01262 
01263     {   Header sourceH = NULL;
01264         int i;
01265 
01266         for (i = 0; i < numTriggers; i++) {
01267             rpmdbMatchIterator mi;
01268 
01269             if (triggersRun[triggerIndices[i]] != 0) continue;
01270         
01271             mi = rpmtsInitIterator(ts, RPMTAG_NAME, triggerNames[i], 0);
01272 
01273             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01274                 rc |= handleOneTrigger(psm, sourceH, fi->h, 
01275                                 rpmdbGetIteratorCount(mi),
01276                                 triggersRun);
01277             }
01278 
01279             mi = rpmdbFreeIterator(mi);
01280         }
01281     }
01282     triggerIndices = hfd(triggerIndices, tit);
01283     triggerNames = hfd(triggerNames, tnt);
01284     return rc;
01285 }
01286 
01287 /*@observer@*/ static const char *const pkgStageString(pkgStage a)
01288         /*@*/
01289 {
01290     switch(a) {
01291     case PSM_UNKNOWN:           return "unknown";
01292 
01293     case PSM_PKGINSTALL:        return "  install";
01294     case PSM_PKGERASE:          return "    erase";
01295     case PSM_PKGCOMMIT:         return "   commit";
01296     case PSM_PKGSAVE:           return "repackage";
01297 
01298     case PSM_INIT:              return "init";
01299     case PSM_PRE:               return "pre";
01300     case PSM_PROCESS:           return "process";
01301     case PSM_POST:              return "post";
01302     case PSM_UNDO:              return "undo";
01303     case PSM_FINI:              return "fini";
01304 
01305     case PSM_CREATE:            return "create";
01306     case PSM_NOTIFY:            return "notify";
01307     case PSM_DESTROY:           return "destroy";
01308     case PSM_COMMIT:            return "commit";
01309 
01310     case PSM_CHROOT_IN:         return "chrootin";
01311     case PSM_CHROOT_OUT:        return "chrootout";
01312     case PSM_SCRIPT:            return "script";
01313     case PSM_TRIGGERS:          return "triggers";
01314     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01315 
01316     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01317 
01318     case PSM_RPMDB_LOAD:        return "rpmdbload";
01319     case PSM_RPMDB_ADD:         return "rpmdbadd";
01320     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01321 
01322     default:                    return "???";
01323     }
01324     /*@noteached@*/
01325 }
01326 
01327 rpmpsm XrpmpsmUnlink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01328 {
01329     if (psm == NULL) return NULL;
01330 /*@-modfilesys@*/
01331 if (_psm_debug && msg != NULL)
01332 fprintf(stderr, "--> psm %p -- %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01333 /*@=modfilesys@*/
01334     psm->nrefs--;
01335     return NULL;
01336 }
01337 
01338 rpmpsm XrpmpsmLink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01339 {
01340     if (psm == NULL) return NULL;
01341     psm->nrefs++;
01342 
01343 /*@-modfilesys@*/
01344 if (_psm_debug && msg != NULL)
01345 fprintf(stderr, "--> psm %p ++ %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01346 /*@=modfilesys@*/
01347 
01348     /*@-refcounttrans@*/ return psm; /*@=refcounttrans@*/
01349 }
01350 
01351 rpmpsm rpmpsmFree(rpmpsm psm)
01352 {
01353     const char * msg = "rpmpsmFree";
01354     if (psm == NULL)
01355         return NULL;
01356 
01357     if (psm->nrefs > 1)
01358         return rpmpsmUnlink(psm, msg);
01359 
01360 /*@-nullstate@*/
01361     psm->fi = rpmfiFree(psm->fi);
01362 #ifdef  NOTYET
01363     psm->te = rpmteFree(psm->te);
01364 #else
01365     psm->te = NULL;
01366 #endif
01367     psm->ts = rpmtsFree(psm->ts);
01368 
01369     (void) rpmpsmUnlink(psm, msg);
01370 
01371     /*@-refcounttrans -usereleased@*/
01372 /*@-boundswrite@*/
01373     memset(psm, 0, sizeof(*psm));               /* XXX trash and burn */
01374 /*@=boundswrite@*/
01375     psm = _free(psm);
01376     /*@=refcounttrans =usereleased@*/
01377 
01378     return NULL;
01379 /*@=nullstate@*/
01380 }
01381 
01382 rpmpsm rpmpsmNew(rpmts ts, rpmte te, rpmfi fi)
01383 {
01384     const char * msg = "rpmpsmNew";
01385     rpmpsm psm = xcalloc(1, sizeof(*psm));
01386 
01387     if (ts)     psm->ts = rpmtsLink(ts, msg);
01388 #ifdef  NOTYET
01389     if (te)     psm->te = rpmteLink(te, msg);
01390 #else
01391 /*@-assignexpose -temptrans @*/
01392     if (te)     psm->te = te;
01393 /*@=assignexpose =temptrans @*/
01394 #endif
01395     if (fi)     psm->fi = rpmfiLink(fi, msg);
01396 
01397     return rpmpsmLink(psm, msg);
01398 }
01399 
01404 /*@-bounds -nullpass@*/ /* FIX: testing null annotation for fi->h */
01405 rpmRC rpmpsmStage(rpmpsm psm, pkgStage stage)
01406 {
01407     const rpmts ts = psm->ts;
01408     uint_32 tscolor = rpmtsColor(ts);
01409     rpmfi fi = psm->fi;
01410     HGE_t hge = fi->hge;
01411     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01412     rpmRC rc = psm->rc;
01413     int saveerrno;
01414     int xx;
01415 
01416     /*@-branchstate@*/
01417     switch (stage) {
01418     case PSM_UNKNOWN:
01419         break;
01420     case PSM_INIT:
01421         rpmMessage(RPMMESS_DEBUG, _("%s: %s has %d files, test = %d\n"),
01422                 psm->stepName, rpmteNEVR(psm->te),
01423                 rpmfiFC(fi), (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST));
01424 
01425         /*
01426          * When we run scripts, we pass an argument which is the number of 
01427          * versions of this package that will be installed when we are
01428          * finished.
01429          */
01430         psm->npkgs_installed = rpmdbCountPackages(rpmtsGetRdb(ts), rpmteN(psm->te));
01431         if (psm->npkgs_installed < 0) {
01432             rc = RPMRC_FAIL;
01433             break;
01434         }
01435 
01436         if (psm->goal == PSM_PKGINSTALL) {
01437             int fc = rpmfiFC(fi);
01438 
01439             psm->scriptArg = psm->npkgs_installed + 1;
01440 
01441 assert(psm->mi == NULL);
01442             psm->mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(psm->te), 0);
01443             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_EPOCH, RPMMIRE_DEFAULT,
01444                         rpmteE(psm->te));
01445             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
01446                         rpmteV(psm->te));
01447             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
01448                         rpmteR(psm->te));
01449             if (tscolor) {
01450                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_ARCH, RPMMIRE_DEFAULT,
01451                         rpmteA(psm->te));
01452                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_OS, RPMMIRE_DEFAULT,
01453                         rpmteO(psm->te));
01454             }
01455 
01456             while ((psm->oh = rpmdbNextIterator(psm->mi)) != NULL) {
01457                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01458                 psm->oh = NULL;
01459                 /*@loopbreak@*/ break;
01460             }
01461             psm->mi = rpmdbFreeIterator(psm->mi);
01462             rc = RPMRC_OK;
01463 
01464             /* XXX lazy alloc here may need to be done elsewhere. */
01465             if (fi->fstates == NULL && fc > 0) {
01466                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fc);
01467                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fc);
01468             }
01469 
01470             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01471             if (fc <= 0)                                break;
01472         
01473             /*
01474              * Old format relocateable packages need the entire default
01475              * prefix stripped to form the cpio list, while all other packages
01476              * need the leading / stripped.
01477              */
01478             {   const char * p;
01479                 xx = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01480                 fi->striplen = (xx ? strlen(p) + 1 : 1); 
01481             }
01482             fi->mapflags =
01483                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01484         
01485             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01486                 rpmfiBuildFNames(fi->h, RPMTAG_ORIGBASENAMES, &fi->apath, NULL);
01487             else
01488                 rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
01489         
01490             if (fi->fuser == NULL)
01491                 xx = hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01492                                 (void **) &fi->fuser, NULL);
01493             if (fi->fgroup == NULL)
01494                 xx = hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01495                                 (void **) &fi->fgroup, NULL);
01496             if (fi->fuids == NULL)
01497                 fi->fuids = xcalloc(sizeof(*fi->fuids), fc);
01498             if (fi->fgids == NULL)
01499                 fi->fgids = xcalloc(sizeof(*fi->fgids), fc);
01500             rc = RPMRC_OK;
01501         }
01502         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01503             psm->scriptArg = psm->npkgs_installed - 1;
01504         
01505             /* Retrieve installed header. */
01506             rc = rpmpsmStage(psm, PSM_RPMDB_LOAD);
01507 if (rc == RPMRC_OK)
01508 if (psm->te)
01509 psm->te->h = headerLink(fi->h);
01510         }
01511         if (psm->goal == PSM_PKGSAVE) {
01512             /* Open output package for writing. */
01513             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01514                 const char * pkgbn =
01515                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01516 
01517                 bfmt = _free(bfmt);
01518                 psm->pkgURL = rpmGenPath("%{?_repackage_root}",
01519                                          "%{?_repackage_dir}",
01520                                         pkgbn);
01521                 pkgbn = _free(pkgbn);
01522                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01523                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01524                 if (psm->fd == NULL || Ferror(psm->fd)) {
01525                     rc = RPMRC_FAIL;
01526                     break;
01527                 }
01528             }
01529         }
01530         break;
01531     case PSM_PRE:
01532         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01533 
01534 /* XXX insure that trigger index is opened before entering chroot. */
01535 #ifdef  NOTYET
01536  { static int oneshot = 0;
01537    dbiIndex dbi;
01538    if (!oneshot) {
01539      dbi = dbiOpen(rpmtsGetRdb(ts), RPMTAG_TRIGGERNAME, 0);
01540      oneshot++;
01541    }
01542  }
01543 #endif
01544 
01545         /* Change root directory if requested and not already done. */
01546         rc = rpmpsmStage(psm, PSM_CHROOT_IN);
01547 
01548         if (psm->goal == PSM_PKGINSTALL) {
01549             psm->scriptTag = RPMTAG_PREIN;
01550             psm->progTag = RPMTAG_PREINPROG;
01551 
01552             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01553                 /* XXX FIXME: implement %triggerprein. */
01554             }
01555 
01556             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPRE)) {
01557                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01558                 if (rc != RPMRC_OK) {
01559                     rpmError(RPMERR_SCRIPT,
01560                         _("%s: %s scriptlet failed (%d), skipping %s\n"),
01561                         psm->stepName, tag2sln(psm->scriptTag), rc,
01562                         rpmteNEVR(psm->te));
01563                     break;
01564                 }
01565             }
01566         }
01567 
01568         if (psm->goal == PSM_PKGERASE) {
01569             psm->scriptTag = RPMTAG_PREUN;
01570             psm->progTag = RPMTAG_PREUNPROG;
01571             psm->sense = RPMSENSE_TRIGGERUN;
01572             psm->countCorrection = -1;
01573 
01574             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERUN)) {
01575                 /* Run triggers in this package other package(s) set off. */
01576                 rc = rpmpsmStage(psm, PSM_IMMED_TRIGGERS);
01577                 if (rc) break;
01578 
01579                 /* Run triggers in other package(s) this package sets off. */
01580                 rc = rpmpsmStage(psm, PSM_TRIGGERS);
01581                 if (rc) break;
01582             }
01583 
01584             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPREUN))
01585                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01586         }
01587         if (psm->goal == PSM_PKGSAVE) {
01588             int noArchiveSize = 0;
01589 
01590             /* Regenerate original header. */
01591             {   void * uh = NULL;
01592                 int_32 uht, uhc;
01593 
01594                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01595                     psm->oh = headerCopyLoad(uh);
01596                     uh = hfd(uh, uht);
01597                 } else
01598                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMAGE, &uht, &uh, &uhc))
01599                 {
01600                     HeaderIterator hi;
01601                     int_32 tag, type, count;
01602                     hPTR_t ptr;
01603                     Header oh;
01604 
01605                     /* Load the original header from the blob. */
01606                     oh = headerCopyLoad(uh);
01607 
01608                     /* XXX this is headerCopy w/o headerReload() */
01609                     psm->oh = headerNew();
01610 
01611                     /*@-branchstate@*/
01612                     for (hi = headerInitIterator(oh);
01613                         headerNextIterator(hi, &tag, &type, &ptr, &count);
01614                         ptr = headerFreeData((void *)ptr, type))
01615                     {
01616                         if (tag == RPMTAG_ARCHIVESIZE)
01617                             noArchiveSize = 1;
01618                         if (ptr) (void) headerAddEntry(psm->oh, tag, type, ptr, count);
01619                     }
01620                     hi = headerFreeIterator(hi);
01621                     /*@=branchstate@*/
01622 
01623                     oh = headerFree(oh);
01624                     uh = hfd(uh, uht);
01625                 } else
01626                     break;      /* XXX shouldn't ever happen */
01627             }
01628 
01629             /* Retrieve type of payload compression. */
01630             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01631             rc = rpmpsmStage(psm, PSM_RPMIO_FLAGS);
01632             /*@=nullstate@*/
01633 
01634             /* Write the lead section into the package. */
01635             {   int archnum = -1;
01636                 int osnum = -1;
01637                 struct rpmlead lead;
01638 
01639 #ifndef DYING
01640                 rpmGetArchInfo(NULL, &archnum);
01641                 rpmGetOsInfo(NULL, &osnum);
01642 #endif
01643 
01644                 memset(&lead, 0, sizeof(lead));
01645                 /* XXX Set package version conditioned on noDirTokens. */
01646                 lead.major = 3;
01647                 lead.minor = 0;
01648                 lead.type = RPMLEAD_BINARY;
01649                 lead.archnum = archnum;
01650                 lead.osnum = osnum;
01651                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01652 
01653                 strncpy(lead.name, rpmteNEVR(psm->te), sizeof(lead.name));
01654 
01655                 rc = writeLead(psm->fd, &lead);
01656                 if (rc != RPMRC_OK) {
01657                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01658                          Fstrerror(psm->fd));
01659                     break;
01660                 }
01661             }
01662 
01663             /* Write the signature section into the package. */
01664             /* XXX rpm-4.1 and later has archive size in signature header. */
01665             {   Header sigh = headerRegenSigHeader(fi->h, noArchiveSize);
01666                 /* Reallocate the signature into one contiguous region. */
01667                 sigh = headerReload(sigh, RPMTAG_HEADERSIGNATURES);
01668                 if (sigh == NULL) {
01669                     rpmError(RPMERR_NOSPACE, _("Unable to reload signature header\n"));
01670                     rc = RPMRC_FAIL;
01671                     break;
01672                 }
01673                 rc = rpmWriteSignature(psm->fd, sigh);
01674                 sigh = rpmFreeSignature(sigh);
01675                 if (rc) break;
01676             }
01677 
01678             /* Add remove transaction id to header. */
01679             if (psm->oh != NULL)
01680             {   int_32 tid = rpmtsGetTid(ts);
01681                 xx = headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01682                         RPM_INT32_TYPE, &tid, 1);
01683             }
01684 
01685             /* Write the metadata section into the package. */
01686             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01687             if (rc) break;
01688         }
01689         break;
01690     case PSM_PROCESS:
01691         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01692 
01693         if (psm->goal == PSM_PKGINSTALL) {
01694             int i;
01695 
01696             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01697 
01698             /* XXX Synthesize callbacks for packages with no files. */
01699             if (rpmfiFC(fi) <= 0) {
01700                 void * ptr;
01701                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_START, 0, 100);
01702                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS, 100, 100);
01703                 break;
01704             }
01705 
01706             (void) rpmfiInit(fi, 0);
01707             while ((i = rpmfiNext(fi)) >= 0) {
01708                 uid_t uid;
01709                 gid_t gid;
01710 
01711                 uid = fi->uid;
01712                 gid = fi->gid;
01713                 if (fi->fuser && unameToUid(fi->fuser[i], &uid)) {
01714                     rpmMessage(RPMMESS_WARNING,
01715                         _("user %s does not exist - using root\n"),
01716                         fi->fuser[i]);
01717                     uid = 0;
01718                     /* XXX this diddles header memory. */
01719                     fi->fmodes[i] &= ~S_ISUID;  /* turn off the suid bit */
01720                 }
01721 
01722                 if (fi->fgroup && gnameToGid(fi->fgroup[i], &gid)) {
01723                     rpmMessage(RPMMESS_WARNING,
01724                         _("group %s does not exist - using root\n"),
01725                         fi->fgroup[i]);
01726                     gid = 0;
01727                     /* XXX this diddles header memory. */
01728                     fi->fmodes[i] &= ~S_ISGID;  /* turn off the sgid bit */
01729                 }
01730                 if (fi->fuids) fi->fuids[i] = uid;
01731                 if (fi->fgids) fi->fgids[i] = gid;
01732             }
01733 
01734             /* Retrieve type of payload compression. */
01735             rc = rpmpsmStage(psm, PSM_RPMIO_FLAGS);
01736 
01737             if (rpmteFd(fi->te) == NULL) {      /* XXX can't happen */
01738                 rc = RPMRC_FAIL;
01739                 break;
01740             }
01741 
01742             /*@-nullpass@*/     /* LCL: fi->fd != NULL here. */
01743             psm->cfd = Fdopen(fdDup(Fileno(rpmteFd(fi->te))), psm->rpmio_flags);
01744             /*@=nullpass@*/
01745             if (psm->cfd == NULL) {     /* XXX can't happen */
01746                 rc = RPMRC_FAIL;
01747                 break;
01748             }
01749 
01750             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01751                         psm->cfd, NULL, &psm->failedFile);
01752             xx = fsmTeardown(fi->fsm);
01753 
01754             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01755             xx = Fclose(psm->cfd);
01756             psm->cfd = NULL;
01757             /*@-mods@*/
01758             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01759             /*@=mods@*/
01760 
01761             if (!rc)
01762                 rc = rpmpsmStage(psm, PSM_COMMIT);
01763 
01764             /* XXX make sure progress is closed out */
01765             psm->what = RPMCALLBACK_INST_PROGRESS;
01766             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01767             psm->total = psm->amount;
01768             xx = rpmpsmStage(psm, PSM_NOTIFY);
01769 
01770             if (rc) {
01771                 rpmError(RPMERR_CPIO,
01772                         _("unpacking of archive failed%s%s: %s\n"),
01773                         (psm->failedFile != NULL ? _(" on file ") : ""),
01774                         (psm->failedFile != NULL ? psm->failedFile : ""),
01775                         cpioStrerror(rc));
01776                 rc = RPMRC_FAIL;
01777 
01778                 /* XXX notify callback on error. */
01779                 psm->what = RPMCALLBACK_UNPACK_ERROR;
01780                 psm->amount = 0;
01781                 psm->total = 0;
01782                 xx = rpmpsmStage(psm, PSM_NOTIFY);
01783 
01784                 break;
01785             }
01786         }
01787         if (psm->goal == PSM_PKGERASE) {
01788             int fc = rpmfiFC(fi);
01789 
01790             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01791             if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)       break;
01792             if (fc <= 0)                                break;
01793 
01794             psm->what = RPMCALLBACK_UNINST_START;
01795             psm->amount = fc;           /* XXX W2DO? looks wrong. */
01796             psm->total = fc;
01797             xx = rpmpsmStage(psm, PSM_NOTIFY);
01798 
01799             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01800                         NULL, NULL, &psm->failedFile);
01801             xx = fsmTeardown(fi->fsm);
01802 
01803             psm->what = RPMCALLBACK_UNINST_STOP;
01804             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01805             psm->total = fc;
01806             xx = rpmpsmStage(psm, PSM_NOTIFY);
01807 
01808         }
01809         if (psm->goal == PSM_PKGSAVE) {
01810             fileAction * actions = fi->actions;
01811             fileAction action = fi->action;
01812 
01813             fi->action = FA_COPYOUT;
01814             fi->actions = NULL;
01815 
01816             if (psm->fd == NULL) {      /* XXX can't happen */
01817                 rc = RPMRC_FAIL;
01818                 break;
01819             }
01820             /*@-nullpass@*/     /* FIX: fdDup mey return NULL. */
01821             xx = Fflush(psm->fd);
01822             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01823             /*@=nullpass@*/
01824             if (psm->cfd == NULL) {     /* XXX can't happen */
01825                 rc = RPMRC_FAIL;
01826                 break;
01827             }
01828 
01829             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01830                         NULL, &psm->failedFile);
01831             xx = fsmTeardown(fi->fsm);
01832 
01833             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01834             xx = Fclose(psm->cfd);
01835             psm->cfd = NULL;
01836             /*@-mods@*/
01837             errno = saveerrno;
01838             /*@=mods@*/
01839 
01840             /* XXX make sure progress is closed out */
01841             psm->what = RPMCALLBACK_INST_PROGRESS;
01842             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01843             psm->total = psm->amount;
01844             xx = rpmpsmStage(psm, PSM_NOTIFY);
01845 
01846             fi->action = action;
01847             fi->actions = actions;
01848         }
01849         break;
01850     case PSM_POST:
01851         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01852 
01853         if (psm->goal == PSM_PKGINSTALL) {
01854             int_32 installTime = (int_32) time(NULL);
01855             int fc = rpmfiFC(fi);
01856 
01857             if (fi->h == NULL) break;   /* XXX can't happen */
01858             if (fi->fstates != NULL && fc > 0)
01859                 xx = headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01860                                 fi->fstates, fc);
01861 
01862             xx = headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01863                                 &installTime, 1);
01864 
01865             xx = headerAddEntry(fi->h, RPMTAG_INSTALLCOLOR, RPM_INT32_TYPE,
01866                                 &tscolor, 1);
01867 
01868             /*
01869              * If this package has already been installed, remove it from
01870              * the database before adding the new one.
01871              */
01872             if (fi->record && !(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)) {
01873                 rc = rpmpsmStage(psm, PSM_RPMDB_REMOVE);
01874                 if (rc) break;
01875             }
01876 
01877             rc = rpmpsmStage(psm, PSM_RPMDB_ADD);
01878             if (rc) break;
01879 
01880             psm->scriptTag = RPMTAG_POSTIN;
01881             psm->progTag = RPMTAG_POSTINPROG;
01882             psm->sense = RPMSENSE_TRIGGERIN;
01883             psm->countCorrection = 0;
01884 
01885             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOST)) {
01886                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01887                 if (rc) break;
01888             }
01889             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERIN)) {
01890                 /* Run triggers in other package(s) this package sets off. */
01891                 rc = rpmpsmStage(psm, PSM_TRIGGERS);
01892                 if (rc) break;
01893 
01894                 /* Run triggers in this package other package(s) set off. */
01895                 rc = rpmpsmStage(psm, PSM_IMMED_TRIGGERS);
01896                 if (rc) break;
01897             }
01898 
01899             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01900                 rc = markReplacedFiles(psm);
01901 
01902         }
01903         if (psm->goal == PSM_PKGERASE) {
01904 
01905             psm->scriptTag = RPMTAG_POSTUN;
01906             psm->progTag = RPMTAG_POSTUNPROG;
01907             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01908             psm->countCorrection = -1;
01909 
01910             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOSTUN)) {
01911                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01912                 /* XXX WTFO? postun failures don't cause erasure failure. */
01913             }
01914 
01915             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01916                 /* Run triggers in other package(s) this package sets off. */
01917                 rc = rpmpsmStage(psm, PSM_TRIGGERS);
01918                 if (rc) break;
01919             }
01920 
01921             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01922                 rc = rpmpsmStage(psm, PSM_RPMDB_REMOVE);
01923         }
01924         if (psm->goal == PSM_PKGSAVE) {
01925         }
01926 
01927         /* Restore root directory if changed. */
01928         xx = rpmpsmStage(psm, PSM_CHROOT_OUT);
01929         break;
01930     case PSM_UNDO:
01931         break;
01932     case PSM_FINI:
01933         /* Restore root directory if changed. */
01934         xx = rpmpsmStage(psm, PSM_CHROOT_OUT);
01935 
01936         if (psm->fd != NULL) {
01937             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01938             xx = Fclose(psm->fd);
01939             psm->fd = NULL;
01940             /*@-mods@*/
01941             errno = saveerrno;
01942             /*@=mods@*/
01943         }
01944 
01945         if (psm->goal == PSM_PKGSAVE) {
01946             if (!rc && ts && ts->notify == NULL) {
01947                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01948                         (psm->pkgURL ? psm->pkgURL : "???"));
01949             }
01950         }
01951 
01952         if (rc) {
01953             if (psm->failedFile)
01954                 rpmError(RPMERR_CPIO,
01955                         _("%s failed on file %s: %s\n"),
01956                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01957             else
01958                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01959                         psm->stepName, cpioStrerror(rc));
01960 
01961             /* XXX notify callback on error. */
01962             psm->what = RPMCALLBACK_CPIO_ERROR;
01963             psm->amount = 0;
01964             psm->total = 0;
01965             /*@-nullstate@*/ /* FIX: psm->fd may be NULL. */
01966             xx = rpmpsmStage(psm, PSM_NOTIFY);
01967             /*@=nullstate@*/
01968         }
01969 
01970 /*@-branchstate@*/
01971         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01972 if (psm->te != NULL)
01973 if (psm->te->h != NULL)
01974 psm->te->h = headerFree(psm->te->h);
01975             if (fi->h != NULL)
01976                 fi->h = headerFree(fi->h);
01977         }
01978 /*@=branchstate@*/
01979         psm->oh = headerFree(psm->oh);
01980         psm->pkgURL = _free(psm->pkgURL);
01981         psm->rpmio_flags = _free(psm->rpmio_flags);
01982         psm->failedFile = _free(psm->failedFile);
01983 
01984         fi->fgids = _free(fi->fgids);
01985         fi->fuids = _free(fi->fuids);
01986         fi->fgroup = hfd(fi->fgroup, -1);
01987         fi->fuser = hfd(fi->fuser, -1);
01988         fi->apath = _free(fi->apath);
01989         fi->fstates = _free(fi->fstates);
01990         break;
01991 
01992     case PSM_PKGINSTALL:
01993     case PSM_PKGERASE:
01994     case PSM_PKGSAVE:
01995         psm->goal = stage;
01996         psm->rc = RPMRC_OK;
01997         psm->stepName = pkgStageString(stage);
01998 
01999         rc = rpmpsmStage(psm, PSM_INIT);
02000         if (!rc) rc = rpmpsmStage(psm, PSM_PRE);
02001         if (!rc) rc = rpmpsmStage(psm, PSM_PROCESS);
02002         if (!rc) rc = rpmpsmStage(psm, PSM_POST);
02003         xx = rpmpsmStage(psm, PSM_FINI);
02004         break;
02005     case PSM_PKGCOMMIT:
02006         break;
02007 
02008     case PSM_CREATE:
02009         break;
02010     case PSM_NOTIFY:
02011     {   void * ptr;
02012 /*@-nullpass@*/ /* FIX: psm->te may be NULL */
02013         ptr = rpmtsNotify(ts, psm->te, psm->what, psm->amount, psm->total);
02014 /*@-nullpass@*/
02015     }   break;
02016     case PSM_DESTROY:
02017         break;
02018     case PSM_COMMIT:
02019         if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_PKGCOMMIT)) break;
02020         if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY) break;
02021 
02022         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
02023                         NULL, NULL, &psm->failedFile);
02024         xx = fsmTeardown(fi->fsm);
02025         break;
02026 
02027     case PSM_CHROOT_IN:
02028     {   const char * rootDir = rpmtsRootDir(ts);
02029         /* Change root directory if requested and not already done. */
02030         if (rootDir != NULL && !rpmtsChrootDone(ts) && !psm->chrootDone) {
02031             static int _loaded = 0;
02032 
02033             /*
02034              * This loads all of the name services libraries, in case we
02035              * don't have access to them in the chroot().
02036              */
02037             if (!_loaded) {
02038                 (void)getpwnam("root");
02039                 endpwent();
02040                 _loaded++;
02041             }
02042 
02043             xx = chdir("/");
02044             /*@-superuser@*/
02045             rc = chroot(rootDir);
02046             /*@=superuser@*/
02047             psm->chrootDone = 1;
02048             (void) rpmtsSetChrootDone(ts, 1);
02049         }
02050     }   break;
02051     case PSM_CHROOT_OUT:
02052         /* Restore root directory if changed. */
02053         if (psm->chrootDone) {
02054             const char * currDir = rpmtsCurrDir(ts);
02055             /*@-superuser@*/
02056             rc = chroot(".");
02057             /*@=superuser@*/
02058             psm->chrootDone = 0;
02059             (void) rpmtsSetChrootDone(ts, 0);
02060             if (currDir != NULL)        /* XXX can't happen */
02061                 xx = chdir(currDir);
02062         }
02063         break;
02064     case PSM_SCRIPT:    /* Run current package scriptlets. */
02065         rc = runInstScript(psm);
02066         break;
02067     case PSM_TRIGGERS:
02068         /* Run triggers in other package(s) this package sets off. */
02069         rc = runTriggers(psm);
02070         break;
02071     case PSM_IMMED_TRIGGERS:
02072         /* Run triggers in this package other package(s) set off. */
02073         rc = runImmedTriggers(psm);
02074         break;
02075 
02076     case PSM_RPMIO_FLAGS:
02077     {   const char * payload_compressor = NULL;
02078         char * t;
02079 
02080         /*@-branchstate@*/
02081         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
02082                             (void **) &payload_compressor, NULL))
02083             payload_compressor = "gzip";
02084         /*@=branchstate@*/
02085         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
02086         *t = '\0';
02087         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
02088         if (!strcmp(payload_compressor, "gzip"))
02089             t = stpcpy(t, ".gzdio");
02090         if (!strcmp(payload_compressor, "bzip2"))
02091             t = stpcpy(t, ".bzdio");
02092         rc = RPMRC_OK;
02093     }   break;
02094 
02095     case PSM_RPMDB_LOAD:
02096 assert(psm->mi == NULL);
02097         psm->mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
02098                                 &fi->record, sizeof(fi->record));
02099 
02100         fi->h = rpmdbNextIterator(psm->mi);
02101         if (fi->h != NULL)
02102             fi->h = headerLink(fi->h);
02103 
02104         psm->mi = rpmdbFreeIterator(psm->mi);
02105         rc = (fi->h != NULL ? RPMRC_OK : RPMRC_FAIL);
02106         break;
02107     case PSM_RPMDB_ADD:
02108         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02109         if (fi->h == NULL)      break;  /* XXX can't happen */
02110         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02111         if (!(rpmtsVSFlags(ts) & RPMVSF_NOHDRCHK))
02112             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02113                                 ts, headerCheck);
02114         else
02115             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02116                                 NULL, NULL);
02117         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02118         break;
02119     case PSM_RPMDB_REMOVE:
02120         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02121         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02122         rc = rpmdbRemove(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->record,
02123                                 NULL, NULL);
02124         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02125         break;
02126 
02127     default:
02128         break;
02129 /*@i@*/    }
02130     /*@=branchstate@*/
02131 
02132     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
02133     return rc;
02134     /*@=nullstate@*/
02135 }
02136 /*@=bounds =nullpass@*/

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