Merge "Added cap_dac_override capability" into tizen_4.0
[platform/core/connectivity/net-config.git] / src / clatd-handler.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <unistd.h>
21 #include <stdlib.h>
22
23 #include "log.h"
24 #include "util.h"
25 #include "network-state.h"
26 #include "clatd-handler.h"
27
28 #define CLAT_EXEC_PATH "/usr/sbin/clatd"
29 #define ROUTE_EXEC_PATH "/sbin/route"
30 #define KILLALL_EXEC_PATH "/usr/bin/killall"
31 #define IFCONFIG_EXEC_PATH "/sbin/ifconfig"
32
33 char g_ifname[32] = {0, };
34
35 int netconfig_clatd_enable(void)
36 {
37         int rv = 0;
38
39         if (g_ifname[0] != '\0') {
40                 rv = netconfig_clatd_disable();
41
42                 if (rv < 0) {
43                         DBG("Failed to disable existing clatd process");
44                         return -1;
45                 }
46         }
47
48         const char *if_name = netconfig_get_default_ifname();
49
50         if (if_name == NULL) {
51                 DBG("There is no interface name");
52                 return -1;
53         }
54
55         memset(g_ifname, 0, sizeof(g_ifname));
56         g_strlcat(g_ifname, if_name, 32);
57
58         const char *path = CLAT_EXEC_PATH;
59         char *const args[] = { "/usr/sbin/clatd", "-i", g_ifname, NULL };
60
61         rv = netconfig_execute_clatd(path, args);
62
63         if (rv < 0) {
64                 DBG("Failed to enable clatd process %d", rv);
65                 return -1;
66         }
67
68         DBG("Successfully enabled clatd process with %s interface", g_ifname);
69         return 0;
70 }
71
72 int netconfig_clatd_disable(void)
73 {
74         int rv = 0;
75
76         const char *path = KILLALL_EXEC_PATH;
77         char *const args[] = { "/usr/bin/kill -15", "clatd", NULL };
78         char *const envs[] = { NULL };
79
80         if (g_ifname[0] == '\0') {
81                 DBG("There is no clatd process");
82                 return -1;
83         }
84
85         memset(g_ifname, 0, sizeof(g_ifname));
86
87         rv = netconfig_execute_file(path, args, envs);
88
89         if (rv < 0) {
90                 DBG("Failed to disable clatd process %d", rv);
91                 return -1;
92         }
93
94         DBG("Successfully disable clatd process");;
95         return 0;
96 }