summaryrefslogtreecommitdiff
path: root/controller/main.c
blob: be13773625e29c3ccae55740e666b84dab404fe6 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#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;
}

FILE *logfile=NULL;

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(logfile,"{\"stamp\":[%" PRIi64 ",%d],\"procs\":[",
			(i64)frame->stamp.tv_sec,frame->stamp.tv_usec);
	for(i64 i=0;i<frame->numpids;i++){
		const struct frame_process *p=&frame->procs[i];
		if(i!=0)fputc(',',logfile);
		fprintf(logfile,"{\"pid\":%d,\"name\":\"",p->pid);
		for(i64 j=0;j<p->namelen;j++){
			if(p->name[j]>=32&&p->name[j]<=126&&p->name[j]!='"'&&p->name[j]!='\\'){
				fputc(p->name[j],logfile);
			} else {
				fprintf(logfile,"\\x%c%c",
						"0123456789abcdef"[(unsigned char)p->name[j]/16],
						"0123456789abcdef"[(unsigned char)p->name[j]%16]);
			}
		}
		fprintf(logfile,"\",\"usertime\":%" PRIi64 ",\"systime\":%" PRIi64 ",\"rss\":%" PRIi64 "}",
				p->usertime,p->systime,p->rss);
	}
	fprintf(logfile,"]}\n");

	/*char buf[26];
	memcpy(buf,ctime(&frame->stamp.tv_sec),26);
	buf[24]='\0';
	fprintf(stderr,"%s: (%" PRIi64 ")\n",buf,frame->numpids);
	for(i64 i=0;i<frame->numpids;i++){
		const struct frame_process *p=&frame->procs[i];
		fprintf(stderr,"  (%d) \"%s\" u=%.2f s=%.2f rss=%" PRIi64 "\n",
				p->pid,p->name,p->usertime/1000000000.0,p->systime/1000000000.0,p->rss);
	}*/
}

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);
		frame->procs[pi].name[namelen]='\0';
		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;
}

static int connect_server(const char *logfname,const char *ipaddr,const char *port){
	logfile=fopen(logfname,"w");
	if(logfile==NULL){
		fprintf(stderr,"Cannot open log file '%s'\n",logfname);
		return 1;
	}

	struct addrinfo hints,*res;
	memset(&hints,0,sizeof(hints));
	hints.ai_family=AF_UNSPEC;
	hints.ai_socktype=SOCK_STREAM;
	int ret=getaddrinfo(ipaddr,port,&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;
}

static int read_zz_file(const char *zzfname,const char *logfname){
	logfile=fopen(logfname,"w");
	if(logfile==NULL){
		fprintf(stderr,"Cannot open log file '%s'\n",logfname);
		return 1;
	}

	FILE *f=fopen(zzfname,"r");
	if(f==NULL){
		fprintf(stderr,"Cannot open zz file '%s'\n",zzfname);
		return 1;
	}

	struct unzbuffer *z=unzbuffer_init(frame_data_sink,NULL);
	u8 buf[4096];
	while(true){
		i64 nr=fread(buf,1,sizeof buf,f);
		if(nr<=0){
			if(ferror(f)){
				fprintf(stderr,"An error occurred reading the zz file '%s'\n",zzfname);
				return 1;
			}
			break;
		}
		if(!unzbuffer_write(z,buf,nr)){
			return 1;
		}
	}
	if(!unzbuffer_finish_destroy(z))return 1;
	return 0;
}

int main(int argc,char **argv){
	if(argc<=1){
		fprintf(stderr,"Usage:\n");
		fprintf(stderr,"  %s connect <logfile> <ip addr> [port=57575]\n",argv[0]);
		fprintf(stderr,"  %s read <zzfile> <logfile>\n",argv[0]);
		fprintf(stderr,"<logfile> will probably contain JSON.\n");
		fprintf(stderr,"<zzfile> should contain zlib-compressed protocol data.\n");
		return 1;
	}

	if(strcmp(argv[1],"connect")==0){
		if(argc<4||argc>5){
			fprintf(stderr,"Invalid number of arguments, run without arguments for usage\n");
			return 1;
		}
		const char *logfname=argv[2];
		const char *ipaddr=argv[3];
		const char *port=argc==5?argv[4]:"57575";
		return connect_server(logfname,ipaddr,port);
	} else if(strcmp(argv[1],"read")==0){
		if(argc!=4){
			fprintf(stderr,"Invalid number of arguments, run without arguments for usage\n");
			return 1;
		}
		const char *zzfname=argv[2];
		const char *logfname=argv[3];
		return read_zz_file(zzfname,logfname);
	} else {
		fprintf(stderr,"Invalid command '%s'; run without arguments for usage\n",argv[1]);
		return 1;
	}
}