forked from idor/ti-utils
-
Notifications
You must be signed in to change notification settings - Fork 13
/
wl_logproxy.c
executable file
·434 lines (376 loc) · 9.24 KB
/
wl_logproxy.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
*
* Description: wl Firmware Log Service for off-line and real-time log viewing
* Maintainer: [email protected]
* Last Update: 11/09/2012
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <signal.h>
#include <errno.h>
#include <pthread.h>
/* Defines */
#define INVALID_HANDLE -1
#define FILE_NAME "wl_fwlog.log"
#define FILE_NAME_PREV "wl_fwlog_prev.log"
#define BUFFER_SIZE 1024
#define FILE_POLL_INTERVAL 5 // in seconds
#define KEEP_ALIVE_INTERVAL 30 // in seconds
#define MAX_PATH 256
#define MAX_FILE_SIZE 100000000 // Max file size is 100mb
/***********/
/* Globals */
int socket_connected = 0;
int socket_enabled = 1;
/***********/
void error(const char *msg)
{
printf("FW LOGGER ERROR: %s\r\n", msg);
}
void sigpipe_handler()
{
error("sigpipe caught!\r\n");
socket_connected = 0;
}
// ************************************************** //
//
// Function name: create_listen_socket
//
// Description: Listen on a local port for remote
// client connections
//
//
//
//
// ************************************************** //
int create_listen_socket(int listen_port)
{
struct sockaddr_in serv_addr, cli_addr;
int sock_flags = 0;
int listen_sock = INVALID_HANDLE;
if(listen_port == 0)
{
socket_enabled = 0;
return INVALID_HANDLE;
}
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock == INVALID_HANDLE)
{
error("opening socket\r\n");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(listen_port);
// Bind to local port
if (bind(listen_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
error("on binding\r\n");
exit(1);
}
listen(listen_sock,1);
// Set the callback for socket timeout signal handling
signal(SIGPIPE,sigpipe_handler);
// Set the listening socket to non-blocking mode
sock_flags = fcntl(listen_sock, F_GETFL, 0);
fcntl(listen_sock, F_SETFL, sock_flags | O_NONBLOCK);
return listen_sock;
}
// ************************************************** //
//
// Function Name: accept_client
//
// Description:
// Monitor client activity and allow new
// connections if the client is disconnected
//
//
// ************************************************** //
int accept_client(int listen_sock)
{
int client_sock = INVALID_HANDLE;
socklen_t clilen;
struct sockaddr_in cli_addr;
struct timeval tv;
tv.tv_sec = KEEP_ALIVE_INTERVAL;
tv.tv_usec = 0;
clilen = sizeof(cli_addr);
// Non-Blocking accept for remote client connections
client_sock = accept(listen_sock, (struct sockaddr *) &cli_addr, &clilen);
if(client_sock != INVALID_HANDLE)
{
printf("Client connected!\r\n");
if(setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)))
{
error("failed to set timeout for recv\r\n");
close(client_sock);
close(listen_sock);
exit(EXIT_FAILURE);
}
socket_connected = 1;
}
return client_sock;
}
// ************************************************** //
//
// Function Name: send_data
//
// Description:
// Send the binary logs to the remote
// client that is currently connected
//
//
// ************************************************** //
void send_data(int client_sock, char *buffer, int buf_size)
{
int sent_bytes = 0;
// Send the buffer to the remote client
if(socket_connected)
{
sent_bytes = 0;
while( buf_size > 0 )
{
sent_bytes = send(client_sock,(char *)(buffer + sent_bytes),buf_size,0);
if (sent_bytes < 0)
{
printf("Send failed! %d %d\n", buf_size, client_sock);
close(client_sock);
socket_connected = 0;
break;
}
buf_size -= sent_bytes;
}
}
}
// ************************************************** //
//
// Function Name: keep_alive
//
// Description:
// Verify that the remote client is
// alive by receiving keep alive data
//
//
// ************************************************** //
void keep_alive(int client_sock)
{
char ka_buff;
int n = 0;
// Verify keep alive
if(socket_connected)
{
n = recv(client_sock, &ka_buff, 1, 0);
if (n < 0)
{
error("Client disconnected");
close(client_sock);
socket_connected = 0;
}
}
}
// ************************************************** //
//
// Function Name: open_fwlog
//
// Description:
// Wait until the fwlog file is created
// and open it for read access
//
// Return:
// file handle to fwlog
//
// ************************************************** //
int open_fwlog(int fp_fwlog, char *file_path)
{
if(fp_fwlog == INVALID_HANDLE)
{
fp_fwlog = open(file_path, O_RDONLY);
}
return fp_fwlog;
}
// ************************************************** //
//
// Function Name: read_file
//
// Description:
// read the binary logs from the file
// into the buffer
//
// Return:
// file handle
//
// ************************************************** //
int read_file(int fp_file, char *buffer)
{
int n = 0;
bzero(buffer,BUFFER_SIZE);
// Attempt to read logs from the fwlog
n = read(fp_file,buffer,BUFFER_SIZE);
if (n < 0)
{
error("reading from file\r\n");
return INVALID_HANDLE;
}
return n;
}
void backup_file(int fp_bkp, char *buffer, int buf_size)
{
int wr_bytes = 0;
do
{
wr_bytes = write(fp_bkp, buffer, buf_size);
if(wr_bytes < 0)
{
error("writing to backup file!");
exit(EXIT_FAILURE);
}
}
while (wr_bytes < buf_size);
}
int create_file(char *filename)
{
int fp_bkplog;
#ifdef ANDROID
fp_bkplog = open(filename, O_RDWR|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR);
#else
fp_bkplog = open(filename, O_RDWR|O_TRUNC|O_CREAT, S_IREAD|S_IWRITE);
#endif
if (fp_bkplog == INVALID_HANDLE)
{
error("Could not open output log file");
exit(EXIT_FAILURE);
}
return fp_bkplog;
}
/*
void core_dump_thread( void *ptr)
{
char buffer[BUFFER_SIZE + 1] = {0}; // +1 for safety
int fp_fwdump = INVALID_HANDLE;
int fp_fwdumpbkp = INVALID_HANDLE;
int buf_size = 0;
while(1)
{
if(fp_fwdump == INVALID_HANDLE)
{
fp_fwdump = open(fp_fwdump, O_RDONLY);
continue;
}
if(fp_fwdumpbkp == INVALID_HANDLE)
{
fp_fwdumpbkp = open(filename, O_RDWR|O_TRUNC|O_CREAT, S_IREAD|S_IWRITE);
continue;
}
// Read the core_dump sysfs file - This is a blocking function
buf_size = read_file(fp_fwdump, buffer);
while( (buf_size = read_file(fp_fwdump, buffer)) != 0)
{
backup_file(fp_fwdumpbkp, buffer, buf_size);
}
close(fp_fwdumpbkp);
close(fp_fwdump);
}
pthread_exit(0);
}
*/
// ************************************************** //
//
// Function Name: main
//
// Description:
//
//
//
//
// ************************************************** //
int main(int argc, char *argv[])
{
int listen_sock, client_sock = INVALID_HANDLE;
char buffer[BUFFER_SIZE + 1] = {0}; // +1 for safety
int n = 0, buf_size = 0;
int fp_fwlog = INVALID_HANDLE;
int fp_bkplog = INVALID_HANDLE;
char filename[MAX_PATH + 1] = {0}; // +1 for safety
char filename_prev[MAX_PATH + 1] = {0}; // +1 for safety
uint max_file_size = MAX_FILE_SIZE;
struct stat st;
pthread_t thread;
if (argc < 3)
{
fprintf(stderr,"Usage: ./logProxy [listen port] [log file] [backup directory]\n");
exit(EXIT_FAILURE);
}
// Max file size is an optional value
if(argc > 4)
{
max_file_size = atol(argv[4]);
}
listen_sock = create_listen_socket(atoi(argv[1]));
//
// If we arrive here the listen socket was created successfully, otherwise we would have exited the process
//
// Create the backup file names
strncpy(filename,argv[3], MAX_PATH - sizeof(FILE_NAME));
strncpy(filename_prev,argv[3], MAX_PATH - sizeof(FILE_NAME_PREV));
strcat(filename, FILE_NAME);
strcat(filename_prev, FILE_NAME_PREV);
fp_bkplog = create_file(filename);
// Create the core_dump thread
//pthread_create(&thread, NULL, (void *) &core_dump_thread, NULL);
// Main Loop
while(1)
{
// Check on the client connection status
if ( (socket_enabled == 1) &&
(client_sock == INVALID_HANDLE || socket_connected == 0) )
{
client_sock = accept_client(listen_sock);
}
if(fp_fwlog != INVALID_HANDLE)
{
// Read the fwlog sysfs file - This is a blocking function
buf_size = read_file(fp_fwlog, buffer);
if(buf_size == INVALID_HANDLE)
{
error("driver unloaded: fwlog has been removed");
fp_fwlog = INVALID_HANDLE;
continue;
}
// Create a new file if the log file exceeds the max size
stat(filename, &st);
if(st.st_size >= max_file_size)
{
close(fp_bkplog);
rename(filename, filename_prev);
fp_bkplog = create_file(filename);
}
// keep alive
keep_alive(client_sock);
if(buf_size > 0)
{
// write the log data into the backup file
backup_file(fp_bkplog, buffer, buf_size);
// Try to send the logs to the remote client
send_data(client_sock, buffer, buf_size);
}
}
else
{
// To prevent high io/cpu usage sleep between attempts
sleep(FILE_POLL_INTERVAL);
// Try to open the fwlog file
fp_fwlog = open_fwlog(fp_fwlog, argv[2]);
}
}
return 0;
}