San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 1 | /* |
| 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 Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 20 | #include <string.h> |
San Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 21 | |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
San Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #include <dirent.h> |
| 28 | |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 29 | #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 | |
| 37 | extern "C" int logwrap(int argc, const char **argv, int background); |
| 38 | |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 39 | PppController::PppController() { |
| 40 | mTtys = new TtyCollection(); |
| 41 | mPid = 0; |
| 42 | } |
| 43 | |
| 44 | PppController::~PppController() { |
| 45 | TtyCollection::iterator it; |
| 46 | |
| 47 | for (it = mTtys->begin(); it != mTtys->end(); ++it) { |
| 48 | free(*it); |
| 49 | } |
| 50 | mTtys->clear(); |
| 51 | } |
| 52 | |
| 53 | int PppController::attachPppd(const char *tty, struct in_addr local, |
Robert Greenwalt | 74d8fdd | 2010-02-01 10:02:28 -0800 | [diff] [blame] | 54 | struct in_addr remote, struct in_addr dns1, |
| 55 | struct in_addr dns2) { |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 56 | pid_t pid; |
| 57 | |
| 58 | if (mPid) { |
| 59 | LOGE("Multiple PPPD instances not currently supported"); |
| 60 | errno = EBUSY; |
| 61 | return -1; |
| 62 | } |
| 63 | |
San Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 64 | 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()) { |
| 71 | LOGE("Invalid tty '%s' specified", tty); |
| 72 | errno = -EINVAL; |
| 73 | return -1; |
| 74 | } |
| 75 | |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 76 | if ((pid = fork()) < 0) { |
| 77 | LOGE("fork failed (%s)", strerror(errno)); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | if (!pid) { |
| 82 | char *l = strdup(inet_ntoa(local)); |
| 83 | char *r = strdup(inet_ntoa(remote)); |
Robert Greenwalt | 74d8fdd | 2010-02-01 10:02:28 -0800 | [diff] [blame] | 84 | char *d1 = strdup(inet_ntoa(dns1)); |
| 85 | char *d2 = strdup(inet_ntoa(dns2)); |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 86 | char dev[32]; |
| 87 | char *lr; |
| 88 | |
| 89 | asprintf(&lr, "%s:%s", l, r); |
| 90 | |
San Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 91 | snprintf(dev, sizeof(dev), "/dev/%s", tty); |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 92 | |
| 93 | // TODO: Deal with pppd bailing out after 99999 seconds of being started |
| 94 | // but not getting a connection |
Robert Greenwalt | 74d8fdd | 2010-02-01 10:02:28 -0800 | [diff] [blame] | 95 | if (execl("/system/bin/pppd", "/system/bin/pppd", "-detach", dev, "115200", |
Robert Greenwalt | 73ea55b | 2010-02-04 15:25:22 -0800 | [diff] [blame] | 96 | lr, "ms-dns", d1, "ms-dns", d2, "lcp-max-configure", "99999", (char *) NULL)) { |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 97 | LOGE("execl failed (%s)", strerror(errno)); |
| 98 | } |
| 99 | LOGE("Should never get here!"); |
| 100 | return 0; |
| 101 | } else { |
| 102 | mPid = pid; |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int PppController::detachPppd(const char *tty) { |
| 108 | |
| 109 | if (mPid == 0) { |
| 110 | LOGE("PPPD already stopped"); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 114 | ALOGD("Stopping PPPD services on port %s", tty); |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 115 | kill(mPid, SIGTERM); |
San Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 116 | waitpid(mPid, NULL, 0); |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 117 | mPid = 0; |
Steve Block | 7b984e3 | 2011-12-20 16:22:42 +0000 | [diff] [blame] | 118 | ALOGD("PPPD services on port %s stopped", tty); |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | TtyCollection *PppController::getTtyList() { |
San Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 123 | updateTtyList(); |
San Mehat | d5573d3 | 2010-01-19 17:19:41 -0800 | [diff] [blame] | 124 | return mTtys; |
| 125 | } |
| 126 | |
San Mehat | 1881762 | 2010-01-21 09:23:06 -0800 | [diff] [blame] | 127 | int 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) { |
| 137 | LOGE("Error opening /sys/class/tty (%s)", strerror(errno)); |
| 138 | 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 | } |