Esempio n. 1
0
void set_cores(int cores) {
	long num_cores_onfire = sysconf( _SC_NPROCESSORS_ONLN );
	Logger logger;
	string log_line="shakti_son;";
	log_line+=dynamic_cast<std::stringstream *> (&(std::stringstream() << cores))->str();

	if(cores<((int)num_cores_onfire)) {
		for(int i=num_cores_onfire-1;i>cores-1;i--) {
			string sys_file="/sys/devices/system/cpu/cpu";
			sys_file+=dynamic_cast<stringstream *> (&(stringstream() << i))->str();
			sys_file+="/online";

			ofstream cpu_down_stream;
			cpu_down_stream.open(sys_file.c_str(), ios::trunc);
			cpu_down_stream.write("0", 1);
			cpu_down_stream.close();
		}
		log_line+=" cores down. ";
	} else {
		for(int i=num_cores_onfire;i<cores;i++) {
			string sys_file="/sys/devices/system/cpu/cpu";
			sys_file+=dynamic_cast<stringstream *> (&(stringstream() << i))->str();
			sys_file+="/online";

			ofstream cpu_up_stream;
			cpu_up_stream.open(sys_file.c_str(), ios::trunc);
			cpu_up_stream.write("1", 1);
			cpu_up_stream.close();
		}
		log_line+="cores up. ";
	}
	logger.SendToLog(0, log_line);
}
Esempio n. 2
0
/* Envio de la mac del hijo al servidor */
string SendMacToServer() {
	char          buf[1024];
	struct ifconf ifc;
	struct ifreq *ifr;
	int           sck;
	int           num_interfaces;
	string line_to_server="";
	stringstream ss;

	sck = socket(AF_INET, SOCK_DGRAM, 0);
	if(sck < 0) {
		perror("socket");
		return "-1";
	}

	ifc.ifc_len = sizeof(buf);
	ifc.ifc_buf = buf;
	if(ioctl(sck, SIOCGIFCONF, &ifc) < 0) {
		perror("ioctl(SIOCGIFCONF)");
		return "-1";
	}

	ifr = ifc.ifc_req;
	num_interfaces = ifc.ifc_len / sizeof(struct ifreq);
	ss.clear();
	for(int i = 0; i < num_interfaces; i++) {
		struct ifreq *item = &ifr[i];
		if(strcmp(item->ifr_name, "lo")!=0) { //TODO: Detect only wired interface.
			if(ioctl(sck, SIOCGIFHWADDR, item) < 0) {
				return "-1";
			}

			char *mac_addr=new char(18);
			sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
					(unsigned char)item->ifr_hwaddr.sa_data[0], (unsigned char)item->ifr_hwaddr.sa_data[1], (unsigned char)item->ifr_hwaddr.sa_data[2],
					(unsigned char)item->ifr_hwaddr.sa_data[3], (unsigned char)item->ifr_hwaddr.sa_data[4], (unsigned char)item->ifr_hwaddr.sa_data[5]);
			ss<<item->ifr_name<<";"<<mac_addr<<";";
			ss>>line_to_server;
			free(mac_addr);
		}
	}

	Logger logger;
	logger.SendToLog(0, "shakti_son;MACs sent.");

	line_to_server+="EOL";
	return line_to_server;
}
Esempio n. 3
0
int main(int argc, char **argv) { //TODO: Puede haber un gran fallo de seguridad si alguien desarrolla una app y lanza un comando. REVISAR.
	int sockfd=-1, newsockfd=-1, portno=-1, n=-1;
	socklen_t clilen;
	char buffer[10];
	struct sockaddr_in serv_addr, cli_addr;
	string son_answer="";
	Logger logger;

	if(getuid()!=0) {
		logger.SendToLog(1, "shakti_son;You MUST run ShakTI Son as root");
		return -1;
	}

	/* Inicialización en modo demonio del sistema*/
	pid_t pid, sid;

	/* Fork del proceso padre */
	pid=fork();
	if (pid<0) {
		exit(EXIT_FAILURE);
	}

	/* Si obtenemos un PID válido, matamos el proceso padre */
	if (pid>0) {
		exit(EXIT_SUCCESS);
	}

	/* Cambiar la máscara del fichero  */
	umask(0);

	/* Crear un nuevo SID para el proceso hijo */
	sid=setsid();
	if (sid<0) {
		/* Fallo en la asignación del nuevo SID */
		exit(EXIT_FAILURE);
	}

	/* Cerrar todos los flujos estándares de entrada, salida y error */
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);


	sockfd=socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd<0) {
		logger.SendToLog(1, "shakti_son;Can't open a socket.");
		return -1;
	}

	bzero((char *) &serv_addr, sizeof(serv_addr));

	portno=atoi(SHAKTI_COM_PORT);
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=INADDR_ANY;
	serv_addr.sin_port=htons(portno);
	
	if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0) 
		logger.SendToLog(1, "shakti_son;Can't binding.");

	while(true) {
		son_answer="";
		int action=-1, cores=-1;
		listen(sockfd, 11);
		clilen = sizeof(cli_addr);
		newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
		
		if (newsockfd < 0)
			logger.SendToLog(1, "shakti_son;Can't accept the petition");
		
		bzero(buffer, 10);
		n = read(newsockfd, buffer, 10);

		if (n < 0)
			logger.SendToLog(1, "shakti_son;Can't reading from socket.");

		if((strlen(buffer))>1) {
			char action_char;
			char cores_char;
			bzero(&action_char, 1);
			bzero(&cores_char, 1);
			action_char=buffer[0];
			action=atoi(&action_char);
			cores_char=buffer[2];
			cores=atoi(&cores_char);
		} else {
			action=atoi(buffer);
		}

		switch(action) {
		case 0: ShutdownNode(); son_answer="1"; break;
		case 1: set_cores(cores); son_answer="1"; break;
		case 2: son_answer=SendMacToServer(); break;
		case 3: son_answer=NodeIsUp();	break;
		default: son_answer="0"; break;
		};
		n = write(newsockfd, son_answer.c_str(), son_answer.size());
		if (n < 0) logger.SendToLog(1, "shakti_son;Can't write to the socket. "+n);
	}
	
	close(newsockfd);
	close(sockfd);
	return 0; 
}