perform.c revision 113594
1/* 2 * FreeBSD install - a package for the installation and maintainance 3 * of non-core utilities. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * Jordan K. Hubbard 15 * 18 July 1993 16 * 17 * This is the main body of the add module. 18 * 19 */ 20 21#include <sys/cdefs.h> 22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/perform.c 113594 2003-04-17 09:56:05Z kris $"); 23 24#include <err.h> 25#include <paths.h> 26#include "lib.h" 27#include "add.h" 28 29#include <libgen.h> 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, *extract; 70 FILE *cfile; 71 int code; 72 PackingList p; 73 struct stat sb; 74 int inPlace, conflictsfound, i, errcode; 75 /* support for separate pre/post install scripts */ 76 int new_m = 0; 77 char pre_script[FILENAME_MAX] = INSTALL_FNAME; 78 char post_script[FILENAME_MAX]; 79 char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX]; 80 char *conflict[2]; 81 char **matched; 82 83 conflictsfound = 0; 84 code = 0; 85 zapLogDir = 0; 86 LogDir[0] = '\0'; 87 strcpy(playpen, FirstPen); 88 inPlace = 0; 89 90 /* Are we coming in for a second pass, everything already extracted? */ 91 if (!pkg) { 92 fgets(playpen, FILENAME_MAX, stdin); 93 playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */ 94 if (chdir(playpen) == FAIL) { 95 warnx("pkg_add in SLAVE mode can't chdir to %s", playpen); 96 return 1; 97 } 98 read_plist(&Plist, stdin); 99 where_to = playpen; 100 } 101 /* Nope - do it now */ 102 else { 103 /* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */ 104 if (isURL(pkg)) { 105 if (!(Home = fileGetURL(NULL, pkg))) { 106 warnx("unable to fetch '%s' by URL", pkg); 107 return 1; 108 } 109 where_to = Home; 110 strcpy(pkg_fullname, pkg); 111 cfile = fopen(CONTENTS_FNAME, "r"); 112 if (!cfile) { 113 warnx( 114 "unable to open table of contents file '%s' - not a package?", 115 CONTENTS_FNAME); 116 goto bomb; 117 } 118 read_plist(&Plist, cfile); 119 fclose(cfile); 120 } 121 else { 122 strcpy(pkg_fullname, pkg); /* 123 * Copy for sanity's sake, 124 * could remove pkg_fullname 125 */ 126 if (strcmp(pkg, "-")) { 127 if (stat(pkg_fullname, &sb) == FAIL) { 128 warnx("can't stat package file '%s'", pkg_fullname); 129 goto bomb; 130 } 131 sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME); 132 extract = extract_contents; 133 } 134 else { 135 extract = NULL; 136 sb.st_size = 100000; /* Make up a plausible average size */ 137 } 138 Home = make_playpen(playpen, sb.st_size * 4); 139 if (!Home) 140 errx(1, "unable to make playpen for %qd bytes", (long long)sb.st_size * 4); 141 where_to = Home; 142 /* Since we can call ourselves recursively, keep notes on where we came from */ 143 if (!getenv("_TOP")) 144 setenv("_TOP", Home, 1); 145 if (unpack(pkg_fullname, extract)) { 146 warnx( 147 "unable to extract table of contents file from '%s' - not a package?", 148 pkg_fullname); 149 goto bomb; 150 } 151 cfile = fopen(CONTENTS_FNAME, "r"); 152 if (!cfile) { 153 warnx( 154 "unable to open table of contents file '%s' - not a package?", 155 CONTENTS_FNAME); 156 goto bomb; 157 } 158 read_plist(&Plist, cfile); 159 fclose(cfile); 160 161 /* Extract directly rather than moving? Oh goodie! */ 162 if (find_plist_option(&Plist, "extract-in-place")) { 163 if (Verbose) 164 printf("Doing in-place extraction for %s\n", pkg_fullname); 165 p = find_plist(&Plist, PLIST_CWD); 166 if (p) { 167 if (!isdir(p->name) && !Fake) { 168 if (Verbose) 169 printf("Desired prefix of %s does not exist, creating..\n", p->name); 170 vsystem("mkdir -p %s", p->name); 171 if (chdir(p->name) == -1) { 172 warn("unable to change directory to '%s'", p->name); 173 goto bomb; 174 } 175 } 176 where_to = p->name; 177 inPlace = 1; 178 } 179 else { 180 warnx( 181 "no prefix specified in '%s' - this is a bad package!", 182 pkg_fullname); 183 goto bomb; 184 } 185 } 186 187 /* 188 * Apply a crude heuristic to see how much space the package will 189 * take up once it's unpacked. I've noticed that most packages 190 * compress an average of 75%, so multiply by 4 for good measure. 191 */ 192 193 if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) { 194 warnx("projected size of %qd exceeds available free space.\n" 195"Please set your PKG_TMPDIR variable to point to a location with more\n" 196 "free space and try again", (long long)sb.st_size * 4); 197 warnx("not extracting %s\ninto %s, sorry!", 198 pkg_fullname, where_to); 199 goto bomb; 200 } 201 202 /* If this is a direct extract and we didn't want it, stop now */ 203 if (inPlace && Fake) 204 goto success; 205 206 /* Finally unpack the whole mess. If extract is null we 207 already + did so so don't bother doing it again. */ 208 if (extract && unpack(pkg_fullname, NULL)) { 209 warnx("unable to extract '%s'!", pkg_fullname); 210 goto bomb; 211 } 212 } 213 214 /* Check for sanity and dependencies */ 215 if (sanity_check(pkg)) 216 goto bomb; 217 218 /* If we're running in MASTER mode, just output the plist and return */ 219 if (AddMode == MASTER) { 220 printf("%s\n", where_playpen()); 221 write_plist(&Plist, stdout); 222 return 0; 223 } 224 } 225 226 /* 227 * If we have a prefix, delete the first one we see and add this 228 * one in place of it. 229 */ 230 if (Prefix) { 231 delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 232 add_plist_top(&Plist, PLIST_CWD, Prefix); 233 } 234 235 setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1); 236 /* Protect against old packages with bogus @name and origin fields */ 237 if (Plist.name == NULL) 238 Plist.name = "anonymous"; 239 if (Plist.origin == NULL) 240 Plist.origin = "anonymous/anonymous"; 241 242 /* 243 * See if we're already registered either with the same name (the same 244 * version) or some other version with the same origin. 245 */ 246 if ((isinstalledpkg(Plist.name) || 247 matchbyorigin(Plist.origin, NULL) != NULL) && !Force) { 248 warnx("package '%s' or its older version already installed", 249 Plist.name); 250 code = 1; 251 goto success; /* close enough for government work */ 252 } 253 254 /* Now check the packing list for conflicts */ 255 for (p = Plist.head; p != NULL; p = p->next) { 256 if (p->type == PLIST_CONFLICTS) { 257 conflict[0] = strdup(p->name); 258 conflict[1] = NULL; 259 matched = matchinstalled(MATCH_GLOB, conflict, &errcode); 260 free(conflict[0]); 261 if (errcode == 0 && matched != NULL) 262 for (i = 0; matched[i] != NULL; i++) 263 if (isinstalledpkg(matched[i])) { 264 warnx("package '%s' conflicts with %s", Plist.name, 265 matched[i]); 266 conflictsfound = 1; 267 } 268 269 continue; 270 } 271 } 272 if(conflictsfound) { 273 if(!Force) { 274 warnx("please use pkg_delete first to remove conflicting package(s) or -f to force installation"); 275 code = 1; 276 goto bomb; 277 } else 278 warnx("-f specified; proceeding anyway"); 279 } 280 281 /* Now check the packing list for dependencies */ 282 for (p = Plist.head; p ; p = p->next) { 283 char *deporigin; 284 285 if (p->type != PLIST_PKGDEP) 286 continue; 287 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL; 288 if (Verbose) { 289 printf("Package '%s' depends on '%s'", Plist.name, p->name); 290 if (deporigin != NULL) 291 printf(" with '%s' origin", deporigin); 292 printf(".\n"); 293 } 294 if (!isinstalledpkg(p->name) && 295 !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) { 296 char path[FILENAME_MAX], *cp = NULL; 297 298 if (!Fake) { 299 if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) { 300 const char *ext; 301 302 ext = strrchr(pkg_fullname, '.'); 303 if (ext == NULL) 304 ext = ".tbz"; 305 snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext); 306 if (fexists(path)) 307 cp = path; 308 else 309 cp = fileFindByPath(pkg, p->name); 310 if (cp) { 311 if (Verbose) 312 printf("Loading it from %s.\n", cp); 313 if (vsystem("pkg_add %s'%s'", Verbose ? "-v " : "", cp)) { 314 warnx("autoload of dependency '%s' failed%s", 315 cp, Force ? " (proceeding anyway)" : "!"); 316 if (!Force) 317 ++code; 318 } 319 } 320 else { 321 warnx("could not find package %s %s", 322 p->name, Force ? " (proceeding anyway)" : "!"); 323 if (!Force) 324 ++code; 325 } 326 } 327 else if ((cp = fileGetURL(pkg, p->name)) != NULL) { 328 if (Verbose) 329 printf("Finished loading %s over FTP.\n", p->name); 330 if (!fexists("+CONTENTS")) { 331 warnx("autoloaded package %s has no +CONTENTS file?", 332 p->name); 333 if (!Force) 334 ++code; 335 } 336 else if (vsystem("(pwd; cat +CONTENTS) | pkg_add %s-S", Verbose ? "-v " : "")) { 337 warnx("pkg_add of dependency '%s' failed%s", 338 p->name, Force ? " (proceeding anyway)" : "!"); 339 if (!Force) 340 ++code; 341 } 342 else if (Verbose) 343 printf("\t'%s' loaded successfully.\n", p->name); 344 /* Nuke the temporary playpen */ 345 leave_playpen(); 346 } 347 } 348 else { 349 if (Verbose) 350 printf("and was not found%s.\n", Force ? " (proceeding anyway)" : ""); 351 else 352 printf("Package dependency %s for %s not found%s\n", p->name, pkg, 353 Force ? " (proceeding anyway)" : "!"); 354 if (!Force) 355 ++code; 356 } 357 } 358 else if (Verbose) 359 printf(" - already installed.\n"); 360 } 361 362 if (code != 0) 363 goto bomb; 364 365 /* Look for the requirements file */ 366 if (fexists(REQUIRE_FNAME)) { 367 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 368 if (Verbose) 369 printf("Running requirements file first for %s..\n", Plist.name); 370 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) { 371 warnx("package %s fails requirements %s", pkg_fullname, 372 Force ? "installing anyway" : "- not installed"); 373 if (!Force) { 374 code = 1; 375 goto success; /* close enough for government work */ 376 } 377 } 378 } 379 380 /* 381 * Test whether to use the old method of passing tokens to installation 382 * scripts, and set appropriate variables.. 383 */ 384 385 if (fexists(POST_INSTALL_FNAME)) { 386 new_m = 1; 387 sprintf(post_script, "%s", POST_INSTALL_FNAME); 388 pre_arg[0] = '\0'; 389 post_arg[0] = '\0'; 390 } else { 391 if (fexists(INSTALL_FNAME)) { 392 sprintf(post_script, "%s", INSTALL_FNAME); 393 sprintf(pre_arg, "PRE-INSTALL"); 394 sprintf(post_arg, "POST-INSTALL"); 395 } 396 } 397 398 /* If we're really installing, and have an installation file, run it */ 399 if (!NoInstall && fexists(pre_script)) { 400 vsystem("chmod +x %s", pre_script); /* make sure */ 401 if (Verbose) 402 printf("Running pre-install for %s..\n", Plist.name); 403 if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) { 404 warnx("install script returned error status"); 405 unlink(pre_script); 406 code = 1; 407 goto success; /* nothing to uninstall yet */ 408 } 409 } 410 411 /* Now finally extract the entire show if we're not going direct */ 412 if (!inPlace && !Fake) 413 extract_plist(".", &Plist); 414 415 if (!Fake && fexists(MTREE_FNAME)) { 416 if (Verbose) 417 printf("Running mtree for %s..\n", Plist.name); 418 p = find_plist(&Plist, PLIST_CWD); 419 if (Verbose) 420 printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL); 421 if (!Fake) { 422 if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL)) 423 warnx("mtree returned a non-zero status - continuing"); 424 } 425 } 426 427 /* Run the installation script one last time? */ 428 if (!NoInstall && fexists(post_script)) { 429 vsystem("chmod +x %s", post_script); /* make sure */ 430 if (Verbose) 431 printf("Running post-install for %s..\n", Plist.name); 432 if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) { 433 warnx("install script returned error status"); 434 unlink(post_script); 435 code = 1; 436 goto fail; 437 } 438 } 439 440 /* Time to record the deed? */ 441 if (!NoRecord && !Fake) { 442 char contents[FILENAME_MAX]; 443 FILE *contfile; 444 445 if (getuid() != 0) 446 warnx("not running as root - trying to record install anyway"); 447 sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name); 448 zapLogDir = 1; 449 if (Verbose) 450 printf("Attempting to record package into %s..\n", LogDir); 451 if (make_hierarchy(LogDir)) { 452 warnx("can't record package into '%s', you're on your own!", 453 LogDir); 454 bzero(LogDir, FILENAME_MAX); 455 code = 1; 456 goto success; /* close enough for government work */ 457 } 458 /* Make sure pkg_info can read the entry */ 459 vsystem("chmod a+rx %s", LogDir); 460 move_file(".", DESC_FNAME, LogDir); 461 move_file(".", COMMENT_FNAME, LogDir); 462 if (fexists(INSTALL_FNAME)) 463 move_file(".", INSTALL_FNAME, LogDir); 464 if (fexists(POST_INSTALL_FNAME)) 465 move_file(".", POST_INSTALL_FNAME, LogDir); 466 if (fexists(DEINSTALL_FNAME)) 467 move_file(".", DEINSTALL_FNAME, LogDir); 468 if (fexists(POST_DEINSTALL_FNAME)) 469 move_file(".", POST_DEINSTALL_FNAME, LogDir); 470 if (fexists(REQUIRE_FNAME)) 471 move_file(".", REQUIRE_FNAME, LogDir); 472 if (fexists(DISPLAY_FNAME)) 473 move_file(".", DISPLAY_FNAME, LogDir); 474 if (fexists(MTREE_FNAME)) 475 move_file(".", MTREE_FNAME, LogDir); 476 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 477 contfile = fopen(contents, "w"); 478 if (!contfile) { 479 warnx("can't open new contents file '%s'! can't register pkg", 480 contents); 481 goto success; /* can't log, but still keep pkg */ 482 } 483 write_plist(&Plist, contfile); 484 fclose(contfile); 485 for (p = Plist.head; p ; p = p->next) { 486 char *deporigin, **depnames; 487 int i; 488 489 if (p->type != PLIST_PKGDEP) 490 continue; 491 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : 492 NULL; 493 if (Verbose) { 494 printf("Trying to record dependency on package '%s'", p->name); 495 if (deporigin != NULL) 496 printf(" with '%s' origin", deporigin); 497 printf(".\n"); 498 } 499 500 depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) : 501 NULL; 502 if (depnames == NULL) { 503 depnames = alloca(sizeof(*depnames) * 2); 504 depnames[0] = p->name; 505 depnames[1] = NULL; 506 } 507 for (i = 0; depnames[i] != NULL; i++) { 508 sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i], 509 REQUIRED_BY_FNAME); 510 if (strcmp(p->name, depnames[i]) != 0) 511 warnx("warning: package '%s' requires '%s', but '%s' " 512 "is installed", Plist.name, p->name, depnames[i]); 513 contfile = fopen(contents, "a"); 514 if (!contfile) 515 warnx("can't open dependency file '%s'!\n" 516 "dependency registration is incomplete", contents); 517 else { 518 fprintf(contfile, "%s\n", Plist.name); 519 if (fclose(contfile) == EOF) 520 warnx("cannot properly close file %s", contents); 521 } 522 } 523 } 524 if (Verbose) 525 printf("Package %s registered in %s\n", Plist.name, LogDir); 526 } 527 528 if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) { 529 FILE *fp; 530 char buf[BUFSIZ]; 531 532 snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name); 533 fp = fopen(buf, "r"); 534 if (fp) { 535 putc('\n', stdout); 536 while (fgets(buf, sizeof(buf), fp)) 537 fputs(buf, stdout); 538 putc('\n', stdout); 539 (void) fclose(fp); 540 } else 541 warnx("cannot open %s as display file", buf); 542 } 543 544 goto success; 545 546 bomb: 547 code = 1; 548 goto success; 549 550 fail: 551 /* Nuke the whole (installed) show, XXX but don't clean directories */ 552 if (!Fake) 553 delete_package(FALSE, FALSE, &Plist); 554 555 success: 556 /* delete the packing list contents */ 557 free_plist(&Plist); 558 leave_playpen(); 559 return code; 560} 561 562static int 563sanity_check(char *pkg) 564{ 565 int code = 0; 566 567 if (!fexists(CONTENTS_FNAME)) { 568 warnx("package %s has no CONTENTS file!", pkg); 569 code = 1; 570 } 571 else if (!fexists(COMMENT_FNAME)) { 572 warnx("package %s has no COMMENT file!", pkg); 573 code = 1; 574 } 575 else if (!fexists(DESC_FNAME)) { 576 warnx("package %s has no DESC file!", pkg); 577 code = 1; 578 } 579 return code; 580} 581 582void 583cleanup(int sig) 584{ 585 static int in_cleanup = 0; 586 587 if (!in_cleanup) { 588 in_cleanup = 1; 589 if (sig) 590 printf("Signal %d received, cleaning up..\n", sig); 591 if (!Fake && zapLogDir && LogDir[0]) 592 vsystem("%s -rf %s", REMOVE_CMD, LogDir); 593 leave_playpen(); 594 } 595 if (sig) 596 exit(1); 597} 598