perform.c revision 7713
1#ifndef lint 2static const char *rcsid = "$Id: perform.c,v 1.13 1994/12/06 00:51:34 jkh Exp $"; 3#endif 4 5/* 6 * FreeBSD install - a package for the installation and maintainance 7 * of non-core utilities. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Jordan K. Hubbard 19 * 18 July 1993 20 * 21 * This is the main body of the add module. 22 * 23 */ 24 25#include "lib.h" 26#include "add.h" 27 28#include <signal.h> 29#include <sys/wait.h> 30 31static int pkg_do(char *); 32static int sanity_check(char *); 33static char LogDir[FILENAME_MAX]; 34 35 36int 37pkg_perform(char **pkgs) 38{ 39 int i, err_cnt = 0; 40 41 signal(SIGINT, cleanup); 42 signal(SIGHUP, cleanup); 43 44 if (AddMode == SLAVE) 45 err_cnt = pkg_do(NULL); 46 else { 47 for (i = 0; pkgs[i]; i++) 48 err_cnt += pkg_do(pkgs[i]); 49 } 50 return err_cnt; 51} 52 53static Package Plist; 54 55/* This is seriously ugly code following. Written very fast! */ 56static int 57pkg_do(char *pkg) 58{ 59 char pkg_fullname[FILENAME_MAX]; 60 char home[FILENAME_MAX]; 61 FILE *cfile; 62 int code = 0; 63 PackingList p; 64 struct stat sb; 65 66 /* Reset some state */ 67 if (Plist.head) 68 free_plist(&Plist); 69 LogDir[0] = '\0'; 70 if (AddMode == SLAVE) { 71 char tmp_dir[FILENAME_MAX]; 72 73 fgets(tmp_dir, FILENAME_MAX, stdin); 74 tmp_dir[strlen(tmp_dir) - 1] = '\0'; /* pesky newline! */ 75 if (chdir(tmp_dir) == FAIL) { 76 whinge("pkg_add in SLAVE mode can't chdir to %s.", tmp_dir); 77 return 1; 78 } 79 read_plist(&Plist, stdin); 80 } 81 else { 82 if (!getcwd(home, FILENAME_MAX)) 83 upchuck("getcwd"); 84 85 if (pkg[0] == '/') /* full pathname? */ 86 strcpy(pkg_fullname, pkg); 87 else 88 sprintf(pkg_fullname, "%s/%s", home, pkg); 89 if (!fexists(pkg_fullname)) { 90 whinge("Can't find package '%s'.", pkg_fullname); 91 return 1; 92 } 93 /* 94 * Apply a crude heuristic to see how much space the package will 95 * take up once it's unpacked. I've noticed that most packages 96 * compress an average of 75%, so multiply by 4 for good measure. 97 */ 98 if (stat(pkg_fullname, &sb) == FAIL) { 99 whinge("Can't stat package file '%s'.", pkg_fullname); 100 return 1; 101 } 102 sb.st_size *= 4; 103 Home = make_playpen(PlayPen, sb.st_size); 104 if (unpack(pkg_fullname, NULL)) 105 return 1; 106 107 if (sanity_check(pkg_fullname)) 108 return 1; 109 110 cfile = fopen(CONTENTS_FNAME, "r"); 111 if (!cfile) { 112 whinge("Unable to open %s file.", CONTENTS_FNAME); 113 goto fail; 114 } 115 read_plist(&Plist, cfile); 116 fclose(cfile); 117 if (Prefix) { 118 /* 119 * If we have a prefix, delete the first one we see and add this 120 * one in place of it. 121 */ 122 delete_plist(&Plist, FALSE, PLIST_CWD, NULL); 123 add_plist_top(&Plist, PLIST_CWD, Prefix); 124 } 125 /* If we're running in MASTER mode, just output the plist and return */ 126 if (AddMode == MASTER) { 127 printf("%s\n", where_playpen()); 128 write_plist(&Plist, stdout); 129 return 0; 130 } 131 } 132 setenv(PKG_PREFIX_VNAME, 133 (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1); 134 PkgName = (p = find_plist(&Plist, PLIST_NAME)) ? p->name : "anonymous"; 135 /* Protect against old packages with bogus @name fields */ 136 sprintf(LogDir, "%s/%s", LOG_DIR, basename_of(PkgName)); 137 if (isdir(LogDir)) { 138 whinge("Package `%s' already recorded as installed.\n", PkgName); 139 code = 1; 140 goto success; /* close enough for government work */ 141 } 142 for (p = Plist.head; p ; p = p->next) { 143 if (p->type != PLIST_PKGDEP) 144 continue; 145 if (!Fake && vsystem("pkg_info -e %s", p->name)) { 146 char tmp[120]; 147 148 sprintf(tmp, "%s/%s.tgz", Home, p->name); 149 if (fexists(tmp)) { 150 if (Verbose) 151 printf("Package `%s' depends on `%s': Trying to load it.\n", PkgName, p->name); 152 if (vsystem("pkg_add %s", tmp)) { 153 whinge("Autoload of dependency package `%s' failed!%s", 154 p->name, Force ? " (proceeding anyway)" : ""); 155 if (!Force) 156 code++; 157 } 158 else if (Verbose) 159 printf("Dependency `%s' loaded successfully.\n", p->name); 160 } 161 else { 162 whinge("Package `%s' depends on missing package `%s'%s.", PkgName, 163 p->name, Force ? " (proceeding anyway)" : ""); 164 if (!Force) 165 code++; 166 } 167 } 168 } 169 if (code != 0) 170 goto success; /* close enough for government work */ 171 172 if (fexists(REQUIRE_FNAME)) { 173 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 174 if (Verbose) 175 printf("Running requirements file first for %s..\n", PkgName); 176 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 177 whinge("Package %s fails requirements %s", 178 pkg_fullname, 179 Force ? "installing anyway" : "- not installed."); 180 if (!Force) { 181 code = 1; 182 goto success; /* close enough for government work */ 183 } 184 } 185 } 186 if (!NoInstall && fexists(INSTALL_FNAME)) { 187 vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 188 if (Verbose) 189 printf("Running install with PRE-INSTALL for %s..\n", PkgName); 190 if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 191 whinge("Install script returned error status."); 192 code = 1; 193 goto success; /* nothing to uninstall yet */ 194 } 195 } 196 extract_plist(home, &Plist); 197 if (!NoInstall && fexists(MTREE_FNAME)) { 198 if (Verbose) 199 printf("Running mtree for %s..\n", PkgName); 200 p = find_plist(&Plist, PLIST_CWD); 201 if (Verbose) 202 printf("mtree -u -f %s -d -e -p %s\n", MTREE_FNAME, 203 p ? p->name : "/"); 204 if (!Fake) { 205 pid_t chpid; 206 int rval, status; 207 chpid = fork(); 208 if (chpid == 0) { 209 execl("/usr/sbin/mtree", "mtree", "-u", "-f", MTREE_FNAME, 210 "-d", "-e", "-p", p ? p->name : "/"); 211 perror("cannot execute mtree"); 212 exit(3); 213 } 214 if (chpid == (pid_t) -1) { 215 warn("Cannot fork mtree"); 216 code = 1; 217 goto fail; 218 } 219 if (waitpid(chpid, &status, 0) == -1) { 220 warn("Cannot wait for mtree"); 221 code = 1; 222 goto fail; 223 } 224 if (!WIFEXITED(status)) { 225 whinge("Strange exit from mtree: %x", status); 226 code = 1; 227 goto fail; 228 } 229#ifdef DEBUG 230 whinge("mtree exits %d\n", WEXITSTATUS(status)); 231#endif 232 switch (WEXITSTATUS(status)) { 233 case 0: 234 break; /* normal */ 235 case 2: 236 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"); 237 break; 238 default: 239 whinge("Error status %d from mtree.", WEXITSTATUS(status)); 240 code = 1; 241 goto fail; 242 } 243 } 244 } 245 if (!NoInstall && fexists(INSTALL_FNAME)) { 246 if (Verbose) 247 printf("Running install with POST-INSTALL for %s..\n", PkgName); 248 if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 249 whinge("Install script returned error status."); 250 code = 1; 251 goto fail; 252 } 253 } 254 if (!NoRecord && !Fake) { 255 char contents[FILENAME_MAX]; 256 FILE *cfile; 257 258 umask(022); 259 if (getuid() != 0) 260 whinge("Not running as root - trying to record install anyway."); 261 if (!PkgName) { 262 whinge("No package name! Can't record package, sorry."); 263 code = 1; 264 goto success; /* well, partial anyway */ 265 } 266 /* Protect against old packages with bogus @name fields */ 267 sprintf(LogDir, "%s/%s", LOG_DIR, basename_of(PkgName)); 268 if (Verbose) 269 printf("Attempting to record package into %s..\n", LogDir); 270 if (make_hierarchy(LogDir)) { 271 whinge("Can't record package into '%s', you're on your own!", 272 LogDir); 273 bzero(LogDir, FILENAME_MAX); 274 code = 1; 275 goto success; /* close enough for government work */ 276 } 277 /* Make sure pkg_info can read the entry */ 278 vsystem("chmod a+rx %s", LogDir); 279 if (fexists(DEINSTALL_FNAME)) 280 copy_file(".", DEINSTALL_FNAME, LogDir); 281 if (fexists(REQUIRE_FNAME)) 282 copy_file(".", REQUIRE_FNAME, LogDir); 283 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 284 cfile = fopen(contents, "w"); 285 if (!cfile) { 286 whinge("Can't open new contents file '%s'! Can't register pkg.", 287 contents); 288 goto success; /* can't log, but still keep pkg */ 289 } 290 write_plist(&Plist, cfile); 291 fclose(cfile); 292 copy_file(".", DESC_FNAME, LogDir); 293 copy_file(".", COMMENT_FNAME, LogDir); 294 if (fexists(DISPLAY_FNAME)) 295 copy_file(".", DISPLAY_FNAME, LogDir); 296 for (p = Plist.head; p ; p = p->next) { 297 if (p->type != PLIST_PKGDEP) 298 continue; 299 if (Verbose) 300 printf("Attempting to record dependency on package `%s'\n", 301 p->name); 302 sprintf(contents, "%s/%s/%s", LOG_DIR, basename_of(p->name), 303 REQUIRED_BY_FNAME); 304 cfile = fopen(contents, "a"); 305 if (!cfile) { 306 whinge("Can't open dependency file '%s'!\n\tDependency registration incomplete.", 307 contents); 308 continue; 309 } 310 fprintf(cfile, "%s\n", basename_of(PkgName)); 311 if (fclose(cfile) == EOF) 312 warn("Cannot properly close file %s", contents); 313 } 314 if (Verbose) 315 printf("Package %s registered in %s\n", PkgName, LogDir); 316 } 317 318 if (p = find_plist(&Plist, PLIST_DISPLAY)) { 319 FILE *fp; 320 char buf[BUFSIZ]; 321 fp = fopen(p->name, "r"); 322 if (fp) { 323 putc('\n', stdout); 324 while (fgets(buf, sizeof(buf), fp)) 325 fputs(buf, stdout); 326 putc('\n', stdout); 327 (void) fclose(fp); 328 } else 329 warn("Cannot open display file `%s'.", p->name); 330 } 331 332 goto success; 333 334 fail: 335 /* Nuke the whole (installed) show, XXX but don't clean directories */ 336 if (!Fake) 337 delete_package(FALSE, FALSE, &Plist); 338 339 success: 340 /* delete the packing list contents */ 341 leave_playpen(); 342 343 return code; 344} 345 346static int 347sanity_check(char *pkg) 348{ 349 if (!fexists(CONTENTS_FNAME)) { 350 whinge("Package %s has no CONTENTS file!", pkg); 351 return 1; 352 } 353 if (!fexists(COMMENT_FNAME)) { 354 whinge("Package %s has no COMMENT file!", pkg); 355 return 1; 356 } 357 if (!fexists(DESC_FNAME)) { 358 whinge("Package %s has no DESC file!", pkg); 359 return 1; 360 } 361 return 0; 362} 363 364void 365cleanup(int signo) 366{ 367 if (signo) 368 printf("Signal %d received, cleaning up..\n", signo); 369 if (Plist.head) { 370 if (!Fake) 371 delete_package(FALSE, FALSE, &Plist); 372 free_plist(&Plist); 373 } 374 if (!Fake && LogDir[0]) 375 vsystem("%s -rf %s", REMOVE_CMD, LogDir); 376 leave_playpen(); 377} 378