chiark / gitweb /
logind: minor clean-ups
[elogind.git] / src / basic / util.h
index 4e30fe1286aa275b57bb373dca1f1fe88425cadb..f55480abe8b4fd626ae2724aa0cae831f5921113 100644 (file)
 ***/
 
 #include <alloca.h>
+#include <dirent.h>
 #include <fcntl.h>
 #include <inttypes.h>
-#include <time.h>
+#include <limits.h>
+#include <locale.h>
+#include <mntent.h>
 #include <stdarg.h>
 #include <stdbool.h>
-#include <stdlib.h>
+#include <stddef.h>
 #include <stdio.h>
-#include <sched.h>
-#include <limits.h>
-#include <sys/types.h>
+#include <stdlib.h>
+#include <sys/inotify.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
-#include <dirent.h>
-#include <stddef.h>
-#include <unistd.h>
-#include <locale.h>
-#include <mntent.h>
-#include <sys/inotify.h>
 #include <sys/statfs.h>
+#include <sys/sysmacros.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
 
+#include "formats-util.h"
 #include "macro.h"
 #include "missing.h"
 #include "time-util.h"
-#include "formats-util.h"
 
 /* What is interpreted as whitespace? */
 #define WHITESPACE " \t\n\r"
@@ -71,6 +71,7 @@ size_t page_size(void) _pure_;
 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
 
 bool streq_ptr(const char *a, const char *b) _pure_;
+int strcmp_ptr(const char *a, const char *b) _pure_;
 
 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
 
@@ -82,7 +83,12 @@ bool streq_ptr(const char *a, const char *b) _pure_;
 
 #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
 
