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

Generated on Fri Oct 12 08:44:53 2007 for rpm by  doxygen 1.5.2