2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
17 * Routines for managing the "play pen".
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
27 #include <sys/signal.h>
28 #include <sys/param.h>
29 #include <sys/mount.h>
31 /* For keeping track of where we are */
32 static char PenLocation[FILENAME_MAX];
33 static char Previous[FILENAME_MAX];
41 /* Find a good place to play. */
43 find_play_pen(char *pen, off_t sz)
48 if (pen[0] && isdir(dirname(pen)) == TRUE && (min_free(dirname(pen)) >= sz))
50 else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
51 sprintf(pen, "%s/instmp.XXXXXX", cp);
52 else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
53 sprintf(pen, "%s/instmp.XXXXXX", cp);
54 else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz)
55 strcpy(pen, "/var/tmp/instmp.XXXXXX");
56 else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz)
57 strcpy(pen, "/tmp/instmp.XXXXXX");
58 else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz)
59 strcpy(pen, "/usr/tmp/instmp.XXXXXX");
63 "%s: can't find enough temporary space to extract the files, please set your\n"
64 "PKG_TMPDIR environment variable to a location with at least %ld bytes\n"
65 "free", __func__, (long)sz);
72 static char *pstack[MAX_STACK];
73 static int pdepth = -1;
76 pushPen(const char *pen)
78 if (++pdepth == MAX_STACK)
79 errx(2, "%s: stack overflow.\n", __func__);
80 pstack[pdepth] = strdup(pen);
90 strcpy(pen, pstack[pdepth]);
91 free(pstack[pdepth--]);
95 * Make a temporary directory to play in and chdir() to it, returning
96 * pathname of previous working directory.
99 make_playpen(char *pen, off_t sz)
101 if (!find_play_pen(pen, sz))
106 errx(2, "%s: can't mktemp '%s'", __func__, pen);
108 if (chmod(pen, 0700) == FAIL) {
110 errx(2, "%s: can't mkdir '%s'", __func__, pen);
115 fprintf(stderr, "Requested space: %d bytes, free space: %lld bytes in %s\n", (int)sz, (long long)min_free(pen), pen);
118 if (min_free(pen) < sz) {
121 errx(2, "%s: not enough free space to create '%s'.\n"
122 "Please set your PKG_TMPDIR environment variable to a location\n"
123 "with more space and\ntry the command again", __func__, pen);
126 if (!getcwd(Previous, FILENAME_MAX)) {
131 if (chdir(pen) == FAIL) {
133 errx(2, "%s: can't chdir to '%s'", __func__, pen);
137 pushPen(PenLocation);
139 strcpy(PenLocation, pen);
143 /* Convenience routine for getting out of playpen */
149 /* Don't interrupt while we're cleaning up */
150 oldsig = signal(SIGINT, SIG_IGN);
152 if (chdir(Previous) == FAIL) {
154 errx(2, "%s: can't chdir back to '%s'", __func__, Previous);
158 if (PenLocation[0]) {
159 if (PenLocation[0] == '/' && vsystem("/bin/rm -rf %s", PenLocation))
160 warnx("couldn't remove temporary dir '%s'", PenLocation);
163 signal(SIGINT, oldsig);
167 min_free(const char *tmpdir)
171 if (statfs(tmpdir, &buf) != 0) {
175 return (off_t)buf.f_bavail * (off_t)buf.f_bsize;