~jamesodhunt/upstart/make-cgroups-quiet-in-debug-mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* upstart
 *
 * test_util_check_env.c - meta test to ensure environment sane for
 * running tests.
 *
 * Copyright © 2013 Canonical Ltd.
 * Author: James Hunt <james.hunt@canonical.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <limits.h>
#include <unistd.h>
#include <mntent.h>
#include <sys/vfs.h>

#include <nih/string.h>
#include <nih/test.h>
#include <nih/logging.h>

#include "test_util_common.h"

#ifndef OVERLAYFS_SUPER_MAGIC
#define OVERLAYFS_SUPER_MAGIC 0x794c764f
#endif

/**
 * check_for_overlayfs:
 *
 * Determine if the mount point used by the tests for creating temporary
 * files is using overlayfs.
 *
 * Returns: TRUE if temporary work area is on overlayfs, else FALSE.
 **/
int
check_for_overlayfs (void)
{
	struct statfs  statbuf;
	char           path[PATH_MAX];
	int            found = FALSE;

	/* Create a file in the temporary work area */
	TEST_FILENAME (path);
	fclose (fopen (path, "w"));

	/* Check it exits */
	assert0 (statfs (path, &statbuf));

	if (statbuf.f_type == OVERLAYFS_SUPER_MAGIC) {
		nih_warn ("Mountpoint for '%s' (needed by the Upstart tests) is an overlayfs "
				"filesystem, which does not support inotify.",
				path);
		found = TRUE;
	}

	assert0 (unlink (path));

	return found;
}

#ifdef ENABLE_CGROUPS

void print_my_cgroup(void)
{
	char *str;
	str = get_pid_cgroup("freezer", getpid());
	if (str) {
		nih_warn("I am in freezer cgroup: %s", str);
		TEST_EQ_STR(str, "/");
		nih_free(str);
	} else {
		TEST_FAILED("Failed to get my freezer cgroup");
	}
}

char *get_my_cgroup()
{
	char line[1024], *ret = NULL;
	FILE *f = fopen("/proc/self/cgroup", "r");

	while (fgets(line, 1024, f)) {
		char *p, *p2;
		if ((p = strchr(line, ':')) == NULL)
			continue;
		p++;
		if ((p2 = strchr(p, ':')) == NULL)
			continue;
		if (strncmp(p, "name=", 5) == 0)
			continue;
		ret = NIH_MUST( nih_strdup(NULL, p2+1) );
		break;
	}
	fclose(f);
	return ret;
}

int check_cgroup_sandbox(void)
{
	char *cg_prev = NULL, *cg_post = NULL;
	int ret = -1;

	cg_prev = get_my_cgroup();
	if (!cg_prev)
		return -1;
	if (setup_cgroup_sandbox() < 0) {
		nih_free(cg_prev);
		return -1;
	}
	cg_post = get_my_cgroup();
	if (!cg_post) {
		nih_free(cg_prev);
		return -1;
	}
	/* we should have moved cgroups, so the two should be different */
	if (strcmp(cg_prev, cg_post) != 0) {
		nih_warn("setup_cgroup_sandbox moved me from %s to %s",
				cg_prev, cg_post);
		ret = 0;
	}
	nih_free(cg_prev);
	nih_free(cg_post);
	return ret;
}

#endif /* ENABLE_CGROUPS */

/**
 * test_checks:
 *
 * Perform any checks necessary before real tests are run.
 **/
void
test_checks (void)
{
	int ret;

	TEST_GROUP ("test environment");

	/*
	 * Warn (*) if overlayfs detected.
	 *
	 * (*) - Don't fail in the hope that one day someone might fix
	 * overlayfs.
	 */
	TEST_FEATURE ("checking for overlayfs");
	if (check_for_overlayfs ()) {
		nih_warn ("Found overlayfs mounts");
		nih_warn ("This environment will probably cause tests to fail mysteriously!!");
		nih_warn ("See bug LP:#882147 for further details.");
	}


#ifdef ENABLE_CGROUPS
	if (file_exists ("/sys/fs/cgroup/cgmanager/sock")) {
		TEST_FEATURE ("checking for cgmanager");
		ret = connect_to_cgmanager ();
		switch (ret) {
		case -2:
			nih_warn ("Found no cgroup manager");
			goto out_skip;
		case -1:
			nih_warn ("Error connecting to cgmanager");
			goto out_skip;
		case 0:
			print_my_cgroup ();
			break;
		default: nih_warn ("Unknown error from connect_to_cgmanager: %d", ret);
			goto out_skip;
		}

		TEST_FEATURE ("cgroup sandbox");
		if (check_cgroup_sandbox() != 0)
			nih_warn ("Could not create cgroup sandbox");
	} else {
		nih_warn ("Skipping CGManager tests, CGManager socket not found");
	}
out_skip:
	disconnect_cgmanager();
	if (ret)
		nih_warn ("Skipping CGManager tests, CGManager not properly configured");
#endif /* ENABLE_CGROUPS */

}

int
main (int   argc,
      char *argv[])
{
	test_checks ();

	return 0;
}