[sheepdog] [PATCH 2/2] modify util to build sheepdog on rhel5 environment
Liu Yuan
namei.unix at gmail.com
Wed Aug 21 07:18:40 CEST 2013
On Wed, Aug 21, 2013 at 01:02:36PM +0800, Robin Dong wrote:
> From: Robin Dong <sanbai at taobao.com>
>
> For using timerfd, eventfd, signalfd, it need to use syscall() instead of glibc's
> standard fucntions on rhel5.
>
> In gcc-4.1.2, it dose not support attribute like "constructor(priority)" (it only support
> contstructor without 'priority'), so call regist_json/server/default_log_formatter in
> init_log_formatter() directly.
It is better to split logger change as a separate patch.
>
> Signed-off-by: Robin Dong <sanbai at taobao.com>
> ---
> include/bitops.h | 6 +-
> include/util.h | 54 +++++++++++++-
> lib/event.c | 1 -
> lib/logger.c | 185 +++++++++++++++++++++++----------------------
> lib/util.c | 47 ++++++++++++-
> lib/work.c | 1 -
> sheep/cluster/local.c | 1 -
> sheep/cluster/shepherd.c | 1 -
sheep/cluster/zookeeper.c need to replace eventfd.h too
> sheep/sheep.c | 1 -
> shepherd/shepherd.c | 1 -
> 10 files changed, 197 insertions(+), 101 deletions(-)
>
> diff --git a/include/bitops.h b/include/bitops.h
> index c2ff3e7..c4e8f74 100644
> --- a/include/bitops.h
> +++ b/include/bitops.h
> @@ -157,7 +157,7 @@ static inline void clear_bit(unsigned int nr, unsigned long *addr)
> * set bit if value is nonzero. The last (most significant) bit is
> * at position 64.
> */
> -#if __SIZEOF_LONG__ == 4
> +#if SIZEOF_LONG == 4
> static __always_inline int fls64(uint64_t x)
> {
> uint32_t h = x >> 32;
> @@ -169,7 +169,7 @@ static __always_inline int fls64(uint64_t x)
> return 64 - __builtin_clzl(h);
> return 32 - __builtin_clzl(x);
> }
> -#elif __SIZEOF_LONG__ == 8
> +#elif SIZEOF_LONG == 8
> static __always_inline int fls64(uint64_t x)
> {
> if (x == 0)
> @@ -177,7 +177,7 @@ static __always_inline int fls64(uint64_t x)
> return 64 - __builtin_clzl(x);
> }
> #else
> -#error __SIZEOF_LONG__ not 4 or 8
> +#error SIZEOF_LONG not 4 or 8
> #endif
>
> #endif /* __BITOPS_H__ */
> diff --git a/include/util.h b/include/util.h
> index 9e6b959..f104e17 100644
> --- a/include/util.h
> +++ b/include/util.h
> @@ -10,14 +10,66 @@
> #include <unistd.h>
> #include <search.h>
> #include <urcu/uatomic.h>
> -#include <sys/eventfd.h>
> +#include <linux/types.h>
> #include <pthread.h>
> #include <errno.h>
>
> +#include "config.h"
> #include "logger.h"
> #include "list.h"
> #include "compiler.h"
>
> +#ifdef HAVE_SYS_SIGNALFD_H
> +#include <sys/signalfd.h>
> +#else
> +#define SFD_NONBLOCK (04000)
> +struct signalfd_siginfo {
> + __u32 ssi_signo;
Can we use uint32 and int32_t here? Then we don't need include linux specifici
<linux/types.h>.
> + __s32 ssi_errno;
> + __s32 ssi_code;
> + __u32 ssi_pid;
> + __u32 ssi_uid;
> + __s32 ssi_fd;
> + __u32 ssi_tid;
> + __u32 ssi_band;
> + __u32 ssi_overrun;
> + __u32 ssi_trapno;
> + __s32 ssi_status;
> + __s32 ssi_int;
> + __u64 ssi_ptr;
> + __u64 ssi_utime;
> + __u64 ssi_stime;
> + __u64 ssi_addr;
> + __u16 ssi_addr_lsb;
> + __u8 __pad[46];
> +};
> +int signalfd(int __fd, const sigset_t *__mask, int __flags);
> +#endif
> +
> +#ifdef HAVE_SYS_EVENTFD_H
> +#include <sys/eventfd.h>
> +#else
> +#define EFD_SEMAPHORE (1)
> +#define EFD_NONBLOCK (04000)
> +#define eventfd_t uint64_t
> +int eventfd_write(int fd, eventfd_t value);
> +int eventfd_read(int fd, eventfd_t *value);
> +int eventfd(unsigned int initval, int flags);
> +#endif
> +
> +#ifdef HAVE_SYS_TIMERFD_H
> +#include <sys/timerfd.h>
> +#else
> +#define TFD_NONBLOCK (04000)
> +int timerfd_create(clockid_t __clock_id, int __flags);
> +int timerfd_settime(int __ufd, int __flags, __const struct itimerspec *__utmr,
> + struct itimerspec *__otmr);
> +#endif
> +
> +#ifndef HAVE_FALLOCATE
> +int fallocate(int fd, int mode, __off_t offset, __off_t len);
> +#endif
> +
> #define SECTOR_SIZE (1U << 9)
> #define BLOCK_SIZE (1U << 12)
>
> diff --git a/lib/event.c b/lib/event.c
> index 2143c5e..55f94a2 100644
> --- a/lib/event.c
> +++ b/lib/event.c
> @@ -14,7 +14,6 @@
> #include <string.h>
> #include <unistd.h>
> #include <sys/epoll.h>
> -#include <sys/timerfd.h>
>
> #include "list.h"
> #include "util.h"
> diff --git a/lib/logger.c b/lib/logger.c
> index 6c2f96f..531721a 100644
> --- a/lib/logger.c
> +++ b/lib/logger.c
> @@ -102,7 +102,7 @@ struct log_format {
> };
>
> #define log_format_register(n, formatter_fn) \
> - static void __attribute__((constructor(101))) \
> + static void \
> regist_ ## formatter_fn(void) { \
> static struct log_format f = \
> { .name = n, .formatter = formatter_fn }; \
> @@ -141,95 +141,6 @@ static const char *format_thread_name(char *str, size_t size, const char *name,
> return str;
> }
>
> -/*
> - * We need to set default log formatter because dog doesn't want to call
> - * select_log_formatter().
> - */
> -static void __attribute__((constructor(65535)))
> -init_log_formatter(void)
> -{
> - struct log_format *f;
> -
> - list_for_each_entry(f, &log_formats, list) {
> - if (!strcmp(f->name, "default")) {
> - format = f;
> - return;
> - }
> - }
> - syslog(LOG_ERR, "failed to set default formatter\n");
> - exit(1);
> -}
> -
> -static int logarea_init(int size)
> -{
> - int shmid;
> -
> - shmid = shmget(IPC_PRIVATE, sizeof(struct logarea),
> - 0644 | IPC_CREAT | IPC_EXCL);
> - if (shmid == -1) {
> - syslog(LOG_ERR, "shmget logarea failed: %m");
> - return 1;
> - }
> -
> - la = shmat(shmid, NULL, 0);
> - if (!la) {
> - syslog(LOG_ERR, "shmat logarea failed: %m");
> - return 1;
> - }
> -
> - shmctl(shmid, IPC_RMID, NULL);
> -
> - if (size < MAX_MSG_SIZE)
> - size = LOG_SPACE_SIZE;
> -
> - shmid = shmget(IPC_PRIVATE, size, 0644 | IPC_CREAT | IPC_EXCL);
> - if (shmid == -1) {
> - syslog(LOG_ERR, "shmget msg failed: %m");
> - shmdt(la);
> - return 1;
> - }
> -
> - la->start = shmat(shmid, NULL, 0);
> - if (!la->start) {
> - syslog(LOG_ERR, "shmat msg failed: %m");
> - shmdt(la);
> - return 1;
> - }
> - memset(la->start, 0, size);
> -
> - shmctl(shmid, IPC_RMID, NULL);
> -
> - la->end = la->start + size;
> - la->tail = la->start;
> -
> - la->semid = semget(semkey, 1, 0666 | IPC_CREAT);
> - if (la->semid < 0) {
> - syslog(LOG_ERR, "semget failed: %m");
> - shmdt(la->start);
> - shmdt(la);
> - return 1;
> - }
> -
> - la->semarg.val = 1;
> - if (semctl(la->semid, 0, SETVAL, la->semarg) < 0) {
> - syslog(LOG_ERR, "semctl failed: %m");
> - shmdt(la->start);
> - shmdt(la);
> - return 1;
> - }
> -
> - return 0;
> -}
> -
> -static void free_logarea(void)
> -{
> - if (log_fd >= 0)
> - close(log_fd);
> - semctl(la->semid, 0, IPC_RMID, la->semarg);
> - shmdt(la->start);
> - shmdt(la);
> -}
> -
> static int server_log_formatter(char *buff, size_t size,
> const struct logmsg *msg)
> {
> @@ -318,6 +229,100 @@ static int json_log_formatter(char *buff, size_t size,
> }
> log_format_register("json", json_log_formatter);
>
> +/*
> + * We need to set default log formatter because dog doesn't want to call
> + * select_log_formatter().
> + */
> +static void __attribute__((constructor))
> +init_log_formatter(void)
> +{
> + struct log_format *f;
> +
> + regist_json_log_formatter();
> + regist_server_log_formatter();
> + regist_default_log_formatter();
> +
> + list_for_each_entry(f, &log_formats, list) {
> + printf("%s\n", f->name);
> + if (!strcmp(f->name, "default")) {
> + format = f;
> + return;
> + }
> + }
> + syslog(LOG_ERR, "failed to set default formatter\n");
> + exit(1);
> +}
> +
> +static int logarea_init(int size)
> +{
> + int shmid;
> +
> + shmid = shmget(IPC_PRIVATE, sizeof(struct logarea),
> + 0644 | IPC_CREAT | IPC_EXCL);
> + if (shmid == -1) {
> + syslog(LOG_ERR, "shmget logarea failed: %m");
> + return 1;
> + }
> +
> + la = shmat(shmid, NULL, 0);
> + if (!la) {
> + syslog(LOG_ERR, "shmat logarea failed: %m");
> + return 1;
> + }
> +
> + shmctl(shmid, IPC_RMID, NULL);
> +
> + if (size < MAX_MSG_SIZE)
> + size = LOG_SPACE_SIZE;
> +
> + shmid = shmget(IPC_PRIVATE, size, 0644 | IPC_CREAT | IPC_EXCL);
> + if (shmid == -1) {
> + syslog(LOG_ERR, "shmget msg failed: %m");
> + shmdt(la);
> + return 1;
> + }
> +
> + la->start = shmat(shmid, NULL, 0);
> + if (!la->start) {
> + syslog(LOG_ERR, "shmat msg failed: %m");
> + shmdt(la);
> + return 1;
> + }
> + memset(la->start, 0, size);
> +
> + shmctl(shmid, IPC_RMID, NULL);
> +
> + la->end = la->start + size;
> + la->tail = la->start;
> +
> + la->semid = semget(semkey, 1, 0666 | IPC_CREAT);
> + if (la->semid < 0) {
> + syslog(LOG_ERR, "semget failed: %m");
> + shmdt(la->start);
> + shmdt(la);
> + return 1;
> + }
> +
> + la->semarg.val = 1;
> + if (semctl(la->semid, 0, SETVAL, la->semarg) < 0) {
> + syslog(LOG_ERR, "semctl failed: %m");
> + shmdt(la->start);
> + shmdt(la);
> + return 1;
> + }
> +
> + return 0;
> +}
> +
> +static void free_logarea(void)
> +{
> + if (log_fd >= 0)
> + close(log_fd);
> + semctl(la->semid, 0, IPC_RMID, la->semarg);
> + shmdt(la->start);
> + shmdt(la);
> +}
> +
> /* this one can block under memory pressure */
> static void log_syslog(const struct logmsg *msg)
> {
> diff --git a/lib/util.c b/lib/util.c
> index 5c57449..a2e34d2 100644
> --- a/lib/util.c
> +++ b/lib/util.c
> @@ -13,7 +13,6 @@
> #include <errno.h>
> #include <stdlib.h>
> #include <sys/syscall.h>
> -#include <sys/types.h>
> #include <dirent.h>
> #include <sys/stat.h>
> #include <unistd.h>
> @@ -25,6 +24,52 @@
>
> #include "util.h"
>
> +#ifndef HAVE_SYS_EVENTFD_H
> +int eventfd_write(int fd, eventfd_t value)
> +{
> + return write(fd, &value, sizeof(eventfd_t)) !=
> + sizeof(eventfd_t) ? -1 : 0;
> +}
> +
> +int eventfd_read(int fd, eventfd_t *value)
> +{
> + return read(fd, value, sizeof(eventfd_t)) !=
> + sizeof(eventfd_t) ? -1 : 0;
> +}
> +
> +int eventfd(unsigned int initval, int flags)
> +{
> + return syscall(__NR_eventfd, initval, flags);
> +}
> +#endif
> +
> +#ifndef HAVE_SYS_SIGNALFD_H
> +int signalfd(int __fd, const sigset_t *__mask, int __flags)
> +{
> + return syscall(__NR_signalfd, __fd, __mask, _NSIG / 8);
> +}
> +#endif
> +
> +#ifndef HAVE_SYS_TIMERFD_H
> +int timerfd_create(clockid_t __clock_id, int __flags)
> +{
> + return syscall(__NR_timerfd_create, CLOCK_MONOTONIC, TFD_NONBLOCK);
> +}
> +
> +int timerfd_settime(int __ufd, int __flags, __const struct itimerspec *__utmr,
> + struct itimerspec *__otmr)
> +{
> + return syscall(__NR_timerfd_settime, __ufd, __flags, __utmr, __otmr);
> +}
> +#endif
> +
> +#ifndef HAVE_FALLOCATE
> +int fallocate(int fd, int mode, __off_t offset, __off_t len)
> +{
> + return syscall(__NR_fallocate, fd, mode, offset, len);
> +}
> +#endif
I'd suggest move these declarations into include/compiler.h
Thanks
Yuan
More information about the sheepdog
mailing list