-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobileconnd.c
88 lines (77 loc) · 2.26 KB
/
mobileconnd.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
/**
* \file mobileconnd.c
* \brief Guess internet connection status by pinging google.com
* and call mobile connection dialing script if needed.
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
volatile sig_atomic_t DONE = 0;
pid_t CHILD_PID = -1;
// TODO: Modularize?
const char* const DIALSCRIPT = "autowvdial";
const char* const MODEM = "/dev/gsmmodem";
const char* const DIALER = "LTE";
const char* const PIN = "00000000";
const char* const TIMEOUT = "60";
void terminate(int signo)
{
if(signo == SIGTERM || signo == SIGINT)
{
DONE = 1;
kill(CHILD_PID, SIGTERM);
}
}
int main(int argc, char* argv[])
{
setbuf(stdout, NULL);
printf("starting mobileconnd\n");
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = terminate;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
while(!DONE)
{
int status = system("ping -c 1 -I ppp0 google.com > /dev/null");
if(status != EXIT_SUCCESS)
{
printf("ping failed\n");
// Ping failed but we already have a child.
// Did the dialer script hang or error?
// Kill the child and spawn a new one.
if(CHILD_PID != -1)
{
printf("autowvdial already running, killing...\n");
kill(CHILD_PID, SIGTERM);
sleep(5); // Grace period.
kill(CHILD_PID, SIGKILL);
CHILD_PID = -1;
printf("autowvdial killed\n");
}
if((CHILD_PID = fork()) < 0)
{
perror("fork error\n");
exit(EXIT_FAILURE);
}
else if(CHILD_PID == 0)
{
printf("starting autowvdial\n");
execlp(DIALSCRIPT, DIALSCRIPT, MODEM, PIN, "-d",
DIALER, "-t", TIMEOUT, (char*)NULL);
perror("execlp error\n");
_exit(EXIT_FAILURE);
}
}
else
{
printf("ping succesful\n");
}
printf("sleeping for 5 minutes...\n");
sleep(60 * 5);
}
printf("done, exiting");
return 0;
}