zoolib.c 9.1 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
/*
 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
 * or the like.  Any license provided herein, whether implied or
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 * Mountain View, CA  94043, or:
 *
 * http://www.sgi.com
 *
 * For further information regarding this notice, see:
 *
 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
 *
 */
/* $Id: zoolib.c,v 1.8 2009/06/09 17:59:46 subrata_modak Exp $ */
/*
 * ZooLib
 *
 * A Zoo is a file used to record what test tags are running at the moment.
 * If the system crashes, we should be able to look at the zoo file to find out
 * what was currently running.  This is especially helpful when running multiple
 * tests at the same time.
 *
 * The zoo file is meant to be a text file that fits on a standard console.
 * You should be able to watch it with `cat zoofile`
 *
 * zoo file format:
 * 	80 characters per line, ending with a \n
 * 	available lines start with '#'
 * 	expected line fromat: pid_t,tag,cmdline
 *
 */

#include <signal.h>
#include <stdlib.h>		/* for getenv */
#include <string.h>
#include "zoolib.h"

char zoo_error[ZELEN];

#ifdef __linux__
/* glibc2.2 definition needs -D_XOPEN_SOURCE, which breaks other things. */
extern int sighold(int __sig);
extern int sigrelse(int __sig);
#endif

/* zoo_mark(): private function to make an entry to the zoo
 * 	returns 0 on success, -1 on error */
static int zoo_mark(zoo_t z, char *entry);
static int zoo_lock(zoo_t z);
static int zoo_unlock(zoo_t z);
/* cat_args(): helper function to make cmdline from argc, argv */
char *cat_args(int argc, char **argv);

/* zoo_getname(): create a filename to use for the zoo */
char *zoo_getname(void)
{
	char buf[1024];
	char *zoo;

	zoo = getenv("ZOO");
	if (zoo) {
		snprintf(buf, 1024, "%s/%s", zoo, "active");
		return strdup(buf);
	} else {
		/* if there is no environment variable, we don't know where to put it */
		return NULL;
	}
}

/* zoo_open(): open a zoo for use */
zoo_t zoo_open(char *zooname)
{
	zoo_t new_zoo;

	new_zoo = (zoo_t) fopen(zooname, "r+");
	if (!new_zoo) {
		if (errno == ENOENT) {
			/* file doesn't exist, try fopen(xxx, "a+") */
			new_zoo = (zoo_t) fopen(zooname, "a+");
			if (!new_zoo) {
				/* total failure */
				snprintf(zoo_error, ZELEN,
					 "Could not open zoo as \"%s\", errno:%d %s",
					 zooname, errno, strerror(errno));
				return 0;
			}
			fclose(new_zoo);
			new_zoo = fopen(zooname, "r+");
		} else {
			snprintf(zoo_error, ZELEN,
				 "Could not open zoo as \"%s\", errno:%d %s",
				 zooname, errno, strerror(errno));
		}
	}
	return new_zoo;
}

int zoo_close(zoo_t z)
{
	int ret;

	ret = fclose(z);
	if (ret) {
		snprintf(zoo_error, ZELEN,
			 "closing zoo caused error, errno:%d %s",
			 errno, strerror(errno));
	}
	return ret;
}

static int zoo_mark(zoo_t z, char *entry)
{
	FILE *fp = (FILE *) z;
	int found = 0;
	long pos;
	char buf[BUFLEN];

	if (fp == NULL)
		return -1;

	if (zoo_lock(z))
		return -1;

	/* first fit */
	rewind(fp);

	do {
		pos = ftell(fp);

		if (fgets(buf, BUFLEN, fp) == NULL)
			break;

		if (buf[0] == '#') {
			rewind(fp);
			if (fseek(fp, pos, SEEK_SET)) {
				/* error */
				snprintf(zoo_error, ZELEN,
					 "seek error while writing to zoo file, errno:%d %s",
					 errno, strerror(errno));
				return -1;
			}
			/* write the entry, left justified, and padded/truncated to the
			 * same size as the previous entry */
			fprintf(fp, "%-*.*s\n", (int)strlen(buf) - 1,
				(int)strlen(buf) - 1, entry);
			found = 1;
			break;
		}
	} while (1);

	if (!found) {
		if (fseek(fp, 0, SEEK_END)) {
			snprintf(zoo_error, ZELEN,
				 "error seeking to end of zoo file, errno:%d %s",
				 errno, strerror(errno));
			return -1;
		}
		fprintf(fp, "%-*.*s\n", 79, 79, entry);
	}
	fflush(fp);

	if (zoo_unlock(z))
		return -1;
	return 0;
}

int zoo_mark_cmdline(zoo_t z, pid_t p, char *tag, char *cmdline)
{
	char new_entry[BUFLEN];

	snprintf(new_entry, 80, "%d,%s,%s", p, tag, cmdline);
	return zoo_mark(z, new_entry);
}

int zoo_mark_args(zoo_t z, pid_t p, char *tag, int ac, char **av)
{
	char *cmdline;
	int ret;

	cmdline = cat_args(ac, av);
	ret = zoo_mark_cmdline(z, p, tag, cmdline);

	free(cmdline);
	return ret;
}

