[Add] tethering client monitoring
[platform/core/connectivity/stc-manager.git] / src / helper / helper-cgroup.h
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 /*
18  * Cgroup creation interface
19  */
20 #ifndef __STC_HELPER_CGROUP_H__
21 #define __STC_HELPER_CGROUP_H__
22
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <sys/mount.h>
26 #include <stdlib.h>
27
28 #include "stc-manager.h"
29 #include "helper-file.h"
30
31 #define DEFAULT_CGROUP                  "/sys/fs/cgroup"
32 #define CGROUP_NETWORK                  DEFAULT_CGROUP "/net_cls"
33 #define STC_CGROUP_NETWORK              CGROUP_NETWORK "/stc"
34 #define BACKGROUND_CGROUP_NETWORK       STC_CGROUP_NETWORK "/BACKGROUND"
35 #define FOREGROUND_CGROUP_NETWORK       STC_CGROUP_NETWORK "/FOREGROUND"
36 #define TETHERING_CGROUP_NETWORK        STC_CGROUP_NETWORK "/TETHERING"
37 #define PROC_TASK_CHILDREN              "/proc/%d/task/%d/children"
38 #define STC_CGROUP_NAME                 "stc"
39 #define STC_BACKGROUND_CGROUP_NAME      "BACKGROUND"
40 #define STC_FOREGROUND_CGROUP_NAME      "FOREGROUND"
41 #define STC_TETHERING_CGROUP_NAME       "TETHERING"
42
43 /**
44  * @desc Get one unsigned int32 value from cgroup
45  * @param cgroup_name - cgroup path
46  * @param file_name - cgroup content to write
47  * @param value - out parameter, value to fill
48  * @return negative value if error
49  */
50 int cgroup_read_node_uint32(const char *cgroup_name, const char *file_name,
51                             uint32_t *value);
52
53 /**
54  * @desc Put unsigned int32 value to cgroup,
55  * @param cgroup_name - cgroup path
56  * @param file_name - cgroup content to write
57  * @param value - unsigned int32 data to write
58  * @return negative value if error
59  */
60 int cgroup_write_node_uint32(const char *cgroup_name, const char *file_name,
61                              uint32_t value);
62
63 /**
64  * @desc Put value to cgroup,
65  * @param cgroup_name - cgroup path
66  * @param file_name - cgroup content to write
67  * @param string -string to write
68  * @return negative value if error
69  */
70 int cgroup_write_node_str(const char *cgroup_name, const char *file_name,
71                           const char *string);
72
73 /**
74  * @desc make cgroup,
75  * @param parentdir - parent cgroup path
76  * @param cgroup_name - cgroup subdirectory to write
77  * @param already - true if subdir already exists, NULL pointer is possible
78  * as formal argument, in this case it will not be filled
79  * @return negative value if error
80  */
81 int cgroup_make_subdir(const char *parentdir, const char *cgroup_name,
82                        bool *already);
83
84 /**
85  * @desc write pid into cgroup_subsystem/cgroup_name file,
86  * @param cgroup_subsystem path to /sys/fs/cgroup/subsystem
87  * @param cgroup_name - name in /sys/fs/cgroup/subsystem/
88  * @return negative value if error
89  */
90 stc_error_e cgroup_write_pid(const char *cgroup_subsystem,
91                              const char *cgroup_name, const int pid);
92
93 stc_error_e cgroup_write_pid_fullpath(const char *cgroup_full_path,
94                                       const int pid);
95
96 /**
97  * @desc doing the same as @see cgroup_write_pid,
98  * but also put into cgroup first level child processes
99  */
100 stc_error_e cgroup_write_pidtree(const char *cgroup_subsystem,
101                                  const char *cgroup_name, const int pid);
102
103 /**
104  * @desc this function sets release agent path into cgroup subsystem
105  * and enables this mechanism
106  * @param cgroup_sussys - cgroup subsystem name, it's relative path to cgroup,
107  * relativelly default cgroup path (DEFAULT_CGROUP)
108  * @param release_agent full path to release agent executable
109  * @return negative value if error
110  */
111 int cgroup_set_release_agent(const char *cgroup_subsys,
112                              const char *release_agent);
113
114 /**
115  * @desc get PIDs of processes in a certain cgroup, an allocated array must be provided
116  * @return 0 if pids were read and array filled
117  */
118 int cgroup_get_pids(const char *name, GArray **pids);
119
120 /**
121  * @desc initializes cgroups.
122  */
123 void cgroup_init(void);
124
125 #endif /*__STC_HELPER_CGROUP_H__*/