OSDN Git Service

Launch android-clat via netd
[android-x86/system-netd.git] / ClatdController.cpp
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 #include <unistd.h>
18
19 #include <sys/types.h>
20 #include <sys/wait.h>
21
22 #define LOG_TAG "ClatdController"
23 #include <cutils/log.h>
24
25 #include "ClatdController.h"
26
27 ClatdController::ClatdController() {
28     mClatdPid = 0;
29 }
30
31 ClatdController::~ClatdController() {
32 }
33
34 int ClatdController::startClatd(char *interface) {
35     pid_t pid;
36
37     if(mClatdPid != 0) {
38         ALOGE("clatd already running");
39         errno = EBUSY;
40         return -1;
41     }
42
43     ALOGD("starting clatd");
44
45     if ((pid = fork()) < 0) {
46         ALOGE("fork failed (%s)", strerror(errno));
47         return -1;
48     }
49
50     if (!pid) {
51         char **args = (char **)malloc(sizeof(char *) * 4);
52         args[0] = (char *)"/system/bin/clatd";
53         args[1] = (char *)"-i";
54         args[2] = interface;
55         args[3] = NULL;
56
57         if (execv(args[0], args)) {
58             ALOGE("execv failed (%s)", strerror(errno));
59         }
60         ALOGE("Should never get here!");
61         free(args);
62         _exit(0);
63     } else {
64         mClatdPid = pid;
65         ALOGD("clatd started");
66     }
67
68     return 0;
69 }
70
71 int ClatdController::stopClatd() {
72     if (mClatdPid == 0) {
73         ALOGE("clatd already not running");
74         return -1;
75     }
76
77     ALOGD("Stopping clatd");
78
79     kill(mClatdPid, SIGTERM);
80     waitpid(mClatdPid, NULL, 0);
81     mClatdPid = 0;
82
83     ALOGD("clatd stopped");
84
85     return 0;
86 }
87
88 bool ClatdController::isClatdRunning() {
89     pid_t waitpid_status;
90     if(mClatdPid == 0) {
91         return false;
92     }
93     waitpid_status = waitpid(mClatdPid, NULL, WNOHANG);
94     if(waitpid_status != 0) {
95         // child exited or stopped, don't call waitpid on it again
96         mClatdPid = 0;
97     }
98     return waitpid_status == 0; // 0 while child is running
99 }