int zoo_clear(zoo_t z, pid_t p)
{
	FILE *fp = (FILE *) z;
	long pos;
	char buf[BUFLEN];
	pid_t that_pid;
	int found = 0;

	if (fp == NULL)
		return -1;

	if (zoo_lock(z))
		return -1;
	rewind(fp);

	do {
		pos = ftell(fp);

		if (fgets(buf, BUFLEN, fp) == NULL)
			break;

		if (buf[0] == '#')
			continue;

		that_pid = atoi(buf);
		if (that_pid == p) {
			if (fseek(fp, pos, SEEK_SET)) {
				/* error */
				snprintf(zoo_error, ZELEN,
					 "seek error while writing to zoo file, errno:%d %s",
					 errno, strerror(errno));
				return -1;
			}
			if (ftell(fp) != pos) {
				printf("fseek failed\n");
			}
			fputs("#", fp);
			found = 1;
			break;
		}
	} while (1);

	fflush(fp);

	/* FIXME: unlock zoo file */
	if (zoo_unlock(z))
		return -1;

	if (!found) {
		snprintf(zoo_error, ZELEN,
			 "zoo_clear() did not find pid(%d)", p);
		return 1;
	}
	return 0;

}

pid_t zoo_getpid(zoo_t z, char *tag)
{
	FILE *fp = (FILE *) z;
	char buf[BUFLEN], *s;
	pid_t this_pid = -1;

	if (fp == NULL)
		return -1;

	if (zoo_lock(z))
		return -1;

	rewind(fp);
	do {
		if (fgets(buf, BUFLEN, fp) == NULL)
			break;

		if (buf[0] == '#')
			continue;	/* recycled line */

		if ((s = strchr(buf, ',')) == NULL)
			continue;	/* line was not expected format */

		if (strncmp(s + 1, tag, strlen(tag)))
			continue;	/* tag does not match */

		this_pid = atoi(buf);
		break;
	} while (1);

	if (zoo_unlock(z))
		return -1;
	return this_pid;
}

int zoo_lock(zoo_t z)
{
	FILE *fp = (FILE *) z;
	struct flock zlock;
	sigset_t block_these;
	int ret;

	if (fp == NULL)
		return -1;

	zlock.l_whence = zlock.l_start = zlock.l_len = 0;
	zlock.l_type = F_WRLCK;

	sigemptyset(&block_these);
	sigaddset(&block_these, SIGINT);
	sigaddset(&block_these, SIGTERM);
	sigaddset(&block_these, SIGHUP);
	sigaddset(&block_these, SIGUSR1);
	sigaddset(&block_these, SIGUSR2);
	sigprocmask(SIG_BLOCK, &block_these, NULL);

	do {
		ret = fcntl(fileno(fp), F_SETLKW, &zlock);
	} while (ret == -1 && errno == EINTR);

	sigprocmask(SIG_UNBLOCK, &block_these, NULL);
	if (ret == -1) {
		snprintf(zoo_error, ZELEN,
			 "failed to unlock zoo file, errno:%d %s",
			 errno, strerror(errno));
		return -1;
	}
	return 0;

}

int zoo_unlock(zoo_t z)
{
	FILE *fp = (FILE *) z;
	struct flock zlock;
	sigset_t block_these;
	int ret;

	if (fp == NULL)
		return -1;

	zlock.l_whence = zlock.l_start = zlock.l_len = 0;
	zlock.l_type = F_UNLCK;

	sigemptyset(&block_these);
	sigaddset(&block_these, SIGINT);
	sigaddset(&block_these, SIGTERM);
	sigaddset(&block_these, SIGHUP);
	sigaddset(&block_these, SIGUSR1);
	sigaddset(&block_these, SIGUSR2);
	sigprocmask(SIG_BLOCK, &block_these, NULL);

	do {
		ret = fcntl(fileno(fp), F_SETLKW, &zlock);
	} while (ret == -1 && errno == EINTR);

	sigprocmask(SIG_UNBLOCK, &block_these, NULL);

	if (ret == -1) {
		snprintf(zoo_error, ZELEN,
			 "failed to lock zoo file, errno:%d %s",
			 errno, strerror(errno));
		return -1;
	}
	return 0;
}

char *cat_args(int argc, char **argv)
{
	int a, size;
	char *cmd;

	for (size = a = 0; a < argc; a++) {
		size += strlen(argv[a]);
		size++;
	}

	if ((cmd = malloc(size)) == NULL) {
		snprintf(zoo_error, ZELEN,
			 "Malloc Error, %s/%d", __FILE__, __LINE__);
		return NULL;
	}

	*cmd = '\0';
	for (a = 0; a < argc; a++) {
		if (a != 0)
			strcat(cmd, " ");
		strcat(cmd, argv[a]);
	}

	return cmd;
}

#if defined(UNIT_TEST)

void zt_add(zoo_t z, int n)
{
	char cmdline[200];
	char tag[10];

	snprintf(tag, 10, "%s%d", "test", n);
	snprintf(cmdline, 200, "%s%d %s %s %s", "runtest", n, "one", "two",
		 "three");

	zoo_mark_cmdline(z, n, tag, cmdline);
}

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

	char *zooname;
	zoo_t test_zoo;
	char *test_tag = "unittest";
	int i, j;

	zooname = zoo_getname();

	if (!zooname) {
		zooname = strdup("test_zoo");
	}
	printf("Test zoo filename is %s\n", zooname);

	if ((test_zoo = zoo_open(zooname)) == NULL) {
		printf("Error opennning zoo\n");
		exit(-1);
	}

	zoo_mark_args(test_zoo, getpid(), test_tag, argc, argv);

	for (j = 0; j < 5; j++) {
		for (i = 0; i < 20; i++) {
			zt_add(test_zoo, i);
		}

		for (; i >= 0; i--) {
			zoo_clear(test_zoo, i);
		}
	}

	zoo_clear(test_zoo, getpid());

	return 0;
}

#endif