4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * @desc Generic Helper functions
25 #ifndef _RESOURCED_UTIL_H_
26 #define _RESOURCED_UTIL_H_
38 #endif /* __cplusplus */
42 #define NEWLINE "\n\r"
43 #define WHITESPACE " \t\n\r"
45 #define _pure_ __attribute__ ((pure))
46 #define _cleanup_(x) __attribute__((cleanup(x)))
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;
60 static inline void freep(void *p)
65 static inline void closep(int *fd)
71 static inline void fclosep(FILE **f)
77 static inline void pclosep(FILE **f)
83 static inline void closedirp(DIR **d)
89 #define _cleanup_free_ _cleanup_(freep)
90 #define _cleanup_close_ _cleanup_(closep)
91 #define _cleanup_fclose_ _cleanup_(fclosep)
92 #define _cleanup_closedir_ _cleanup_(closedirp)
94 #define BYTE_TO_KBYTE(b) ((b) >> 10)
95 #define BYTE_TO_MBYTE(b) ((b) >> 20)
96 #define BYTE_TO_PAGE(b) ((b) >> 12)
98 #define KBYTE_TO_BYTE(k) ((unsigned long long)(k) << 10)
99 #define KBYTE_TO_MBYTE(k) ((k) >> 10)
101 #define MBYTE_TO_BYTE(m) ((unsigned long long)(m) << 20)
102 #define MBYTE_TO_KBYTE(m) ((m) << 10)
104 #define GBYTE_TO_BYTE(g) ((unsigned long long)(g) << 30)
105 #define GBYTE_TO_MBYTE(g) ((g) << 10)
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)
111 #define new(t, n) ((t*) malloc(sizeof(t) * (n)))
112 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
114 static inline bool is_empty(const char *p)
119 static inline bool strstart_with(const char *str, const char *with)
121 return strncmp(str, with, strlen(with)) == 0;
124 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
125 for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); \
127 (word) = split((s), &(length), (separator), &(state)))
129 #define FOREACH_WORD(word, length, s, state) \
130 FOREACH_WORD_SEPARATOR(word, length, s, WHITESPACE, state)
132 #define FOREACH_DIRENT(de, d, on_error) \
133 for (errno = 0, de = readdir(d);; errno = 0, de = readdir(d)) \
139 } else if (streq(de->d_name, ".") || \
140 streq(de->d_name, "..")) \
144 #define FOREACH_STRV(s, l) \
145 for ((s) = (l); (s) && *(s); (s)++)
148 * @desc executes given command and dumps output to a file
149 * @param argv - command to be executed with parameters
150 filename - output file
153 int exec_cmd(int argc, const char *argv[]);
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);
166 int resourced_restarted(void);
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);
173 #endif /* __cplusplus */
175 #endif /*_RESOURCED_UTIL_H_*/