Example #1
0
int
dssfclose(Dssfile_t* file)
{
	int		r;
	Dss_t*		dss;

	if (!file)
		return -1;
	dss = file->dss;
	if (!file->io)
		r = -1;
	else
	{
		r = file->format ? (*file->format->closef)(file, dss->disc) : 0;
		if ((file->flags & DSS_FILE_WRITE) && sfsync(file->io))
		{
			if (dss->disc->errorf)
				(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: write error", file->path);
			r = -1;
		}
		if (!(file->flags & DSS_FILE_KEEP))
			sfclose(file->io);
		if (!r && (file->flags & DSS_FILE_ERROR))
			r = -1;
	}
	vmclose(file->vm);
	return r;
}
Example #2
0
int
pssclose(Pss_t* pss)
{
	int	r;

	if (!pss || !pss->vm)
		return -1;
	r = (pss->meth->donef && (*pss->meth->donef)(pss) <= 0) ? -1 : 0;
	if ((pss->disc->flags & PSS_VERBOSE) && pss->disc->errorf)
		(*pss->disc->errorf)(pss, pss->disc, 1, "%s: method done", pss->meth->name);
	vmclose(pss->vm);
	return r;
}
Example #3
0
Pss_t*
pssopen(Pssdisc_t* disc)
{
	register Pss_t*	pss;
	Vmalloc_t*	vm;

	if (!disc)
		return 0;
	if (!(vm = vmopen(Vmdcheap, Vmbest, 0)) || !(pss = vmnewof(vm, 0, Pss_t, 1, 0)))
		goto bad;
	pss->id = lib;
	pss->disc = disc;
	pss->vm = vm;
	pss->ttybynamedisc.key = offsetof(Tty_t, name);
	pss->ttybynamedisc.size = 0;
	pss->ttybynamedisc.link = offsetof(Tty_t, byname);
	pss->ttybydevdisc.key = offsetof(Tty_t, dev);
	pss->ttybydevdisc.size = sizeof(Pss_dev_t);
	pss->ttybydevdisc.link = offsetof(Tty_t, bydev);
	if (!(pss->ttybyname = dtnew(pss->vm, &pss->ttybynamedisc, Dtset)) ||
	    !(pss->ttybydev = dtnew(pss->vm, &pss->ttybydevdisc, Dtset)))
		goto bad;
	pss->meth = (_pss_ps && ((disc->flags & PSS_PS) || getenv("_PSS_ps"))) ? _pss_ps : _pss_method;
	while (pss->meth->initf && (*pss->meth->initf)(pss) <= 0)
		if (pss->meth == _pss_ps || !(pss->meth = _pss_ps))
		{
			vmclose(vm);
			return 0;
		}
	return pss;
 bad:
	if (disc->errorf)
		(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
	if (vm)
		vmclose(vm);
	return 0;
}
Example #4
0
static int
validate_end(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
{
	register State_t*	state = (State_t*)expr->data;
	register Field_t*	field;
	Invalid_t*		ip;
	Cxoperand_t		val;
	int			heading;

	if (state->summary)
	{
		heading = 1;
		if (state->invalid && dtsize(state->invalid))
		{
			heading = 0;
			sfprintf(expr->op, "%16s  %11s  %s\n", "FIELD", "COUNT", "VALUE");
			for (ip = (Invalid_t*)dtfirst(state->invalid); ip; ip = (Invalid_t*)dtnext(state->invalid, ip))
			{
				val.type = ip->variable->type;
				val.value = ip->value;
				if (!cxcast(cx, &val, NiL, cx->state->type_string, NiL, NiL))
					sfprintf(expr->op, "%16s  %11I*u  %*.*s\n", ip->variable->name, sizeof(ip->count), ip->count, val.value.string.size, val.value.string.size, val.value.string.data);
			}
		}
		if (!heading)
		{
			heading = 1;
			sfprintf(expr->op, "\n");
		}
		for (field = state->field; field; field = field->next)
			if (field->invalid || field->discarded || field->repaired)
			{
				if (heading)
				{
					heading = 0;
					sfprintf(expr->op, "%16s  %11s %11s %11s\n", "FIELD", "INVALID", "DISCARDED", "REPAIRED");
				}
				sfprintf(expr->op, "%16s  %11I*u %11I*u %11I*u\n", field->variable->name, sizeof(field->invalid), field->invalid, sizeof(field->discarded), field->discarded, sizeof(field->repaired), field->repaired);
			}
	}
	vmclose(state->vm);
	return 0;
}
Example #5
0
Dssfile_t*
dssfopen(Dss_t* dss, const char* path, Sfio_t* io, Dssflags_t flags, Dssformat_t* format)
{
	Dssfile_t*	file;
	Vmalloc_t*	vm;
	char*		s;
	size_t		n;
	int		i;
	struct stat	st;
	Sfdisc_t	top;
	char		buf[PATH_MAX];

	if (flags & DSS_FILE_WRITE)
	{
		if (io)
		{
			memset(&top, 0, sizeof(top));
			if (sfdisc(io, &top))
			{
				n = top.disc == &dss->state->compress_preferred;
				sfdisc(io, SF_POPDISC);
				if (n)
				{
					sfdisc(io, SF_POPDISC);
					sfdczip(io, path, dss->meth->compress ? dss->meth->compress : "gzip", dss->disc->errorf);
				}
			}
		}
		if (dss->flags & DSS_APPEND)
			flags |= DSS_FILE_APPEND;
	}
	if (!path || !*path || streq(path, "-"))
	{
		if (flags & DSS_FILE_WRITE)
		{
			if (io)
				path = "output-stream";
			else
			{
				path = "/dev/stdout";
				io = sfstdout;
			}
		}
		else if (io)
			path = "input-stream";
		else
		{
			path = "/dev/stdin";
			io = sfstdin;
		}
		flags |= DSS_FILE_KEEP;
	}
	else if (io)
		flags |= DSS_FILE_KEEP;
	else if (flags & DSS_FILE_WRITE)
	{
		if (!(io = sfopen(NiL, path, (flags & DSS_FILE_APPEND) ? "a" : "w")))
		{
			if (dss->disc->errorf)
				(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot open", path);
			return 0;
		}
	}
	else if (!(io = dssfind(path, "", DSS_VERBOSE, buf, sizeof(buf), dss->disc)))
		return 0;
	else
		path = (const char*)buf;
	if (!(vm = vmopen(Vmdcheap, Vmbest, 0)))
	{
		if (dss->disc->errorf)
			(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space");
		return 0;
	}
	if (!(file = vmnewof(vm, 0, Dssfile_t, 1, strlen(path) + 1)))
	{
		if (dss->disc->errorf)
			(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space");
		if (!(flags & DSS_FILE_KEEP))
			sfclose(io);
		vmclose(vm);
		return 0;
	}
	strcpy(file->path = (char*)(file + 1), path);
	file->dss = dss;
	file->vm = vm;
	file->io = io;
	file->flags = flags;
	if (flags & DSS_FILE_WRITE)
	{
		if (!(file->format = format) && !(file->format = dss->format))
		{
			if (dss->disc->errorf)
				(*dss->disc->errorf)(NiL, dss->disc, 2, "output method format must be specified");
			if (!(flags & DSS_FILE_KEEP))
				sfclose(io);
			return 0;
		}
		file->readf = noreadf;
		file->writef = file->format->writef;
	}
	else
	{
		if (sfsize(file->io) || !fstat(sffileno(file->io), &st) && (S_ISFIFO(st.st_mode)
#ifdef S_ISSOCK
			|| S_ISSOCK(st.st_mode)
#endif
			))
		{
			if (sfdczip(file->io, file->path, NiL, dss->disc->errorf) < 0)
			{
				if (dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: inflate error", file->path);
				dssfclose(file);
				return 0;
			}
			s = sfreserve(file->io, SF_UNBOUND, SF_LOCKR);
			n = sfvalue(file->io);
			if (!s)
			{
				if (n && dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot peek", file->path);
				dssfclose(file);
				return 0;
			}
			for (file->format = (Dssformat_t*)dtfirst(dss->meth->formats); file->format && !(i = (*file->format->identf)(file, s, n, dss->disc)); file->format = (Dssformat_t*)dtnext(dss->meth->formats, file->format));
			sfread(file->io, s, 0);
			if (!file->format)
			{
				if (dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, 2, "%s: unknown %s format", file->path, dss->meth->name);
				dssfclose(file);
				return 0;
			}
			if (i < 0)
				return 0;
			if (format && format != file->format)
			{
				if (dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s file format %s incompatible with %s", file->path, dss->meth->name, file->format->name, format->name);
				dssfclose(file);
				return 0;
			}
			if ((dss->flags & DSS_VERBOSE) && dss->disc->errorf)
				(*dss->disc->errorf)(dss, dss->disc, 1, "%s: %s method %s format", file->path, dss->meth->name, file->format->name);
			file->readf = file->format->readf;
		}
		else
		{
			file->format = format ? format : dss->format ? dss->format : (Dssformat_t*)dtfirst(dss->meth->formats);
			file->readf = nullreadf;
		}
		file->writef = nowritef;
		if (!dss->format)
			dss->format = file->format;
	}
	if (!file->format)
	{
		if (dss->disc->errorf)
			(*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s method did not set file format", file->path, dss->meth->name);
		dssfclose(file);
		return 0;
	}
	file->record.file = file;
	if ((*file->format->openf)(file, dss->disc))
	{
		dssfclose(file);
		return 0;
	}
	return file;
}
Example #6
0
tmain()
{
	int		k, code = 0;
	Vmalloc_t	*shm, *map;
	char		*arg[5];
	pid_t		ppid, cpid;

	if(k = tchild())
	{	cpid = getpid();
		shmfile = argv[k];
		mapfile = argv[k+1];

		tinfo("Child[pid=%d]: allocating heap memory before opening shm region", cpid);
		for(k = 0; k < 1024; ++k)
			if(!malloc(32*1024) )
				terror("Child[pid=%d]: Can't allocate segment %d", cpid, k);

		if(*shmfile)
		{	tinfo("Child[pid=%d]: opening shm region", cpid);
			if(!(shm = vmmopen(shmfile, 1, MAPSIZE)) )
				terror("Child[pid=%d]: Can't open shm region in child process", cpid);
			tinfo("Child[pid=%d]: shm region opened", cpid);
		}

		tinfo("Child[pid=%d]: allocating heap memory before opening map region", cpid);
		for(k = 0; k < 1024; ++k)
			if(!malloc(32*1024) )
				terror("Child[pid=%d]: Can't allocate segment %d", cpid, k);

		if(*mapfile)
		{	tinfo("Child[pid=%d]: opening map region", cpid);
			if(!(map = vmmopen(mapfile, -1, MAPSIZE)) )
				terror("Child[pid=%d]: Can't open map region in child process", cpid);
			tinfo("Child[pid=%d]: map region opened", cpid);
		}
	}
	else
	{	ppid = getpid();
		shmfile = tstfile("shm", -1);
		mapfile = tstfile("map", -1);
		(void)unlink(shmfile);
		(void)unlink(mapfile);
		
		tinfo("Parent[pid=%d]: %s: opening shm region", ppid, shmfile);
		if(shm = vmmopen(shmfile, 1, MAPSIZE) )
			tinfo("Parent[pid=%d]: %s: shm region opened", ppid, shmfile);
		else
		{	tnote("shm not supported");
			shmfile = "";
		}

		tinfo("Parent[pid=%d]: %s: opening map region", ppid, mapfile);
		if(map = vmmopen(mapfile, -1, MAPSIZE) )
			tinfo("Parent[pid=%d]: %s: map region opened", ppid, mapfile);
		else
		{	tnote("map not supported");
			mapfile = "";
		}

		switch((cpid = fork()) ) /* make a child process */
		{ default :
			code = twait(&cpid, 1);
			break;
		  case 0 :
			arg[0] = argv[0];
			arg[1] = "--child";
			arg[2] = shmfile;
			arg[3] = mapfile;
			arg[4] = 0;
			if(execv(argv[0], arg) < 0 )
				terror("Could not exec child process");
		  case -1:
			terror("Could not fork a child process");
		}
	}

	vmmrelease(shm, 1); vmclose(shm);
	vmmrelease(map, 1); vmclose(map);

	texit(code);
}
Example #7
0
static int
validate_beg(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
{
	char**			argv = (char**)data;
	int			errors = error_info.errors;
	char*			s;
	State_t*		state;
	Cxvariable_t*		variable;
	register Field_t*	field;
	Field_t*		lastfield;
	Cxconstraint_t*		constraint;
	int			all;
	int			list;
	Vmalloc_t*		vm;

	if (!(vm = vmopen(Vmdcheap, Vmlast, 0)) || !(state = vmnewof(vm, 0, State_t, 1, 0)))
	{
		if (vm)
			vmclose(vm);
		if (disc->errorf)
			(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
		return -1;
	}
	state->vm = vm;
	list = 0;
	sfprintf(cx->buf, "%s%s", strchr(dss_lib_validate.description, '['), validate_usage);
	s = sfstruse(cx->buf);
	for (;;)
	{
		switch (optget(argv, s))
		{
		case 'd':
			state->discard = 1;
			continue;
		case 'l':
			list = 1;
			continue;
		case 'r':
			if (!(state->setf = cxcallout(cx, CX_SET, cx->state->type_void, cx->state->type_void, cx->disc)))
			{
				if (cx->disc->errorf)
					(*cx->disc->errorf)(NiL, cx->disc, 3, "reair requires CX_SET callout");
				return -1;
			}
			continue;
		case 's':
			state->summary = 1;
			continue;
		case 'v':
			state->summary = state->verbose = 1;
			continue;
		case '?':
			if (disc->errorf)
				(*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", opt_info.arg);
			else
				return -1;
			continue;
		case ':':
			if (disc->errorf)
				(*disc->errorf)(NiL, disc, 2, "%s", opt_info.arg);
			else
				return -1;
			continue;
		}
		break;
	}
	if (error_info.errors > errors)
		goto bad;
	argv += opt_info.index;
	if (all = !*argv)
		variable = 0;
	do
	{
		if (all)
		{
			if (!(variable = (Cxvariable_t*)(variable ? dtnext(cx->fields, variable) : dtfirst(cx->fields))))
				break;
		}
		else if (!(variable = cxvariable(cx, *argv, NiL, disc)))
			goto bad;
		if (variable->format.constraint || variable->format.map)
		{
			if (!(field = vmnewof(vm, 0, Field_t, 1, 0)))
			{
				if (disc->errorf)
					(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
				goto bad;
			}
			field->variable = variable;
			if (state->field)
				lastfield = lastfield->next = field;
			else
				lastfield = state->field = field;
		}
	} while (all || *++argv);
	if (!state->field && disc->errorf)
		(*disc->errorf)(NiL, disc, 1, "no field has constraints or maps");
	if (list)
	{
		for (field = state->field; field; field = field->next)
		{
			sfprintf(expr->op, "%16s", field->variable->name);
			if (field->variable->format.map)
				sfprintf(expr->op, " map");
			if (constraint = field->variable->format.constraint)
			{
				if (constraint->name)
					sfprintf(expr->op, " name=%s", constraint->name);
				if (constraint->constraintf)
					sfprintf(expr->op, " external");
				if (cxisnumber(field->variable->type))
				{
					if (constraint->def)
						number(expr->op, "default", constraint->def->number, &field->variable->format);
					if (constraint->min)
						number(expr->op, "min", constraint->min->number, &field->variable->format);
					if (constraint->max)
						number(expr->op, "max", constraint->max->number, &field->variable->format);
				}
				else if (cxisstring(field->variable->type) && constraint->def)
					sfprintf(expr->op, " default=\"%-.*s\"", constraint->def->string.size, constraint->def->string.data);
				if (constraint->expression)
					sfprintf(expr->op, " expression=\"%s\"", constraint->expression);
				if (constraint->pattern)
					sfprintf(expr->op, " pattern=\"%s\"", constraint->pattern);
			}
			sfprintf(expr->op, "\n");
		}
		goto bad;
	}
	if (!(state->getf = cxcallout(cx, CX_GET, cx->state->type_void, cx->state->type_void, cx->disc)))
	{
		if (cx->disc->errorf)
			(*cx->disc->errorf)(NiL, cx->disc, 3, "validation requires CX_GET callout");
		goto bad;
	}
	if (!state->verbose)
	{
		state->invaliddisc.comparf = invalidcmp;
		if (!(state->invalid = dtnew(vm, &state->invaliddisc, Dtoset)))
		{
			if (cx->disc->errorf)
				(*cx->disc->errorf)(NiL, cx->disc, 3, "validation requires CX_GET callout");
			goto bad;
		}
	}
	expr->data = state;
	return 0;
 bad:
	vmclose(vm);
	return -1;
}
Example #8
0
static int
merge_beg(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc)
{
	register State_t*	state;
	register File_t*	file;
	char**			argv = (char**)data;
	char**			files = expr->files;
	Dss_t*			dss = DSS(cx);
	int			errors = error_info.errors;
	int			i;
	int			k;
	int			n;
	int			r;
	char*			path;
	char*			u;
	char**			v;
	Cxvariable_t*		variable;
	Cxoperand_t*		operands;
	Key_t*			key;
	Key_t*			lastkey;
	Key_t*			keys;
	Vmalloc_t*		vm;

	if (!(vm = vmopen(Vmdcheap, Vmbest, 0)))
	{
		if (disc->errorf)
			(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
		return -1;
	}
	r = -1;
	k = 0;
	keys = 0;
	sfprintf(cx->buf, "%s%s", strchr(dss_lib_merge.description, '['), merge_usage);
	u = sfstruse(cx->buf);
	for (;;)
	{
		switch (i = optget(argv, u))
		{
		case 'k':
		case 'r':
			if (!(variable = cxvariable(cx, opt_info.arg, NiL, disc)))
				goto bad;
			if (!(key = vmnewof(vm, 0, Key_t, 1, 0)))
			{
				if (disc->errorf)
					(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
				goto bad;
			}
			key->variable = variable;
			key->sense = (i == 'r') ? -1 : 1;
			if (keys)
				lastkey = lastkey->next = key;
			else
				lastkey = keys = key;
			k++;
			continue;
		case '?':
			if (disc->errorf)
				(*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", opt_info.arg);
			else
				return -1;
			continue;
		case ':':
			if (disc->errorf)
				(*disc->errorf)(NiL, disc, 2, "%s", opt_info.arg);
			else
				return -1;
			continue;
		}
		break;
	}
	if (error_info.errors > errors)
		goto bad;
	argv += opt_info.index;
	for (v = argv; *v; v++);
	n = v - argv;
	if (files)
	{
		for (v = files; *v; v++);
		n += v - files;
	}
	if (!n)
		n = 1;
	if (!(state = vmnewof(vm, 0, State_t, 1, (n - 1) * sizeof(File_t))) || !(operands = vmnewof(vm, 0, Cxoperand_t, n * k, 0)))
	{
		if (cx->disc->errorf)
			(*cx->disc->errorf)(NiL, cx->disc, ERROR_SYSTEM|2, "out of space");
		goto bad;
	}
	state->cx = cx;
	if (!(state->getf = cxcallout(cx, CX_GET, cx->state->type_void, cx->state->type_void, cx->disc)))
	{
		if (cx->disc->errorf)
			(*cx->disc->errorf)(NiL, cx->disc, 2, "CX_GET callout required");
		goto bad;
	}
	state->nfiles = n;
	state->nkeys = k;
	state->keys = keys;
	for (n = 0; n < state->nfiles; n++)
	{
		state->files[n].data = operands;
		operands += k;
	}
	state->orderdisc.comparf = ordercmp;
	if (!(state->order = dtnew(vm, &state->orderdisc, Dtoset)))
	{
		if (cx->disc->errorf)
			(*cx->disc->errorf)(NiL, cx->disc, ERROR_SYSTEM|2, "out of space");
		goto bad;
	}
	n = 0;
	if (path = *argv)
		argv++;
	else if (files)
	{
		argv = files;
		files = 0;
		if (path = *argv)
			argv++;
	}
	for (;;)
	{
		if (!(state->files[n].file = dssfopen(dss, path, NiL, DSS_FILE_READ, NiL)))
			goto drop;
		enter(dss, state, &state->files[n]);
		n++;
		if (!(path = *argv++))
		{
			if (!files)
				break;
			argv = files;
			files = 0;
			if (!(path = *argv++))
				break;
		}
	}
	expr = expr->pass;
	if (dssbeg(dss, expr))
		goto drop;
	while (file = (File_t*)dtfirst(state->order))
	{
		if (dsseval(dss, expr, file->record) < 0)
			goto drop;
		enter(dss, state, file);
	}
	if (error_info.errors == errors)
		r = 0;
 drop:
	for (n = 0; n < state->nfiles; n++)
		if (state->files[n].file)
			dssfclose(state->files[n].file);
 bad:
	vmclose(vm);
	return r;
}
Example #9
0
tmain()
{
	int		i, k, m;
	ssize_t		size;
	Vmdisc_t	*dc;
	Region_t	region[N_REGION];
	char		*shmfile, *mapfile, *warn;

	warn = (char*)0;
	size = 0;
	m = sizeof(char*) == 4 ? 2 : 16;
	for(k = 0; k < N_REGION; ++k)
	{
		region[k].size = (trandom()%m + m)*m;
		region[k].size *= 1024*1024;

		if(k%(N_SHMREG+1) != 0 ) /* do a bunch of shm memory */
		{
			shmfile = tstfile("shm", k);
			if(!(dc = vmdcshare(shmfile, 1, region[k].size, -1)) ||
			   !(region[k].vm = vmopen(dc, Vmbest, 0)) )
			{	warn = "shmem";
				break;
			}

			tinfo("Region-shmem[%d] size=%lu addr=%p",
				k, region[k].size/(1024*1024), region[k].vm->data);
			size += region[k].size;
		}
		else /* interspersed the above with mmap */
		{	
			mapfile = tstfile("map", k);
			if(!(dc = vmdcshare(mapfile, -1, region[k].size, -1)) ||
			   !(region[k].vm = vmopen(dc, Vmbest, 0)) )
			{	warn = "mmap";
				break;
			}

			tinfo("Region-mmap[%d] size=%lu addr=%p",
				k, region[k].size/(1024*1024), region[k].vm->data);
			size += region[k].size;
		}

		for(i = 0; i < k; ++i)
		{	if((char*)region[i].vm->data >= (char*)region[k].vm->data &&
			   (char*)region[i].vm->data <= ((char*)region[k].vm->data + region[k].size) )
				terror("Region[%d] and Region[%d] overlap", i, k);
		}
	}

	if(size > 0 )
		tinfo("#regions to try=%d #regions actually opened=%d total memory=%luM",
			N_REGION, k, size/(1024*1024) );

	for(i = 0; i < k; ++i)
		vmclose(region[i].vm);

	if(warn && k == 0)
		terror("Region-%s[%d] size=%luM failed", warn, k, region[k].size/(1024*1024) );

	texit(0);
}