⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 linux/unix下c/s形式的资源监视,客户端负责搜集机器,再传送到服务端.
💻 C
字号:
/* * Some small functions. * * Copyright (c) 2004, by:      Jian Shen *    All rights reserved.      Peking University, China *                             <shenjian@net.pku.edu.cn> * * This file may be used subject to the terms and conditions of the * GNU Library General Public License Version 2, or any later version * at your option, as published by the Free Software Foundation. * 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 Library General Public License for more details. * */#include "util.h"#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <netdb.h>#include <pwd.h>#include <fcntl.h>#include <errno.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/select.h>#include <netinet/in.h>#include <arpa/inet.h>/* get host's ip address by its alias according to /etc/hosts */char* get_host_ip(const char* hostname){	char* hostIP;	struct hostent * h;	struct in_addr* in;	hostIP = (char*)malloc(20*sizeof(char));	/* get host structure by host name */	h = gethostbyname(hostname);	if(h != NULL) {		in = (struct in_addr*)h->h_addr;		sprintf(hostIP,"%s", inet_ntoa(*in));		return hostIP;			/* caller free */	}else {		fprintf(stderr, "gethostbyname failed.\n");		return NULL;	}}/* get current process's real user name */char* get_user_name(){	uid_t uid;	struct passwd* ppw;	uid = getuid();	ppw = getpwuid(uid);		if( ppw == NULL)		return NULL;	else		return ppw->pw_name;}int nonb_connect(int sockfd, struct sockaddr* sa, int sec){	int flags;	int status;	fd_set mask;	struct timeval timeout;	flags = fcntl(sockfd, F_GETFL, 0);	if(flags < 0)		return -1;	flags |= O_NONBLOCK;	if(fcntl(sockfd, F_SETFL, flags) < 0) {		perror("fcntl in nonb_connect");		return -1;	}	if(connect(sockfd,sa,sizeof(struct sockaddr)) == 0) {		flags &= ~O_NONBLOCK;		fcntl(sockfd, F_SETFL, flags);		return sockfd;	}   	FD_ZERO(&mask);	FD_SET(sockfd,&mask);	timeout.tv_sec = sec;	timeout.tv_usec = 0;	status = select(sockfd+1, NULL, &mask, NULL, &timeout);	switch(status){		case -1:			perror("select");			flags &= ~O_NONBLOCK;			fcntl(sockfd, F_SETFL, flags);			return -1;		case 0:			fprintf(stderr, "Connection Timeout.\n");			flags &= ~O_NONBLOCK;			fcntl(sockfd, F_SETFL, flags);			return -2;		default:			FD_CLR(sockfd, &mask);			flags &= ~O_NONBLOCK;			fcntl(sockfd, F_SETFL, flags);			return 0;	}}void print_load(system_load_info* sysload){	int i;/* 	for(i=0; i<sysload->num_of_cpu; i++) { *//* 		printf("CPU%d (%.3fMHZ): %.2f%s\n", i,  *//* 				sysload->cpu_freq[i], sysload->cpu_util[i]*100, "%"); *//* 	} *//* 	printf("Total memory : %ld KB\n", sysload->total_mem); *//* 	printf("Free memory : %ld KB\n", sysload->free_mem); *//* 	printf("Load Avarage(1,5,15min) : %.2f %.2f %.2f\n",  *//* 			sysload->loadavg[0], sysload->loadavg[1], sysload->loadavg[2]); */	for(i=0; i<sysload->num_of_cpu; i++) {		printf("CPU%d (%sMHZ): %s%s\n", i, 				sysload->cpu_freq[i], sysload->cpu_util[i], "%");	}	printf("Total memory : %d KB\n", sysload->total_mem);	printf("Free memory : %d KB\n", sysload->free_mem);	printf("Load Avarage(1,5,15min) : %s %s %s\n", 			sysload->loadavg[0], sysload->loadavg[1], sysload->loadavg[2]);}int xread(int sd, void *buf, size_t len){	char *p = (char *)buf;	size_t nrecv = 0;	ssize_t rv;		while (nrecv < len) {		rv = read(sd, p, len - nrecv);		if (rv < 0 && errno == EINTR)			continue;		if (rv < 0)			return -1;		if (rv == 0)			return 0;			/* remote close the socket */		nrecv += rv;		p += rv;	}	return nrecv;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -