-
Notifications
You must be signed in to change notification settings - Fork 1
/
sys.c
71 lines (56 loc) · 1.3 KB
/
sys.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
#include "sys.h"
int run_cmd(const char *cmd, char *result, int len)
{
FILE *fp = NULL;
fp = popen(cmd, "r");
if (NULL == fp) {
printf("Error:popen '%s' failed!\n", cmd);
return -1;
}
if (result)
fgets(result, len, fp);
pclose(fp);
if (result)
return strlen(result);
return 0;
}
int pru_start(char *pru)
{
int i = 0;
char str[64] = "";
for (i=0; i<2; i++) {
sprintf(str, "cat %s/state", pru);
run_cmd(str, str, 64);
if (!strcmp(str, "running\n")) {
printf("running:%s\n", pru);
break;
}
sprintf(str, "echo start > %s/state", pru);
printf("%s\n", str);
run_cmd(str, str, 64);
}
if (i >= 2) {
return -1;
}
return 0;
}
int pru_stop(char *pru)
{
int i = 0;
char str[64] = "";
for (i=0; i<2; i++) {
sprintf(str, "cat %s/state", pru);
run_cmd(str, str, 64);
if (!strcmp(str, "offline\n")) {
printf("offline:%s\n", pru);
break;
}
sprintf(str, "echo stop > %s/state", pru);
printf("%s\n", str);
run_cmd(str, str, 64);
}
if (i >= 2) {
return -1;
}
return 0;
}