perform.c revision 7713
1213688Smm#ifndef lint 2213688Smmstatic const char *rcsid = "$Id: perform.c,v 1.13 1994/12/06 00:51:34 jkh Exp $"; 3213688Smm#endif 4213688Smm 5245128Smm/* 6223935Smm * FreeBSD install - a package for the installation and maintainance 7213688Smm * of non-core utilities. 8213688Smm * 9213688Smm * Redistribution and use in source and binary forms, with or without 10245128Smm * modification, are permitted provided that the following conditions 11245128Smm * are met: 12245128Smm * 1. Redistributions of source code must retain the above copyright 13213688Smm * notice, this list of conditions and the following disclaimer. 14213688Smm * 2. Redistributions in binary form must reproduce the above copyright 15223935Smm * notice, this list of conditions and the following disclaimer in the 16213688Smm * documentation and/or other materials provided with the distribution. 17213688Smm * 18245128Smm * Jordan K. Hubbard 19213688Smm * 18 July 1993 20213688Smm * 21245128Smm * This is the main body of the add module. 22213688Smm * 23245128Smm */ 24213688Smm 25213688Smm#include "lib.h" 26223935Smm#include "add.h" 27213688Smm 28245128Smm#include <signal.h> 29213688Smm#include <sys/wait.h> 30213688Smm 31213688Smmstatic int pkg_do(char *); 32213688Smmstatic int sanity_check(char *); 33245128Smmstatic char LogDir[FILENAME_MAX]; 34213688Smm 35213688Smm 36213688Smmint 37245128Smmpkg_perform(char **pkgs) 38213688Smm{ 39213688Smm int i, err_cnt = 0; 40213688Smm 41213688Smm signal(SIGINT, cleanup); 42245128Smm signal(SIGHUP, cleanup); 43213688Smm 44213688Smm if (AddMode == SLAVE) 45213688Smm err_cnt = pkg_do(NULL); 46245128Smm else { 47213688Smm for (i = 0; pkgs[i]; i++) 48219001Smm err_cnt += pkg_do(pkgs[i]); 49219001Smm } 50219001Smm return err_cnt; 51213688Smm} 52245128Smm 53213688Smmstatic Package Plist; 54213688Smm 55213688Smm/* This is seriously ugly code following. Written very fast! */ 56245128Smmstatic int 57213688Smmpkg_do(char *pkg) 58219001Smm{ 59219001Smm char pkg_fullname[FILENAME_MAX]; 60213688Smm char home[FILENAME_MAX]; 61245128Smm FILE *cfile; 62213688Smm int code = 0; 63213688Smm PackingList p; 64213688Smm struct stat sb; 65245128Smm 66213688Smm /* Reset some state */ 67213688Smm if (Plist.head) 68213688Smm free_plist(&Plist); 69245128Smm LogDir[0] = '\0'; 70213688Smm if (AddMode == SLAVE) { 71213688Smm char tmp_dir[FILENAME_MAX]; 72213688Smm 73245128Smm fgets(tmp_dir, FILENAME_MAX, stdin); 74213688Smm tmp_dir[strlen(tmp_dir) - 1] = '\0'; /* pesky newline! */ 75213688Smm if (chdir(tmp_dir) == FAIL) { 76213688Smm whinge("pkg_add in SLAVE mode can't chdir to %s.", tmp_dir); 77213688Smm return 1; 78245128Smm } 79213688Smm read_plist(&Plist, stdin); 80219001Smm } 81219001Smm else { 82219001Smm if (!getcwd(home, FILENAME_MAX)) 83219001Smm upchuck("getcwd"); 84219001Smm 85219001Smm if (pkg[0] == '/') /* full pathname? */ 86213688Smm strcpy(pkg_fullname, pkg); 87213688Smm else 88213688Smm sprintf(pkg_fullname, "%s/%s", home, pkg); 89213688Smm if (!fexists(pkg_fullname)) { 90213688Smm whinge("Can't find package '%s'.", pkg_fullname); 91213688Smm return 1; 92213688Smm } 93213688Smm /* 94213688Smm * Apply a crude heuristic to see how much space the package will 95213688Smm * take up once it's unpacked. I've noticed that most packages 96213688Smm * compress an average of 75%, so multiply by 4 for good measure. 97245128Smm */ 98213688Smm if (stat(pkg_fullname, &sb) == FAIL) { 99213688Smm whinge("Can't stat package file '%s'.", pkg_fullname); 100213688Smm return 1; 101213688Smm } 102245128Smm sb.st_size *= 4; 103213688Smm Home = make_playpen(PlayPen, sb.st_size); 104213688Smm if (unpack(pkg_fullname, NULL)) 105213688Smm return 1; 106213688Smm 107245128Smm if (sanity_check(pkg_fullname)) 108213688Smm return 1; 109213688Smm 110213688Smm cfile = fopen(CONTENTS_FNAME, "r"); 111213688Smm if (!cfile) { 112245128Smm whinge("Unable to open %s file.", CONTENTS_FNAME); 113213688Smm goto fail; 114213688Smm } 115213688Smm read_plist(&Plist, cfile); 116213688Smm fclose(cfile); 117245128Smm if (Prefix) { 118213688Smm /* 119213688Smm * If we have a prefix, delete the first one we see and add this 120213688Smm * one in place of it. 121213688Smm */ 122245128Smm delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 123213688Smm add_plist_top(&Plist, PLIST_CWD, Prefix); 124213688Smm } 125213688Smm /* If we're running in MASTER mode, just output the plist and return */ 126213688Smm if (AddMode == MASTER) { 127245128Smm printf("%s\n", where_playpen()); 128213688Smm write_plist(&Plist, stdout); 129213688Smm return 0; 130213688Smm } 131213688Smm } 132245128Smm setenv(PKG_PREFIX_VNAME, 133213688Smm (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1); 134213688Smm PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous"; 135213688Smm /* Protect against old packages with bogus @name fields */ 136213688Smm sprintf(LogDir, "%s/%s", LOG_DIR, basename_of(PkgName)); 137245128Smm if (isdir(LogDir)) { 138213688Smm whinge("Package `%s' already recorded as installed.\n", PkgName); 139213688Smm code = 1; 140213688Smm goto success; /* close enough for government work */ 141213688Smm } 142245128Smm for (p = Plist.head; p ; p = p->next) { 143213688Smm if (p->type != PLIST_PKGDEP) 144213688Smm continue; 145213688Smm if (!Fake && vsystem("pkg_info -e %s", p->name)) { 146213688Smm char tmp[120]; 147245128Smm 148213688Smm sprintf(tmp, "%s/%s.tgz", Home, p->name); 149213688Smm if (fexists(tmp)) { 150213688Smm if (Verbose) 151213688Smm printf("Package `%s' depends on `%s': Trying to load it.\n", PkgName, p->name); 152245128Smm if (vsystem("pkg_add %s", tmp)) { 153213688Smm whinge("Autoload of dependency package `%s' failed!%s", 154213688Smm p->name, Force ? " (proceeding anyway)" : ""); 155213688Smm if (!Force) 156213688Smm code++; 157245128Smm } 158213688Smm else if (Verbose) 159213688Smm printf("Dependency `%s' loaded successfully.\n", p->name); 160213688Smm } 161213688Smm else { 162245128Smm whinge("Package `%s' depends on missing package `%s'%s.", PkgName, 163213688Smm p->name, Force ? " (proceeding anyway)" : ""); 164213688Smm if (!Force) 165219001Smm code++; 166219001Smm } 167213688Smm } 168245128Smm } 169213688Smm if (code != 0) 170213688Smm goto success; /* close enough for government work */ 171213688Smm 172213688Smm if (fexists(REQUIRE_FNAME)) { 173245128Smm vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 174213688Smm if (Verbose) 175213688Smm printf("Running requirements file first for %s..\n", PkgName); 176213688Smm if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 177213688Smm whinge("Package %s fails requirements %s", 178245128Smm pkg_fullname, 179213688Smm Force ? "installing anyway" : "- not installed."); 180213688Smm if (!Force) { 181213688Smm code = 1; 182213688Smm goto success; /* close enough for government work */ 183245128Smm } 184213688Smm } 185213688Smm } 186213688Smm if (!NoInstall && fexists(INSTALL_FNAME)) { 187213688Smm vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 188245128Smm if (Verbose) 189213688Smm printf("Running install with PRE-INSTALL for %s..\n", PkgName); 190213688Smm if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 191213688Smm whinge("Install script returned error status."); 192213688Smm code = 1; 193213688Smm goto success; /* nothing to uninstall yet */ 194245128Smm } 195213688Smm } 196213688Smm extract_plist(home, &Plist); 197213688Smm if (!NoInstall && fexists(MTREE_FNAME)) { 198245128Smm if (Verbose) 199213688Smm printf("Running mtree for %s..\n", PkgName); 200213688Smm p = find_plist(&Plist, PLIST_CWD); 201213688Smm if (Verbose) 202245128Smm printf("mtree -u -f %s -d -e -p %s\n", MTREE_FNAME, 203213688Smm p ? p->name : "/"); 204213688Smm if (!Fake) { 205213688Smm pid_t chpid; 206213688Smm int rval, status; 207213688Smm chpid = fork(); 208213688Smm if (chpid == 0) { 209245128Smm execl("/usr/sbin/mtree", "mtree", "-u", "-f", MTREE_FNAME, 210213688Smm "-d", "-e", "-p", p ? p->name : "/"); 211213688Smm perror("cannot execute mtree"); 212213688Smm exit(3); 213213688Smm } 214213688Smm if (chpid == (pid_t) -1) { 215213688Smm warn("Cannot fork mtree"); 216213688Smm code = 1; 217223935Smm goto fail; 218245128Smm } 219213688Smm if (waitpid(chpid, &status, 0) == -1) { 220213688Smm warn("Cannot wait for mtree"); 221213688Smm code = 1; 222245128Smm goto fail; 223213688Smm } 224213688Smm if (!WIFEXITED(status)) { 225213688Smm whinge("Strange exit from mtree: %x", status); 226245128Smm code = 1; 227213688Smm goto fail; 228213688Smm } 229213688Smm#ifdef DEBUG 230245128Smm whinge("mtree exits %d\n", WEXITSTATUS(status)); 231213688Smm#endif 232213688Smm switch (WEXITSTATUS(status)) { 233213688Smm case 0: 234245128Smm break; /* normal */ 235213688Smm case 2: 236213688Smm whinge("\nWARNING: Mtree attempted some work which may not have completed.\n\tExamine the above output.\n\t(most likely it changed directory permissions)\n"); 237213688Smm break; 238245128Smm default: 239213688Smm whinge("Error status %d from mtree.", WEXITSTATUS(status)); 240213688Smm code = 1; 241213688Smm goto fail; 242245128Smm } 243213688Smm } 244213688Smm } 245213688Smm if (!NoInstall && fexists(INSTALL_FNAME)) { 246245128Smm if (Verbose) 247213688Smm printf("Running install with POST-INSTALL for %s..\n", PkgName); 248213688Smm if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 249213688Smm whinge("Install script returned error status."); 250245128Smm code = 1; 251213688Smm goto fail; 252213688Smm } 253213688Smm } 254245128Smm if (!NoRecord && !Fake) { 255213688Smm char contents[FILENAME_MAX]; 256213688Smm FILE *cfile; 257213688Smm 258245128Smm umask(022); 259213688Smm if (getuid() != 0) 260213688Smm whinge("Not running as root - trying to record install anyway."); 261213688Smm if (!PkgName) { 262245128Smm whinge("No package name! Can't record package, sorry."); 263213688Smm code = 1; 264213688Smm goto success; /* well, partial anyway */ 265213688Smm } 266245128Smm /* Protect against old packages with bogus @name fields */ 267213688Smm sprintf(LogDir, "%s/%s", LOG_DIR, basename_of(PkgName)); 268213688Smm if (Verbose) 269213688Smm printf("Attempting to record package into %s..\n", LogDir); 270213688Smm if (make_hierarchy(LogDir)) { 271245128Smm whinge("Can't record package into '%s', you're on your own!", 272213688Smm LogDir); 273213688Smm bzero(LogDir, FILENAME_MAX); 274213688Smm code = 1; 275213688Smm goto success; /* close enough for government work */ 276213688Smm } 277213688Smm /* Make sure pkg_info can read the entry */ 278213688Smm vsystem("chmod a+rx %s", LogDir); 279213688Smm if (fexists(DEINSTALL_FNAME)) 280245128Smm copy_file(".", DEINSTALL_FNAME, LogDir); 281213688Smm if (fexists(REQUIRE_FNAME)) 282213688Smm copy_file(".", REQUIRE_FNAME, LogDir); 283213688Smm sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 284245128Smm cfile = fopen(contents, "w"); 285213688Smm if (!cfile) { 286213688Smm whinge("Can't open new contents file '%s'! Can't register pkg.", 287213688Smm contents); 288213688Smm goto success; /* can't log, but still keep pkg */ 289245128Smm } 290213688Smm write_plist(&Plist, cfile); 291213688Smm fclose(cfile); 292213688Smm copy_file(".", DESC_FNAME, LogDir); 293213688Smm copy_file(".", COMMENT_FNAME, LogDir); 294245128Smm if (fexists(DISPLAY_FNAME)) 295213688Smm copy_file(".", DISPLAY_FNAME, LogDir); 296213688Smm for (p = Plist.head; p ; p = p->next) { 297213688Smm if (p->type != PLIST_PKGDEP) 298213688Smm continue; 299245128Smm if (Verbose) 300213688Smm printf("Attempting to record dependency on package `%s'\n", 301213688Smm p->name); 302213688Smm sprintf(contents, "%s/%s/%s", LOG_DIR, basename_of(p->name), 303213688Smm REQUIRED_BY_FNAME); 304245128Smm cfile = fopen(contents, "a"); 305213688Smm if (!cfile) { 306213688Smm whinge("Can't open dependency file '%s'!\n\tDependency registration incomplete.", 307213688Smm contents); 308213688Smm continue; 309245128Smm } 310213688Smm fprintf(cfile, "%s\n", basename_of(PkgName)); 311213688Smm if (fclose(cfile) == EOF) 312213688Smm warn("Cannot properly close file %s", contents); 313213688Smm } 314245128Smm if (Verbose) 315213688Smm printf("Package %s registered in %s\n", PkgName, LogDir); 316213688Smm } 317213688Smm 318213688Smm if (p = find_plist(&Plist, PLIST_DISPLAY)) { 319213688Smm FILE *fp; 320213688Smm char buf[BUFSIZ]; 321213688Smm fp = fopen(p->name, "r"); 322245128Smm if (fp) { 323213688Smm putc('\n', stdout); 324213688Smm while (fgets(buf, sizeof(buf), fp)) 325219001Smm fputs(buf, stdout); 326219001Smm putc('\n', stdout); 327213688Smm (void) fclose(fp); 328213688Smm } else 329219001Smm warn("Cannot open display file `%s'.", p->name); 330219001Smm } 331213688Smm 332213688Smm goto success; 333213688Smm 334245128Smm fail: 335213688Smm /* Nuke the whole (installed) show, XXX but don't clean directories */ 336213688Smm if (!Fake) 337213688Smm delete_package(FALSE, FALSE, &Plist); 338219001Smm 339219001Smm success: 340213688Smm /* delete the packing list contents */ 341213688Smm leave_playpen(); 342219001Smm 343219001Smm return code; 344213688Smm} 345213688Smm 346213688Smmstatic int 347213688Smmsanity_check(char *pkg) 348213688Smm{ 349213688Smm if (!fexists(CONTENTS_FNAME)) { 350213688Smm whinge("Package %s has no CONTENTS file!", pkg); 351213688Smm return 1; 352245128Smm } 353213688Smm if (!fexists(COMMENT_FNAME)) { 354213688Smm whinge("Package %s has no COMMENT file!", pkg); 355213688Smm return 1; 356213688Smm } 357245128Smm if (!fexists(DESC_FNAME)) { 358213688Smm whinge("Package %s has no DESC file!", pkg); 359213688Smm return 1; 360213688Smm } 361213688Smm return 0; 362245128Smm} 363213688Smm 364213688Smmvoid 365213688Smmcleanup(int signo) 366213688Smm{ 367245128Smm if (signo) 368213688Smm printf("Signal %d received, cleaning up..\n", signo); 369213688Smm if (Plist.head) { 370213688Smm if (!Fake) 371245128Smm delete_package(FALSE, FALSE, &Plist); 372213688Smm free_plist(&Plist); 373213688Smm } 374213688Smm if (!Fake && LogDir[0]) 375213688Smm vsystem("%s -rf %s", REMOVE_CMD, LogDir); 376213688Smm leave_playpen(); 377245128Smm} 378213688Smm