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

Generated on Tue Jan 27 15:19:08 2009 for rpm by  doxygen 1.4.7