Example #1
0
static void serve(int sockfd)
{
	int n;
	socklen_t alen;
	FILE *fp;
	char buf[BUFLEN];
	char abuf[MAXADDRLEN];
	struct sockaddr *addr = (struct sockaddr *)abuf;

	set_cloexec(sockfd);
	for (;;) {
		alen = MAXADDRLEN;
		if ((n = recvfrom(sockfd, buf, BUFLEN, 0, addr, &alne)) < 0) {
			syslog(LOG_ERR, "rpsd: recvfrom error: %s",
			       strerror(errno));		
		}
		if ((fp = popen("/usr/bin/ps -e | wc", "r")) == NULL) {
			sprintf(buf, "error: %s\n", gai_strerror(errno));
			sendo(sockfd, buf, strlen(buf), 0, addr, alen);
		} else {
			if (fgets(buf, BUFLEN, fp) != NULL)
				sendto(sockfd, buf, strlen(buf), 0, addr, alen);
			pclose(fp);
		}
	}
}
Example #2
0
void dg_cli_options(FILE *fp,int sockfd,const struct sockaddr *sa,socklen_t servlen){
	int n;
	char sendline[MAXLINE],recvline[MAXLINE+1];
	struct timeval tv;
	tv.tv_sec=5;
	tv.tv_usec=0;
	setsockopt(sockfd,SOL_SOCKET,SO_RECVTIMEO,&tv,sizeof(tv));
	while(Fgets(sendline,MAXLINE,fp) != NULL){
		sendo(sockfd,sendline,MAXLINE,0,sa,servlen);
		n = recvfrom(sockfd,recvline,MAXLINE,0,sa,servlen);
		if(n<0){
			if(errno == EWOULDBLOCK)
				fprintf(stderr,"socket timeout!\n");
			else
				err_sys("recvfrom error");
		}
		recvline[n]='\0';
		Fputs(recvline,stdout);
	}
}