Esempio n. 1
0
int
ShelloutSimple(int verbose, struct cfg *cfg, char *args[])
{
	struct sho *sh;
	char buf[256];
	const unsigned char *pv[] = { "sh> " };
	int n, i;
	cfg = NULL;

	sh = sho_open("sh", "sh> ", 0);

	n = sprintf(buf, "%s\n", args[0]); /* add newline */
	writen(sh->ptym, buf, n);
	i = sho_expect(sh, pv, 1, buf, 256, 10);
	if (i == 1) {
		tcase_printf(verbose, "success\n");
	} else if (i == -1) {
		perror("timeout");
	} else if (i == 0) {
		tcase_printf(verbose, "EOF\n");
	}

	n = sprintf(buf, "exit $?\n");
	writen(sh->ptym, buf, n);

	sho_close(sh);

	return i == 1 ? 0 : -1;
}
Esempio n. 2
0
int
PoolExercise(int verbose, struct cfg *cfg, char *args[])
{
    void *data;
    int rate, i;
    struct linkedlist *l;
    struct pool *p;
    cfg = NULL;
    args[0] = NULL;

    if ((p = pool_new(EXERCISE_SM_COUNT,
                      (new_fn)allocator_alloc,
                      allocator_free,
                      NULL,
                      NULL, BUFFER_SIZE_SM, 0, NULL)) == NULL ||
            (l = linkedlist_new(0, NULL)) == NULL) {
        PMNO(errno);
        return 1;
    }

    rate = EXERCISE_R0;
    for (i = 0; i < EXERCISE_SM_COUNT; i++) {
        if (i == EXERCISE_SM_P1) {
            rate = EXERCISE_R1;
        } else if (i == EXERCISE_SM_P2) {
            rate = EXERCISE_R2;
        } else if (i == EXERCISE_SM_P3) {
            rate = EXERCISE_R3;
        }

        if (rand() % 10 < rate) {
            if (pool_size(p) == EXERCISE_SM_COUNT && pool_unused(p) == 0) {
                continue;
            } else if ((data = pool_get(p)) == NULL) {
                AMSG("");
                return -1;
            } else if (linkedlist_add(l, data) == -1) {
                AMSG("%04d %p s=%d,u=%d\n", i, data, pool_size(p), pool_unused(p));
                return -1;
            }
            tcase_printf(verbose, "%04d get %p s=%d,u=%d\n", i, data, pool_size(p), pool_unused(p));
        } else if ((data = linkedlist_remove(l, 0))) {
            if (data == NULL || pool_release(p, data) == -1) {
                AMSG("%04d %p s=%d,u=%d\n", i, data, pool_size(p), pool_unused(p));
                return -1;
            }
            tcase_printf(verbose, "%04d rel %p s=%d,u=%d\n", i, data, pool_size(p), pool_unused(p));
        } else {
            tcase_printf(verbose, "%04d nothing to release\n", i);
        }
    }

    linkedlist_del(l, NULL, NULL);
    pool_del(p);

    return 0;
}
Esempio n. 3
0
int
CfgQueryStringExamples(int verbose, struct cfg *cfg, char *args[])
{
	unsigned char buf[1024], *row[10];
	FILE *in;
	int ret;

	if ((in = fopen(args[0], "r")) == NULL) {
		PMNO(errno);
		return -1;
	}

	while ((ret = csv_row_fread(in, buf, 1024, row, 10, ',', CSV_QUOTES | CSV_TRIM)) > 0) {
    	int success = atoi(row[0]);

		tcase_printf(verbose, "%s:\n", row[1]);

		cfg_clear(cfg);

		if (cfg_load_cgi_query_string(cfg, row[1], row[1] + tcslen(row[1])) == -1) {
			if (success) {
				ret = -1;
				AMSG("");
				goto out;
			}
		} else if (!success) {
			ret = -1;
			AMSG("Supposed to fail");
			goto out;
		}

		if (verbose) {
			iter_t iter;
			const tchar *name;
			tchar dst[512];

			cfg_iterate(cfg, &iter);
			while ((name = cfg_next(cfg, &iter))) {
				if (cfg_get_str(cfg, dst, 512, NULL, name) == -1) {
					errno = ENOENT;
					PMNO(errno);
					return -1;
				}
				tcase_printf(verbose, "\t%s=%s\n", name, dst);
			}
		}
	}

out:
	fclose(in);

    return ret;
}
Esempio n. 4
0
int
OpenCre(int verbose, struct cfg *cfg, char *args[])
{
	int nprocs = atoi(args[0]), n = atoi(args[1]), i, j, status;
	pid_t *pids;
	svsem_t sem;

	if (svsem_create(&sem, 0, 1) == -1) {
		AMSG("");
		return -1;
	}

	if ((pids = malloc(nprocs * sizeof *pids)) == NULL) {
		PMNO(errno);
		return -1;
	}

	for (i = 0; i < nprocs; i++) {
		if ((pids[i] = fork()) == 0) {
			run(&sem, n, verbose);
			exit(0);
		}
	}
	for (j = 0; j < n; j++) {
		usleep(200);
		if (verbose) {
			printf("%d\n", j);
		}
		svsem_post_multiple(&sem, nprocs);
	}
	do {
		waitpid(pids[--i], &status, 0);
		if ((errno = WEXITSTATUS(status))) {
			perror("");
		}
		tcase_printf(verbose, "process complete\n");
	} while(i);

	free(pids);
	svsem_destroy(&sem);

	tcase_printf(verbose, "done");
	cfg = NULL;
    return 0;
}
Esempio n. 5
0
int
AdtInit(int verbose, struct cfg *cfg, char *args[])
{
	char buf[0xFFFF];
	struct allocator *suba;
	struct stack s;
	struct linkedlist l;
	struct varray va;

	if ((suba = suba_init(buf, 0xFFFF, 1, 0)) == NULL ||
			stack_init(&s, 0, suba) == -1 ||
			linkedlist_init(&l, 0, suba) == -1 ||
			varray_init(&va, sizeof(int), suba) == -1) {
		AMSG("");
		return -1;
	}

	linkedlist_add(&l, "two");
	stack_push(&s, "two");
	linkedlist_add(&l, "three");
	stack_push(&s, "one");
	varray_get(&va, 444);
	stack_push(&s, "three");
	varray_get(&va, 4);
	varray_get(&va, 44);
	linkedlist_add(&l, "one");

	if (varray_deinit(&va) != 0 ||
			linkedlist_deinit(&l, NULL, NULL) != 0 ||
			stack_deinit(&s, NULL, NULL) != 0) {
		AMSG("");
		return -1;
	}

	tcase_printf(verbose, "done ");

	cfg = NULL;
	args[0] = NULL;
    return 0;
}
Esempio n. 6
0
int
HashmapExercise(int verbose, struct cfg *cfg, char *args[])
{
	struct hashmap *h;
    int rate = 0, i, n = 0;
	int count = atoi(args[0]);
	int interval = atoi(args[1]);
	iter_t riter;
	iter_t iter;
	char *str;
	struct allocator *al = NULL;
	char *mem;
	clock_t t0;
	int chkpnt = 0;
	struct allocator *hal;

	if ((mem = malloc(0xFFFFFF)) == NULL ||
			(al = suba_init(mem, 0xFFFFFF, 1, 0)) == NULL ||
			(h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) {
		AMSG("");
		return -1;
	}
	hal = AL(h);

/*
	if ((h = hashmap_new(hash_str, cmp_str, NULL, al)) == NULL) {
		AMSG("");
		return -1;
	}
*/
srand(0);

	t0 = clock();
if (verbose)
fprintf(stderr, "\n       time    count     size      mem\n");

	hashmap_iterate(h, &iter);
	rate_iterate(&riter);
	for (i = 0; i < count; i++) {
		if ((i % interval) == 0) {
if (verbose)
fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total);
			rate = rate_next(&riter);
		}

		if (rand() % 10 < rate) {
	        str = allocator_alloc(NULL, 8, 0);
	        sprintf(str, "%07d", n++);
	        if (hashmap_put(h, str, str) == -1) {
				AMSG("");
				return -1;
			} else {
/*fputc('+', stderr);
*/
	 	      	tcase_printf(verbose, "put %s %d\r", str, hashmap_size(h));
			}
		} else {
			if (hashmap_is_empty(h)) {
/*fputc('0', stderr);
*/
				tcase_printf(verbose, "hashmap empty\r");
			} else {
		        str = hashmap_next(h, &iter);
				if (str) {
					char *data;

	    	    	tcase_printf(verbose, "get %s %d\r", str, hashmap_size(h));
					if (hashmap_remove(h, (void **)&str, (void **)&data) == -1) {
						AMSG("");
						return -1;
					}
/*fputc('-', stderr);
*/
					tcase_printf(verbose, "rem %s %d\r", str, hashmap_size(h));
					allocator_free(NULL, str);
				} else {
/*fputc('x', stderr);
*/
					tcase_printf(verbose, "nothing left to iterate over\r");
					hashmap_iterate(h, &iter);
				}
			}
		}
    }

if (verbose)
fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap_size(h), table_sizes[h->table_size_index], hal->alloc_total - hal->free_total);
	hashmap_del(h, allocator_free, NULL, NULL);