-#define malloc0(n) (calloc((n), 1))
+#define malloc0(n) (calloc(1, (n)))
+
+static inline void *mfree(void *memory) {
+        free(memory);
+        return NULL;
+}
 
 static inline const char* yes_no(bool b) {
         return b ? "yes" : "no";
@@ -143,12 +149,22 @@ void safe_close_pair(int p[]);
 
 void close_many(const int fds[], unsigned n_fd);
 
-int parse_size(const char *t, off_t base, off_t *size);
+int fclose_nointr(FILE *f);
+FILE* safe_fclose(FILE *f);
+DIR* safe_closedir(DIR *f);
+
+int parse_size(const char *t, uint64_t base, uint64_t *size);
 
 int parse_boolean(const char *v) _pure_;
 int parse_pid(const char *s, pid_t* ret_pid);
 int parse_uid(const char *s, uid_t* ret_uid);
-#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
+#define parse_gid(s, ret_gid) parse_uid(s, ret_gid)
+
+bool uid_is_valid(uid_t uid);
+
+static inline bool gid_is_valid(gid_t gid) {
+        return uid_is_valid((uid_t) gid);
+}
 
 int safe_atou(const char *s, unsigned *ret_u);
 int safe_atoi(const char *s, int *ret_i);
@@ -222,9 +238,9 @@ char *strnappend(const char *s, const char *suffix, size_t length);
 
 int readlinkat_malloc(int fd, const char *p, char **ret);
 int readlink_malloc(const char *p, char **r);
-int readlink_value(const char *p, char **ret);
+// UNNEEDED int readlink_value(const char *p, char **ret);
 int readlink_and_make_absolute(const char *p, char **r);
-int readlink_and_canonicalize(const char *p, char **r);
+// UNNEEDED int readlink_and_canonicalize(const char *p, char **r);
 
 char *strstrip(char *s);
 // UNNEEDED char *delete_chars(char *s, const char *bad);
@@ -232,7 +248,7 @@ char *truncate_nl(char *s);
 
 char *file_in_same_dir(const char *path, const char *filename);
 
-int rmdir_parents(const char *path, const char *stop);
+// UNNEEDED int rmdir_parents(const char *path, const char *stop);
 
 char hexchar(int x) _const_;
 int unhexchar(char c) _const_;
@@ -240,6 +256,10 @@ char octchar(int x) _const_;
 int unoctchar(char c) _const_;
 char decchar(int x) _const_;
 int undecchar(char c) _const_;
+char base32hexchar(int x) _const_;
+int unbase32hexchar(char c) _const_;
+char base64char(int x) _const_;
+int unbase64char(char c) _const_;
 
 char *cescape(const char *s);
 size_t cescape_char(char c, char *buf);
@@ -254,7 +274,7 @@ int cunescape_length_with_prefix(const char *s, size_t length, const char *prefi
 
 char *xescape(const char *s, const char *bad);
 
-char *ascii_strlower(char *path);
+// UNNEEDED char *ascii_strlower(char *path);
 
 bool dirent_is_file(const struct dirent *de) _pure_;
 bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;
@@ -273,9 +293,9 @@ bool chars_intersect(const char *a, const char *b) _pure_;
 
 ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
 
-#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)                                \
-        scope inline type name##_from_string(const char *s) {                                   \
-                return (type)string_table_lookup(name##_table, ELEMENTSOF(name##_table), s);    \
+#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
+        scope type name##_from_string(const char *s) {                  \
+                return (type) string_table_lookup(name##_table, ELEMENTSOF(name##_table), s); \
         }
 
 #define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                    \
@@ -292,17 +312,15 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k
 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max)         \
         int name##_to_string_alloc(type i, char **str) {                \
                 char *s;                                                \
-                int r;                                                  \
                 if (i < 0 || i > max)                                   \
                         return -ERANGE;                                 \
                 if (i < (type) ELEMENTSOF(name##_table)) {              \
                         s = strdup(name##_table[i]);                    \
                         if (!s)                                         \
-                                return log_oom();                       \
+                                return -ENOMEM;                         \
                 } else {                                                \
-                        r = asprintf(&s, "%i", i);                      \
-                        if (r < 0)                                      \
-                                return log_oom();                       \
+                        if (asprintf(&s, "%i", i) < 0)                  \
+                                return -ENOMEM;                         \
                 }                                                       \
                 *str = s;                                               \
                 return 0;                                               \
@@ -310,10 +328,10 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k
         type name##_from_string(const char *s) {                        \
                 type i;                                                 \
                 unsigned u = 0;                                         \
-                assert(s);                                              \
-                for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
-                        if (name##_table[i] &&                          \
-                            streq(name##_table[i], s))                  \
+                if (!s)                                                 \
+                        return (type) -1;                               \
+                for (i = 0; i < (type) ELEMENTSOF(name##_table); i++)   \
+                        if (streq_ptr(name##_table[i], s))              \
                                 return i;                               \
                 if (safe_atou(s, &u) >= 0 && u <= max)                  \
                         return (type) u;                                \
@@ -326,7 +344,7 @@ int fd_cloexec(int fd, bool cloexec);
 
 int close_all_fds(const int except[], unsigned n_except);
 
-bool fstype_is_network(const char *fstype);
+// UNNEEDED bool fstype_is_network(const char *fstype);
 
 int flush_fd(int fd);
 
@@ -338,12 +356,12 @@ int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
 
 bool is_device_path(const char *path);
 
-int dir_is_empty(const char *path);
-char* dirname_malloc(const char *path);
+// UNNEEDED int dir_is_empty(const char *path);
+// UNNEEDED char* dirname_malloc(const char *path);
 
 char* lookup_uid(uid_t uid);
-char* getlogname_malloc(void);
-char* getusername_malloc(void);
+// UNNEEDED char* getlogname_malloc(void);
+// UNNEEDED char* getusername_malloc(void);
 
 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
 // UNNEEDED int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
@@ -353,9 +371,9 @@ int fd_is_temporary_fs(int fd);
 
 int pipe_eof(int fd);
 
-cpu_set_t* cpu_set_malloc(unsigned *ncpus);
-
-#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
+#define xsprintf(buf, fmt, ...) \
+        assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \
+                          "xsprintf: " #buf "[] must be big enough")
 
 int files_same(const char *filea, const char *fileb);
 
@@ -378,19 +396,17 @@ DIR *xopendirat(int dirfd, const char *name, int flags);
 
 // UNNEEDED char *fstab_node_to_udev_node(const char *p);
 
-// UNNEEDED void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
+void execute_directories(const char* const* directories, usec_t timeout, char *argv[]);
 
 bool nulstr_contains(const char*nulstr, const char *needle);
 
 // UNNEEDED bool plymouth_running(void);
 
-bool machine_name_is_valid(const char *s) _pure_;
-
 char* strshorten(char *s, size_t l);
 
 // UNNEEDED int symlink_idempotent(const char *from, const char *to);
 
-int symlink_atomic(const char *from, const char *to);
+// UNNEEDED int symlink_atomic(const char *from, const char *to);
 // UNNEEDED int mknod_atomic(const char *path, mode_t mode, dev_t dev);
 // UNNEEDED int mkfifo_atomic(const char *path, mode_t mode);
 
@@ -408,7 +424,7 @@ int in_gid(gid_t gid);
 char* uid_to_name(uid_t uid);
 char* gid_to_name(gid_t gid);
 
-int glob_exists(const char *path);
+// UNNEEDED int glob_exists(const char *path);
 // UNNEEDED int glob_extend(char ***strv, const char *path);
 
 int dirent_ensure_type(DIR *d, struct dirent *de);
@@ -425,7 +441,7 @@ static inline bool _pure_ in_charset(const char *s, const char* charset) {
         return s[strspn(s, charset)] == '\0';
 }
 
-int block_get_whole_disk(dev_t d, dev_t *ret);
+// UNNEEDED int block_get_whole_disk(dev_t d, dev_t *ret);
 
 #define NULSTR_FOREACH(i, l)                                    \
         for ((i) = (l); (i) && *(i); (i) = strchr((i), 0)+1)
@@ -436,32 +452,32 @@ int block_get_whole_disk(dev_t d, dev_t *ret);
 int ioprio_class_to_string_alloc(int i, char **s);
 int ioprio_class_from_string(const char *s);
 
-const char *sigchld_code_to_string(int i) _const_;
-int sigchld_code_from_string(const char *s) _pure_;
+// UNNEEDED const char *sigchld_code_to_string(int i) _const_;
+// UNNEEDED int sigchld_code_from_string(const char *s) _pure_;
 
-int log_facility_unshifted_to_string_alloc(int i, char **s);
-int log_facility_unshifted_from_string(const char *s);
+// UNNEEDED int log_facility_unshifted_to_string_alloc(int i, char **s);
+// UNNEEDED int log_facility_unshifted_from_string(const char *s);
 
 int log_level_to_string_alloc(int i, char **s);
 int log_level_from_string(const char *s);
 
-int sched_policy_to_string_alloc(int i, char **s);
-int sched_policy_from_string(const char *s);
+// UNNEEDED int sched_policy_to_string_alloc(int i, char **s);
+// UNNEEDED int sched_policy_from_string(const char *s);
 
 const char *rlimit_to_string(int i) _const_;
 int rlimit_from_string(const char *s) _pure_;
 
-int ip_tos_to_string_alloc(int i, char **s);
-int ip_tos_from_string(const char *s);
+// UNNEEDED int ip_tos_to_string_alloc(int i, char **s);
+// UNNEEDED int ip_tos_from_string(const char *s);
 
 extern int saved_argc;
 extern char **saved_argv;
 
-bool kexec_loaded(void);
+// UNNEEDED bool kexec_loaded(void);
 
 // UNNEEDED int prot_from_flags(int flags) _const_;
 
-char *format_bytes(char *buf, size_t l, off_t t);
+// UNNEEDED char *format_bytes(char *buf, size_t l, off_t t);
 
 int fd_wait_for_event(int fd, int event, usec_t timeout);
 
@@ -472,7 +488,7 @@ int fd_inc_rcvbuf(int fd, size_t n);
 
 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
 
-int setrlimit_closest(int resource, const struct rlimit *rlim);
+// UNNEEDED int setrlimit_closest(int resource, const struct rlimit *rlim);
 
 bool http_url_is_valid(const char *url) _pure_;
 bool documentation_url_is_valid(const char *url) _pure_;
@@ -482,7 +498,7 @@ bool documentation_url_is_valid(const char *url) _pure_;
 bool in_initrd(void);
 
 int get_home_dir(char **ret);
-int get_shell(char **_ret);
+// UNNEEDED int get_shell(char **_ret);
 
 static inline void freep(void *p) {
         free(*(void**) p);
@@ -500,7 +516,10 @@ static inline void close_pairp(int (*p)[2]) {
         safe_close_pair(*p);
 }
 
-DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, fclose);
+static inline void fclosep(FILE **f) {
+        safe_fclose(*f);
+}
+
 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
 DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
 DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, endmntent);
@@ -553,6 +572,7 @@ _pure_ static inline bool string_is_glob(const char *p) {
 // UNNEEDED                 void *arg);
 
 #define _(String) gettext (String)
+#define N_(String) String
 // UNNEEDED void init_gettext(void);
 bool is_locale_utf8(void);
 
@@ -570,11 +590,11 @@ typedef enum DrawSpecialChar {
 
 const char *draw_special_char(DrawSpecialChar ch);
 
-char *strreplace(const char *text, const char *old_string, const char *new_string);
+// UNNEEDED char *strreplace(const char *text, const char *old_string, const char *new_string);
 
-char *strip_tab_ansi(char **p, size_t *l);
+// UNNEEDED char *strip_tab_ansi(char **p, size_t *l);
 
-int on_ac_power(void);
+// UNNEEDED int on_ac_power(void);
 
 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f);
 // UNNEEDED int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f);
@@ -614,7 +634,13 @@ static inline void *mempset(void *s, int c, size_t n) {
 }
 
 char *hexmem(const void *p, size_t l);
-void *unhexmem(const char *p, size_t l);
+int unhexmem(const char *p, size_t l, void **mem, size_t *len);
+
+char *base32hexmem(const void *p, size_t l, bool padding);
+int unbase32hexmem(const char *p, size_t l, bool padding, void **mem, size_t *len);
+
+char *base64mem(const void *p, size_t l);
+int unbase64mem(const char *p, size_t l, void **mem, size_t *len);
 
 char *strextend(char **x, ...) _sentinel_;
 char *strrep(const char *s, unsigned n);
@@ -787,8 +813,8 @@ int get_proc_cmdline_key(const char *parameter, char **value);
 
 int container_get_leader(const char *machine, pid_t *pid);
 
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd);
-int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd);
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd);
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd);
 
 int getpeercred(int fd, struct ucred *ucred);
 int getpeersec(int fd, char **ret);
@@ -796,7 +822,7 @@ int getpeersec(int fd, char **ret);
 int writev_safe(int fd, const struct iovec *w, int j);
 
 int mkostemp_safe(char *pattern, int flags);
-int open_tmpfile(const char *path, int flags);
+// UNNEEDED int open_tmpfile(const char *path, int flags);
 
 int fd_warn_permissions(const char *path, int fd);
 
@@ -808,7 +834,7 @@ int fd_warn_permissions(const char *path, int fd);
 #endif
 
 // UNNEEDED unsigned long personality_from_string(const char *p);
-const char *personality_to_string(unsigned long);
+// UNNEEDED const char *personality_to_string(unsigned long);
 
 uint64_t physical_memory(void);
 
@@ -820,11 +846,11 @@ union file_handle_union {
 };
 #define FILE_HANDLE_INIT { .handle.handle_bytes = MAX_HANDLE_SZ }
 
-int update_reboot_param_file(const char *param);
+// UNNEEDED int update_reboot_param_file(const char *param);
 
-int umount_recursive(const char *target, int flags);
+// UNNEEDED int umount_recursive(const char *target, int flags);
 
-int bind_remount_recursive(const char *prefix, bool ro);
+// UNNEEDED int bind_remount_recursive(const char *prefix, bool ro);
 
 int fflush_and_check(FILE *f);
 
@@ -834,19 +860,21 @@ int tempfn_random(const char *p, const char *extra, char **ret);
 
 // UNNEEDED int take_password_lock(const char *root);
 
-int is_symlink(const char *path);
+// UNNEEDED int is_symlink(const char *path);
 int is_dir(const char *path, bool follow);
 // UNNEEDED int is_device_node(const char *path);
 
-typedef enum UnquoteFlags {
-        UNQUOTE_RELAX           = 1,
-        UNQUOTE_CUNESCAPE       = 2,
-        UNQUOTE_CUNESCAPE_RELAX = 4,
-} UnquoteFlags;
+typedef enum ExtractFlags {
+        EXTRACT_RELAX           = 1,
+        EXTRACT_CUNESCAPE       = 2,
+        EXTRACT_CUNESCAPE_RELAX = 4,
+        EXTRACT_QUOTES          = 8,
+        EXTRACT_DONT_COALESCE_SEPARATORS = 16,
+} ExtractFlags;
 
-int unquote_first_word(const char **p, char **ret, UnquoteFlags flags);
-int unquote_first_word_and_warn(const char **p, char **ret, UnquoteFlags flags, const char *unit, const char *filename, unsigned line, const char *rvalue);
-// UNNEEDED int unquote_many_words(const char **p, UnquoteFlags flags, ...) _sentinel_;
+int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags);
+// UNNEEDED int extract_first_word_and_warn(const char **p, char **ret, const char *separators, ExtractFlags flags, const char *unit, const char *filename, unsigned line, const char *rvalue);
+// UNNEEDED int extract_many_words(const char **p, const char *separators, ExtractFlags flags, ...) _sentinel_;
 
 int free_and_strdup(char **p, const char *s);
 
@@ -862,26 +890,30 @@ union inotify_event_buffer {
         uint8_t raw[INOTIFY_EVENT_MAX];
 };
 
-#define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
+#ifdef __GLIBC__
+ #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW)
+#else
+ #define laccess(path, mode) faccessat(AT_FDCWD, (path), (mode), 0)
+#endif
 
-int ptsname_malloc(int fd, char **ret);
+// UNNEEDED int ptsname_malloc(int fd, char **ret);
 
 // UNNEEDED int openpt_in_namespace(pid_t pid, int flags);
 
 ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
 
-int fd_setcrtime(int fd, usec_t usec);
+// UNNEEDED int fd_setcrtime(int fd, usec_t usec);
 int fd_getcrtime(int fd, usec_t *usec);
 // UNNEEDED int path_getcrtime(const char *p, usec_t *usec);
-int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
+// UNNEEDED int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
 
-int same_fd(int a, int b);
+// UNNEEDED int same_fd(int a, int b);
 
-int chattr_fd(int fd, unsigned value, unsigned mask);
-int chattr_path(const char *p, unsigned value, unsigned mask);
+// UNNEEDED int chattr_fd(int fd, unsigned value, unsigned mask);
+// UNNEEDED int chattr_path(const char *p, unsigned value, unsigned mask);
 
-int read_attr_fd(int fd, unsigned *ret);
-int read_attr_path(const char *p, unsigned *ret);
+// UNNEEDED int read_attr_fd(int fd, unsigned *ret);
+// UNNEEDED int read_attr_path(const char *p, unsigned *ret);
 
 #define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
 
@@ -892,14 +924,27 @@ void sigkill_wait(pid_t *pid);
 
 // UNNEEDED int syslog_parse_priority(const char **p, int *priority, bool with_facility);
 
-void cmsg_close_all(struct msghdr *mh);
+// UNNEEDED void cmsg_close_all(struct msghdr *mh);
 
-int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
+// UNNEEDED int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
 
-char *shell_maybe_quote(const char *s);
+// UNNEEDED char *shell_escape(const char *s, const char *bad);
+// UNNEEDED char *shell_maybe_quote(const char *s);
 
 int parse_mode(const char *s, mode_t *ret);
 
-int mount_move_root(const char *path);
+// UNNEEDED int mount_move_root(const char *path);
 
 int reset_uid_gid(void);
+
+int getxattr_malloc(const char *path, const char *name, char **value, bool allow_symlink);
+int fgetxattr_malloc(int fd, const char *name, char **value);
+
+int send_one_fd(int transport_fd, int fd, int flags);
+// UNNEEDED int receive_one_fd(int transport_fd, int flags);
+
+// UNNEEDED void nop_signal_handler(int sig);
+
+int version(void);
+
+// UNNEEDED bool fdname_is_valid(const char *s);