Пример #1
0
int
main(int ac, char **av)
{
    int i;
    char *p;
    FILE *f;

    while ((i = getopt(ac, av, "hprdm:a")) != -1) {
	switch (i) {
	case 'h':
	    hexmode = 1;
	    break;
	case 'p':
	    pppmode = 1;
	    break;
	case 'r':
	    reverse = 1;
	    break;
	case 'd':
	    decompress = 1;
	    break;
	case 'm':
	    mru = atoi(optarg);
	    break;
	case 'a':
	    abs_times = 1;
	    break;
	default:
	    fprintf(stderr, "Usage: %s [-h | -p[d]] [-r] [-m mru] [-a] [file ...]\n", av[0]);
	    exit(1);
	}
    }
    if (optind >= ac)
	dumplog(stdin);
    else {
	for (i = optind; i < ac; ++i) {
	    p = av[i];
	    if ((f = fopen(p, "r")) == NULL) {
		perror(p);
		exit(1);
	    }
	    if (pppmode)
		dumpppp(f);
	    else
		dumplog(f);
	    fclose(f);
	}
    }
    exit(0);
}
Пример #2
0
int PickInfoNode(INFOLIST * infoList,char * infoName,char * infoValue){
	int ret = -1;
	INFONODE * preptr = infoList->ListHead;

	int dfd = openlog();
	dumplog(dfd, "In PickInfoNode");

	if(preptr == NULL || infoName == NULL || infoValue == NULL){
		dumplog(dfd, "null pointer");
		ret = 0;
	}else{
		dumplog(dfd, "O");
		bzero(infoName, strlen(infoName));
		bzero(infoValue, strlen(infoValue));
		strcpy(infoName, preptr->Name);
		strcpy(infoValue, preptr->Value);
		dumplog(dfd, "T");
		if(preptr != infoList->ListTail){
			dumplog(dfd, "A");
			infoList->ListHead = preptr->Next;
		}else{
			dumplog(dfd, "B");
			infoList->ListHead = NULL;
			infoList->ListTail = NULL;
		}
		free(preptr);
		ret = 1;
	}
	closelog(dfd);
	return ret;
}
Пример #3
0
void AppendInfoNode(INFOLIST * infoList, char * infoName, char * infoValue){
	INFONODE * infoNode = NULL;

	int dfd = openlog();
	dumplog(dfd, "In AppendInfoNode");
	
	if(infoList == NULL || infoName == NULL || infoValue == NULL) {/* Invalid Parameters */
		dumplog(dfd, "A");
		closelog(dfd);
		return;
	}
	infoNode = (INFONODE *)malloc(sizeof(INFONODE));
	//memset(infoNode, 0, sizeof(INFONODE));
	if(infoNode == NULL) {/* malloc failed */
		dumplog(dfd, "B");
		closelog(dfd);
		return;
	}
	/* Set Values */
	strncpy(infoNode->Name, infoName, MAX_NAMELEN);
	strncpy(infoNode->Value, infoValue, MAX_URLLEN);
	infoNode->Next = NULL;
	dumplog(dfd, "C");
	
	if(infoList->ListHead == NULL || infoList->ListTail == NULL){
		dumplog(dfd, "D");
		// Fist time, make init node
		infoList->ListHead = infoNode;
		infoList->ListTail = infoNode;
		dumplog(dfd, "E");
	}else{
		// Append Data
		dumplog(dfd, "F");
		infoList->ListTail->Next = infoNode;
		infoList->ListTail = infoNode;
		dumplog(dfd, "G");
	}
	dumplog(dfd, "H");

	closelog(dfd);
}
Пример #4
0
void daemonize(int servfd){
	int childpid, fd, fdtablesize, pid;
	int logfd;

	logfd = openlog();

	signal(SIGTTOU, SIG_IGN);
	signal(SIGTTIN, SIG_IGN);
	signal(SIGTSTP, SIG_IGN);

	if((childpid=fork())<0){
		dumplog(logfd, "failed to fork fist child");
		closelog(logfd);
		exit(1);
	}else if(childpid > 0){
		closelog(logfd);
		exit(0);
	}
	setsid();
	signal(SIGHUP, SIG_IGN);

	if((pid=fork())!=0){
		closelog(logfd);
		exit(0);
	}

	for(fd=0,fdtablesize=getdtablesize();fd<fdtablesize;fd++){
		if(fd!=servfd){
			close(fd);
		}
	}

	chdir("/");
	umask(0);
	
	closelog(logfd);
}
Пример #5
0
void do_esxi_session(int sockfd){
	int localfd, i, n, len;
	char logbuf[BUFMAXLEN] = "\0";
	char msgbuf[MAXPACKLEN] = "\0";
	char respbuf[MAXPACKLEN] = "\0";
	char filebuf[MAXPACKLEN + 1] = "\0";
	char *ptr = NULL;
	int rfd;
	char ok[]="[0]";
	char pkgtail[] = "[EOF]";
	char testbuf[MAXPACKLEN + 1] = "\0";

	localfd = openlog();
	bzero(logbuf, BUFMAXLEN);
	sprintf(logbuf, "Do esxi session on socket fd: %d", sockfd);
	dumplog(localfd, logbuf);

	for(;;){
		bzero(msgbuf, sizeof(msgbuf));
		n = read(sockfd, msgbuf, sizeof(msgbuf));
		if(n <= 0)
			break;
		for(i=n-1;i>0;i--){
			if(msgbuf[i] == 10 || msgbuf[i] == 13){
				msgbuf[i] = 0;
			}else{
				break;
			}
		}
		dumplog(localfd, msgbuf);
		if(strncmp(msgbuf, "quit", 4) == 0){
			dumplog(localfd, "Bye!!!");
			break;
		}else{
			bzero(logbuf, sizeof(logbuf));
			sprintf(logbuf, "Incoming %s", msgbuf);
			dumplog(localfd, logbuf);
			handle_esxi_session(msgbuf, respbuf);
			dumplog(localfd, "TEST AFTER handle esxi");
			dumplog(localfd, respbuf);
		}
		if(strncmp(respbuf, respfile, strlen(respfile)) == 0){			
			ptr = respbuf + strlen(respfile);
			dumplog(localfd, ptr);
			// read content from large file and send them out
			rfd = open(ptr, O_RDONLY);
			if(rfd != -1){
				write(sockfd, ok, strlen(ok));
				bzero(testbuf, sizeof(testbuf));
				sprintf(testbuf, "sent:|%s|", ok);
				dumplog(localfd, testbuf);
				len = read(rfd, filebuf, MAXPACKLEN);
				while(len > 0){
					write(sockfd, filebuf, MAXPACKLEN);
					bzero(testbuf, sizeof(testbuf));
					sprintf(testbuf, "sent:|%s|", filebuf);
					dumplog(localfd, testbuf);
					bzero(filebuf, MAXPACKLEN + 1);
					len = read(rfd, filebuf, MAXPACKLEN);
				}
				close(rfd);
				// kill the temp file
				remove(ptr);
			}
		}else{
			write(sockfd, respbuf, strlen(respbuf));
			bzero(testbuf, sizeof(testbuf));
			sprintf(testbuf, "sent:|%s|", respbuf);
			dumplog(localfd, testbuf);
		}
		write(sockfd, pkgtail, strlen(pkgtail));
	}
	

	closelog(localfd);
}
Пример #6
0
int main(int argc, char * argv[]){
	//define
	int opt, len;
	int clilen;
	int childpid;
	int mainfd;
	int sockfd, newsockfd;
	int listen_port;
	char * listen_ip;
	struct sockaddr_in servaddr, cliaddr;
	char msgbuf[BUFMAXLEN] = "\0";
	int ret ; 


	if(argc < 3){
		fprintf(stderr, "Usage: vmmengine <127.0.0.1> <4564>\n");
		return 1;
	}
	listen_ip = argv[1];
	listen_port = atoi(argv[2]);
	// init
	mainfd = openlog();
	dumplog(mainfd, "vmmengine started");
	bzero(msgbuf, BUFMAXLEN);
	sprintf(msgbuf, "Listening at %s:%d", listen_ip, listen_port);
	dumplog(mainfd, msgbuf);
	bzero((char *)&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = inet_addr(listen_ip);
	servaddr.sin_port = htons(listen_port);

	// main body
	// network
	if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0){
		dumplog(mainfd, "Failed to create server socket!");
		closelog(mainfd);
		exit(1);
	}

	opt = 1;
	len = sizeof(opt);
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, &len);
	if(bind(sockfd, (struct sockaddr_in *)&servaddr, sizeof(servaddr))<0){
		dumplog(mainfd, "Failed to bind!");
		closelog(mainfd);
		exit(1);
	}
	listen(sockfd, LISTEN_NUM);

	daemonize(sockfd);

	signal(SIGCHLD, reap_status);
	
	for(;;){
		clilen = sizeof(cliaddr);
		newsockfd = accept(sockfd, (struct sockaddr_in *)&cliaddr, &clilen);
		if(newsockfd < 0 && errno == EINTR)
			continue;
		else if(newsockfd < 0)
			dumplog(mainfd, "Failed to accept connection! Kill me!");
			
		if((childpid = fork()) == 0){
			close(sockfd);
			do_esxi_session(newsockfd);
			exit(0);
		}
		close(newsockfd);
	}


	dumplog(mainfd, "vmmengine stopped nicely");
	closelog(mainfd);
	return 0;
}