]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/pkg_install/lib/pen.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / pkg_install / lib / pen.c
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  * Routines for managing the "play pen".
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include "lib.h"
25 #include <err.h>
26 #include <libutil.h>
27 #include <libgen.h>
28 #include <sys/signal.h>
29 #include <sys/param.h>
30 #include <sys/mount.h>
31
32 /* For keeping track of where we are */
33 static char PenLocation[FILENAME_MAX];
34 static char Previous[FILENAME_MAX];
35
36 char *
37 where_playpen(void)
38 {
39     return PenLocation;
40 }
41
42 /* Find a good place to play. */
43 static char *
44 find_play_pen(char *pen, off_t sz)
45 {
46     char *cp;
47     struct stat sb;
48     char humbuf[6];
49
50     if (pen[0] && isdir(dirname(pen)) == TRUE && (min_free(dirname(pen)) >= sz))
51         return pen;
52     else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
53         sprintf(pen, "%s/instmp.XXXXXX", cp);
54     else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
55         sprintf(pen, "%s/instmp.XXXXXX", cp);
56     else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz)
57         strcpy(pen, "/var/tmp/instmp.XXXXXX");
58     else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz)
59         strcpy(pen, "/tmp/instmp.XXXXXX");
60     else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz)
61         strcpy(pen, "/usr/tmp/instmp.XXXXXX");
62     else {
63         cleanup(0);
64         humanize_number(humbuf, sizeof humbuf, sz, "", HN_AUTOSCALE,
65             HN_NOSPACE);
66         errx(2,
67 "%s: can't find enough temporary space to extract the files, please set your\n"
68 "PKG_TMPDIR environment variable to a location with at least %s bytes\n"
69 "free", __func__, humbuf);
70         return NULL;
71     }
72     return pen;
73 }
74
75 #define MAX_STACK       20
76 static char *pstack[MAX_STACK];
77 static int pdepth = -1;
78
79 static void
80 pushPen(const char *pen)
81 {
82     if (++pdepth == MAX_STACK)
83         errx(2, "%s: stack overflow.\n", __func__);
84     pstack[pdepth] = strdup(pen);
85 }
86
87 static void
88 popPen(char *pen)
89 {
90     if (pdepth == -1) {
91         pen[0] = '\0';
92         return;
93     }
94     strcpy(pen, pstack[pdepth]);
95     free(pstack[pdepth--]);
96 }
97     
98 /*
99  * Make a temporary directory to play in and chdir() to it, returning
100  * pathname of previous working directory.
101  */
102 char *
103 make_playpen(char *pen, off_t sz)
104 {
105     char humbuf1[6], humbuf2[6];
106
107     if (!find_play_pen(pen, sz))
108         return NULL;
109
110     if (!mkdtemp(pen)) {
111         cleanup(0);
112         errx(2, "%s: can't mktemp '%s'", __func__, pen);
113     }
114     if (chmod(pen, 0700) == FAIL) {
115         cleanup(0);
116         errx(2, "%s: can't mkdir '%s'", __func__, pen);
117     }
118
119     if (Verbose) {
120         if (sz) {
121             humanize_number(humbuf1, sizeof humbuf1, sz, "", HN_AUTOSCALE,
122                 HN_NOSPACE);
123             humanize_number(humbuf2, sizeof humbuf2, min_free(pen),
124                 "", HN_AUTOSCALE, HN_NOSPACE);
125             fprintf(stderr, "Requested space: %s bytes, free space: %s bytes in %s\n", humbuf1, humbuf2, pen);
126         }
127     }
128
129     if (min_free(pen) < sz) {
130         rmdir(pen);
131         cleanup(0);
132         errx(2, "%s: not enough free space to create '%s'.\n"
133              "Please set your PKG_TMPDIR environment variable to a location\n"
134              "with more space and\ntry the command again", __func__, pen);
135     }
136
137     if (!getcwd(Previous, FILENAME_MAX)) {
138         upchuck("getcwd");
139         return NULL;
140     }
141
142     if (chdir(pen) == FAIL) {
143         cleanup(0);
144         errx(2, "%s: can't chdir to '%s'", __func__, pen);
145     }
146
147     if (PenLocation[0])
148         pushPen(PenLocation);
149
150     strcpy(PenLocation, pen);
151     return Previous;
152 }
153
154 /* Convenience routine for getting out of playpen */
155 void
156 leave_playpen()
157 {
158     void (*oldsig)(int);
159
160     /* Don't interrupt while we're cleaning up */
161     oldsig = signal(SIGINT, SIG_IGN);
162     if (Previous[0]) {
163         if (chdir(Previous) == FAIL) {
164             cleanup(0);
165             errx(2, "%s: can't chdir back to '%s'", __func__, Previous);
166         }
167         Previous[0] = '\0';
168     }
169     if (PenLocation[0]) {
170         if (PenLocation[0] == '/' && vsystem("/bin/rm -rf %s", PenLocation))
171             warnx("couldn't remove temporary dir '%s'", PenLocation);
172         popPen(PenLocation);
173     }
174     signal(SIGINT, oldsig);
175 }
176
177 off_t
178 min_free(const char *tmpdir)
179 {
180     struct statfs buf;
181
182     if (statfs(tmpdir, &buf) != 0) {
183         warn("statfs");
184         return -1;
185     }
186     return (off_t)buf.f_bavail * (off_t)buf.f_bsize;
187 }