San Mehat | 94447ca | 2009-05-13 11:54:16 -0700 | [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 | #include <errno.h> |
| 17 | |
| 18 | #define LOG_TAG "OpenVpnController" |
| 19 | #include <cutils/log.h> |
| 20 | #include <cutils/properties.h> |
| 21 | |
| 22 | #include "OpenVpnController.h" |
| 23 | |
| 24 | #define DAEMON_PROP_NAME "vpn.openvpn.status" |
| 25 | |
| 26 | OpenVpnController::OpenVpnController() : |
| 27 | VpnController() { |
| 28 | } |
| 29 | |
| 30 | int OpenVpnController::start() { |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | int OpenVpnController::stop() { |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | int OpenVpnController::enable() { |
| 39 | |
| 40 | // Validate configuration file |
| 41 | |
| 42 | // Validate key file |
| 43 | |
| 44 | if (startServiceDaemon()) |
| 45 | return -1; |
| 46 | |
| 47 | errno = -ENOSYS; |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | int OpenVpnController::startServiceDaemon() { |
| 52 | char status[PROPERTY_VALUE_MAX]; |
| 53 | int count = 100; |
| 54 | |
| 55 | property_set("ctl.start", "openvpn"); |
| 56 | sched_yield(); |
| 57 | |
| 58 | while (count-- > 0) { |
| 59 | if (property_get(DAEMON_PROP_NAME, status, NULL)) { |
| 60 | if (strcmp(status, "ok") == 0) |
| 61 | return 0; |
| 62 | else if (strcmp(DAEMON_PROP_NAME, "failed") == 0) |
| 63 | return -1; |
| 64 | } |
| 65 | usleep(200000); |
| 66 | } |
| 67 | property_set(DAEMON_PROP_NAME, "timeout"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | int OpenVpnController::stopServiceDaemon() { |
| 72 | char status[PROPERTY_VALUE_MAX] = {'\0'}; |
| 73 | int count = 50; |
| 74 | |
| 75 | if (property_get(DAEMON_PROP_NAME, status, NULL) && |
| 76 | !strcmp(status, "stopped")) { |
| 77 | LOGD("Service already stopped"); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | property_set("ctl.stop", "openvpn"); |
| 82 | sched_yield(); |
| 83 | |
| 84 | while (count-- > 0) { |
| 85 | if (property_get(DAEMON_PROP_NAME, status, NULL)) { |
| 86 | if (!strcmp(status, "stopped")) |
| 87 | break; |
| 88 | } |
| 89 | usleep(100000); |
| 90 | } |
| 91 | |
| 92 | if (!count) { |
| 93 | LOGD("Timed out waiting for openvpn to stop"); |
| 94 | errno = ETIMEDOUT; |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int OpenVpnController::disable() { |
| 102 | errno = -ENOSYS; |
| 103 | return -1; |
| 104 | } |