Initialize smart traffic control iptables package
[platform/core/connectivity/stc-iptables.git] / test / stc_ipt_sys.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 <glib.h>
18 #include <gio/gio.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <sys/wait.h>
24
25 #include "stc_ipt_test.h"
26 #include "stc_ipt_sys.h"
27 #include "stc_ipt_menu.h"
28
29 #define MAX_ERR_BUFFER   256
30
31 int stc_ipt_exe_cmd(const char *cmd)
32 {
33         int pid = 0;
34         int status = 0;
35         int ret = 0;
36         char err_buf[MAX_ERR_BUFFER] = { 0, };
37         gchar **args = NULL;
38
39         if (cmd == NULL)
40                 return STC_ERROR_INVALID_PARAMETER;
41
42         errno = 0;
43         args = g_strsplit_set(cmd, " ", -1);
44         pid = fork();
45
46         if (pid == 0) {
47                 errno = 0;
48                 ret = execv(args[0], args);
49                 if (ret) {
50                         strerror_r(errno, err_buf, MAX_ERR_BUFFER);
51                         msg("Failed to exe cmd " LOG_RED "[%s]" LOG_END, err_buf);
52                         g_strfreev(args);
53                 }
54                 exit(ret);
55         } else if (pid > 0) {
56                 if (waitpid(pid, &status, 0) < 0) {
57                         strerror_r(errno, err_buf, MAX_ERR_BUFFER);
58                         msg("Can't wait for a pid " LOG_RED "[%d %d %s]" LOG_END,
59                                 pid, status, err_buf);
60                         g_strfreev(args);
61                         return STC_ERROR_INVALID_OPERATION;
62                 }
63                 g_strfreev(args);
64                 return STC_ERROR_NONE;
65         }
66
67         strerror_r(errno, err_buf, MAX_ERR_BUFFER);
68         msg("Failed to fork " LOG_RED "[%s]" LOG_END, err_buf);
69         g_strfreev(args);
70
71         return STC_ERROR_OPERATION_FAILED;
72 }