diff options
Diffstat (limited to 'src')
42 files changed, 453 insertions, 164 deletions
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c index 7d64c6a2b..630ae23a0 100644 --- a/src/basic/cgroup-util.c +++ b/src/basic/cgroup-util.c @@ -103,9 +103,12 @@ int cg_read_pid(FILE *f, pid_t *_pid) { return 1; } -int cg_read_event(const char *controller, const char *path, const char *event, - char **val) -{ +int cg_read_event( + const char *controller, + const char *path, + const char *event, + char **val) { + _cleanup_free_ char *events = NULL, *content = NULL; char *p, *line; int r; @@ -1034,7 +1037,7 @@ int cg_get_xattr(const char *controller, const char *path, const char *name, voi int cg_pid_get_path(const char *controller, pid_t pid, char **path) { _cleanup_fclose_ FILE *f = NULL; char line[LINE_MAX]; - const char *fs, *controller_str; + const char *fs, *controller_str = NULL; size_t cs = 0; int unified; @@ -2412,7 +2415,6 @@ int cg_mask_supported(CGroupMask *ret) { #if 0 /// UNNEEDED by elogind int cg_kernel_controllers(Set *controllers) { _cleanup_fclose_ FILE *f = NULL; - char buf[LINE_MAX]; int r; assert(controllers); @@ -2430,7 +2432,7 @@ int cg_kernel_controllers(Set *controllers) { } /* Ignore the header line */ - (void) fgets(buf, sizeof(buf), f); + (void) read_line(f, (size_t) -1, NULL); for (;;) { char *controller; diff --git a/src/basic/def.h b/src/basic/def.h index 1571f921b..377c0008c 100644 --- a/src/basic/def.h +++ b/src/basic/def.h @@ -55,7 +55,7 @@ #define SIGNALS_CRASH_HANDLER SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT #define SIGNALS_IGNORE SIGPIPE -#ifdef HAVE_SPLIT_USR +#if HAVE_SPLIT_USR #define KBD_KEYMAP_DIRS \ "/usr/share/keymaps/\0" \ "/usr/share/kbd/keymaps/\0" \ @@ -80,7 +80,7 @@ #define NOTIFY_FD_MAX 768 #define NOTIFY_BUFFER_MAX PIPE_BUF -#ifdef HAVE_SPLIT_USR +#if HAVE_SPLIT_USR # define _CONF_PATHS_SPLIT_USR(n) "/lib/" n "\0" #else # define _CONF_PATHS_SPLIT_USR(n) diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 3990d4dce..46b8b4a69 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -53,13 +53,17 @@ #define READ_FULL_BYTES_MAX (4U*1024U*1024U) -int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) { +int write_string_stream_ts( + FILE *f, + const char *line, + WriteStringFileFlags flags, + struct timespec *ts) { assert(f); assert(line); fputs(line, f); - if (enforce_newline && !endswith(line, "\n")) + if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n")) fputc('\n', f); if (ts) { @@ -69,10 +73,18 @@ int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, stru return -errno; } - return fflush_and_check(f); + if (flags & WRITE_STRING_FILE_SYNC) + return fflush_sync_and_check(f); + else + return fflush_and_check(f); } -static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline, bool do_fsync) { +static int write_string_file_atomic( + const char *fn, + const char *line, + WriteStringFileFlags flags, + struct timespec *ts) { + _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *p = NULL; int r; @@ -86,22 +98,28 @@ static int write_string_file_atomic(const char *fn, const char *line, bool enfor (void) fchmod_umask(fileno(f), 0644); - r = write_string_stream(f, line, enforce_newline); - if (r >= 0 && do_fsync) - r = fflush_sync_and_check(f); + r = write_string_stream_ts(f, line, flags, ts); + if (r < 0) + goto fail; - if (r >= 0) { - if (rename(p, fn) < 0) - r = -errno; + if (rename(p, fn) < 0) { + r = -errno; + goto fail; } - if (r < 0) - (void) unlink(p); + return 0; +fail: + (void) unlink(p); return r; } -int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) { +int write_string_file_ts( + const char *fn, + const char *line, + WriteStringFileFlags flags, + struct timespec *ts) { + _cleanup_fclose_ FILE *f = NULL; int q, r; @@ -114,8 +132,7 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags if (flags & WRITE_STRING_FILE_ATOMIC) { assert(flags & WRITE_STRING_FILE_CREATE); - r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), - flags & WRITE_STRING_FILE_SYNC); + r = write_string_file_atomic(fn, line, flags, ts); if (r < 0) goto fail; @@ -148,16 +165,10 @@ int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags } } - r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts); + r = write_string_stream_ts(f, line, flags, ts); if (r < 0) goto fail; - if (flags & WRITE_STRING_FILE_SYNC) { - r = fflush_sync_and_check(f); - if (r < 0) - return r; - } - return 0; fail: @@ -247,11 +258,11 @@ int read_full_stream(FILE *f, char **contents, size_t *size) { if (st.st_size > READ_FULL_BYTES_MAX) return -E2BIG; - /* Start with the right file size, but be prepared for - * files from /proc which generally report a file size - * of 0 */ + /* Start with the right file size, but be prepared for files from /proc which generally report a file + * size of 0. Note that we increase the size to read here by one, so that the first read attempt + * already makes us notice the EOF. */ if (st.st_size > 0) - n = st.st_size; + n = st.st_size + 1; } l = 0; @@ -264,12 +275,13 @@ int read_full_stream(FILE *f, char **contents, size_t *size) { return -ENOMEM; buf = t; + errno = 0; k = fread(buf + l, 1, n - l, f); if (k > 0) l += k; if (ferror(f)) - return -errno; + return errno > 0 ? -errno : -EIO; if (feof(f)) break; diff --git a/src/basic/fileio.h b/src/basic/fileio.h index 1160c5849..5b3d045e0 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -36,7 +36,7 @@ typedef enum { WRITE_STRING_FILE_SYNC = 1<<4, } WriteStringFileFlags; -int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts); +int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts); static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) { return write_string_stream_ts(f, line, flags, NULL); } diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 33467c379..9f18a42ff 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -46,6 +46,9 @@ #include "user-util.h" #include "util.h" +/// Additional includes needed by elogind +#include "process-util.h" + int unlink_noerrno(const char *path) { PROTECT_ERRNO; int r; diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 51190b381..0a8f48d82 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -34,7 +34,7 @@ #include "strv.h" #include "util.h" -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP #include <pthread.h> #include "list.h" #endif @@ -142,7 +142,7 @@ typedef uint8_t dib_raw_t; #define DIB_FREE UINT_MAX -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP struct hashmap_debug_info { LIST_FIELDS(struct hashmap_debug_info, debug_list); unsigned max_entries; /* high watermark of n_entries */ @@ -499,7 +499,7 @@ static void base_remove_entry(HashmapBase *h, unsigned idx) { dibs = dib_raw_ptr(h); assert(dibs[idx] != DIB_RAW_FREE); -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP h->debug.rem_count++; h->debug.last_rem_idx = idx; #endif @@ -578,7 +578,7 @@ static unsigned hashmap_iterate_in_insertion_order(OrderedHashmap *h, Iterator * assert(e->p.b.key == i->next_key); } -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP i->prev_idx = idx; #endif @@ -635,7 +635,7 @@ static unsigned hashmap_iterate_in_internal_order(HashmapBase *h, Iterator *i) { } idx = i->idx; -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP i->prev_idx = idx; #endif @@ -658,7 +658,7 @@ static unsigned hashmap_iterate_entry(HashmapBase *h, Iterator *i) { return IDX_NIL; } -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP if (i->idx == IDX_FIRST) { i->put_count = h->debug.put_count; i->rem_count = h->debug.rem_count; @@ -750,7 +750,7 @@ static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enu shared_hash_key_initialized= true; } -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP h->debug.func = func; h->debug.file = file; h->debug.line = line; @@ -807,7 +807,7 @@ static void hashmap_free_no_clear(HashmapBase *h) { assert(!h->has_indirect); assert(!h->n_direct_entries); -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP assert_se(pthread_mutex_lock(&hashmap_debug_list_mutex) == 0); LIST_REMOVE(debug_list, hashmap_debug_list, &h->debug); assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0); @@ -919,7 +919,7 @@ static bool hashmap_put_robin_hood(HashmapBase *h, unsigned idx, dib_raw_t raw_dib, *dibs; unsigned dib, distance; -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP h->debug.put_count++; #endif @@ -1012,7 +1012,7 @@ static int hashmap_base_put_boldly(HashmapBase *h, unsigned idx, assert_se(hashmap_put_robin_hood(h, idx, swap) == false); n_entries_inc(h); -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP h->debug.max_entries = MAX(h->debug.max_entries, n_entries(h)); #endif @@ -1240,7 +1240,7 @@ int hashmap_replace(Hashmap *h, const void *key, void *value) { idx = bucket_scan(h, hash, key); if (idx != IDX_NIL) { e = plain_bucket_at(h, idx); -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP /* Although the key is equal, the key pointer may have changed, * and this would break our assumption for iterating. So count * this operation as incompatible with iteration. */ diff --git a/src/basic/hashmap.h b/src/basic/hashmap.h index 6d1ae48b2..c1089652d 100644 --- a/src/basic/hashmap.h +++ b/src/basic/hashmap.h @@ -58,7 +58,7 @@ typedef struct Set Set; /* Stores just keys */ typedef struct { unsigned idx; /* index of an entry to be iterated next */ const void *next_key; /* expected value of that entry's key pointer */ -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP unsigned put_count; /* hashmap's put_count recorded at start of iteration */ unsigned rem_count; /* hashmap's rem_count in previous iteration */ unsigned prev_idx; /* idx in previous iteration */ @@ -89,7 +89,7 @@ typedef struct { (Hashmap*)(h), \ (void)0) -#ifdef ENABLE_DEBUG_HASHMAP +#if ENABLE_DEBUG_HASHMAP # define HASHMAP_DEBUG_PARAMS , const char *func, const char *file, int line # define HASHMAP_DEBUG_SRC_ARGS , __func__, __FILE__, __LINE__ # define HASHMAP_DEBUG_PASS_ARGS , func, file, line diff --git a/src/basic/memfd-util.c b/src/basic/memfd-util.c index e857c040d..ad80d5662 100644 --- a/src/basic/memfd-util.c +++ b/src/basic/memfd-util.c @@ -21,7 +21,7 @@ #include <fcntl.h> #include <sys/stat.h> #include <unistd.h> -#ifdef HAVE_LINUX_MEMFD_H +#if HAVE_LINUX_MEMFD_H #include <linux/memfd.h> #endif #include <stdio.h> diff --git a/src/basic/meson.build b/src/basic/meson.build index 19e679fac..c1712f2db 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -295,6 +295,7 @@ basic_sources_plain = files(''' rm-rf.h selinux-util.c selinux-util.h + set.c set.h signal-util.c signal-util.h @@ -350,7 +351,7 @@ generate_gperfs = find_program('generate-gperfs.py') # output : 'af-list.txt', # command : [generate_af_list, cpp], # capture : true) -# +# # generate_arphrd_list = find_program('generate-arphrd-list.sh') # arphrd_list_txt = custom_target( # 'arphrd-list.txt', diff --git a/src/basic/missing.h b/src/basic/missing.h index f2f727822..7768b07e8 100644 --- a/src/basic/missing.h +++ b/src/basic/missing.h @@ -43,7 +43,7 @@ /// Additional includes needed by elogind #include "musl_missing.h" -#ifdef HAVE_AUDIT +#if HAVE_AUDIT #include <libaudit.h> #endif @@ -51,12 +51,12 @@ #include <asm/sgidefs.h> #endif -#ifdef HAVE_LINUX_BTRFS_H +#if HAVE_LINUX_BTRFS_H #include <linux/btrfs.h> #endif #if 0 /// UNNEEDED by elogind -#ifdef HAVE_LINUX_VM_SOCKETS_H +#if HAVE_LINUX_VM_SOCKETS_H #include <linux/vm_sockets.h> #else #define VMADDR_CID_ANY -1U @@ -210,7 +210,7 @@ struct sockaddr_vm { #endif #if 0 /// UNNEEDED by elogind (It can not support BTRFS at all) -#ifndef HAVE_LINUX_BTRFS_H +#if ! HAVE_LINUX_BTRFS_H struct btrfs_ioctl_vol_args { int64_t fd; char name[BTRFS_PATH_NAME_MAX + 1]; @@ -553,8 +553,8 @@ struct btrfs_ioctl_quota_ctl_args { #define MAX_HANDLE_SZ 128 #endif -#ifndef HAVE_SECURE_GETENV -# ifdef HAVE___SECURE_GETENV +#if ! HAVE_SECURE_GETENV +# if HAVE___SECURE_GETENV # define secure_getenv __secure_getenv # else # error "neither secure_getenv nor __secure_getenv are available" @@ -1117,7 +1117,7 @@ struct input_mask { #endif #endif // 0 -#ifndef HAVE_KEY_SERIAL_T +#if ! HAVE_KEY_SERIAL_T typedef int32_t key_serial_t; #endif @@ -1213,11 +1213,11 @@ typedef int32_t key_serial_t; #ifndef IF_OPER_UP #define IF_OPER_UP 6 -#ifndef HAVE_CHAR32_T +#if ! HAVE_CHAR32_T #define char32_t uint32_t #endif -#ifndef HAVE_CHAR16_T +#if ! HAVE_CHAR16_T #define char16_t uint16_t #endif @@ -1230,7 +1230,7 @@ typedef int32_t key_serial_t; #endif #if 0 /// UNNEEDED by elogind -#ifndef HAVE_STRUCT_ETHTOOL_LINK_SETTINGS +#if ! HAVE_STRUCT_ETHTOOL_LINK_SETTINGS #define ETHTOOL_GLINKSETTINGS 0x0000004c /* Get ethtool_link_settings */ #define ETHTOOL_SLINKSETTINGS 0x0000004d /* Set ethtool_link_settings */ @@ -1258,7 +1258,7 @@ struct ethtool_link_settings { #endif #endif // 0 -#ifndef HAVE_STRUCT_FIB_RULE_UID_RANGE +#if ! HAVE_STRUCT_FIB_RULE_UID_RANGE struct fib_rule_uid_range { __u32 start; diff --git a/src/basic/path-util.h b/src/basic/path-util.h index 0d162bac2..ed305e012 100644 --- a/src/basic/path-util.h +++ b/src/basic/path-util.h @@ -30,7 +30,7 @@ #define DEFAULT_PATH_NORMAL "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin" #define DEFAULT_PATH_SPLIT_USR DEFAULT_PATH_NORMAL ":/sbin:/bin" -#ifdef HAVE_SPLIT_USR +#if HAVE_SPLIT_USR # define DEFAULT_PATH DEFAULT_PATH_SPLIT_USR #else # define DEFAULT_PATH DEFAULT_PATH_NORMAL diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 62c7acfa2..708b45aa0 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -27,13 +27,14 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/mman.h> #include <sys/personality.h> #include <sys/prctl.h> #include <sys/types.h> #include <sys/wait.h> #include <syslog.h> #include <unistd.h> -#ifdef HAVE_VALGRIND_VALGRIND_H +#if HAVE_VALGRIND_VALGRIND_H #include <valgrind/valgrind.h> #endif @@ -48,6 +49,7 @@ #include "macro.h" #include "missing.h" #include "process-util.h" +//#include "raw-clone.h" #include "signal-util.h" //#include "stat-util.h" #include "string-table.h" @@ -478,7 +480,7 @@ static int get_process_id(pid_t pid, const char *field, uid_t *uid) { assert(field); assert(uid); - if (pid < 0) + if (!pid_is_valid(pid)) return -EINVAL; p = procfs_file_alloca(pid, "status"); @@ -793,7 +795,7 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) { bool pid_is_unwaited(pid_t pid) { /* Checks whether a PID is still valid at all, including a zombie */ - if (pid < 0) + if (!pid_is_valid(pid)) return false; if (pid <= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */ @@ -813,7 +815,7 @@ bool pid_is_alive(pid_t pid) { /* Checks whether a PID is still valid and not a zombie */ - if (pid < 0) + if (!pid_is_valid(pid)) return false; if (pid <= 1) /* If we or PID 1 would be a zombie, this code would not be running */ @@ -833,7 +835,7 @@ bool pid_is_alive(pid_t pid) { int pid_from_same_root_fs(pid_t pid) { const char *root; - if (pid < 0) + if (!pid_is_valid(pid)) return false; if (pid == 0 || pid == getpid_cached()) @@ -954,7 +956,7 @@ int opinionated_personality(unsigned long *ret) { } void valgrind_summary_hack(void) { -#ifdef HAVE_VALGRIND_VALGRIND_H +#if HAVE_VALGRIND_VALGRIND_H if (getpid_cached() == 1 && RUNNING_ON_VALGRIND) { pid_t pid; pid = raw_clone(SIGCHLD); diff --git a/src/basic/random-util.c b/src/basic/random-util.c index 42f484d68..146c8f55e 100644 --- a/src/basic/random-util.c +++ b/src/basic/random-util.c @@ -26,11 +26,11 @@ #include <linux/random.h> #include <stdint.h> -#ifdef HAVE_SYS_AUXV_H +#if HAVE_SYS_AUXV_H # include <sys/auxv.h> #endif -#ifdef USE_SYS_RANDOM_H +#if USE_SYS_RANDOM_H # include <sys/random.h> #else # include <linux/random.h> @@ -100,14 +100,14 @@ int acquire_random_bytes(void *p, size_t n, bool high_quality_required) { void initialize_srand(void) { static bool srand_called = false; unsigned x; -#ifdef HAVE_SYS_AUXV_H +#if HAVE_SYS_AUXV_H void *auxv; #endif if (srand_called) return; -#ifdef HAVE_SYS_AUXV_H +#if HAVE_SYS_AUXV_H /* The kernel provides us with 16 bytes of entropy in auxv, so let's * try to make use of that to seed the pseudo-random generator. It's * better than nothing... */ diff --git a/src/basic/selinux-util.c b/src/basic/selinux-util.c index c7a04ad76..51b807b85 100644 --- a/src/basic/selinux-util.c +++ b/src/basic/selinux-util.c @@ -26,7 +26,7 @@ #include <sys/un.h> #include <syslog.h> -#ifdef HAVE_SELINUX +#if HAVE_SELINUX #include <selinux/context.h> #include <selinux/label.h> #include <selinux/selinux.h> @@ -40,7 +40,7 @@ #include "time-util.h" #include "util.h" -#ifdef HAVE_SELINUX +#if HAVE_SELINUX DEFINE_TRIVIAL_CLEANUP_FUNC(char*, freecon); DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free); @@ -54,7 +54,7 @@ static struct selabel_handle *label_hnd = NULL; #endif bool mac_selinux_use(void) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX if (cached_use < 0) cached_use = is_selinux_enabled() > 0; @@ -65,7 +65,7 @@ bool mac_selinux_use(void) { } void mac_selinux_retest(void) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX cached_use = -1; #endif } @@ -73,7 +73,7 @@ void mac_selinux_retest(void) { int mac_selinux_init(void) { int r = 0; -#ifdef HAVE_SELINUX +#if HAVE_SELINUX usec_t before_timestamp, after_timestamp; struct mallinfo before_mallinfo, after_mallinfo; @@ -110,7 +110,7 @@ int mac_selinux_init(void) { void mac_selinux_finish(void) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX if (!label_hnd) return; @@ -121,7 +121,7 @@ void mac_selinux_finish(void) { int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX struct stat st; int r; @@ -170,7 +170,7 @@ int mac_selinux_fix(const char *path, bool ignore_enoent, bool ignore_erofs) { #if 0 /// UNNEDED by elogind int mac_selinux_apply(const char *path, const char *label) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX if (!mac_selinux_use()) return 0; @@ -189,7 +189,7 @@ int mac_selinux_apply(const char *path, const char *label) { int mac_selinux_get_create_label_from_exe(const char *exe, char **label) { int r = -EOPNOTSUPP; -#ifdef HAVE_SELINUX +#if HAVE_SELINUX _cleanup_freecon_ char *mycon = NULL, *fcon = NULL; security_class_t sclass; @@ -221,7 +221,7 @@ int mac_selinux_get_our_label(char **label) { assert(label); -#ifdef HAVE_SELINUX +#if HAVE_SELINUX if (!mac_selinux_use()) return -EOPNOTSUPP; @@ -236,7 +236,7 @@ int mac_selinux_get_our_label(char **label) { int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char *exec_label, char **label) { int r = -EOPNOTSUPP; -#ifdef HAVE_SELINUX +#if HAVE_SELINUX _cleanup_freecon_ char *mycon = NULL, *peercon = NULL, *fcon = NULL; _cleanup_context_free_ context_t pcon = NULL, bcon = NULL; security_class_t sclass; @@ -297,7 +297,7 @@ int mac_selinux_get_child_mls_label(int socket_fd, const char *exe, const char * char* mac_selinux_free(char *label) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX if (!label) return NULL; @@ -314,7 +314,7 @@ char* mac_selinux_free(char *label) { int mac_selinux_create_file_prepare(const char *path, mode_t mode) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX _cleanup_freecon_ char *filecon = NULL; int r; @@ -357,7 +357,7 @@ int mac_selinux_create_file_prepare(const char *path, mode_t mode) { void mac_selinux_create_file_clear(void) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX PROTECT_ERRNO; if (!mac_selinux_use()) @@ -370,7 +370,7 @@ void mac_selinux_create_file_clear(void) { #if 0 /// UNNEEDED by elogind int mac_selinux_create_socket_prepare(const char *label) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX if (!mac_selinux_use()) return 0; @@ -389,7 +389,7 @@ int mac_selinux_create_socket_prepare(const char *label) { void mac_selinux_create_socket_clear(void) { -#ifdef HAVE_SELINUX +#if HAVE_SELINUX PROTECT_ERRNO; if (!mac_selinux_use()) @@ -403,7 +403,7 @@ int mac_selinux_bind(int fd, const struct sockaddr *addr, socklen_t addrlen) { /* Binds a socket and label its file system object according to the SELinux policy */ -#ifdef HAVE_SELINUX +#if HAVE_SELINUX _cleanup_freecon_ char *fcon = NULL; const struct sockaddr_un *un; bool context_changed = false; diff --git a/src/basic/set.c b/src/basic/set.c new file mode 100644 index 000000000..fd398b821 --- /dev/null +++ b/src/basic/set.c @@ -0,0 +1,61 @@ +/*** + This file is part of systemd. + + Copyright 2017 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "set.h" + +int set_make(Set **ret, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS, void *add, ...) { + _cleanup_set_free_ Set *s = NULL; + int r; + + assert(ret); + + s = set_new(hash_ops HASHMAP_DEBUG_PASS_ARGS); + if (!s) + return -ENOMEM; + + if (add) { + va_list ap; + + r = set_put(s, add); + if (r < 0) + return r; + + va_start(ap, add); + + for(;;) { + void *arg = va_arg(ap, void*); + + if (!arg) + break; + + r = set_put(s, arg); + if (r < 0) { + va_end(ap); + return r; + } + } + + va_end(ap); + } + + *ret = s; + s = NULL; + + return 0; +} diff --git a/src/basic/smack-util.c b/src/basic/smack-util.c index 9bcc4a3a2..46ec68975 100644 --- a/src/basic/smack-util.c +++ b/src/basic/smack-util.c @@ -35,7 +35,7 @@ #include "string-table.h" #include "xattr-util.h" -#ifdef HAVE_SMACK +#if HAVE_SMACK bool mac_smack_use(void) { static int cached_use = -1; diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index fec5ee12e..3d622bb1c 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -48,7 +48,7 @@ #include "util.h" #if 0 /// UNNEEDED by elogind -#ifdef ENABLE_IDN +#if ENABLE_IDN # define IDN_FLAGS (NI_IDN|NI_IDN_USE_STD3_ASCII_RULES) #else # define IDN_FLAGS 0 diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index b83e778b2..d205870c2 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -47,7 +47,6 @@ #include "log.h" #include "macro.h" #include "parse-util.h" -#include "path-util.h" #include "process-util.h" #include "socket-util.h" #include "stat-util.h" @@ -57,6 +56,9 @@ #include "time-util.h" #include "util.h" +/// Additional includes needed by elogind +#include "path-util.h" + static volatile unsigned cached_columns = 0; static volatile unsigned cached_lines = 0; diff --git a/src/basic/time-util.c b/src/basic/time-util.c index f0b9cc183..fc94c945e 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -861,11 +861,11 @@ parse_usec: } from_tm: - x = mktime_or_timegm(&tm, utc); - if (x < 0) + if (weekday >= 0 && tm.tm_wday != weekday) return -EINVAL; - if (weekday >= 0 && tm.tm_wday != weekday) + x = mktime_or_timegm(&tm, utc); + if (x < 0) return -EINVAL; ret = (usec_t) x * USEC_PER_SEC + x_usec; @@ -895,20 +895,18 @@ typedef struct ParseTimestampResult { } ParseTimestampResult; int parse_timestamp(const char *t, usec_t *usec) { - char *last_space, *timezone = NULL; + char *last_space, *tz = NULL; ParseTimestampResult *shared, tmp; int r; pid_t pid; last_space = strrchr(t, ' '); if (last_space != NULL && timezone_is_valid(last_space + 1)) - timezone = last_space + 1; + tz = last_space + 1; - if (timezone == NULL || endswith_no_case(t, " UTC")) + if (tz == NULL || endswith_no_case(t, " UTC")) return parse_timestamp_impl(t, usec, false); - t = strndupa(t, last_space - t); - shared = mmap(NULL, sizeof *shared, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0); if (shared == MAP_FAILED) return negative_errno(); @@ -922,14 +920,24 @@ int parse_timestamp(const char *t, usec_t *usec) { } if (pid == 0) { - if (setenv("TZ", timezone, 1) != 0) { + bool with_tz = true; + + if (setenv("TZ", tz, 1) != 0) { shared->return_value = negative_errno(); _exit(EXIT_FAILURE); } tzset(); - shared->return_value = parse_timestamp_impl(t, &shared->usec, true); + /* If there is a timezone that matches the tzname fields, leave the parsing to the implementation. + * Otherwise just cut it off */ + with_tz = !STR_IN_SET(tz, tzname[0], tzname[1]); + + /*cut off the timezone if we dont need it*/ + if (with_tz) + t = strndupa(t, last_space - t); + + shared->return_value = parse_timestamp_impl(t, &shared->usec, with_tz); _exit(EXIT_SUCCESS); } diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index a2c5ea1a9..4a5a22f9b 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -67,7 +67,7 @@ typedef struct MountPoint { * fourth (securityfs) is needed by IMA to load a custom policy. The * other ones we can delay until SELinux and IMA are loaded. When * SMACK is enabled we need smackfs, too, so it's a fifth one. */ -#ifdef HAVE_SMACK +#if HAVE_SMACK #define N_EARLY_MOUNT 5 #else #define N_EARLY_MOUNT 4 @@ -83,7 +83,7 @@ static const MountPoint mount_table[] = { NULL, MNT_FATAL|MNT_IN_CONTAINER }, { "securityfs", "/sys/kernel/security", "securityfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL, MNT_NONE }, -#ifdef HAVE_SMACK +#if HAVE_SMACK { "smackfs", "/sys/fs/smackfs", "smackfs", "smackfsdef=*", MS_NOSUID|MS_NOEXEC|MS_NODEV, mac_smack_use, MNT_FATAL }, { "tmpfs", "/dev/shm", "tmpfs", "mode=1777,smackfsroot=*", MS_NOSUID|MS_NODEV|MS_STRICTATIME, @@ -93,7 +93,7 @@ static const MountPoint mount_table[] = { NULL, MNT_FATAL|MNT_IN_CONTAINER }, { "devpts", "/dev/pts", "devpts", "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, NULL, MNT_IN_CONTAINER }, -#ifdef HAVE_SMACK +#if HAVE_SMACK { "tmpfs", "/run", "tmpfs", "mode=755,smackfsroot=*", MS_NOSUID|MS_NODEV|MS_STRICTATIME, mac_smack_use, MNT_FATAL }, #endif @@ -117,7 +117,7 @@ static const MountPoint mount_table[] = { cg_is_legacy_wanted, MNT_FATAL|MNT_IN_CONTAINER }, { "pstore", "/sys/fs/pstore", "pstore", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL, MNT_NONE }, -#ifdef ENABLE_EFI +#if ENABLE_EFI { "efivarfs", "/sys/firmware/efi/efivars", "efivarfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, is_efi_boot, MNT_NONE }, #endif @@ -351,7 +351,7 @@ int mount_cgroup_controllers(char ***join_controllers) { return 0; } -#if defined(HAVE_SELINUX) || defined(HAVE_SMACK) +#if HAVE_SELINUX || HAVE_SMACK static int nftw_cb( const char *fpath, const struct stat *sb, @@ -384,7 +384,7 @@ int mount_setup(bool loaded_policy) { return r; #if 0 /// elogind does not control /, /dev, /run and /run/systemd/* are setup elsewhere. -#if defined(HAVE_SELINUX) || defined(HAVE_SMACK) +#if HAVE_SELINUX || HAVE_SMACK /* Nodes in devtmpfs and /run need to be manually updated for * the appropriate labels, after mounting. The other virtual * API file systems like /sys and /proc do not need that, they diff --git a/src/libelogind/sd-bus/bus-control.c b/src/libelogind/sd-bus/bus-control.c index 226e54e03..23f83a631 100644 --- a/src/libelogind/sd-bus/bus-control.c +++ b/src/libelogind/sd-bus/bus-control.c @@ -17,7 +17,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#ifdef HAVE_VALGRIND_MEMCHECK_H +#if HAVE_VALGRIND_MEMCHECK_H #include <valgrind/memcheck.h> #endif diff --git a/src/libelogind/sd-bus/bus-kernel.c b/src/libelogind/sd-bus/bus-kernel.c index 43032efd1..edb9b6460 100644 --- a/src/libelogind/sd-bus/bus-kernel.c +++ b/src/libelogind/sd-bus/bus-kernel.c @@ -17,7 +17,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#ifdef HAVE_VALGRIND_MEMCHECK_H +#if HAVE_VALGRIND_MEMCHECK_H #include <valgrind/memcheck.h> #endif diff --git a/src/libelogind/sd-bus/sd-bus.c b/src/libelogind/sd-bus/sd-bus.c index a674b9ea8..107fc363c 100644 --- a/src/libelogind/sd-bus/sd-bus.c +++ b/src/libelogind/sd-bus/sd-bus.c @@ -53,6 +53,9 @@ #include "strv.h" #include "util.h" +/// Additional includes needed by elogind +#include "process-util.h" + #define log_debug_bus_message(m) \ do { \ sd_bus_message *_mm = (m); \ diff --git a/src/libelogind/sd-daemon/sd-daemon.c b/src/libelogind/sd-daemon/sd-daemon.c index 78274e167..87e7f7842 100644 --- a/src/libelogind/sd-daemon/sd-daemon.c +++ b/src/libelogind/sd-daemon/sd-daemon.c @@ -42,6 +42,9 @@ #include "strv.h" #include "util.h" +/// Additional includes needed by elogind +#include "process-util.h" + #define SNDBUF_SIZE (8*1024*1024) static void unsetenv_all(bool unset_environment) { diff --git a/src/login/logind-acl.h b/src/login/logind-acl.h index 5587becca..df25beca2 100644 --- a/src/login/logind-acl.h +++ b/src/login/logind-acl.h @@ -28,7 +28,7 @@ #include <libudev.h> #endif // 0 -#ifdef HAVE_ACL +#if HAVE_ACL int devnode_acl(const char *path, bool flush, diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 4d75e3e15..63cd1a4d4 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -348,7 +348,7 @@ static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd r = sd_bus_message_read(message, "u", &pid); if (r < 0) return r; - if (pid < 0) + if (!pid_is_valid((pid_t) pid)) return -EINVAL; if (pid == 0) { @@ -411,7 +411,7 @@ static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bu r = sd_bus_message_read(message, "u", &pid); if (r < 0) return r; - if (pid < 0) + if (!pid_is_valid((pid_t) pid)) return -EINVAL; if (pid == 0) { diff --git a/src/login/logind-session.c b/src/login/logind-session.c index d2125112d..8712dae62 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -33,7 +33,6 @@ #include "bus-error.h" #include "bus-util.h" #include "escape.h" -#include "extract-word.h" #include "fd-util.h" #include "fileio.h" #include "format-util.h" @@ -42,11 +41,14 @@ #include "mkdir.h" #include "parse-util.h" #include "path-util.h" +#include "process-util.h" #include "string-table.h" #include "terminal-util.h" #include "user-util.h" #include "util.h" -#include "process-util.h" + +/// Additional includes needed by elogind +#include "extract-word.h" #define RELEASE_USEC (20*USEC_PER_SEC) diff --git a/src/login/meson.build b/src/login/meson.build index d98b4f43d..aff6e3f2a 100644 --- a/src/login/meson.build +++ b/src/login/meson.build @@ -61,7 +61,7 @@ liblogind_core_sources += [files(''' update_utmp_files] #endif // 1 logind_acl_c = files('logind-acl.c') -if conf.get('HAVE_ACL', false) +if conf.get('HAVE_ACL') == 1 liblogind_core_sources += logind_acl_c endif @@ -89,7 +89,7 @@ loginctl_sources += files(''' '''.split()) #endif // 1 #if 0 /// elogind builds itself unconditionally and with some adaptations -# if conf.get('ENABLE_LOGIND', false) +# if conf.get('ENABLE_LOGIND') == 1 # logind_conf = configure_file( # input : 'logind.conf.in', # output : 'logind.conf', diff --git a/src/shared/acl-util.h b/src/shared/acl-util.h index 133996d77..d4613f33c 100644 --- a/src/shared/acl-util.h +++ b/src/shared/acl-util.h @@ -19,7 +19,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ -#ifdef HAVE_ACL +#if HAVE_ACL #include <acl/libacl.h> #include <stdbool.h> diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index f3579d07e..0fb28c24d 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -257,7 +257,7 @@ int bus_test_polkit( return r; else if (r > 0) return 1; -#ifdef ENABLE_POLKIT +#if ENABLE_POLKIT else { _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; @@ -336,7 +336,7 @@ int bus_test_polkit( return -EACCES; } -#ifdef ENABLE_POLKIT +#if ENABLE_POLKIT typedef struct AsyncPolkitQuery { sd_bus_message *request, *reply; @@ -400,7 +400,7 @@ int bus_verify_polkit_async( Hashmap **registry, sd_bus_error *error) { -#ifdef ENABLE_POLKIT +#if ENABLE_POLKIT _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL; AsyncPolkitQuery *q; const char *sender, **k, **v; @@ -418,7 +418,7 @@ int bus_verify_polkit_async( if (r != 0) return r; -#ifdef ENABLE_POLKIT +#if ENABLE_POLKIT q = hashmap_get(*registry, call); if (q) { int authorized, challenge; @@ -465,7 +465,7 @@ int bus_verify_polkit_async( else if (r > 0) return 1; -#ifdef ENABLE_POLKIT +#if ENABLE_POLKIT if (sd_bus_get_current_message(call->bus) != call) return -EINVAL; @@ -554,7 +554,7 @@ int bus_verify_polkit_async( } void bus_verify_polkit_async_registry_free(Hashmap *registry) { -#ifdef ENABLE_POLKIT +#if ENABLE_POLKIT AsyncPolkitQuery *q; while ((q = hashmap_steal_first(registry))) diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index ab66bfe60..8dff2886c 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -43,6 +43,10 @@ #include "time-util.h" #include "utf8.h" +/// Additional includes needed by elogind +#include "def.h" +#include "fileio.h" + int config_item_table_lookup( const void *table, const char *section, @@ -315,24 +319,44 @@ int config_parse(const char *unit, fd_warn_permissions(filename, fileno(f)); for (;;) { - char buf[LINE_MAX], *l, *p, *c = NULL, *e; + _cleanup_free_ char *buf = NULL; + char *l, *p, *c = NULL, *e; bool escaped = false; - if (!fgets(buf, sizeof buf, f)) { - if (feof(f)) - break; + r = read_line(f, LONG_LINE_MAX, &buf); + if (r == 0) + break; + if (r == -ENOBUFS) { + if (warn) + log_error_errno(r, "%s:%u: Line too long", filename, line); - return log_error_errno(errno, "Failed to read configuration file '%s': %m", filename); + return r; + } + if (r < 0) { + if (warn) + log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line); + + return r; } l = buf; - if (allow_bom && startswith(l, UTF8_BYTE_ORDER_MARK)) - l += strlen(UTF8_BYTE_ORDER_MARK); - allow_bom = false; + if (allow_bom) { + char *q; - truncate_nl(l); + q = startswith(buf, UTF8_BYTE_ORDER_MARK); + if (q) { + l = q; + allow_bom = false; + } + } if (continuation) { + if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) { + if (warn) + log_error("%s:%u: Continuation line too long", filename, line); + return -ENOBUFS; + } + c = strappend(continuation, l); if (!c) { if (warn) @@ -386,8 +410,7 @@ int config_parse(const char *unit, if (r < 0) { if (warn) - log_warning_errno(r, "Failed to parse file '%s': %m", - filename); + log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line); return r; } } diff --git a/src/shared/meson.build b/src/shared/meson.build index 06ecaf633..172f55ba7 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -127,20 +127,20 @@ shared_sources = ''' test_tables_h = files('test-tables.h') shared_sources += [test_tables_h] -if conf.get('HAVE_ACL', false) +if conf.get('HAVE_ACL') == 1 shared_sources += ['acl-util.c'] endif -if conf.get('HAVE_UTMP', false) +if conf.get('HAVE_UTMP') == 1 shared_sources += ['utmp-wtmp.c'] endif #if 0 /// UNNEEDED by elogind -# if conf.get('HAVE_SECCOMP', false) +# if conf.get('HAVE_SECCOMP') == 1 # shared_sources += ['seccomp-util.c'] # endif # -# if conf.get('HAVE_LIBIPTC', false) +# if conf.get('HAVE_LIBIPTC') == 1 # shared_sources += ['firewall-util.c'] # endif #endif // 0 diff --git a/src/shared/spawn-polkit-agent.c b/src/shared/spawn-polkit-agent.c index 7dae4d14f..9a4014766 100644 --- a/src/shared/spawn-polkit-agent.c +++ b/src/shared/spawn-polkit-agent.c @@ -33,7 +33,7 @@ #include "time-util.h" #include "util.h" -#ifdef ENABLE_POLKIT +#if ENABLE_POLKIT static pid_t agent_pid = 0; int polkit_agent_open(void) { diff --git a/src/shared/udev-util.c b/src/shared/udev-util.c index ed32f0305..e0b8980f3 100644 --- a/src/shared/udev-util.c +++ b/src/shared/udev-util.c @@ -19,12 +19,14 @@ #include <string.h> -#include "alloc-util.h" #include "fileio.h" #include "log.h" #include "string-util.h" #include "udev-util.h" +/// Additional includes needed by elogind +#include "alloc-util.h" + int udev_parse_config(void) { _cleanup_free_ char *val = NULL; const char *log; diff --git a/src/shared/utmp-wtmp.h b/src/shared/utmp-wtmp.h index 438e270a2..8da594100 100644 --- a/src/shared/utmp-wtmp.h +++ b/src/shared/utmp-wtmp.h @@ -25,7 +25,7 @@ #include "time-util.h" #include "util.h" -#ifdef HAVE_UTMP +#if HAVE_UTMP int utmp_get_runlevel(int *runlevel, int *previous); int utmp_put_shutdown(void); diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 01fa22349..c6dd13197 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -69,7 +69,7 @@ static int write_state(FILE **f, char **states) { STRV_FOREACH(state, states) { int k; - k = write_string_stream(*f, *state, true); + k = write_string_stream(*f, *state, 0); if (k == 0) return 0; log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m", diff --git a/src/systemd/sd-messages.h b/src/systemd/sd-messages.h index 4bc248a4b..8c2348677 100644 --- a/src/systemd/sd-messages.h +++ b/src/systemd/sd-messages.h @@ -103,6 +103,9 @@ _SD_BEGIN_DECLARATIONS; #define SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR \ SD_ID128_MAKE_STR(5e,b0,34,94,b6,58,48,70,a5,36,b3,37,29,08,09,b3) +#define SD_MESSAGE_UNIT_RESOURCES SD_ID128_MAKE(ae,8f,7b,86,6b,03,47,b9,af,31,fe,1c,80,b1,27,c0) +#define SD_MESSAGE_UNIT_RESOURCES_STR SD_ID128_MAKE_STR(ae,8f,7b,86,6b,03,47,b9,af,31,fe,1c,80,b1,27,c0) + #define SD_MESSAGE_SPAWN_FAILED SD_ID128_MAKE(64,12,57,65,1c,1b,4e,c9,a8,62,4d,7a,40,a9,e1,e7) #define SD_MESSAGE_SPAWN_FAILED_STR SD_ID128_MAKE_STR(64,12,57,65,1c,1b,4e,c9,a8,62,4d,7a,40,a9,e1,e7) diff --git a/src/test/meson.build b/src/test/meson.build index c811dd675..a923d6144 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -35,10 +35,10 @@ test_libelogind_sym_c = custom_target( # output : 'test-libudev-sym.c', # command : [generate_sym_test_py, '@INPUT0@', '@INPUT1@'], # capture : true) -# -# test_dlopen_c = files('test-dlopen.c') #endif // 0 +test_dlopen_c = files('test-dlopen.c') + ############################################################ tests += [ @@ -365,6 +365,7 @@ tests += [ [libbasic], []], +#if 0 /// UNNEEDED in elogind # [['src/test/test-bpf.c', # 'src/test/test-helper.c'], # [libcore, @@ -375,6 +376,7 @@ tests += [ # libseccomp, # libselinux, # libblkid]], +#endif // 0 [['src/test/test-hashmap.c', 'src/test/test-hashmap-plain.c', diff --git a/src/test/test-conf-parser.c b/src/test/test-conf-parser.c index a66ed1bd3..17d12ec76 100644 --- a/src/test/test-conf-parser.c +++ b/src/test/test-conf-parser.c @@ -24,13 +24,15 @@ #include "strv.h" #include "util.h" +/// Additional includes needed by elogind +#include "fd-util.h" +#include "fileio.h" + static void test_config_parse_path_one(const char *rvalue, const char *expected) { - char *path = NULL; + _cleanup_free_ char *path = NULL; assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0); assert_se(streq_ptr(expected, path)); - - free(path); } static void test_config_parse_log_level_one(const char *rvalue, int expected) { @@ -80,12 +82,10 @@ static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected } static void test_config_parse_strv_one(const char *rvalue, char **expected) { - char **strv = 0; + _cleanup_strv_free_ char **strv = NULL; assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0); assert_se(strv_equal(expected, strv)); - - strv_free(strv); } static void test_config_parse_mode_one(const char *rvalue, mode_t expected) { @@ -239,7 +239,130 @@ static void test_config_parse_iec_uint64(void) { } #endif // 0 +#define x10(x) x x x x x x x x x x +#define x100(x) x10(x10(x)) +#define x1000(x) x10(x100(x)) + +static const char* const config_file[] = { + "[Section]\n" + "setting1=1\n", + + "[Section]\n" + "setting1=1", /* no terminating newline */ + + "\n\n\n\n[Section]\n\n\n" + "setting1=1", /* some whitespace, no terminating newline */ + + "[Section]\n" + "[Section]\n" + "setting1=1\n" + "setting1=2\n" + "setting1=1\n", /* repeated settings */ + + "[Section]\n" + "setting1=1\\\n" /* normal continuation */ + "2\\\n" + "3\n", + + "[Section]\n" + "setting1=1\\\\\\\n" /* continuation with trailing escape symbols */ + "\\\\2\n", /* note that C requires one level of escaping, so the + * parser gets "…1 BS BS BS NL BS BS 2 NL", which + * it translates into "…1 BS BS SP BS BS 2" */ + + "\n[Section]\n\n" + "setting1=" /* a line above LINE_MAX length */ + x1000("ABCD") + "\n", + + "[Section]\n" + "setting1=" /* a line above LINE_MAX length, with continuation */ + x1000("ABCD") "\\\n" + "foobar", + + "[Section]\n" + "setting1=" /* a line above the allowed limit: 9 + 1050000 + 1 */ + x1000(x1000("x") x10("abcde")) "\n", + + "[Section]\n" + "setting1=" /* many continuation lines, together above the limit */ + x1000(x1000("x") x10("abcde") "\\\n") "xxx", +}; + +static void test_config_parse(unsigned i, const char *s) { + char name[] = "/tmp/test-conf-parser.XXXXXX"; + int fd, r; + _cleanup_fclose_ FILE *f = NULL; + _cleanup_free_ char *setting1 = NULL; + + const ConfigTableItem items[] = { + { "Section", "setting1", config_parse_string, 0, &setting1}, + {} + }; + + log_info("== %s[%i] ==", __func__, i); + + fd = mkostemp_safe(name); + assert_se(fd >= 0); + assert_se((size_t) write(fd, s, strlen(s)) == strlen(s)); + + assert_se(lseek(fd, 0, SEEK_SET) == 0); + assert_se(f = fdopen(fd, "r")); + + /* + int config_parse(const char *unit, + const char *filename, + FILE *f, + const char *sections, + ConfigItemLookup lookup, + const void *table, + bool relaxed, + bool allow_include, + bool warn, + void *userdata) + */ + + r = config_parse(NULL, name, f, + "Section\0", + config_item_table_lookup, items, + false, false, true, NULL); + + switch (i) { + case 0 ... 3: + assert_se(r == 0); + assert_se(streq(setting1, "1")); + break; + + case 4: + assert_se(r == 0); + assert_se(streq(setting1, "1 2 3")); + break; + + case 5: + assert_se(r == 0); + assert_se(streq(setting1, "1\\\\ \\\\2")); + break; + + case 6: + assert_se(r == 0); + assert_se(streq(setting1, x1000("ABCD"))); + break; + + case 7: + assert_se(r == 0); + assert_se(streq(setting1, x1000("ABCD") " foobar")); + break; + + case 8 ... 9: + assert_se(r == -ENOBUFS); + assert_se(setting1 == NULL); + break; + } +} + int main(int argc, char **argv) { + unsigned i; + log_parse_environment(); log_open(); @@ -262,5 +385,8 @@ int main(int argc, char **argv) { test_config_parse_iec_uint64(); #endif // 0 + for (i = 0; i < ELEMENTSOF(config_file); i++) + test_config_parse(i, config_file[i]); + return 0; } diff --git a/src/test/test-dlopen.c b/src/test/test-dlopen.c new file mode 100644 index 000000000..9f5343a7e --- /dev/null +++ b/src/test/test-dlopen.c @@ -0,0 +1,32 @@ +/*** + This file is part of systemd. + + Copyright 2016 Zbigniew Jędrzejewski-Szmek + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <dlfcn.h> +#include <stdlib.h> + +#include "macro.h" + +int main(int argc, char **argv) { + void *handle; + + assert_se((handle = dlopen(argv[1], RTLD_NOW))); + assert_se(dlclose(handle) == 0); + + return EXIT_SUCCESS; +} diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 19ffe01d7..c573f4127 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -26,7 +26,7 @@ #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> -#ifdef HAVE_VALGRIND_VALGRIND_H +#if HAVE_VALGRIND_VALGRIND_H #include <valgrind/valgrind.h> #endif @@ -176,7 +176,7 @@ static void test_get_process_cmdline_harder(void) { if (geteuid() != 0) return; -#ifdef HAVE_VALGRIND_VALGRIND_H +#if HAVE_VALGRIND_VALGRIND_H /* valgrind patches open(/proc//cmdline) * so, test_get_process_cmdline_harder fails always * See https://github.com/systemd/systemd/pull/3555#issuecomment-226564908 */ @@ -376,7 +376,7 @@ static void test_rename_process_now(const char *p, int ret) { if (r < 0) return; -#ifdef HAVE_VALGRIND_VALGRIND_H +#if HAVE_VALGRIND_VALGRIND_H /* see above, valgrind is weird, we can't verify what we are doing here */ if (RUNNING_ON_VALGRIND) return; diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index ec5682e66..fc00845ab 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -21,7 +21,7 @@ #include <string.h> #include <unistd.h> -#ifdef HAVE_AUDIT +#if HAVE_AUDIT #include <libaudit.h> #endif @@ -30,10 +30,12 @@ //#include "alloc-util.h" #include "bus-error.h" //#include "bus-util.h" -//#include "formats-util.h" +//#include "format-util.h" //#include "log.h" //#include "macro.h" +#include "process-util.h" //#include "special.h" +#include "strv.h" //#include "unit-name.h" //#include "util.h" #include "utmp-wtmp.h" @@ -44,7 +46,7 @@ #include "update-utmp.h" typedef struct Context { sd_bus *bus; -#ifdef HAVE_AUDIT +#if HAVE_AUDIT int audit_fd; #endif } Context; @@ -129,7 +131,7 @@ static int on_reboot(Context *c) { /* We finished start-up, so let's write the utmp * record and send the audit msg */ -#ifdef HAVE_AUDIT +#if HAVE_AUDIT if (c->audit_fd >= 0) if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM) { @@ -162,7 +164,7 @@ static int on_shutdown(Context *c) { /* We started shut-down, so let's write the utmp * record and send the audit msg */ -#ifdef HAVE_AUDIT +#if HAVE_AUDIT if (c->audit_fd >= 0) if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM) { @@ -207,7 +209,7 @@ static int on_runlevel(Context *c) { if (previous == runlevel) return 0; -#ifdef HAVE_AUDIT +#if HAVE_AUDIT if (c->audit_fd >= 0) { _cleanup_free_ char *s = NULL; @@ -237,7 +239,7 @@ int main(int argc, char *argv[]) { void update_utmp(int argc, char* argv[], sd_bus *bus) { #endif // 0 Context c = { -#ifdef HAVE_AUDIT +#if HAVE_AUDIT .audit_fd = -1 #endif }; @@ -265,7 +267,7 @@ void update_utmp(int argc, char* argv[], sd_bus *bus) { assert(bus); #endif // 0 -#ifdef HAVE_AUDIT +#if HAVE_AUDIT /* If the kernel lacks netlink or audit support, * don't worry about it. */ c.audit_fd = audit_open(); @@ -303,7 +305,7 @@ finish: else if (streq(argv[1], "shutdown")) (void)on_shutdown(&c); #endif // 0 -#ifdef HAVE_AUDIT +#if HAVE_AUDIT if (c.audit_fd >= 0) audit_close(c.audit_fd); #endif |