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