Merge lp:~mterry/snap-confine/tmpdir into lp:~snappy-dev/snap-confine/trunk

Proposed by Michael Terry
Status: Merged
Approved by: John Lenton
Approved revision: 70
Merged at revision: 68
Proposed branch: lp:~mterry/snap-confine/tmpdir
Merge into: lp:~snappy-dev/snap-confine/trunk
Prerequisite: lp:~mterry/snap-confine/fix-tests
Diff against target: 71 lines (+45/-2)
2 files modified
src/main.c (+11/-2)
tests/test_tmpdir (+34/-0)
To merge this branch: bzr merge lp:~mterry/snap-confine/tmpdir
Reviewer Review Type Date Requested Status
John Lenton (community) Approve
Review via email: mp+260620@code.launchpad.net

Commit message

Fix propagation of TMPDIR from the launcher to the command being run.

Description of the change

Fix propagation of TMPDIR from the launcher to the command being run.

Our logic for making sure that TMPDIR exists iterated over the return value of getenv("TMPDIR") with strtok. But strtok modifies its argument. And getenv returns the real environment memory used, so modifications there actually change your environment.

The end result is that we were emptying TMPDIR while making it.

I've also fixed a bug that caused us to stop trying to make TMPDIR if any piece of it (above /tmp) already existed (like, /tmp/snaps/!).

And of course, a test.

To post a comment you must log in.
Revision history for this message
John Lenton (chipaca) :
review: Approve
lp:~mterry/snap-confine/tmpdir updated
69. By Michael Terry

Check return of strdup, add comment about why we do it

70. By Michael Terry

Make it die instead of just returning

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/main.c'
--- src/main.c 2015-05-29 20:20:46 +0000
+++ src/main.c 2015-06-03 13:12:41 +0000
@@ -245,17 +245,26 @@
245 return;245 return;
246 }246 }
247247
248 // strtok trashes its argument, so to avoid changing the TMPDIR variable,
249 // we make a copy.
250 dir = strdup(dir);
251 if (!dir) {
252 die("out of memory");
253 }
254
248 int n = 4;255 int n = 4;
249 char buf[MAX_BUF] = "/tmp";256 char buf[MAX_BUF] = "/tmp";
250 char *d = strtok(dir+4, "/");257 char *d = strtok(dir+4, "/");
251 while (d) {258 while (d) {
252 n += must_snprintf(buf+n, MAX_BUF-n, "/%s", d);259 n += must_snprintf(buf+n, MAX_BUF-n, "/%s", d);
253 if (mkdir(buf, 01777) < 0) {260 if (mkdir(buf, 01777) < 0 && errno != EEXIST) {
254 return;261 break;
255 }262 }
256263
257 d = strtok(NULL, "/");264 d = strtok(NULL, "/");
258 }265 }
266
267 free(dir);
259}268}
260269
261int main(int argc, char **argv)270int main(int argc, char **argv)
262271
=== added file 'tests/test_tmpdir'
--- tests/test_tmpdir 1970-01-01 00:00:00 +0000
+++ tests/test_tmpdir 2015-06-03 13:12:41 +0000
@@ -0,0 +1,34 @@
1#!/bin/sh
2
3set -e
4
5. $(pwd)/common.sh
6
7printf "Test that we create TMPDIR and propagate that variable intact"
8
9cat >$TMP/echotest <<EOF
10#!/bin/sh
11echo \$TMPDIR
12EOF
13chmod a+x $TMP/echotest
14
15cat >$TMP/unrestricted <<EOF
16@unrestricted
17EOF
18
19export TMPDIR="$(mktemp -du /tmp/snaps/XXXXXX)/foo/bar"
20test ! -e "$TMPDIR" # just sanity checking
21
22OUTPUT="$($L appname unrestricted $TMP/echotest)"
23
24if [ ! -d "$TMPDIR" ]; then
25 # we didn't create TMPDIR like we should have
26 FAIL
27fi
28
29if [ "$TMPDIR" != "$OUTPUT" ]; then
30 # we didn't propagate TMPDIR to the command
31 FAIL
32fi
33
34PASS

Subscribers

People subscribed via source and target branches