perform.c revision 30221
1327Sjkh#ifndef lint 230221Scharnierstatic const char rcsid[] = 330221Scharnier "$Id: perform.c,v 1.42 1997/06/29 10:41:44 jkh Exp $"; 4327Sjkh#endif 5327Sjkh 6327Sjkh/* 7327Sjkh * FreeBSD install - a package for the installation and maintainance 8327Sjkh * of non-core utilities. 9327Sjkh * 10327Sjkh * Redistribution and use in source and binary forms, with or without 11327Sjkh * modification, are permitted provided that the following conditions 12327Sjkh * are met: 13327Sjkh * 1. Redistributions of source code must retain the above copyright 14327Sjkh * notice, this list of conditions and the following disclaimer. 15327Sjkh * 2. Redistributions in binary form must reproduce the above copyright 16327Sjkh * notice, this list of conditions and the following disclaimer in the 17327Sjkh * documentation and/or other materials provided with the distribution. 18327Sjkh * 19327Sjkh * Jordan K. Hubbard 20327Sjkh * 18 July 1993 21327Sjkh * 22327Sjkh * This is the main body of the add module. 23327Sjkh * 24327Sjkh */ 25327Sjkh 2630221Scharnier#include <err.h> 27327Sjkh#include "lib.h" 28327Sjkh#include "add.h" 29327Sjkh 30327Sjkh#include <signal.h> 314996Sjkh#include <sys/wait.h> 32327Sjkh 33327Sjkhstatic int pkg_do(char *); 34327Sjkhstatic int sanity_check(char *); 35327Sjkhstatic char LogDir[FILENAME_MAX]; 36327Sjkh 37327Sjkhint 38327Sjkhpkg_perform(char **pkgs) 39327Sjkh{ 40327Sjkh int i, err_cnt = 0; 41327Sjkh 42327Sjkh signal(SIGINT, cleanup); 43327Sjkh signal(SIGHUP, cleanup); 44327Sjkh 45382Sjkh if (AddMode == SLAVE) 46382Sjkh err_cnt = pkg_do(NULL); 47382Sjkh else { 48382Sjkh for (i = 0; pkgs[i]; i++) 49382Sjkh err_cnt += pkg_do(pkgs[i]); 50382Sjkh } 51327Sjkh return err_cnt; 52327Sjkh} 53327Sjkh 54327Sjkhstatic Package Plist; 5511780Sjkhstatic char *Home; 56327Sjkh 578083Sjkh/* 588083Sjkh * This is seriously ugly code following. Written very fast! 598083Sjkh * [And subsequently made even worse.. Sigh! This code was just born 608083Sjkh * to be hacked, I guess.. :) -jkh] 618083Sjkh */ 62327Sjkhstatic int 63327Sjkhpkg_do(char *pkg) 64327Sjkh{ 65327Sjkh char pkg_fullname[FILENAME_MAX]; 6611780Sjkh char playpen[FILENAME_MAX]; 677998Sjkh char extract_contents[FILENAME_MAX]; 6814582Sjkh char *where_to, *tmp, *extract; 69327Sjkh FILE *cfile; 7011780Sjkh int code; 711545Sjkh PackingList p; 723364Sjkh struct stat sb; 7311780Sjkh int inPlace; 74327Sjkh 7511780Sjkh code = 0; 76327Sjkh LogDir[0] = '\0'; 7711780Sjkh strcpy(playpen, FirstPen); 7811780Sjkh inPlace = 0; 798075Sjkh 808083Sjkh /* Are we coming in for a second pass, everything already extracted? */ 8111780Sjkh if (!pkg) { 8211780Sjkh fgets(playpen, FILENAME_MAX, stdin); 8311780Sjkh playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */ 8411780Sjkh if (chdir(playpen) == FAIL) { 8530221Scharnier warnx("pkg_add in SLAVE mode can't chdir to %s", playpen); 86382Sjkh return 1; 87382Sjkh } 88382Sjkh read_plist(&Plist, stdin); 8911780Sjkh where_to = playpen; 90327Sjkh } 918083Sjkh /* Nope - do it now */ 92382Sjkh else { 9311780Sjkh /* Is it an ftp://foo.bar.baz/file.tgz specification? */ 948083Sjkh if (isURL(pkg)) { 9511780Sjkh if (!(Home = fileGetURL(NULL, pkg))) { 9630221Scharnier warnx("unable to fetch `%s' by URL", pkg); 978083Sjkh return 1; 988083Sjkh } 9911780Sjkh where_to = Home; 10011780Sjkh strcpy(pkg_fullname, pkg); 10111780Sjkh cfile = fopen(CONTENTS_FNAME, "r"); 10211780Sjkh if (!cfile) { 10330221Scharnier warnx( 10430221Scharnier "unable to open table of contents file `%s' - not a package?", 10530221Scharnier CONTENTS_FNAME); 10611780Sjkh goto bomb; 10711780Sjkh } 10811780Sjkh read_plist(&Plist, cfile); 10911780Sjkh fclose(cfile); 110382Sjkh } 1118083Sjkh else { 11214582Sjkh strcpy(pkg_fullname, pkg); /* copy for sanity's sake, could remove pkg_fullname */ 11314582Sjkh if (strcmp(pkg, "-")) { 11414582Sjkh if (stat(pkg_fullname, &sb) == FAIL) { 11530221Scharnier warnx("can't stat package file '%s'", pkg_fullname); 11614582Sjkh goto bomb; 11714582Sjkh } 11814582Sjkh sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME); 11914582Sjkh extract = extract_contents; 12011780Sjkh } 12122750Sjkh else { 12214582Sjkh extract = NULL; 12322750Sjkh sb.st_size = 100000; /* Make up a plausible average size */ 12422750Sjkh } 12511780Sjkh Home = make_playpen(playpen, sb.st_size * 4); 12611780Sjkh if (!Home) 12730221Scharnier warnx("unable to make playpen for %d bytes", sb.st_size * 4); 12811780Sjkh where_to = Home; 12914582Sjkh if (unpack(pkg_fullname, extract)) { 13030221Scharnier warnx( 13130221Scharnier "unable to extract table of contents file from `%s' - not a package?", 13230221Scharnier pkg_fullname); 13311780Sjkh goto bomb; 13411780Sjkh } 13511780Sjkh cfile = fopen(CONTENTS_FNAME, "r"); 13611780Sjkh if (!cfile) { 13730221Scharnier warnx( 13830221Scharnier "unable to open table of contents file `%s' - not a package?", 13930221Scharnier CONTENTS_FNAME); 14011780Sjkh goto bomb; 14111780Sjkh } 14211780Sjkh read_plist(&Plist, cfile); 14311780Sjkh fclose(cfile); 1447998Sjkh 14511780Sjkh /* Extract directly rather than moving? Oh goodie! */ 14611780Sjkh if (find_plist_option(&Plist, "extract-in-place")) { 14711780Sjkh if (Verbose) 14811780Sjkh printf("Doing in-place extraction for %s\n", pkg_fullname); 14911780Sjkh p = find_plist(&Plist, PLIST_CWD); 15011780Sjkh if (p) { 15111780Sjkh if (!isdir(p->name) && !Fake) { 15211780Sjkh if (Verbose) 15311780Sjkh printf("Desired prefix of %s does not exist, creating..\n", p->name); 15411780Sjkh vsystem("mkdir -p %s", p->name); 15511780Sjkh if (chdir(p->name) == -1) { 15630221Scharnier warn("unable to change directory to `%s'", p->name); 15711780Sjkh goto bomb; 15811780Sjkh } 1597998Sjkh } 16011780Sjkh where_to = p->name; 16111780Sjkh inPlace = 1; 1627998Sjkh } 16311780Sjkh else { 16430221Scharnier warnx( 16530221Scharnier "no prefix specified in `%s' - this is a bad package!", 16630221Scharnier pkg_fullname); 16711780Sjkh goto bomb; 16811780Sjkh } 1697998Sjkh } 17011780Sjkh 17111780Sjkh /* 17211780Sjkh * Apply a crude heuristic to see how much space the package will 17311780Sjkh * take up once it's unpacked. I've noticed that most packages 17411780Sjkh * compress an average of 75%, so multiply by 4 for good measure. 17511780Sjkh */ 17611780Sjkh 17714582Sjkh if (!inPlace && min_free(playpen) < sb.st_size * 4) { 17830221Scharnier warnx("projected size of %d exceeds available free space.\n" 17930221Scharnier"Please set your PKG_TMPDIR variable to point to a location with more\n" 18030221Scharnier "free space and try again", sb.st_size * 4); 18130221Scharnier warnx("not extracting %s\ninto %s, sorry!", 18230221Scharnier pkg_fullname, where_to); 1838083Sjkh goto bomb; 1847998Sjkh } 1859786Sjkh 18611780Sjkh /* If this is a direct extract and we didn't want it, stop now */ 18711780Sjkh if (inPlace && Fake) 18811780Sjkh goto success; 1897998Sjkh 19011780Sjkh /* Finally unpack the whole mess */ 19111780Sjkh if (unpack(pkg_fullname, NULL)) { 19230221Scharnier warnx("unable to extract `%s'!", pkg_fullname); 19311780Sjkh goto bomb; 19411780Sjkh } 19511780Sjkh } 19614582Sjkh 19711780Sjkh /* Check for sanity and dependencies */ 19811780Sjkh if (sanity_check(pkg)) 1997998Sjkh goto bomb; 20011780Sjkh 20111780Sjkh /* If we're running in MASTER mode, just output the plist and return */ 20211780Sjkh if (AddMode == MASTER) { 20311780Sjkh printf("%s\n", where_playpen()); 20411780Sjkh write_plist(&Plist, stdout); 20511780Sjkh return 0; 2067996Sjkh } 20711780Sjkh } 208327Sjkh 20911780Sjkh /* 21011780Sjkh * If we have a prefix, delete the first one we see and add this 21111780Sjkh * one in place of it. 21211780Sjkh */ 21311780Sjkh if (Prefix) { 21411780Sjkh delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 21511780Sjkh add_plist_top(&Plist, PLIST_CWD, Prefix); 21611780Sjkh } 2177998Sjkh 21811780Sjkh setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1); 21911780Sjkh /* Protect against old packages with bogus @name fields */ 22011780Sjkh PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous"; 2217998Sjkh 22211780Sjkh /* See if we're already registered */ 22311780Sjkh sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 22417373Sjkh if (isdir(LogDir) && !Force) { 22530221Scharnier warnx("package `%s' already recorded as installed", PkgName); 22611780Sjkh code = 1; 22711780Sjkh goto success; /* close enough for government work */ 22811780Sjkh } 2297998Sjkh 23011780Sjkh /* Now check the packing list for dependencies */ 23111780Sjkh for (p = Plist.head; p ; p = p->next) { 23211780Sjkh if (p->type != PLIST_PKGDEP) 23311780Sjkh continue; 23411780Sjkh if (Verbose) 23511780Sjkh printf("Package `%s' depends on `%s'.\n", PkgName, p->name); 23618929Sjkh if (vsystem("pkg_info -e %s", p->name)) { 23711780Sjkh char path[FILENAME_MAX], *cp = NULL; 2388083Sjkh 23918929Sjkh if (!Fake) { 24018929Sjkh if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) { 24118929Sjkh snprintf(path, FILENAME_MAX, "%s/%s.tgz", Home, p->name); 24218929Sjkh if (fexists(path)) 24318929Sjkh cp = path; 24418929Sjkh else 24518929Sjkh cp = fileFindByPath(pkg, p->name); 24618929Sjkh if (cp) { 24718929Sjkh if (Verbose) 24818929Sjkh printf("Loading it from %s.\n", cp); 24918929Sjkh if (vsystem("pkg_add %s", cp)) { 25030221Scharnier warnx("autoload of dependency `%s' failed%s", 25130221Scharnier cp, Force ? " (proceeding anyway)" : "!"); 25218929Sjkh if (!Force) 25318929Sjkh ++code; 25418929Sjkh } 25518929Sjkh } 25618929Sjkh } 25718929Sjkh else if ((cp = fileGetURL(pkg, p->name)) != NULL) { 2588075Sjkh if (Verbose) 25918929Sjkh printf("Finished loading %s over FTP.\n", p->name); 26018929Sjkh if (!fexists("+CONTENTS")) { 26130221Scharnier warnx("autoloaded package %s has no +CONTENTS file?", 26230221Scharnier p->name); 2638075Sjkh if (!Force) 2648075Sjkh ++code; 2658075Sjkh } 26618929Sjkh else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) { 26730221Scharnier warnx("pkg_add of dependency `%s' failed%s", 26830221Scharnier p->name, Force ? " (proceeding anyway)" : "!"); 26918929Sjkh if (!Force) 27018929Sjkh ++code; 27118929Sjkh } 27218929Sjkh else if (Verbose) 27318929Sjkh printf("\t`%s' loaded successfully.\n", p->name); 27418929Sjkh /* Nuke the temporary playpen */ 27518929Sjkh leave_playpen(cp); 2768075Sjkh } 27711780Sjkh } 27811780Sjkh else { 27911780Sjkh if (Verbose) 28011780Sjkh printf("and was not found%s.\n", Force ? " (proceeding anyway)" : ""); 28111780Sjkh else 28211780Sjkh printf("Package dependency %s for %s not found%s\n", p->name, pkg, 28311780Sjkh Force ? " (proceeding anyway)" : "!"); 28411780Sjkh if (!Force) 28511780Sjkh ++code; 28611780Sjkh } 2878075Sjkh } 28811780Sjkh else if (Verbose) 28911780Sjkh printf(" - already installed.\n"); 290327Sjkh } 2917713Sjkh 29216087Sjkh if (code != 0) 29316087Sjkh goto bomb; 29416087Sjkh 2958075Sjkh /* Look for the requirements file */ 296327Sjkh if (fexists(REQUIRE_FNAME)) { 297327Sjkh vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 298327Sjkh if (Verbose) 299327Sjkh printf("Running requirements file first for %s..\n", PkgName); 300545Sjkh if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 30130221Scharnier warnx("package %s fails requirements %s", pkg_fullname, 30230221Scharnier Force ? "installing anyway" : "- not installed"); 3034996Sjkh if (!Force) { 3044996Sjkh code = 1; 3054996Sjkh goto success; /* close enough for government work */ 3064996Sjkh } 307327Sjkh } 308327Sjkh } 3098075Sjkh 3108075Sjkh /* If we're really installing, and have an installation file, run it */ 311327Sjkh if (!NoInstall && fexists(INSTALL_FNAME)) { 312327Sjkh vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 313327Sjkh if (Verbose) 314327Sjkh printf("Running install with PRE-INSTALL for %s..\n", PkgName); 315545Sjkh if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 31630221Scharnier warnx("install script returned error status"); 3177998Sjkh unlink(INSTALL_FNAME); 3184996Sjkh code = 1; 3194996Sjkh goto success; /* nothing to uninstall yet */ 320327Sjkh } 321327Sjkh } 3228075Sjkh 3238075Sjkh /* Now finally extract the entire show if we're not going direct */ 32411780Sjkh if (!inPlace && !Fake) 32511780Sjkh extract_plist(".", &Plist); 3268075Sjkh 3278075Sjkh if (!Fake && fexists(MTREE_FNAME)) { 3284996Sjkh if (Verbose) 3294996Sjkh printf("Running mtree for %s..\n", PkgName); 3304996Sjkh p = find_plist(&Plist, PLIST_CWD); 3314996Sjkh if (Verbose) 33211780Sjkh printf("mtree -U -f %s -d -e -p %s\n", MTREE_FNAME, p ? p->name : "/"); 3338115Sjkh if (!Fake) { 33411780Sjkh if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s", MTREE_FNAME, p ? p->name : "/")) 33530221Scharnier warnx("mtree returned a non-zero status - continuing"); 3368115Sjkh } 3377998Sjkh unlink(MTREE_FNAME); 3384996Sjkh } 3398075Sjkh 3408083Sjkh /* Run the installation script one last time? */ 341327Sjkh if (!NoInstall && fexists(INSTALL_FNAME)) { 342327Sjkh if (Verbose) 343327Sjkh printf("Running install with POST-INSTALL for %s..\n", PkgName); 344545Sjkh if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 34530221Scharnier warnx("install script returned error status"); 3467998Sjkh unlink(INSTALL_FNAME); 3474996Sjkh code = 1; 348327Sjkh goto fail; 349327Sjkh } 3507998Sjkh unlink(INSTALL_FNAME); 351327Sjkh } 3528075Sjkh 3538083Sjkh /* Time to record the deed? */ 354327Sjkh if (!NoRecord && !Fake) { 355382Sjkh char contents[FILENAME_MAX]; 356382Sjkh FILE *cfile; 357382Sjkh 3584996Sjkh umask(022); 359327Sjkh if (getuid() != 0) 36030221Scharnier warnx("not running as root - trying to record install anyway"); 361327Sjkh if (!PkgName) { 36230221Scharnier warnx("no package name! can't record package, sorry"); 363327Sjkh code = 1; 364327Sjkh goto success; /* well, partial anyway */ 365327Sjkh } 36611780Sjkh sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 367327Sjkh if (Verbose) 368327Sjkh printf("Attempting to record package into %s..\n", LogDir); 369327Sjkh if (make_hierarchy(LogDir)) { 37030221Scharnier warnx("can't record package into '%s', you're on your own!", 371327Sjkh LogDir); 372327Sjkh bzero(LogDir, FILENAME_MAX); 373327Sjkh code = 1; 374327Sjkh goto success; /* close enough for government work */ 375327Sjkh } 376477Sjkh /* Make sure pkg_info can read the entry */ 377477Sjkh vsystem("chmod a+rx %s", LogDir); 378327Sjkh if (fexists(DEINSTALL_FNAME)) 3797998Sjkh move_file(".", DEINSTALL_FNAME, LogDir); 380327Sjkh if (fexists(REQUIRE_FNAME)) 3817998Sjkh move_file(".", REQUIRE_FNAME, LogDir); 382382Sjkh sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 383382Sjkh cfile = fopen(contents, "w"); 384382Sjkh if (!cfile) { 38530221Scharnier warnx("can't open new contents file '%s'! can't register pkg", 38630221Scharnier contents); 387382Sjkh goto success; /* can't log, but still keep pkg */ 388382Sjkh } 389382Sjkh write_plist(&Plist, cfile); 390382Sjkh fclose(cfile); 3917998Sjkh move_file(".", DESC_FNAME, LogDir); 3927998Sjkh move_file(".", COMMENT_FNAME, LogDir); 3934996Sjkh if (fexists(DISPLAY_FNAME)) 3947998Sjkh move_file(".", DISPLAY_FNAME, LogDir); 3954996Sjkh for (p = Plist.head; p ; p = p->next) { 3964996Sjkh if (p->type != PLIST_PKGDEP) 3974996Sjkh continue; 3984996Sjkh if (Verbose) 39911780Sjkh printf("Attempting to record dependency on package `%s'\n", p->name); 40011780Sjkh sprintf(contents, "%s/%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, 4017937Sjkh basename_of(p->name), REQUIRED_BY_FNAME); 4024996Sjkh cfile = fopen(contents, "a"); 4039202Srgrimes if (!cfile) 40430221Scharnier warnx("can't open dependency file '%s'!\n" 40530221Scharnier "dependency registration is incomplete", contents); 4069202Srgrimes else { 40711780Sjkh fprintf(cfile, "%s\n", PkgName); 4089202Srgrimes if (fclose(cfile) == EOF) 40930221Scharnier warnx("cannot properly close file %s", contents); 4104996Sjkh } 4114996Sjkh } 412327Sjkh if (Verbose) 413327Sjkh printf("Package %s registered in %s\n", PkgName, LogDir); 414327Sjkh } 4158857Srgrimes 41616549Sjkh if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) { 4174996Sjkh FILE *fp; 4184996Sjkh char buf[BUFSIZ]; 41916549Sjkh 42021650Sjkh snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name); 42121650Sjkh fp = fopen(buf, "r"); 4224996Sjkh if (fp) { 4234996Sjkh putc('\n', stdout); 4244996Sjkh while (fgets(buf, sizeof(buf), fp)) 4254996Sjkh fputs(buf, stdout); 4264996Sjkh putc('\n', stdout); 4274996Sjkh (void) fclose(fp); 4284996Sjkh } else 42930221Scharnier warnx("cannot open %s as display file", buf); 4304996Sjkh } 4314996Sjkh 432327Sjkh goto success; 433327Sjkh 4347998Sjkh bomb: 4357998Sjkh code = 1; 4367998Sjkh goto success; 4377998Sjkh 438327Sjkh fail: 4394996Sjkh /* Nuke the whole (installed) show, XXX but don't clean directories */ 440327Sjkh if (!Fake) 4414996Sjkh delete_package(FALSE, FALSE, &Plist); 442327Sjkh 443327Sjkh success: 444327Sjkh /* delete the packing list contents */ 44511780Sjkh free_plist(&Plist); 44611780Sjkh leave_playpen(Home); 447327Sjkh return code; 448327Sjkh} 449327Sjkh 450327Sjkhstatic int 451327Sjkhsanity_check(char *pkg) 452327Sjkh{ 4538075Sjkh int code = 0; 4548075Sjkh 455327Sjkh if (!fexists(CONTENTS_FNAME)) { 45630221Scharnier warnx("package %s has no CONTENTS file!", pkg); 4578075Sjkh code = 1; 458327Sjkh } 4598075Sjkh else if (!fexists(COMMENT_FNAME)) { 46030221Scharnier warnx("package %s has no COMMENT file!", pkg); 4618075Sjkh code = 1; 462327Sjkh } 4638075Sjkh else if (!fexists(DESC_FNAME)) { 46430221Scharnier warnx("package %s has no DESC file!", pkg); 4658075Sjkh code = 1; 466327Sjkh } 4678075Sjkh return code; 468327Sjkh} 469327Sjkh 470327Sjkhvoid 471327Sjkhcleanup(int signo) 472327Sjkh{ 473382Sjkh if (signo) 474382Sjkh printf("Signal %d received, cleaning up..\n", signo); 475327Sjkh if (!Fake && LogDir[0]) 476327Sjkh vsystem("%s -rf %s", REMOVE_CMD, LogDir); 47711780Sjkh leave_playpen(Home); 478327Sjkh} 479