blob: 3b93a5a8eb57e33909e76c1f87faad2fe433946f
1 | #include <stdio.h> |
2 | #include <ctype.h> |
3 | #include <stdlib.h> |
4 | #include <string.h> |
5 | #include <unistd.h> |
6 | #include <signal.h> |
7 | #include <errno.h> |
8 | #include <fcntl.h> |
9 | #include <syslog.h> |
10 | #include <netdb.h> |
11 | #include <utmp.h> |
12 | #include <pwd.h> |
13 | #include <setjmp.h> |
14 | #include <sys/param.h> |
15 | #include <sys/types.h> |
16 | #include <sys/wait.h> |
17 | #include <sys/time.h> |
18 | #include <sys/resource.h> |
19 | #include <sys/stat.h> |
20 | #include <sys/socket.h> |
21 | #include <netinet/in.h> |
22 | #include <arpa/inet.h> |
23 | #include <cutils/properties.h> |
24 | #include <sys/un.h> |
25 | |
26 | #include <android/log.h> |
27 | #include <netwrapper.h> |
28 | |
29 | #include "pppoe_status.h" |
30 | |
31 | #define LOCAL_TAG "PPPOE_WRAPPER" |
32 | |
33 | static pid_t read_pid(const char *pidfile) |
34 | { |
35 | FILE *fp; |
36 | pid_t pid; |
37 | |
38 | if ((fp = fopen(pidfile, "r")) == NULL) { |
39 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
40 | "failed to open %s (%s)\n", pidfile, strerror(errno)); |
41 | errno = ENOENT; |
42 | return 0; |
43 | } |
44 | |
45 | if (fscanf(fp, "%d", &pid) != 1) { |
46 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
47 | "failed to read pid, make pid as 0\n"); |
48 | pid = 0; |
49 | } |
50 | fclose(fp); |
51 | |
52 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
53 | "read_pid: %d\n", pid); |
54 | |
55 | return pid; |
56 | } |
57 | |
58 | static int pppoe_disconnect_handler(char *request) |
59 | { |
60 | pid_t pid; |
61 | int ret; |
62 | |
63 | pid = read_pid(PPPOE_PIDFILE); |
64 | if ( 0 == pid ) { |
65 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
66 | "failed to stop ppp for no pid got\n" ); |
67 | return -1; |
68 | } |
69 | |
70 | ret = kill(pid, 0); |
71 | if ( 0 != ret ) { |
72 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
73 | "process(#%d) died already???\n", pid ); |
74 | return -1; |
75 | } |
76 | |
77 | /* |
78 | The signals SIGKILL and SIGSTOP cannot |
79 | be caught, blocked, or ignored. |
80 | So send SIGUSR1 to notify pppoe to send PADT. |
81 | */ |
82 | ret = kill(pid, SIGUSR1); |
83 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
84 | "Send SIGUSR1 to pid(#%d), ret = %d\n", pid, ret ); |
85 | |
86 | /* |
87 | If no sleep before send SIGKILL, pppoe will just being killed |
88 | rather than sending PADT. |
89 | */ |
90 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
91 | "sleep before send SIGKILL to pid(#%d)\n", pid ); |
92 | |
93 | sleep(5); |
94 | |
95 | ret = kill(pid, SIGKILL); |
96 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
97 | "Send SIGKILL to pid(#%d), ret = %d\n", pid, ret ); |
98 | |
99 | unlink(PPPOE_PIDFILE); |
100 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
101 | "removed %s\n", PPPOE_PIDFILE ); |
102 | |
103 | return 0; |
104 | } |
105 | |
106 | extern void sendSavedPADT(char *padt_file); |
107 | static int pppoe_terminate_handler(char *request) |
108 | { |
109 | int i; |
110 | |
111 | for ( i = 0; i < 3; i++ ) { |
112 | sendSavedPADT(_ROOT_PATH "/etc/ppp/padt_bin"); |
113 | sleep(1); |
114 | } |
115 | |
116 | return 0; |
117 | } |
118 | |
119 | |
120 | static int pppoe_connect_handler(char *request) |
121 | { |
122 | return system(request); |
123 | } |
124 | |
125 | |
126 | int main(int argc, char* argv[]) |
127 | { |
128 | netwrapper_register_handler("ppp-stop", pppoe_disconnect_handler); |
129 | netwrapper_register_handler("ppp-terminate", pppoe_terminate_handler); |
130 | netwrapper_register_handler("pppd pty", pppoe_connect_handler); |
131 | netwrapper_main(PPPOE_WRAPPER_SERVER_PATH); |
132 | |
133 | return 0; |
134 | } |
135 | |
136 | |
137 | |
138 | |
139 | |
140 |