lib/psm.c

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

Generated on Tue Feb 21 14:28:54 2012 for rpm by  doxygen 1.4.7