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