/*
	free(mem);
*/
if (verbose)
fprintf(stderr, "bytes outstanding from allocator: %d\n", hal->alloc_total - hal->free_total);

	cfg = NULL;
    return 0;
}
Esempio n. 7
0
int
SvsemCounter(int verbose, struct cfg *cfg, char *args[])
{
	svsem_t sem;
	int pi, nprocs, *pids, fd, *ptr, status;
	const char *path = "/tmp/SvsemCounter.sem";

	if (svsem_open(&sem, path, O_CREAT | SEM_UNDO, 0600, 1) == -1) {
		if (errno != EEXIST) {
			perror("svsem_open");
			return EXIT_FAILURE;
		}
	}

	if ((fd = open("/tmp/SvsemCounter.shm", O_RDWR | O_CREAT, 0600)) == -1 ||
			ftruncate(fd, sizeof *ptr) == -1 ||
			(ptr = (int *)mmap(NULL, sizeof *ptr, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == NULL) {
		perror("");
		return -1;
	}
	close(fd);

	nprocs = strtoul(args[0], NULL, 10);
	count = strtoul(args[1], NULL, 10);

	if ((pids = malloc(nprocs * sizeof *pids)) == NULL) {
		perror("");
		return -1;
	}

	*ptr = 0;
	for (pi = 0; pi < nprocs; pi++) {
		pid_t pid;
		if ((pid = fork()) == -1) {
			perror("");
			return EXIT_FAILURE;
		}
		if (pid) {
			pids[pi] = pid;
		} else {
			int ret;
			ret = run();
			free(pids);
			munmap(ptr, sizeof *ptr);
			svsem_close(&sem);
			exit(ret);
		}
	}
	do {
		waitpid(pids[--pi], &status, 0);
		if ((errno = WEXITSTATUS(status))) {
			perror("");
		}
		tcase_printf(verbose, "process complete\n");
	} while (pi);

	if (*ptr != (nprocs * count)) {
		tcase_printf(verbose, "nprocs=%d,count=%d,ptr=%d\n", nprocs, count, *ptr);
		return -1;
	}

	free(pids);
	munmap(ptr, sizeof *ptr);
	if (svsem_close(&sem) != 0) {
		MMSG("");
	}

	tcase_printf(verbose, "done");

	cfg = NULL;
	return 0;
}
Esempio n. 8
0
int
LinkedlistExercise(int verbose, struct cfg *cfg, char *args[])
{
    int rate, i, n = 0, idx;
	char *str;
    struct linkedlist *l = linkedlist_new(EXERCISE_MED_COUNT, NULL);

	cfg = NULL; args[0] = NULL;

	if (l == NULL) {
		AMSG("");
		return -1;
	}

	rate = EXERCISE_R0;
	for (i = 0; i < EXERCISE_MED_COUNT; i++) {
		if (i == EXERCISE_MED_P1) {
			rate = EXERCISE_R1;
		} else if (i == EXERCISE_MED_P2) {
			rate = EXERCISE_R2;
		} else if (i == EXERCISE_MED_P3) {
			rate = EXERCISE_R3;
		}

		if (rand() % 10 < rate) {
			idx = 0;
	        str = malloc(8);
	        sprintf(str, "%07d", n++);
			if (rand() % 5) {
				idx = linkedlist_size(l);
				if (idx) {
					idx = rand() % idx;
				}
			}
	        if (linkedlist_insert(l, idx, str) == -1) {
				PMNO(errno);
				return -1;
			}
			tcase_printf(verbose, "INSERT: %s size now %d\n", str, linkedlist_size(l));
		} else {
			if (linkedlist_is_empty(l)) {
				tcase_printf(verbose, "EMPTY\n");
			} else {
				idx = rand() % linkedlist_size(l);
		        str = linkedlist_get(l, idx);
				if (linkedlist_remove_data(l, str) == NULL) {
					PMNO(errno);
					return -1;
				}
				if ((idx % 10) == 0) {
					unsigned int count = 0;
					iter_t iter;

					linkedlist_iterate(l, &iter);
					while (linkedlist_next(l, &iter)) {
						count++;
					}
					if (count != linkedlist_size(l)) {
						PMSG("count=%u,linkedlist_size=%u\n", count, linkedlist_size(l));
						return -1;
					}
				}
				if (str) {
	    	    	tcase_printf(verbose, "REMOVE: %s %d\n", str, linkedlist_size(l));
			       	free(str);
				} else {
					PMSG("remove failure");
					return -1;
				}
			}
		}
    }
    linkedlist_del(l, allocator_free, NULL);

    return 0;
}
Esempio n. 9
0
int
HashmapExercise0(int verbose, struct cfg *cfg, char *args[])
{
	struct hashmap0 *h;
    int rate = 0, i, n = 0;
	int count = atoi(args[0]);
	int interval = atoi(args[1]);
	iter_t riter;
	char *str;
	clock_t t0;
	void *mem;
	struct allocator *al;
	iter_t iter;
	int chkpnt = 0;

	if ((mem = malloc(0xFFFFFFF)) == NULL ||
			(al = suba_init(mem, 0xFFFFFFF, 1, 0)) == NULL ||
    		(h = hashmap0_new(HASHMAP_LARGE, hash_str,
			(compare_fn)strcmp, NULL, allocator_free, NULL, al)) == NULL) {
		AMSG("");
		return -1;
	}
srand(1);

fputc('\n', stderr);

	t0 = clock();
fprintf(stderr, "       time    count     size      mem\n");

	hashmap0_iterate(h, &iter);
	rate_iterate(&riter);
	for (i = 0; i < count; i++) {
		if ((i % interval) == 0) {
fprintf(stderr, "%2d %8ld %8d %8d %8d %d\n", chkpnt++, (clock() - t0) / 1000, hashmap0_size(h), HASHMAP_LARGE, h->al->size_total - h->al->free_total, rate);
			rate = rate_next(&riter);
		}

		if (rand() % 10 < rate) {
	        str = allocator_alloc(NULL, 16, 0);
	        sprintf(str, "%015d", n++);
	        if (hashmap0_put(h, str, str) == -1) {
				AMSG("");
				return -1;
			} else {
/*fputc('+', stderr);
*/
	 	      	tcase_printf(verbose, "put  %s %d\n", str, hashmap0_size(h));
			}
		} else {
			if (hashmap0_is_empty(h)) {
				tcase_printf(verbose, "hashmap0 empty\n");
			} else {
		        str = hashmap0_next(h, &iter);
				if (str) {
	    	    	tcase_printf(verbose, "get %s %d\n", str, hashmap0_size(h));
					if ((str = hashmap0_remove(h, str)) == NULL) {
						errno = EFAULT;
						PMNO(errno);
						return -1;
					}
/*fputc('-', stderr);
*/
					tcase_printf(verbose, "remove %s %d\n", str, hashmap0_size(h));
					allocator_free(NULL, str);
				} else {
/*fputc('x', stderr);
*/
					tcase_printf(verbose, "nothing left to iterate over\r");
					hashmap0_iterate(h, &iter);
				}
			}
		}
    }
fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap0_size(h), HASHMAP_LARGE, h->al->size_total);

	hashmap0_del(h);
	tcase_printf(verbose, "hashmap0 done\n");

	cfg = NULL;
    return 0;
}