blob: 800f9e406bd31720ba81c1b0f83360dae851985e
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 <netutils/ifc.h> |
28 | |
29 | #include <netwrapper.h> |
30 | |
31 | #include "pppoe_status.h" |
32 | |
33 | #define LOCAL_TAG "PPPOE_WRAPPER" |
34 | |
35 | static pid_t read_pid(const char *pidfile) |
36 | { |
37 | FILE *fp; |
38 | pid_t pid; |
39 | |
40 | if ((fp = fopen(pidfile, "r")) == NULL) { |
41 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
42 | "failed to open %s (%s)\n", pidfile, strerror(errno)); |
43 | errno = ENOENT; |
44 | return 0; |
45 | } |
46 | |
47 | if (fscanf(fp, "%d", &pid) != 1) { |
48 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
49 | "failed to read pid, make pid as 0\n"); |
50 | pid = 0; |
51 | } |
52 | fclose(fp); |
53 | |
54 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
55 | "read_pid: %d\n", pid); |
56 | |
57 | return pid; |
58 | } |
59 | |
60 | static int pppoe_disconnect_handler(char *request) |
61 | { |
62 | pid_t pid; |
63 | int ret; |
64 | |
65 | pid = read_pid(PPPOE_PIDFILE); |
66 | if ( 0 == pid ) { |
67 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
68 | "failed to stop ppp for no pid got\n" ); |
69 | return -1; |
70 | } |
71 | |
72 | ret = kill(pid, 0); |
73 | if ( 0 != ret ) { |
74 | __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, |
75 | "process(#%d) died already???\n", pid ); |
76 | return -1; |
77 | } |
78 | |
79 | /* |
80 | The signals SIGKILL and SIGSTOP cannot |
81 | be caught, blocked, or ignored. |
82 | So send SIGUSR1 to notify pppoe to send PADT. |
83 | */ |
84 | ret = kill(pid, SIGUSR1); |
85 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
86 | "Send SIGUSR1 to pid(#%d), ret = %d\n", pid, ret ); |
87 | |
88 | /* |
89 | If no sleep before send SIGKILL, pppoe will just being killed |
90 | rather than sending PADT. |
91 | */ |
92 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
93 | "sleep before send SIGKILL to pid(#%d)\n", pid ); |
94 | |
95 | sleep(5); |
96 | |
97 | ret = kill(pid, SIGKILL); |
98 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
99 | "Send SIGKILL to pid(#%d), ret = %d\n", pid, ret ); |
100 | |
101 | unlink(PPPOE_PIDFILE); |
102 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
103 | "removed %s\n", PPPOE_PIDFILE ); |
104 | |
105 | return 0; |
106 | } |
107 | |
108 | extern void sendSavedPADT(char *padt_file); |
109 | static int pppoe_terminate_handler(char *request) |
110 | { |
111 | int i; |
112 | |
113 | for ( i = 0; i < 3; i++ ) { |
114 | sendSavedPADT(_ROOT_PATH "/etc/ppp/padt_bin"); |
115 | sleep(1); |
116 | } |
117 | |
118 | return 0; |
119 | } |
120 | |
121 | |
122 | static int pppoe_connect_handler(char *request) |
123 | { |
124 | if (strstr(request, "eth0")) { |
125 | __android_log_print(ANDROID_LOG_INFO, LOCAL_TAG, |
126 | "FIXME!!! SHOULD NOT clear eth0 ip address. Reference to bug#98924.\n"); |
127 | ifc_init(); |
128 | ifc_set_addr("eth0",0); |
129 | ifc_close(); |
130 | } |
131 | |
132 | return system(request); |
133 | } |
134 | |
135 | |
136 | int main(int argc, char* argv[]) |
137 | { |
138 | netwrapper_register_handler("ppp-stop", pppoe_disconnect_handler); |
139 | netwrapper_register_handler("ppp-terminate", pppoe_terminate_handler); |
140 | netwrapper_register_handler("pppd pty", pppoe_connect_handler); |
141 | netwrapper_main(PPPOE_WRAPPER_SERVER_PATH); |
142 | |
143 | return 0; |
144 | } |
145 | |
146 | |
147 | |
148 | |
149 | |
150 |