]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/pkg_install/delete/perform.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / pkg_install / delete / perform.c
1 /*
2  * FreeBSD install - a package for the installation and maintenance
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 delete module.
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include <err.h>
25 #include "lib.h"
26 #include "delete.h"
27
28 static int pkg_do(char *);
29 static void sanity_check(char *);
30 static void undepend(char *, char *);
31 static char LogDir[FILENAME_MAX];
32
33
34 int
35 pkg_perform(char **pkgs)
36 {
37     char **matched, **rb, **rbtmp;
38     int errcode, i, j;
39     int err_cnt = 0;
40     struct reqr_by_entry *rb_entry;
41     struct reqr_by_head *rb_list;
42
43     if (MatchType != MATCH_EXACT) {
44         matched = matchinstalled(MatchType, pkgs, &errcode);
45         if (errcode != 0)
46             return 1;
47             /* Not reached */
48
49         /*
50          * Copy matched[] into pkgs[], because we'll need to use
51          * matchinstalled() later on.
52          */
53         if (matched != NULL) {
54             pkgs = NULL;
55             for (i = 0; matched[i] != NULL; i++) {
56                 pkgs = realloc(pkgs, sizeof(*pkgs) * (i + 2));
57                 pkgs[i] = strdup(matched[i]);
58             }
59             pkgs[i] = NULL;
60         }
61         else switch (MatchType) {
62             case MATCH_GLOB:
63                 break;
64             case MATCH_ALL:
65                 warnx("no packages installed");
66                 return 0;
67             case MATCH_EREGEX:
68             case MATCH_REGEX:
69                 warnx("no packages match pattern(s)");
70                 return 1;
71             default:
72                 break;
73         }
74     }
75
76     err_cnt += sortdeps(pkgs);
77     for (i = 0; pkgs[i]; i++) {
78         if (Recursive == TRUE) {
79             errcode = requiredby(pkgs[i], &rb_list, FALSE, TRUE);
80             if (errcode < 0) {
81                 err_cnt++;
82             } else if (errcode > 0) {
83                 /*
84                  * Copy values from the rb_list queue into argv-like NULL
85                  * terminated list because requiredby() uses some static
86                  * storage, while pkg_do() below will call this function,
87                  * thus blowing our rb_list away.
88                  */
89                 rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
90                 if (rb == NULL) {
91                     warnx("%s(): alloca() failed", __func__);
92                     err_cnt++;
93                     continue;
94                 }
95                 STAILQ_FOREACH(rb_entry, rb_list, link) {
96                     *rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
97                     if (*rbtmp == NULL) {
98                         warnx("%s(): alloca() failed", __func__);
99                         err_cnt++;
100                         continue;
101                     }
102                     strcpy(*rbtmp, rb_entry->pkgname);
103                     rbtmp++;
104                 }
105                 *rbtmp = NULL;
106
107                 err_cnt += sortdeps(rb);
108                 for (j = 0; rb[j]; j++)
109                     err_cnt += pkg_do(rb[j]);
110             }
111         }
112         err_cnt += pkg_do(pkgs[i]);
113     }
114
115     return err_cnt;
116 }
117
118 static Package Plist;
119
120 /* This is seriously ugly code following.  Written very fast! */
121 static int
122 pkg_do(char *pkg)
123 {
124     FILE *cfile;
125     char *deporigin, **deporigins = NULL, **depnames = NULL, ***depmatches, home[FILENAME_MAX];
126     PackingList p;
127     int i, len;
128     int isinstalled;
129     /* support for separate pre/post install scripts */
130     int new_m = 0, dep_count = 0;
131     const char *pre_script = DEINSTALL_FNAME;
132     const char *post_script, *pre_arg, *post_arg;
133     struct reqr_by_entry *rb_entry;
134     struct reqr_by_head *rb_list;
135     int fd;
136     struct stat sb;
137
138     if (!pkg || !(len = strlen(pkg)))
139         return 1;
140     if (pkg[len - 1] == '/')
141         pkg[len - 1] = '\0';
142
143     /* Reset some state */
144     if (Plist.head)
145         free_plist(&Plist);
146
147     sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
148
149     isinstalled = isinstalledpkg(pkg);
150     if (isinstalled == 0) {
151         warnx("no such package '%s' installed", pkg);
152         return 1;
153     } else if (isinstalled < 0) {
154         warnx("the package info for package '%s' is corrupt%s",
155               pkg, Force ? " (but I'll delete it anyway)" : " (use -f to force removal)");
156         if (!Force)
157             return 1;
158         if (!Fake) {
159             if (vsystem("%s -rf %s", REMOVE_CMD, LogDir)) {
160                 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
161             } else {
162                 warnx("couldn't completely deinstall package '%s',\n"
163                       "only the log entry in %s was removed", pkg, LogDir);
164             }
165         }
166         return 0;
167     }
168
169     if (!getcwd(home, FILENAME_MAX)) {
170         cleanup(0);
171         errx(2, "%s: unable to get current working directory!", __func__);
172     }
173
174     if (chdir(LogDir) == FAIL) {
175         warnx("unable to change directory to %s! deinstall failed", LogDir);
176         return 1;
177     }
178
179     if (Interactive == TRUE) {
180         int first, ch;
181
182         (void)fprintf(stderr, "delete %s? ", pkg);
183         (void)fflush(stderr);
184         first = ch = getchar();
185         while (ch != '\n' && ch != EOF)
186             ch = getchar();
187         if (first != 'y' && first != 'Y')
188             return 0;
189             /* Not reached */
190     }
191
192     if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
193         return 1;
194     if (!STAILQ_EMPTY(rb_list)) {
195         warnx("package '%s' is required by these other packages\n"
196               "and may not be deinstalled%s:",
197               pkg, Force ? " (but I'll delete it anyway)" : "");
198         STAILQ_FOREACH(rb_entry, rb_list, link)
199             fprintf(stderr, "%s\n", rb_entry->pkgname);
200         if (!Force)
201             return 1;
202     }
203
204     sanity_check(LogDir);
205     cfile = fopen(CONTENTS_FNAME, "r");
206
207     if (!cfile) {
208         warnx("unable to open '%s' file", CONTENTS_FNAME);
209         return 1;
210     }
211
212     /* If we have a prefix, add it now */
213     if (Prefix)
214         add_plist(&Plist, PLIST_CWD, Prefix);
215     read_plist(&Plist, cfile);
216     fclose(cfile);
217     p = find_plist(&Plist, PLIST_CWD);
218
219     if (!p) {
220         warnx("package '%s' doesn't have a prefix", pkg);
221         return 1;
222     }
223
224     setenv(PKG_PREFIX_VNAME, p->name, 1);
225
226     if ((fd = open(REQUIRE_FNAME, O_RDWR)) != -1) {
227         fstat(fd, &sb);
228         fchmod(fd, sb.st_mode | S_IXALL);       /* be sure, chmod a+x */
229         close(fd);
230         if (Verbose)
231             printf("Executing 'require' script.\n");
232         if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
233             warnx("package %s fails requirements %s", pkg,
234                    Force ? "" : "- not deleted");
235             if (!Force)
236                 return 1;
237         }
238     }
239
240     /*
241      * Test whether to use the old method of passing tokens to deinstallation
242      * scripts, and set appropriate variables..
243      */
244
245     if (fexists(POST_DEINSTALL_FNAME)) {
246         new_m = 1;
247         post_script = POST_DEINSTALL_FNAME;
248         pre_arg = post_arg = "";
249     } else if (fexists(DEINSTALL_FNAME)) {
250         post_script = DEINSTALL_FNAME;
251         pre_arg = "DEINSTALL";
252         post_arg = "POST-DEINSTALL";
253     } else {
254         post_script = pre_arg = post_arg = NULL;
255     }
256
257     if (!NoDeInstall && pre_script != NULL && (fd = open(pre_script, O_RDWR)) != -1) {
258         if (Fake)
259             printf("Would execute de-install script at this point.\n");
260         else {
261             fstat(fd, &sb);
262             fchmod(fd, sb.st_mode | S_IXALL);       /* be sure, chmod a+x */
263             close(fd);
264             if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) {
265                 warnx("deinstall script returned error status");
266                 if (!Force)
267                     return 1;
268             }
269         }
270     }
271
272     for (p = Plist.head; p ; p = p->next) {
273         if (p->type != PLIST_PKGDEP)
274             continue;
275         deporigin = (p->next != NULL && p->next->type == PLIST_DEPORIGIN) ? p->next->name :
276                                                          NULL;
277         if (Verbose) {
278             printf("Trying to remove dependency on package '%s'", p->name);
279             if (deporigin != NULL)
280                 printf(" with '%s' origin", deporigin);
281             printf(".\n");
282         }
283         if (!Fake) {
284             if (deporigin) {
285                 deporigins = realloc(deporigins, (dep_count + 2) * sizeof(*deporigins));
286                 depnames = realloc(depnames, (dep_count + 1) * sizeof(*depnames));
287                 deporigins[dep_count] = deporigin;
288                 deporigins[dep_count + 1] = NULL;
289                 depnames[dep_count] = p->name;
290                 dep_count++;
291             } else {
292                 undepend(p->name, pkg);
293             }
294         }
295     }
296
297     if (dep_count > 0) {
298         /* Undepend all the dependencies at once */
299         depmatches = matchallbyorigin((const char **)deporigins, NULL);
300         free(deporigins);
301         if (depmatches) {
302             for (i = 0; i < dep_count; i++) {
303                 if (depmatches[i]) {
304                     char **tmp = depmatches[i];
305                     int j;
306                     for (j = 0; tmp[j] != NULL; j++)
307                         undepend(tmp[j], pkg);
308                 } else if (depnames[i]) {
309                     undepend(depnames[i], pkg);
310                 }
311             }
312         }
313     }
314
315     if (chdir(home) == FAIL) {
316         cleanup(0);
317         errx(2, "%s: unable to return to working directory %s!", __func__,
318             home);
319     }
320
321     /*
322      * Some packages aren't packed right, so we need to just ignore
323      * delete_package()'s status.  Ugh! :-(
324      */
325     if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
326         warnx(
327         "couldn't entirely delete package `%s'\n"
328         "(perhaps the packing list is incorrectly specified?)", pkg);
329
330     if (chdir(LogDir) == FAIL) {
331         warnx("unable to change directory to %s! deinstall failed", LogDir);
332         return 1;
333     }
334
335     if (!NoDeInstall && post_script != NULL && (fd = open(post_script, O_RDWR)) != -1) {
336         if (Fake)
337             printf("Would execute post-deinstall script at this point.\n");
338         else {
339             fstat(fd, &sb);
340             fchmod(fd, sb.st_mode | S_IXALL);       /* be sure, chmod a+x */
341             close(fd);
342             if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
343                 warnx("post-deinstall script returned error status");
344                 if (!Force)
345                     return 1;
346             }
347         }
348     }
349
350     if (chdir(home) == FAIL) {
351         cleanup(0);
352         errx(2, "%s: unable to return to working directory %s!", __func__,
353             home);
354     }
355
356     if (!Fake) {
357         if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
358             warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
359             if (!Force)
360                 return 1;
361         }
362     }
363     return 0;
364 }
365
366 static void
367 sanity_check(char *pkg)
368 {
369     if (!fexists(CONTENTS_FNAME)) {
370         cleanup(0);
371         errx(2, "%s: installed package %s has no %s file!", __func__,
372             pkg, CONTENTS_FNAME);
373     }
374 }
375
376 void
377 cleanup(int sig)
378 {
379     if (sig)
380         exit(1);
381 }
382
383 static void
384 undepend(char *p, char *pkgname)
385 {
386     char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
387     FILE *fpwr;
388     int s;
389     struct reqr_by_entry *rb_entry;
390     struct reqr_by_head *rb_list;
391
392
393     if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
394         return;
395     snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
396     snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
397     s = mkstemp(ftmp);
398     if (s == -1) {
399         warnx("couldn't open temp file '%s'", ftmp);
400         return;
401     }
402     fpwr = fdopen(s, "w");
403     if (fpwr == NULL) {
404         close(s);
405         warnx("couldn't fdopen temp file '%s'", ftmp);
406         goto cleanexit;
407     }
408     STAILQ_FOREACH(rb_entry, rb_list, link)
409         if (strcmp(rb_entry->pkgname, pkgname))         /* no match */
410             fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
411     if (fchmod(s, 0644) == FAIL) {
412         warnx("error changing permission of temp file '%s'", ftmp);
413         fclose(fpwr);
414         goto cleanexit;
415     }
416     if (fclose(fpwr) == EOF) {
417         warnx("error closing temp file '%s'", ftmp);
418         goto cleanexit;
419     }
420     if (rename(ftmp, fname) == -1)
421         warnx("error renaming '%s' to '%s'", ftmp, fname);
422 cleanexit:
423     remove(ftmp);
424     return;
425 }