perform.c revision 16087
1327Sjkh#ifndef lint 216087Sjkhstatic const char *rcsid = "$Id: perform.c,v 1.33 1996/03/12 06:12:37 jkh Exp $"; 3327Sjkh#endif 4327Sjkh 5327Sjkh/* 6327Sjkh * FreeBSD install - a package for the installation and maintainance 7327Sjkh * of non-core utilities. 8327Sjkh * 9327Sjkh * Redistribution and use in source and binary forms, with or without 10327Sjkh * modification, are permitted provided that the following conditions 11327Sjkh * are met: 12327Sjkh * 1. Redistributions of source code must retain the above copyright 13327Sjkh * notice, this list of conditions and the following disclaimer. 14327Sjkh * 2. Redistributions in binary form must reproduce the above copyright 15327Sjkh * notice, this list of conditions and the following disclaimer in the 16327Sjkh * documentation and/or other materials provided with the distribution. 17327Sjkh * 18327Sjkh * Jordan K. Hubbard 19327Sjkh * 18 July 1993 20327Sjkh * 21327Sjkh * This is the main body of the add module. 22327Sjkh * 23327Sjkh */ 24327Sjkh 25327Sjkh#include "lib.h" 26327Sjkh#include "add.h" 27327Sjkh 28327Sjkh#include <signal.h> 294996Sjkh#include <sys/wait.h> 30327Sjkh 31327Sjkhstatic int pkg_do(char *); 32327Sjkhstatic int sanity_check(char *); 33327Sjkhstatic char LogDir[FILENAME_MAX]; 34327Sjkh 35327Sjkhint 36327Sjkhpkg_perform(char **pkgs) 37327Sjkh{ 38327Sjkh int i, err_cnt = 0; 39327Sjkh 40327Sjkh signal(SIGINT, cleanup); 41327Sjkh signal(SIGHUP, cleanup); 42327Sjkh 43382Sjkh if (AddMode == SLAVE) 44382Sjkh err_cnt = pkg_do(NULL); 45382Sjkh else { 46382Sjkh for (i = 0; pkgs[i]; i++) 47382Sjkh err_cnt += pkg_do(pkgs[i]); 48382Sjkh } 49327Sjkh return err_cnt; 50327Sjkh} 51327Sjkh 52327Sjkhstatic Package Plist; 5311780Sjkhstatic char *Home; 54327Sjkh 558083Sjkh/* 568083Sjkh * This is seriously ugly code following. Written very fast! 578083Sjkh * [And subsequently made even worse.. Sigh! This code was just born 588083Sjkh * to be hacked, I guess.. :) -jkh] 598083Sjkh */ 60327Sjkhstatic int 61327Sjkhpkg_do(char *pkg) 62327Sjkh{ 63327Sjkh char pkg_fullname[FILENAME_MAX]; 6411780Sjkh char playpen[FILENAME_MAX]; 657998Sjkh char extract_contents[FILENAME_MAX]; 6614582Sjkh char *where_to, *tmp, *extract; 67327Sjkh FILE *cfile; 6811780Sjkh int code; 691545Sjkh PackingList p; 703364Sjkh struct stat sb; 718083Sjkh char *isTMP = NULL; 7211780Sjkh char *cp; 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) { 8511780Sjkh whinge("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))) { 968083Sjkh whinge("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) { 10311780Sjkh whinge("Unable to open table of contents file `%s' - not a package?", CONTENTS_FNAME); 10411780Sjkh goto bomb; 10511780Sjkh } 10611780Sjkh read_plist(&Plist, cfile); 10711780Sjkh fclose(cfile); 108382Sjkh } 1098083Sjkh else { 11014582Sjkh strcpy(pkg_fullname, pkg); /* copy for sanity's sake, could remove pkg_fullname */ 11114582Sjkh if (strcmp(pkg, "-")) { 11214582Sjkh if (stat(pkg_fullname, &sb) == FAIL) { 11314582Sjkh whinge("Can't stat package file '%s'.", pkg_fullname); 11414582Sjkh goto bomb; 11514582Sjkh } 11614582Sjkh sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME); 11714582Sjkh extract = extract_contents; 11811780Sjkh } 11914582Sjkh else 12014582Sjkh extract = NULL; 12111780Sjkh Home = make_playpen(playpen, sb.st_size * 4); 12211780Sjkh if (!Home) 12311780Sjkh whinge("Unable to make playpen for %d bytes.\n", sb.st_size * 4); 12411780Sjkh where_to = Home; 12514582Sjkh if (unpack(pkg_fullname, extract)) { 12611780Sjkh whinge("Unable to extract table of contents file from `%s' - not a package?.", pkg_fullname); 12711780Sjkh goto bomb; 12811780Sjkh } 12911780Sjkh cfile = fopen(CONTENTS_FNAME, "r"); 13011780Sjkh if (!cfile) { 13111780Sjkh whinge("Unable to open table of contents file `%s' - not a package?", CONTENTS_FNAME); 13211780Sjkh goto bomb; 13311780Sjkh } 13411780Sjkh read_plist(&Plist, cfile); 13511780Sjkh fclose(cfile); 1367998Sjkh 13711780Sjkh /* Extract directly rather than moving? Oh goodie! */ 13811780Sjkh if (find_plist_option(&Plist, "extract-in-place")) { 13911780Sjkh if (Verbose) 14011780Sjkh printf("Doing in-place extraction for %s\n", pkg_fullname); 14111780Sjkh p = find_plist(&Plist, PLIST_CWD); 14211780Sjkh if (p) { 14311780Sjkh if (!isdir(p->name) && !Fake) { 14411780Sjkh if (Verbose) 14511780Sjkh printf("Desired prefix of %s does not exist, creating..\n", p->name); 14611780Sjkh vsystem("mkdir -p %s", p->name); 14711780Sjkh if (chdir(p->name) == -1) { 14811780Sjkh whinge("Unable to change directory to `%s' - no permission?", p->name); 14911780Sjkh perror("chdir"); 15011780Sjkh goto bomb; 15111780Sjkh } 1527998Sjkh } 15311780Sjkh where_to = p->name; 15411780Sjkh inPlace = 1; 1557998Sjkh } 15611780Sjkh else { 15711780Sjkh whinge("No prefix specified in `%s' - this is a bad package!", pkg_fullname); 15811780Sjkh goto bomb; 15911780Sjkh } 1607998Sjkh } 16111780Sjkh 16211780Sjkh /* 16311780Sjkh * Apply a crude heuristic to see how much space the package will 16411780Sjkh * take up once it's unpacked. I've noticed that most packages 16511780Sjkh * compress an average of 75%, so multiply by 4 for good measure. 16611780Sjkh */ 16711780Sjkh 16814582Sjkh if (!inPlace && min_free(playpen) < sb.st_size * 4) { 16911780Sjkh whinge("Projected size of %d exceeds available free space.\n" 17011780Sjkh "Please set your PKG_TMPDIR variable to point to a location with more\n" 17111780Sjkh "free space and try again.", sb.st_size * 4); 17211780Sjkh whinge("Not extracting %s\ninto %s, sorry!", pkg_fullname, where_to); 1738083Sjkh goto bomb; 1747998Sjkh } 1759786Sjkh 17611780Sjkh /* If this is a direct extract and we didn't want it, stop now */ 17711780Sjkh if (inPlace && Fake) 17811780Sjkh goto success; 1797998Sjkh 18011780Sjkh /* Finally unpack the whole mess */ 18111780Sjkh if (unpack(pkg_fullname, NULL)) { 18211780Sjkh whinge("Unable to extract `%s'!", pkg_fullname); 18311780Sjkh goto bomb; 18411780Sjkh } 18511780Sjkh } 18614582Sjkh 18711780Sjkh /* Check for sanity and dependencies */ 18811780Sjkh if (sanity_check(pkg)) 1897998Sjkh goto bomb; 19011780Sjkh 19111780Sjkh /* If we're running in MASTER mode, just output the plist and return */ 19211780Sjkh if (AddMode == MASTER) { 19311780Sjkh printf("%s\n", where_playpen()); 19411780Sjkh write_plist(&Plist, stdout); 19511780Sjkh return 0; 1967996Sjkh } 19711780Sjkh } 198327Sjkh 19911780Sjkh /* 20011780Sjkh * If we have a prefix, delete the first one we see and add this 20111780Sjkh * one in place of it. 20211780Sjkh */ 20311780Sjkh if (Prefix) { 20411780Sjkh delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 20511780Sjkh add_plist_top(&Plist, PLIST_CWD, Prefix); 20611780Sjkh } 2077998Sjkh 20811780Sjkh setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1); 20911780Sjkh /* Protect against old packages with bogus @name fields */ 21011780Sjkh PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous"; 2117998Sjkh 21211780Sjkh /* See if we're already registered */ 21311780Sjkh sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 21411780Sjkh if (isdir(LogDir)) { 21511780Sjkh char tmp[FILENAME_MAX]; 21611780Sjkh 21711780Sjkh whinge("Package `%s' already recorded as installed.\n", PkgName); 21811780Sjkh code = 1; 21911780Sjkh goto success; /* close enough for government work */ 22011780Sjkh } 2217998Sjkh 22211780Sjkh /* Now check the packing list for dependencies */ 22311780Sjkh for (p = Plist.head; p ; p = p->next) { 22411780Sjkh if (p->type != PLIST_PKGDEP) 22511780Sjkh continue; 22611780Sjkh if (Verbose) 22711780Sjkh printf("Package `%s' depends on `%s'.\n", PkgName, p->name); 22811780Sjkh if (!Fake && vsystem("pkg_info -e %s", p->name)) { 22911780Sjkh char path[FILENAME_MAX], *cp = NULL; 2308083Sjkh 23112219Sjkh if (!Fake && !isURL(pkg) && !getenv("PKG_ADD_BASE")) { 23211780Sjkh snprintf(path, FILENAME_MAX, "%s/%s.tgz", Home, p->name); 23311780Sjkh if (fexists(path)) 23411780Sjkh cp = path; 23511780Sjkh else 23611780Sjkh cp = fileFindByPath(pkg, p->name); 2378083Sjkh if (cp) { 2388075Sjkh if (Verbose) 23911780Sjkh printf("Loading it from %s.\n", cp); 24011780Sjkh if (vsystem("pkg_add %s", cp)) { 24111780Sjkh whinge("Autoload of dependency `%s' failed%s", cp, Force ? " (proceeding anyway)" : "!"); 2428075Sjkh if (!Force) 2438075Sjkh ++code; 2448075Sjkh } 2458075Sjkh } 24611780Sjkh } 24711780Sjkh else if (!Fake && (cp = fileGetURL(pkg, p->name)) != NULL) { 24811780Sjkh if (Verbose) 24911780Sjkh printf("Finished loading %s over FTP.\n", p->name); 25012219Sjkh if (!Fake) { 25112219Sjkh if (!fexists("+CONTENTS")) 25212219Sjkh whinge("Autoloaded package %s has no +CONTENTS file?", p->name); 25312219Sjkh else 25412219Sjkh if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) { 25512219Sjkh whinge("pkg_add of dependency `%s' failed%s", 25612219Sjkh p->name, Force ? " (proceeding anyway)" : "!"); 25712219Sjkh if (!Force) 25812219Sjkh ++code; 25912219Sjkh } 26012219Sjkh else if (Verbose) 26112219Sjkh printf("\t`%s' loaded successfully.\n", p->name); 2628075Sjkh } 26311780Sjkh /* Nuke the temporary playpen */ 26411780Sjkh leave_playpen(cp); 2658075Sjkh } 26611780Sjkh else { 26711780Sjkh if (Verbose) 26811780Sjkh printf("and was not found%s.\n", Force ? " (proceeding anyway)" : ""); 26911780Sjkh else 27011780Sjkh printf("Package dependency %s for %s not found%s\n", p->name, pkg, 27111780Sjkh Force ? " (proceeding anyway)" : "!"); 27211780Sjkh if (!Force) 27311780Sjkh ++code; 27411780Sjkh } 2758075Sjkh } 27611780Sjkh else if (Verbose) 27711780Sjkh printf(" - already installed.\n"); 278327Sjkh } 2797713Sjkh 28016087Sjkh if (code != 0) 28116087Sjkh goto bomb; 28216087Sjkh 2838075Sjkh /* Look for the requirements file */ 284327Sjkh if (fexists(REQUIRE_FNAME)) { 285327Sjkh vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 286327Sjkh if (Verbose) 287327Sjkh printf("Running requirements file first for %s..\n", PkgName); 288545Sjkh if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 28911780Sjkh whinge("Package %s fails requirements %s", pkg_fullname, 2904996Sjkh Force ? "installing anyway" : "- not installed."); 2914996Sjkh if (!Force) { 2924996Sjkh code = 1; 2934996Sjkh goto success; /* close enough for government work */ 2944996Sjkh } 295327Sjkh } 296327Sjkh } 2978075Sjkh 2988075Sjkh /* If we're really installing, and have an installation file, run it */ 299327Sjkh if (!NoInstall && fexists(INSTALL_FNAME)) { 300327Sjkh vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 301327Sjkh if (Verbose) 302327Sjkh printf("Running install with PRE-INSTALL for %s..\n", PkgName); 303545Sjkh if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 304327Sjkh whinge("Install script returned error status."); 3057998Sjkh unlink(INSTALL_FNAME); 3064996Sjkh code = 1; 3074996Sjkh goto success; /* nothing to uninstall yet */ 308327Sjkh } 309327Sjkh } 3108075Sjkh 3118075Sjkh /* Now finally extract the entire show if we're not going direct */ 31211780Sjkh if (!inPlace && !Fake) 31311780Sjkh extract_plist(".", &Plist); 3148075Sjkh 3158075Sjkh if (!Fake && fexists(MTREE_FNAME)) { 3164996Sjkh if (Verbose) 3174996Sjkh printf("Running mtree for %s..\n", PkgName); 3184996Sjkh p = find_plist(&Plist, PLIST_CWD); 3194996Sjkh if (Verbose) 32011780Sjkh printf("mtree -U -f %s -d -e -p %s\n", MTREE_FNAME, p ? p->name : "/"); 3218115Sjkh if (!Fake) { 32211780Sjkh if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s", MTREE_FNAME, p ? p->name : "/")) 3238115Sjkh whinge("mtree returned a non-zero status - continuing."); 3248115Sjkh } 3257998Sjkh unlink(MTREE_FNAME); 3264996Sjkh } 3278075Sjkh 3288083Sjkh /* Run the installation script one last time? */ 329327Sjkh if (!NoInstall && fexists(INSTALL_FNAME)) { 330327Sjkh if (Verbose) 331327Sjkh printf("Running install with POST-INSTALL for %s..\n", PkgName); 332545Sjkh if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 333327Sjkh whinge("Install script returned error status."); 3347998Sjkh unlink(INSTALL_FNAME); 3354996Sjkh code = 1; 336327Sjkh goto fail; 337327Sjkh } 3387998Sjkh unlink(INSTALL_FNAME); 339327Sjkh } 3408075Sjkh 3418083Sjkh /* Time to record the deed? */ 342327Sjkh if (!NoRecord && !Fake) { 343382Sjkh char contents[FILENAME_MAX]; 344382Sjkh FILE *cfile; 345382Sjkh 3464996Sjkh umask(022); 347327Sjkh if (getuid() != 0) 348327Sjkh whinge("Not running as root - trying to record install anyway."); 349327Sjkh if (!PkgName) { 350327Sjkh whinge("No package name! Can't record package, sorry."); 351327Sjkh code = 1; 352327Sjkh goto success; /* well, partial anyway */ 353327Sjkh } 35411780Sjkh sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 355327Sjkh if (Verbose) 356327Sjkh printf("Attempting to record package into %s..\n", LogDir); 357327Sjkh if (make_hierarchy(LogDir)) { 358327Sjkh whinge("Can't record package into '%s', you're on your own!", 359327Sjkh LogDir); 360327Sjkh bzero(LogDir, FILENAME_MAX); 361327Sjkh code = 1; 362327Sjkh goto success; /* close enough for government work */ 363327Sjkh } 364477Sjkh /* Make sure pkg_info can read the entry */ 365477Sjkh vsystem("chmod a+rx %s", LogDir); 366327Sjkh if (fexists(DEINSTALL_FNAME)) 3677998Sjkh move_file(".", DEINSTALL_FNAME, LogDir); 368327Sjkh if (fexists(REQUIRE_FNAME)) 3697998Sjkh move_file(".", REQUIRE_FNAME, LogDir); 370382Sjkh sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 371382Sjkh cfile = fopen(contents, "w"); 372382Sjkh if (!cfile) { 37311780Sjkh whinge("Can't open new contents file '%s'! Can't register pkg.", contents); 374382Sjkh goto success; /* can't log, but still keep pkg */ 375382Sjkh } 376382Sjkh write_plist(&Plist, cfile); 377382Sjkh fclose(cfile); 3787998Sjkh move_file(".", DESC_FNAME, LogDir); 3797998Sjkh move_file(".", COMMENT_FNAME, LogDir); 3804996Sjkh if (fexists(DISPLAY_FNAME)) 3817998Sjkh move_file(".", DISPLAY_FNAME, LogDir); 3824996Sjkh for (p = Plist.head; p ; p = p->next) { 3834996Sjkh if (p->type != PLIST_PKGDEP) 3844996Sjkh continue; 3854996Sjkh if (Verbose) 38611780Sjkh printf("Attempting to record dependency on package `%s'\n", p->name); 38711780Sjkh sprintf(contents, "%s/%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, 3887937Sjkh basename_of(p->name), REQUIRED_BY_FNAME); 3894996Sjkh cfile = fopen(contents, "a"); 3909202Srgrimes if (!cfile) 39111780Sjkh whinge("Warning: Can't open dependency file '%s'!\n" 39211780Sjkh "\tDependency registration is incomplete.", contents); 3939202Srgrimes else { 39411780Sjkh fprintf(cfile, "%s\n", PkgName); 3959202Srgrimes if (fclose(cfile) == EOF) 3969202Srgrimes warn("Cannot properly close file %s", contents); 3974996Sjkh } 3984996Sjkh } 399327Sjkh if (Verbose) 400327Sjkh printf("Package %s registered in %s\n", PkgName, LogDir); 401327Sjkh } 4028857Srgrimes 4034996Sjkh if (p = find_plist(&Plist, PLIST_DISPLAY)) { 4044996Sjkh FILE *fp; 4054996Sjkh char buf[BUFSIZ]; 4064996Sjkh fp = fopen(p->name, "r"); 4074996Sjkh if (fp) { 4084996Sjkh putc('\n', stdout); 4094996Sjkh while (fgets(buf, sizeof(buf), fp)) 4104996Sjkh fputs(buf, stdout); 4114996Sjkh putc('\n', stdout); 4124996Sjkh (void) fclose(fp); 4134996Sjkh } else 4144996Sjkh warn("Cannot open display file `%s'.", p->name); 4154996Sjkh } 4164996Sjkh 417327Sjkh goto success; 418327Sjkh 4197998Sjkh bomb: 4207998Sjkh code = 1; 4217998Sjkh goto success; 4227998Sjkh 423327Sjkh fail: 4244996Sjkh /* Nuke the whole (installed) show, XXX but don't clean directories */ 425327Sjkh if (!Fake) 4264996Sjkh delete_package(FALSE, FALSE, &Plist); 427327Sjkh 428327Sjkh success: 429327Sjkh /* delete the packing list contents */ 43011780Sjkh free_plist(&Plist); 43111780Sjkh leave_playpen(Home); 432327Sjkh return code; 433327Sjkh} 434327Sjkh 435327Sjkhstatic int 436327Sjkhsanity_check(char *pkg) 437327Sjkh{ 4388075Sjkh PackingList p; 4398075Sjkh int code = 0; 4408075Sjkh 441327Sjkh if (!fexists(CONTENTS_FNAME)) { 442327Sjkh whinge("Package %s has no CONTENTS file!", pkg); 4438075Sjkh code = 1; 444327Sjkh } 4458075Sjkh else if (!fexists(COMMENT_FNAME)) { 446327Sjkh whinge("Package %s has no COMMENT file!", pkg); 4478075Sjkh code = 1; 448327Sjkh } 4498075Sjkh else if (!fexists(DESC_FNAME)) { 450327Sjkh whinge("Package %s has no DESC file!", pkg); 4518075Sjkh code = 1; 452327Sjkh } 4538075Sjkh return code; 454327Sjkh} 455327Sjkh 456327Sjkhvoid 457327Sjkhcleanup(int signo) 458327Sjkh{ 459382Sjkh if (signo) 460382Sjkh printf("Signal %d received, cleaning up..\n", signo); 461327Sjkh if (Plist.head) { 462327Sjkh if (!Fake) 4634996Sjkh delete_package(FALSE, FALSE, &Plist); 464327Sjkh free_plist(&Plist); 465327Sjkh } 466327Sjkh if (!Fake && LogDir[0]) 467327Sjkh vsystem("%s -rf %s", REMOVE_CMD, LogDir); 46811780Sjkh leave_playpen(Home); 469327Sjkh} 470