summaryrefslogtreecommitdiff
path: root/controller/main.c
blob: 2e37e76275a53644a4d6558237e2a6f3c6cea7e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/select.h>
#include "global.h"
#include "line_reader.h"
#include "unzbuffer.h"


static bool writeall(int sock,const char *data,i64 len){
	i64 cursor=0;
	while(cursor<len){
		i64 nwr=write(sock,data+cursor,len-cursor);
		if(nwr<=0)return false;
		cursor+=nwr;
	}
	return true;
}

struct frame_process {
	pid_t pid;
	i64 namelen;
	char *name;
	i64 usertime,systime,rss;
};

struct data_frame {
	struct timeval stamp;
	i64 numpids;
	struct frame_process *procs;
};

static i64 deserialise8(const u8 *buf){
	u64 value=0;
	for(i64 i=0;i<8;i++)value|=((u64)buf[i])<<(8*i);
	return (i64)value;
}

static i32 deserialise4(const u8 *buf){
	u32 value=0;
	for(i64 i=0;i<4;i++)value|=((u32)buf[i])<<(8*i);
	return (i32)value;
}

static void process_frame(const struct data_frame *frame){
	fprintf(stderr,"%s: (%" PRIi64 ")\n",ctime(&frame->stamp.tv_sec),frame->numpids);
}

static i64 read_frame(const u8 *buffer,i64 len){
	if(len<20)return 0;
	struct data_frame *frame=malloc(1,struct data_frame);
	frame->stamp.tv_sec=deserialise8(buffer);
	frame->stamp.tv_usec=deserialise4(buffer+8);
	i64 numpids=frame->numpids=deserialise8(buffer+12);
	i64 cursor=20;
	fprintf(stderr,"numpids=%" PRIi64 "\n",numpids);
	if(len-cursor<numpids*36){
		free(frame);
		return 0;
	}
	// calloc to null unfilled pointers in procs
	frame->procs=calloc(numpids,struct frame_process);

	i64 pi;
	for(pi=0;pi<frame->numpids;pi++){
		fprintf(stderr,"pi=%" PRIi64 "\n",pi);
		if(len-cursor<36){
			fprintf(stderr,"insuff: pi\n");
			goto insufficient_data;
		}
		frame->procs[pi].pid=deserialise4(buffer+cursor);
		cursor+=4;
		i64 namelen=frame->procs[pi].namelen=deserialise8(buffer+cursor);
		cursor+=8;
		fprintf(stderr,"  pid=%d  namelen=%" PRIi64 "\n",frame->procs[pi].pid,namelen);
		if(len-cursor<namelen+24){
			fprintf(stderr,"insuff: namelen\n");
			goto insufficient_data;
		}
		frame->procs[pi].name=malloc(namelen+1,char);
		memcpy(frame->procs[pi].name,buffer+cursor,namelen);
		cursor+=namelen;
		frame->procs[pi].usertime=deserialise8(buffer+cursor);
		cursor+=8;
		frame->procs[pi].systime=deserialise8(buffer+cursor);
		cursor+=8;
		frame->procs[pi].rss=deserialise8(buffer+cursor);
		cursor+=8;
	}

	process_frame(frame);
	for(i64 i=0;i<numpids;i++)free(frame->procs[i].name);
	free(frame->procs);
	free(frame);
	return cursor;

insufficient_data:
	if(frame->procs){
		for(i64 i=0;i<=pi;i++){
			if(frame->procs[i].name)free(frame->procs[i].name);
		}
		free(frame->procs);
	}
	free(frame);
	return 0;
}

