-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathe_loop.c
144 lines (123 loc) · 2.59 KB
/
e_loop.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
/*
* e_loop helper program
* Copyright (c) 2015, Qualcomm Atheros, Inc.
* All Rights Reserved.
* Licensed under the Clear BSD license. See README for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
char *e_loop_cmd_file = "/data/local/hs2/To_Phone/tag_file";
char *e_loop_log_file = "/data/local/hs2/To_Phone/Logs/e_loop.log";
static const char *log_file = NULL;
static const char *tag_file = NULL;
int main(int argc, char *argv[])
{
char *buf = NULL;
char *cmd = NULL;
long pos;
int c, ret;
size_t len = 0;
FILE *f, *f2 = NULL;
/* Set the defaults */
log_file = e_loop_log_file;
tag_file = e_loop_cmd_file;
for (;;) {
c = getopt(argc, argv, "l:t:");
if (c < 0)
break;
switch (c) {
case 'l':
log_file = optarg;
break;
case 't':
tag_file = optarg;
break;
default:
printf("usage: e_loop [-l<log_filename>] [-t<tag_filename>]\n");
exit(0);
break;
}
}
/* Main command event loop */
while (1) {
/* Wait for a tag_file with a command to process */
while (!(f = fopen(tag_file, "rb")))
sleep(1);
len = 80;
/* Figure out how long the file is */
if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
fclose(f);
return -1;
}
len = pos;
if (fseek(f, 0, SEEK_SET) < 0) {
fclose(f);
return -1;
}
buf = malloc(len);
if (!buf) {
fclose(f);
return -1;
}
/* Read up the command line */
if (fread(buf, 1, len, f) != len) {
fclose(f);
free(buf);
return -1;
}
fclose(f);
buf[len - 1] = '\0';
if (log_file) {
len = strlen(buf) + strlen(log_file) + 7;
cmd = malloc(len);
if (cmd == NULL) {
free(buf);
return -1;
}
ret = snprintf(cmd, len, "%s > %s", buf, log_file);
if (ret < 0 || (size_t) ret >= len) {
free(buf);
free(cmd);
return -1;
}
free(buf);
buf = NULL;
} else {
cmd = buf;
}
cmd[len - 1] = '\0';
/*
* This string "cmd" will contain the command passed in by
* hs20-action.sh. And the name of the "logfile". Which can be
* monitored for the result.
*/
ret = system(cmd);
if (WIFEXITED(ret)) {
ret = WEXITSTATUS(ret);
}
if ((f2 = fopen(log_file, "a")) == NULL) {
free(cmd);
return -1;
}
if (fprintf(f2,"\nELOOP_CMD : %s\n", cmd) <= 0) {
fclose(f2);
free(cmd);
return -1;
}
if (fprintf(f2,"\nELOOP_CMD_STATUS : %d\n", ret) <= 0) {
fclose(f2);
free(cmd);
return -1;
}
/* Only free the cmd buffer. It is all that is left allocated */
free(cmd);
fclose(f2);
/* Clean up */
unlink(tag_file);
}
return ret;
}