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
1=== modified file 'src/main.c'
2--- src/main.c 2015-05-29 20:20:46 +0000
3+++ src/main.c 2015-06-03 13:12:41 +0000
4@@ -245,17 +245,26 @@
5 return;
6 }
7
8+ // strtok trashes its argument, so to avoid changing the TMPDIR variable,
9+ // we make a copy.
10+ dir = strdup(dir);
11+ if (!dir) {
12+ die("out of memory");
13+ }
14+
15 int n = 4;
16 char buf[MAX_BUF] = "/tmp";
17 char *d = strtok(dir+4, "/");
18 while (d) {
19 n += must_snprintf(buf+n, MAX_BUF-n, "/%s", d);
20- if (mkdir(buf, 01777) < 0) {
21- return;
22+ if (mkdir(buf, 01777) < 0 && errno != EEXIST) {
23+ break;
24 }
25
26 d = strtok(NULL, "/");
27 }
28+
29+ free(dir);
30 }
31
32 int main(int argc, char **argv)
33
34=== added file 'tests/test_tmpdir'
35--- tests/test_tmpdir 1970-01-01 00:00:00 +0000
36+++ tests/test_tmpdir 2015-06-03 13:12:41 +0000
37@@ -0,0 +1,34 @@
38+#!/bin/sh
39+
40+set -e
41+
42+. $(pwd)/common.sh
43+
44+printf "Test that we create TMPDIR and propagate that variable intact"
45+
46+cat >$TMP/echotest <<EOF
47+#!/bin/sh
48+echo \$TMPDIR
49+EOF
50+chmod a+x $TMP/echotest
51+
52+cat >$TMP/unrestricted <<EOF
53+@unrestricted
54+EOF
55+
56+export TMPDIR="$(mktemp -du /tmp/snaps/XXXXXX)/foo/bar"
57+test ! -e "$TMPDIR" # just sanity checking
58+
59+OUTPUT="$($L appname unrestricted $TMP/echotest)"
60+
61+if [ ! -d "$TMPDIR" ]; then
62+ # we didn't create TMPDIR like we should have
63+ FAIL
64+fi
65+
66+if [ "$TMPDIR" != "$OUTPUT" ]; then
67+ # we didn't propagate TMPDIR to the command
68+ FAIL
69+fi
70+
71+PASS

Subscribers

People subscribed via source and target branches