util.h revision 56cf987fe74270bde4e16c7ec9e0414a9030723b
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering/*-*- Mode: C; c-basic-offset: 8 -*-*/
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering This file is part of systemd.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Copyright 2010 Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is free software; you can redistribute it and/or modify it
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering under the terms of the GNU General Public License as published by
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering the Free Software Foundation; either version 2 of the License, or
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering (at your option) any later version.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering systemd is distributed in the hope that it will be useful, but
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering General Public License for more details.
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón You should have received a copy of the GNU General Public License
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón along with systemd; If not, see <http://www.gnu.org/licenses/>.
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define USEC_PER_SEC 1000000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define USEC_PER_MSEC 1000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define NSEC_PER_SEC 1000000000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define NSEC_PER_MSEC 1000000ULL
0e3b0a95cc57cf8c51b20b5b94af7e51c5e424f3Jan Alexander Steffens (heftig)#define NSEC_PER_USEC 1000ULL
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#define USEC_PER_DAY (24ULL*USEC_PER_HOUR)
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez/* What is interpreted as whitespace? */
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburgerdual_timestamp* dual_timestamp_get(dual_timestamp *ts);
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburgerusec_t timespec_load(const struct timespec *ts);
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburgerstruct timespec *timespec_store(struct timespec *ts, usec_t u);
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburgerusec_t timeval_load(const struct timeval *tv);
a59f16ce4a11c440cb2136ad3d5e3184714c545eRobert Schielestruct timeval *timeval_store(struct timeval *tv, usec_t u);
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburgerbool streq_ptr(const char *a, const char *b);
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger#define new(t, n) ((t*) malloc(sizeof(t)*(n)))
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger#define new0(t, n) ((t*) calloc((n), sizeof(t)))
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocerastatic inline const char* yes_no(bool b) {
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poetteringstatic inline const char* strempty(const char *s) {
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering return s ? s : "";
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poetteringstatic inline const char* strnull(const char *s) {
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers return s ? s : "(null)";
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominenstatic inline const char *strna(const char *s) {
b51fc639f01ee6194af3e7e944a79accce474fe1Dave Reisner return s ? s : "n/a";
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominenstatic inline bool is_path_absolute(const char *p) {
96ede2601f27cd5fe52eed96b873bef55cd0ce23Lennart Poettering return *p == '/';
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominenbool endswith(const char *s, const char *postfix);
7801356442578ff6e1c65844eb9e65c819af4660Zbigniew Jędrzejewski-Szmekbool startswith(const char *s, const char *prefix);
66b0e0e0e3652227fe107ab9d09fa14fd4bc4dfaCristian Rodríguezbool startswith_no_case(const char *s, const char *prefix);
f00929ad622c978f8ad83590a15a765b4beecac9Dimitri John Ledkovbool first_word(const char *s, const char *word);
446883528524429283626208928b51f49f28f810Lennart Poetteringvoid close_many(const int fds[], unsigned n_fd);
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmekint parse_boolean(const char *v);
c4a77bcb9a50f152557e25c90837d8bfef858729Marc-Antoine Perennouint parse_usec(const char *t, usec_t *usec);
9a60da2834074d970ca063c210fe9d2f05c70532Thierry Redingint safe_atollu(const char *s, unsigned long long *ret_u);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekint safe_atolli(const char *s, long long int *ret_i);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(unsigned long) == sizeof(unsigned));
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek return safe_atou(s, (unsigned*) ret_u);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekstatic inline int safe_atoli(const char *s, long int *ret_u) {
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(long int) == sizeof(int));
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek return safe_atoi(s, (int*) ret_u);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekstatic inline int safe_atolu(const char *s, unsigned long *ret_u) {
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atollu(s, (unsigned long long*) ret_u);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekstatic inline int safe_atoli(const char *s, long int *ret_u) {
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(long int) == sizeof(long long int));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atolli(s, (long long int*) ret_u);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekstatic inline int safe_atou32(const char *s, uint32_t *ret_u) {
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(uint32_t) == sizeof(unsigned));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atou(s, (unsigned*) ret_u);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekstatic inline int safe_atoi32(const char *s, int32_t *ret_i) {
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek assert_cc(sizeof(int32_t) == sizeof(int));
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return safe_atoi(s, (int*) ret_i);
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poetteringstatic inline int safe_atou64(const char *s, uint64_t *ret_u) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering return safe_atollu(s, (unsigned long long*) ret_u);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringstatic inline int safe_atoi64(const char *s, int64_t *ret_i) {
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering assert_cc(sizeof(int64_t) == sizeof(long long int));
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering return safe_atolli(s, (long long int*) ret_i);
03930e48636e86ad84946253ca2bf4f91deeb645Lennart Poetteringchar *split(const char *c, size_t *l, const char *separator, char **state);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar *split_quoted(const char *c, size_t *l, char **state);
8dc31a63981267f257583ef82ceb79859ff243f8Daniel Mack for ((state) = NULL, (word) = split((s), &(length), WHITESPACE, &(state)); (word); (word) = split((s), &(length), WHITESPACE, &(state)))
8dc31a63981267f257583ef82ceb79859ff243f8Daniel Mack#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering for ((state) = NULL, (word) = split((s), &(length), (separator), &(state)); (word); (word) = split((s), &(length), (separator), &(state)))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering#define FOREACH_WORD_QUOTED(word, length, s, state) \
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar **split_path_and_make_absolute(const char *p);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringpid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
f73fb7b742f294b6d2126afa16001bd2ff6ab461Filipe Brandenburgerint write_one_line_file(const char *fn, const char *line);
0a98d66159e474915afd6597d3aa444a698fdd2dDavid Herrmannint read_one_line_file(const char *fn, char **line);
213298fb822258bb69c6b85b7c8d7f019fd2306aLennart Poetteringchar *strappend(const char *s, const char *suffix);
693eb9a2d42d71445dad273a76e2470199d1dc5aLennart Poetteringchar *strnappend(const char *s, const char *suffix, size_t length);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar *replace_env(const char *format, char **env);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringchar **replace_env_argv(char **argv, char **env);
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieriint readlink_malloc(const char *p, char **r);
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poetteringint readlink_and_make_absolute(const char *p, char **r);
2a4d1ec1520c926b0160efe14142634e164ddc30Cristian Rodríguezchar *file_name_from_path(const char *p);
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poetteringbool is_path(const char *p);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerbool path_is_absolute(const char *p);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar *path_make_absolute(const char *p, const char *prefix);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar *path_make_absolute_cwd(const char *p);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar **strv_path_make_absolute_cwd(char **l);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar **strv_path_canonicalize(char **l);
a01a4517e16c532fbd5203fbfe2571255e2cd312Filipe Brandenburgerchar *delete_chars(char *s, const char *bad);
65e3a2cf7c3b399853dd309f702ca5078b7d16eaZbigniew Jędrzejewski-Szmekchar *file_in_same_dir(const char *path, const char *filename);
65e3a2cf7c3b399853dd309f702ca5078b7d16eaZbigniew Jędrzejewski-Szmekint safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
65e3a2cf7c3b399853dd309f702ca5078b7d16eaZbigniew Jędrzejewski-Szmekint mkdir_parents(const char *path, mode_t mode);
4f47bb8c5e5f234c614dc14532a9483328e61002Zbigniew Jędrzejewski-Szmekint mkdir_p(const char *path, mode_t mode);
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sieversint parent_of_path(const char *path, char **parent);
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sieversint rmdir_parents(const char *path, const char *stop);
5a45a93627609451784a04366cfa1150d32611d1Lennart Poetteringint get_process_name(pid_t pid, char **name);
0289f2fb2a64df53b589b771f69c43126b029590Zbigniew Jędrzejewski-Szmekint get_process_cmdline(pid_t pid, size_t max_length, char **line);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *cescape(const char *s);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *cunescape(const char *s);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *cunescape_length(const char *s, size_t length);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *bus_path_escape(const char *s);
2b4919a68cf826efbe939291e6dc4f08e824dc41Kay Sieverschar *bus_path_unescape(const char *s);
9d2d0fe1e3f28a639c26b62391f79cfd1450d91bEvangelos Foutrasbool path_startswith(const char *path, const char *prefix);
5a45a93627609451784a04366cfa1150d32611d1Lennart Poetteringbool path_equal(const char *a, const char *b);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekbool ignore_file(const char *filename);
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmekbool chars_intersect(const char *a, const char *b);
1c231f56482546725c4dbd3303f70300bd3c63e9Lennart Poetteringchar *format_timestamp(char *buf, size_t l, usec_t t);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmekchar *format_timespan(char *buf, size_t l, usec_t t);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmekbool is_clean_exit(int code, int status);
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmekunsigned long long random_ull(void);
2799e519cabb6dfa99341b9a56ebd4dc2a4ec22aZbigniew Jędrzejewski-Szmek#define DEFINE_STRING_TABLE_LOOKUP(name,type) \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek const char *name##_to_string(type i) { \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek type name##_from_string(const char *s) { \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger unsigned u = 0; \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger for (i = 0; i < (type)ELEMENTSOF(name##_table); i++) \
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger if (safe_atou(s, &u) >= 0 && \
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek struct __useless_struct_to_allow_trailing_semicolon__
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint fd_nonblock(int fd, bool nonblock);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint fd_cloexec(int fd, bool cloexec);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint close_all_fds(const int except[], unsigned n_except);
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sieversint read_one_char(FILE *f, char *ret, bool *need_nl);
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Bieblint ask(char *ret, const char *replies, const char *text, ...);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poetteringint open_terminal(const char *name, int mode);
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Bieblint acquire_terminal(const char *name, bool fail, bool force, bool ignore_tiocstty_eperm);
4db17f291c627c885de668200ff8cce2e57c933fZbigniew Jędrzejewski-Szmekint ignore_signals(int sig, ...);
c932fb71cc90461b88ecdffe47c071d001d78fb4Shawn Landdenint sigaction_many(const struct sigaction *sa, ...);
9388e99e208a6487b26dcbda86005ee9eba8d93dMichael Olbrichssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
3b794314149e40afaf3c456285e1e529747b6560Holger Schurigssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekint path_is_mount_point(const char *path);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekbool is_device_path(const char *path);
75616a1332aff00d27db713cda3bd93c508a5b59Zbigniew Jędrzejewski-Szmekvoid sigset_add_many(sigset_t *ss, ...);
38a0245fb2248744f3c38f451231c4f45bffdc42Susant Sahaniint chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
34f7b9f98facbf3431c6849622104cee992f2b7dLennart Poetteringint rm_rf(const char *path, bool only_dirs, bool delete_root);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekcpu_set_t* cpu_set_malloc(unsigned *ncpus);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekvoid status_vprintf(const char *format, va_list ap);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekvoid status_printf(const char *format, ...);
34f7b9f98facbf3431c6849622104cee992f2b7dLennart Poetteringchar *ellipsize(const char *s, unsigned length, unsigned percent);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmekconst char *ioprio_class_to_string(int i);
a8348796c0d39435b1c3d85ce6e95dad1ac85fecLennart Poetteringint ioprio_class_from_string(const char *s);
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremerconst char *sigchld_code_to_string(int i);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringint sigchld_code_from_string(const char *s);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekconst char *log_facility_to_string(int i);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint log_facility_from_string(const char *s);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekconst char *log_level_to_string(int i);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint log_level_from_string(const char *s);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekconst char *sched_policy_to_string(int i);
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmekint sched_policy_from_string(const char *s);
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineauconst char *rlimit_to_string(int i);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint rlimit_from_string(const char *s);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingconst char *ip_tos_to_string(int i);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint ip_tos_from_string(const char *s);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingconst char *signal_to_string(int i);
37161c5148396448921841ae1026b281c7949652Emil Renner Berthingint signal_from_string(const char *s);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekint label_socket_set(const char *label);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekint label_fifofile_set(const char *label, const char *path);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekvoid label_free(const char *label);
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmekint label_get_socket_label_from_exe(const char *exe, char **label);