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 (getenv("RPM_FORCE_NPTL") != NULL) {
00958             if (getenv("OLD_LD_ASSUME_KERNEL") != NULL) {
00959                 setenv("LD_ASSUME_KERNEL", getenv("OLD_LD_ASSUME_KERNEL"), 1);
00960             }
00961         }
00962         
00963         if (prefixes != NULL)
00964         for (i = 0; i < numPrefixes; i++) {
00965             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00966             xx = doputenv(prefixBuf);
00967 
00968             /* backwards compatibility */
00969             if (i == 0) {
00970                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00971                 xx = doputenv(prefixBuf);
00972             }
00973         }
00974 
00975         rootDir = rpmtsRootDir(ts);
00976         if (rootDir  != NULL)   /* XXX can't happen */
00977         switch(urlIsURL(rootDir)) {
00978         case URL_IS_PATH:
00979             rootDir += sizeof("file://") - 1;
00980             rootDir = strchr(rootDir, '/');
00981             /*@fallthrough@*/
00982         case URL_IS_UNKNOWN:
00983             if (!rpmtsChrootDone(ts) &&
00984                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00985             {
00986                 /*@-superuser -noeffect @*/
00987                 xx = chroot(rootDir);
00988                 /*@=superuser =noeffect @*/
00989             }
00990             xx = chdir("/");
00991             rpmMessage(RPMMESS_DEBUG, _("%s: %s(%s-%s-%s)\texecv(%s) pid %d\n"),
00992                         psm->stepName, sln, n, v, r,
00993                         argv[0], (unsigned)getpid());
00994 /*@-modfilesys@*/
00995 if (_psm_debug)
00996 fprintf(stderr, "      Exec: %s \"%s\"\n", sln, argv[0]);
00997 /*@=modfilesys@*/
00998             unsetenv("MALLOC_CHECK_");
00999             xx = execv(argv[0], (char *const *)argv);
01000             break;
01001         default:
01002             break;
01003         }
01004 
01005         _exit(-1);
01006         /*@notreached@*/
01007     }
01008     /*@=branchstate@*/
01009 
01010     (void) psmWait(psm);
01011 
01012     if (psm->reaped < 0) {
01013         rpmError(RPMERR_SCRIPT,
01014                 _("%s(%s-%s-%s) scriptlet failed, waitpid(%d) rc %d: %s\n"),
01015                  sln, n, v, r, psm->child, psm->reaped, strerror(errno));
01016         rc = RPMRC_FAIL;
01017     } else
01018     if (!WIFEXITED(psm->status) || WEXITSTATUS(psm->status)) {
01019         rpmError(RPMERR_SCRIPT,
01020                 _("%s(%s-%s-%s) scriptlet failed, exit status %d\n"),
01021                 sln, n, v, r, WEXITSTATUS(psm->status));
01022         rc = RPMRC_FAIL;
01023     }
01024 
01025     if (freePrefixes) prefixes = hfd(prefixes, ipt);
01026 
01027     xx = Fclose(out);   /* XXX dup'd STDOUT_FILENO */
01028     
01029     /*@-branchstate@*/
01030     if (script) {
01031         if (!rpmIsDebug())
01032             xx = unlink(fn);
01033         fn = _free(fn);
01034     }
01035     /*@=branchstate@*/
01036 
01037     return rc;
01038 }
01039 
01045 static rpmRC runInstScript(rpmpsm psm)
01046         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
01047         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
01048 {
01049     rpmfi fi = psm->fi;
01050     HGE_t hge = fi->hge;
01051     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01052     void ** progArgv;
01053     int progArgc;
01054     const char ** argv;
01055     rpmTagType ptt, stt;
01056     const char * script;
01057     rpmRC rc = RPMRC_OK;
01058     int xx;
01059 
01060     /*
01061      * headerGetEntry() sets the data pointer to NULL if the entry does
01062      * not exist.
01063      */
01064     xx = hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
01065     xx = hge(fi->h, psm->progTag, &ptt, (void **) &progArgv, &progArgc);
01066     if (progArgv == NULL && script == NULL)
01067         goto exit;
01068 
01069     /*@-branchstate@*/
01070     if (progArgv && ptt == RPM_STRING_TYPE) {
01071         argv = alloca(sizeof(*argv));
01072         *argv = (const char *) progArgv;
01073     } else {
01074         argv = (const char **) progArgv;
01075     }
01076     /*@=branchstate@*/
01077 
01078     if (fi->h != NULL)  /* XXX can't happen */
01079     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), progArgc, argv,
01080                 script, psm->scriptArg, -1);
01081 
01082 exit:
01083     progArgv = hfd(progArgv, ptt);
01084     script = hfd(script, stt);
01085     return rc;
01086 }
01087 
01098 static rpmRC handleOneTrigger(const rpmpsm psm,
01099                         Header sourceH, Header triggeredH,
01100                         int arg2, unsigned char * triggersAlreadyRun)
01101         /*@globals rpmGlobalMacroContext, fileSystem, internalState@*/
01102         /*@modifies psm, sourceH, triggeredH, *triggersAlreadyRun,
01103                 rpmGlobalMacroContext, fileSystem, internalState @*/
01104 {
01105     int scareMem = 1;
01106     const rpmts ts = psm->ts;
01107     rpmfi fi = psm->fi;
01108     HGE_t hge = fi->hge;
01109     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01110     rpmds trigger = NULL;
01111     const char ** triggerScripts;
01112     const char ** triggerProgs;
01113     int_32 * triggerIndices;
01114     const char * sourceName;
01115     rpmRC rc = RPMRC_OK;
01116     int xx;
01117     int i;
01118 
01119     xx = headerNVR(sourceH, &sourceName, NULL, NULL);
01120 
01121     trigger = rpmdsInit(rpmdsNew(triggeredH, RPMTAG_TRIGGERNAME, scareMem));
01122     if (trigger == NULL)
01123         return rc;
01124 
01125     (void) rpmdsSetNoPromote(trigger, 1);
01126 
01127     while ((i = rpmdsNext(trigger)) >= 0) {
01128         rpmTagType tit, tst, tpt;
01129         const char * Name;
01130         int_32 Flags = rpmdsFlags(trigger);
01131 
01132         if ((Name = rpmdsN(trigger)) == NULL)
01133             continue;   /* XXX can't happen */
01134 
01135         if (strcmp(Name, sourceName))
01136             continue;
01137         if (!(Flags & psm->sense))
01138             continue;
01139 
01140         /*
01141          * XXX Trigger on any provided dependency, not just the package NEVR.
01142          */
01143         if (!rpmdsAnyMatchesDep(sourceH, trigger, 1))
01144             continue;
01145 
01146         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
01147                        (void **) &triggerIndices, NULL) &&
01148                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
01149                        (void **) &triggerScripts, NULL) &&
01150                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
01151                        (void **) &triggerProgs, NULL))
01152             )
01153             continue;
01154 
01155         {   int arg1;
01156             int index;
01157 
01158             arg1 = rpmdbCountPackages(rpmtsGetRdb(ts), Name);
01159             if (arg1 < 0) {
01160                 /* XXX W2DO? fails as "execution of script failed" */
01161                 rc = RPMRC_FAIL;
01162             } else {
01163                 arg1 += psm->countCorrection;
01164                 index = triggerIndices[i];
01165                 if (triggersAlreadyRun == NULL ||
01166                     triggersAlreadyRun[index] == 0)
01167                 {
01168                     rc = runScript(psm, triggeredH, "%trigger", 1,
01169                             triggerProgs + index, triggerScripts[index], 
01170                             arg1, arg2);
01171                     if (triggersAlreadyRun != NULL)
01172                         triggersAlreadyRun[index] = 1;
01173                 }
01174             }
01175         }
01176 
01177         triggerIndices = hfd(triggerIndices, tit);
01178         triggerScripts = hfd(triggerScripts, tst);
01179         triggerProgs = hfd(triggerProgs, tpt);
01180 
01181         /*
01182          * Each target/source header pair can only result in a single
01183          * script being run.
01184          */
01185         break;
01186     }
01187 
01188     trigger = rpmdsFree(trigger);
01189 
01190     return rc;
01191 }
01192 
01198 static rpmRC runTriggers(rpmpsm psm)
01199         /*@globals rpmGlobalMacroContext,
01200                 fileSystem, internalState @*/
01201         /*@modifies psm, rpmGlobalMacroContext,
01202                 fileSystem, internalState @*/
01203 {
01204     const rpmts ts = psm->ts;
01205     rpmfi fi = psm->fi;
01206     int numPackage = -1;
01207     rpmRC rc = RPMRC_OK;
01208     const char * N = NULL;
01209 
01210     if (psm->te)        /* XXX can't happen */
01211         N = rpmteN(psm->te);
01212     if (N)              /* XXX can't happen */
01213         numPackage = rpmdbCountPackages(rpmtsGetRdb(ts), N)
01214                                 + psm->countCorrection;
01215     if (numPackage < 0)
01216         return RPMRC_NOTFOUND;
01217 
01218     if (fi != NULL && fi->h != NULL)    /* XXX can't happen */
01219     {   Header triggeredH;
01220         rpmdbMatchIterator mi;
01221         int countCorrection = psm->countCorrection;
01222 
01223         psm->countCorrection = 0;
01224         mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, N, 0);
01225         while((triggeredH = rpmdbNextIterator(mi)) != NULL)
01226             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01227         mi = rpmdbFreeIterator(mi);
01228         psm->countCorrection = countCorrection;
01229     }
01230 
01231     return rc;
01232 }
01233 
01239 static rpmRC runImmedTriggers(rpmpsm psm)
01240         /*@globals rpmGlobalMacroContext,
01241                 fileSystem, internalState @*/
01242         /*@modifies psm, rpmGlobalMacroContext,
01243                 fileSystem, internalState @*/
01244 {
01245     const rpmts ts = psm->ts;
01246     rpmfi fi = psm->fi;
01247     HGE_t hge = fi->hge;
01248     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01249     const char ** triggerNames;
01250     int numTriggers;
01251     int_32 * triggerIndices;
01252     rpmTagType tnt, tit;
01253     int numTriggerIndices;
01254     unsigned char * triggersRun;
01255     rpmRC rc = RPMRC_OK;
01256 
01257     if (fi->h == NULL)  return rc;      /* XXX can't happen */
01258 
01259     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01260                         (void **) &triggerNames, &numTriggers) &&
01261                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01262                         (void **) &triggerIndices, &numTriggerIndices))
01263         )
01264         return rc;
01265 
01266     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01267     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01268 
01269     {   Header sourceH = NULL;
01270         int i;
01271 
01272         for (i = 0; i < numTriggers; i++) {
01273             rpmdbMatchIterator mi;
01274 
01275             if (triggersRun[triggerIndices[i]] != 0) continue;
01276         
01277             mi = rpmtsInitIterator(ts, RPMTAG_NAME, triggerNames[i], 0);
01278 
01279             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01280                 rc |= handleOneTrigger(psm, sourceH, fi->h, 
01281                                 rpmdbGetIteratorCount(mi),
01282                                 triggersRun);
01283             }
01284 
01285             mi = rpmdbFreeIterator(mi);
01286         }
01287     }
01288     triggerIndices = hfd(triggerIndices, tit);
01289     triggerNames = hfd(triggerNames, tnt);
01290     return rc;
01291 }
01292 
01293 /*@observer@*/ static const char *const pkgStageString(pkgStage a)
01294         /*@*/
01295 {
01296     switch(a) {
01297     case PSM_UNKNOWN:           return "unknown";
01298 
01299     case PSM_PKGINSTALL:        return "  install";
01300     case PSM_PKGERASE:          return "    erase";
01301     case PSM_PKGCOMMIT:         return "   commit";
01302     case PSM_PKGSAVE:           return "repackage";
01303 
01304     case PSM_INIT:              return "init";
01305     case PSM_PRE:               return "pre";
01306     case PSM_PROCESS:           return "process";
01307     case PSM_POST:              return "post";
01308     case PSM_UNDO:              return "undo";
01309     case PSM_FINI:              return "fini";
01310 
01311     case PSM_CREATE:            return "create";
01312     case PSM_NOTIFY:            return "notify";
01313     case PSM_DESTROY:           return "destroy";
01314     case PSM_COMMIT:            return "commit";
01315 
01316     case PSM_CHROOT_IN:         return "chrootin";
01317     case PSM_CHROOT_OUT:        return "chrootout";
01318     case PSM_SCRIPT:            return "script";
01319     case PSM_TRIGGERS:          return "triggers";
01320     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01321 
01322     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01323 
01324     case PSM_RPMDB_LOAD:        return "rpmdbload";
01325     case PSM_RPMDB_ADD:         return "rpmdbadd";
01326     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01327 
01328     default:                    return "???";
01329     }
01330     /*@noteached@*/
01331 }
01332 
01333 rpmpsm XrpmpsmUnlink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01334 {
01335     if (psm == NULL) return NULL;
01336 /*@-modfilesys@*/
01337 if (_psm_debug && msg != NULL)
01338 fprintf(stderr, "--> psm %p -- %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01339 /*@=modfilesys@*/
01340     psm->nrefs--;
01341     return NULL;
01342 }
01343 
01344 rpmpsm XrpmpsmLink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01345 {
01346     if (psm == NULL) return NULL;
01347     psm->nrefs++;
01348 
01349 /*@-modfilesys@*/
01350 if (_psm_debug && msg != NULL)
01351 fprintf(stderr, "--> psm %p ++ %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01352 /*@=modfilesys@*/
01353 
01354     /*@-refcounttrans@*/ return psm; /*@=refcounttrans@*/
01355 }
01356 
01357 rpmpsm rpmpsmFree(rpmpsm psm)
01358 {
01359     const char * msg = "rpmpsmFree";
01360     if (psm == NULL)
01361         return NULL;
01362 
01363     if (psm->nrefs > 1)
01364         return rpmpsmUnlink(psm, msg);
01365 
01366 /*@-nullstate@*/
01367     psm->fi = rpmfiFree(psm->fi);
01368 #ifdef  NOTYET
01369     psm->te = rpmteFree(psm->te);
01370 #else
01371     psm->te = NULL;
01372 #endif
01373     psm->ts = rpmtsFree(psm->ts);
01374 
01375     (void) rpmpsmUnlink(psm, msg);
01376 
01377     /*@-refcounttrans -usereleased@*/
01378 /*@-boundswrite@*/
01379     memset(psm, 0, sizeof(*psm));               /* XXX trash and burn */
01380 /*@=boundswrite@*/
01381     psm = _free(psm);
01382     /*@=refcounttrans =usereleased@*/
01383 
01384     return NULL;
01385 /*@=nullstate@*/
01386 }
01387 
01388 rpmpsm rpmpsmNew(rpmts ts, rpmte te, rpmfi fi)
01389 {
01390     const char * msg = "rpmpsmNew";
01391     rpmpsm psm = xcalloc(1, sizeof(*psm));
01392 
01393     if (ts)     psm->ts = rpmtsLink(ts, msg);
01394 #ifdef  NOTYET
01395     if (te)     psm->te = rpmteLink(te, msg);
01396 #else
01397 /*@-assignexpose -temptrans @*/
01398     if (te)     psm->te = te;
01399 /*@=assignexpose =temptrans @*/
01400 #endif
01401     if (fi)     psm->fi = rpmfiLink(fi, msg);
01402 
01403     return rpmpsmLink(psm, msg);
01404 }
01405 
01410 /*@-bounds -nullpass@*/ /* FIX: testing null annotation for fi->h */
01411 rpmRC rpmpsmStage(rpmpsm psm, pkgStage stage)
01412 {
01413     const rpmts ts = psm->ts;
01414     uint_32 tscolor = rpmtsColor(ts);
01415     rpmfi fi = psm->fi;
01416     HGE_t hge = fi->hge;
01417     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01418     rpmRC rc = psm->rc;
01419     int saveerrno;
01420     int xx;
01421 
01422     /*@-branchstate@*/
01423     switch (stage) {
01424     case PSM_UNKNOWN:
01425         break;
01426     case PSM_INIT:
01427         rpmMessage(RPMMESS_DEBUG, _("%s: %s has %d files, test = %d\n"),
01428                 psm->stepName, rpmteNEVR(psm->te),
01429                 rpmfiFC(fi), (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST));
01430 
01431         /*
01432          * When we run scripts, we pass an argument which is the number of 
01433          * versions of this package that will be installed when we are
01434          * finished.
01435          */
01436         psm->npkgs_installed = rpmdbCountPackages(rpmtsGetRdb(ts), rpmteN(psm->te));
01437         if (psm->npkgs_installed < 0) {
01438             rc = RPMRC_FAIL;
01439             break;
01440         }
01441 
01442         if (psm->goal == PSM_PKGINSTALL) {
01443             int fc = rpmfiFC(fi);
01444 
01445             psm->scriptArg = psm->npkgs_installed + 1;
01446 
01447 assert(psm->mi == NULL);
01448             psm->mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(psm->te), 0);
01449             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_EPOCH, RPMMIRE_DEFAULT,
01450                         rpmteE(psm->te));
01451             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
01452                         rpmteV(psm->te));
01453             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
01454                         rpmteR(psm->te));
01455             if (tscolor) {
01456                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_ARCH, RPMMIRE_DEFAULT,
01457                         rpmteA(psm->te));
01458                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_OS, RPMMIRE_DEFAULT,
01459                         rpmteO(psm->te));
01460             }
01461 
01462             while ((psm->oh = rpmdbNextIterator(psm->mi)) != NULL) {
01463                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01464                 psm->oh = NULL;
01465                 /*@loopbreak@*/ break;
01466             }
01467             psm->mi = rpmdbFreeIterator(psm->mi);
01468             rc = RPMRC_OK;
01469 
01470             /* XXX lazy alloc here may need to be done elsewhere. */
01471             if (fi->fstates == NULL && fc > 0) {
01472                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fc);
01473                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fc);
01474             }
01475 
01476             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01477             if (fc <= 0)                                break;
01478         
01479             /*
01480              * Old format relocateable packages need the entire default
01481              * prefix stripped to form the cpio list, while all other packages
01482              * need the leading / stripped.
01483              */
01484             {   const char * p;
01485                 xx = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01486                 fi->striplen = (xx ? strlen(p) + 1 : 1); 
01487             }
01488             fi->mapflags =
01489                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01490         
01491             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01492                 rpmfiBuildFNames(fi->h, RPMTAG_ORIGBASENAMES, &fi->apath, NULL);
01493             else
01494                 rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
01495         
01496             if (fi->fuser == NULL)
01497                 xx = hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01498                                 (void **) &fi->fuser, NULL);
01499             if (fi->fgroup == NULL)
01500                 xx = hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01501                                 (void **) &fi->fgroup, NULL);
01502             if (fi->fuids == NULL)
01503                 fi->fuids = xcalloc(sizeof(*fi->fuids), fc);
01504             if (fi->fgids == NULL)
01505                 fi->fgids = xcalloc(sizeof(*fi->fgids), fc);
01506             rc = RPMRC_OK;
01507         }
01508         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01509             psm->scriptArg = psm->npkgs_installed - 1;
01510         
01511             /* Retrieve installed header. */
01512             rc = rpmpsmStage(psm, PSM_RPMDB_LOAD);
01513 if (rc == RPMRC_OK)
01514 if (psm->te)
01515 psm->te->h = headerLink(fi->h);
01516         }
01517         if (psm->goal == PSM_PKGSAVE) {
01518             /* Open output package for writing. */
01519             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01520                 const char * pkgbn =
01521                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01522 
01523                 bfmt = _free(bfmt);
01524                 psm->pkgURL = rpmGenPath("%{?_repackage_root}",
01525                                          "%{?_repackage_dir}",
01526                                         pkgbn);
01527                 pkgbn = _free(pkgbn);
01528                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01529                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01530                 if (psm->fd == NULL || Ferror(psm->fd)) {
01531                     rc = RPMRC_FAIL;
01532                     break;
01533                 }
01534             }
01535         }
01536         break;
01537     case PSM_PRE:
01538         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01539 
01540 /* XXX insure that trigger index is opened before entering chroot. */
01541 #ifdef  NOTYET
01542  { static int oneshot = 0;
01543    dbiIndex dbi;
01544    if (!oneshot) {
01545      dbi = dbiOpen(rpmtsGetRdb(ts), RPMTAG_TRIGGERNAME, 0);
01546      oneshot++;
01547    }
01548  }
01549 #endif
01550 
01551         /* Change root directory if requested and not already done. */
01552         rc = rpmpsmStage(psm, PSM_CHROOT_IN);
01553 
01554         if (psm->goal == PSM_PKGINSTALL) {
01555             psm->scriptTag = RPMTAG_PREIN;
01556             psm->progTag = RPMTAG_PREINPROG;
01557 
01558             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01559                 /* XXX FIXME: implement %triggerprein. */
01560             }
01561 
01562             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPRE)) {
01563                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01564                 if (rc != RPMRC_OK) {
01565                     rpmError(RPMERR_SCRIPT,
01566                         _("%s: %s scriptlet failed (%d), skipping %s\n"),
01567                         psm->stepName, tag2sln(psm->scriptTag), rc,
01568                         rpmteNEVR(psm->te));
01569                     break;
01570                 }
01571             }
01572         }
01573 
01574         if (psm->goal == PSM_PKGERASE) {
01575             psm->scriptTag = RPMTAG_PREUN;
01576             psm->progTag = RPMTAG_PREUNPROG;
01577             psm->sense = RPMSENSE_TRIGGERUN;
01578             psm->countCorrection = -1;
01579 
01580             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERUN)) {
01581                 /* Run triggers in this package other package(s) set off. */
01582                 rc = rpmpsmStage(psm, PSM_IMMED_TRIGGERS);
01583                 if (rc) break;
01584 
01585                 /* Run triggers in other package(s) this package sets off. */
01586                 rc = rpmpsmStage(psm, PSM_TRIGGERS);
01587                 if (rc) break;
01588             }
01589 
01590             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPREUN))
01591                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01592         }
01593         if (psm->goal == PSM_PKGSAVE) {
01594             int noArchiveSize = 0;
01595 
01596             /* Regenerate original header. */
01597             {   void * uh = NULL;
01598                 int_32 uht, uhc;
01599 
01600                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01601                     psm->oh = headerCopyLoad(uh);
01602                     uh = hfd(uh, uht);
01603                 } else
01604                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMAGE, &uht, &uh, &uhc))
01605                 {
01606                     HeaderIterator hi;
01607                     int_32 tag, type, count;
01608                     hPTR_t ptr;
01609                     Header oh;
01610 
01611                     /* Load the original header from the blob. */
01612                     oh = headerCopyLoad(uh);
01613 
01614                     /* XXX this is headerCopy w/o headerReload() */
01615                     psm->oh = headerNew();
01616 
01617                     /*@-branchstate@*/
01618                     for (hi = headerInitIterator(oh);
01619                         headerNextIterator(hi, &tag, &type, &ptr, &count);
01620                         ptr = headerFreeData((void *)ptr, type))
01621                     {
01622                         if (tag == RPMTAG_ARCHIVESIZE)
01623                             noArchiveSize = 1;
01624                         if (ptr) (void) headerAddEntry(psm->oh, tag, type, ptr, count);
01625                     }
01626                     hi = headerFreeIterator(hi);
01627                     /*@=branchstate@*/
01628 
01629                     oh = headerFree(oh);
01630                     uh = hfd(uh, uht);
01631                 } else
01632                     break;      /* XXX shouldn't ever happen */
01633             }
01634 
01635             /* Retrieve type of payload compression. */
01636             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01637             rc = rpmpsmStage(psm, PSM_RPMIO_FLAGS);
01638             /*@=nullstate@*/
01639 
01640             /* Write the lead section into the package. */
01641             {   int archnum = -1;
01642                 int osnum = -1;
01643                 struct rpmlead lead;
01644 
01645 #ifndef DYING
01646                 rpmGetArchInfo(NULL, &archnum);
01647                 rpmGetOsInfo(NULL, &osnum);
01648 #endif
01649 
01650                 memset(&lead, 0, sizeof(lead));
01651                 /* XXX Set package version conditioned on noDirTokens. */
01652                 lead.major = 3;
01653                 lead.minor = 0;
01654                 lead.type = RPMLEAD_BINARY;
01655                 lead.archnum = archnum;
01656                 lead.osnum = osnum;
01657                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01658 
01659                 strncpy(lead.name, rpmteNEVR(psm->te), sizeof(lead.name));
01660 
01661                 rc = writeLead(psm->fd, &lead);
01662                 if (rc != RPMRC_OK) {
01663                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01664                          Fstrerror(psm->fd));
01665                     break;
01666                 }
01667             }
01668 
01669             /* Write the signature section into the package. */
01670             /* XXX rpm-4.1 and later has archive size in signature header. */
01671             {   Header sigh = headerRegenSigHeader(fi->h, noArchiveSize);
01672                 /* Reallocate the signature into one contiguous region. */
01673                 sigh = headerReload(sigh, RPMTAG_HEADERSIGNATURES);
01674                 if (sigh == NULL) {
01675                     rpmError(RPMERR_NOSPACE, _("Unable to reload signature header\n"));
01676                     rc = RPMRC_FAIL;
01677                     break;
01678                 }
01679                 rc = rpmWriteSignature(psm->fd, sigh);
01680                 sigh = rpmFreeSignature(sigh);
01681                 if (rc) break;
01682             }
01683 
01684             /* Add remove transaction id to header. */
01685             if (psm->oh != NULL)
01686             {   int_32 tid = rpmtsGetTid(ts);
01687                 xx = headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01688                         RPM_INT32_TYPE, &tid, 1);
01689             }
01690 
01691             /* Write the metadata section into the package. */
01692             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01693             if (rc) break;
01694         }
01695         break;
01696     case PSM_PROCESS:
01697         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01698 
01699         if (psm->goal == PSM_PKGINSTALL) {
01700             int i;
01701 
01702             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01703 
01704             /* XXX Synthesize callbacks for packages with no files. */
01705             if (rpmfiFC(fi) <= 0) {
01706                 void * ptr;
01707                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_START, 0, 100);
01708                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS, 100, 100);
01709                 break;
01710             }
01711 
01712             (void) rpmfiInit(fi, 0);
01713             while ((i = rpmfiNext(fi)) >= 0) {
01714                 uid_t uid;
01715                 gid_t gid;
01716 
01717                 uid = fi->uid;
01718                 gid = fi->gid;
01719                 if (fi->fuser && unameToUid(fi->fuser[i], &uid)) {
01720                     rpmMessage(RPMMESS_WARNING,
01721                         _("user %s does not exist - using root\n"),
01722                         fi->fuser[i]);
01723                     uid = 0;
01724                     /* XXX this diddles header memory. */
01725                     fi->fmodes[i] &= ~S_ISUID;  /* turn off the suid bit */
01726                 }
01727 
01728                 if (fi->fgroup && gnameToGid(fi->fgroup[i], &gid)) {
01729                     rpmMessage(RPMMESS_WARNING,
01730                         _("group %s does not exist - using root\n"),
01731                         fi->fgroup[i]);
01732                     gid = 0;
01733                     /* XXX this diddles header memory. */
01734                     fi->fmodes[i] &= ~S_ISGID;  /* turn off the sgid bit */
01735                 }
01736                 if (fi->fuids) fi->fuids[i] = uid;
01737                 if (fi->fgids) fi->fgids[i] = gid;
01738             }
01739 
01740             /* Retrieve type of payload compression. */
01741             rc = rpmpsmStage(psm, PSM_RPMIO_FLAGS);
01742 
01743             if (rpmteFd(fi->te) == NULL) {      /* XXX can't happen */
01744                 rc = RPMRC_FAIL;
01745                 break;
01746             }
01747 
01748             /*@-nullpass@*/     /* LCL: fi->fd != NULL here. */
01749             psm->cfd = Fdopen(fdDup(Fileno(rpmteFd(fi->te))), psm->rpmio_flags);
01750             /*@=nullpass@*/
01751             if (psm->cfd == NULL) {     /* XXX can't happen */
01752                 rc = RPMRC_FAIL;
01753                 break;
01754             }
01755 
01756             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01757                         psm->cfd, NULL, &psm->failedFile);
01758             xx = fsmTeardown(fi->fsm);
01759 
01760             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01761             xx = Fclose(psm->cfd);
01762             psm->cfd = NULL;
01763             /*@-mods@*/
01764             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01765             /*@=mods@*/
01766 
01767             if (!rc)
01768                 rc = rpmpsmStage(psm, PSM_COMMIT);
01769 
01770             /* XXX make sure progress is closed out */
01771             psm->what = RPMCALLBACK_INST_PROGRESS;
01772             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01773             psm->total = psm->amount;
01774             xx = rpmpsmStage(psm, PSM_NOTIFY);
01775 
01776             if (rc) {
01777                 rpmError(RPMERR_CPIO,
01778                         _("unpacking of archive failed%s%s: %s\n"),
01779                         (psm->failedFile != NULL ? _(" on file ") : ""),
01780                         (psm->failedFile != NULL ? psm->failedFile : ""),
01781                         cpioStrerror(rc));
01782                 rc = RPMRC_FAIL;
01783 
01784                 /* XXX notify callback on error. */
01785                 psm->what = RPMCALLBACK_UNPACK_ERROR;
01786                 psm->amount = 0;
01787                 psm->total = 0;
01788                 xx = rpmpsmStage(psm, PSM_NOTIFY);
01789 
01790                 break;
01791             }
01792         }
01793         if (psm->goal == PSM_PKGERASE) {
01794             int fc = rpmfiFC(fi);
01795 
01796             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01797             if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)       break;
01798             if (fc <= 0)                                break;
01799 
01800             psm->what = RPMCALLBACK_UNINST_START;
01801             psm->amount = fc;           /* XXX W2DO? looks wrong. */
01802             psm->total = fc;
01803             xx = rpmpsmStage(psm, PSM_NOTIFY);
01804 
01805             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01806                         NULL, NULL, &psm->failedFile);
01807             xx = fsmTeardown(fi->fsm);
01808 
01809             psm->what = RPMCALLBACK_UNINST_STOP;
01810             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01811             psm->total = fc;
01812             xx = rpmpsmStage(psm, PSM_NOTIFY);
01813 
01814         }
01815         if (psm->goal == PSM_PKGSAVE) {
01816             fileAction * actions = fi->actions;
01817             fileAction action = fi->action;
01818 
01819             fi->action = FA_COPYOUT;
01820             fi->actions = NULL;
01821 
01822             if (psm->fd == NULL) {      /* XXX can't happen */
01823                 rc = RPMRC_FAIL;
01824                 break;
01825             }
01826             /*@-nullpass@*/     /* FIX: fdDup mey return NULL. */
01827             xx = Fflush(psm->fd);
01828             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01829             /*@=nullpass@*/
01830             if (psm->cfd == NULL) {     /* XXX can't happen */
01831                 rc = RPMRC_FAIL;
01832                 break;
01833             }
01834 
01835             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01836                         NULL, &psm->failedFile);
01837             xx = fsmTeardown(fi->fsm);
01838 
01839             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01840             xx = Fclose(psm->cfd);
01841             psm->cfd = NULL;
01842             /*@-mods@*/
01843             errno = saveerrno;
01844             /*@=mods@*/
01845 
01846             /* XXX make sure progress is closed out */
01847             psm->what = RPMCALLBACK_INST_PROGRESS;
01848             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01849             psm->total = psm->amount;
01850             xx = rpmpsmStage(psm, PSM_NOTIFY);
01851 
01852             fi->action = action;
01853             fi->actions = actions;
01854         }
01855         break;
01856     case PSM_POST:
01857         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01858 
01859         if (psm->goal == PSM_PKGINSTALL) {
01860             int_32 installTime = (int_32) time(NULL);
01861             int fc = rpmfiFC(fi);
01862 
01863             if (fi->h == NULL) break;   /* XXX can't happen */
01864             if (fi->fstates != NULL && fc > 0)
01865                 xx = headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01866                                 fi->fstates, fc);
01867 
01868             xx = headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01869                                 &installTime, 1);
01870 
01871             xx = headerAddEntry(fi->h, RPMTAG_INSTALLCOLOR, RPM_INT32_TYPE,
01872                                 &tscolor, 1);
01873 
01874             /*
01875              * If this package has already been installed, remove it from
01876              * the database before adding the new one.
01877              */
01878             if (fi->record && !(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)) {
01879                 rc = rpmpsmStage(psm, PSM_RPMDB_REMOVE);
01880                 if (rc) break;
01881             }
01882 
01883             rc = rpmpsmStage(psm, PSM_RPMDB_ADD);
01884             if (rc) break;
01885 
01886             psm->scriptTag = RPMTAG_POSTIN;
01887             psm->progTag = RPMTAG_POSTINPROG;
01888             psm->sense = RPMSENSE_TRIGGERIN;
01889             psm->countCorrection = 0;
01890 
01891             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOST)) {
01892                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01893                 if (rc) break;
01894             }
01895             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERIN)) {
01896                 /* Run triggers in other package(s) this package sets off. */
01897                 rc = rpmpsmStage(psm, PSM_TRIGGERS);
01898                 if (rc) break;
01899 
01900                 /* Run triggers in this package other package(s) set off. */
01901                 rc = rpmpsmStage(psm, PSM_IMMED_TRIGGERS);
01902                 if (rc) break;
01903             }
01904 
01905             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01906                 rc = markReplacedFiles(psm);
01907 
01908         }
01909         if (psm->goal == PSM_PKGERASE) {
01910 
01911             psm->scriptTag = RPMTAG_POSTUN;
01912             psm->progTag = RPMTAG_POSTUNPROG;
01913             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01914             psm->countCorrection = -1;
01915 
01916             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOSTUN)) {
01917                 rc = rpmpsmStage(psm, PSM_SCRIPT);
01918                 /* XXX WTFO? postun failures don't cause erasure failure. */
01919             }
01920 
01921             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01922                 /* Run triggers in other package(s) this package sets off. */
01923                 rc = rpmpsmStage(psm, PSM_TRIGGERS);
01924                 if (rc) break;
01925             }
01926 
01927             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01928                 rc = rpmpsmStage(psm, PSM_RPMDB_REMOVE);
01929         }
01930         if (psm->goal == PSM_PKGSAVE) {
01931         }
01932 
01933         /* Restore root directory if changed. */
01934         xx = rpmpsmStage(psm, PSM_CHROOT_OUT);
01935         break;
01936     case PSM_UNDO:
01937         break;
01938     case PSM_FINI:
01939         /* Restore root directory if changed. */
01940         xx = rpmpsmStage(psm, PSM_CHROOT_OUT);
01941 
01942         if (psm->fd != NULL) {
01943             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01944             xx = Fclose(psm->fd);
01945             psm->fd = NULL;
01946             /*@-mods@*/
01947             errno = saveerrno;
01948             /*@=mods@*/
01949         }
01950 
01951         if (psm->goal == PSM_PKGSAVE) {
01952             if (!rc && ts && ts->notify == NULL) {
01953                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01954                         (psm->pkgURL ? psm->pkgURL : "???"));
01955             }
01956         }
01957 
01958         if (rc) {
01959             if (psm->failedFile)
01960                 rpmError(RPMERR_CPIO,
01961                         _("%s failed on file %s: %s\n"),
01962                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01963             else
01964                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01965                         psm->stepName, cpioStrerror(rc));
01966 
01967             /* XXX notify callback on error. */
01968             psm->what = RPMCALLBACK_CPIO_ERROR;
01969             psm->amount = 0;
01970             psm->total = 0;
01971             /*@-nullstate@*/ /* FIX: psm->fd may be NULL. */
01972             xx = rpmpsmStage(psm, PSM_NOTIFY);
01973             /*@=nullstate@*/
01974         }
01975 
01976 /*@-branchstate@*/
01977         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01978 if (psm->te != NULL)
01979 if (psm->te->h != NULL)
01980 psm->te->h = headerFree(psm->te->h);
01981             if (fi->h != NULL)
01982                 fi->h = headerFree(fi->h);
01983         }
01984 /*@=branchstate@*/
01985         psm->oh = headerFree(psm->oh);
01986         psm->pkgURL = _free(psm->pkgURL);
01987         psm->rpmio_flags = _free(psm->rpmio_flags);
01988         psm->failedFile = _free(psm->failedFile);
01989 
01990         fi->fgids = _free(fi->fgids);
01991         fi->fuids = _free(fi->fuids);
01992         fi->fgroup = hfd(fi->fgroup, -1);
01993         fi->fuser = hfd(fi->fuser, -1);
01994         fi->apath = _free(fi->apath);
01995         fi->fstates = _free(fi->fstates);
01996         break;
01997 
01998     case PSM_PKGINSTALL:
01999     case PSM_PKGERASE:
02000     case PSM_PKGSAVE:
02001         psm->goal = stage;
02002         psm->rc = RPMRC_OK;
02003         psm->stepName = pkgStageString(stage);
02004 
02005         rc = rpmpsmStage(psm, PSM_INIT);
02006         if (!rc) rc = rpmpsmStage(psm, PSM_PRE);
02007         if (!rc) rc = rpmpsmStage(psm, PSM_PROCESS);
02008         if (!rc) rc = rpmpsmStage(psm, PSM_POST);
02009         xx = rpmpsmStage(psm, PSM_FINI);
02010         break;
02011     case PSM_PKGCOMMIT:
02012         break;
02013 
02014     case PSM_CREATE:
02015         break;
02016     case PSM_NOTIFY:
02017     {   void * ptr;
02018 /*@-nullpass@*/ /* FIX: psm->te may be NULL */
02019         ptr = rpmtsNotify(ts, psm->te, psm->what, psm->amount, psm->total);
02020 /*@-nullpass@*/
02021     }   break;
02022     case PSM_DESTROY:
02023         break;
02024     case PSM_COMMIT:
02025         if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_PKGCOMMIT)) break;
02026         if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY) break;
02027 
02028         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
02029                         NULL, NULL, &psm->failedFile);
02030         xx = fsmTeardown(fi->fsm);
02031         break;
02032 
02033     case PSM_CHROOT_IN:
02034     {   const char * rootDir = rpmtsRootDir(ts);
02035         /* Change root directory if requested and not already done. */
02036         if (rootDir != NULL && !rpmtsChrootDone(ts) && !psm->chrootDone) {
02037             static int _loaded = 0;
02038 
02039             /*
02040              * This loads all of the name services libraries, in case we
02041              * don't have access to them in the chroot().
02042              */
02043             if (!_loaded) {
02044                 (void)getpwnam("root");
02045                 endpwent();
02046                 _loaded++;
02047             }
02048 
02049             xx = chdir("/");
02050             /*@-superuser@*/
02051             rc = chroot(rootDir);
02052             /*@=superuser@*/
02053             psm->chrootDone = 1;
02054             (void) rpmtsSetChrootDone(ts, 1);
02055         }
02056     }   break;
02057     case PSM_CHROOT_OUT:
02058         /* Restore root directory if changed. */
02059         if (psm->chrootDone) {
02060             const char * currDir = rpmtsCurrDir(ts);
02061             /*@-superuser@*/
02062             rc = chroot(".");
02063             /*@=superuser@*/
02064             psm->chrootDone = 0;
02065             (void) rpmtsSetChrootDone(ts, 0);
02066             if (currDir != NULL)        /* XXX can't happen */
02067                 xx = chdir(currDir);
02068         }
02069         break;
02070     case PSM_SCRIPT:    /* Run current package scriptlets. */
02071         rc = runInstScript(psm);
02072         break;
02073     case PSM_TRIGGERS:
02074         /* Run triggers in other package(s) this package sets off. */
02075         rc = runTriggers(psm);
02076         break;
02077     case PSM_IMMED_TRIGGERS:
02078         /* Run triggers in this package other package(s) set off. */
02079         rc = runImmedTriggers(psm);
02080         break;
02081 
02082     case PSM_RPMIO_FLAGS:
02083     {   const char * payload_compressor = NULL;
02084         char * t;
02085 
02086         /*@-branchstate@*/
02087         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
02088                             (void **) &payload_compressor, NULL))
02089             payload_compressor = "gzip";
02090         /*@=branchstate@*/
02091         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
02092         *t = '\0';
02093         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
02094         if (!strcmp(payload_compressor, "gzip"))
02095             t = stpcpy(t, ".gzdio");
02096         if (!strcmp(payload_compressor, "bzip2"))
02097             t = stpcpy(t, ".bzdio");
02098         rc = RPMRC_OK;
02099     }   break;
02100 
02101     case PSM_RPMDB_LOAD:
02102 assert(psm->mi == NULL);
02103         psm->mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
02104                                 &fi->record, sizeof(fi->record));
02105 
02106         fi->h = rpmdbNextIterator(psm->mi);
02107         if (fi->h != NULL)
02108             fi->h = headerLink(fi->h);
02109 
02110         psm->mi = rpmdbFreeIterator(psm->mi);
02111         rc = (fi->h != NULL ? RPMRC_OK : RPMRC_FAIL);
02112         break;
02113     case PSM_RPMDB_ADD:
02114         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02115         if (fi->h == NULL)      break;  /* XXX can't happen */
02116         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02117         if (!(rpmtsVSFlags(ts) & RPMVSF_NOHDRCHK))
02118             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02119                                 ts, headerCheck);
02120         else
02121             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02122                                 NULL, NULL);
02123         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02124         break;
02125     case PSM_RPMDB_REMOVE:
02126         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02127         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02128         rc = rpmdbRemove(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->record,
02129                                 NULL, NULL);
02130         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02131         break;
02132 
02133     default:
02134         break;
02135 /*@i@*/    }
02136     /*@=branchstate@*/
02137 
02138     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
02139     return rc;
02140     /*@=nullstate@*/
02141 }
02142 /*@=bounds =nullpass@*/

Generated on Wed Mar 17 14:58:28 2004 for rpm by doxygen1.2.18