perform.c revision 4996
1#ifndef lint 2static const char *rcsid = "$Id: perform.c,v 1.12 1994/11/17 10:53:21 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 (void)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 (Verbose) 146 printf("Checking dependency on package list `%s'\n", p->name); 147 if (!Fake && vsystem("pkg_info -e %s", p->name)) { 148 whinge("Package `%s' depends on missing package `%s'%s.", PkgName, 149 p->name, Force ? " (proceeding anyway)" : ""); 150 if (!Force) 151 code++; 152 } 153 } 154 if (code != 0) 155 goto success; /* close enough for government work */ 156 157 if (fexists(REQUIRE_FNAME)) { 158 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ 159 if (Verbose) 160 printf("Running requirements file first for %s..\n", PkgName); 161 if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, PkgName)) { 162 whinge("Package %s fails requirements %s", 163 pkg_fullname, 164 Force ? "installing anyway" : "- not installed."); 165 if (!Force) { 166 code = 1; 167 goto success; /* close enough for government work */ 168 } 169 } 170 } 171 if (!NoInstall && fexists(INSTALL_FNAME)) { 172 vsystem("chmod +x %s", INSTALL_FNAME); /* make sure */ 173 if (Verbose) 174 printf("Running install with PRE-INSTALL for %s..\n", PkgName); 175 if (!Fake && vsystem("./%s %s PRE-INSTALL", INSTALL_FNAME, PkgName)) { 176 whinge("Install script returned error status."); 177 code = 1; 178 goto success; /* nothing to uninstall yet */ 179 } 180 } 181 extract_plist(home, &Plist); 182 if (!NoInstall && fexists(MTREE_FNAME)) { 183 if (Verbose) 184 printf("Running mtree for %s..\n", PkgName); 185 p = find_plist(&Plist, PLIST_CWD); 186 if (Verbose) 187 printf("mtree -u -f %s -d -e -p %s\n", MTREE_FNAME, 188 p ? p->name : "/"); 189 if (!Fake) { 190 pid_t chpid; 191 int rval, status; 192 chpid = fork(); 193 if (chpid == 0) { 194 execl("/usr/sbin/mtree", "mtree", "-u", "-f", MTREE_FNAME, 195 "-d", "-e", "-p", p ? p->name : "/"); 196 perror("cannot execute mtree"); 197 exit(3); 198 } 199 if (chpid == (pid_t) -1) { 200 warn("Cannot fork mtree"); 201 code = 1; 202 goto fail; 203 } 204 if (waitpid(chpid, &status, 0) == -1) { 205 warn("Cannot wait for mtree"); 206 code = 1; 207 goto fail; 208 } 209 if (!WIFEXITED(status)) { 210 whinge("Strange exit from mtree: %x", status); 211 code = 1; 212 goto fail; 213 } 214#ifdef DEBUG 215 whinge("mtree exits %d\n", WEXITSTATUS(status)); 216#endif 217 switch (WEXITSTATUS(status)) { 218 case 0: 219 break; /* normal */ 220 case 2: 221 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"); 222 break; 223 default: 224 whinge("Error status %d from mtree.", WEXITSTATUS(status)); 225 code = 1; 226 goto fail; 227 } 228 } 229 } 230 if (!NoInstall && fexists(INSTALL_FNAME)) { 231 if (Verbose) 232 printf("Running install with POST-INSTALL for %s..\n", PkgName); 233 if (!Fake && vsystem("./%s %s POST-INSTALL", INSTALL_FNAME, PkgName)) { 234 whinge("Install script returned error status."); 235 code = 1; 236 goto fail; 237 } 238 } 239 if (!NoRecord && !Fake) { 240 char contents[FILENAME_MAX]; 241 FILE *cfile; 242 243 umask(022); 244 if (getuid() != 0) 245 whinge("Not running as root - trying to record install anyway."); 246 if (!PkgName) { 247 whinge("No package name! Can't record package, sorry."); 248 code = 1; 249 goto success; /* well, partial anyway */ 250 } 251 /* Protect against old packages with bogus @name fields */ 252 sprintf(LogDir, "%s/%s", LOG_DIR, basename_of(PkgName)); 253 if (Verbose) 254 printf("Attempting to record package into %s..\n", LogDir); 255 if (make_hierarchy(LogDir)) { 256 whinge("Can't record package into '%s', you're on your own!", 257 LogDir); 258 bzero(LogDir, FILENAME_MAX); 259 code = 1; 260 goto success; /* close enough for government work */ 261 } 262 /* Make sure pkg_info can read the entry */ 263 vsystem("chmod a+rx %s", LogDir); 264 if (fexists(DEINSTALL_FNAME)) 265 copy_file(".", DEINSTALL_FNAME, LogDir); 266 if (fexists(REQUIRE_FNAME)) 267 copy_file(".", REQUIRE_FNAME, LogDir); 268 sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME); 269 cfile = fopen(contents, "w"); 270 if (!cfile) { 271 whinge("Can't open new contents file '%s'! Can't register pkg.", 272 contents); 273 goto success; /* can't log, but still keep pkg */ 274 } 275 write_plist(&Plist, cfile); 276 fclose(cfile); 277 copy_file(".", DESC_FNAME, LogDir); 278 copy_file(".", COMMENT_FNAME, LogDir); 279 if (fexists(DISPLAY_FNAME)) 280 copy_file(".", DISPLAY_FNAME, LogDir); 281 for (p = Plist.head; p ; p = p->next) { 282 if (p->type != PLIST_PKGDEP) 283 continue; 284 if (Verbose) 285 printf("Attempting to record dependency on package `%s'\n", 286 p->name); 287 sprintf(contents, "%s/%s/%s", LOG_DIR, basename_of(p->name), 288 REQUIRED_BY_FNAME); 289 cfile = fopen(contents, "a"); 290 if (!cfile) { 291 whinge("Can't open dependency file '%s'!\n\tDependency registration incomplete.", 292 contents); 293 continue; 294 } 295 fprintf(cfile, "%s\n", basename_of(PkgName)); 296 if (fclose(cfile) == EOF) 297 warn("Cannot properly close file %s", contents); 298 } 299 if (Verbose) 300 printf("Package %s registered in %s\n", PkgName, LogDir); 301 } 302 303 if (p = find_plist(&Plist, PLIST_DISPLAY)) { 304 FILE *fp; 305 char buf[BUFSIZ]; 306 fp = fopen(p->name, "r"); 307 if (fp) { 308 putc('\n', stdout); 309 while (fgets(buf, sizeof(buf), fp)) 310 fputs(buf, stdout); 311 putc('\n', stdout); 312 (void) fclose(fp); 313 } else 314 warn("Cannot open display file `%s'.", p->name); 315 } 316 317 goto success; 318 319 fail: 320 /* Nuke the whole (installed) show, XXX but don't clean directories */ 321 if (!Fake) 322 delete_package(FALSE, FALSE, &Plist); 323 324 success: 325 /* delete the packing list contents */ 326 leave_playpen(); 327 328 return code; 329} 330 331static int 332sanity_check(char *pkg) 333{ 334 if (!fexists(CONTENTS_FNAME)) { 335 whinge("Package %s has no CONTENTS file!", pkg); 336 return 1; 337 } 338 if (!fexists(COMMENT_FNAME)) { 339 whinge("Package %s has no COMMENT file!", pkg); 340 return 1; 341 } 342 if (!fexists(DESC_FNAME)) { 343 whinge("Package %s has no DESC file!", pkg); 344 return 1; 345 } 346 return 0; 347} 348 349void 350cleanup(int signo) 351{ 352 if (signo) 353 printf("Signal %d received, cleaning up..\n", signo); 354 if (Plist.head) { 355 if (!Fake) 356 delete_package(FALSE, FALSE, &Plist); 357 free_plist(&Plist); 358 } 359 if (!Fake && LogDir[0]) 360 vsystem("%s -rf %s", REMOVE_CMD, LogDir); 361 leave_playpen(); 362} 363