utstest.c 7.9 KB
Newer Older
M
m00302376 已提交
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/*
* Copyright (c) International Business Machines Corp., 2007
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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
*
***************************************************************************
 * Copyright 2007 IBM
 * Author: Serge Hallyn <serue@us.ibm.com>
 *
 * test1:
	P1: A=gethostname
	P2: B=gethostname
	Ensure(A==B)

 * test2:
	P1: sethostname(A);
	P2: (wait); B=gethostname
	Ensure (A==B)

 * test3:
	P1: A=gethostname; unshare(utsname); sethostname(newname); C=gethostname
	P2: B=gethostname; (wait); (wait); D=gethostname
	Ensure (A==B && A==D && C!=D)

 * test4:
	P1: A=gethostname; unshare(utsname); (wait); C=gethostname
	P2: B=gethostname; (wait); sethostname(newname); D=gethostname
	Ensure (A==B && A==C && C!=D)

 * test5:
	P1: drop_privs(); unshare(utsname); (wait); C=gethostname
	P2: (wait); sethostname(B); D=gethostname
	Ensure (B==C==D) and state is ok.
 *
 */

#define _GNU_SOURCE 1
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "libclone.h"
#include "test.h"
#include "safe_macros.h"

char *TCID = "uts_namespace";
int TST_TOTAL = 1;

static int dummy_child(void *v)
{
	(void) v;
	return 0;
}

static void check_newuts(void)
{
	int pid, status;

	if (tst_kvercmp(2, 6, 19) < 0)
		tst_brkm(TCONF, NULL, "CLONE_NEWUTS not supported");

	pid = do_clone_unshare_test(T_CLONE, CLONE_NEWUTS, dummy_child, NULL);
	if (pid == -1)
		tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWUTS not supported");

	SAFE_WAIT(NULL, &status);
}

int drop_root(void)
{
	int ret;
	ret = setresuid(1000, 1000, 1000);
	if (ret) {
		perror("setresuid");
		exit(4);
	}
	return 1;
}

#define HLEN 100
#define NAME1 "serge1"
#define NAME2 "serge2"

int p1fd[2], p2fd[2];
static char oldhost[HLEN];
pid_t cpid;

void picknewhostname(char *orig, char *new)
{
	memset(new, 0, HLEN);
	if (strcmp(orig, NAME1) == 0)
		strcpy(new, NAME2);
	else
		strcpy(new, NAME1);
}

void zeroize(char *s)
{
	memset(s, 0, HLEN);
}

char *tsttype;
int P1(void *vtest)
{
	char hostname[HLEN], newhostname[HLEN], rhostname[HLEN];
	int err;
	int len;
	int testnum;

	testnum = atoi((char *)vtest);

	close(p1fd[1]);
	close(p2fd[0]);

	switch (testnum) {
	case 1:
		gethostname(hostname, HLEN);
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		if (strcmp(hostname, rhostname) == 0) {
			tst_resm(TPASS, "test 1 (%s): success", tsttype);
			tst_exit();
		}
		tst_brkm(TFAIL, NULL,
			 "test 1 (%s): hostname 1 %s, hostname 2 %s",
			 tsttype, hostname, rhostname);
	case 2:
		gethostname(hostname, HLEN);
		picknewhostname(hostname, newhostname);
		err = sethostname(newhostname, strlen(newhostname));
		write(p2fd[1], "1", 1);
		if (err == -1) {
			tst_brkm(TFAIL, NULL,
				 "test 2 (%s): failed to sethostname",
				 tsttype);
		}
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		if (strcmp(newhostname, rhostname) == 0) {
			tst_resm(TPASS, "test 2 (%s): success", tsttype);
			tst_exit();
		}
		tst_brkm(TFAIL, NULL,
			 "test 2 (%s) hostname 1 %s, hostname 2 %s",
			 tsttype, newhostname, rhostname);
	case 3:
		gethostname(hostname, HLEN);
		picknewhostname(hostname, newhostname);
		err = sethostname(newhostname, strlen(newhostname));
		write(p2fd[1], "1", 1);
		if (err == -1) {
			tst_brkm(TFAIL, NULL,
				 "test 3 (%s): failed to sethostname",
				 tsttype);
		}

		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		if (strcmp(newhostname, rhostname) == 0) {
			tst_brkm(TFAIL,
				 NULL,
				 "test 3 (%s): hostname 1 %s, hostname 2 %s, these should have been different",
				 tsttype, newhostname, rhostname);
		}
		if (strcmp(hostname, rhostname) == 0) {
			tst_resm(TPASS, "test 3 (%s): success", tsttype);
			tst_exit();
		}
		tst_brkm(TFAIL,
			 NULL,
			 "test 3 (%s): hostname 1 %s, hostname 2 %s, should have been same",
			 tsttype, hostname, rhostname);

	case 4:
		gethostname(hostname, HLEN);
		write(p2fd[1], "1", 1);	/* tell p2 to go ahead and sethostname */
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		gethostname(newhostname, HLEN);
		if (strcmp(hostname, newhostname) != 0) {
			tst_brkm(TFAIL,
				 NULL,
				 "test 4 (%s): hostname 1 %s, hostname 2 %s, should be same",
				 tsttype, hostname, newhostname);
		}
		if (strcmp(hostname, rhostname) == 0) {
			tst_brkm(TFAIL,
				 NULL,
				 "test 4 (%s): hostname 1 %s, hostname 2 %s, should be different",
				 tsttype, hostname, rhostname);
		}
		tst_resm(TPASS, "test 4 (%s): successful", tsttype);
		tst_exit();
	case 5:
		write(p2fd[1], "1", 1);	/* tell p2 to go ahead and sethostname */
		zeroize(rhostname);
		len = read(p1fd[0], rhostname, HLEN);
		gethostname(newhostname, HLEN);
		if (strcmp(rhostname, newhostname) != 0) {
			tst_brkm(TFAIL,
				 NULL,
				 "test 5 (%s): hostnames %s and %s should be same",
				 tsttype, rhostname, newhostname);
		}
		tst_resm(TPASS, "test 5 (%s): successful", tsttype);
		tst_exit();
	default:
		break;
	}
	tst_exit();
}

