Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / common / util.h
1 /*
2  * resourced
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 /*
21  * @file util.h
22  * @desc Generic Helper functions
23  */
24
25 #ifndef _RESOURCED_UTIL_H_
26 #define _RESOURCED_UTIL_H_
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdbool.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <dirent.h>
34 #include <stdint.h>
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39
40 #define COMMENT         '#'
41 #define COMMENTS        "#;"
42 #define NEWLINE         "\n\r"
43 #define WHITESPACE      " \t\n\r"
44
45 #define _pure_ __attribute__ ((pure))
46 #define _cleanup_(x) __attribute__((cleanup(x)))
47
48 struct sched_attr {
49         uint32_t size;              /* Size of this structure */
50         uint32_t sched_policy;      /* Policy (SCHED_*) */
51         unsigned long long sched_flags;       /* Flags */
52         int32_t sched_nice;         /* Nice value (SCHED_OTHER, SCHED_BATCH) */
53         uint32_t sched_priority;    /* Static priority (SCHED_FIFO, SCHED_RR) */
54         /* Remaining fields are for SCHED_DEADLINE */
55         unsigned long long sched_runtime;
56         unsigned long long sched_deadline;
57         unsigned long long sched_period;
58 };
59
60 static inline void freep(void *p)
61 {
62         free(*(void**) p);
63 }
64
65 static inline void closep(int *fd)
66 {
67         if (*fd >= 0)
68                 close(*fd);
69 }
70
71 static inline void fclosep(FILE **f)
72 {
73         if (*f)
74                 fclose(*f);
75 }
76
77 static inline void pclosep(FILE **f)
78 {
79         if (*f)
80                 pclose(*f);
81 }
82
83 static inline void closedirp(DIR **d)
84 {
85         if (*d)
86                 closedir(*d);
87 }
88
89 #define _cleanup_free_ _cleanup_(freep)
90 #define _cleanup_close_ _cleanup_(closep)
91 #define _cleanup_fclose_ _cleanup_(fclosep)
92 #define _cleanup_closedir_ _cleanup_(closedirp)
93
94 #define BYTE_TO_KBYTE(b) ((b) >> 10)
95 #define BYTE_TO_MBYTE(b) ((b) >> 20)
96 #define BYTE_TO_PAGE(b) ((b) >> 12)
97
98 #define KBYTE_TO_BYTE(k) ((unsigned long long)(k) << 10)
99 #define KBYTE_TO_MBYTE(k) ((k) >> 10)
100
101 #define MBYTE_TO_BYTE(m) ((unsigned long long)(m) << 20)
102 #define MBYTE_TO_KBYTE(m) ((m) << 10)
103
104 #define GBYTE_TO_BYTE(g) ((unsigned long long)(g) << 30)
105 #define GBYTE_TO_MBYTE(g) ((g) << 10)
106
107 #define streq(a, b) (strncmp((a), (b), strlen(b)+1) == 0)
108 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
109 #define strcaseeq(a, b) (strcasecmp((a), (b)) == 0)
110
111 #define new(t, n) ((t*) malloc(sizeof(t) * (n)))
112 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
113
114 static inline bool is_empty(const char *p)
115 {
116         return !p || !p[0];
117 }
118
119 static inline bool strstart_with(const char *str, const char *with)
120 {
121         return strncmp(str, with, strlen(with)) == 0;
122 }
123
124 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
125         for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); \
126              (word);                                                    \
127              (word) = split((s), &(length), (separator), &(state)))
128
129 #define FOREACH_WORD(word, length, s, state)                            \
130         FOREACH_WORD_SEPARATOR(word, length, s, WHITESPACE, state)
131
132 #define FOREACH_DIRENT(de, d, on_error)                                 \
133         for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d))   \
134                 if (!de) {                                              \
135                         if (errno > 0) {                                \
136                                 on_error;                               \
137                         }                                               \
138                         break;                                          \
139                 } else if (streq(de->d_name, ".") ||                    \
140                            streq(de->d_name, ".."))                     \
141                         continue;                                       \
142                 else
143
144 #define FOREACH_STRV(s, l)                      \
145         for ((s) = (l); (s) && *(s); (s)++)
146
147 /**
148  * @desc executes given command and dumps output to a file
149  * @param argv - command to be executed with parameters
150  filename - output file
151  * @return None
152  */
153 int exec_cmd(int argc, const char *argv[]);
154
155 bool streq_ptr(const char *a, const char *b) _pure_;
156 int parse_boolean(const char *v) _pure_;
157 int parse_bytes(const char *b, size_t *s) _pure_;
158 char *split(const char *c, size_t *l, const char *separator, char **state);
159 char *truncate_nl(char *s);
160 char *strstrip(char *s);
161 int str_to_strv(const char *str, char ***strv, const char *seperator);
162 size_t sizeof_strv(const char **strv);
163 int strv_attach(char **first, char **second, char ***strv, bool free_second);
164 void strv_free_full(char ***strv);
165
166 int resourced_restarted(void);
167
168 int sched_setattr(pid_t pid, struct sched_attr *attr, unsigned int flags);
169 int sched_getattr(pid_t pid, struct sched_attr *attr, unsigned int flags);
170
171 #ifdef __cplusplus
172 }
173 #endif /* __cplusplus */
174
175 #endif /*_RESOURCED_UTIL_H_*/