示例#1
0
文件: stats.c 项目: aahud/harvey
void
readmach(Machine *m, int init)
{
	int n, i;
	uint64_t a[nelem(m->devsysstat)];
	char buf[32];

	if(m->remote && (m->disable || setjmp(catchalarm))){
		if (m->disable++ >= 5)
			m->disable = 0; /* give it another chance */
		memmove(m->devsysstat, m->prevsysstat, sizeof m->devsysstat);
		memmove(m->netetherstats, m->prevetherstats, sizeof m->netetherstats);
		return;
	}
	snprint(buf, sizeof buf, "%s", m->name);
	if (strcmp(m->name, buf) != 0){
		free(m->name);
		m->name = estrdup(buf);
		free(m->shortname);
		m->shortname = shortname(buf);
		if(display != nil)	/* else we're still initializing */
			eresized(0);
	}
	if(m->remote){
		notify(alarmed);
		alarm(5000);
	}
	if(needswap(init) && loadbuf(m, &m->swapfd) && readswap(m, a))
		memmove(m->devswap, a, sizeof m->devswap);
	if(needstat(init) && loadbuf(m, &m->statsfd)){
		memmove(m->prevsysstat, m->devsysstat, sizeof m->devsysstat);
		memset(m->devsysstat, 0, sizeof m->devsysstat);
		for(n=0; n<m->nproc && readnums(m, nelem(m->devsysstat), a, 0); n++)
			for(i=0; i<nelem(m->devsysstat); i++)
				m->devsysstat[i] += a[i];
	}
	if(needether(init) && loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1)){
		memmove(m->prevetherstats, m->netetherstats, sizeof m->netetherstats);
		memmove(m->netetherstats, a, sizeof m->netetherstats);
	}
	if(needsignal(init) && loadbuf(m, &m->ifstatsfd) && strncmp(m->buf, "Signal: ", 8)==0 && readnums(m, nelem(m->netetherifstats), a, 1)){
		memmove(m->netetherifstats, a, sizeof m->netetherifstats);
	}
	if(needbattery(init) && loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
		memmove(m->batterystats, a, sizeof(m->batterystats));
	if(needbattery(init) && loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
		memmove(m->batterystats, a, sizeof(m->batterystats));
	if(needtemp(init) && loadbuf(m, &m->tempfd))
		for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
			 m->temp[n] = a[0];
	if(m->remote){
		alarm(0);
		notify(nil);
	}
}
示例#2
0
void *
a_out_mod_load(int fd)
{
	struct exec info_buf;
	size_t b;
	ssize_t n;
	char buf[10 * BUFSIZ];

	/*
	 * Get the load module post load size... do this by reading the
	 * header and doing page counts.
	 */
	if (a_out_read_header(fd, &info_buf) < 0)
		return NULL;

	/*
	 * Seek to the text offset to start loading...
	 */
	if (lseek(fd, N_TXTOFF(info_buf), 0) == -1)
		err(12, "lseek");

	/*
	 * Transfer the relinked module to kernel memory in chunks of
	 * MODIOBUF size at a time.
	 */
	b = info_buf.a_text + info_buf.a_data;
	while (b) {
		n = read(fd, buf, MIN(b, sizeof(buf)));
		if (n < 0)
			err(1, "while reading from prelinked module");
		if (n == 0)
			errx(1, "EOF while reading from prelinked module");

		loadbuf(buf, n);
		b -= n;
	}
	return (void *)info_buf.a_entry;
}
示例#3
0
文件: stats.c 项目: aahud/harvey
int
initmach(Machine *m, char *name)
{
	int n, fd;
	uint64_t a[MAXNUM];
	char *p, mpt[256], buf[256];

	p = strchr(name, '!');
	if(p)
		p++;
	else
		p = name;
	m->name = estrdup(p);
	m->shortname = shortname(p);
	m->remote = (strcmp(p, mysysname) != 0);
	if(m->remote == 0)
		strcpy(mpt, "");
	else{
		snprint(mpt, sizeof mpt, "/n/%s", p);
		fd = connectexportfs(name);
		if(fd < 0){
			fprint(2, "can't connect to %s: %r\n", name);
			return 0;
		}
		/* BUG? need to use amount() now? */
		if(mount(fd, -1, mpt, MREPL, "", 'M') < 0){
			fprint(2, "stats: mount %s on %s failed (%r); trying /n/sid\n", name, mpt);
			strcpy(mpt, "/n/sid");
			if(mount(fd, -1, mpt, MREPL, "", 'M') < 0){
				fprint(2, "stats: mount %s on %s failed: %r\n", name, mpt);
				return 0;
			}
		}
	}

	snprint(buf, sizeof buf, "%s/dev/swap", mpt);
	m->swapfd = open(buf, OREAD);
	if(loadbuf(m, &m->swapfd) && readswap(m, a))
		memmove(m->devswap, a, sizeof m->devswap);
	else{
		m->devswap[Maxswap] = 100;
		m->devswap[Maxmem] = 100;
	}

	snprint(buf, sizeof buf, "%s/dev/sysstat", mpt);
	m->statsfd = open(buf, OREAD);
	if(loadbuf(m, &m->statsfd)){
		for(n=0; readnums(m, nelem(m->devsysstat), a, 0); n++)
			;
		m->nproc = n;
	}else
		m->nproc = 1;
	m->lgproc = ilog10(m->nproc);

	snprint(buf, sizeof buf, "%s/net/ether0/stats", mpt);
	m->etherfd = open(buf, OREAD);
	if(loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1))
		memmove(m->netetherstats, a, sizeof m->netetherstats);

	snprint(buf, sizeof buf, "%s/net/ether0/ifstats", mpt);
	m->ifstatsfd = open(buf, OREAD);
	if(loadbuf(m, &m->ifstatsfd)){
		/* need to check that this is a wavelan interface */
		if(strncmp(m->buf, "Signal: ", 8) == 0 && readnums(m, nelem(m->netetherifstats), a, 1))
			memmove(m->netetherifstats, a, sizeof m->netetherifstats);
	}

	snprint(buf, sizeof buf, "%s/mnt/apm/battery", mpt);
	m->batteryfd = open(buf, OREAD);
	m->bitsybatfd = -1;
	if(m->batteryfd >= 0){
		if(loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
			memmove(m->batterystats, a, sizeof(m->batterystats));
	}else{
		snprint(buf, sizeof buf, "%s/dev/battery", mpt);
		m->bitsybatfd = open(buf, OREAD);
		if(loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
			memmove(m->batterystats, a, sizeof(m->batterystats));
	}
	snprint(buf, sizeof buf, "%s/dev/cputemp", mpt);
	m->tempfd = open(buf, OREAD);
	if(loadbuf(m, &m->tempfd))
		for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
			 m->temp[n] = a[0];
	return 1;
}
示例#4
0
/* load a prelinked module; returns entry point */
void *
elf_mod_load(int fd)
{
	Elf_Ehdr ehdr;
	size_t zero_size = 0;
	size_t b;
	ssize_t n;
	char *shstrtab;
	struct elf_section *head, *s;
	char buf[10 * BUFSIZ];
	void *addr = NULL;

	if (read_elf_header(fd, &ehdr) < 0)
		return NULL;

	shstrtab = read_shstring_table(fd, &ehdr);
	read_sections(fd, &ehdr, shstrtab, &head);

	for (s = head; s; s = s->next) {
		if (s->type != SHT_STRTAB && s->type != SHT_SYMTAB &&
		    s->type != SHT_DYNSYM) {
			if (debug)
				fprintf(stderr, "loading `%s': addr = %p, "
				    "size = %#lx\n",
				    s->name, s->addr, (u_long)s->size);
			if (s->type == SHT_NOBITS) {
				/* skip some space */
				zero_size += s->size;
			} else {
				if (addr != NULL) {
					/*
					 * if there is a gap in the prelinked
					 * module, transfer some empty space.
					 */
					zero_size += (char*)s->addr -
					    (char*)addr;
				}
				if (zero_size) {
					loadspace(zero_size);
					zero_size = 0;
				}
				b = s->size;
				if (lseek(fd, s->offset, SEEK_SET) == -1)
					err(1, "lseek");
				while (b) {
					n = read(fd, buf, MIN(b, sizeof(buf)));
					if (n == 0)
						errx(1, "unexpected EOF");
					if (n < 0)
						err(1, "read");
					loadbuf(buf, n);
					b -= n;
				}
				addr = (char*)s->addr + s->size;
			}
		}
	}
	if (zero_size)
		loadspace(zero_size);

	free_sections(head);
	free(shstrtab);
	return (void *)ehdr.e_entry;
}