blob: f508f4ed6f34eac9b19986f71c2d0953c88650b0 [file] [log] [blame]
San Mehatd5573d32010-01-19 17:19:41 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <errno.h>
San Mehat18817622010-01-21 09:23:06 -080019#include <fcntl.h>
20
San Mehatd5573d32010-01-19 17:19:41 -080021#include <sys/socket.h>
22#include <sys/stat.h>
San Mehat18817622010-01-21 09:23:06 -080023#include <sys/types.h>
24#include <sys/wait.h>
25
26#include <dirent.h>
27
San Mehatd5573d32010-01-19 17:19:41 -080028#include <netinet/in.h>
29#include <arpa/inet.h>
30
31#define LOG_TAG "PppController"
32#include <cutils/log.h>
33
34#include "PppController.h"
35
36extern "C" int logwrap(int argc, const char **argv, int background);
37
San Mehatd5573d32010-01-19 17:19:41 -080038PppController::PppController() {
39 mTtys = new TtyCollection();
40 mPid = 0;
41}
42
43PppController::~PppController() {
44 TtyCollection::iterator it;
45
46 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
47 free(*it);
48 }
49 mTtys->clear();
50}
51
52int PppController::attachPppd(const char *tty, struct in_addr local,
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080053 struct in_addr remote, struct in_addr dns1,
54 struct in_addr dns2) {
San Mehatd5573d32010-01-19 17:19:41 -080055 pid_t pid;
56
57 if (mPid) {
58 LOGE("Multiple PPPD instances not currently supported");
59 errno = EBUSY;
60 return -1;
61 }
62
San Mehat18817622010-01-21 09:23:06 -080063 TtyCollection::iterator it;
64 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
65 if (!strcmp(tty, *it)) {
66 break;
67 }
68 }
69 if (it == mTtys->end()) {
70 LOGE("Invalid tty '%s' specified", tty);
71 errno = -EINVAL;
72 return -1;
73 }
74
San Mehatd5573d32010-01-19 17:19:41 -080075 if ((pid = fork()) < 0) {
76 LOGE("fork failed (%s)", strerror(errno));
77 return -1;
78 }
79
80 if (!pid) {
81 char *l = strdup(inet_ntoa(local));
82 char *r = strdup(inet_ntoa(remote));
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080083 char *d1 = strdup(inet_ntoa(dns1));
84 char *d2 = strdup(inet_ntoa(dns2));
San Mehatd5573d32010-01-19 17:19:41 -080085 char dev[32];
86 char *lr;
87
88 asprintf(&lr, "%s:%s", l, r);
89
San Mehat18817622010-01-21 09:23:06 -080090 snprintf(dev, sizeof(dev), "/dev/%s", tty);
San Mehatd5573d32010-01-19 17:19:41 -080091
92 // TODO: Deal with pppd bailing out after 99999 seconds of being started
93 // but not getting a connection
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080094 if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200",
Robert Greenwalt73ea55b2010-02-04 15:25:22 -080095 lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) NULL)) {
San Mehatd5573d32010-01-19 17:19:41 -080096 LOGE("execl failed (%s)", strerror(errno));
97 }
98 LOGE("Should never get here!");
99 return 0;
100 } else {
101 mPid = pid;
102 }
103 return 0;
104}
105
106int PppController::detachPppd(const char *tty) {
107
108 if (mPid == 0) {
109 LOGE("PPPD already stopped");
110 return 0;
111 }
112
113 LOGD("Stopping PPPD services on port %s", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800114 kill(mPid, SIGTERM);
San Mehat18817622010-01-21 09:23:06 -0800115 waitpid(mPid, NULL, 0);
San Mehatd5573d32010-01-19 17:19:41 -0800116 mPid = 0;
San Mehat18817622010-01-21 09:23:06 -0800117 LOGD("PPPD services on port %s stopped", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800118 return 0;
119}
120
121TtyCollection *PppController::getTtyList() {
San Mehat18817622010-01-21 09:23:06 -0800122 updateTtyList();
San Mehatd5573d32010-01-19 17:19:41 -0800123 return mTtys;
124}
125
San Mehat18817622010-01-21 09:23:06 -0800126int PppController::updateTtyList() {
127 TtyCollection::iterator it;
128
129 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
130 free(*it);
131 }
132 mTtys->clear();
133
134 DIR *d = opendir("/sys/class/tty");
135 if (!d) {
136 LOGE("Error opening /sys/class/tty (%s)", strerror(errno));
137 return -1;
138 }
139
140 struct dirent *de;
141 while ((de = readdir(d))) {
142 if (de->d_name[0] == '.')
143 continue;
144 if ((!strncmp(de->d_name, "tty", 3)) && (strlen(de->d_name) > 3)) {
145 mTtys->push_back(strdup(de->d_name));
146 }
147 }
148 closedir(d);
149 return 0;
150}