Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

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

Generated on Mon Nov 1 21:57:35 2004 for rpm by  doxygen 1.3.9.1