static void frame_data_sink(u8 *newdata,i64 newdatalen,void *payload){
	(void)payload;
	static u8 *buffer=NULL;
	static i64 buffersize=0,bufferlen=0;

	fprintf(stderr,"frame_data_sink(len=%" PRIi64 ")\n",newdatalen);

	if(buffer==NULL){
		buffersize=4096;
		buffer=malloc(buffersize,u8);
	}

	if(bufferlen+newdatalen>buffersize){
		do buffersize*=2; while(bufferlen+newdatalen>buffersize);
		buffer=realloc(buffer,buffersize,u8);
	}
	memcpy(buffer+bufferlen,newdata,newdatalen);
	bufferlen+=newdatalen;

	i64 cursor=0;
	while(cursor<bufferlen){
		i64 nr=read_frame(buffer+cursor,bufferlen-cursor);
		if(nr<=0)break;
		cursor+=nr;
	}
	if(cursor<bufferlen){
		memmove(buffer,buffer+cursor,bufferlen-cursor);
	}
	bufferlen-=cursor;
}

int main(int argc,char **argv){
	if(argc!=2&&argc!=3){
		fprintf(stderr,"Usage: %s <ip addr> [port=57575]\n",argv[0]);
		return 1;
	}

	struct addrinfo hints,*res;
	memset(&hints,0,sizeof(hints));
	hints.ai_family=AF_UNSPEC;
	hints.ai_socktype=SOCK_STREAM;
	int ret=getaddrinfo(argv[1],argc==3?argv[2]:"57575",&hints,&res);
	if(ret!=0){
		fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(ret));
		return 1;
	}

	int sock=-1;
	for(struct addrinfo *r=res;r!=NULL;r=r->ai_next){
		char buf[256];
		inet_ntop(r->ai_family,&((struct sockaddr_in*)r->ai_addr)->sin_addr,buf,sizeof buf);
		fprintf(stderr,"Trying: %s\n",buf);
		sock=socket(r->ai_family,r->ai_socktype,r->ai_protocol);
		if(sock<0){
			sock=-1;
			perror("socket");
			continue;
		}
		if(connect(sock,r->ai_addr,r->ai_addrlen)<0){
			close(sock);
			sock=-1;
			perror("connect");
			continue;
		}
		break;
	}
	freeaddrinfo(res);
	if(sock==-1)return 1;
	fprintf(stderr,"Connected.\n");

	struct line_reader *liner=line_reader_init(STDIN_FILENO);
	struct unzbuffer *z=unzbuffer_init(frame_data_sink,NULL);

	bool haderror=false;
	while(true){
		fd_set inset;
		FD_ZERO(&inset);
		FD_SET(STDIN_FILENO,&inset);
		FD_SET(sock,&inset);
		int ret=select(sock+1,&inset,NULL,NULL,NULL);
		if(ret<0){
			if(errno==EINTR)continue;
			perror("select");
			break;
		}

		if(FD_ISSET(STDIN_FILENO,&inset)){
			char buf[1024];
			i64 nr=read(STDIN_FILENO,buf,sizeof buf);
			if(nr<0){
				if(errno==EINTR)continue;
				perror("read");
				break;
			}
			if(nr==0)break;  // EOF
			line_reader_supply_data(liner,buf,nr);
			char *line;
			while((line=line_reader_get_line(liner,true))!=NULL){
				if(!writeall(sock,line,strlen(line))){
					fprintf(stderr,"Socket write error\n");
					goto cleanup;
				}
				// printf("Written <%s>\n",line);
				free(line);
			}
		}

		if(FD_ISSET(sock,&inset)){
			u8 buf[1024];
			i64 nr=read(sock,buf,sizeof buf);
			if(nr<0){
				if(errno==EINTR)continue;
				perror("read");
				break;
			}
			if(nr==0){
				fprintf(stderr,"End-of-file on socket\n");
				break;
			}
			fprintf(stderr,"unzbuffer_write(nr=%" PRIi64 ")\n",nr);
			if(!unzbuffer_write(z,buf,nr)){
				haderror=true;
				break;
			}
		}
	}

cleanup:
	line_reader_destroy(liner);
	if(!unzbuffer_finish_destroy(z))return 1;
	if(haderror)return 1;
	return 0;
}