blob: d20cc481b6cc4e7730b1499d163ef03cbd62dd26 [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
37extern "C" int logwrap(int argc, const char **argv, int background);
38
San Mehatd5573d32010-01-19 17:19:41 -080039PppController::PppController() {
40 mTtys = new TtyCollection();
41 mPid = 0;
42}
43
44PppController::~PppController() {
45 TtyCollection::iterator it;
46
47 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
48 free(*it);
49 }
50 mTtys->clear();
51}
52
53int PppController::attachPppd(const char *tty, struct in_addr local,
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080054 struct in_addr remote, struct in_addr dns1,
55 struct in_addr dns2) {
San Mehatd5573d32010-01-19 17:19:41 -080056 pid_t pid;
57
58 if (mPid) {
Steve Block5ea0c052012-01-06 19:18:11 +000059 ALOGE("Multiple PPPD instances not currently supported");
San Mehatd5573d32010-01-19 17:19:41 -080060 errno = EBUSY;
61 return -1;
62 }
63
San Mehat18817622010-01-21 09:23:06 -080064 TtyCollection::iterator it;
65 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
66 if (!strcmp(tty, *it)) {
67 break;
68 }
69 }
70 if (it == mTtys->end()) {
Steve Block5ea0c052012-01-06 19:18:11 +000071 ALOGE("Invalid tty '%s' specified", tty);
San Mehat18817622010-01-21 09:23:06 -080072 errno = -EINVAL;
73 return -1;
74 }
75
San Mehatd5573d32010-01-19 17:19:41 -080076 if ((pid = fork()) < 0) {
Steve Block5ea0c052012-01-06 19:18:11 +000077 ALOGE("fork failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080078 return -1;
79 }
80
81 if (!pid) {
82 char *l = strdup(inet_ntoa(local));
83 char *r = strdup(inet_ntoa(remote));
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080084 char *d1 = strdup(inet_ntoa(dns1));
85 char *d2 = strdup(inet_ntoa(dns2));
San Mehatd5573d32010-01-19 17:19:41 -080086 char dev[32];
87 char *lr;
88
89 asprintf(&lr, "%s:%s", l, r);
90
San Mehat18817622010-01-21 09:23:06 -080091 snprintf(dev, sizeof(dev), "/dev/%s", tty);
San Mehatd5573d32010-01-19 17:19:41 -080092
93 // TODO: Deal with pppd bailing out after 99999 seconds of being started
94 // but not getting a connection
Robert Greenwalt74d8fdd2010-02-01 10:02:28 -080095 if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200",
Robert Greenwalt73ea55b2010-02-04 15:25:22 -080096 lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) NULL)) {
Steve Block5ea0c052012-01-06 19:18:11 +000097 ALOGE("execl failed (%s)", strerror(errno));
San Mehatd5573d32010-01-19 17:19:41 -080098 }
Steve Block5ea0c052012-01-06 19:18:11 +000099 ALOGE("Should never get here!");
San Mehatd5573d32010-01-19 17:19:41 -0800100 return 0;
101 } else {
102 mPid = pid;
103 }
104 return 0;
105}
106
107int PppController::detachPppd(const char *tty) {
108
109 if (mPid == 0) {
Steve Block5ea0c052012-01-06 19:18:11 +0000110 ALOGE("PPPD already stopped");
San Mehatd5573d32010-01-19 17:19:41 -0800111 return 0;
112 }
113
Steve Block7b984e32011-12-20 16:22:42 +0000114 ALOGD("Stopping PPPD services on port %s", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800115 kill(mPid, SIGTERM);
San Mehat18817622010-01-21 09:23:06 -0800116 waitpid(mPid, NULL, 0);
San Mehatd5573d32010-01-19 17:19:41 -0800117 mPid = 0;
Steve Block7b984e32011-12-20 16:22:42 +0000118 ALOGD("PPPD services on port %s stopped", tty);
San Mehatd5573d32010-01-19 17:19:41 -0800119 return 0;
120}
121
122TtyCollection *PppController::getTtyList() {
San Mehat18817622010-01-21 09:23:06 -0800123 updateTtyList();
San Mehatd5573d32010-01-19 17:19:41 -0800124 return mTtys;
125}
126
San Mehat18817622010-01-21 09:23:06 -0800127int PppController::updateTtyList() {
128 TtyCollection::iterator it;
129
130 for (it = mTtys->begin(); it != mTtys->end(); ++it) {
131 free(*it);
132 }
133 mTtys->clear();
134
135 DIR *d = opendir("/sys/class/tty");
136 if (!d) {
Steve Block5ea0c052012-01-06 19:18:11 +0000137 ALOGE("Error opening /sys/class/tty (%s)", strerror(errno));
San Mehat18817622010-01-21 09:23:06 -0800138 return -1;
139 }
140
141 struct dirent *de;
142 while ((de = readdir(d))) {
143 if (de->d_name[0] == '.')
144 continue;
145 if ((!strncmp(de->d_name, "tty", 3)) && (strlen(de->d_name) > 3)) {
146 mTtys->push_back(strdup(de->d_name));
147 }
148 }
149 closedir(d);
150 return 0;
151}