int P2(void *vtest)
{
	char hostname[HLEN], newhostname[HLEN];
	int len;
	int testnum;

	testnum = atoi((char *)vtest);

	close(p1fd[0]);
	close(p2fd[1]);

	switch (testnum) {
	case 1:
		gethostname(hostname, HLEN);
		write(p1fd[1], hostname, strlen(hostname));
		break;
	case 2:
	case 3:
		len = 0;
		while (!len) {
			len = read(p2fd[0], hostname, 1);
		}
		gethostname(hostname, HLEN);
		write(p1fd[1], hostname, strlen(hostname));
		break;
	case 4:
	case 5:
		len = 0;
		while (!len) {
			len = read(p2fd[0], hostname, 1);
		}
		if (hostname[0] == '0') {
			tst_resm(TPASS, "P2: P1 claims error");
			return 0;
		}
		gethostname(hostname, HLEN);
		picknewhostname(hostname, newhostname);
		sethostname(newhostname, strlen(newhostname));
		write(p1fd[1], newhostname, strlen(newhostname));
		break;
	default:
		tst_resm(TFAIL, "undefined test: %d", testnum);
		break;
	}
	return 0;
}

static void setup(void)
{
	gethostname(oldhost, HLEN);
	tst_require_root();
	check_newuts();
}

static void cleanup(void)
{
	sethostname(oldhost, strlen(oldhost));
}

#define UNSHARESTR "unshare"
#define CLONESTR "clone"
int main(int argc, char *argv[])
{
	int r, pid, use_clone = T_UNSHARE;
	int testnum;
	void *vtest;

	setup();
	if (argc != 3) {
		tst_resm(TFAIL, "Usage: %s <clone|unshare> <testnum>",
			 argv[0]);
		tst_resm(TFAIL,
			 " where clone or unshare specifies unshare method,");
		tst_resm(TFAIL, " and testnum is between 1 and 5 inclusive");
		exit(2);
	}
	if (pipe(p1fd) == -1) {
		perror("pipe");
		exit(EXIT_FAILURE);
	}
	if (pipe(p2fd) == -1) {
		perror("pipe");
		exit(EXIT_FAILURE);
	}

	tsttype = UNSHARESTR;
	if (strcmp(argv[1], "clone") == 0) {
		use_clone = T_CLONE;
		tsttype = CLONESTR;
	}

	testnum = atoi(argv[2]);

	vtest = (void *)argv[2];
	switch (testnum) {
	case 1:
	case 2:
		r = do_clone_unshare_tests(T_NONE, 0, P1, vtest, P2, vtest);
		break;
	case 3:
	case 4:
		r = do_clone_unshare_tests(use_clone, CLONE_NEWUTS,
					   P1, vtest, P2, vtest);
		break;
	case 5:
		pid = fork();
		if (pid == -1) {
			perror("fork");
			exit(2);
		}
		if (pid == 0) {
			if (!drop_root()) {
				tst_brkm(TFAIL, NULL, "failed to drop root.");
			}
			r = do_clone_unshare_test(use_clone, CLONE_NEWUTS,
						  P1, vtest);
			write(p2fd[1], "0", 1);	/* don't let p2 hang */
			exit(0);
		} else {
			P2(vtest);
		}
		break;
	default:
		tst_resm(TFAIL,
			 "testnum should be between 1 and 5 inclusive.");
		break;
	}

	cleanup();
	tst_exit();
}