blob: 6b54c31b5dff537888e2ba21ecb4a369547cb81d [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>
Olivier Baillyff2c0d82010-11-17 11:45:07 -080020#include <string.h>
San Mehat18817622010-01-21 09:23:06 -080021
San Mehatd5573d32010-01-19 17:19:41 -080022#include <sys/socket.h>
23#include <sys/stat.h>
San Mehat18817622010-01-21 09:23:06 -080024#include <sys/types.h>
25#include <sys/wait.h>
26
27#include <dirent.h>
28
San Mehatd5573d32010-01-19 17:19:41 -080029#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#define LOG_TAG "PppController"
33#include <cutils/log.h>
34
35#include "PppController.h"
36
San Mehatd5573d32010-01-19 17:19:41 -080037PppController::PppController() {
38 mTtys = new TtyCollection();
39 mPid = 0;
40}
41
42PppController::~PppController() {
43 TtyCollection::iterator it;
44
45 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
46 free(*it);
47 }
48 mTtys->clear();
49}
50
51int PppController::attachPppd(const char *tty, struct in_addr local,
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080052 struct in_addr remote, struct in_addr dns1,
53 struct in_addr dns2) {
San Mehatd5573d32010-01-19 17:19:41 -080054 pid_t pid;
55
56 if (mPid) {
Steve Block5ea0c052012-01-06 19:18:11 +000057 ALOGE("Multiple PPPD instances not currently supported");
San Mehatd5573d32010-01-19 17:19:41 -080058 errno = EBUSY;
59 return -1;
60 }
61
San Mehat18817622010-01-21 09:23:06 -080062 TtyCollection::iterator it;
63 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
64 if (!strcmp(tty, *it)) {
65 break;
66 }
67 }
68 if (it == mTtys->end()) {
Steve Block5ea0c052012-01-06 19:18:11 +000069 ALOGE("Invalid tty '%s' specified", tty);
San Mehat18817622010-01-21 09:23:06 -080070 errno = -EINVAL;
71 return -1;
72 }
73
San Mehatd5573d32010-01-19 17:19:41 -080074 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000075 ALOGE("fork failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080076 return -1;
77 }
78
79 if (!pid) {
80 char *l = strdup(inet_ntoa(local));
81 char *r = strdup(inet_ntoa(remote));
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080082 char *d1 = strdup(inet_ntoa(dns1));
83 char *d2 = strdup(inet_ntoa(dns2));
San Mehatd5573d32010-01-19 17:19:41 -080084 char dev[32];
85 char *lr;
86
87 asprintf(&lr, "%s:%s", l, r);
88
San Mehat18817622010-01-21 09:23:06 -080089 snprintf(dev, sizeof(dev), "/dev/%s", tty);
San Mehatd5573d32010-01-19 17:19:41 -080090
91 // TODO: Deal with pppd bailing out after 99999 seconds of being started
92 // but not getting a connection
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080093 if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200",
Robert Greenwalt73ea55b2010-02-04 15:25:22 -080094 lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +000095 ALOGE("execl failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080096 }
Steve Block5ea0c052012-01-06 19:18:11 +000097 ALOGE("Should never get here!");
San Mehatd5573d32010-01-19 17:19:41 -080098 return 0;
99 } else {
100 mPid = pid;
101 }
102 return 0;
103}
104
105int PppController::detachPppd(const char *tty) {
106
107 if (mPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000108 ALOGE("PPPD already stopped");
San Mehatd5573d32010-01-19 17:19:41 -0800109 return 0;
110 }
111
Steve Block7b984e32011-12-20 16:22:42 +0000112 ALOGD("Stopping PPPD services on port %s", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800113 kill(mPid, SIGTERM);
San Mehat18817622010-01-21 09:23:06 -0800114 waitpid(mPid, NULL, 0);
San Mehatd5573d32010-01-19 17:19:41 -0800115 mPid = 0;
Steve Block7b984e32011-12-20 16:22:42 +0000116 ALOGD("PPPD services on port %s stopped", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800117 return 0;
118}
119
120TtyCollection *PppController::getTtyList() {
San Mehat18817622010-01-21 09:23:06 -0800121 updateTtyList();
San Mehatd5573d32010-01-19 17:19:41 -0800122 return mTtys;
123}
124
San Mehat18817622010-01-21 09:23:06 -0800125int PppController::updateTtyList() {
126 TtyCollection::iterator it;
127
128 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
129 free(*it);
130 }
131 mTtys->clear();
132
133 DIR *d = opendir("/sys/class/tty");
134 if (!d) {
Steve Block5ea0c052012-01-06 19:18:11 +0000135 ALOGE("Error opening /sys/class/tty (%s)", strerror(errno));
San Mehat18817622010-01-21 09:23:06 -0800136 return -1;
137 }
138
139 struct dirent *de;
140 while ((de = readdir(d))) {
141 if (de->d_name[0] == '.')
142 continue;
143 if ((!strncmp(de->d_name, "tty", 3)) && (strlen(de->d_name) > 3)) {
144 mTtys->push_back(strdup(de->d_name));
145 }
146 }
147 closedir(d);
148 return 0;
149}