chiark / gitweb /
sd-daemon: fix sd_is_mq for non-mq fds
[elogind.git] / src / basic / util.h
index 0d0b881b95fee0357eb652e5d4e279c651382617..42117ed31efc8b85e42732df4ed5cec7f8941957 100644 (file)
@@ -41,6 +41,7 @@
 #include <mntent.h>
 #include <sys/inotify.h>
 #include <sys/statfs.h>
+#include <sys/sysmacros.h>
 
 #include "macro.h"
 #include "missing.h"
@@ -71,6 +72,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 +84,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";
@@ -148,7 +155,10 @@ int parse_size(const char *t, off_t base, off_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);
+#define gid_is_valid(gid) uid_is_valid(gid)
 
 int safe_atou(const char *s, unsigned *ret_u);
 int safe_atoi(const char *s, int *ret_i);
@@ -240,6 +250,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);
@@ -353,13 +367,16 @@ int fd_is_temporary_fs(int fd);
 
 int pipe_eof(int fd);
 
+DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE);
+#define _cleanup_cpu_free_ _cleanup_(CPU_FREEp)
+
 // UNNEEDED 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))
 
 int files_same(const char *filea, const char *fileb);
 
-// UNNEEDED int running_in_chroot(void);
+int running_in_chroot(void);
 
 char *ellipsize(const char *s, size_t length, unsigned percent);
                                    /* bytes                 columns */
@@ -378,14 +395,12 @@ 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);
@@ -553,6 +568,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);
 
@@ -614,7 +630,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);
@@ -785,10 +807,10 @@ int proc_cmdline(char **ret);
 int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
 int get_proc_cmdline_key(const char *parameter, char **value);
 
-// UNNEEDED int container_get_leader(const char *machine, pid_t *pid);
+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);
@@ -838,15 +860,22 @@ int tempfn_random(const char *p, const char *extra, char **ret);
 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 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 unquote_first_word(const char **p, char **ret, UnquoteFlags flags);
-// UNNEEDED 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_;
+static inline void free_and_replace(char **s, char *v) {
+        free(*s);
+        *s = v;
+}
 
 int free_and_strdup(char **p, const char *s);
 
@@ -862,7 +891,11 @@ 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
 
 // UNNEEDED int ptsname_malloc(int fd, char **ret);
 
@@ -896,10 +929,14 @@ void sigkill_wait(pid_t *pid);
 
 // 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);
 
 // 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);