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".
19 * $FreeBSD: src/usr.sbin/pkg_install/lib/pen.c,v 1.31.2.7 2002/08/20 06:35:08 obrien Exp $
20 * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/pen.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
26 #include <sys/signal.h>
27 #include <sys/param.h>
28 #include <sys/mount.h>
30 /* For keeping track of where we are */
31 static char PenLocation[FILENAME_MAX];
32 static char Previous[FILENAME_MAX];
40 /* Find a good place to play. */
42 find_play_pen(char *pen, off_t sz)
47 if (pen[0] && isdir(dirname(pen)) == TRUE && (min_free(dirname(pen)) >= sz))
49 else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
50 sprintf(pen, "%s/instmp.XXXXXX", cp);
51 else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
52 sprintf(pen, "%s/instmp.XXXXXX", cp);
53 else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz)
54 strcpy(pen, "/var/tmp/instmp.XXXXXX");
55 else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz)
56 strcpy(pen, "/tmp/instmp.XXXXXX");
57 else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz)
58 strcpy(pen, "/usr/tmp/instmp.XXXXXX");
62 "%s: can't find enough temporary space to extract the files, please set your\n"
63 "PKG_TMPDIR environment variable to a location with at least %ld bytes\n"
64 "free", __func__, (long)sz);
71 static char *pstack[MAX_STACK];
72 static int pdepth = -1;
75 pushPen(const char *pen)
77 if (++pdepth == MAX_STACK)
78 errx(2, "%s: stack overflow.\n", __func__);
79 pstack[pdepth] = strdup(pen);
89 strcpy(pen, pstack[pdepth]);
90 free(pstack[pdepth--]);
94 * Make a temporary directory to play in and chdir() to it, returning
95 * pathname of previous working directory.
98 make_playpen(char *pen, off_t sz)
100 if (!find_play_pen(pen, sz))
105 errx(2, "%s: can't mktemp '%s'", __func__, pen);
107 if (chmod(pen, 0700) == FAIL) {
109 errx(2, "%s: can't mkdir '%s'", __func__, pen);
114 fprintf(stderr, "Requested space: %d bytes, free space: %qd bytes in %s\n", (int)sz, (long long)min_free(pen), pen);
117 if (min_free(pen) < sz) {
120 errx(2, "%s: not enough free space to create '%s'.\n"
121 "Please set your PKG_TMPDIR environment variable to a location\n"
122 "with more space and\ntry the command again", __func__, pen);
125 if (!getcwd(Previous, FILENAME_MAX)) {
130 if (chdir(pen) == FAIL) {
132 errx(2, "%s: can't chdir to '%s'", __func__, pen);
136 pushPen(PenLocation);
138 strcpy(PenLocation, pen);
142 /* Convenience routine for getting out of playpen */
148 /* Don't interrupt while we're cleaning up */
149 oldsig = signal(SIGINT, SIG_IGN);
151 if (chdir(Previous) == FAIL) {
153 errx(2, "%s: can't chdir back to '%s'", __func__, Previous);
157 if (PenLocation[0]) {
158 if (PenLocation[0] == '/' && vsystem("rm -rf %s", PenLocation))
159 warnx("couldn't remove temporary dir '%s'", PenLocation);
162 signal(SIGINT, oldsig);
166 min_free(const char *tmpdir)
170 if (statfs(tmpdir, &buf) != 0) {
174 return (off_t)buf.f_bavail * (off_t)buf.f_bsize;