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 #ifdef WITH_LUA
00498 
00501 static rpmRC runLuaScript(rpmpsm psm, Header h, const char *sln,
00502                    int progArgc, const char **progArgv,
00503                    const char *script, int arg1, int arg2)
00504         /*@globals fileSystem, internalState @*/
00505         /*@modifies psm, fileSystem, internalState @*/
00506 {
00507     const rpmts ts = psm->ts;
00508     int rootFd = -1;
00509     const char *n, *v, *r;
00510     rpmRC rc = RPMRC_OK;
00511     int i;
00512     int xx;
00513     rpmlua lua = NULL; /* Global state. */
00514     rpmluav var;
00515 
00516     xx = headerNVR(h, &n, &v, &r);
00517 
00518     if (!rpmtsChrootDone(ts)) {
00519         const char *rootDir = rpmtsRootDir(ts);
00520         xx = chdir("/");
00521 /*@-nullpass@*/
00522         rootFd = open(".", O_RDONLY, 0);
00523 /*@=nullpass@*/
00524         if (rootFd >= 0) {
00525             /*@-superuser -noeffect @*/
00526             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
00527                 xx = chroot(rootDir);
00528             /*@=superuser =noeffect @*/
00529             xx = rpmtsSetChrootDone(ts, 1);
00530         }
00531     }
00532 
00533     /* Create arg variable */
00534     rpmluaPushTable(lua, "arg");
00535     var = rpmluavNew();
00536     rpmluavSetListMode(var, 1);
00537 /*@+relaxtypes@*/
00538     if (progArgv) {
00539         for (i = 0; i < progArgc && progArgv[i]; i++) {
00540             rpmluavSetValue(var, RPMLUAV_STRING, progArgv[i]);
00541             rpmluaSetVar(lua, var);
00542         }
00543     }
00544     if (arg1 >= 0) {
00545         rpmluavSetValueNum(var, arg1);
00546         rpmluaSetVar(lua, var);
00547     }
00548     if (arg2 >= 0) {
00549         rpmluavSetValueNum(var, arg2);
00550         rpmluaSetVar(lua, var);
00551     }
00552 /*@=relaxtypes@*/
00553 /*@-moduncon@*/
00554     var = rpmluavFree(var);
00555 /*@=moduncon@*/
00556     rpmluaPop(lua);
00557 
00558     {
00559         char buf[BUFSIZ];
00560         xx = snprintf(buf, BUFSIZ, "%s(%s-%s-%s)", sln, n, v, r);
00561         if (rpmluaRunScript(lua, script, buf) == -1)
00562             rc = RPMRC_FAIL;
00563     }
00564 
00565     rpmluaDelVar(lua, "arg");
00566 
00567     if (rootFd >= 0) {
00568         const char *rootDir = rpmtsRootDir(ts);
00569         xx = fchdir(rootFd);
00570         xx = close(rootFd);
00571         /*@-superuser -noeffect @*/
00572         if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
00573             xx = chroot(".");
00574         /*@=superuser =noeffect @*/
00575         xx = rpmtsSetChrootDone(ts, 0);
00576     }
00577 
00578     return rc;
00579 }
00580 #endif
00581 
00584 /*@unchecked@*/
00585 static int ldconfig_done = 0;
00586 
00587 /*@unchecked@*/ /*@observer@*/ /*@null@*/
00588 static const char * ldconfig_path = "/sbin/ldconfig";
00589 
00608 static rpmRC runScript(rpmpsm psm, Header h, const char * sln,
00609                 int progArgc, const char ** progArgv,
00610                 const char * script, int arg1, int arg2)
00611         /*@globals ldconfig_done, rpmGlobalMacroContext, h_errno,
00612                 fileSystem, internalState@*/
00613         /*@modifies psm, ldconfig_done, rpmGlobalMacroContext,
00614                 fileSystem, internalState @*/
00615 {
00616     const rpmts ts = psm->ts;
00617     rpmfi fi = psm->fi;
00618     HGE_t hge = fi->hge;
00619     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00620     const char ** argv = NULL;
00621     int argc = 0;
00622     const char ** prefixes = NULL;
00623     int numPrefixes;
00624     rpmTagType ipt;
00625     const char * oldPrefix;
00626     int maxPrefixLength;
00627     int len;
00628     char * prefixBuf = NULL;
00629     const char * fn = NULL;
00630     int xx;
00631     int i;
00632     int freePrefixes = 0;
00633     FD_t scriptFd;
00634     FD_t out;
00635     rpmRC rc = RPMRC_OK;
00636     const char *n, *v, *r, *a;
00637 
00638     if (progArgv == NULL && script == NULL)
00639         return rc;
00640 
00641     /* XXX FIXME: except for %verifyscript, rpmteNEVR can be used. */
00642     xx = headerNVR(h, &n, &v, &r);
00643     xx = hge(h, RPMTAG_ARCH, NULL, (void **) &a, NULL);
00644 
00645     if (progArgv && strcmp(progArgv[0], "<lua>") == 0) {
00646 #ifdef WITH_LUA
00647         rpmMessage(RPMMESS_DEBUG,
00648                 _("%s: %s(%s-%s-%s.%s) running <lua> scriptlet.\n"),
00649                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a);
00650         return runLuaScript(psm, h, sln, progArgc, progArgv,
00651                             script, arg1, arg2);
00652 #else
00653         return RPMRC_FAIL;
00654 #endif
00655     }
00656 
00657     psm->sq.reaper = 1;
00658 
00659     /*
00660      * If a successor node, and ldconfig was just run, don't bother.
00661      */
00662     if (ldconfig_path && progArgv != NULL && psm->unorderedSuccessor) {
00663         if (ldconfig_done && !strcmp(progArgv[0], ldconfig_path)) {
00664             rpmMessage(RPMMESS_DEBUG,
00665                 _("%s: %s(%s-%s-%s.%s) skipping redundant \"%s\".\n"),
00666                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00667                 progArgv[0]);
00668             return rc;
00669         }
00670     }
00671 
00672     rpmMessage(RPMMESS_DEBUG,
00673                 _("%s: %s(%s-%s-%s.%s) %ssynchronous scriptlet start\n"),
00674                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00675                 (psm->unorderedSuccessor ? "a" : ""));
00676 
00677     if (!progArgv) {
00678         argv = alloca(5 * sizeof(*argv));
00679         argv[0] = "/bin/sh";
00680         argc = 1;
00681         ldconfig_done = 0;
00682     } else {
00683         argv = alloca((progArgc + 4) * sizeof(*argv));
00684         memcpy(argv, progArgv, progArgc * sizeof(*argv));
00685         argc = progArgc;
00686         ldconfig_done = (ldconfig_path && !strcmp(argv[0], ldconfig_path)
00687                 ? 1 : 0);
00688     }
00689 
00690 #if __ia64__
00691     /* XXX This assumes that all interpreters are elf executables. */
00692     if ((a != NULL && a[0] == 'i' && a[1] != '\0' && a[2] == '8' && a[3] == '6')
00693      && strcmp(argv[0], "/sbin/ldconfig"))
00694     {
00695         const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
00696         const char * errstr;
00697         char * newPath;
00698         char * t;
00699 
00700         newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
00701         fmt = _free(fmt);
00702 
00703         /* XXX On ia64, change leading /emul/ix86 -> /emul/ia32, ick. */
00704         if (newPath != NULL && *newPath != '\0'
00705          && strlen(newPath) >= (sizeof("/emul/i386")-1)
00706          && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
00707          && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
00708          && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
00709         {
00710             newPath[7] = 'a';
00711             newPath[8] = '3';
00712             newPath[9] = '2';
00713         }
00714 
00715         t = alloca(strlen(newPath) + strlen(argv[0]) + 1);
00716         *t = '\0';
00717         (void) stpcpy( stpcpy(t, newPath), argv[0]);
00718         newPath = _free(newPath);
00719         argv[0] = t;
00720     }
00721 #endif
00722 
00723     if (hge(h, RPMTAG_INSTPREFIXES, &ipt, (void **) &prefixes, &numPrefixes)) {
00724         freePrefixes = 1;
00725     } else if (hge(h, RPMTAG_INSTALLPREFIX, NULL, (void **) &oldPrefix, NULL)) {
00726         prefixes = &oldPrefix;
00727         numPrefixes = 1;
00728     } else {
00729         numPrefixes = 0;
00730     }
00731 
00732     maxPrefixLength = 0;
00733     if (prefixes != NULL)
00734     for (i = 0; i < numPrefixes; i++) {
00735         len = strlen(prefixes[i]);
00736         if (len > maxPrefixLength) maxPrefixLength = len;
00737     }
00738     prefixBuf = alloca(maxPrefixLength + 50);
00739 
00740     if (script) {
00741         const char * rootDir = rpmtsRootDir(ts);
00742         FD_t fd;
00743 
00744         /*@-branchstate@*/
00745         if (makeTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn, &fd)) {
00746             if (prefixes != NULL && freePrefixes) free(prefixes);
00747             return RPMRC_FAIL;
00748         }
00749         /*@=branchstate@*/
00750 
00751         if (rpmIsDebug() &&
00752             (!strcmp(argv[0], "/bin/sh") || !strcmp(argv[0], "/bin/bash")))
00753         {
00754             static const char set_x[] = "set -x\n";
00755             xx = Fwrite(set_x, sizeof(set_x[0]), sizeof(set_x)-1, fd);
00756         }
00757 
00758         if (ldconfig_path && strstr(script, ldconfig_path) != NULL)
00759             ldconfig_done = 1;
00760 
00761         xx = Fwrite(script, sizeof(script[0]), strlen(script), fd);
00762         xx = Fclose(fd);
00763 
00764         {   const char * sn = fn;
00765             if (!rpmtsChrootDone(ts) && rootDir != NULL &&
00766                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00767             {
00768                 sn += strlen(rootDir)-1;
00769             }
00770             argv[argc++] = sn;
00771         }
00772 
00773         if (arg1 >= 0) {
00774             char *av = alloca(20);
00775             sprintf(av, "%d", arg1);
00776             argv[argc++] = av;
00777         }
00778         if (arg2 >= 0) {
00779             char *av = alloca(20);
00780             sprintf(av, "%d", arg2);
00781             argv[argc++] = av;
00782         }
00783     }
00784 
00785     argv[argc] = NULL;
00786 
00787     scriptFd = rpmtsScriptFd(ts);
00788     if (scriptFd != NULL) {
00789         if (rpmIsVerbose()) {
00790             out = fdDup(Fileno(scriptFd));
00791         } else {
00792             out = Fopen("/dev/null", "w.fdio");
00793             if (Ferror(out)) {
00794                 out = fdDup(Fileno(scriptFd));
00795             }
00796         }
00797     } else {
00798         out = fdDup(STDOUT_FILENO);
00799     }
00800     if (out == NULL) return RPMRC_FAIL; /* XXX can't happen */
00801 
00802     /*@-branchstate@*/
00803     xx = rpmsqFork(&psm->sq);
00804     if (psm->sq.child == 0) {
00805         const char * rootDir;
00806         int pipes[2];
00807         int flag;
00808         int fdno;
00809 
00810         pipes[0] = pipes[1] = 0;
00811         /* make stdin inaccessible */
00812         xx = pipe(pipes);
00813         xx = close(pipes[1]);
00814         xx = dup2(pipes[0], STDIN_FILENO);
00815         xx = close(pipes[0]);
00816 
00817         /* XXX Force FD_CLOEXEC on 1st 100 inherited fdno's. */
00818         for (fdno = 3; fdno < 100; fdno++) {
00819             flag = fcntl(fdno, F_GETFD);
00820             if (flag == -1 || (flag & FD_CLOEXEC))
00821                 continue;
00822             xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
00823             /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
00824         }
00825 
00826         if (scriptFd != NULL) {
00827             int sfdno = Fileno(scriptFd);
00828             int ofdno = Fileno(out);
00829             if (sfdno != STDERR_FILENO)
00830                 xx = dup2(sfdno, STDERR_FILENO);
00831             if (ofdno != STDOUT_FILENO)
00832                 xx = dup2(ofdno, STDOUT_FILENO);
00833             /* make sure we don't close stdin/stderr/stdout by mistake! */
00834             if (ofdno > STDERR_FILENO && ofdno != sfdno)
00835                 xx = Fclose (out);
00836             if (sfdno > STDERR_FILENO && ofdno != sfdno)
00837                 xx = Fclose (scriptFd);
00838         }
00839 
00840         {   const char *ipath = rpmExpand("PATH=%{_install_script_path}", NULL);
00841             const char *path = SCRIPT_PATH;
00842 
00843             if (ipath && ipath[5] != '%')
00844                 path = ipath;
00845 
00846             xx = doputenv(path);
00847             /*@-modobserver@*/
00848             ipath = _free(ipath);
00849             /*@=modobserver@*/
00850         }
00851 
00852         if (prefixes != NULL)
00853         for (i = 0; i < numPrefixes; i++) {
00854             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00855             xx = doputenv(prefixBuf);
00856 
00857             /* backwards compatibility */
00858             if (i == 0) {
00859                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00860                 xx = doputenv(prefixBuf);
00861             }
00862         }
00863 
00864         rootDir = ts->rootDir;  /* HACK: rootDir = rpmtsRootDir(ts); instead */
00865         if (rootDir  != NULL)   /* XXX can't happen */
00866         switch(urlIsURL(rootDir)) {
00867         case URL_IS_PATH:
00868             rootDir += sizeof("file://") - 1;
00869             rootDir = strchr(rootDir, '/');
00870             /*@fallthrough@*/
00871         case URL_IS_UNKNOWN:
00872             if (!rpmtsChrootDone(ts) &&
00873                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00874             {
00875                 /*@-superuser -noeffect @*/
00876                 xx = chroot(rootDir);
00877                 /*@=superuser =noeffect @*/
00878             }
00879             xx = chdir("/");
00880             rpmMessage(RPMMESS_DEBUG, _("%s: %s(%s-%s-%s.%s)\texecv(%s) pid %d\n"),
00881                         psm->stepName, sln, n, v, r, a,
00882                         argv[0], (unsigned)getpid());
00883 
00884             /* XXX Don't mtrace into children. */
00885             unsetenv("MALLOC_CHECK_");
00886 
00887             /* Permit libselinux to do the scriptlet exec. */
00888             if (rpmtsSELinuxEnabled(ts) == 1) { 
00889 /*@-moduncon@*/
00890                 xx = rpm_execcon(0, argv[0], argv, environ);
00891 /*@=moduncon@*/
00892                 if (xx != 0)
00893                     break;
00894             }
00895 
00896 /*@-nullstate@*/
00897             xx = execv(argv[0], (char *const *)argv);
00898 /*@=nullstate@*/
00899             break;
00900         case URL_IS_HTTPS:
00901         case URL_IS_HTTP:
00902         case URL_IS_FTP:
00903         case URL_IS_DASH:
00904         case URL_IS_HKP:
00905         default:
00906             break;
00907         }
00908 
00909         _exit(-1);
00910         /*@notreached@*/
00911     }
00912     /*@=branchstate@*/
00913 
00914     if (psm->sq.child == (pid_t)-1) {
00915         rpmError(RPMERR_FORK, _("Couldn't fork %s: %s\n"), sln, strerror(errno));
00916         rc = RPMRC_FAIL;
00917         goto exit;
00918     }
00919 
00920     (void) psmWait(psm);
00921 
00922   /* XXX filter order dependent multilib "other" arch helper error. */
00923   if (!(psm->sq.reaped >= 0 && !strcmp(argv[0], "/usr/sbin/glibc_post_upgrade") && WEXITSTATUS(psm->sq.status) == 110)) {
00924     if (psm->sq.reaped < 0) {
00925         rpmError(RPMERR_SCRIPT,
00926                 _("%s(%s-%s-%s.%s) scriptlet failed, waitpid(%d) rc %d: %s\n"),
00927                  sln, n, v, r, a, psm->sq.child, psm->sq.reaped, strerror(errno));
00928         rc = RPMRC_FAIL;
00929     } else
00930     if (!WIFEXITED(psm->sq.status) || WEXITSTATUS(psm->sq.status)) {
00931       if (WIFSIGNALED(psm->sq.status)) {
00932         rpmError(RPMERR_SCRIPT,
00933                  _("%s(%s-%s-%s.%s) scriptlet failed, signal %d\n"),
00934                  sln, n, v, r, a, WTERMSIG(psm->sq.status));
00935       } else {
00936         rpmError(RPMERR_SCRIPT,
00937                 _("%s(%s-%s-%s.%s) scriptlet failed, exit status %d\n"),
00938                 sln, n, v, r, a, WEXITSTATUS(psm->sq.status));
00939       }
00940         rc = RPMRC_FAIL;
00941     }
00942   }
00943 
00944 exit:
00945     if (freePrefixes) prefixes = hfd(prefixes, ipt);
00946 
00947     xx = Fclose(out);   /* XXX dup'd STDOUT_FILENO */
00948 
00949     /*@-branchstate@*/
00950     if (script) {
00951         if (!rpmIsDebug())
00952             xx = unlink(fn);
00953         fn = _free(fn);
00954     }
00955     /*@=branchstate@*/
00956 
00957     return rc;
00958 }
00959 
00965 static rpmRC runInstScript(rpmpsm psm)
00966         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00967         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00968 {
00969     rpmfi fi = psm->fi;
00970     HGE_t hge = fi->hge;
00971     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00972     void ** progArgv;
00973     int progArgc;
00974     const char ** argv;
00975     rpmTagType ptt, stt;
00976     const char * script;
00977     rpmRC rc = RPMRC_OK;
00978     int xx;
00979 
00980     /*
00981      * headerGetEntry() sets the data pointer to NULL if the entry does
00982      * not exist.
00983      */
00984     xx = hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
00985     xx = hge(fi->h, psm->progTag, &ptt, (void **) &progArgv, &progArgc);
00986     if (progArgv == NULL && script == NULL)
00987         goto exit;
00988 
00989     /*@-branchstate@*/
00990     if (progArgv && ptt == RPM_STRING_TYPE) {
00991         argv = alloca(sizeof(*argv));
00992         *argv = (const char *) progArgv;
00993     } else {
00994         argv = (const char **) progArgv;
00995     }
00996     /*@=branchstate@*/
00997 
00998     if (fi->h != NULL)  /* XXX can't happen */
00999     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), progArgc, argv,
01000                 script, psm->scriptArg, -1);
01001 
01002 exit:
01003     progArgv = hfd(progArgv, ptt);
01004     script = hfd(script, stt);
01005     return rc;
01006 }
01007 
01018 static rpmRC handleOneTrigger(const rpmpsm psm,
01019                         Header sourceH, Header triggeredH,
01020                         int arg2, unsigned char * triggersAlreadyRun)
01021         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState@*/
01022         /*@modifies psm, sourceH, triggeredH, *triggersAlreadyRun,
01023                 rpmGlobalMacroContext, fileSystem, internalState @*/
01024 {
01025     int scareMem = 1;
01026     const rpmts ts = psm->ts;
01027     rpmfi fi = psm->fi;
01028     HGE_t hge = fi->hge;
01029     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01030     rpmds trigger = NULL;
01031     const char ** triggerScripts;
01032     const char ** triggerProgs;
01033     int_32 * triggerIndices;
01034     const char * sourceName;
01035     const char * triggerName;
01036     rpmRC rc = RPMRC_OK;
01037     int xx;
01038     int i;
01039 
01040     xx = headerNVR(sourceH, &sourceName, NULL, NULL);
01041     xx = headerNVR(triggeredH, &triggerName, NULL, NULL);
01042 
01043     trigger = rpmdsInit(rpmdsNew(triggeredH, RPMTAG_TRIGGERNAME, scareMem));
01044     if (trigger == NULL)
01045         return rc;
01046 
01047     (void) rpmdsSetNoPromote(trigger, 1);
01048 
01049     while ((i = rpmdsNext(trigger)) >= 0) {
01050         rpmTagType tit, tst, tpt;
01051         const char * Name;
01052         int_32 Flags = rpmdsFlags(trigger);
01053 
01054         if ((Name = rpmdsN(trigger)) == NULL)
01055             continue;   /* XXX can't happen */
01056 
01057         if (strcmp(Name, sourceName))
01058             continue;
01059         if (!(Flags & psm->sense))
01060             continue;
01061 
01062         /*
01063          * XXX Trigger on any provided dependency, not just the package NEVR.
01064          */
01065         if (!rpmdsAnyMatchesDep(sourceH, trigger, 1))
01066             continue;
01067 
01068         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
01069                        (void **) &triggerIndices, NULL) &&
01070                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
01071                        (void **) &triggerScripts, NULL) &&
01072                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
01073                        (void **) &triggerProgs, NULL))
01074             )
01075             continue;
01076 
01077         {   int arg1;
01078             int index;
01079 
01080             arg1 = rpmdbCountPackages(rpmtsGetRdb(ts), triggerName);
01081             if (arg1 < 0) {
01082                 /* XXX W2DO? fails as "execution of script failed" */
01083                 rc = RPMRC_FAIL;
01084             } else {
01085                 arg1 += psm->countCorrection;
01086                 index = triggerIndices[i];
01087                 if (triggersAlreadyRun == NULL ||
01088                     triggersAlreadyRun[index] == 0)
01089                 {
01090                     rc = runScript(psm, triggeredH, "%trigger", 1,
01091                             triggerProgs + index, triggerScripts[index],
01092                             arg1, arg2);
01093                     if (triggersAlreadyRun != NULL)
01094                         triggersAlreadyRun[index] = 1;
01095                 }
01096             }
01097         }
01098 
01099         triggerIndices = hfd(triggerIndices, tit);
01100         triggerScripts = hfd(triggerScripts, tst);
01101         triggerProgs = hfd(triggerProgs, tpt);
01102 
01103         /*
01104          * Each target/source header pair can only result in a single
01105          * script being run.
01106          */
01107         break;
01108     }
01109 
01110     trigger = rpmdsFree(trigger);
01111 
01112     return rc;
01113 }
01114 
01120 static rpmRC runTriggers(rpmpsm psm)
01121         /*@globals rpmGlobalMacroContext, h_errno,
01122                 fileSystem, internalState @*/
01123         /*@modifies psm, rpmGlobalMacroContext,
01124                 fileSystem, internalState @*/
01125 {
01126     const rpmts ts = psm->ts;
01127     rpmfi fi = psm->fi;
01128     int numPackage = -1;
01129     rpmRC rc = RPMRC_OK;
01130     const char * N = NULL;
01131 
01132     if (psm->te)        /* XXX can't happen */
01133         N = rpmteN(psm->te);
01134 /* XXX: Might need to adjust instance counts four autorollback. */
01135     if (N)              /* XXX can't happen */
01136         numPackage = rpmdbCountPackages(rpmtsGetRdb(ts), N)
01137                                 + psm->countCorrection;
01138     if (numPackage < 0)
01139         return RPMRC_NOTFOUND;
01140 
01141     if (fi != NULL && fi->h != NULL)    /* XXX can't happen */
01142     {   Header triggeredH;
01143         rpmdbMatchIterator mi;
01144         int countCorrection = psm->countCorrection;
01145 
01146         psm->countCorrection = 0;
01147         mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, N, 0);
01148         while((triggeredH = rpmdbNextIterator(mi)) != NULL)
01149             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01150         mi = rpmdbFreeIterator(mi);
01151         psm->countCorrection = countCorrection;
01152     }
01153 
01154     return rc;
01155 }
01156 
01162 static rpmRC runImmedTriggers(rpmpsm psm)
01163         /*@globals rpmGlobalMacroContext, h_errno,
01164                 fileSystem, internalState @*/
01165         /*@modifies psm, rpmGlobalMacroContext,
01166                 fileSystem, internalState @*/
01167 {
01168     const rpmts ts = psm->ts;
01169     rpmfi fi = psm->fi;
01170     HGE_t hge = fi->hge;
01171     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01172     const char ** triggerNames;
01173     int numTriggers;
01174     int_32 * triggerIndices;
01175     rpmTagType tnt, tit;
01176     int numTriggerIndices;
01177     unsigned char * triggersRun;
01178     rpmRC rc = RPMRC_OK;
01179 
01180     if (fi->h == NULL)  return rc;      /* XXX can't happen */
01181 
01182     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01183                         (void **) &triggerNames, &numTriggers) &&
01184                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01185                         (void **) &triggerIndices, &numTriggerIndices))
01186         )
01187         return rc;
01188 
01189     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01190     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01191 
01192     {   Header sourceH = NULL;
01193         int i;
01194 
01195         for (i = 0; i < numTriggers; i++) {
01196             rpmdbMatchIterator mi;
01197 
01198             if (triggersRun[triggerIndices[i]] != 0) continue;
01199         
01200             mi = rpmtsInitIterator(ts, RPMTAG_NAME, triggerNames[i], 0);
01201 
01202             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01203                 rc |= handleOneTrigger(psm, sourceH, fi->h,
01204                                 rpmdbGetIteratorCount(mi),
01205                                 triggersRun);
01206             }
01207 
01208             mi = rpmdbFreeIterator(mi);
01209         }
01210     }
01211     triggerIndices = hfd(triggerIndices, tit);
01212     triggerNames = hfd(triggerNames, tnt);
01213     return rc;
01214 }
01215 
01216 /*@observer@*/ static const char *const pkgStageString(pkgStage a)
01217         /*@*/
01218 {
01219     switch(a) {
01220     case PSM_UNKNOWN:           return "unknown";
01221 
01222     case PSM_PKGINSTALL:        return "  install";
01223     case PSM_PKGERASE:          return "    erase";
01224     case PSM_PKGCOMMIT:         return "   commit";
01225     case PSM_PKGSAVE:           return "repackage";
01226 
01227     case PSM_INIT:              return "init";
01228     case PSM_PRE:               return "pre";
01229     case PSM_PROCESS:           return "process";
01230     case PSM_POST:              return "post";
01231     case PSM_UNDO:              return "undo";
01232     case PSM_FINI:              return "fini";
01233 
01234     case PSM_CREATE:            return "create";
01235     case PSM_NOTIFY:            return "notify";
01236     case PSM_DESTROY:           return "destroy";
01237     case PSM_COMMIT:            return "commit";
01238 
01239     case PSM_CHROOT_IN:         return "chrootin";
01240     case PSM_CHROOT_OUT:        return "chrootout";
01241     case PSM_SCRIPT:            return "script";
01242     case PSM_TRIGGERS:          return "triggers";
01243     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01244 
01245     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01246 
01247     case PSM_RPMDB_LOAD:        return "rpmdbload";
01248     case PSM_RPMDB_ADD:         return "rpmdbadd";
01249     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01250 
01251     default:                    return "???";
01252     }
01253     /*@noteached@*/
01254 }
01255 
01256 rpmpsm XrpmpsmUnlink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01257 {
01258     if (psm == NULL) return NULL;
01259 /*@-modfilesys@*/
01260 if (_psm_debug && msg != NULL)
01261 fprintf(stderr, "--> psm %p -- %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01262 /*@=modfilesys@*/
01263     psm->nrefs--;
01264     return NULL;
01265 }
01266 
01267 rpmpsm XrpmpsmLink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01268 {
01269     if (psm == NULL) return NULL;
01270     psm->nrefs++;
01271 
01272 /*@-modfilesys@*/
01273 if (_psm_debug && msg != NULL)
01274 fprintf(stderr, "--> psm %p ++ %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01275 /*@=modfilesys@*/
01276 
01277     /*@-refcounttrans@*/ return psm; /*@=refcounttrans@*/
01278 }
01279 
01280 rpmpsm rpmpsmFree(rpmpsm psm)
01281 {
01282     const char * msg = "rpmpsmFree";
01283     if (psm == NULL)
01284         return NULL;
01285 
01286     if (psm->nrefs > 1)
01287         return rpmpsmUnlink(psm, msg);
01288 
01289 /*@-nullstate@*/
01290     psm->fi = rpmfiFree(psm->fi);
01291 #ifdef  NOTYET
01292     psm->te = rpmteFree(psm->te);
01293 #else
01294     psm->te = NULL;
01295 #endif
01296 /*@-internalglobs@*/
01297     psm->ts = rpmtsFree(psm->ts);
01298 /*@=internalglobs@*/
01299 
01300     (void) rpmpsmUnlink(psm, msg);
01301 
01302     /*@-refcounttrans -usereleased@*/
01303 /*@-boundswrite@*/
01304     memset(psm, 0, sizeof(*psm));               /* XXX trash and burn */
01305 /*@=boundswrite@*/
01306     psm = _free(psm);
01307     /*@=refcounttrans =usereleased@*/
01308 
01309     return NULL;
01310 /*@=nullstate@*/
01311 }
01312 
01313 rpmpsm rpmpsmNew(rpmts ts, rpmte te, rpmfi fi)
01314 {
01315     const char * msg = "rpmpsmNew";
01316     rpmpsm psm = xcalloc(1, sizeof(*psm));
01317 
01318     if (ts)     psm->ts = rpmtsLink(ts, msg);
01319 #ifdef  NOTYET
01320     if (te)     psm->te = rpmteLink(te, msg);
01321 #else
01322 /*@-assignexpose -temptrans @*/
01323     if (te)     psm->te = te;
01324 /*@=assignexpose =temptrans @*/
01325 #endif
01326     if (fi)     psm->fi = rpmfiLink(fi, msg);
01327 
01328     return rpmpsmLink(psm, msg);
01329 }
01330 
01331 static void * rpmpsmThread(void * arg)
01332         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01333         /*@modifies arg, rpmGlobalMacroContext, fileSystem, internalState @*/
01334 {
01335     rpmpsm psm = arg;
01336 /*@-unqualifiedtrans@*/
01337     return ((void *) rpmpsmStage(psm, psm->nstage));
01338 /*@=unqualifiedtrans@*/
01339 }
01340 
01341 static int rpmpsmNext(rpmpsm psm, pkgStage nstage)
01342         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01343         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
01344 {
01345     psm->nstage = nstage;
01346     if (_psm_threads)
01347         return rpmsqJoin( rpmsqThread(rpmpsmThread, psm) );
01348     return rpmpsmStage(psm, psm->nstage);
01349 }
01350 
01355 /*@-bounds -nullpass@*/ /* FIX: testing null annotation for fi->h */
01356 rpmRC rpmpsmStage(rpmpsm psm, pkgStage stage)
01357 {
01358     const rpmts ts = psm->ts;
01359     uint_32 tscolor = rpmtsColor(ts);
01360     rpmfi fi = psm->fi;
01361     HGE_t hge = fi->hge;
01362     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01363     rpmRC rc = psm->rc;
01364     int saveerrno;
01365     int xx;
01366 
01367     /*@-branchstate@*/
01368     switch (stage) {
01369     case PSM_UNKNOWN:
01370         break;
01371     case PSM_INIT:
01372         rpmMessage(RPMMESS_DEBUG, _("%s: %s has %d files, test = %d\n"),
01373                 psm->stepName, rpmteNEVR(psm->te),
01374                 rpmfiFC(fi), (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST));
01375 
01376         /*
01377          * When we run scripts, we pass an argument which is the number of
01378          * versions of this package that will be installed when we are
01379          * finished.
01380          */
01381         psm->npkgs_installed = rpmdbCountPackages(rpmtsGetRdb(ts), rpmteN(psm->te));
01382         if (psm->npkgs_installed < 0) {
01383             rc = RPMRC_FAIL;
01384             break;
01385         }
01386 
01387         /* If we have a score then autorollback is enabled.  If autorollback is
01388          * enabled, and this is an autorollback transaction, then we may need to
01389          * adjust the pkgs installed count.
01390          *
01391          * If all this is true, this adjustment should only be made if the PSM goal
01392          * is an install.  No need to make this adjustment on the erase
01393          * component of the upgrade, or even more absurd to do this when doing a
01394          * PKGSAVE.
01395          */
01396         if (rpmtsGetScore(ts) != NULL &&
01397             rpmtsGetType(ts) == RPMTRANS_TYPE_AUTOROLLBACK &&
01398             (psm->goal & ~(PSM_PKGSAVE|PSM_PKGERASE))) {
01399             /* Get the score, if its not NULL, get the appropriate
01400              * score entry.
01401              */
01402             rpmtsScore score = rpmtsGetScore(ts);
01403             if (score != NULL) {
01404                 /* OK, we got a real score so lets get the appropriate
01405                  * score entry.
01406                  */
01407                 rpmtsScoreEntry se;
01408                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
01409 
01410                 /* IF the header for the install element has been installed,
01411                  * but the header for the erase element has not been erased,
01412                  * then decrement the instance count.  This is because in an
01413                  * autorollback, if the header was added in the initial transaction
01414                  * then in the case of an upgrade the instance count will be
01415                  * 2 instead of one when re-installing the old package, and 3 when
01416                  * erasing the new package.
01417                  *
01418                  * Another wrinkle is we only want to make this adjustement
01419                  * if the thing we are rollback was an upgrade of package.  A pure
01420                  * install or erase does not need the adjustment
01421                  */
01422                 if (se && se->installed &&
01423                     !se->erased &&
01424                     (se->te_types & (TR_ADDED|TR_REMOVED)))
01425                     psm->npkgs_installed--;
01426            }
01427         }
01428 
01429         if (psm->goal == PSM_PKGINSTALL) {
01430             int fc = rpmfiFC(fi);
01431 
01432             psm->scriptArg = psm->npkgs_installed + 1;
01433 
01434 assert(psm->mi == NULL);
01435             psm->mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(psm->te), 0);
01436             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_EPOCH, RPMMIRE_STRCMP,
01437                         rpmteE(psm->te));
01438             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION, RPMMIRE_STRCMP,
01439                         rpmteV(psm->te));
01440             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE, RPMMIRE_STRCMP,
01441                         rpmteR(psm->te));
01442             if (tscolor) {
01443                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_ARCH, RPMMIRE_STRCMP,
01444                         rpmteA(psm->te));
01445                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_OS, RPMMIRE_STRCMP,
01446                         rpmteO(psm->te));
01447             }
01448 
01449             while ((psm->oh = rpmdbNextIterator(psm->mi)) != NULL) {
01450                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01451                 psm->oh = NULL;
01452                 /*@loopbreak@*/ break;
01453             }
01454             psm->mi = rpmdbFreeIterator(psm->mi);
01455             rc = RPMRC_OK;
01456 
01457             /* XXX lazy alloc here may need to be done elsewhere. */
01458             if (fi->fstates == NULL && fc > 0) {
01459                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fc);
01460                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fc);
01461             }
01462 
01463             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01464             if (fc <= 0)                                break;
01465         
01466             /*
01467              * Old format relocatable packages need the entire default
01468              * prefix stripped to form the cpio list, while all other packages
01469              * need the leading / stripped.
01470              */
01471             {   const char * p;
01472                 xx = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01473                 fi->striplen = (xx ? strlen(p) + 1 : 1);
01474             }
01475             fi->mapflags =
01476                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID | (fi->mapflags & CPIO_SBIT_CHECK);
01477         
01478             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01479                 rpmfiBuildFNames(fi->h, RPMTAG_ORIGBASENAMES, &fi->apath, NULL);
01480             else
01481                 rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
01482         
01483             if (fi->fuser == NULL)
01484                 xx = hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01485                                 (void **) &fi->fuser, NULL);
01486             if (fi->fgroup == NULL)
01487                 xx = hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01488                                 (void **) &fi->fgroup, NULL);
01489             rc = RPMRC_OK;
01490         }
01491         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01492             psm->scriptArg = psm->npkgs_installed - 1;
01493         
01494             /* Retrieve installed header. */
01495             rc = rpmpsmNext(psm, PSM_RPMDB_LOAD);
01496 if (rc == RPMRC_OK)
01497 if (psm->te)
01498 psm->te->h = headerLink(fi->h);
01499         }
01500         if (psm->goal == PSM_PKGSAVE) {
01501             /* Open output package for writing. */
01502             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01503                 const char * pkgbn =
01504                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01505 
01506                 bfmt = _free(bfmt);
01507                 psm->pkgURL = rpmGenPath("%{?_repackage_root}",
01508                                          "%{?_repackage_dir}",
01509                                         pkgbn);
01510                 pkgbn = _free(pkgbn);
01511                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01512                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01513                 if (psm->fd == NULL || Ferror(psm->fd)) {
01514                     rc = RPMRC_FAIL;
01515                     break;
01516                 }
01517             }
01518         }
01519         break;
01520     case PSM_PRE:
01521         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01522 
01523 /* XXX insure that trigger index is opened before entering chroot. */
01524 #ifdef  NOTYET
01525  { static int oneshot = 0;
01526    dbiIndex dbi;
01527    if (!oneshot) {
01528      dbi = dbiOpen(rpmtsGetRdb(ts), RPMTAG_TRIGGERNAME, 0);
01529      oneshot++;
01530    }
01531  }
01532 #endif
01533 
01534         /* Change root directory if requested and not already done. */
01535         rc = rpmpsmNext(psm, PSM_CHROOT_IN);
01536 
01537         if (psm->goal == PSM_PKGINSTALL) {
01538             psm->scriptTag = RPMTAG_PREIN;
01539             psm->progTag = RPMTAG_PREINPROG;
01540             psm->sense = RPMSENSE_TRIGGERPREIN;
01541             psm->countCorrection = 0;   /* XXX is this correct?!? */
01542 
01543             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01544 
01545                 /* Run triggers in other package(s) this package sets off. */
01546                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01547                 if (rc) break;
01548 
01549                 /* Run triggers in this package other package(s) set off. */
01550                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01551                 if (rc) break;
01552             }
01553 
01554             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPRE)) {
01555                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01556                 if (rc != RPMRC_OK) {
01557                     rpmError(RPMERR_SCRIPT,
01558                         _("%s: %s scriptlet failed (%d), skipping %s\n"),
01559                         psm->stepName, tag2sln(psm->scriptTag), rc,
01560                         rpmteNEVR(psm->te));
01561                     break;
01562                 }
01563             }
01564         }
01565 
01566         if (psm->goal == PSM_PKGERASE) {
01567             psm->scriptTag = RPMTAG_PREUN;
01568             psm->progTag = RPMTAG_PREUNPROG;
01569             psm->sense = RPMSENSE_TRIGGERUN;
01570             psm->countCorrection = -1;
01571 
01572             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERUN)) {
01573                 /* Run triggers in this package other package(s) set off. */
01574                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01575                 if (rc) break;
01576 
01577                 /* Run triggers in other package(s) this package sets off. */
01578                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01579                 if (rc) break;
01580             }
01581 
01582             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPREUN))
01583                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01584         }
01585         if (psm->goal == PSM_PKGSAVE) {
01586             int noArchiveSize = 0;
01587 
01588             /* Regenerate original header. */
01589             {   void * uh = NULL;
01590                 int_32 uht, uhc;
01591 
01592                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01593                     psm->oh = headerCopyLoad(uh);
01594                     uh = hfd(uh, uht);
01595                 } else
01596                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMAGE, &uht, &uh, &uhc))
01597                 {
01598                     HeaderIterator hi;
01599                     int_32 tag, type, count;
01600                     hPTR_t ptr;
01601                     Header oh;
01602 
01603                     /* Load the original header from the blob. */
01604                     oh = headerCopyLoad(uh);
01605 
01606                     /* XXX this is headerCopy w/o headerReload() */
01607                     psm->oh = headerNew();
01608 
01609                     /*@-branchstate@*/
01610                     for (hi = headerInitIterator(oh);
01611                         headerNextIterator(hi, &tag, &type, &ptr, &count);
01612                         ptr = headerFreeData((void *)ptr, type))
01613                     {
01614                         if (tag == RPMTAG_ARCHIVESIZE)
01615                             noArchiveSize = 1;
01616                         if (ptr) (void) headerAddEntry(psm->oh, tag, type, ptr, count);
01617                     }
01618                     hi = headerFreeIterator(hi);
01619                     /*@=branchstate@*/
01620 
01621                     oh = headerFree(oh);
01622                     uh = hfd(uh, uht);
01623                 } else
01624                     break;      /* XXX shouldn't ever happen */
01625             }
01626 
01627             /* Retrieve type of payload compression. */
01628             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01629             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01630             /*@=nullstate@*/
01631 
01632             /* Write the lead section into the package. */
01633             {   int archnum = -1;
01634                 int osnum = -1;
01635                 struct rpmlead lead;
01636 
01637 #ifndef DYING
01638                 rpmGetArchInfo(NULL, &archnum);
01639                 rpmGetOsInfo(NULL, &osnum);
01640 #endif
01641 
01642                 memset(&lead, 0, sizeof(lead));
01643                 /* XXX Set package version conditioned on noDirTokens. */
01644                 lead.major = 3;
01645                 lead.minor = 0;
01646                 lead.type = RPMLEAD_BINARY;
01647                 lead.archnum = archnum;
01648                 lead.osnum = osnum;
01649                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01650 
01651                 strncpy(lead.name, rpmteNEVR(psm->te), sizeof(lead.name));
01652 
01653                 rc = writeLead(psm->fd, &lead);
01654                 if (rc != RPMRC_OK) {
01655                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01656                          Fstrerror(psm->fd));
01657                     break;
01658                 }
01659             }
01660 
01661             /* Write the signature section into the package. */
01662             /* XXX rpm-4.1 and later has archive size in signature header. */
01663             {   Header sigh = headerRegenSigHeader(fi->h, noArchiveSize);
01664                 /* Reallocate the signature into one contiguous region. */
01665                 sigh = headerReload(sigh, RPMTAG_HEADERSIGNATURES);
01666                 if (sigh == NULL) {
01667                     rpmError(RPMERR_NOSPACE, _("Unable to reload signature header\n"));
01668                     rc = RPMRC_FAIL;
01669                     break;
01670                 }
01671                 rc = rpmWriteSignature(psm->fd, sigh);
01672                 sigh = rpmFreeSignature(sigh);
01673                 if (rc) break;
01674             }
01675 
01676             /* Add remove transaction id to header. */
01677             if (psm->oh != NULL)
01678             {   int_32 tid = rpmtsGetTid(ts);
01679                 xx = headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01680                         RPM_INT32_TYPE, &tid, 1);
01681             }
01682 
01683             /* Write the metadata section into the package. */
01684             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01685             if (rc) break;
01686         }
01687         break;
01688     case PSM_PROCESS:
01689         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01690 
01691         if (psm->goal == PSM_PKGINSTALL) {
01692 
01693             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01694 
01695             /* XXX Synthesize callbacks for packages with no files. */
01696             if (rpmfiFC(fi) <= 0) {
01697                 void * ptr;
01698                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_START, 0, 100);
01699                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS, 100, 100);
01700                 break;
01701             }
01702 
01703             /* Retrieve type of payload compression. */
01704             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01705 
01706             if (rpmteFd(fi->te) == NULL) {      /* XXX can't happen */
01707                 rc = RPMRC_FAIL;
01708                 break;
01709             }
01710 
01711             /*@-nullpass@*/     /* LCL: fi->fd != NULL here. */
01712             psm->cfd = Fdopen(fdDup(Fileno(rpmteFd(fi->te))), psm->rpmio_flags);
01713             /*@=nullpass@*/
01714             if (psm->cfd == NULL) {     /* XXX can't happen */
01715                 rc = RPMRC_FAIL;
01716                 break;
01717             }
01718 
01719             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01720                         psm->cfd, NULL, &psm->failedFile);
01721             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_UNCOMPRESS),
01722                         fdstat_op(psm->cfd, FDSTAT_READ));
01723             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01724                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01725             xx = fsmTeardown(fi->fsm);
01726 
01727             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01728             xx = Fclose(psm->cfd);
01729             psm->cfd = NULL;
01730             /*@-mods@*/
01731             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01732             /*@=mods@*/
01733 
01734             if (!rc)
01735                 rc = rpmpsmNext(psm, PSM_COMMIT);
01736 
01737             /* XXX make sure progress is closed out */
01738             psm->what = RPMCALLBACK_INST_PROGRESS;
01739             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01740             psm->total = psm->amount;
01741             xx = rpmpsmNext(psm, PSM_NOTIFY);
01742 
01743             if (rc) {
01744                 rpmError(RPMERR_CPIO,
01745                         _("unpacking of archive failed%s%s: %s\n"),
01746                         (psm->failedFile != NULL ? _(" on file ") : ""),
01747                         (psm->failedFile != NULL ? psm->failedFile : ""),
01748                         cpioStrerror(rc));
01749                 rc = RPMRC_FAIL;
01750 
01751                 /* XXX notify callback on error. */
01752                 psm->what = RPMCALLBACK_UNPACK_ERROR;
01753                 psm->amount = 0;
01754                 psm->total = 0;
01755                 xx = rpmpsmNext(psm, PSM_NOTIFY);
01756 
01757                 break;
01758             }
01759         }
01760         if (psm->goal == PSM_PKGERASE) {
01761             int fc = rpmfiFC(fi);
01762 
01763             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01764             if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)       break;
01765 
01766             /* XXX Synthesize callbacks for packages with no files. */
01767             if (rpmfiFC(fi) <= 0) {
01768                 void * ptr;
01769                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_UNINST_START, 0, 100);
01770                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_UNINST_STOP, 0, 100);
01771                 break;
01772             }
01773 
01774             psm->what = RPMCALLBACK_UNINST_START;
01775             psm->amount = fc;           /* XXX W2DO? looks wrong. */
01776             psm->total = fc;
01777             xx = rpmpsmNext(psm, PSM_NOTIFY);
01778 
01779             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01780                         NULL, NULL, &psm->failedFile);
01781             xx = fsmTeardown(fi->fsm);
01782 
01783             psm->what = RPMCALLBACK_UNINST_STOP;
01784             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01785             psm->total = fc;
01786             xx = rpmpsmNext(psm, PSM_NOTIFY);
01787 
01788         }
01789         if (psm->goal == PSM_PKGSAVE) {
01790             fileAction * actions = fi->actions;
01791             fileAction action = fi->action;
01792 
01793             fi->action = FA_COPYOUT;
01794             fi->actions = NULL;
01795 
01796             if (psm->fd == NULL) {      /* XXX can't happen */
01797                 rc = RPMRC_FAIL;
01798                 break;
01799             }
01800             /*@-nullpass@*/     /* FIX: fdDup mey return NULL. */
01801             xx = Fflush(psm->fd);
01802             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01803             /*@=nullpass@*/
01804             if (psm->cfd == NULL) {     /* XXX can't happen */
01805                 rc = RPMRC_FAIL;
01806                 break;
01807             }
01808 
01809             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01810                         NULL, &psm->failedFile);
01811             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_COMPRESS),
01812                         fdstat_op(psm->cfd, FDSTAT_WRITE));
01813             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01814                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01815             xx = fsmTeardown(fi->fsm);
01816 
01817             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01818             xx = Fclose(psm->cfd);
01819             psm->cfd = NULL;
01820             /*@-mods@*/
01821             errno = saveerrno;
01822             /*@=mods@*/
01823 
01824             /* XXX make sure progress is closed out */
01825             psm->what = RPMCALLBACK_INST_PROGRESS;
01826             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01827             psm->total = psm->amount;
01828             xx = rpmpsmNext(psm, PSM_NOTIFY);
01829 
01830             fi->action = action;
01831             fi->actions = actions;
01832         }
01833         break;
01834     case PSM_POST:
01835         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01836 
01837         if (psm->goal == PSM_PKGINSTALL) {
01838             int_32 installTime = (int_32) time(NULL);
01839             int fc = rpmfiFC(fi);
01840 
01841             if (fi->h == NULL) break;   /* XXX can't happen */
01842             if (fi->fstates != NULL && fc > 0)
01843                 xx = headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01844                                 fi->fstates, fc);
01845 
01846             xx = headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01847                                 &installTime, 1);
01848 
01849             xx = headerAddEntry(fi->h, RPMTAG_INSTALLCOLOR, RPM_INT32_TYPE,
01850                                 &tscolor, 1);
01851 
01852             /*
01853              * If this package has already been installed, remove it from
01854              * the database before adding the new one.
01855              */
01856             if (fi->record && !(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)) {
01857                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01858                 if (rc) break;
01859             }
01860 
01861             rc = rpmpsmNext(psm, PSM_RPMDB_ADD);
01862             if (rc) break;
01863 
01864             psm->scriptTag = RPMTAG_POSTIN;
01865             psm->progTag = RPMTAG_POSTINPROG;
01866             psm->sense = RPMSENSE_TRIGGERIN;
01867             psm->countCorrection = 0;
01868 
01869             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOST)) {
01870                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01871                 if (rc) break;
01872             }
01873             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERIN)) {
01874                 /* Run triggers in other package(s) this package sets off. */
01875                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01876                 if (rc) break;
01877 
01878                 /* Run triggers in this package other package(s) set off. */
01879                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01880                 if (rc) break;
01881             }
01882 
01883             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01884                 rc = markReplacedFiles(psm);
01885 
01886         }
01887         if (psm->goal == PSM_PKGERASE) {
01888 
01889             psm->scriptTag = RPMTAG_POSTUN;
01890             psm->progTag = RPMTAG_POSTUNPROG;
01891             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01892             psm->countCorrection = -1;
01893 
01894             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOSTUN)) {
01895                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01896                 if (rc) break;
01897             }
01898 
01899             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01900                 /* Run triggers in other package(s) this package sets off. */
01901                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01902                 if (rc) break;
01903             }
01904 
01905             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01906                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01907         }
01908         if (psm->goal == PSM_PKGSAVE) {
01909         }
01910 
01911         /* Restore root directory if changed. */
01912         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01913         break;
01914     case PSM_UNDO:
01915         break;
01916     case PSM_FINI:
01917         /* Restore root directory if changed. */
01918         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01919 
01920         if (psm->fd != NULL) {
01921             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01922             xx = Fclose(psm->fd);
01923             psm->fd = NULL;
01924             /*@-mods@*/
01925             errno = saveerrno;
01926             /*@=mods@*/
01927         }
01928 
01929         if (psm->goal == PSM_PKGSAVE) {
01930             if (!rc && ts && ts->notify == NULL) {
01931                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01932                         (psm->pkgURL ? psm->pkgURL : "???"));
01933             }
01934         }
01935 
01936         if (rc) {
01937             if (psm->failedFile)
01938                 rpmError(RPMERR_CPIO,
01939                         _("%s failed on file %s: %s\n"),
01940                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01941             else
01942                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01943                         psm->stepName, cpioStrerror(rc));
01944 
01945             /* XXX notify callback on error. */
01946             psm->what = RPMCALLBACK_CPIO_ERROR;
01947             psm->amount = 0;
01948             psm->total = 0;
01949             /*@-nullstate@*/ /* FIX: psm->fd may be NULL. */
01950             xx = rpmpsmNext(psm, PSM_NOTIFY);
01951             /*@=nullstate@*/
01952         }
01953 
01954 /*@-branchstate@*/
01955         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01956 if (psm->te != NULL)
01957 if (psm->te->h != NULL)
01958 psm->te->h = headerFree(psm->te->h);
01959             if (fi->h != NULL)
01960                 fi->h = headerFree(fi->h);
01961         }
01962 /*@=branchstate@*/
01963         psm->oh = headerFree(psm->oh);
01964         psm->pkgURL = _free(psm->pkgURL);
01965         psm->rpmio_flags = _free(psm->rpmio_flags);
01966         psm->failedFile = _free(psm->failedFile);
01967 
01968         fi->fgroup = hfd(fi->fgroup, -1);
01969         fi->fuser = hfd(fi->fuser, -1);
01970         fi->apath = _free(fi->apath);
01971         fi->fstates = _free(fi->fstates);
01972         break;
01973 
01974     case PSM_PKGINSTALL:
01975     case PSM_PKGERASE:
01976     case PSM_PKGSAVE:
01977         psm->goal = stage;
01978         psm->rc = RPMRC_OK;
01979         psm->stepName = pkgStageString(stage);
01980 
01981         rc = rpmpsmNext(psm, PSM_INIT);
01982         if (!rc) rc = rpmpsmNext(psm, PSM_PRE);
01983         if (!rc) rc = rpmpsmNext(psm, PSM_PROCESS);
01984         if (!rc) rc = rpmpsmNext(psm, PSM_POST);
01985         xx = rpmpsmNext(psm, PSM_FINI);
01986         break;
01987     case PSM_PKGCOMMIT:
01988         break;
01989 
01990     case PSM_CREATE:
01991         break;
01992     case PSM_NOTIFY:
01993     {   void * ptr;
01994 /*@-nullpass@*/ /* FIX: psm->te may be NULL */
01995         ptr = rpmtsNotify(ts, psm->te, psm->what, psm->amount, psm->total);
01996 /*@-nullpass@*/
01997     }   break;
01998     case PSM_DESTROY:
01999         break;
02000     case PSM_COMMIT:
02001         if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_PKGCOMMIT)) break;
02002         if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY) break;
02003 
02004         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
02005                         NULL, NULL, &psm->failedFile);
02006         xx = fsmTeardown(fi->fsm);
02007         break;
02008 
02009     case PSM_CHROOT_IN:
02010     {   const char * rootDir = rpmtsRootDir(ts);
02011         /* Change root directory if requested and not already done. */
02012         if (rootDir != NULL && !(rootDir[0] == '/' && rootDir[1] == '\0')
02013          && !rpmtsChrootDone(ts) && !psm->chrootDone)
02014         {
02015             static int _pw_loaded = 0;
02016             static int _gr_loaded = 0;
02017 
02018             if (!_pw_loaded) {
02019                 (void)getpwnam("root");
02020                 endpwent();
02021                 _pw_loaded++;
02022             }
02023             if (!_gr_loaded) {
02024                 (void)getgrnam("root");
02025                 endgrent();
02026                 _gr_loaded++;
02027             }
02028 
02029             xx = chdir("/");
02030             /*@-superuser@*/
02031             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
02032                 rc = chroot(rootDir);
02033             /*@=superuser@*/
02034             psm->chrootDone = 1;
02035             (void) rpmtsSetChrootDone(ts, 1);
02036         }
02037     }   break;
02038     case PSM_CHROOT_OUT:
02039         /* Restore root directory if changed. */
02040         if (psm->chrootDone) {
02041             const char * rootDir = rpmtsRootDir(ts);
02042             const char * currDir = rpmtsCurrDir(ts);
02043             /*@-superuser@*/
02044             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
02045                 rc = chroot(".");
02046             /*@=superuser@*/
02047             psm->chrootDone = 0;
02048             (void) rpmtsSetChrootDone(ts, 0);
02049             if (currDir != NULL)        /* XXX can't happen */
02050                 xx = chdir(currDir);
02051         }
02052         break;
02053     case PSM_SCRIPT:    /* Run current package scriptlets. */
02054         rc = runInstScript(psm);
02055         break;
02056     case PSM_TRIGGERS:
02057         /* Run triggers in other package(s) this package sets off. */
02058         rc = runTriggers(psm);
02059         break;
02060     case PSM_IMMED_TRIGGERS:
02061         /* Run triggers in this package other package(s) set off. */
02062         rc = runImmedTriggers(psm);
02063         break;
02064 
02065     case PSM_RPMIO_FLAGS:
02066     {   const char * payload_compressor = NULL;
02067         char * t;
02068 
02069         /*@-branchstate@*/
02070         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
02071                             (void **) &payload_compressor, NULL))
02072             payload_compressor = "gzip";
02073         /*@=branchstate@*/
02074         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
02075         *t = '\0';
02076         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
02077         if (!strcmp(payload_compressor, "gzip"))
02078             t = stpcpy(t, ".gzdio");
02079         if (!strcmp(payload_compressor, "bzip2"))
02080             t = stpcpy(t, ".bzdio");
02081         rc = RPMRC_OK;
02082     }   break;
02083 
02084     case PSM_RPMDB_LOAD:
02085 assert(psm->mi == NULL);
02086         psm->mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
02087                                 &fi->record, sizeof(fi->record));
02088 
02089         fi->h = rpmdbNextIterator(psm->mi);
02090         if (fi->h != NULL)
02091             fi->h = headerLink(fi->h);
02092 
02093         psm->mi = rpmdbFreeIterator(psm->mi);
02094         rc = (fi->h != NULL ? RPMRC_OK : RPMRC_FAIL);
02095         break;
02096     case PSM_RPMDB_ADD:
02097         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02098         if (fi->h == NULL)      break;  /* XXX can't happen */
02099         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02100         if (!(rpmtsVSFlags(ts) & RPMVSF_NOHDRCHK))
02101             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02102                                 ts, headerCheck);
02103         else
02104             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02105                                 NULL, NULL);
02106 
02107         /* Set the database instance so consumers (i.e. rpmtsRun())
02108          * can add this to a rollback transaction.
02109          */
02110         rpmteSetDBInstance(psm->te, myinstall_instance);
02111 
02112         /*
02113          * If the score exists and this is not a rollback or autorollback
02114          * then lets check off installed for this package.
02115          */
02116         if (rpmtsGetScore(ts) != NULL &&
02117             rpmtsGetType(ts) != RPMTRANS_TYPE_ROLLBACK &&
02118             rpmtsGetType(ts) != RPMTRANS_TYPE_AUTOROLLBACK)
02119         {
02120             /* Get the score, if its not NULL, get the appropriate
02121              * score entry.
02122              */
02123             rpmtsScore score = rpmtsGetScore(ts);
02124             if (score != NULL) {
02125                 rpmtsScoreEntry se;
02126                 /* OK, we got a real score so lets get the appropriate
02127                  * score entry.
02128                  */
02129                 rpmMessage(RPMMESS_DEBUG,
02130                     _("Attempting to mark %s as installed in score board(%u).\n"),
02131                     rpmteN(psm->te), (unsigned) score);
02132                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
02133                 if (se != NULL) se->installed = 1;
02134             }
02135         }
02136         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02137         break;
02138     case PSM_RPMDB_REMOVE:
02139         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02140         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02141         rc = rpmdbRemove(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->record,
02142                                 NULL, NULL);
02143 
02144         /*
02145          * If the score exists and this is not a rollback or autorollback
02146          * then lets check off erased for this package.
02147          */
02148         if (rpmtsGetScore(ts) != NULL &&
02149            rpmtsGetType(ts) != RPMTRANS_TYPE_ROLLBACK &&
02150            rpmtsGetType(ts) != RPMTRANS_TYPE_AUTOROLLBACK)
02151         {
02152             /* Get the score, if its not NULL, get the appropriate
02153              * score entry.
02154              */
02155             rpmtsScore score = rpmtsGetScore(ts);
02156 
02157             if (score != NULL) { /* XXX: Can't happen */
02158                 rpmtsScoreEntry se;
02159                 /* OK, we got a real score so lets get the appropriate
02160                  * score entry.
02161                  */
02162                 rpmMessage(RPMMESS_DEBUG,
02163                     _("Attempting to mark %s as erased in score board(0x%x).\n"),
02164                     rpmteN(psm->te), (unsigned) score);
02165                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
02166                 if (se != NULL) se->erased = 1;
02167             }
02168         }
02169 
02170         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02171         break;
02172 
02173     default:
02174         break;
02175 /*@i@*/    }
02176     /*@=branchstate@*/
02177 
02178     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
02179     return rc;
02180     /*@=nullstate@*/
02181 }
02182 /*@=bounds =nullpass@*/

Generated on Thu Apr 24 01:17:58 2008 for rpm by  doxygen 1.5.2