perform.c revision 38931
1#ifndef lint 2static const char rcsid[] = 3 "$Id: perform.c,v 1.51 1998/08/27 14:59:55 jkh Exp $"; 4#endif 5 6/* 7 * FreeBSD install - a package for the installation and maintainance 8 * of non-core utilities. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Jordan K. Hubbard 20 * 18 July 1993 21 * 22 * This is the main body of the add module. 23 * 24 */ 25 26#include <err.h> 27#include "lib.h" 28#include "add.h" 29 30#include <signal.h> 31#include <sys/wait.h> 32 33static int pkg_do(char *); 34static int sanity_check(char *); 35static char LogDir[FILENAME_MAX]; 36static int zapLogDir; /* Should we delete LogDir? */ 37 38int 39pkg_perform(char **pkgs) 40{ 41 int i, err_cnt = 0; 42 43 signal(SIGINT, cleanup); 44 signal(SIGHUP, cleanup); 45 46 if (AddMode == SLAVE) 47 err_cnt = pkg_do(NULL); 48 else { 49 for (i = 0; pkgs[i]; i++) 50 err_cnt += pkg_do(pkgs[i]); 51 } 52 return err_cnt; 53} 54 55static Package Plist; 56static char *Home; 57 58/* 59 * This is seriously ugly code following. Written very fast! 60 * [And subsequently made even worse.. Sigh! This code was just born 61 * to be hacked, I guess.. :) -jkh] 62 */ 63static int 64pkg_do(char *pkg) 65{ 66 char pkg_fullname[FILENAME_MAX]; 67 char playpen[FILENAME_MAX]; 68 char extract_contents[FILENAME_MAX]; 69 char *where_to, *tmp, *extract; 70 FILE *cfile; 71 int code; 72 PackingList p; 73 struct stat sb; 74 int inPlace; 75 76 code = 0; 77 zapLogDir = 0; 78 LogDir[0] = '\0'; 79 strcpy(playpen, FirstPen); 80 inPlace = 0; 81 82 /* Are we coming in for a second pass, everything already extracted? */ 83 if (!pkg) { 84 fgets(playpen, FILENAME_MAX, stdin); 85 playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */ 86 if (chdir(playpen) == FAIL) { 87 warnx("pkg_add in SLAVE mode can't chdir to %s", playpen); 88 return 1; 89 } 90 read_plist(&Plist, stdin); 91 where_to = playpen; 92 } 93 /* Nope - do it now */ 94 else { 95 /* Is it an ftp://foo.bar.baz/file.tgz specification? */ 96 if (isURL(pkg)) { 97 if (!(Home = fileGetURL(NULL, pkg))) { 98 warnx("unable to fetch `%s' by URL", pkg); 99 return 1; 100 } 101 where_to = Home; 102 strcpy(pkg_fullname, pkg); 103 cfile = fopen(CONTENTS_FNAME, "r"); 104 if (!cfile) { 105 warnx( 106 "unable to open table of contents file `%s' - not a package?", 107 CONTENTS_FNAME); 108 goto bomb; 109 } 110 read_plist(&Plist, cfile); 111 fclose(cfile); 112 } 113 else { 114 strcpy(pkg_fullname, pkg); /* copy for sanity's sake, could remove pkg_fullname */ 115 if (strcmp(pkg, "-")) { 116 if (stat(pkg_fullname, &sb) == FAIL) { 117 warnx("can't stat package file '%s'", pkg_fullname); 118 goto bomb; 119 } 120 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME); 121 extract = extract_contents; 122 } 123 else { 124 extract = NULL; 125 sb.st_size = 100000; /* Make up a plausible average size */ 126 } 127 Home = make_playpen(playpen, sb.st_size * 4); 128 if (!Home) 129 errx(1, "unable to make playpen for %d bytes", sb.st_size * 4); 130 where_to = Home; 131 /* Since we can call ourselves recursively, keep notes on where we came from */ 132 if (!getenv("_TOP")) 133 setenv("_TOP", Home, 1); 134 if (unpack(pkg_fullname, extract)) { 135 warnx( 136 "unable to extract table of contents file from `%s' - not a package?", 137 pkg_fullname); 138 goto bomb; 139 } 140 cfile = fopen(CONTENTS_FNAME, "r"); 141 if (!cfile) { 142 warnx( 143 "unable to open table of contents file `%s' - not a package?", 144 CONTENTS_FNAME); 145 goto bomb; 146 } 147 read_plist(&Plist, cfile); 148 fclose(cfile); 149 150 /* Extract directly rather than moving? Oh goodie! */ 151 if (find_plist_option(&Plist, "extract-in-place")) { 152 if (Verbose) 153 printf("Doing in-place extraction for %s\n", pkg_fullname); 154 p = find_plist(&Plist, PLIST_CWD); 155 if (p) { 156 if (!isdir(p->name) && !Fake) { 157 if (Verbose) 158 printf("Desired prefix of %s does not exist, creating..\n", p->name); 159 vsystem("mkdir -p %s", p->name); 160 if (chdir(p->name) == -1) { 161 warn("unable to change directory to `%s'", p->name); 162 goto bomb; 163 } 164 } 165 where_to = p->name; 166 inPlace = 1; 167 } 168 else { 169 warnx( 170 "no prefix specified in `%s' - this is a bad package!", 171 pkg_fullname); 172 goto bomb; 173 } 174 } 175 176 /* 177 * Apply a crude heuristic to see how much space the package will 178 * take up once it's unpacked. I've noticed that most packages 179 * compress an average of 75%, so multiply by 4 for good measure. 180 */ 181 182 if (!inPlace && min_free(playpen) < sb.st_size * 4) { 183 warnx("projected size of %d exceeds available free space.\n" 184"Please set your PKG_TMPDIR variable to point to a location with more\n" 185 "free space and try again", sb.st_size * 4); 186 warnx("not extracting %s\ninto %s, sorry!", 187 pkg_fullname, where_to); 188 goto bomb; 189 } 190 191 /* If this is a direct extract and we didn't want it, stop now */ 192 if (inPlace && Fake) 193 goto success; 194 195 /* Finally unpack the whole mess */ 196 if (unpack(pkg_fullname, NULL)) { 197 warnx("unable to extract `%s'!", pkg_fullname); 198 goto bomb; 199 } 200 } 201 202 /* Check for sanity and dependencies */ 203 if (sanity_check(pkg)) 204 goto bomb; 205 206 /* If we're running in MASTER mode, just output the plist and return */ 207 if (AddMode == MASTER) { 208 printf("%s\n", where_playpen()); 209 write_plist(&Plist, stdout); 210 return 0; 211 } 212 } 213 214 /* 215 * If we have a prefix, delete the first one we see and add this 216 * one in place of it. 217 */ 218 if (Prefix) { 219 delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 220 add_plist_top(&Plist, PLIST_CWD, Prefix); 221 } 222 223 setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1); 224 /* Protect against old packages with bogus @name fields */ 225 PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous"; 226 227 /* See if we're already registered */ 228 sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 229 if (isdir(LogDir) && !Force) { 230 warnx("package `%s' already recorded as installed", PkgName); 231 code = 1; 232 goto success; /* close enough for government work */ 233 } 234 235 /* Now check the packing list for dependencies */ 236 for (p = Plist.head; p ; p = p->next) { 237 if (p->type != PLIST_PKGDEP) 238 continue; 239 if (Verbose) 240 printf("Package `%s' depends on `%s'.\n", PkgName, p->name); 241 if (vsystem("pkg_info -e %s", p->name)) { 242 char path[FILENAME_MAX], *cp = NULL; 243 244 if (!Fake) { 245 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) { 246 snprintf(path, FILENAME_MAX, "%s/%s.tgz", getenv("_TOP"), p->name); 247 if (fexists(path)) 248 cp = path; 249 else 250 cp = fileFindByPath(pkg, p->name); 251 if (cp) { 252 if (Verbose) 253 printf("Loading it from %s.\n", cp); 254 if (vsystem("pkg_add %s%s", Verbose ? "-v " : "", cp)) { 255 warnx("autoload of dependency `%s' failed%s", 256 cp, Force ? " (proceeding anyway)" : "!"); 257 if (!Force) 258 ++code; 259 } 260 } 261 else { 262 warnx("could not find package %s %s", 263 p->name, Force ? " (proceeding anyway)" : "!"); 264 if (!Force) 265 ++code; 266 } 267 } 268 else if ((cp = fileGetURL(pkg, p->name)) != NULL) { 269 if (Verbose) 270 printf("Finished loading %s over FTP.\n", p->name); 271 if (!fexists("+CONTENTS")) { 272 warnx("autoloaded package %s has no +CONTENTS file?", 273 p->name); 274 if (!Force) 275 ++code; 276 } 277 else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) { 278 warnx("pkg_add of dependency `%s' failed%s", 279 p->name, Force ? " (proceeding anyway)" : "!"); 280 if (!Force) 281 ++code; 282 } 283 else if (Verbose) 284 printf("\t`%s' loaded successfully.\n", p->name); 285 /* Nuke the temporary playpen */ 286 leave_playpen(); 287 } 288 } 289 else { 290 if (Verbose) 291 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : ""); 292 else 293 printf("Package dependency %s for %s not found%s\n", p->name, pkg, 294 Force ? " (proceeding anyway)" : "!"); 295 if (!Force) 296 ++code; 297 } 298 } 299 else if (Verbose) 300 printf(" - already installed.\n"); 301 } 302 303 if (code != 0) 304 goto bomb; 305 306 /* Look for the requirements file */ 307 if (fexists(REQUIRE_FNAME)) { 308 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 309 if (Verbose) 310 printf("Running requirements file first for %s..\n", PkgName); 311 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 312 warnx("package %s fails requirements %s", pkg_fullname, 313 Force ? "installing anyway" : "- not installed"); 314 if (!Force) { 315 code = 1; 316 goto success; /* close enough for government work */ 317 } 318 } 319 } 320 321 /* If we're really installing, and have an installation file, run it */ 322 if (!NoInstall && fexists(INSTALL_FNAME)) { 323 vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 324 if (Verbose) 325 printf("Running install with PRE-INSTALL for %s..\n", PkgName); 326 if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 327 warnx("install script returned error status"); 328 unlink(INSTALL_FNAME); 329 code = 1; 330 goto success; /* nothing to uninstall yet */ 331 } 332 } 333 334 /* Now finally extract the entire show if we're not going direct */ 335 if (!inPlace && !Fake) 336 extract_plist(".", &Plist); 337 338 if (!Fake && fexists(MTREE_FNAME)) { 339 if (Verbose) 340 printf("Running mtree for %s..\n", PkgName); 341 p = find_plist(&Plist, PLIST_CWD); 342 if (Verbose) 343 printf("mtree -U -f %s -d -e -p %s\n", MTREE_FNAME, p ? p->name : "/"); 344 if (!Fake) { 345 if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s", MTREE_FNAME, p ? p->name : "/")) 346 warnx("mtree returned a non-zero status - continuing"); 347 } 348 unlink(MTREE_FNAME); 349 } 350 351 /* Run the installation script one last time? */ 352 if (!NoInstall && fexists(INSTALL_FNAME)) { 353 if (Verbose) 354 printf("Running install with POST-INSTALL for %s..\n", PkgName); 355 if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 356 warnx("install script returned error status"); 357 unlink(INSTALL_FNAME); 358 code = 1; 359 goto fail; 360 } 361 unlink(INSTALL_FNAME); 362 } 363 364 /* Time to record the deed? */ 365 if (!NoRecord && !Fake) { 366 char contents[FILENAME_MAX]; 367 FILE *cfile; 368 369 umask(022); 370 if (getuid() != 0) 371 warnx("not running as root - trying to record install anyway"); 372 if (!PkgName) { 373 warnx("no package name! can't record package, sorry"); 374 code = 1; 375 goto success; /* well, partial anyway */ 376 } 377 sprintf(LogDir, "%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, PkgName); 378 zapLogDir = 1; 379 if (Verbose) 380 printf("Attempting to record package into %s..\n", LogDir); 381 if (make_hierarchy(LogDir)) { 382 warnx("can't record package into '%s', you're on your own!", 383 LogDir); 384 bzero(LogDir, FILENAME_MAX); 385 code = 1; 386 goto success; /* close enough for government work */ 387 } 388 /* Make sure pkg_info can read the entry */ 389 vsystem("chmod a+rx %s", LogDir); 390 if (fexists(DEINSTALL_FNAME)) 391 move_file(".", DEINSTALL_FNAME, LogDir); 392 if (fexists(REQUIRE_FNAME)) 393 move_file(".", REQUIRE_FNAME, LogDir); 394 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 395 cfile = fopen(contents, "w"); 396 if (!cfile) { 397 warnx("can't open new contents file '%s'! can't register pkg", 398 contents); 399 goto success; /* can't log, but still keep pkg */ 400 } 401 write_plist(&Plist, cfile); 402 fclose(cfile); 403 move_file(".", DESC_FNAME, LogDir); 404 move_file(".", COMMENT_FNAME, LogDir); 405 if (fexists(DISPLAY_FNAME)) 406 move_file(".", DISPLAY_FNAME, LogDir); 407 for (p = Plist.head; p ; p = p->next) { 408 if (p->type != PLIST_PKGDEP) 409 continue; 410 if (Verbose) 411 printf("Attempting to record dependency on package `%s'\n", p->name); 412 sprintf(contents, "%s/%s/%s", (tmp = getenv(PKG_DBDIR)) ? tmp : DEF_LOG_DIR, 413 basename_of(p->name), REQUIRED_BY_FNAME); 414 cfile = fopen(contents, "a"); 415 if (!cfile) 416 warnx("can't open dependency file '%s'!\n" 417 "dependency registration is incomplete", contents); 418 else { 419 fprintf(cfile, "%s\n", PkgName); 420 if (fclose(cfile) == EOF) 421 warnx("cannot properly close file %s", contents); 422 } 423 } 424 if (Verbose) 425 printf("Package %s registered in %s\n", PkgName, LogDir); 426 } 427 428 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) { 429 FILE *fp; 430 char buf[BUFSIZ]; 431 432 snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name); 433 fp = fopen(buf, "r"); 434 if (fp) { 435 putc('\n', stdout); 436 while (fgets(buf, sizeof(buf), fp)) 437 fputs(buf, stdout); 438 putc('\n', stdout); 439 (void) fclose(fp); 440 } else 441 warnx("cannot open %s as display file", buf); 442 } 443 444 goto success; 445 446 bomb: 447 code = 1; 448 goto success; 449 450 fail: 451 /* Nuke the whole (installed) show, XXX but don't clean directories */ 452 if (!Fake) 453 delete_package(FALSE, FALSE, &Plist); 454 455 success: 456 /* delete the packing list contents */ 457 free_plist(&Plist); 458 leave_playpen(); 459 return code; 460} 461 462static int 463sanity_check(char *pkg) 464{ 465 int code = 0; 466 467 if (!fexists(CONTENTS_FNAME)) { 468 warnx("package %s has no CONTENTS file!", pkg); 469 code = 1; 470 } 471 else if (!fexists(COMMENT_FNAME)) { 472 warnx("package %s has no COMMENT file!", pkg); 473 code = 1; 474 } 475 else if (!fexists(DESC_FNAME)) { 476 warnx("package %s has no DESC file!", pkg); 477 code = 1; 478 } 479 return code; 480} 481 482void 483cleanup(int signo) 484{ 485 static int in_cleanup = 0; 486 487 if (!in_cleanup) { 488 in_cleanup = 1; 489 if (signo) 490 printf("Signal %d received, cleaning up..\n", signo); 491 if (!Fake && zapLogDir && LogDir[0]) 492 vsystem("%s -rf %s", REMOVE_CMD, LogDir); 493 leave_playpen(); 494 } 495 exit(1); 496} 497